-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
288 lines (246 loc) · 12 KB
/
utils.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
#!/usr/bin/env python3
"""
This script contains all the functions that are called by the main script local_eikcoefs_gen.py
"""
import numpy as np
from scipy.signal import find_peaks
def derm(arr, ch, par='e'):
# Finite difference subroutine
# ch = 'l' means difference along the flux surface
# ch = 'r' mean difference across the flux surfaces
# par corresponds to parity of the equilibrium quantities, i.e., up-down symmetry or anti-symmetry
# par = 'e' means even parity of the arr. PARITY OF THE INPUT ARRAY
# par = 'o' means odd parity
# par is only useful for ch = 'l'
# arr must be in the range [-pi, pi]
# This routine is only valid for up-down symmetric Miller equilibria
temp = np.shape(arr)
if len(temp) == 1 and ch == 'l': #finite diff along the flux surface for a single array
#pdb.set_trace()
if par == 'e':
d1, d2 = np.shape(arr)[0], 1
arr = np.reshape(arr, (d2,d1))
diff_arr = np.zeros((d2,d1))
diff_arr[0, 0] = 0. #(arr_theta_0- - arr_theta_0+) = 0
diff_arr[0, -1] = 0. #(arr_theta_pi- - arr_theta_pi+) = 0
diff_arr[0, 1:-1] = np.diff(arr[0,:-1], axis=0) + np.diff(arr[0,1:], axis=0)
else:
d1, d2 = np.shape(arr)[0], 1
arr = np.reshape(arr, (d2,d1))
diff_arr = np.zeros((d2,d1))
diff_arr[0, 0] = 2*(arr[0, 1] - arr[0, 0])
diff_arr[0, -1] = 2*(arr[0, -1] - arr[0, -2])
diff_arr[0, 1:-1] = np.diff(arr[0,:-1], axis=0) + np.diff(arr[0,1:], axis=0)
elif len(temp) == 1 and ch == 'r': # across surfaces for a single array
#pdb.set_trace()
d1, d2 = np.shape(arr)[0], 1
diff_arr = np.zeros((d1,d2))
arr = np.reshape(arr, (d1,d2))
diff_arr[0, 0] = 2*(arr[1, 0] - arr[0, 0]) # single dimension arrays like psi, F and q don't have parity
diff_arr[-1, 0] = 2*(arr[-1, 0] - arr[-2, 0])
diff_arr[1:-1, 0] = np.diff(arr[:-1,0], axis=0) + np.diff(arr[1:,0], axis=0)
else:
d1, d2 = np.shape(arr)[0], np.shape(arr)[1]
diff_arr = np.zeros((d1,d2))
if ch == 'r': # across surfaces for multi-dim array
#pdb.set_trace()
diff_arr[0, :] = 2*(arr[1,:] - arr[0,:])
diff_arr[-1, :] = 2*(arr[-1,:] - arr[-2,:])
diff_arr[1:-1, :] = (np.diff(arr[:-1,:], axis=0) + np.diff(arr[1:,:], axis=0))
else: #along a surface for a multi-dim array
#pdb.set_trace()
if par == 'e':
#pdb.set_trace()
diff_arr[:, 0] = np.zeros((d1,))
diff_arr[:, -1] = np.zeros((d1,))
diff_arr[:, 1:-1] = (np.diff(arr[:,:-1], axis=1) + np.diff(arr[:,1:], axis=1))
else:
diff_arr[:, 0] = 2*(arr[:, 1] - arr[:, 0])
diff_arr[:, -1] = 2*(arr[:, -1] - arr[:, -2])
diff_arr[:, 1:-1] = (np.diff(arr[:,:-1], axis=1) + np.diff(arr[:,1:], axis=1))
arr = np.reshape(diff_arr, temp)
return diff_arr
def dermv(arr, brr, ch, par='e'):
# Finite difference subroutine
# brr is the independent variable arr. Needed for weighted finite-difference
# ch = 'l' means difference along the flux surface
# ch = 'r' mean difference across the flux surfaces
# par = 'e' means even parity of the arr. PARITY OF THE INPUT ARRAY
# par = 'o' means odd parity
#pdb.set_trace()
temp = np.shape(arr)
if len(temp) == 1 and ch == 'l': #finite diff along the flux surface for a single array
if par == 'e':
d1, d2 = np.shape(arr)[0], 1
arr = np.reshape(arr, (d2,d1))
brr = np.reshape(brr, (d2,d1))
diff_arr = np.zeros((d2,d1))
diff_arr[0, 0] = 0. #(arr_theta_-0 - arr_theta_+0) = 0
diff_arr[0, -1] = 0.
#diff_arr[0, 1:-1] = np.diff(arr[0,:-1], axis=0) + np.diff(arr[0,1:], axis=0)
for i in range(1, d1-1):
h1 = (brr[0, i+1] - brr[0, i])
h0 = (brr[0, i] - brr[0, i-1])
diff_arr[0, i] = (arr[0, i+1]/h1**2 + arr[0, i]*(1/h0**2 - 1/h1**2) - arr[0, i-1]/h0**2)/(1/h1 + 1/h0)
else:
#pdb.set_trace()
d1, d2 = np.shape(arr)[0], 1
arr = np.reshape(arr, (d2,d1))
brr = np.reshape(brr, (d2,d1))
diff_arr = np.zeros((d2,d1))
h1 = (np.abs(brr[0, 1]) - np.abs(brr[0, 0]))
h0 = (np.abs(brr[0, -1]) - np.abs(brr[0, -2]))
diff_arr[0, 0] = (4*arr[0, 1]-3*arr[0, 0]-arr[0,2])/(2*(brr[0, 1]-brr[0,0]))
#diff_arr[0, -1] = (-4*arr[0,-1]+3*arr[0, -2]+arr[0, -3])/(2*(brr[0, -1]-brr[0, -2]))
diff_arr[0, -1] = (-4*arr[0,-2]+3*arr[0, -1]+arr[0, -3])/(2*(brr[0, -1]-brr[0, -2]))
#diff_arr[0, -1] = 2*(arr[0, -1] - arr[0, -2])/(2*(brr[0, -1] - brr[0, -2]))
#diff_arr[0, 1:-1] = np.diff(arr[0,:-1], axis=0) + np.diff(arr[0,1:], axis=0)
for i in range(1, d1-1):
h1 = (brr[0, i+1] - brr[0, i])
h0 = (brr[0, i] - brr[0, i-1])
diff_arr[0, i] = (arr[0, i+1]/h1**2 + arr[0, i]*(1/h0**2 - 1/h1**2) - arr[0, i-1]/h0**2)/(1/h1 + 1/h0)
elif len(temp) == 1 and ch == 'r': # across surfaces for a single array
pdb.set_trace()
d1, d2 = np.shape(arr)[0], 1
diff_arr = np.zeros((d1,d2))
arr = np.reshape(arr, (d1,d2))
diff_arr[0, 0] = 2*(arr[1, 0] - arr[0, 0])/(2*(brr[1, 0] - brr[0, 0])) # single dimension arrays like psi, F and q don't have parity
diff_arr[-1, 0] = 2*(arr[-1, 0] - arr[-2, 0])/(2*(brr[-1, 0] - brr[-2, 0]))
#diff_arr[1:-1, 0] = np.diff(arr[:-1,0], axis=0) + np.diff(arr[1:,0], axis=0)
for i in range(1, d1-1):
h1 = (brr[i+1, 0] - brr[i, 0])
h0 = (brr[i, 0] - brr[i-1, 0])
diff_arr[i, 0] = (arr[i+1, 0]/h1**2 - arr[i, 0]*(1/h0**2 - 1/h1**2) - arr[i-1, 0]/h0**2)/(1/h1 + 1/h0)
else:
d1, d2 = np.shape(arr)[0], np.shape(arr)[1]
diff_arr = np.zeros((d1,d2))
if ch == 'r': # across surfaces for multi-dim array
#pdb.set_trace()
diff_arr[0, :] = 2*(arr[1,:] - arr[0,:])/(2*(brr[1, :] - brr[0, :]))
diff_arr[-1, :] = 2*(arr[-1,:] - arr[-2,:])/(2*(brr[-1, :] - brr[-2, :]))
#diff_arr[1:-1, :] = (np.diff(arr[:-1,:], axis=0) + np.diff(arr[1:,:], axis=0))
for i in range(1, d1-1):
h1 = (brr[i+1, :] - brr[i, :])
h0 = (brr[i, :] - brr[i-1, :])
diff_arr[i, :] = (arr[i+1, :]/h1**2 + arr[i, :]*(1/h0**2 - 1/h1**2) - arr[i-1, :]/h0**2)/(1/h1 + 1/h0)
else: #along a surface for a multi-dim array
#pdb.set_trace()
if par == 'e':
#pdb.set_trace()
diff_arr[:, 0] = np.zeros((d1,))
diff_arr[:, -1] = np.zeros((d1,))
#diff_arr[:, 1:-1] = (np.diff(arr[:,:-1], axis=1) + np.diff(arr[:,1:], axis=1))
for i in range(1, d2-1):
h1 = (brr[:, i+1] - brr[:, i])
h0 = (brr[:, i] - brr[:, i-1])
diff_arr[:, i] = (arr[:, i+1]/h1**2 + arr[:, i]*(1/h0**2 - 1/h1**2) - arr[:, i-1]/h0**2)/(1/h1 + 1/h0)
#pdb.set_trace()
else:
#pdb.set_trace()
diff_arr[:, 0] = 2*(arr[:, 1] - arr[:, 0])/(2*(brr[:, 1] - brr[:, 0]))
diff_arr[:, -1] = 2*(arr[:, -1] - arr[:, -2])/(2*(brr[:, -1] - brr[:, -2]))
#diff_arr[:, 1:-1] = (np.diff(arr[:,:-1], axis=1) + np.diff(arr[:,1:], axis=1))
for i in range(1, d2-1):
h1 = (brr[:, i+1] - brr[:, i])
h0 = (brr[:, i] - brr[:, i-1])
diff_arr[:, i] = (arr[:, i+1]/h1**2 + arr[:, i]*(1/h0**2 - 1/h1**2) - arr[:, i-1]/h0**2)/(1/h1 + 1/h0)
arr = np.reshape(diff_arr, temp)
return diff_arr
def intersection_chk(R, Z, R_mag_ax):
# Inputs: R and Z arrays on a common theta grid
# Output: number informing whether the surfaces intersect
r = np.sqrt((R-R_mag_ax)**2 + Z**2)
diffr = derm(r, ch = 'r')
if np.min(diffr) <= 0:
return 1
else:
return 0
def nperiod_data_extend(arr, nperiod, istheta=0, par='e'):
# the purpose of this routine is to extend the values of various parameters from their usual range of [0, pi]
# to [0, (2*nperiod-1)*pi]
# par = 'e' is the parity of arr. It can either be up-down symmetric(par = 'e') or up-down asymmetric(par='o')
if nperiod > 1:
if istheta: #for istheta par='o'
arr_dum = arr
for i in range(nperiod-1):
arr_app = np.concatenate((2*np.pi*(i+1)-arr_dum[::-1][1:], 2*np.pi*(i+1)+arr_dum[1:]))
arr = np.concatenate((arr, arr_app))
else:
if par == 'e':
arr_app = np.concatenate((arr[::-1][1:], arr[1:]))
for i in range(nperiod-1):
arr = np.concatenate((arr, arr_app))
else:
arr_app = np.concatenate((-arr[::-1][1:], arr[1:]))
for i in range(nperiod-1):
arr = np.concatenate((arr, arr_app))
return arr
def reflect_n_append(arr, ch):
"""
The purpose of this function is to increase the span of an array from [0, (2*nperiod-1)*np.pi] to [-(2*nperiod-1)*pi, (2*nperiod-1)*pi].
ch can either be 'e'(even) or 'o'(odd) depending upon the parity of the input array.
"""
rows = 1
brr = np.zeros((2*len(arr)-1, ))
if ch == 'e':
for i in range(rows):
brr = np.concatenate((arr[::-1][:-1], arr[0:]))
else :
for i in range(rows):
brr = np.concatenate((-arr[::-1][:-1],np.array([0.]), arr[1:]))
return brr
def find_optim_theta_arr(arr, theta_arr, res_par):
# The purpose of this routine is to optimize the size of the theta array so that one can keep all the important features
# with the minimum number of theta points. This routine is only used when one wants to save a grid.out file for a GS2 run
rows, colms = np.shape(arr)
idx = []
idx2 = []
idx3 = []
idx4 = []
for i in range(rows):
peaks, _ = find_peaks(arr[i], height=-1E10)
peaks = peaks.astype(np.int)
idx.append(np.ndarray.tolist(peaks))
peaks2, _ = find_peaks(-arr[i], height=-1E10)
idx.append(np.ndarray.tolist(peaks2))
idx.append([0, len(theta_arr)-1])
idx = np.sum(idx)
idx = list(set(idx))
idx.sort()
comb_peaks = np.array(idx)
diff_peaks = np.sort(np.unique(np.diff(np.sort(comb_peaks))))
diff_peaks = diff_peaks[diff_peaks>8]
#pdb.set_trace()
diff = int(diff_peaks[0]/2)
comb_peaks = np.sort(np.abs(np.concatenate((peaks-diff, peaks, peaks+diff, peaks2-diff, peaks2, peaks2+diff, np.array([0, len(theta_arr)-1-diff])))))
diff2 = int(np.mean(np.diff(comb_peaks)))-2
comb_peaks_diff = np.diff(comb_peaks)
idx_gt_diff2 = np.where(comb_peaks_diff>diff2)[0][:]
for i in idx_gt_diff2:
j = comb_peaks[i]
#pdb.set_trace()
while j < comb_peaks[i+1]:
idx2.append(j+diff2)
j = j + diff2
comb_peaks = np.concatenate((comb_peaks, np.array(idx2)))
comb_peaks = np.concatenate((comb_peaks, np.array([len(theta_arr)-1])))
comb_peaks = comb_peaks[comb_peaks < len(theta_arr)]
comb_peaks = np.sort(np.unique(comb_peaks))
#pdb.set_trace()
return theta_arr[comb_peaks]
def lambda_create(arr, fac):
arr1 = np.sort(np.unique(1/arr))
#arr1 = arr
diff_arr1 = np.diff(arr1)
req_diff = np.mean(diff_arr1)/fac
idx = [arr1[0], arr1[-1]]
diff_arr_sum = 0
i = 1
while i < len(arr1)-1:
if diff_arr1[i] <= req_diff and diff_arr_sum <= 3*req_diff:
diff_arr_sum = diff_arr_sum + req_diff
else:
idx.append(arr1[i])
diff_arr_sum = 0
i = i + 1
return np.unique(np.array(idx))