forked from bendudson/eqdsk2helena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqdsk2helena.pro
316 lines (264 loc) · 10.8 KB
/
eqdsk2helena.pro
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
; Convert a G-EQDSK format file into input for HELENA
;
;
PRO eqdsk2helena, input, psin=psin, mfm=mfm, nrmap=nrmap, npmap=npmap, new=new
safe_colors, /first
IF NOT KEYWORD_SET(psin) THEN psin=0.995
IF NOT KEYWORD_SET(mfm) THEN mfm = 64
IF NOT KEYWORD_SET(nrmap) THEN nrmap = 101
IF NOT KEYWORD_SET(npmap) THEN npmap = 129
IF NOT is_pow2(mfm) THEN BEGIN
PRINT, "ERROR: mfm must be a power of 2"
RETURN
ENDIF
IF NOT is_pow2(npmap-1) THEN BEGIN
PRINT, "ERROR: npmap must be 2^n + 1"
RETURN
ENDIF
; Read the grid input
grid = read_neqdsk(input)
psi_boundary = grid.sibdry
psi_axis = grid.simagx
; Contour psi to get the plasma boundary
contour_lines, grid.psi, findgen(grid.nx), findgen(grid.ny), $
levels=[psi_axis + (psi_boundary - psi_axis)*psin], $
path_info=info, path_xy=xy
IF N_ELEMENTS(info) GT 1 THEN BEGIN
; Find the surface closest to the magnetic axis
; Need the index of the magnetic axis
ri = INTERPOL(findgen(grid.nx), grid.r[*,0], grid.rmagx)
zi = INTERPOL(findgen(grid.ny), grid.z[0,*], grid.zmagx)
ind = closest_line(info, xy, ri, zi)
info = info[ind]
ENDIF ELSE info = info[0]
CONTOUR, grid.psi, grid.r, grid.z, nlevels=50, xtit="R [m]" , ytit="Z [m]", /iso, color=1
oplot_contour, info, xy, grid.r[*,0], grid.z[0,*], /periodic, color=2, thick=1.5
; Need to create boundary points uniformly spaced in theta
rbndry = FLTARR(mfm)
zbndry = FLTARR(mfm)
FOR i=0, mfm-1 DO BEGIN
theta = !DPI*2. * DOUBLE(i) / DOUBLE(mfm)
; Create a line going from magnetic axis outwards at angle theta
r1 = [grid.rmagx, 100.*COS(theta)]
z1 = [grid.zmagx, 100.*SIN(theta)]
; Get the location of the boundary in meters
ri = REFORM(xy[0,info.offset:(info.offset+info.n-1)])
zi = REFORM(xy[1,info.offset:(info.offset+info.n-1)])
r2 = INTERPOLATE(grid.r[*,0], ri)
z2 = INTERPOLATE(grid.z[0,*], zi)
; find where the radial line intersects the boundary
hit = line_crossings(r1,z1, 0, r2, z2, 1, ncross=ncross)
IF ncross NE 1 THEN BEGIN
PRINT, "ERROR finding boundary"
STOP
ENDIF
rbndry[i] = hit[0,0]
zbndry[i] = hit[1,0]
oplot, [grid.rmagx, rbndry[i]], [grid.zmagx, zbndry[i]], color=4
ENDFOR
oplot, rbndry, zbndry, psym=1
OPENW, fid, "plasma_boundary.in", /GET_LUN
FOR i=0, mfm-1 DO BEGIN
PRINTF, fid, " "+STR(rbndry[i], format="(E24.16)")+" "+STR(zbndry[i], format="(E24.16)")
ENDFOR
CLOSE, fid
minor_radius = MEAN(SQRT((rbndry - grid.rmagx)^2 + (zbndry - grid.zmagx)^2))
eps = minor_radius / grid.rmagx ; inverse aspect ratio
npts = N_ELEMENTS(grid.fpol)
pn = FINDGEN(npts) / FLOAT(npts-1)
; Calculate Fprime and Pprime, taking derivative w.r.t. normalised psi
pprime = DERIV(pn, grid.pres)
fprime = DERIV(pn, grid.fpol)
ffprime = grid.fpol * fprime
; Save pressure gradient
OPENW, fid, "dp.in"
FOR i=0, npts-1 DO BEGIN
PRINTF, fid, STR(pprime[i])
ENDFOR
CLOSE, fid
; Save FFprime
OPENW, fid, "fdf.in"
FOR i=0, npts-1 DO BEGIN
PRINTF, fid, STR(ffprime[i])
ENDFOR
CLOSE, fid
OPENW, fid, "q_in.in"
FOR i=0, npts-1 DO BEGIN
PRINTF, fid, STR(grid.qpsi[i])
ENDFOR
CLOSE, fid
; Save XML input file
IF KEYWORD_SET(new) THEN BEGIN
OPENW, fid, "input_helena.xml"
PRINTF, fid, "<?xml version='1.0'?>"
PRINTF, fid, "<?xml-stylesheet type='text/xsl' href='./input_helena.xsl'"
PRINTF, fid, "charset='ISO-8859-1'?>"
PRINTF, fid, "<parameters>"
PRINTF, fid, "<!-- shape parameters -->"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>ishape</name>"
PRINTF, fid, " <value>2</value>"
PRINTF, fid, " <description>Read plasma boundary from plasma_boundary.in</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>mfm</name>"
PRINTF, fid, " <value>"+STR(mfm)+"</value>"
PRINTF, fid, " <description>Number of boundary points in theta</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>isol</name>"
PRINTF, fid, " <value>0</value>"
PRINTF, fid, " <description>do NOT use Soloviev equilibrium</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>ias</name>"
PRINTF, fid, " <value>1</value>"
PRINTF, fid, " <description>equilibrium is asymmetric</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>npts</name>"
PRINTF, fid, " <value>"+STR(npts)+"</value>"
PRINTF, fid, " <description>Number of pprime and ffprime points in psi</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<!-- profile parameters -->"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>p%type</name>"
PRINTF, fid, " <value>7</value>"
PRINTF, fid, " <description>Read p-prime(psi) from dp.in</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>gam%type</name>"
PRINTF, fid, " <value>7</value>"
PRINTF, fid, " <description>Read f * f-prime(psi) from fdf.in</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>cur%type</name>"
PRINTF, fid, " <value>0</value>"
PRINTF, fid, " <description>fdf instead of current profile</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<!-- global parameters -->"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>eps</name>"
PRINTF, fid, " <value>"+STR(eps)+"</value>"
PRINTF, fid, " <description>Inverse aspect ratio</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>alfa</name>"
PRINTF, fid, " <value>"+STR(psin)+"</value>"
PRINTF, fid, " <description>Normalised psi</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<!-- numerical parameters -->"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>nr</name>"
PRINTF, fid, " <value>101</value>"
PRINTF, fid, " <description>number of radial grid points</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>np</name>"
PRINTF, fid, " <value>129</value>"
PRINTF, fid, " <description>number of poloidal grid points = 2^(n_1+1) for asymmetric equilibria</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>nrmap</name>"
PRINTF, fid, " <value>"+STR(nrmap)+"</value>"
PRINTF, fid, " <description>number of radial points for mapping to final grid</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>npmap</name>"
PRINTF, fid, " <value>"+STR(npmap)+"</value>"
PRINTF, fid, " <description>number of poloidal points for mapping to final grid. Must be 2^(n_2+1) for asymmetric equilibria</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>nchi</name>"
PRINTF, fid, " <value>"+STR(npmap-1)+"</value>"
PRINTF, fid, " <description>Number of poloidal points written to equilibrium file. 2^n_3 for asymmetric equilibria</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>niter</name>"
PRINTF, fid, " <value>20</value>"
PRINTF, fid, " <description>Maximum number of iterations with fixed grid</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>nmesh</name>"
PRINTF, fid, " <value>3</value>"
PRINTF, fid, " <description>Maximum number of iterations for current or q profile</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>errcur</name>"
PRINTF, fid, " <value>1.0E-07</value>"
PRINTF, fid, " <description>Convergence criterion for ff'</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<!-- diagnostic parameters -->"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>npr1</name>"
PRINTF, fid, " <value>1</value>"
PRINTF, fid, " <description>Output profiles and grid to out_he and SI output</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>npr2</name>"
PRINTF, fid, " <value>1</value>"
PRINTF, fid, " <description>Output EQDSK file</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "<parameter>"
PRINTF, fid, " <name>nprxmgr</name>"
PRINTF, fid, " <value>1</value>"
PRINTF, fid, " <description>Controls output of profiles to xmgr-readable ASCII files</description>"
PRINTF, fid, "</parameter>"
PRINTF, fid, "</parameters>"
CLOSE, fid
ENDIF ELSE BEGIN
; Old-style XML input
OPENW, fid, "input_helena.xml"
PRINTF, fid, '<?xml version="1.0"?>'
PRINTF, fid, '<?xml-stylesheet type="text/xsl" href="./input_helena.xsl"'
PRINTF, fid, 'charset="ISO-8859-1"?>'
PRINTF, fid, "<parameters>"
PRINTF, fid, "<!-- profile parameters -->"
PRINTF, fid, " <profile_parameters>"
PRINTF, fid, " <p>"
PRINTF, fid, " <type> 7 </type>"
PRINTF, fid, " </p>"
PRINTF, fid, " <gam>"
PRINTF, fid, " <type> 7 </type>"
PRINTF, fid, " </gam>"
PRINTF, fid, " <cur>"
PRINTF, fid, " <type> 0 </type>"
PRINTF, fid, " </cur>"
PRINTF, fid, " <npts> "+STR(npts)+" </npts>"
PRINTF, fid, " </profile_parameters>"
PRINTF, fid, " <shape_parameters>"
PRINTF, fid, " <ishape> 2 </ishape>"
PRINTF, fid, " <isol> 0 </isol>"
PRINTF, fid, " <ias> 1 </ias>"
PRINTF, fid, " <mfm> "+STR(mfm)+" </mfm>"
PRINTF, fid, " </shape_parameters>"
PRINTF, fid, "<!-- global parameters -->"
PRINTF, fid, " <global_parameters>"
PRINTF, fid, " <eps> "+STR(eps)+" </eps>"
PRINTF, fid, " <alfa> "+STR(psin)+" </alfa>"
PRINTF, fid, " </global_parameters>"
PRINTF, fid, "<!-- numerical parameters -->"
PRINTF, fid, " <numerical_parameters>"
PRINTF, fid, " <nr> 101 </nr>"
PRINTF, fid, " <np> 129 </np>"
PRINTF, fid, " <nrmap> "+STR(nrmap)+" </nrmap>"
PRINTF, fid, " <npmap> "+STR(npmap)+" </npmap>"
PRINTF, fid, " <nchi> "+STR(npmap-1)+" </nchi>"
PRINTF, fid, " <niter> 30 </niter>"
PRINTF, fid, " <nmesh> 5 </nmesh>"
PRINTF, fid, " <errcur> 0.1000E-06 </errcur>"
PRINTF, fid, " </numerical_parameters>"
PRINTF, fid, "<!-- diagnostics parameters -->"
PRINTF, fid, " <diagnostics_parameters>"
PRINTF, fid, " <npr1> 1 </npr1>"
PRINTF, fid, " <npr2> 1 </npr2>"
PRINTF, fid, " <nprxmgr> 1 </nprxmgr>"
PRINTF, fid, " <ndiag> 1 </ndiag>"
PRINTF, fid, " <npl1> 0 </npl1>"
PRINTF, fid, " </diagnostics_parameters>"
PRINTF, fid, "</parameters>"
CLOSE, fid
ENDELSE
PRINT, "DONE"
STOP
END