-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmec_optimization.py
More file actions
1325 lines (1201 loc) · 61.4 KB
/
vmec_optimization.py
File metadata and controls
1325 lines (1201 loc) · 61.4 KB
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
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import os
from vmec_output import VmecOutput
from vmec_input import VmecInput
import errno
from scipy import interpolate
import copy
from scipy.io import netcdf
from mpi4py import MPI
from surface_utils import init_modes
def axis_weight(s):
return np.exp(-(s-0.1)**2/0.05**2)
class VmecOptimization:
"""
A class used for optimization of a VMEC equilibrium
...
Attributes
---------
vmecInputFilename (str) : Fortran namelist for initial equilibrium
evaluation. This namelist will be modified as the boundary is modified.
callVMEC_function (function) : takes vmecInputFilename (str) as sole input
argument and returns runs VMEC with given Fortran namelist. Returns
error code (nonzero = error). See call_vmec.py for examples.
name (str) : Name used as prefix for directory names of new VMEC
evaluations.
callANIMEC_function (function) : takes vmecInputFilename (str) as sole input
argument and returns runs ANIMEC with given Fortran namelist. Returns
error code (nonzero = error). See call_vmec.py for examples.
mmax_sensitivity (int) : maximum poloidal mode number of boundary to
optimize
nmax_sensitivity (int) : maximum toroidal mode number of boundary to
optimize
delta_curr (float) : defines size of current perturbation for adjoint shape
gradient calculation
delta_pres (float) : defines size of pressure perturbation for adjoint shape
gradient calculation
woutFilename (nc filename) : VMEC outputfile for initialization of
optimization
ntheta (int) : number of poloidal grid points
nzeta (int) : number of toroidal grid points per period
verbose (boolean) : if True, more output is printed
xn_sensitivity (int array) : toroidal mode numbers for optimization
xm_sensitivity (int array) : poloidal mode numbers for optimizationm. Must
be same size as xn_sensitivity.
"""
def __init__(self,vmecInputFilename,callVMEC_function=None,name='optimization',
callANIMEC_function=None,mmax_sensitivity=0,nmax_sensitivity=0,
delta_curr=10,delta_pres=10,woutFilename=None,ntheta=100,nzeta=100,
verbose=True,xn_sensitivity=None,xm_sensitivity=None):
if (xn_sensitivity is None or xm_sensitivity is None):
[mnmax_sensitivity,xm_sensitivity,xn_sensitivity] = \
init_modes(mmax_sensitivity,nmax_sensitivity)
self.mmax_sensitivity = mmax_sensitivity
self.nmax_sensitivity = nmax_sensitivity
else:
assert(len(xn_sensitivity)==len(xm_sensitivity))
self.mmax_sensitivity = np.max(xm_sensitivity)
self.nmax_sensitivity = np.max(xn_sensitivity)
mnmax_sensitivity = len(xn_sensitivity)
self.mnmax_sensitivity = mnmax_sensitivity
self.xm_sensitivity = xm_sensitivity
self.xn_sensitivity = xn_sensitivity
self.callVMEC_function = callVMEC_function
self.callANIMEC_function = callANIMEC_function
self.counter = 0 # Counts number of function evals (used for
# naming directories)
self.name = name # Name used for directories
self.vmecInputFilename = vmecInputFilename
self.directory = os.getcwd()
self.delta_pres = delta_pres
self.delta_curr = delta_curr
self.ntheta = ntheta
self.nzeta = nzeta
self.jacobian_threshold = 1e-4
self.verbose = verbose
self.vmec_objectives = ['iota','iota_prime','iota_target','well',
'well_ratio','volume','area','jacobian',
'radius','normalized_jacobian','modB_vol',
'axis_ripple']
self.input_objectives = ['volume','area','jacobian','radius',
'normalized_jacobian','summed_proximity',
'summed_radius','curvature',
'curvature_integrated']
self.vmecInputObject = VmecInput(vmecInputFilename,ntheta,nzeta,
verbose=verbose)
self.nfp = self.vmecInputObject.nfp
mpol_input = self.vmecInputObject.mpol
ntor_input = self.vmecInputObject.ntor
mnmax_input = self.vmecInputObject.mnmax
rbc_input = self.vmecInputObject.rbc
zbs_input = self.vmecInputObject.zbs
xm_input = self.vmecInputObject.xm
xn_input = self.vmecInputObject.xn
if (mpol_input<2):
raise ValueError('Error! mpol must be > 1.')
# mpol, ntor for calculaitons must be large enough for sensitivity
#required
self.mmax = max(self.mmax_sensitivity,mpol_input-1)
self.nmax = max(self.nmax_sensitivity,ntor_input)
[self.mnmax,self.xm,self.xn] = init_modes(self.mmax,self.nmax)
# Update input object with required mpol and ntor
self.vmecInputObject.update_modes(self.mmax+1,self.nmax)
rmnc = np.zeros(self.mnmax)
zmns = np.zeros(self.mnmax)
rmnc_opt = np.zeros(self.mnmax_sensitivity)
zmns_opt = np.zeros(self.mnmax_sensitivity)
for imn in range(mnmax_input):
cond = np.logical_and((self.xm == xm_input[imn]),\
(self.xn == xn_input[imn]))
if (any(cond)):
rmnc[cond] = rbc_input[imn]
zmns[cond] = zbs_input[imn]
cond = np.logical_and((xm_sensitivity == xm_input[imn]),\
(xn_sensitivity == xn_input[imn]))
if (any(cond)):
rmnc_opt[cond] = rbc_input[imn]
zmns_opt[cond] = zbs_input[imn]
self.boundary = np.hstack((rmnc,zmns))
# Remove 0,0 mode from zmns
cond = np.logical_not(np.logical_and(xm_sensitivity == 0,
xn_sensitivity==0))
zmns_opt = zmns_opt[cond]
self.boundary_opt = np.hstack((rmnc_opt,zmns_opt))
self.norm_normal = None
self.vmecOutputObject = None
if (woutFilename is not None):
self.vmecOutputObject = VmecOutput(woutFilename,self.ntheta,
self.nzeta)
def evaluate_vmec(self,boundary=None,It=None,pres=None,update=True):
"""
Evaluates VMEC equilibrium with specified boundary, pressure, or
toroidal current profile update
boundary, It, and pres should not be specified simultaneously.
If neither are specified, then an exception is raised.
Parameters
----------
boundary (float array) : values of boundary harmonics for VMEC
evaluations
It (float array) : value of toroidal current on half grid VMEC mesh for
prescription of profile with "ac_aux_f" Fortran namelist variable
pres (float array) : value of pressure on half grid VMEC mesh for
prescription of profile with "am_aux_f" Fortran namelist variable
update (bool) : if True, self.vmecOutputObject is updated and boundary
is assigned to self.boundary_opt
Returns
----------
exit_code (integer) : code returned by VMEC evaluation. non-zero
indicates error.
vmecOutput_new (VmecOutput) : object corresponding to output
of VMEC evaluations
"""
self.counter += 1
if (boundary is not None and update):
# Update boundary
self.update_boundary_opt(boundary)
rank = MPI.COMM_WORLD.Get_rank()
if (os.getcwd()!=self.directory):
if (self.verbose):
print('''evaluate_vmec called from incorrect directory.
Changing to '''+self.directory)
os.chdir(self.directory)
if (np.count_nonzero([It,pres,boundary] is not None)>1):
raise RuntimeError('''evaluate_vmec called with more than one type of
perturbation (boundary, It, pres). This behavior is not
supported.''')
if (rank == 0):
if (boundary is not None):
# Update boundary
directory_name = self.name+"_"+str(self.counter)
elif (It is not None):
directory_name = "delta_curr_"+str(self.counter)
elif (pres is not None):
directory_name = "delta_pres_"+str(self.counter)
else:
raise RuntimeError('''Error! evaluate_vmec called when
equilibrium does not need to be evaluated.''')
# Make directory for VMEC evaluations
try:
os.mkdir(directory_name)
except OSError as exc:
if exc.errno != errno.EEXIST:
if (self.verbose):
print('Warning. Directory '+directory_name+\
' already exists. Contents may be overwritten.')
raise
pass
input_file = "input."+directory_name
inputObject_new = copy.deepcopy(self.vmecInputObject)
inputObject_new.input_filename = input_file
inputObject_new.ntor = self.nmax
inputObject_new.mpol = self.mmax+1
# Edit input filename with boundary
if (It is not None):
curtor = 1.5*It[-1] - 0.5*It[-2]
s_half = self.vmecOutputObject.s_half
if (self.vmecOutputObject.ns>101):
s_spline = np.linspace(0,1,101)
ds_spline = s_spline[1]-s_spline[0]
s_spline_half = s_spline - 0.5*ds_spline
s_spline_half = np.delete(s_spline_half,0)
It_spline = \
interpolate.InterpolatedUnivariateSpline(s_half,It)
It = It_spline(s_spline_half)
s_half = s_spline_half
inputObject_new.curtor = curtor
inputObject_new.ac_aux_f = list(It)
inputObject_new.ac_aux_s = list(s_half)
inputObject_new.pcurr_type = "line_segment_I"
if (pres is not None):
s_half = self.vmecOutputObject.s_half
if (self.vmecOutputObject.ns>101):
s_spline = np.linspace(0,1,101)
ds_spline = s_spline[1]-s_spline[0]
s_spline_half = s_spline - 0.5*ds_spline
s_spline_half = np.delete(s_spline_half,0)
pres_spline = \
interpolate.InterpolatedUnivariateSpline(s_half,pres)
pres = pres_spline(s_spline_half)
s_half = s_spline_half
inputObject_new.am_aux_f = list(pres)
inputObject_new.am_aux_s = list(s_half)
inputObject_new.pmass_type = "cubic_spline"
if (boundary is not None):
inputObject_new.rbc = self.boundary[0:self.mnmax]
inputObject_new.zbs = self.boundary[self.mnmax::]
else:
inputObject_new = None
directory_name = None
input_file = None
# Call VMEC with revised input file
inputObject_new = MPI.COMM_WORLD.bcast(inputObject_new,root=0)
directory_name = MPI.COMM_WORLD.bcast(directory_name,root=0)
input_file = MPI.COMM_WORLD.bcast(input_file,root=0)
os.chdir(directory_name)
exit_code = self.call_vmec(inputObject_new)
if (exit_code == 0):
# Read from new equilibrium
outputFileName = "wout_"+input_file[6::]+".nc"
if (rank == 0):
vmecOutput_new = \
VmecOutput(outputFileName,self.ntheta,self.nzeta)
else:
vmecOutput_new = None
vmecOutput_new = MPI.COMM_WORLD.bcast(vmecOutput_new,root=0)
if (boundary is not None and update):
self.vmecOutputObject = vmecOutput_new
self.vmecInputObject = inputObject_new
else:
vmecOutput_new = None
os.chdir("..")
return exit_code, vmecOutput_new
def evaluate_animec(self,boundary=None,pres=None,It=None,bcrit=1):
"""
Evaluates ANIVMEC equilibrium with specified boundary pressure
profile update
boundary and pres should not be specified simultaneously.
If neither are specified, then an exception is raised.
Parameters
----------
boundary (float array) : values of boundary harmonics for VMEC
evaluations
pres (float array) : value of hot pressure on half grid VMEC mesh for
prescription of profile with "ah_aux_f" Fortran namelist variable
It (float array) : value of toroidal current on half grid VMEC mesh
for prescription of profile with "ac_aux_f" Fortran namelist
variable.
bcrit (float) : defines the BCRIT parameter in ANIMEC namelist. Sets the
scale of the perturbation of the pressure tensor.
Returns
----------
exit_code (integer) : code returned by VMEC evaluation. non-zero
indicates error.
vmecOutput_new (VmecOutput) : object corresponding to output of ANIMEC
evaluations
"""
self.counter += 1
rank = MPI.COMM_WORLD.Get_rank()
if (rank == 0):
if (os.getcwd()!=self.directory):
if (self.verbose):
print('''evaluate_animec called from incorrect directory.
Changing to '''+self.directory)
os.chdir(self.directory)
if (np.count_nonzero([pres,boundary] is not None)>1):
raise RuntimeError('''evaluate_animec called with more than one
type of perturbation (boundary, pres). This behavior is not
supported.''')
if (boundary is not None):
# Update boundary
self.update_boundary_opt(boundary)
directory_name = self.name+"_"+str(self.counter)
elif (pres is not None):
directory_name = "delta_pres_"+str(self.counter)
elif (self.counter == 0):
directory_name = self.name+"_"+str(self.counter)
else:
raise RuntimeError('''Error! evaluate_animec called when
equilibrium does not need to be evaluated.''')
# Make directory for ANIMEC evaluations
try:
os.mkdir(directory_name)
except OSError as exc:
if exc.errno != errno.EEXIST:
if (self.verbose):
print('Warning. Directory '+directory_name+\
' already exists. Contents may be overwritten.')
raise
pass
input_file = "input."+directory_name
inputObject_new = copy.deepcopy(self.vmecInputObject)
inputObject_new.input_filename = input_file
inputObject_new.ntor = self.nmax
inputObject_new.mpol = self.mmax+1
# Edit input filename with boundary
if (pres is not None):
s_half = self.vmecOutputObject.s_half
if (self.vmecOutputObject.ns>101):
s_spline = np.linspace(0,1,101)
ds_spline = s_spline[1]-s_spline[0]
s_spline_half = s_spline - 0.5*ds_spline
s_spline_half = np.delete(s_spline_half,0)
pres_spline = \
interpolate.InterpolatedUnivariateSpline(s_half,pres)
pres = pres_spline(s_spline_half)
s_half = s_spline_half
inputObject_new.animec = True
inputObject_new.ah_aux_f = list(pres)
inputObject_new.ah_aux_s = list(s_half)
inputObject_new.photp_type = "line_segment"
inputObject_new.bcrit = bcrit
if (It is not None):
curtor = 1.5*It[-1] - 0.5*It[-2]
s_half = self.vmecOutputObject.s_half
if (self.vmecOutputObject.ns>101):
s_spline = np.linspace(0,1,101)
ds_spline = s_spline[1]-s_spline[0]
s_spline_half = s_spline - 0.5*ds_spline
s_spline_half = np.delete(s_spline_half,0)
It_spline = \
interpolate.InterpolatedUnivariateSpline(s_half,It)
It = It_spline(s_spline_half)
s_half = s_spline_half
inputObject_new.curtor = curtor
inputObject_new.ac_aux_f = list(It)
inputObject_new.ac_aux_s = list(s_half)
inputObject_new.pcurr_type = "line_segment_I"
if (boundary is not None):
inputObject_new.rbc = self.boundary[0:self.mnmax]
inputObject_new.zbs = self.boundary[self.mnmax::]
else:
inputObject_new = None
directory_name = None
input_file = None
# Call VMEC with revised input file
inputObject_new = MPI.COMM_WORLD.bcast(inputObject_new,root=0)
directory_name = MPI.COMM_WORLD.bcast(directory_name,root=0)
input_file = MPI.COMM_WORLD.bcast(input_file,root=0)
os.chdir(directory_name)
exit_code = self.call_vmec(inputObject_new,animec=True)
if (exit_code == 0):
# Read from new equilibrium
outputFileName = "wout_"+input_file[6::]+".nc"
vmecOutput_new = \
VmecOutput(outputFileName,self.ntheta,self.nzeta)
else:
vmecOutput_new = None
os.chdir("..")
return exit_code, vmecOutput_new
def update_boundary_opt(self,boundary_opt_new):
"""
Updates boundary harmonics at new evaluation point
Parameters
----------
boundary_opt_new (float array) : new boundary to evaluate
(same size as self.boundary_opt)
"""
if (len(boundary_opt_new)!=len(self.boundary_opt)):
raise ValueError('Incorrect size of boundary_opt_new.')
rmnc_opt_new = boundary_opt_new[0:self.mnmax_sensitivity]
zmns_opt_new = boundary_opt_new[self.mnmax_sensitivity::]
# m = 0, n = 0 mode excluded in boundary_opt
if (np.min(self.xm_sensitivity)==0 and
np.min(np.abs(self.xn_sensitivity)) == 0):
zmns_opt_new = np.append([0],zmns_opt_new)
self.boundary_opt = np.copy(boundary_opt_new)
rmnc_new = self.boundary[0:self.mnmax]
zmns_new = self.boundary[self.mnmax::]
for imn in range(self.mnmax_sensitivity):
cond = np.logical_and((self.xm == self.xm_sensitivity[imn]),\
(self.xn == self.xn_sensitivity[imn]))
if (any(cond)):
rmnc_new[cond] = rmnc_opt_new[imn]
zmns_new[cond] = zmns_opt_new[imn]
self.boundary = np.hstack((rmnc_new,zmns_new))
def input_objective(self,boundary=None,which_objective='volume',\
update=True,which_kappa=1,**kwargs):
"""
Evaluates objective from VMEC input namelist.
Parameters
----------
boundary (np array) : coefficients defining boundary in the format of
self.boundary_opt. If None, objective
will be computed from self.vmecOutputObject
which_objective (string) : string defining which input objective. Must
be in self.input_objectives
update (boolean) : If True, self.vmecInputObject will be updated.
which_kappa (int) : (1 or 2) Only relevant for 'curvature_integrated'.
Indicates which principal curvature for evaluation.
**kwargs (dict) : additional keyword arguments to pass to objective
function. See input.py for details.
Returns
----------
objective_function (float) : value of objective function
"""
if (which_objective not in self.input_objectives):
raise ValueError('''incorrect value of which_objective in
input_objective.''')
elif self.verbose:
print("Evaluating ",which_objective," objective.")
if (boundary is None):
boundary = self.boundary_opt
if (np.shape(boundary)!=np.shape(self.boundary_opt)):
raise ValueError('''Error! input_objective called with
incorrect boundary shape.''')
if ((np.copy(boundary) != self.boundary_opt).any()
or self.vmecInputObject is None):
# Save old boundary if update=False
if (update==False):
boundary_old = np.copy(self.boundary_opt)
# Update equilibrium count
self.counter += 1
# Update boundary
self.update_boundary_opt(boundary)
directory_name = self.name+"_"+str(self.counter)
if (self.verbose):
print("Creating new input object in "+directory_name)
# Make directory for VMEC evaluations
try:
os.mkdir(directory_name)
except OSError as exc:
if exc.errno != errno.EEXIST:
if (self.verbose):
print('Warning. Directory '+directory_name+\
' already exists. Contents may be overwritten.')
raise
pass
input_file = "input."+directory_name
# Copy input object and edit with new boundary
vmecInputObject = copy.deepcopy(self.vmecInputObject)
vmecInputObject.input_filename = input_file
vmecInputObject.rbc = np.copy(self.boundary[0:self.mnmax])
vmecInputObject.zbs = np.copy(self.boundary[self.mnmax::])
os.chdir(directory_name)
vmecInputObject.print_namelist()
os.chdir('..')
# Reset old boundary
if (update==False):
self.update_boundary_opt(boundary_old)
else:
self.vmecInputObject = vmecInputObject
else:
vmecInputObject = copy.deepcopy(self.vmecInputObject)
if (which_objective == 'volume'):
objective_function = vmecInputObject.volume()
elif (which_objective == 'area'):
objective_function = vmecInputObject.area()
elif (which_objective == 'jacobian'):
objective_function = vmecInputObject.jacobian()
elif (which_objective == 'radius'):
[x,y,z,R] = vmecInputObject.position()
objective_function = R
elif (which_objective == 'normalized_jacobian'):
objective_function = vmecInputObject.normalized_jacobian()
elif (which_objective == 'summed_proximity'):
objective_function = vmecInputObject.summed_proximity(**kwargs)
elif (which_objective == 'summed_radius'):
objective_function = \
vmecInputObject.summed_radius_constraint(**kwargs)
elif (which_objective == 'curvature'):
objective_function = \
vmecInputObject.surface_curvature_metric(**kwargs)
elif (which_objective == 'curvature_integrated'):
[metric1,metric2] = \
vmecInputObject.surface_curvature_metric_integrated(**kwargs)
if (which_kappa == 1):
objective_function = metric1
elif (which_kappa == 2):
objective_function = metric2
else:
raise ValueError('Incorrect value of which_kappa')
return objective_function
def vmec_objective(self,boundary=None,which_objective='iota',
update=True,**kwargs):
"""
Evaluates vmec objective function with prescribed boundary
Parameters
----------
boundary (np array) : coefficients defining boundary in the format of
self.boundary_opt. If None, objective
will be computed from self.vmecOutputObject
which_objective (string) : string defining which vmec objective. Must
be in self.vmec_objectives
update (boolean) : If True, self.vmecOutputObject will be updated.
**kwargs (dict) : additional keyword arguments to pass to objective
function. See output.py for details.
Returns
----------
objective function (float) : value of objective function defined through
which_objective and weight_function. If VMEC completes with an
error, the value of the objective function is set to 1e12.
"""
if (which_objective not in self.vmec_objectives):
raise ValueError('''Error! vmec_objectives called with incorrect value
of which_objective''')
elif self.verbose:
print("Evaluating ",which_objective," objective.")
if (boundary is None):
boundary = self.boundary_opt
if (np.shape(boundary)!=np.shape(self.boundary_opt)):
raise ValueError('''Error! vmec_objectives called with incorrect
boundary shape.''')
# Save old boundary if update=False
if (update==False):
boundary_old = np.copy(self.boundary_opt)
# Evaluate new equilibrium if necessary
error_code = 0
if ((boundary is not None and
(np.copy(boundary)!=self.boundary_opt).any())
or self.vmecOutputObject is None):
[error_code,vmecOutputObject] = self.evaluate_vmec(\
boundary=boundary,update=update)
if (error_code != 0 and self.verbose):
print('''VMEC returned with an error when evaluating new
boundary in vmec_objective.''')
print('Objective function will be set to 1e12')
else:
vmecOutputObject = self.vmecOutputObject
# Reset old boundary
if (update==False):
self.update_boundary_opt(boundary_old)
if (error_code == 0):
if (which_objective == 'iota'):
objective_function = \
vmecOutputObject.iota_objective(**kwargs)
elif (which_objective == 'iota_target'):
objective_function = \
vmecOutputObject.iota_target_objective(**kwargs)
elif (which_objective == 'iota_prime'):
objective_function = \
vmecOutputObject.iota_prime_objective(**kwargs)
elif (which_objective == 'well'):
objective_function = \
vmecOutputObject.well_objective(**kwargs)
elif (which_objective == 'well_ratio'):
objective_function = \
vmecOutputObject.well_ratio_objective(**kwargs)
elif (which_objective == 'volume'):
objective_function = vmecOutputObject.volume
elif (which_objective == 'area'):
objective_function = \
vmecOutputObject.area(vmecOutputObject.ns-1)
elif (which_objective == 'jacobian'):
objective_function = vmecOutputObject.jacobian(-1)
elif (which_objective == 'radius'):
[x,y,z,R] = vmecOutputObject.position(-1)
objective_function = R
elif (which_objective == 'normalized_jacobian'):
objective_function = vmecOutputObject.normalized_jacobian(-1)
elif (which_objective == 'modB'):
objective_function = vmecOutputObject.modB_objective()
elif (which_objective == 'modB_vol'):
objective_function = \
vmecOutputObject.modB_objective_volume()
elif (which_objective == 'axis_ripple'):
objective_function = \
vmecOutputObject.ripple_objective(**kwargs)
return objective_function
else:
return 1e12
def vmec_shape_gradient(self,boundary=None,vmecOutputObject=None,
which_objective='iota',update=True,
weight_function=axis_weight,iota_target=None,
weight_function1=axis_weight,
weight_function2=axis_weight):
"""
Evaluates shape gradient for objective function
Parameters
----------
boundary (np array) : coefficients defining boundary in the format of
self.boundary_opt. If None, objective
will be computed from self.vmecOutputObject
vmecOutputObject (VmecOutput) : instance of VmecOuput for evaluation of
objective. If None, self.vmecOutputObject will be used.
which_objective (string) : Which objective to compute shape gradient
of. Must be in self.vmec_objectives.
update (boolean) : If True, self.vmecOutputObject will be updated.
weight_function (function) : weight function w(s) which takes the
normalized flux as input and returns a scalar (see axis_weight
above for example). This is only used for iota, iota_prime,
iota_target, well, and axis_ripple targets.
iota_target (np array or float) : target iota profile. If np array,
must be the same length as the half grid. Only used for
iota_target object.
weight_function1 (function) : weight function w(s) which takes the
normalized flux as input and returns a scalar (see axis_weight
above for example). This only used for the well_ratio objective.
weight_function2 (function) : weight function w(s) which takes the
normalized flux as input and returns a scalar (see axis_weight
above for example). This only used for the well_ratio objective.
Returns
----------
shape_gradient (np array) : shape gradient defined on nzeta,ntheta
grid of specified objective function
"""
if (which_objective not in self.vmec_objectives):
raise ValueError('''incorrect value of which_objective in
vmec_shape_gradient.''')
if (self.verbose):
print("Evaluating "+which_objective+" shape gradient.")
if (which_objective in ['iota','iota_prime','iota_target']):
delta = self.delta_curr
elif (which_objective in ['well','well_ratio','axis_ripple']):
delta = self.delta_pres
elif (which_objective not in ['area','modB_vol']):
raise ValueError('''Error! vmec_shape_gradient called with incorrect
value of '''+which_objective)
if (boundary is not None):
if (np.size(boundary)!=np.size(self.boundary_opt)):
raise ValueError('''Error! vmec_shape_gradient called with incorrect
boundary shape.''')
# Evaluate base equilibrium if necessary
if (vmecOutputObject is None and boundary is not None \
and np.any(np.copy(boundary) != self.boundary_opt)):
[error_code, vmecOutputObject] = self.evaluate_vmec(\
boundary=boundary,update=update)
if (error_code != 0):
raise RuntimeError('''Unable to evaluate base VMEC equilibrium
in vmec_shape_gradient.''')
elif (vmecOutputObject is None):
vmecOutputObject = self.vmecOutputObject
if (which_objective == 'iota'):
if (weight_function is None):
raise RuntimeError('''weight_function must be specified when
computing iota objective''')
[Bx, By, Bz, theta_arclength] = \
vmecOutputObject.B_on_arclength_grid()
It_half = vmecOutputObject.current()
It_new = It_half + \
delta*weight_function(self.vmecOutputObject.s_half)
[error_code, vmecOutput_delta] = self.evaluate_vmec(It=It_new)
if (error_code != 0):
raise RuntimeError('''Unable to evaluate VMEC equilibrium with
current perturbation in vmec_shaep_gradient.''')
[Bx_delta, By_delta, Bz_delta, theta_arclength_delta] = \
vmecOutput_delta.B_on_arclength_grid()
for izeta in range(self.vmecOutputObject.nzeta):
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bx_delta[izeta,:])
Bx_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],By_delta[izeta,:])
By_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bz_delta[izeta,:])
Bz_delta[izeta,:] = f(theta_arclength[izeta,:])
deltaB_dot_B = ((Bx_delta-Bx)*Bx + (By_delta-By)*By + \
(Bz_delta-Bz)*Bz)/delta
shape_gradient = deltaB_dot_B/(2*np.pi*self.vmecOutputObject.mu0)
elif (which_objective == 'iota_target'):
if (weight_function is None):
raise RuntimeError('''weight_function must be specified when
computing iota_target objective''')
[Bx, By, Bz, theta_arclength] = \
vmecOutputObject.B_on_arclength_grid()
It_half = vmecOutputObject.current()
weight_half = weight_function(self.vmecOutputObject.s_half)
iota = self.vmecOutputObject.iota
if (iota_target is None):
iota_target = np.zeros(np.shape(self.vmecOutputObject.s_half))
perturbation = weight_half*(iota-iota_target)\
/(self.vmecOutputObject.psi[-1]*self.vmecOutputObject.sign_jac)
It_new = It_half + delta*perturbation
[error_code, vmecOutput_delta] = self.evaluate_vmec(It=It_new)
if (error_code != 0):
raise RuntimeError('''Unable to evaluate VMEC equilibrium with
current perturbation in vmec_shape_gradient.''')
[Bx_delta, By_delta, Bz_delta, theta_arclength_delta] = \
vmecOutput_delta.B_on_arclength_grid()
for izeta in range(self.vmecOutputObject.nzeta):
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bx_delta[izeta,:])
Bx_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],By_delta[izeta,:])
By_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bz_delta[izeta,:])
Bz_delta[izeta,:] = f(theta_arclength[izeta,:])
deltaB_dot_B = ((Bx_delta-Bx)*Bx + (By_delta-By)*By + \
(Bz_delta-Bz)*Bz)/delta
shape_gradient = deltaB_dot_B/(2*np.pi*self.vmecOutputObject.mu0)
elif (which_objective == 'iota_prime'):
if (weight_function is None):
raise RuntimeError('''weight_function must be specified when
computing iota_prime objective''')
[Bx, By, Bz, theta_arclength] = \
vmecOutputObject.B_on_arclength_grid()
It_half = vmecOutputObject.current()
weight_half = weight_function(self.vmecOutputObject.s_half)
weight_full = weight_function(self.vmecOutputObject.s_full)
# Derivative of weight function on half grid
weight_prime = \
(weight_full[1::]-weight_full[0:-1])/self.vmecOutputObject.ds
# Derivative of iota on half grid
iota_prime_half = (self.vmecOutputObject.iotaf[1::]
-self.vmecOutputObject.iotaf[0:-1]) \
/self.vmecOutputObject.ds
# Derivative of iota on full grid
iota_prime_full = np.zeros(self.vmecOutputObject.ns_half,1)
iota_prime_full[1:-1] = (self.vmecOutputObject.iota[1::]
-self.vmecOutputObject.iota[0:-1]) \
/self.vmecOutputObject.ds
iota_prime_full[0] = 1.5*self.vmecOutputObject.iota[0] \
-0.5*self.vmecOutputObject.vmecOutputObject.iota[1]
iota_prime_full[-1] = 1.5*self.vmecOutputObject.iota[-1] \
-0.5*self.vmecOutputObject.iota[-2]
# Second derivative on half grid
iota_prime_prime_half = (iota_prime_full[1::]\
-iota_prime_full[0:-1])/self.vmecOutputObject.ds
perturbation = -(iota_prime_prime_half*weight_half \
+ iota_prime_half*weight_prime)
It_new = It_half + delta*perturbation
[error_code, vmecOutput_delta] = self.evaluate_vmec(It=It_new)
if (error_code != 0):
raise RuntimeError('''Unable to evaluate VMEC equilibrium with
current perturbation in vmec_shaep_gradient.''')
[Bx_delta, By_delta, Bz_delta, theta_arclength_delta] = \
vmecOutput_delta.B_on_arclength_grid()
for izeta in range(self.vmecOutputObject.nzeta):
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bx_delta[izeta,:])
Bx_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],By_delta[izeta,:])
By_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bz_delta[izeta,:])
Bz_delta[izeta,:] = f(theta_arclength[izeta,:])
deltaB_dot_B = ((Bx_delta-Bx)*Bx + (By_delta-By)*By + \
(Bz_delta-Bz)*Bz)/delta
shape_gradient = deltaB_dot_B/(2*np.pi*self.vmecOutputObject.mu0)
elif (which_objective == 'well_ratio'):
if (weight_function1 is None or weight_function2 is None):
raise RuntimeError('''weight_function1 and weight_function2 must
be specified when computing well_ratio objective''')
[Bx, By, Bz, theta_arclength] = \
vmecOutputObject.B_on_arclength_grid()
pres = vmecOutputObject.pres
numerator = np.sum(weight_function1(self.vmecOutputObject.s_half)
* self.vmecOutputObject.vp)
denominator = np.sum(weight_function2(self.vmecOutputObject.s_half)
* self.vmecOutputObject.vp)
fW = numerator/denominator
perturbation = (weight_function1(self.vmecOutputObject.s_half)
- fW * weight_function2(self.vmecOutputObject.s_half)) \
/ (denominator * self.vmecOutputObject.ds * 4 * np.pi * np.pi)
pres_new = pres + delta*perturbation
[error_code, vmecOutput_delta] = self.evaluate_vmec(pres=pres_new)
if (error_code != 0):
raise RuntimeError('''Unable to evaluate VMEC equilibrium with
pressure perturbation in vmec_shaep_gradient.''')
[Bx_delta, By_delta, Bz_delta, theta_arclength_delta] = \
vmecOutput_delta.B_on_arclength_grid()
for izeta in range(self.vmecOutputObject.nzeta):
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bx_delta[izeta,:])
Bx_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],By_delta[izeta,:])
By_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bz_delta[izeta,:])
Bz_delta[izeta,:] = f(theta_arclength[izeta,:])
deltaB_dot_B = ((Bx_delta-Bx)*Bx + (By_delta-By)*By + \
(Bz_delta-Bz)*Bz)/delta
shape_gradient = deltaB_dot_B/(self.vmecOutputObject.mu0) \
+ perturbation[-1]
elif (which_objective == 'well'):
if (weight_function is None):
raise RuntimeError('''weight_function must be specified when
computing well objective''')
[Bx, By, Bz, theta_arclength] = \
vmecOutputObject.B_on_arclength_grid()
pres = vmecOutputObject.pres
pres_new = pres + \
delta*weight_function(self.vmecOutputObject.s_half) \
/ (self.vmecOutputObject.psi[-1])
[error_code, vmecOutput_delta] = self.evaluate_vmec(pres=pres_new)
if (error_code != 0):
raise RuntimeError('''Unable to evaluate VMEC equilibrium with
pressure perturbation in vmec_shaep_gradient.''')
[Bx_delta, By_delta, Bz_delta, theta_arclength_delta] = \
vmecOutput_delta.B_on_arclength_grid()
for izeta in range(self.vmecOutputObject.nzeta):
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bx_delta[izeta,:])
Bx_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],By_delta[izeta,:])
By_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bz_delta[izeta,:])
Bz_delta[izeta,:] = f(theta_arclength[izeta,:])
deltaB_dot_B = ((Bx_delta-Bx)*Bx + (By_delta-By)*By + \
(Bz_delta-Bz)*Bz)/delta
shape_gradient = deltaB_dot_B/(self.vmecOutputObject.mu0) \
+ weight_function(1)/(self.vmecOutputObject.psi[-1])
elif (which_objective == 'axis_ripple'):
if (weight_function is None):
raise RuntimeError('''weight_function must be specified when
computing axis_ripple objective''')
[Bx, By, Bz, theta_arclength] = \
vmecOutputObject.B_on_arclength_grid()
pperp_end = vmecOutputObject.ripple_pperp(weight_function)
pres_new = weight_function(self.vmecOutputObject.s_half)
if (self.vmecInputObject.ncurr==1):
delta_curr = np.zeros(np.shape(self.vmecOutputObject.s_half))
Bbar = vmecOutputObject.ripple_Bbar(weight_function)
normalization = \
vmecOutputObject.ripple_normalization(weight_function)
ripple = vmecOutputObject.ripple_axis(weight_function)
for isurf in range(self.vmecOutputObject.ns_half):
B = vmecOutputObject.modB(isurf = isurf)
[Bsubtheta, Bsubzeta] = \
vmecOutputObject.B_covariant(isurf=isurf)
delta_curr[isurf] = \
np.sum(((B - Bbar)/B - 0.5*ripple)*Bsubtheta) \
* vmecOutputObject.dtheta * vmecOutputObject.dzeta \
* vmecOutputObject.nfp
delta_curr *= self.delta_curr \
* weight_function(vmecOutputObject.s_half) \
/ (0.5 * normalization)
It_half = vmecOutputObject.current()
It_new = delta_curr + It_half
[error_code, vmecOutput_delta] = self.evaluate_animec(pres=pres_new,\
bcrit=delta,It=It_new)
if (error_code != 0):
raise RuntimeError('''Unable to evaluate VMEC equilibrium with
pressure perturbation in vmec_shape_gradient.''')
[Bx_delta, By_delta, Bz_delta, theta_arclength_delta] = \
vmecOutput_delta.B_on_arclength_grid()
for izeta in range(self.vmecOutputObject.nzeta):
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bx_delta[izeta,:])
Bx_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],By_delta[izeta,:])
By_delta[izeta,:] = f(theta_arclength[izeta,:])
f = interpolate.InterpolatedUnivariateSpline(\
theta_arclength_delta[izeta,:],Bz_delta[izeta,:])
Bz_delta[izeta,:] = f(theta_arclength[izeta,:])
deltaB_dot_B = ((Bx_delta-Bx)*Bx + (By_delta-By)*By + \
(Bz_delta-Bz)*Bz)/delta
shape_gradient = deltaB_dot_B + pperp_end
elif (which_objective == 'area'):
shape_gradient = \
vmecOutputObject.mean_curvature(vmecOutputObject.ns-1)
elif (which_objective == 'modB_vol'):
modB = vmecOutputObject.modB(isurf=vmecOutputObject.ns,
full=True)
shape_gradient = 0.5 * modB * modB
return shape_gradient
def input_objective_grad(self,boundary=None,\
which_objective='volume',update=True,\
which_kappa=1,**kwargs):
"""
Evaluates gradient of input objective
Parameters
----------
boundary (np array) : coefficients defining boundary in the format of
self.boundary_opt. If None, objective
will be computed from self.vmecInputObject
which_objective (string) : Which objective to compute shape gradient
of. Must be in self.input_objectives.
update (boolean) : If True, self.vmecInputObject will be updated.
which_kappa (int) : (1 or 2) Only relevant for 'curvature_integrated'.
Indicates which principal curvature for evaluation.
**kwargs (dict) : additional keyword arguments to pass to objective
gradient function. See input.py for details.
Returns
----------
gradient (np array) : gradient of scalar objective with respect to
the boundary harmonics. Same shape as self.boundary_opt
"""
if (which_objective not in self.input_objectives):
raise ValueError('''Error! input_objective_grad called with
incorrect value of which_objective.''')
elif self.verbose:
print("Evaluating ",which_objective," objective.")
if (boundary is not None):
if (np.shape(boundary)!=np.shape(self.boundary_opt)):
raise ValueError('''Error! input_objective_grad called with
incorrect boundary shape.''')