-
Notifications
You must be signed in to change notification settings - Fork 14
/
_stability.py
361 lines (314 loc) · 12.4 KB
/
_stability.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
"""Objectives for targeting MHD stability."""
import numpy as np
from desc.compute import compute as compute_fun
from desc.compute import get_profiles, get_transforms
from desc.grid import LinearGrid
from desc.utils import Timer, warnif
from .normalization import compute_scaling_factors
from .objective_funs import _Objective
from .utils import _parse_callable_target_bounds
class MercierStability(_Objective):
"""The Mercier criterion is a fast proxy for MHD stability.
This makes it a useful figure of merit for stellarator operation.
Systems with D_Mercier > 0 are favorable for stability.
See equation 4.16 in
Landreman, M., & Jorge, R. (2020). Magnetic well and Mercier stability of
stellarators near the magnetic axis. Journal of Plasma Physics, 86(5), 905860510.
doi:10.1017/S002237782000121X.
Parameters
----------
eq : Equilibrium
Equilibrium that will be optimized to satisfy the Objective.
target : {float, ndarray, callable}, optional
Target value(s) of the objective. Only used if bounds is None.
Must be broadcastable to Objective.dim_f. If a callable, should take a
single argument `rho` and return the desired value of the profile at those
locations.
bounds : tuple of {float, ndarray, callable}, optional
Lower and upper bounds on the objective. Overrides target.
Both bounds must be broadcastable to to Objective.dim_f
If a callable, each should take a single argument `rho` and return the
desired bound (lower or upper) of the profile at those locations.
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.
name : str, optional
Name of the objective function.
"""
_coordinates = "r"
_units = "(Wb^-2)"
_print_value_fmt = "Mercier Stability: {:10.3e} "
def __init__(
self,
eq,
target=None,
bounds=None,
weight=1,
normalize=True,
normalize_target=True,
loss_function=None,
deriv_mode="auto",
grid=None,
name="Mercier Stability",
):
if target is None and bounds is None:
bounds = (0, np.inf)
self._grid = grid
super().__init__(
things=eq,
target=target,
bounds=bounds,
weight=weight,
normalize=normalize,
normalize_target=normalize_target,
loss_function=loss_function,
deriv_mode=deriv_mode,
name=name,
)
def build(self, use_jit=True, 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.
"""
eq = self.things[0]
if self._grid is None:
grid = LinearGrid(
L=eq.L_grid,
M=eq.M_grid,
N=eq.N_grid,
NFP=eq.NFP,
sym=eq.sym,
axis=False,
)
else:
grid = self._grid
warnif(
(grid.num_theta * (1 + eq.sym)) < 2 * eq.M,
RuntimeWarning,
"MercierStability objective grid requires poloidal "
"resolution for surface averages",
)
warnif(
grid.num_zeta < 2 * eq.N,
RuntimeWarning,
"MercierStability objective grid requires toroidal "
"resolution for surface averages",
)
self._target, self._bounds = _parse_callable_target_bounds(
self._target, self._bounds, grid.nodes[grid.unique_rho_idx]
)
self._dim_f = grid.num_rho
self._data_keys = ["D_Mercier"]
timer = Timer()
if verbose > 0:
print("Precomputing transforms")
timer.start("Precomputing transforms")
profiles = get_profiles(self._data_keys, obj=eq, grid=grid)
transforms = get_transforms(self._data_keys, obj=eq, grid=grid)
self._constants = {
"transforms": transforms,
"profiles": profiles,
}
timer.stop("Precomputing transforms")
if verbose > 1:
timer.disp("Precomputing transforms")
if self._normalize:
scales = compute_scaling_factors(eq)
self._normalization = 1 / scales["Psi"] ** 2
super().build(use_jit=use_jit, verbose=verbose)
def compute(self, params, constants=None):
"""Compute the Mercier stability criterion.
Parameters
----------
params : dict
Dictionary of equilibrium degrees of freedom, eg Equilibrium.params_dict
constants : dict
Dictionary of constant data, eg transforms, profiles etc. Defaults to
self.constants
Returns
-------
D_Mercier : ndarray
Mercier stability criterion.
"""
if constants is None:
constants = self.constants
data = compute_fun(
"desc.equilibrium.equilibrium.Equilibrium",
self._data_keys,
params=params,
transforms=constants["transforms"],
profiles=constants["profiles"],
)
return constants["transforms"]["grid"].compress(data["D_Mercier"])
class MagneticWell(_Objective):
"""The magnetic well is a fast proxy for MHD stability.
This makes it a useful figure of merit for stellarator operation.
Systems with magnetic well > 0 are favorable for stability.
This objective uses the magnetic well parameter defined in equation 3.2 of
Landreman, M., & Jorge, R. (2020). Magnetic well and Mercier stability of
stellarators near the magnetic axis. Journal of Plasma Physics, 86(5), 905860510.
doi:10.1017/S002237782000121X.
Parameters
----------
eq : Equilibrium
Equilibrium that will be optimized to satisfy the Objective.
target : {float, ndarray, callable}, optional
Target value(s) of the objective. Only used if bounds is None.
Must be broadcastable to Objective.dim_f. If a callable, should take a
single argument `rho` and return the desired value of the profile at those
locations.
bounds : tuple of {float, ndarray, callable}, optional
Lower and upper bounds on the objective. Overrides target.
Both bounds must be broadcastable to to Objective.dim_f
If a callable, each should take a single argument `rho` and return the
desired bound (lower or upper) of the profile at those locations.
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. Note: Has no effect for this objective.
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.
name : str, optional
Name of the objective function.
"""
_coordinates = "r"
_units = "(dimensionless)"
_print_value_fmt = "Magnetic Well: {:10.3e} "
def __init__(
self,
eq,
target=None,
bounds=None,
weight=1,
normalize=True,
normalize_target=True,
loss_function=None,
deriv_mode="auto",
grid=None,
name="Magnetic Well",
):
if target is None and bounds is None:
bounds = (0, np.inf)
self._grid = grid
super().__init__(
things=eq,
target=target,
bounds=bounds,
weight=weight,
normalize=normalize,
normalize_target=normalize_target,
loss_function=loss_function,
deriv_mode=deriv_mode,
name=name,
)
def build(self, use_jit=True, 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.
"""
eq = self.things[0]
if self._grid is None:
grid = LinearGrid(
L=eq.L_grid,
M=eq.M_grid,
N=eq.N_grid,
NFP=eq.NFP,
sym=eq.sym,
axis=False,
)
else:
grid = self._grid
warnif(
(grid.num_theta * (1 + eq.sym)) < 2 * eq.M,
RuntimeWarning,
"MagneticWell objective grid requires poloidal "
"resolution for surface averages",
)
warnif(
grid.num_zeta < 2 * eq.N,
RuntimeWarning,
"MagneticWell objective grid requires toroidal "
"resolution for surface averages",
)
self._target, self._bounds = _parse_callable_target_bounds(
self._target, self._bounds, grid.nodes[grid.unique_rho_idx]
)
self._dim_f = grid.num_rho
self._data_keys = ["magnetic well"]
timer = Timer()
if verbose > 0:
print("Precomputing transforms")
timer.start("Precomputing transforms")
profiles = get_profiles(self._data_keys, obj=eq, grid=grid)
transforms = get_transforms(self._data_keys, obj=eq, grid=grid)
self._constants = {
"transforms": transforms,
"profiles": profiles,
}
timer.stop("Precomputing transforms")
if verbose > 1:
timer.disp("Precomputing transforms")
super().build(use_jit=use_jit, verbose=verbose)
def compute(self, params, constants=None):
"""Compute a magnetic well parameter.
Parameters
----------
params : dict
Dictionary of equilibrium degrees of freedom, eg Equilibrium.params_dict
constants : dict
Dictionary of constant data, eg transforms, profiles etc. Defaults to
self.constants
Returns
-------
magnetic_well : ndarray
Magnetic well parameter.
"""
if constants is None:
constants = self.constants
data = compute_fun(
"desc.equilibrium.equilibrium.Equilibrium",
self._data_keys,
params=params,
transforms=constants["transforms"],
profiles=constants["profiles"],
)
return constants["transforms"]["grid"].compress(data["magnetic well"])