Skip to content
Merged
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
9 changes: 9 additions & 0 deletions docs/generators/parameters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ init_average_axis:
type: '`logical`'
allowed: '`True` or `False`'
default: '`False`'
init_low_radial_power:
category:
- boundary
description:
- In the initial guess, scale the boundary modes with $m\geq 2$ like $\rho^2$ (even $m$) or $\rho^3$ (odd $m$)
instead of $\rho^m$, which keeps strongly shaped boundaries from folding over near the edge.
type: '`logical`'
allowed: '`True` or `False`'
default: '`False`'
average_axis_move_X1:
category:
- boundary
Expand Down
29 changes: 17 additions & 12 deletions python/gvec/core/run/fixed_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,24 @@ def new_state():
],
)

try:
self.state = new_state()
except gvec.errors.InitializationError as error:
if "JACOBIAN" not in str(error) or self.state.statefile:
raise
# as VMEC does after a bad initial Jacobian: guess the axis and start again
self.logger.warning(f"{error} Retrying with a guessed axis.")
guessed = gvec.util.guess_axis(self._state_parameters)
for params in (self.parameters, self._state_parameters):
params.pop("init_average_axis", None)
# as VMEC does after a bad initial Jacobian: guess the axis and start again,
# then also lower the radial power of the shaping modes in the initial guess
for retry in (None, {}, {"init_low_radial_power": True}):
if retry is not None:
for params in (self.parameters, self._state_parameters):
params.pop("init_average_axis", None)
params.update(retry)
guessed = gvec.util.guess_axis(self._state_parameters)
axis_keys = ("X1_a_cos", "X1_a_sin", "X2_a_cos", "X2_a_sin")
params.update({key: guessed[key] for key in axis_keys if key in guessed})
self.state = new_state()
for params in (self.parameters, self._state_parameters):
params.update({key: guessed[key] for key in axis_keys if key in guessed})
try:
self.state = new_state()
break
except gvec.errors.InitializationError as error:
if "JACOBIAN" not in str(error) or self.state.statefile or retry:
raise
self.logger.warning(f"{error} Retrying with a guessed axis.")

with gvec.util.chdir(self.rundir):
# write parameterfile & run GVEC
Expand Down
12 changes: 10 additions & 2 deletions python/gvec/util/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,16 @@ def guess_axis(
if kind == "cos"
else (np.sin(phase), np.cos(phase))
)
d_rho = d_rho + value * f * (2 * rho if m == 0 else m * rho ** (m - 1))
d_theta = d_theta + value * m * df * rho**m
# init_low_radial_power: modes m >= 2 scale like rho^2 or rho^3, by parity
power = (
min(m, 2 + m % 2)
if parameters.get("init_low_radial_power", False)
else m
)
d_rho = d_rho + value * f * (
2 * rho if m == 0 else power * rho ** (power - 1)
)
d_theta = d_theta + value * m * df * rho**power
d[x] = d_rho, d_theta
(r1, t1), (r2, t2) = d["X1"], d["X2"]
# Jacobian / rho = J0 - 2 X1_axis dX2/dtheta + 2 X2_axis dX1/dtheta
Expand Down
17 changes: 14 additions & 3 deletions src/functionals/mhd3d/mhd3d.F90
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ SUBROUTINE InitMHD3D(sf)
!INITIALIZATION PARAMETERS (ONLY NECESSARY ON MPIroot)
IF(MPIroot)THEN
init_average_axis= GETLOGICAL("init_average_axis",Proposal=.FALSE.)
init_low_radial_power= GETLOGICAL("init_low_radial_power",Proposal=.FALSE.)
IF(init_average_axis)THEN
average_axis_move(1) = GETREAL("average_axis_move_X1",Proposal=0.0_wp)
average_axis_move(2) = GETREAL("average_axis_move_X2",Proposal=0.0_wp)
Expand Down Expand Up @@ -940,7 +941,7 @@ END SUBROUTINE InitAverageAxis
SUBROUTINE InitSolution(U_init,which_init_in)
! MODULES
USE MODgvec_Globals, ONLY:ProgressBar,getTime
USE MODgvec_MHD3D_Vars , ONLY:init_fromBConly,init_BC,init_average_axis,average_axis_move
USE MODgvec_MHD3D_Vars , ONLY:init_fromBConly,init_BC,init_average_axis,average_axis_move,init_low_radial_power
USE MODgvec_MHD3D_Vars , ONLY:X1_base,X1_BC_Type,X1_a,X1_b
USE MODgvec_MHD3D_Vars , ONLY:X2_base,X2_BC_Type,X2_a,X2_b
USE MODgvec_MHD3D_Vars , ONLY:LA_base,LA_BC_Type,init_LA
Expand Down Expand Up @@ -1093,7 +1094,7 @@ SUBROUTINE InitSolution(U_init,which_init_in)
X1_gIP(:,iMode)=s_IP(:)*X1_b(iMode) ! first odd mode ~s
CASE DEFAULT ! ~s^m
X1_gIP(:,iMode)=s_IP(:)*X1_b(iMode)
DO i_m=2,X1_base%f%Xmn(1,iMode)
DO i_m=2,radial_power(X1_base%f%Xmn(1,iMode))
X1_gIP(:,iMode)=X1_gIP(:,iMode)*s_IP(:)
END DO
END SELECT !X1(:,iMode) zero odd even
Expand All @@ -1111,7 +1112,7 @@ SUBROUTINE InitSolution(U_init,which_init_in)
X2_gIP(:,iMode)=s_IP(:)*X2_b(iMode) ! first odd mode ~s
CASE DEFAULT ! ~s^m
X2_gIP(:,iMode)=s_IP(:)*X2_b(iMode)
DO i_m=2,X2_base%f%Xmn(1,iMode)
DO i_m=2,radial_power(X2_base%f%Xmn(1,iMode))
X2_gIP(:,iMode)=X2_gIP(:,iMode)*s_IP(:)
END DO
END SELECT !X2(:,iMode) zero odd even
Expand Down Expand Up @@ -1173,6 +1174,16 @@ SUBROUTINE InitSolution(U_init,which_init_in)
END DO !iMode

END ASSOCIATE !LA
CONTAINS

!> power of rho with which a boundary mode m enters the initial guess
PURE INTEGER FUNCTION radial_power(m)
INTEGER, INTENT(IN) :: m
radial_power = m
! a lower power keeps strongly shaped boundaries from folding over near the edge
IF(init_low_radial_power) radial_power = MIN(m, 2 + MOD(m, 2))
END FUNCTION radial_power

END SUBROUTINE InitSolution


Expand Down
1 change: 1 addition & 0 deletions src/functionals/mhd3d/mhd3d_vars.F90
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ MODULE MODgvec_MHD3D_Vars
LOGICAL :: init_fromBCOnly !! default=TRUE, for VMEC only, if set false: initial mapping is interpolated for s=0..1
LOGICAL :: init_with_profile_pressure !! default=FALSE, if True, overwrite profile from VMEC ini using profile from parameterfile
LOGICAL :: init_with_profile_iota !! default=FALSE, if True, overwrite profile from VMEC ini using profile from parameterfile
LOGICAL :: init_low_radial_power !! default=FALSE, if true, the initial guess scales modes m>=2 like rho^2 (even m) or rho^3 (odd m) instead of rho^m
LOGICAL :: init_average_axis !! default=FALSE, if true, use outer boundary to estimate axis position (center of closed line)
LOGICAL :: boundary_perturb !! default=FALSE, if true, mapping is perturbed with a given modal perturbation of the boundary (X1pert_b,X2pert_b)
INTEGER :: boundary_perturb_type
Expand Down
20 changes: 20 additions & 0 deletions test-CI/unit-pygvec/test_init_average_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,23 @@ def test_run_guesses_axis_after_bad_jacobian(parameters, tmp_path):
np.testing.assert_allclose(guessed["X1_a_cos"][0, 0], 5.0, atol=1e-6)
run = gvec.run(parameters, runpath=tmp_path, quiet=True)
assert np.isfinite(run.max_force)


def test_run_lowers_radial_power_after_bad_jacobian(parameters, tmp_path):
# STELLOPT's belt pinch folds over near the edge for every axis when modes scale like rho^m
parameters.update(
init_average_axis=False,
X1_mn_max=(5, 0),
X2_mn_max=(5, 0),
LA_mn_max=(5, 0),
X1_b_cos={(0, 0): 3.0, (1, 0): 0.465, (3, 0): -0.0158},
X2_b_sin={(1, 0): 0.6, (3, 0): 0.177, (5, 0): -0.0197},
X1_a_cos={(0, 0): 3.0},
sgrid_nElems=5,
X1X2_deg=5,
LA_deg=5,
totalIter=5,
)
run = gvec.run(parameters, runpath=tmp_path, quiet=True)
assert run.state.parameters["init_low_radial_power"]
assert np.isfinite(run.max_force)