-
Notifications
You must be signed in to change notification settings - Fork 1
/
equilibrium.py
executable file
·605 lines (455 loc) · 17.1 KB
/
equilibrium.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
##
# Requires the NumPy library
#
import math
import numpy as np
import copy as cp
try:
# erf function available from Python 3.2
from math import erf
except ImportError:
print('erf import failed, use self-defined method')
# Try SciPy
try:
from scipy.special import erf
except ImportError:
# No erf function, so define
def erf(x):
# save the sign of x
sign = 1 if x >= 0 else -1
x = abs(x)
# constants
a1 = 0.254829592
a2 = -0.284496736
a3 = 1.421413741
a4 = -1.453152027
a5 = 1.061405429
p = 0.3275911
# A&S formula 7.1.26
t = 1.0/(1.0 + p*x)
y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*math.exp(-x*x)
return sign*y # erf(-x) = -erf(x)
try:
from scipy.interpolate import interp1d
except:
print('interp1d import failed, use self-defined method')
# Define interpolation functor using NumPy routine
from numpy import interp
class interp1d:
""" 1-D interpolation using NumPy's interp function
"""
def __init__(self, x, y, copy=True):
if len(x) != len(y):
raise ValueError("Length of x and y must be equal")
# Store x & y, either just references, or a copy of the data
self._xp = np.array(x, copy=copy)
self._yp = np.array(y, copy=copy)
def __call__(self, x):
return interp(x, self._xp, self._yp)
try:
from scipy.integrate import simps
def integrate(f, a, b, n=301):
x = np.linspace(a, b, n)
delx = (b-a)/len(x)
y = f(x)
return simps(y, dx=delx)
except ImportError:
print('integrate import failed, use self-defined method')
# Define simple integrate function
def integrate(f, a, b, n=100):
"""Approximate the definite integral of f from a to b by Simpson's rule"""
if n % 2 != 0:
raise ValueError("n must be even!")
h = (float(b) - a)/n
si = 0.0
sp = 0.0
for i in range(1, n, 2):
xk = a + i*h
si += f(xk)
for i in range(2, n, 2):
xk = a + i*h
sp += f(xk)
s = 2*sp + 4*si + f(a) + f(b)
return (h/3)*s
# print("Using Simpson's rule for integration. Install SciPy for better method")
class FluxSurface:
""" Describes a single flux surface
and provides some useful operations on it
Public data members
-------------------
f = R * Bt
Public functions
----------------
These are created by __init__
R(theta) Major radius in meters
Z(theta) Height in meters
B(theta) Magnetic field in Teslas
Bp(theta) Poloidal magnetic field
Bt(theta) Toroidal magnetic field
Private data members
--------------------
Implementation details which may change
_R, _Z = arrays of (R,Z) coordinates [m]
_Bp = Poloidal field [T]
_Bt = Toroidal field [T]
_B = Field magnitude [T]
_ntheta = Number of points in the arrays
_theta = array of theta values on each point
"""
def __init__(self, f, r, z, Bp, theta=None):
""" Initialise
"""
self.f = f
# Check sizes of input arrays
self._ntheta = np.size(r)
if (np.size(z) != self._ntheta) or (np.size(Bp) != self._ntheta):
raise ValueError("r, z, and Bp arrays must have the same size")
# Copy input arrays to prevent modification from outside
self._R = np.copy(r)
self._Z = np.copy(z)
self._Bp = np.copy(Bp)
# Calculate other components of B
self._Bt = f / r
self._B = np.sqrt(self._Bt**2 + self._Bp**2)
if theta is None:
# Define theta to be equally spaced (arbitrary) in range [0,2pi]
self._theta = np.linspace(0.0, 2.*np.pi, num=self._ntheta, endpoint=False)
else:
if np.size(theta) != self._ntheta:
raise ValueError("theta must have the same number of points as r,z and Bp")
self._theta = theta
self._dldt = np.zeros(self._ntheta)
dtheta = 2.*np.pi / self._ntheta
for i in range(self._ntheta):
drdt = (r[ (i+1) % self._ntheta ] - r[ (i-1) % self._ntheta ]) / (2.*dtheta)
dzdt = (z[ (i+1) % self._ntheta ] - z[ (i-1) % self._ntheta ]) / (2.*dtheta)
self._dldt[i] = np.sqrt(drdt**2 + dzdt**2)
# Create functions for external access to values
self.R = interpPeriodic(self._theta, self._R, copy=False)
self.Z = interpPeriodic(self._theta, self._Z, copy=False)
self.B = interpPeriodic(self._theta, self._B, copy=False)
self.Bp = interpPeriodic(self._theta, self._Bp, copy=False)
self.Bt = interpPeriodic(self._theta, self._Bt, copy=False)
self._dldtheta = interpPeriodic(self._theta, self._dldt, copy=False)
def max(self, var):
""" Maximum value of a variable over theta
Parameters
----------
var(theta) is a callable object
"""
# Only sample at the grid points
return max([var(theta) for theta in self._theta])
def min(self, var):
""" Maximum value of a variable over theta
Parameters
----------
var(theta) is a callable object
"""
# Only sample at the grid points
return min([var(theta) for theta in self._theta])
def integral(self, var):
""" Flux surface integral
result = int var dl
where dl is the poloidal arc length
"""
# Integrate ( var * dl/dtheta ) dtheta
return integrate(lambda x: var(x) * self._dldtheta(x), 0, 2*np.pi )
def deriv(self, var, dtheta=0.01):
""" Flux surface derivative
result = d/dl(var)
result(theta)
where dl is the poloidal arc length
"""
# Define a function which returns derivative
# of var at theta, using central differencing
def dvardl(theta):
dvdtheta = (var(theta + dtheta) - var(theta - dtheta))/(2.*dtheta)
return dvdtheta / self._dldtheta(theta)
# Return this new function
return dvardl
def average(self, var):
""" Flux-surface average a variable
Input can be either an array, or a function
which returns the value as a function of poloidal angle
"""
if "_avintbp" not in self.__dict__:
# Cache this integral as used a lot
self._avintbp = self.integral(lambda x: 1./self.Bp(x))
return self.integral(lambda x: var(x) / self.Bp(x)) / self._avintbp
###### Useful functions
def Bsqav(self):
""" Calculate < B^2 >
"""
return self.average(lambda x: self.B(x)**2)
def eps(self):
""" Calculate inverse aspect ratio r/R
"""
if "_eps" in self.__dict__:
return self._eps
Rav = self.average(self.R)
r = self.integral(lambda x: 1.0) / (2.*np.pi)
self._eps = r/Rav
return self._eps
class Equilibrium:
""" Represents an axisymmetric tokamak equilibrium
Public functions
----------------
pressure(psi) Pressure [Pa]
dens(psi) Density [m^-3]
temp(psi) Temperature [eV]
"""
def __init__(self, fix=None):
""" Construct an Equilibrium object
Parameters
----------
If no inputs are given, an empty equilibrium is created
which can be added to afterwards
fix = Dictionary of values describing fixed-boundary solution
on flux surfaces. Assumed to contain the following keys:
'npsi' Number of points in poloidal flux psi
'npol' Number of poloidal points
'psi' Poloidal flux
"""
if fix != None:
# Check that fix has the required keys
required = ['npsi', 'npol', 'psi', 'f(psi)', 'p', 'R', 'Z', 'Bp']
for r in required:
if r not in fix:
raise ValueError("Missing key: " + r)
if "psinorm" not in fix:
# Add normalised psi, assuming core to edge
psi = fix['psi']
fix['psinorm'] = (psi - psi[0]) / (psi[-1] - psi[0])
# Make a deep copy of the data so it can't be modified afterwards
# in unpredictable ways
self.fix = cp.deepcopy(fix);
# Create a function to return the pressure
self.pressure = interp1d(self.fix['psinorm'], self.fix['p'], copy=False)
# See if density, temperature profiles also set
if "ne" in fix:
self.setDensity(self.fix["ne"])
else:
self.fix = None
def ddpsi(self, f, dpsi=0.01):
""" Calculate psi derivative
"""
psi = self.fix['psi']
pnorm = psi[-1] - psi[0] # normalised psi = psi / pnorm
def dfdpsi(psi):
fp = psi + dpsi
if fp > 1.0:
fp = 1.0
fm = psi - dpsi
if fp < 0.:
fp = 0.
return (f(fp) - f(fm)) / (fp - fm) / pnorm
return dfdpsi
def setDensity(self, dens, psi=None):
""" Sets the density profile
Parameters
----------
dens - Density in m^-3
Can be:
1. A function taking normalised psi and returning density
ne = dens(psinorm)
2. An array of values on psi grid (uniform between 0 and 1 if not)
3. A constant
psi - Optional array of normalised psi values
"""
# Check if we can call it to get a value
try:
val = dens(0.5);
densfunc = dens; # Use this new function
except:
# Not a function type
# Check if it's an array type
try:
val = dens[0]
p = psi
if p is None:
p = np.linspace(0.0, 1.0, num=len(dens), endpoint=False)
densfunc = interp1d(p, dens)
except:
# Should be a constant
val = dens
densfunc = lambda x: dens
# val should now contain a number
if np.size(val) > 1:
raise ValueError("dens argument doesn't yield a single value")
try:
test = 2.*val + val*val
except:
raise ValueError("dens argument must give a numerical value")
# Checks passed, so can set the density function
self.dens = densfunc
# Set temperature function to use the density, assuming Ti = Te
self.temp = lambda x: self.pressure(x) / (2. * 1.602e-19 * self.dens(x) )
def setTemperature(self, T, psi=None):
""" Sets the temperature profile
Parameters
----------
T - temperature in eV
Can be:
1. A function taking normalised psi and returning density
ne = T(psinorm)
2. An array of values on psi grid (uniform between 0 and 1 if not)
3. A constant
psi - Optional array of normalised psi values
"""
# Check if we can call it to get a value
try:
val = T(0.5);
Tfunc = T; # Use this new function
except:
# Not a function type
# Check if it's an array type
try:
val = T[0]
p = psi
if p is None:
p = np.linspace(0.0, 1.0, num=len(T), endpoint=False)
Tfunc = interp1d(p, T)
except:
# Should be a constant
val = T
Tfunc = lambda x: T
# val should now contain a number
if np.size(val) > 1:
raise ValueError("T argument doesn't yield a single value")
try:
test = 2.*val + val*val
except:
raise ValueError("T argument must give a numerical value")
# Checks passed, so can set the density function
self.temp = Tfunc
# Set density function to use the temperature
self.dens = lambda x: self.pressure(x) / (2. * 1.602e-19 * self.temp(x) )
def getFluxSurface(self, psi):
""" Return a FluxSurface object at a given psi
Will be created by interpolation if needed
Paramaters
----------
psi = Normalised poloidal flux
"""
if self.fix != None:
# Find the psi index this value comes before
ind = np.searchsorted(self.fix['psinorm'], psi)
psiarr = self.fix['psinorm']
if ind == 0:
ind = 1
if (ind == self.fix['npsi']):
raise ValueError("normalised psi value %e out of range %e to %e" % (psi, psiarr[0], psiarr[-1]))
# Indices
im = ind-1
ip = ind
# Weights for interpolation
wp = (psi - psiarr[im]) / (psiarr[ip] - psiarr[im])
wm = 1. - wp
# Interpolate
def inter1d(var):
return wp*var[ip] + wm*var[im]
def inter2d(var):
return wp*var[ip,:] + wm*var[im,:]
f = inter1d(self.fix['f(psi)'])
R = inter2d(self.fix['R'])
Z = inter2d(self.fix['Z'])
Bp = inter2d(self.fix['Bp'])
# Create the flux surface
f = FluxSurface(f, R, Z, Bp)
f.psinorm = psi
# Create some species
try:
from . import species as species
# Try to get profiles
d = self.dens(psi)
T = self.temp(psi)
dndpsi = self.ddpsi(self.dens)(psi)
dTdpsi = self.ddpsi(self.temp)(psi)
f.species = species.genSpecies(T, d, AA=[None, 2],
dTdpsi=dTdpsi, dndpsi=dndpsi)
except:
print("Warning: No species information")
return f
# No data
return None
def surfaces(self, psimin=0.0, psimax=1.0, n=None):
""" Iterate over flux surfaces
Keywords
--------
psimin (0.0) = Minimum normalised poloidal flux. 0 = core
psimax (1.0) = Maximum normalised poloidal flux. 1 = edge
Example
-------
for s in eq.surfaces():
<code>
"""
if self.fix != None:
# Got flux-surface data
ind0 = np.searchsorted(self.fix['psinorm'], psimin)
ind1 = np.searchsorted(self.fix['psinorm'], psimax)
# Create a generator which will iterate over surfaces
def surfgen():
for i in range(ind0, ind1):
f = (self.fix['f(psi)'])[i]
R = (self.fix['R'])[i, :]
Z = (self.fix['Z'])[i, :]
Bp = (self.fix['Bp'])[i, :]
f = FluxSurface(f, R, Z, Bp)
try:
from . import species as species
# Try to get profiles
psi = (self.fix['psinorm'])[i]
d = self.dens(psi)
T = self.temp(psi)
dndpsi = self.ddpsi(self.dens)(psi)
dTdpsi = self.ddpsi(self.temp)(psi)
f.species = species.genSpecies(T, d, AA=[None, 2],
dTdpsi=dTdpsi, dndpsi=dndpsi)
f.psinorm = psi
except:
print("Warning: No species information")
#raise
yield f
return surfgen()
# No data
return None
class interpPeriodic:
def __init__(self, x, y, copy=True, period=2.*np.pi):
"""
"""
# Store x & y, either just references, or a copy of the data
self._x = np.array(x, copy=copy)
self._y = np.array(y, copy=copy)
self._period = period
def __call__(self, x):
""" Interpolate
"""
new_x = x % self._period
# Find the index this value comes before
new_ind = np.searchsorted(self._x, new_x) % len(self._x)
# Simple linear interpolation for now
im = (new_ind - 1) % len(self._x)
ip = new_ind
xm = self._x[im]
xp = self._x[ip]
xm = np.where(xm < xp, xm, xm - self._period)
new_x = np.where(new_x > xp, new_x - self._period, new_x)
ym = self._y[im]
yp = self._y[ip]
from warnings import filterwarnings
filterwarnings('error')
try:
slope = (yp - ym) / (xp - xm)
except:
slope = 0.0
if isinstance(slope, np.ndarray):
if any(np.isnan(slope)):
slope[np.isnan(slope)] = 0.0
if any(np.isinf(slope)):
slope[np.isinf(slope)] = 0.0
else:
if np.isnan(slope):
slope = np.array(0.0)
if np.isinf(slope):
slope = np.array(0.0)
return ym + slope * (new_x - xm)