Skip to content

Commit

Permalink
FixCurrents objective
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-dudt committed May 13, 2024
1 parent d9f4a8d commit 7f061e8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
73 changes: 73 additions & 0 deletions desc/objectives/linear_objectives.py
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,79 @@ def __init__(
)


class FixCurrents(FixParameters):
"""Fixes currents in a Coil or CoilSet.
Parameters
----------
coil : Coil
Coil(s) that will be optimized to satisfy the Objective.
target : {float, ndarray}, optional
Target value(s) of the objective. Only used if bounds is None.
Must be broadcastable to Objective.dim_f. Default is ``target=eq.Psi``.
bounds : tuple of {float, ndarray}, optional
Lower and upper bounds on the objective. Overrides target.
Both bounds must be broadcastable to to Objective.dim_f.
Default is ``target=eq.Psi``.
weight : {float, ndarray}, optional
Weighting to apply to the Objective, relative to other Objectives.
Must be broadcastable to to Objective.dim_f
normalize : bool, optional
Whether to compute the error in physical units or non-dimensionalize.
normalize_target : bool, optional
Whether target and bounds should be normalized before comparing to computed
values. If `normalize` is `True` and the target is in physical units,
this should also be set to True.
name : str, optional
Name of the objective function.
"""

_units = "(A)"
_print_value_fmt = "Fixed current error: {:10.3e} "

def __init__(
self,
coil,
target=None,
bounds=None,
weight=1,
normalize=True,
normalize_target=True,
name="fixed currents",
):
super().__init__(

Check warning on line 2655 in desc/objectives/linear_objectives.py

View check run for this annotation

Codecov / codecov/patch

desc/objectives/linear_objectives.py#L2655

Added line #L2655 was not covered by tests
thing=coil,
params={"current": True},
target=target,
bounds=bounds,
weight=weight,
normalize=normalize,
normalize_target=normalize_target,
name=name,
)

def build(self, use_jit=False, verbose=1):
"""Build constant arrays.
Parameters
----------
use_jit : bool, optional
Whether to just-in-time compile the objective and derivatives.
verbose : int, optional
Level of output.
"""
coil = self.things[0]
if self._normalize:
params = tree_leaves(

Check warning on line 2679 in desc/objectives/linear_objectives.py

View check run for this annotation

Codecov / codecov/patch

desc/objectives/linear_objectives.py#L2677-L2679

Added lines #L2677 - L2679 were not covered by tests
coil.params_dict, is_leaf=lambda x: isinstance(x, dict)
)
mean_current = np.mean([np.abs(param["current"]) for param in params])
self._normalization = np.max((mean_current, 1))
super().build(use_jit=use_jit, verbose=verbose)

Check warning on line 2684 in desc/objectives/linear_objectives.py

View check run for this annotation

Codecov / codecov/patch

desc/objectives/linear_objectives.py#L2682-L2684

Added lines #L2682 - L2684 were not covered by tests


class FixOmniWell(FixParameters):
"""Fixes OmnigenousField.B_lm coefficients.
Expand Down
1 change: 1 addition & 0 deletions desc/objectives/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def get_lowest_mode(basis, coeffs):

elif isinstance(thing, Curve):
scales["a"] = thing.compute("length")["length"] / (2 * np.pi)

# replace 0 scales to avoid normalizing by zero
for scale in scales.keys():
if np.isclose(scales[scale], 0):
Expand Down

0 comments on commit 7f061e8

Please sign in to comment.