-
Notifications
You must be signed in to change notification settings - Fork 14
/
continuation.py
845 lines (760 loc) · 24.5 KB
/
continuation.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
"""Functions for solving for equilibria with multigrid continuation method."""
import copy
import warnings
import numpy as np
from termcolor import colored
from desc.equilibrium import EquilibriaFamily, Equilibrium
from desc.objectives import get_equilibrium_objective, get_fixed_boundary_constraints
from desc.optimize import Optimizer
from desc.perturbations import get_deltas
from desc.utils import Timer, errorif
MIN_MRES_STEP = 1
MIN_PRES_STEP = 0.1
MIN_BDRY_STEP = 0.05
def _solve_axisym(
eq,
mres_step,
objective="force",
optimizer="lsq-exact",
pert_order=2,
ftol=None,
xtol=None,
gtol=None,
maxiter=100,
verbose=1,
checkpoint_path=None,
):
"""Solve initial axisymmetric case with adaptive step sizing."""
timer = Timer()
surface = eq.surface
pressure = eq.pressure
L, M, N, L_grid, M_grid, N_grid = eq.L, eq.M, eq.N, eq.L_grid, eq.M_grid, eq.N_grid
spectral_indexing = eq.spectral_indexing
Mi = min(M, mres_step) if mres_step > 0 else M
Li = int(np.ceil(L / M) * Mi)
Ni = 0
L_gridi = np.ceil(L_grid / L * Li).astype(int)
M_gridi = np.ceil(M_grid / M * Mi).astype(int)
N_gridi = np.ceil(N_grid / max(N, 1) * Ni).astype(int)
# first we solve vacuum until we reach full L,M
mres_steps = int(max(np.ceil(M / mres_step), 1)) if mres_step > 0 else 0
deltas = {}
surf_axisym = surface.copy()
pres_vac = pressure.copy()
surf_axisym.change_resolution(L, M, Ni)
# start with zero pressure
pres_vac.params *= 0
eqi = Equilibrium(
Psi=eq.Psi,
NFP=eq.NFP,
L=Li,
M=Mi,
N=Ni,
L_grid=L_gridi,
M_grid=M_gridi,
N_grid=N_gridi,
pressure=pres_vac.copy(),
iota=copy.copy(eq.iota), # have to use copy.copy here since may be None
current=copy.copy(eq.current),
surface=surf_axisym.copy(),
sym=eq.sym,
spectral_indexing=spectral_indexing,
)
if not isinstance(optimizer, Optimizer):
optimizer = Optimizer(optimizer)
eqfam = EquilibriaFamily()
ii = 0
stop = False
while ii < mres_steps and not stop:
timer.start("Iteration {} total".format(ii + 1))
if ii > 0:
eqi = eqfam[-1].copy()
# increase resolution of vacuum solution
Mi = min(Mi + mres_step, M)
Li = int(np.ceil(L / M) * Mi)
L_gridi = np.ceil(L_grid / L * Li).astype(int)
M_gridi = np.ceil(M_grid / M * Mi).astype(int)
N_gridi = np.ceil(N_grid / max(N, 1) * Ni).astype(int)
eqi.change_resolution(Li, Mi, Ni, L_gridi, M_gridi, N_gridi)
surf_i = eqi.surface
surf_i2 = surface.copy()
surf_i2.change_resolution(Li, Mi, Ni)
deltas = get_deltas({"surface": surf_i}, {"surface": surf_i2})
surf_i = surf_i2
constraints_i = get_fixed_boundary_constraints(eq=eqi)
objective_i = get_equilibrium_objective(eq=eqi, mode=objective)
if verbose:
_print_iteration_summary(
ii,
None,
eqi,
0,
0,
1,
pert_order,
objective_i,
optimizer,
)
if len(deltas) > 0:
if verbose > 0:
print("Perturbing equilibrium")
eqi.perturb(
objective=objective_i,
constraints=constraints_i,
deltas=deltas,
order=pert_order,
verbose=verbose,
copy=False,
)
deltas = {}
stop = not eqi.is_nested()
if not stop:
eqi.solve(
optimizer=optimizer,
objective=objective_i,
constraints=constraints_i,
ftol=ftol,
xtol=xtol,
gtol=gtol,
verbose=verbose,
maxiter=maxiter,
)
stop = stop or not eqi.is_nested()
eqfam.append(eqi)
if checkpoint_path is not None:
if verbose > 0:
print("Saving latest iteration")
eqfam.save(checkpoint_path)
timer.stop("Iteration {} total".format(ii + 1))
if verbose > 1:
timer.disp("Iteration {} total".format(ii + 1))
ii += 1
if stop:
if mres_step == MIN_MRES_STEP:
raise RuntimeError(
"Automatic continuation failed with mres_step=1, "
+ "something is probably very wrong with your desired equilibrium."
)
else:
warnings.warn(
colored(
"WARNING: Automatic continuation failed with "
+ f"mres_step={mres_step}, retrying with mres_step={mres_step//2}",
"yellow",
)
)
return _solve_axisym(
eq,
mres_step // 2,
objective,
optimizer,
pert_order,
ftol,
xtol,
gtol,
maxiter,
verbose,
checkpoint_path,
)
return eqfam
def _add_pressure(
eq,
eqfam,
pres_step,
objective="force",
optimizer="lsq-exact",
pert_order=2,
ftol=None,
xtol=None,
gtol=None,
maxiter=100,
verbose=1,
checkpoint_path=None,
):
"""Add pressure with adaptive step sizing."""
timer = Timer()
eqi = eqfam[-1].copy()
eqfam_temp = eqfam.copy()
# make sure its at full radial/poloidal resolution
eqi.change_resolution(L=eq.L, M=eq.M, L_grid=eq.L_grid, M_grid=eq.M_grid)
pres_steps = (
0
if (abs(eq.pressure(np.linspace(0, 1, 20))) < 1e-14).all() or pres_step == 0
else int(np.ceil(1 / pres_step))
)
pres_ratio = 0 if pres_steps else 1
ii = len(eqfam_temp)
stop = False
while ii - len(eqfam_temp) < pres_steps and not stop:
timer.start("Iteration {} total".format(ii + 1))
# increase pressure
deltas = get_deltas(
{"pressure": eqfam_temp[-1].pressure}, {"pressure": eq.pressure}
)
deltas["p_l"] *= pres_step
pres_ratio += pres_step
constraints_i = get_fixed_boundary_constraints(eq=eqi)
objective_i = get_equilibrium_objective(eq=eqi, mode=objective)
if verbose:
_print_iteration_summary(
ii,
None,
eqi,
0,
pres_ratio,
1,
pert_order,
objective_i,
optimizer,
)
if len(deltas) > 0:
if verbose > 0:
print("Perturbing equilibrium")
eqi.perturb(
objective=objective_i,
constraints=constraints_i,
deltas=deltas,
order=pert_order,
verbose=verbose,
copy=False,
)
deltas = {}
stop = not eqi.is_nested()
if not stop:
eqi.solve(
optimizer=optimizer,
objective=objective_i,
constraints=constraints_i,
ftol=ftol,
xtol=xtol,
gtol=gtol,
verbose=verbose,
maxiter=maxiter,
)
stop = stop or not eqi.is_nested()
eqfam.append(eqi)
eqi = eqi.copy()
if checkpoint_path is not None:
if verbose > 0:
print("Saving latest iteration")
eqfam.save(checkpoint_path)
timer.stop("Iteration {} total".format(ii + 1))
if verbose > 1:
timer.disp("Iteration {} total".format(ii + 1))
ii += 1
if stop:
if pres_step <= MIN_PRES_STEP:
raise RuntimeError(
"Automatic continuation failed with "
+ f"pres_step={pres_step}, something is probably very wrong with your "
+ "desired equilibrium."
)
else:
warnings.warn(
colored(
"WARNING: Automatic continuation failed with "
+ f"pres_step={pres_step}, retrying with pres_step={pres_step/2}",
"yellow",
)
)
return _add_pressure(
eq,
eqfam_temp,
pres_step / 2,
objective,
optimizer,
pert_order,
ftol,
xtol,
gtol,
maxiter,
verbose,
checkpoint_path,
)
return eqfam
def _add_shaping(
eq,
eqfam,
bdry_step,
objective="force",
optimizer="lsq-exact",
pert_order=2,
ftol=None,
xtol=None,
gtol=None,
maxiter=100,
verbose=1,
checkpoint_path=None,
):
"""Add 3D shaping with adaptive step sizing."""
timer = Timer()
eqi = eqfam[-1].copy()
eqfam_temp = eqfam.copy()
# make sure its at full resolution
eqi.change_resolution(eq.L, eq.M, eq.N, eq.L_grid, eq.M_grid, eq.N_grid)
bdry_steps = 0 if eq.N == 0 or bdry_step == 0 else int(np.ceil(1 / bdry_step))
bdry_ratio = 0 if eq.N else 1
surf_axisym = eq.surface.copy()
surf_axisym.change_resolution(eq.L, eq.M, 0)
surf_axisym.change_resolution(eq.L, eq.M, eq.N)
ii = len(eqfam_temp)
stop = False
while ii - len(eqfam_temp) < bdry_steps and not stop:
timer.start("Iteration {} total".format(ii + 1))
# increase shaping
deltas = get_deltas({"surface": surf_axisym}, {"surface": eq.surface})
if "Rb_lmn" in deltas:
deltas["Rb_lmn"] *= bdry_step
if "Zb_lmn" in deltas:
deltas["Zb_lmn"] *= bdry_step
bdry_ratio += bdry_step
constraints_i = get_fixed_boundary_constraints(eq=eqi)
objective_i = get_equilibrium_objective(eq=eqi, mode=objective)
if verbose:
_print_iteration_summary(
ii,
None,
eqi,
bdry_ratio,
1,
1,
pert_order,
objective_i,
optimizer,
)
if len(deltas) > 0:
if verbose > 0:
print("Perturbing equilibrium")
eqi.perturb(
objective=objective_i,
constraints=constraints_i,
deltas=deltas,
order=pert_order,
verbose=verbose,
copy=False,
)
deltas = {}
stop = not eqi.is_nested()
if not stop:
eqi.solve(
optimizer=optimizer,
objective=objective_i,
constraints=constraints_i,
ftol=ftol,
xtol=xtol,
gtol=gtol,
verbose=verbose,
maxiter=maxiter,
)
stop = stop or not eqi.is_nested()
eqfam.append(eqi)
eqi = eqi.copy()
if checkpoint_path is not None:
if verbose > 0:
print("Saving latest iteration")
eqfam.save(checkpoint_path)
timer.stop("Iteration {} total".format(ii + 1))
if verbose > 1:
timer.disp("Iteration {} total".format(ii + 1))
ii += 1
if stop:
if bdry_step <= MIN_BDRY_STEP:
raise RuntimeError(
"Automatic continuation failed with "
+ f"bdry_step={bdry_step}, something is probably very wrong with your "
+ "desired equilibrium."
)
else:
warnings.warn(
colored(
"WARNING: Automatic continuation failed with "
+ f"bdry_step={bdry_step}, retrying with bdry_step={bdry_step/2}",
"yellow",
)
)
return _add_shaping(
eq,
eqfam_temp,
bdry_step / 2,
objective,
optimizer,
pert_order,
ftol,
xtol,
gtol,
maxiter,
verbose,
checkpoint_path,
)
return eqfam
def solve_continuation_automatic( # noqa: C901
eq,
objective="force",
optimizer="lsq-exact",
pert_order=2,
ftol=None,
xtol=None,
gtol=None,
maxiter=100,
verbose=1,
checkpoint_path=None,
**kwargs,
):
"""Solve for an equilibrium using an automatic continuation method.
By default, the method first solves for a no pressure tokamak, then a finite beta
tokamak, then a finite beta stellarator. Steps in resolution, pressure, and 3D
shaping are determined adaptively, and the method may backtrack to use smaller steps
if the initial steps are too large.
Parameters
----------
eq : Equilibrium
Unsolved Equilibrium with the final desired boundary, profiles, resolution.
objective : {"force", "energy"}
function to solve for equilibrium solution
optimizer : str or Optimizer (optional)
optimizer to use
pert_order : int
order of perturbations to use.
ftol, xtol, gtol : float
stopping tolerances for subproblem at each step. `None` will use defaults
for given optimizer.
maxiter : int
maximum number of iterations in each equilibrium subproblem.
verbose : integer
* 0: no output
* 1: summary of each iteration
* 2: as above plus timing information
* 3: as above plus detailed solver output
checkpoint_path : str or path-like
file to save checkpoint data (Default value = None)
**kwargs : dict, optional
* ``mres_step``: int, default 6. The amount to increase Mpol by at each
continuation step
* ``pres_step``: float, ``0<=pres_step<=1``, default 0.5. The amount to
increase pres_ratio by at each continuation step
* ``bdry_step``: float, ``0<=bdry_step<=1``, default 0.25. The amount to
increase bdry_ratio by at each continuation step
Returns
-------
eqfam : EquilibriaFamily
family of equilibria for the intermediate steps, where the last member is the
final desired configuration,
"""
errorif(
eq.electron_temperature is not None,
NotImplementedError,
"Continuation method with kinetic profiles is not currently supported",
)
errorif(
eq.anisotropy is not None,
NotImplementedError,
"Continuation method with anisotropic pressure is not currently supported",
)
timer = Timer()
timer.start("Total time")
mres_step = kwargs.pop("mres_step", 6)
pres_step = kwargs.pop("pres_step", 1 / 2)
bdry_step = kwargs.pop("bdry_step", 1 / 4)
assert len(kwargs) == 0, "Got an unexpected kwarg {}".format(kwargs.keys())
if not isinstance(optimizer, Optimizer):
optimizer = Optimizer(optimizer)
eqfam = _solve_axisym(
eq,
mres_step,
objective,
optimizer,
pert_order,
ftol,
xtol,
gtol,
maxiter,
verbose,
checkpoint_path,
)
# for zero current we want to do shaping before pressure to avoid having a
# tokamak with zero current but finite pressure (non-physical)
if eq.current is not None and np.all(eq.current(np.linspace(0, 1, 20)) == 0):
eqfam = _add_shaping(
eq,
eqfam,
bdry_step,
objective,
optimizer,
pert_order,
ftol,
xtol,
gtol,
maxiter,
verbose,
checkpoint_path,
)
eqfam = _add_pressure(
eq,
eqfam,
pres_step,
objective,
optimizer,
pert_order,
ftol,
xtol,
gtol,
maxiter,
verbose,
checkpoint_path,
)
# for other cases such as fixed iota or nonzero current we do pressure first
# since its cheaper to do it without the 3d modes
else:
eqfam = _add_pressure(
eq,
eqfam,
pres_step,
objective,
optimizer,
pert_order,
ftol,
xtol,
gtol,
maxiter,
verbose,
checkpoint_path,
)
eqfam = _add_shaping(
eq,
eqfam,
bdry_step,
objective,
optimizer,
pert_order,
ftol,
xtol,
gtol,
maxiter,
verbose,
checkpoint_path,
)
eq.params_dict = eqfam[-1].params_dict
eqfam[-1] = eq
timer.stop("Total time")
if verbose > 0:
print("====================")
print("Done")
if verbose > 1:
timer.disp("Total time")
if checkpoint_path is not None:
if verbose > 0:
print("Output written to {}".format(checkpoint_path))
eqfam.save(checkpoint_path)
if verbose:
print("====================")
return eqfam
def solve_continuation( # noqa: C901
eqfam,
objective="force",
optimizer="lsq-exact",
pert_order=2,
ftol=None,
xtol=None,
gtol=None,
maxiter=100,
verbose=1,
checkpoint_path=None,
):
"""Solve for an equilibrium by continuation method.
Steps through an EquilibriaFamily, solving each equilibrium, and uses perturbations
to step between different profiles/boundaries.
Uses the previous step as an initial guess for each solution.
Parameters
----------
eqfam : EquilibriaFamily or list of Equilibria
Equilibria to solve for at each step.
objective : {"force", "energy"}
function to solve for equilibrium solution
optimizer : str or Optimizer (optional)
optimizer to use
pert_order : int or array of int
order of perturbations to use. If array-like, should be same length as eqfam
to specify different values for each step.
ftol, xtol, gtol : float or array-like of float
stopping tolerances for subproblem at each step. `None` will use defaults
for given optimizer.
maxiter : int or array-like of int
maximum number of iterations in each equilibrium subproblem.
verbose : integer
* 0: no output
* 1: summary of each iteration
* 2: as above plus timing information
* 3: as above plus detailed solver output
checkpoint_path : str or path-like
file to save checkpoint data (Default value = None)
Returns
-------
eqfam : EquilibriaFamily
family of equilibria for the intermediate steps, where the last member is the
final desired configuration,
"""
errorif(
not all([eq.electron_temperature is None for eq in eqfam]),
NotImplementedError,
"Continuation method with kinetic profiles is not currently supported",
)
errorif(
not all([eq.anisotropy is None for eq in eqfam]),
NotImplementedError,
"Continuation method with anisotropic pressure is not currently supported",
)
timer = Timer()
timer.start("Total time")
pert_order, ftol, xtol, gtol, maxiter, _ = np.broadcast_arrays(
pert_order, ftol, xtol, gtol, maxiter, eqfam
)
if isinstance(eqfam, (list, tuple)):
eqfam = EquilibriaFamily(*eqfam)
if not isinstance(optimizer, Optimizer):
optimizer = Optimizer(optimizer)
objective_i = get_equilibrium_objective(eq=eqfam[0], mode=objective)
constraints_i = get_fixed_boundary_constraints(eq=eqfam[0])
ii = 0
nn = len(eqfam)
stop = False
while ii < nn and not stop:
timer.start("Iteration {} total".format(ii + 1))
eqi = eqfam[ii]
if verbose:
_print_iteration_summary(
ii,
nn,
eqi,
_get_ratio(eqi.surface, eqfam[-1].surface),
_get_ratio(eqi.pressure, eqfam[-1].pressure),
_get_ratio(eqi.current, eqfam[-1].current),
pert_order[ii],
objective_i,
optimizer,
)
deltas = {}
if ii > 0:
eqi.set_initial_guess(eqfam[ii - 1])
# figure out if we need perturbations
things1 = {
"surface": eqfam[ii - 1].surface,
"iota": eqfam[ii - 1].iota,
"current": eqfam[ii - 1].current,
"pressure": eqfam[ii - 1].pressure,
"Psi": eqfam[ii - 1].Psi,
}
things2 = {
"surface": eqi.surface,
"iota": eqi.iota,
"current": eqi.current,
"pressure": eqi.pressure,
"Psi": eqi.Psi,
}
deltas = get_deltas(things1, things2)
if len(deltas) > 0:
if verbose > 0:
print("Perturbing equilibrium")
# TODO: pass Jx if available
eqp = eqfam[ii - 1].copy()
objective_i = get_equilibrium_objective(eq=eqp, mode=objective)
constraints_i = get_fixed_boundary_constraints(eq=eqp)
eqp.change_resolution(**eqi.resolution)
eqp.perturb(
objective=objective_i,
constraints=constraints_i,
deltas=deltas,
order=pert_order[ii],
verbose=verbose,
copy=False,
)
eqi.params_dict = eqp.params_dict
deltas = {}
del eqp
if not eqi.is_nested(msg="manual"):
stop = True
if not stop:
# TODO: add ability to rebind objectives
objective_i = get_equilibrium_objective(eq=eqi, mode=objective)
constraints_i = get_fixed_boundary_constraints(eq=eqi)
eqi.solve(
optimizer=optimizer,
objective=objective_i,
constraints=constraints_i,
ftol=ftol[ii],
xtol=xtol[ii],
gtol=gtol[ii],
verbose=verbose,
maxiter=maxiter[ii],
)
if not eqi.is_nested(msg="manual"):
stop = True
if checkpoint_path is not None:
if verbose > 0:
print("Saving latest iteration")
eqfam.save(checkpoint_path)
timer.stop("Iteration {} total".format(ii + 1))
if verbose > 1:
timer.disp("Iteration {} total".format(ii + 1))
ii += 1
timer.stop("Total time")
if verbose > 0:
print("====================")
print("Done")
if verbose > 1:
timer.disp("Total time")
if checkpoint_path is not None:
if verbose > 0:
print("Output written to {}".format(checkpoint_path))
eqfam.save(checkpoint_path)
if verbose:
print("====================")
return eqfam
def _get_ratio(thing1, thing2):
"""Figure out bdry_ratio, pres_ratio etc from objects."""
if thing1 is None or thing2 is None:
return None
if hasattr(thing1, "R_lmn"): # treat it as surface
R1 = thing1.R_lmn[thing1.R_basis.modes[:, 2] != 0]
R2 = thing2.R_lmn[thing2.R_basis.modes[:, 2] != 0]
Z1 = thing1.Z_lmn[thing1.Z_basis.modes[:, 2] != 0]
Z2 = thing2.Z_lmn[thing2.Z_basis.modes[:, 2] != 0]
num = np.linalg.norm([R1, Z1])
den = np.linalg.norm([R2, Z2])
else:
num = np.linalg.norm(thing1.params)
den = np.linalg.norm(thing2.params)
if num == 0 and den == 0:
return 1
return num / den
def _print_iteration_summary(
ii,
nn,
eq,
bdry_ratio,
pres_ratio,
curr_ratio,
pert_order,
objective,
optimizer,
**kwargs,
):
print("================")
print(f"Step {ii+1}" + ("" if nn is None else f"/{nn}"))
print("================")
eq.resolution_summary()
print("Boundary ratio = {}".format(bdry_ratio))
print("Pressure ratio = {}".format(pres_ratio))
if eq.current is not None:
print("Current ratio = {}".format(curr_ratio))
print("Perturbation Order = {}".format(pert_order))
print(
"Objective: {}".format(
objective if isinstance(objective, str) else objective.objectives[0].name
)
)
print(
"Optimizer: {}".format(
optimizer if isinstance(optimizer, str) else optimizer.method
)
)
print("================")