-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpar_cur_density.py
executable file
·147 lines (124 loc) · 4.23 KB
/
par_cur_density.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
import numpy as np
import scipy.interpolate as interp
from . import geqdsk as gdsk
from . import equilibrium as eq
g_fnam = 'g148712.04101'
#g_fnam = 'g149189.02400'
def get_currs(gfileNam,nw=0,nh=0,thetapnts=0,grid2G=0):
import scipy.constants
mu0 = scipy.constants.mu_0
data = gdsk.Geqdsk()
data.openFile(gfileNam)
#---- Variables ----
if(grid2G == 1):
nw = data.get('nw')
nh = data.get('nh')
thetapnts = nh
bcentr = np.abs(data.get('bcentr'))
rmaxis = data.get('rmaxis')
zmaxis = data.get('zmaxis')
Rmin = data.get('rleft')
Rmax = Rmin + data.get('rdim')
Rbdry = data.get('rbbbs').max()
Zmin = data.get('zmid') - data.get('zdim')/2.0
Zmax = data.get('zmid') + data.get('zdim')/2.0
Zlowest = data.get('zbbbs').min()
siAxis = data.get('simag')
siBry = data.get('sibry')
#---- Profiles ----
fpol = data.get('fpol')
ffunc = interp.UnivariateSpline(np.linspace(0.,1.,np.size(fpol)),fpol,s=0)
fprime = data.get('ffprime')/fpol
fpfunc = interp.UnivariateSpline(np.linspace(0.,1.,np.size(fprime)),fprime,s=0)
ffprime = data.get('ffprime')
ffpfunc = interp.UnivariateSpline(np.linspace(0.,1.,np.size(ffprime)),fprime,s=0)
pprime = data.get('pprime')
ppfunc = interp.UnivariateSpline(np.linspace(0.,1.,np.size(pprime)),pprime,s=0)
pres = data.get('pres')
pfunc = interp.UnivariateSpline(np.linspace(0.,1.,np.size(pres)),pres,s=0)
q_prof = data.get('qpsi')
qfunc = interp.UnivariateSpline(np.linspace(0.,1.,np.size(q_prof)),q_prof,s=0)
g_psi2D = data.get('psirz')
psiN1D = np.linspace(0.0,1.0,nw)
Rsminor = np.linspace(rmaxis,Rbdry,nw)
dR = (Rmax - Rmin)/float(nw - 1)
Rs1D = np.arange(Rmin, Rmax*(1.+1.e-10), dR)
dZ = (Zmax - Zmin)/float(nh - 1)
Zs1D = np.arange(Zmin, Zmax*(1.+1.e-10), dZ)
gRs,gZs = np.meshgrid(np.linspace(Rmin,Rmax,g_psi2D.shape[1]),np.linspace(Zmin,Zmax,g_psi2D.shape[0]))
psi2D = interp.griddata((gRs.flatten(0),gZs.flatten(0)),g_psi2D.flatten(0),(Rs1D[None,:],Zs1D[:,None]),method='cubic',fill_value=0.0)
Bp_R,Bp_Z = np.gradient(psi2D,dR,dZ)
Rs2D,Zs2D = np.meshgrid(Rs1D,Zs1D)
Bp_2D = np.sqrt(Bp_R**2 + Bp_Z**2)/Rs2D
psiN_2D = (psi2D - siAxis)/(siBry-siAxis)
psiN_2D[np.where(psiN_2D > 1.2)] = 1.2
theta = np.linspace(0.0,2.*np.pi,thetapnts)
Bsqrd = np.copy(psiN1D)
R_hold = np.copy(theta)
Z_hold = np.copy(theta)
Bp_hold = np.copy(theta)
psiFunc = interp.RectBivariateSpline(Rs1D,Zs1D,psiN_2D.T,kx=1,ky=1)
BpFunc = interp.RectBivariateSpline(Rs1D,Zs1D,Bp_2D.T,kx=1,ky=1)
for i in enumerate(psiN1D):
psiNVal = i[1]
for thet in enumerate(theta):
try:
Rneu,Zneu = comp_newt(psiNVal,thet[1],rmaxis,zmaxis,psiFunc)
except RuntimeError:
Rneu,Zneu = comp_bisec(psiNVal,thet[1],rmaxis,zmaxis,Zlowest,psiFunc)
R_hold[thet[0]] = Rneu
Z_hold[thet[0]] = Zneu
Bp_hold[thet[0]] = BpFunc.ev(Rneu,Zneu)
fpol_psiN = ffunc(psiNVal)*np.ones(np.size(Bp_hold))
fluxSur = eq.FluxSurface(fpol_psiN,R_hold,Z_hold,Bp_hold)
Bsqrd[i[0]] = fluxSur.Bsqav()
# parallel current calc
# jpar = J (dot) B = fprime*B^2/mu0 + pprime*fpol
jpar = (fpfunc(psiN1D)*Bsqrd/mu0 +ppfunc(psiN1D)*ffunc(psiN1D))/bcentr/1e6
#jtor [A/m**2] = R*pprime +ffprime/R/mu0
jtor = np.abs(Rsminor*ppfunc(psiN1D) +(ffpfunc(psiN1D)/Rsminor/mu0))/1.e6
curDICT={}
curDICT['jpar']=jpar
curDICT['jtor']=jtor
curDICT['psiN']=psiN1D
return curDICT
#figure();plot(psiN1D,jpar);axis([0.0,1,0,2]);
def comp_newt(psiNVal,theta,rmaxis,zmaxis,psiFunc,r_st = 0.5):
eps = 1.e-12
litr = r_st
dlitr = 1
n = 0
h=0.000005
while(np.abs(dlitr) > eps):
Rneu = litr*np.cos(theta)+rmaxis
Zneu = litr*np.sin(theta)+zmaxis
Rbac = (litr-h)*np.cos(theta)+rmaxis
Zbac = (litr-h)*np.sin(theta)+zmaxis
f = psiFunc.ev(Rneu,Zneu) - psiNVal
df = (psiFunc.ev(Rbac,Zbac) - psiFunc.ev(Rneu,Zneu))/(-1*h)
dlitr = f/df
litr -=dlitr
n +=1
if(n > 100):
raise RuntimeError("No Converg.")
return Rneu,Zneu
def comp_bisec(psiNVal,theta,rmaxis,zmaxis,Zlowest,psiFunc):
running = True
litr = 0.001
while running:
Rneu = litr*np.cos(theta)+rmaxis
Zneu = litr*np.sin(theta)+zmaxis
psineu = psiFunc.ev(Rneu,Zneu)
if((psineu < 1.2) & (Zneu < Zlowest)):
psineu = 1.2
comp = psiNVal - psineu
if(comp<0.):
litr -=0.00001
if(comp <= 1e-4):
running = False
elif(comp > 1e-4):
litr +=0.001
else:
print ("bunk!")
break
return Rneu,Zneu