diff --git a/python/gvec/core/run/fixed_boundary.py b/python/gvec/core/run/fixed_boundary.py index deb93787..71c60662 100644 --- a/python/gvec/core/run/fixed_boundary.py +++ b/python/gvec/core/run/fixed_boundary.py @@ -65,19 +65,35 @@ def _run_stage(self): shutil.rmtree(self.rundir) self.rundir.mkdir() self.logger.debug(f"Created run directory {self.rundir}") - self.state = MinimizationState.new( - self._state_parameters, - rundir=self.rundir, - statefile=self.state.statefile, - stdout_path=self.rundir / "stdout.txt", - redirect_stdout=self.redirect_gvec_stdout, - header=[ - "GVEC parameter file", - f"Auto-generated with `pygvec run` (stage {self.nth_stage})", - f"Created at {datetime.now().isoformat()}", - f"pyGVEC v{gvec.__version__}", - ], - ) + + def new_state(): + return MinimizationState.new( + self._state_parameters, + rundir=self.rundir, + statefile=self.state.statefile, + stdout_path=self.rundir / "stdout.txt", + redirect_stdout=self.redirect_gvec_stdout, + header=[ + "GVEC parameter file", + f"Auto-generated with `pygvec run` (stage {self.nth_stage})", + f"Created at {datetime.now().isoformat()}", + f"pyGVEC v{gvec.__version__}", + ], + ) + + 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) + 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() with gvec.util.chdir(self.rundir): # write parameterfile & run GVEC diff --git a/python/gvec/util/__init__.py b/python/gvec/util/__init__.py index ce437054..3e3e8ea0 100644 --- a/python/gvec/util/__init__.py +++ b/python/gvec/util/__init__.py @@ -13,6 +13,7 @@ ) from .geometry import ( axis_from_boundary, # Needs docstrings + guess_axis, boundary_generator, boundary_generator_cases, check_boundary_direction, diff --git a/python/gvec/util/geometry.py b/python/gvec/util/geometry.py index cf7d5e61..28048241 100644 --- a/python/gvec/util/geometry.py +++ b/python/gvec/util/geometry.py @@ -247,6 +247,76 @@ def axis_from_boundary(parameters: MutableMapping) -> MutableMapping: return parameters2 +def guess_axis( + parameters: MutableMapping, n_rho: int = 50, n_theta: int = 128 +) -> MutableMapping: + """Axis that keeps the Jacobian of the initial interpolation as far from zero as possible. + + In each toroidal plane the initial guess (axis blended into the m = 0 boundary modes like + 1 - rho^2, the other modes scaled like rho^m) has a Jacobian that is linear in the axis + position, so the axis maximising its minimum solves a linear program, as VMEC's guess_axis + does by a grid search. The planes are then fitted with the axis Fourier modes. + """ + from scipy.optimize import linprog + + nfp = parameters.get("nfp", 1) + N = parameters["X1_mn_max"][1] + asym = "X1_b_sin" in parameters or "X2_b_cos" in parameters + rho = np.linspace(1.0 / n_rho, 1.0, n_rho)[:, None] + theta = np.linspace(0, 2 * np.pi, n_theta, endpoint=False)[None, :] + zeta = np.linspace(0, 2 * np.pi / nfp, 2 * N + 2, endpoint=False) + axis = np.zeros((2, zeta.size)) + for k, z in enumerate(zeta): + d = {} + for x, kinds in (("X1", ("cos", "sin")), ("X2", ("sin", "cos"))): + d_rho, d_theta = np.zeros((n_rho, n_theta)), np.zeros((n_rho, n_theta)) + for kind in kinds: + for (m, n), value in parameters.get(f"{x}_b_{kind}", {}).items(): + phase = m * theta - n * nfp * z + f, df = ( + (np.cos(phase), -np.sin(phase)) + 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 + 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 + J0, A, B = ( + ((r1 * t2 - r2 * t1) / rho).ravel(), + (-2 * t2).ravel(), + (2 * t1).ravel(), + ) + sign = np.sign(np.median(J0)) + # maximise the smallest Jacobian t: sign * (J0 + A x1 + B x2) >= t + res = linprog( + [0, 0, -1], + A_ub=np.stack([-sign * A, -sign * B, np.ones_like(A)], axis=1), + b_ub=sign * J0, + bounds=[(None, None), (None, None), (None, np.abs(J0).max())], + method="highs", + ) + axis[:, k] = res.x[:2] + n = np.arange(N + 1) + cos, sin = np.cos(np.outer(nfp * zeta, n)), np.sin(np.outer(nfp * zeta, n)) + parameters2 = deepcopy(parameters) + parameters2.pop("init_average_axis", None) + # the axis modes are cos/sin(-n nfp zeta), so the sine coefficients change sign + for x, row in (("X1", 0), ("X2", 1)): + even, odd = ("cos", "sin") if x == "X1" else ("sin", "cos") + basis = {"cos": cos, "sin": -sin[:, 1:]} + kinds = (even, odd) if asym else (even,) + columns = np.concatenate([basis[kind] for kind in kinds], axis=1) + coefs = np.linalg.lstsq(columns, axis[row], rcond=None)[0] + for kind in kinds: + size = basis[kind].shape[1] + offset = 0 if kind == "cos" else 1 + values, coefs = coefs[:size], coefs[size:] + parameters2[f"{x}_a_{kind}"] = {(0, i + offset): v for i, v in enumerate(values)} + return parameters2 + + def ellipse_circumference_factor(epsilon: float) -> float: """ Compute the circumference factor of an ellipse with elongation epsilon. diff --git a/test-CI/unit-pygvec/test_init_average_axis.py b/test-CI/unit-pygvec/test_init_average_axis.py index 4c0303fe..aee664f2 100644 --- a/test-CI/unit-pygvec/test_init_average_axis.py +++ b/test-CI/unit-pygvec/test_init_average_axis.py @@ -136,3 +136,20 @@ def test_init_average_axis_clockwise_ellipse(parameters, tmp_path): finally: if gvec.core.state._binding.bound_state is state: state.unbind() + + +def test_run_guesses_axis_after_bad_jacobian(parameters, tmp_path): + # an axis near the outer edge makes the initial Jacobian change sign + parameters.update( + init_average_axis=False, + X1_mn_max=(1, 0), + X2_mn_max=(1, 0), + LA_mn_max=(1, 0), + X2_b_sin={(1, 0): 0.9}, + X1_a_cos={(0, 0): 5.8}, + totalIter=5, + ) + guessed = gvec.util.guess_axis(parameters) + 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)