Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/stability.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]`.
Expand Down Expand Up @@ -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).
Expand Down
1 change: 0 additions & 1 deletion examples/DIIID-like_error_field_example/gpec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion examples/DIIID-like_ideal_example/gpec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion examples/DIIID-like_ideal_multi_n_example/gpec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion examples/Solovev_ideal_example/gpec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/a10_kinetic_example/gpec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/ForceFreeStates/EulerLagrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`.

Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/ForceFreeStates/Free.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/ForceFreeStates/Integrators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/ForceFreeStates/Result.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion test/runtests_eulerlagrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions test/runtests_fullruns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion test/test_data/regression_solovev_ideal_example/gpec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion test/test_data/regression_solovev_kinetic_nuzero/gpec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading