Skip to content

Commit

Permalink
update test with new UI
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-dudt committed Jun 17, 2024
1 parent 30aeea4 commit 90296ea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
7 changes: 6 additions & 1 deletion desc/objectives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
RadialForceBalance,
)
from ._free_boundary import BoundaryError, VacuumBoundaryError
from ._generic import GenericObjective, LinearObjectiveFromUser, ObjectiveFromUser
from ._generic import (
GenericObjective,
LinearObjectiveFromUser,
ObjectiveFromUser,
_ExternalObjective,
)
from ._geometry import (
AspectRatio,
BScaleLength,
Expand Down
35 changes: 33 additions & 2 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
CoilLength,
CoilTorsion,
CurrentDensity,
ExternalObjective,
FixBoundaryR,
FixBoundaryZ,
FixCurrent,
Expand All @@ -56,6 +55,7 @@
QuasisymmetryTwoTerm,
VacuumBoundaryError,
Volume,
_ExternalObjective,
get_fixed_boundary_constraints,
get_NAE_constraints,
)
Expand Down Expand Up @@ -1319,6 +1319,7 @@ def test_optimize_with_fourier_planar_coil():


@pytest.mark.unit
@pytest.mark.slow
def test_external_vs_generic_objectives(tmpdir_factory):
"""Test ExternalObjective compared to GenericObjective."""
target = np.array([6.2e-3, 1.1e-1, 6.5e-3, 0]) # values at p_l = [2e2, -2e2]
Expand All @@ -1333,6 +1334,36 @@ def data_from_vmec(eq, path=""):
file.close()
return np.atleast_1d([betatot, betapol, betator, presf1])

class TestExternalObjective(_ExternalObjective):

def __init__(
self,
eq,
target=None,
bounds=None,
weight=1,
normalize=False,
normalize_target=False,
loss_function=None,
path="",
name="external",
):
super().__init__(
eq=eq,
fun=data_from_vmec,
dim_f=4,
target=target,
bounds=bounds,
weight=weight,
normalize=normalize,
normalize_target=normalize_target,
loss_function=loss_function,
fd_step=1e-4,
vectorized=False,
name=name,
path=path,
)

eq0 = get("SOLOVEV")
optimizer = Optimizer("lsq-exact")

Expand Down Expand Up @@ -1369,7 +1400,7 @@ def data_from_vmec(eq, path=""):
dir = tmpdir_factory.mktemp("results")
path = dir.join("wout_result.nc")
objective = ObjectiveFunction(
ExternalObjective(eq=eq0, fun=data_from_vmec, dim_f=4, target=target, path=path)
TestExternalObjective(eq=eq0, target=target, path=path)
)
constraints = FixParameters(
eq0,
Expand Down
3 changes: 0 additions & 3 deletions tests/test_objective_funs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
CoilTorsion,
Elongation,
Energy,
ExternalObjective,
ForceBalance,
ForceBalanceAnisotropic,
GenericObjective,
Expand Down Expand Up @@ -1755,7 +1754,6 @@ class TestComputeScalarResolution:
# need to avoid blowup near the axis
MercierStability,
# don't test these since they depend on what user wants
ExternalObjective,
LinearObjectiveFromUser,
ObjectiveFromUser,
]
Expand Down Expand Up @@ -2077,7 +2075,6 @@ class TestObjectiveNaNGrad:
QuadraticFlux,
ToroidalFlux,
# we don't test these since they depend too much on what exactly the user wants
ExternalObjective,
GenericObjective,
LinearObjectiveFromUser,
ObjectiveFromUser,
Expand Down

0 comments on commit 90296ea

Please sign in to comment.