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
20 changes: 19 additions & 1 deletion python/gvec/core/run/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def __init__(
self.parameters["boundary_perturb"] = False
# move init from vmec to initial stage
if self.parameters.get("whichInitEquilibrium", 0) == 1:
self._take_wout_parameters()
self.stages[0]["whichInitEquilibrium"] = 1
self.parameters["whichInitEquilibrium"] = 0

Expand Down Expand Up @@ -207,14 +208,31 @@ def __init__(
project_dir / parameters_stages_name,
)

def _take_wout_parameters(self):
"""Give the stages after the VMEC initialisation the wout's boundary, flux and profiles."""
from gvec.scripts.convert_wout import convert_vmec_wout

wout = convert_vmec_wout(
Path(self.parameters["VMECwoutfile"]),
VMECwoutfile_format=int(self.parameters.get("VMECwoutfile_format", 0)),
)
for key, value in wout.items():
# the discretisation stays as the parameter file sets it for every stage
if (
key.lower() not in ("sgrid", "x1x2_deg", "la_deg")
and key not in self.parameters
):
self.parameters[key] = value

def _reset_params_to_original(self):
"""
Reset the parameters to the original values. Except for `iota` and `maxIter`, which is limited by `totaliter`.
"""
params = self.parameters.copy()
if "stages" in params:
del params["stages"]
params["iota"] = self._state_parameters["iota"]
if "iota" in self._state_parameters:
params["iota"] = self._state_parameters["iota"]
params["maxIter"] = min(
self.totaliter - self.GVEC_iter_used,
self.parameters.get("maxIter", self.totaliter),
Expand Down
28 changes: 28 additions & 0 deletions test-CI/unit-pygvec/test_convert_wout.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,31 @@ def test_converted_wout_state_can_be_reloaded(tmp_path, signgs, asymmetric):
np.testing.assert_allclose(position, [radius, 0.0, height], rtol=1e-10, atol=1e-12)
finally:
converted.unbind()


@pytest.mark.parametrize("asymmetric", ["F", "T"])
def test_run_continues_from_wout(tmp_path, asymmetric):
import gvec

source = Path(__file__).parents[1] / "data" / f"wout_test-signgs-neg-asym{asymmetric}.nc"
parameters = dict(
ProjectName="wout",
whichInitEquilibrium=1,
VMECwoutfile=str(source),
init_LA=False,
sgrid_nElems=4,
X1X2_deg=5,
LA_deg=5,
totalIter=2,
minimize_tol=1e-12,
)
run = gvec.run(parameters, runpath=tmp_path, quiet=True)
position = np.asarray(run.state.evaluate("pos", rho=1.0, theta=0.0, zeta=0.0).pos).reshape(
3
)
# the stages after the initialisation keep the wout's fixed boundary
with netCDF4.Dataset(source) as wout:
radius = np.sum(wout["rmnc"][-1, :])
height = np.sum(wout["zmnc"][-1, :]) if "zmnc" in wout.variables else 0.0
assert run.state.nfp == int(wout["nfp"][:])
np.testing.assert_allclose(position, [radius, 0.0, height], rtol=1e-10, atol=1e-12)