Skip to content

Commit

Permalink
Merge branch 'master' into yge/customjvp_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
YigitElma authored Oct 11, 2024
2 parents ff3a0e9 + 62e82f4 commit b4218ac
Show file tree
Hide file tree
Showing 15 changed files with 462 additions and 1,716 deletions.
44 changes: 5 additions & 39 deletions desc/objectives/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from desc.utils import Timer, errorif, warnif

from .normalization import compute_scaling_factors
from .objective_funs import _Objective
from .objective_funs import _Objective, collect_docs


class BootstrapRedlConsistency(_Objective):
Expand All @@ -31,31 +31,6 @@ class BootstrapRedlConsistency(_Objective):
----------
eq : Equilibrium
Equilibrium 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. Defaults to ``target=0``.
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.
Defaults to ``target=0``.
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.
loss_function : {None, 'mean', 'min', 'max'}, optional
Loss function to apply to the objective values once computed. This loss function
is called on the raw compute value, before any shifting, scaling, or
normalization.
deriv_mode : {"auto", "fwd", "rev"}
Specify how to compute jacobian matrix, either forward mode or reverse mode AD.
"auto" selects forward or reverse mode based on the size of the input and output
of the objective. Has no effect on self.grad or self.hess which always use
reverse mode and forward over reverse mode respectively.
grid : Grid, optional
Collocation grid containing the nodes to evaluate at.
Defaults to ``LinearGrid(eq.L_grid, eq.M_grid, eq.N_grid)``
Expand All @@ -64,22 +39,13 @@ class BootstrapRedlConsistency(_Objective):
First entry must be M=1. Second entry is the toroidal mode number N,
used for evaluating the Redl bootstrap current formula. Set to 0 for axisymmetry
or quasi-axisymmetry; set to +/-NFP for quasi-helical symmetry.
name : str, optional
Name of the objective function.
jac_chunk_size : int, optional
Will calculate the Jacobian for this objective ``jac_chunk_size``
columns at a time, instead of all at once. The memory usage of the
Jacobian calculation is roughly ``memory usage = m0 + m1*jac_chunk_size``:
the smaller the chunk size, the less memory the Jacobian calculation
will require (with some baseline memory usage). The time to compute the
Jacobian is roughly ``t=t0 +t1/jac_chunk_size``, so the larger the
``jac_chunk_size``, the faster the calculation takes, at the cost of
requiring more memory. A ``jac_chunk_size`` of 1 corresponds to the least
memory intensive, but slowest method of calculating the Jacobian.
If None, it will use the largest size i.e ``obj.dim_x``.
"""

__doc__ = __doc__.rstrip() + collect_docs(
target_default="``target=0``.", bounds_default="``target=0``."
)

_coordinates = "r"
_units = "(T A m^-2)"
_print_value_fmt = "Bootstrap current self-consistency error: "
Expand Down
396 changes: 52 additions & 344 deletions desc/objectives/_coils.py

Large diffs are not rendered by default.

250 changes: 25 additions & 225 deletions desc/objectives/_equilibrium.py

Large diffs are not rendered by default.

129 changes: 14 additions & 115 deletions desc/objectives/_free_boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from desc.grid import LinearGrid
from desc.integrals import DFTInterpolator, FFTInterpolator, virtual_casing_biot_savart
from desc.nestor import Nestor
from desc.objectives.objective_funs import _Objective
from desc.objectives.objective_funs import _Objective, collect_docs
from desc.utils import PRINT_WIDTH, Timer, errorif, parse_argname_change, warnif

from .normalization import compute_scaling_factors
Expand All @@ -38,31 +38,6 @@ class VacuumBoundaryError(_Objective):
Equilibrium that will be optimized to satisfy the Objective.
field : MagneticField
External field produced by coils or other sources outside the plasma.
target : float, ndarray, optional
Target value(s) of the objective. Only used if bounds is None.
Must be broadcastable to Objective.dim_f. Defaults to ``target=0``.
bounds : tuple, optional
Lower and upper bounds on the objective. Overrides target.
Both bounds must be broadcastable to to Objective.dim_f.
Defaults to ``target=0``.
weight : float, ndarray, optional
Weighting to apply to the Objective, relative to other Objectives.
Must be broadcastable to Objective.dim_f.
normalize : bool
Whether to compute the error in physical units or non-dimensionalize.
normalize_target : bool
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.
loss_function : {None, 'mean', 'min', 'max'}, optional
Loss function to apply to the objective values once computed. This loss function
is called on the raw compute value, before any shifting, scaling, or
normalization.
deriv_mode : {"auto", "fwd", "rev"}
Specify how to compute jacobian matrix, either forward mode or reverse mode AD.
"auto" selects forward or reverse mode based on the size of the input and output
of the objective. Has no effect on self.grad or self.hess which always use
reverse mode and forward over reverse mode respectively.
eval_grid : Grid, optional
Collocation grid containing the nodes to evaluate error at. Should be at rho=1.
Defaults to ``LinearGrid(M=eq.M_grid, N=eq.N_grid)``
Expand All @@ -71,22 +46,13 @@ class VacuumBoundaryError(_Objective):
field_fixed : bool
Whether to assume the field is fixed. For free boundary solve, should
be fixed. For single stage optimization, should be False (default).
name : str, optional
Name of the objective function.
jac_chunk_size : int , optional
Will calculate the Jacobian for this objective ``jac_chunk_size``
columns at a time, instead of all at once. The memory usage of the
Jacobian calculation is roughly ``memory usage = m0 + m1*jac_chunk_size``:
the smaller the chunk size, the less memory the Jacobian calculation
will require (with some baseline memory usage). The time to compute the
Jacobian is roughly ``t=t0 +t1/jac_chunk_size``, so the larger the
``jac_chunk_size``, the faster the calculation takes, at the cost of
requiring more memory. A ``jac_chunk_size`` of 1 corresponds to the least
memory intensive, but slowest method of calculating the Jacobian.
If None, it will use the largest size i.e ``obj.dim_x``.
"""

__doc__ = __doc__.rstrip() + collect_docs(
target_default="``target=0``.", bounds_default="``target=0``."
)

_scalar = False
_linear = False
_print_value_fmt = "Boundary Error: "
Expand Down Expand Up @@ -381,31 +347,6 @@ class BoundaryError(_Objective):
Equilibrium that will be optimized to satisfy the Objective.
field : MagneticField
External field produced by coils.
target : float, ndarray, optional
Target value(s) of the objective. Only used if bounds is None.
Must be broadcastable to Objective.dim_f. Defaults to ``target=0``.
bounds : tuple, optional
Lower and upper bounds on the objective. Overrides target.
Both bounds must be broadcastable to to Objective.dim_f.
Defaults to ``target=0``.
weight : float, ndarray, optional
Weighting to apply to the Objective, relative to other Objectives.
Must be broadcastable to Objective.dim_f.
normalize : bool
Whether to compute the error in physical units or non-dimensionalize.
normalize_target : bool
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.
loss_function : {None, 'mean', 'min', 'max'}, optional
Loss function to apply to the objective values once computed. This loss function
is called on the raw compute value, before any shifting, scaling, or
normalization.
deriv_mode : {"auto", "fwd", "rev"}
Specify how to compute jacobian matrix, either forward mode or reverse mode AD.
"auto" selects forward or reverse mode based on the size of the input and output
of the objective. Has no effect on self.grad or self.hess which always use
reverse mode and forward over reverse mode respectively.
s, q : integer
Hyperparameters for singular integration scheme, s is roughly equal to the size
of the local singular grid with respect to the global grid, q is the order of
Expand All @@ -423,21 +364,13 @@ class BoundaryError(_Objective):
loop : bool
If True, evaluate integral using loops, as opposed to vmap. Slower, but uses
less memory.
name : str, optional
Name of the objective function.
jac_chunk_size : int , optional
Will calculate the Jacobian for this objective ``jac_chunk_size``
columns at a time, instead of all at once. The memory usage of the
Jacobian calculation is roughly ``memory usage = m0 + m1*jac_chunk_size``:
the smaller the chunk size, the less memory the Jacobian calculation
will require (with some baseline memory usage). The time to compute the
Jacobian is roughly ``t=t0 +t1/jac_chunk_size``, so the larger the
``jac_chunk_size``, the faster the calculation takes, at the cost of
requiring more memory. A ``jac_chunk_size`` of 1 corresponds to the least
memory intensive, but slowest method of calculating the Jacobian.
If None, it will use the largest size i.e ``obj.dim_x``.
"""

__doc__ = __doc__.rstrip() + collect_docs(
target_default="``target=0``.", bounds_default="``target=0``."
)
__doc__ += """
Examples
--------
Assigning a surface current to the equilibrium:
Expand Down Expand Up @@ -862,53 +795,19 @@ class BoundaryErrorNESTOR(_Objective):
Equilibrium that will be optimized to satisfy the Objective.
field : MagneticField
External field produced by coils.
target : float, ndarray, optional
Target value(s) of the objective. Only used if bounds is None.
Must be broadcastable to Objective.dim_f. Defaults to ``target=0``.
bounds : tuple, optional
Lower and upper bounds on the objective. Overrides target.
Both bounds must be broadcastable to to Objective.dim_f.
Defaults to ``target=0``.
weight : float, ndarray, optional
Weighting to apply to the Objective, relative to other Objectives.
Must be broadcastable to Objective.dim_f.
mf, nf : integer
maximum poloidal and toroidal mode numbers to use for NESTOR scalar potential.
ntheta, nzeta : int
number of grid points in poloidal, toroidal directions to use in NESTOR.
field_grid : Grid, optional
Grid used to discretize field.
normalize : bool
Whether to compute the error in physical units or non-dimensionalize.
normalize_target : bool
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.
loss_function : {None, 'mean', 'min', 'max'}, optional
Loss function to apply to the objective values once computed. This loss function
is called on the raw compute value, before any shifting, scaling, or
normalization.
deriv_mode : {"auto", "fwd", "rev"}
Specify how to compute jacobian matrix, either forward mode or reverse mode AD.
"auto" selects forward or reverse mode based on the size of the input and output
of the objective. Has no effect on self.grad or self.hess which always use
reverse mode and forward over reverse mode respectively.
name : str, optional
Name of the objective function.
jac_chunk_size : int , optional
Will calculate the Jacobian for this objective ``jac_chunk_size``
columns at a time, instead of all at once. The memory usage of the
Jacobian calculation is roughly ``memory usage = m0 + m1*jac_chunk_size``:
the smaller the chunk size, the less memory the Jacobian calculation
will require (with some baseline memory usage). The time to compute the
Jacobian is roughly ``t=t0 +t1/jac_chunk_size``, so the larger the
``jac_chunk_size``, the faster the calculation takes, at the cost of
requiring more memory. A ``jac_chunk_size`` of 1 corresponds to the least
memory intensive, but slowest method of calculating the Jacobian.
If None, it will use the largest size i.e ``obj.dim_x``.
"""

__doc__ = __doc__.rstrip() + collect_docs(
target_default="``target=0``.", bounds_default="``target=0``."
)

_scalar = False
_linear = False
_print_value_fmt = "Boundary magnetic pressure error: "
Expand Down
Loading

0 comments on commit b4218ac

Please sign in to comment.