diff --git a/python/gvec/core/run/abstract.py b/python/gvec/core/run/abstract.py index 641a01e4..ab652108 100644 --- a/python/gvec/core/run/abstract.py +++ b/python/gvec/core/run/abstract.py @@ -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 @@ -207,6 +208,22 @@ 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`. @@ -214,7 +231,8 @@ def _reset_params_to_original(self): 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), diff --git a/test-CI/unit-pygvec/test_convert_wout.py b/test-CI/unit-pygvec/test_convert_wout.py index ce5b4edc..7673a6eb 100644 --- a/test-CI/unit-pygvec/test_convert_wout.py +++ b/test-CI/unit-pygvec/test_convert_wout.py @@ -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)