Skip to content

Commit

Permalink
bug fix for proximal scalar optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-dudt committed Dec 2, 2024
1 parent 2741269 commit 0d4c331
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
19 changes: 19 additions & 0 deletions desc/optimize/_constraint_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,25 @@ def compute_scaled_error(self, x, constants=None):
xopt, _ = self._update_equilibrium(x, store=False)
return self._objective.compute_scaled_error(xopt, constants[0])

def compute_scalar(self, x, constants=None):
"""Compute the sum of squares error.
Parameters
----------
x : ndarray
State vector.
constants : list
Constant parameters passed to sub-objectives.
Returns
-------
f : float
Objective function scalar value.
"""
f = jnp.sum(self.compute_scaled_error(x, constants=constants) ** 2) / 2
return f

Check warning on line 856 in desc/optimize/_constraint_wrappers.py

View check run for this annotation

Codecov / codecov/patch

desc/optimize/_constraint_wrappers.py#L855-L856

Added lines #L855 - L856 were not covered by tests

def compute_unscaled(self, x, constants=None):
"""Compute the raw value of the objective function.
Expand Down
38 changes: 38 additions & 0 deletions tests/test_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,44 @@ def test_no_iterations():
np.testing.assert_allclose(x0, out2["x"])


@pytest.mark.regression
@pytest.mark.optimize
def test_proximal_scalar():
"""Test that proximal scalar optimization works."""
# test fix for GH issue #1403

# optimize to reduce DSHAPE volume from 100 m^3 to 90 m^3
eq = desc.examples.get("DSHAPE")
optimizer = Optimizer("proximal-fmintr") # proximal scalar optimizer
R_modes = np.vstack(
(
[0, 0, 0],
eq.surface.R_basis.modes[
np.max(np.abs(eq.surface.R_basis.modes), 1) > 1, :
],
)
)
Z_modes = eq.surface.Z_basis.modes[
np.max(np.abs(eq.surface.Z_basis.modes), 1) > 1, :
]
objective = ObjectiveFunction(Volume(eq=eq, target=90)) # scalar objective function
constraints = (
FixBoundaryR(eq=eq, modes=R_modes),
FixBoundaryZ(eq=eq, modes=Z_modes),
FixIota(eq=eq),
FixPressure(eq=eq),
FixPsi(eq=eq),
ForceBalance(eq=eq), # force balance constraint for proximal projection
)
[eq], _ = optimizer.optimize(
things=eq,
objective=objective,
constraints=constraints,
verbose=3,
)
np.testing.assert_allclose(eq.compute("V")["V"], 90)


@pytest.mark.regression
@pytest.mark.slow
@pytest.mark.optimize
Expand Down

0 comments on commit 0d4c331

Please sign in to comment.