diff --git a/docs/src/stability.md b/docs/src/stability.md index aa86df2ecb..ec32d8c1e7 100644 --- a/docs/src/stability.md +++ b/docs/src/stability.md @@ -66,7 +66,7 @@ rational_offset_fraction = 0.2 [Forward] eulerlagrange_tolerance = 1e-10 save_interval = 1 # save every Nth ODE step; steps near rational surfaces are always saved -renorm_threshold = 1e3 # renormalize the solution basis above this column norm +renorm_threshold = 1e6 # Gaussian reduction when the condition number of u₁ exceeds this ``` `examples/DIIID-like_riccati_deltaprime_example` sets the same three keys under `[Riccati]`. @@ -138,7 +138,7 @@ The perpendicular diffusivities that set the layer widths are described with the - The Riccati integrator splits the domain into chunks whose count is derived from the number of singular surfaces (`[Riccati] nchunks` overrides the target, `0` is automatic); every chunk integrates from identity initial conditions, so the result is independent of the thread count. - The chunk ending at each rational surface is integrated backward, from the surface inward, because the forward propagator is ill-conditioned there; the Δ′ boundary-value problem is assembled from the split forward and backward propagators for the same reason. -- Both integrators use the adaptive ninth-order `Vern9` solver at `eulerlagrange_tolerance`; the Riccati basis is recovered by renormalizing whenever the solution exceeds `renorm_threshold`. +- Both integrators use the adaptive ninth-order `Vern9` solver at `eulerlagrange_tolerance`; Forward reduces the basis when the condition number of u₁ exceeds `renorm_threshold`; Riccati renormalizes when an entry exceeds it. - The two crossing conventions differ slightly, so Riccati and Forward free-boundary energies agree to within a few percent, not to machine precision. Local stability (Mercier, resistive interchange and ballooning) is a separate stage on the same equilibrium; see [Local Stability](ballooning.md). diff --git a/examples/DIIID-like_error_field_example/gpec.toml b/examples/DIIID-like_error_field_example/gpec.toml index 070ffa8172..62a77ad317 100644 --- a/examples/DIIID-like_error_field_example/gpec.toml +++ b/examples/DIIID-like_error_field_example/gpec.toml @@ -68,7 +68,6 @@ rational_offset_fraction = 0.2 # Truncate integration at (last_rational_q + rati [Forward] eulerlagrange_tolerance = 1e-10 # Relative tolerance for ODE integration of Euler-Lagrange equations save_interval = 1 # Save every Nth ODE step (1=all). Always saves near rational surfaces. -renorm_threshold = 1e3 # Column-norm threshold that triggers solution renormalization [ForcingTerms] forcing_data_format = "coil" # Format: "ascii", "hdf5", or "coil" (Biot-Savart from 3D wires) diff --git a/examples/DIIID-like_ideal_example/gpec.toml b/examples/DIIID-like_ideal_example/gpec.toml index e878691667..9211ef5dc8 100644 --- a/examples/DIIID-like_ideal_example/gpec.toml +++ b/examples/DIIID-like_ideal_example/gpec.toml @@ -64,7 +64,6 @@ rational_offset_fraction = 0.2 # Truncate integration at (last_rational_q + rati [Forward] eulerlagrange_tolerance = 1e-10 # Relative tolerance for ODE integration of Euler-Lagrange equations save_interval = 1 # Save every Nth ODE step (1=all). Always saves near rational surfaces. -renorm_threshold = 1e3 # Column-norm threshold that triggers solution renormalization [ForcingTerms] forcing_data_format = "coil" # Format: "ascii", "hdf5", or "coil" (Biot-Savart from 3D wires) diff --git a/examples/DIIID-like_ideal_multi_n_example/gpec.toml b/examples/DIIID-like_ideal_multi_n_example/gpec.toml index c6bfa903ee..12ff122d0d 100644 --- a/examples/DIIID-like_ideal_multi_n_example/gpec.toml +++ b/examples/DIIID-like_ideal_multi_n_example/gpec.toml @@ -57,7 +57,6 @@ rational_offset_fraction = 0.2 # Truncate integration at (last_rational_q + rati [Forward] eulerlagrange_tolerance = 1e-10 # Relative tolerance for ODE integration of Euler-Lagrange equations save_interval = 1 # Save every Nth ODE step (1=all). Always saves near rational surfaces. -renorm_threshold = 1e3 # Column-norm threshold that triggers solution renormalization [ForcingTerms] forcing_data_format = "coil" # Format: "ascii", "hdf5", or "coil" (Biot-Savart from 3D wires) diff --git a/examples/Solovev_ideal_example/gpec.toml b/examples/Solovev_ideal_example/gpec.toml index f4fabe17a9..93ca3234cf 100644 --- a/examples/Solovev_ideal_example/gpec.toml +++ b/examples/Solovev_ideal_example/gpec.toml @@ -67,7 +67,6 @@ rational_offset_fraction = 0.2 # Truncate integration at (last_rational_q + rati # Numerical settings of the forward integrator [Forward] eulerlagrange_tolerance = 1e-7 # Relative tolerance for ODE integration of Euler-Lagrange equations -renorm_threshold = 1e3 # Column-norm threshold that triggers solution renormalization save_interval = 1 # Save every Nth ODE step (1=all). Always saves near rational surfaces. # Solovev analytic equilibrium parameters; this section selects the equilibrium. See SolovevConfig in src/Equilibrium. diff --git a/examples/a10_kinetic_example/gpec.toml b/examples/a10_kinetic_example/gpec.toml index 24b0c408f1..299f48ad3c 100644 --- a/examples/a10_kinetic_example/gpec.toml +++ b/examples/a10_kinetic_example/gpec.toml @@ -62,7 +62,6 @@ singfac_min = 1e-6 # Fractional distance from rational q at which id # Numerical settings of the forward integrator [Forward] eulerlagrange_tolerance = 1e-7 # Relative tolerance for ODE integration of Euler-Lagrange equations -renorm_threshold = 1e3 # Column-norm threshold that triggers solution renormalization [KineticForces] bounce_harmonic_max = 6 # Bounce-harmonic number diff --git a/src/ForceFreeStates/EulerLagrange.jl b/src/ForceFreeStates/EulerLagrange.jl index beed25f0cd..43fa02e309 100644 --- a/src/ForceFreeStates/EulerLagrange.jl +++ b/src/ForceFreeStates/EulerLagrange.jl @@ -300,7 +300,7 @@ end forward_eulerlagrange_integration(ctrl, equil, mats, intr; verbose=intr.verbose) -> (odet, nothing, nothing, nothing) Forward branch of [`eulerlagrange_integration`](@ref): integrates chunk by chunk from the axis, -applying Gaussian reduction whenever a solution norm ratio exceeds `ctrl.algorithm.renorm_threshold` and undoing it +applying Gaussian reduction whenever the condition number of u₁ exceeds `ctrl.algorithm.renorm_threshold` and undoing it via `transform_u!` at the end, so `odet.u_store` comes back dense in the axis basis. Call directly to force this branch regardless of `ctrl.algorithm`; `verbose` overrides `intr.verbose` for progress logging. @@ -803,8 +803,8 @@ end """ compute_solution_norms!(u::Array{ComplexF64,3}, odet::OdeState, ctrl::ForceFreeStatesControl, intr::ForceFreeStatesInternal, sing_flag::Bool; resonant_rows=Int[]) -Compute the column norms of u₁ and apply `apply_gaussian_reduction!` to `u` in place when their -growth since the last reduction exceeds `ctrl.algorithm.renorm_threshold`, and always when `sing_flag` is set. +Compute the column norms of u₁ and apply `apply_gaussian_reduction!` to `u` in place when the condition +number of u₁ with unit columns exceeds `ctrl.algorithm.renorm_threshold`, and always when `sing_flag` is set. The first call after a reduction records reference norms; if `sing_flag` forces a reduction there, it keeps the previous reduction's pivot order. Errors if any norm is zero. Port of Fortran `ode_unorm`. @@ -823,7 +823,7 @@ function compute_solution_norms!(u::Array{ComplexF64,3}, odet::OdeState, ctrl::F error("One of the first solution vector norms unorm(1,$jmax) = 0") end - # A singular surface always reduces; otherwise reduce when the growth since the last reduction is too uneven + # A singular surface always reduces; otherwise reduce when u₁'s columns lose independence fix_required = sing_flag if odet.new odet.new = false @@ -833,8 +833,8 @@ function compute_solution_norms!(u::Array{ComplexF64,3}, odet::OdeState, ctrl::F @views odet.unorm[odet.index[:, odet.ifix]] .= (intr.numpert_total-1):-1:0 end else - odet.unorm ./= odet.unorm0 - fix_required |= maximum(odet.unorm) / minimum(odet.unorm) > ctrl.algorithm.renorm_threshold + fix_required |= @views(cond(u[:, :, 1] ./ transpose(odet.unorm), 1)) > ctrl.algorithm.renorm_threshold + odet.unorm ./= odet.unorm0 # growth since the last reduction still sets the pivot order end if fix_required diff --git a/src/ForceFreeStates/Free.jl b/src/ForceFreeStates/Free.jl index 0d971fa07f..e3bc809b3b 100644 --- a/src/ForceFreeStates/Free.jl +++ b/src/ForceFreeStates/Free.jl @@ -86,6 +86,14 @@ downstream code consumes the stored ξ profiles. return odet end +""" + plasma_energy_matrix(u, psio) + +Plasma energy matrix W_p = u₂u₁⁻¹/ψ₀² by pivoted LU of u₁. `u₂ / u₁` factors u₁ᴴ, whose rows carry the column-norm spread, +so partial pivoting fails. +""" +plasma_energy_matrix(u::Array{ComplexF64,3}, psio::Real) = @views (u[:, :, 2] / lu(u[:, :, 1])) ./ psio^2 + """ compute_scaled_wv(ctrl::ForceFreeStatesControl, equil::Equilibrium.PlasmaEquilibrium, intr::ForceFreeStatesInternal) -> (wv, vac) @@ -128,7 +136,7 @@ plasma/vacuum/total energies as a [`FreeBoundaryResult`](@ref). # Compute plasma response matrix W = U₂ * U₁⁻¹ wp = zeros(ComplexF64, numpert_total, numpert_total) - @views wp .= (odet.u[:, :, 2] / odet.u[:, :, 1]) ./ equil.psio^2 + wp .= plasma_energy_matrix(odet.u, equil.psio) wv, vac = compute_scaled_wv(ctrl, equil, intr) @@ -267,7 +275,7 @@ NamedTuple `(total_eigenvalue, plasma_energy, vacuum_energy, vacuum_eigenvalue)` dV_dpsi = equil.profiles.dVdpsi_spline(odet.psifac) # Compute plasma response matrix - @views wp .= (odet.u[:, :, 2] / odet.u[:, :, 1]) ./ equil.psio^2 + wp .= plasma_energy_matrix(odet.u, equil.psio) # Retrieve raw vacuum matrix from spline and apply singfac analytically at the local q. # Singfac is not pre-applied in the spline (see free_compute_wv_spline) to avoid interpolating diff --git a/src/ForceFreeStates/Integrators.jl b/src/ForceFreeStates/Integrators.jl index 470c0030f4..de584ac376 100644 --- a/src/ForceFreeStates/Integrators.jl +++ b/src/ForceFreeStates/Integrators.jl @@ -14,14 +14,14 @@ Serial forward integration with Gaussian reduction and dense solution profiles. ## Fields - `eulerlagrange_tolerance::Float64` - Relative tolerance for ODE integration. - - `renorm_threshold::Float64` - Column-norm growth threshold for Gaussian reduction or Riccati renormalization. + - `renorm_threshold::Float64` - Gaussian reduction fires when the condition number of u₁, columns normalized, exceeds this. - `numsteps_init::Int` - Initial capacity for stored ODE solution steps. - `numunorms_init::Int` - Initial capacity for solution-normalization records; grown on demand. - `save_interval::Int` - Save every Nth ODE step; always retain steps near rational surfaces. """ @kwdef struct Forward <: AbstractIntegrator eulerlagrange_tolerance::Float64 = 1e-8 - renorm_threshold::Float64 = 1e3 + renorm_threshold::Float64 = 1e6 numsteps_init::Int = 4000 numunorms_init::Int = 100 save_interval::Int = 1 @@ -35,7 +35,7 @@ Chunked Riccati integration and the singular-surface boundary-value solve. ## Fields - `eulerlagrange_tolerance::Float64` - Relative tolerance for ODE integration. - - `renorm_threshold::Float64` - Column-norm growth threshold for Gaussian reduction or Riccati renormalization. + - `renorm_threshold::Float64` - Entry size above which the Riccati state is renormalized. - `numsteps_init::Int` - Initial capacity for stored ODE solution steps. - `numunorms_init::Int` - Initial capacity for solution-normalization records; grown on demand. - `save_interval::Int` - Save every Nth ODE step; always retain steps near rational surfaces. diff --git a/src/ForceFreeStates/Result.jl b/src/ForceFreeStates/Result.jl index 2d2affa457..66773dd5a6 100644 --- a/src/ForceFreeStates/Result.jl +++ b/src/ForceFreeStates/Result.jl @@ -227,7 +227,7 @@ function build_result( wp = if free_energies !== nothing free_energies.wp elseif odet !== nothing - (odet.u[:, :, 2] / odet.u[:, :, 1]) ./ equil.psio^2 + plasma_energy_matrix(odet.u, equil.psio) else nothing end diff --git a/test/runtests_eulerlagrange.jl b/test/runtests_eulerlagrange.jl index b2627b704d..a58dc1ea0b 100644 --- a/test/runtests_eulerlagrange.jl +++ b/test/runtests_eulerlagrange.jl @@ -320,9 +320,10 @@ end @test all(tol[:, 2, 2] .≈ 1e-14) @test FFS.column_abstol!(tol, u, 1e-6) === tol @test all(FFS.column_abstol(zeros(ComplexF64, 2, 2, 2), 1e-6) .> 0) - # Pin the defaults: every shipped deck sets both, so the harness would not catch a revert + # Pin the defaults: the harness decks do not exercise them, so it would not catch a revert c = FFS.ForceFreeStatesControl() @test c.algorithm.renorm_threshold == 1e3 + @test Forward().renorm_threshold == 1e6 @test c.algorithm.save_interval == 1 end @@ -384,6 +385,16 @@ end @test_throws ErrorException FFS.move_start_off_rational!(intr, ctrl) end + @testset "plasma_energy_matrix pivots on u₁" begin + # Graded columns: `u₂ / u₁` is off by ~0.15 here, the LU of u₁ by ~1e-16 + FFS = GeneralizedPerturbedEquilibrium.ForceFreeStates + L = [i == j ? 1.0 + 0im : i > j ? 0.8cis(i + 2j) : 0.0im for i in 1:4, j in 1:4] + U1 = (L .* exp10.(range(110, 0; length=4))')[4:-1:1, :] + A = [cis(0.3i * j) / (i + j) for i in 1:4, j in 1:4] + W = A + A' + @test FFS.plasma_energy_matrix(cat(U1, W * U1; dims=3), 2.0) ≈ W / 4 rtol = 1e-12 + end + @testset "compute_solution_norms!" begin mpert = 2 odet = GeneralizedPerturbedEquilibrium.ForceFreeStates.OdeState(mpert, 10, 10, 10) @@ -421,6 +432,16 @@ end GeneralizedPerturbedEquilibrium.ForceFreeStates.compute_solution_norms!(odet.u, odet, ctrl, intr, false) @test odet.new == true # implies fixup ran + # Case 4b: a large norm spread with independent columns does not reduce; nearly parallel columns do + odet.new = false + odet.unorm0 = ones(intr.mpert) + odet.u[:, :, 1] .= [1e6 0; 0 1] + GeneralizedPerturbedEquilibrium.ForceFreeStates.compute_solution_norms!(odet.u, odet, ctrl, intr, false) + @test odet.new == false + odet.u[:, :, 1] .= [1 1; 0 1e-7] + GeneralizedPerturbedEquilibrium.ForceFreeStates.compute_solution_norms!(odet.u, odet, ctrl, intr, false) + @test odet.new == true + # Case 5: Trigger fixup via sing_flag odet.new = false odet.u[:, 1, 1] .= [1, 0] diff --git a/test/runtests_fullruns.jl b/test/runtests_fullruns.jl index 245b7cdf7c..4f7515c76f 100644 --- a/test/runtests_fullruns.jl +++ b/test/runtests_fullruns.jl @@ -16,8 +16,8 @@ using TOML return h5open(h5 -> read(h5["ForceFreeStates/FreeBoundaryStability/eigenmode_energies"])[1], joinpath(workdir, "gpec.h5"), "r") end end - et_ref = total_energy(1e3) - for renorm_threshold in (2.0, 3.0, 5.0, 10.0) + et_ref = total_energy(1.0) # 1.0 reduces every step + for renorm_threshold in (2.0, 3.0, 5.0, 10.0, 1e3, 1e6, 1e8) @test total_energy(renorm_threshold) ≈ et_ref rtol = 1e-6 end end diff --git a/test/test_data/regression_solovev_ideal_example/gpec.toml b/test/test_data/regression_solovev_ideal_example/gpec.toml index b2663d180e..1c9c87b655 100644 --- a/test/test_data/regression_solovev_ideal_example/gpec.toml +++ b/test/test_data/regression_solovev_ideal_example/gpec.toml @@ -52,7 +52,6 @@ rational_offset_fraction = 0.2 # Truncate integration at (las # Numerical settings of the forward integrator [Forward] eulerlagrange_tolerance = 1e-7 # Relative tolerance for ODE integration of Euler-Lagrange equations -renorm_threshold = 1e3 # Column-norm threshold that triggers solution renormalization # Solovev analytic equilibrium parameters; this section selects the equilibrium. See SolovevConfig in src/Equilibrium. [Solovev] diff --git a/test/test_data/regression_solovev_ideal_example_multi_n/gpec.toml b/test/test_data/regression_solovev_ideal_example_multi_n/gpec.toml index 0571479748..4b76b9c722 100644 --- a/test/test_data/regression_solovev_ideal_example_multi_n/gpec.toml +++ b/test/test_data/regression_solovev_ideal_example_multi_n/gpec.toml @@ -52,7 +52,6 @@ rational_offset_fraction = 0.2 # Truncate integration at (las # Numerical settings of the forward integrator [Forward] eulerlagrange_tolerance = 1e-7 # Relative tolerance for ODE integration of Euler-Lagrange equations -renorm_threshold = 1e3 # Column-norm threshold that triggers solution renormalization # Solovev analytic equilibrium parameters; this section selects the equilibrium. See SolovevConfig in src/Equilibrium. [Solovev] diff --git a/test/test_data/regression_solovev_kinetic_calculated/gpec.toml b/test/test_data/regression_solovev_kinetic_calculated/gpec.toml index 9285035b38..dd5288c756 100644 --- a/test/test_data/regression_solovev_kinetic_calculated/gpec.toml +++ b/test/test_data/regression_solovev_kinetic_calculated/gpec.toml @@ -51,7 +51,6 @@ singfac_min = 1e-4 # Fractional distance from rational q at which ide # Numerical settings of the forward integrator [Forward] eulerlagrange_tolerance = 1e-7 # Relative tolerance for ODE integration of Euler-Lagrange equations -renorm_threshold = 1e3 # Column-norm threshold that triggers solution renormalization # Solovev analytic equilibrium parameters; this section selects the equilibrium. See SolovevConfig in src/Equilibrium. [Solovev] diff --git a/test/test_data/regression_solovev_kinetic_nuzero/gpec.toml b/test/test_data/regression_solovev_kinetic_nuzero/gpec.toml index 2e8954e36a..a4f0e40651 100644 --- a/test/test_data/regression_solovev_kinetic_nuzero/gpec.toml +++ b/test/test_data/regression_solovev_kinetic_nuzero/gpec.toml @@ -51,7 +51,6 @@ singfac_min = 1e-4 # Fractional distance from rational q at which ide # Numerical settings of the forward integrator [Forward] eulerlagrange_tolerance = 1e-7 # Relative tolerance for ODE integration of Euler-Lagrange equations -renorm_threshold = 1e3 # Column-norm threshold that triggers solution renormalization [KineticForces] nutype = "zero" # Collisionless operator — isolates the real-x-space collisionless energy integral