/
read_cmod_pfile.py
134 lines (110 loc) · 3.85 KB
/
read_cmod_pfile.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
import numpy as np
from interp import *
import matplotlib.pyplot as plt
def read_cmod_pfile(p_file_name,shift_Te=False,shift_Ti=True,add_impurity=True):
impurity_charge = 5.0
f=open(p_file_name,'r')
data = f.read()
f.close()
sdata = data.split('\n')
nr = int(sdata[0].split()[0])
print "p-file resolution: nr = ", nr
# ne is electron density profile, dne is gradient of ne
ne = np.empty(0)
dne = np.empty(0)
ni = np.empty(0)
dni = np.empty(0)
te = np.empty(0)
dte = np.empty(0)
ti = np.empty(0)
dti = np.empty(0)
# psipne is the grid of psi_pol on which ne&dne above is recorded
psipne = np.empty(0)
psipni = np.empty(0)
psipte = np.empty(0)
psipti = np.empty(0)
for i in np.array(range(nr)):
temp = sdata[i+1].split()
psipne = np.append(psipne,float(temp[0]))
ne = np.append(ne,float(temp[1]))
dne = np.append(dne,float(temp[2]))
temp = sdata[nr+i+2].split()
psipte = np.append(psipte,float(temp[0]))
te = np.append(te,float(temp[1]))
dte = np.append(dte,float(temp[2]))
temp = sdata[2*nr+i+3].split()
psipni = np.append(psipni,float(temp[0]))
ni = np.append(ni,float(temp[1]))
dni = np.append(dni,float(temp[2]))
temp = sdata[3*nr+i+4].split()
psipti = np.append(psipti,float(temp[0]))
ti = np.append(ti,float(temp[1]))
dti = np.append(dti,float(temp[2]))
if shift_Te:
psipte = psipte + 0.005
if shift_Ti:
psipti = psipti + 0.005
#plt.plot(psipne,'x')
#plt.plot(psipni,'r.')
#plt.show()
#psi0 is normalized psi_pol in [0,1] with 1000 points
#quantities with _out are interpolated on psi0 grid
#print "length of arrays",len(ne),len(ni),len(te),len(ti)
psi0 = np.arange(1000)/999.0
#psi0 = np.linspace(0.0,1.0,len(ne))
ne_out = interp(psipne,ne,psi0)
te_out = interp(psipte,te,psi0)
ni_out = interp(psipni,ni,psi0)
ti_out = interp(psipti,ti,psi0)
dne_out = interp(psipne,dne,psi0)
dte_out = interp(psipte,dte,psi0)
dni_out = interp(psipni,dni,psi0)
dti_out = interp(psipti,dti,psi0)
if add_impurity:
nz_out = np.empty(len(ne_out))
for i in range(len(ne_out)):
nz_out[i] = (ne_out[i]-ni_out[i])/impurity_charge
# output quantities are psi_pol, ne, te, ni, ti
# grid: even psi_pol_norm
# resolution: 1000
return psi0, ne_out, te_out, ni_out, ti_out, nz_out
def read_cmod_pfile_raw(p_file_name):
impurity_charge = 5.0
f=open(p_file_name,'r')
data = f.read()
f.close()
sdata = data.split('\n')
nr = int(sdata[0].split()[0])
print "p file resolution: nr = ", nr
# ne is electron density profile, dne is gradient of ne
ne = np.empty(0)
dne = np.empty(0)
ni = np.empty(0)
dni = np.empty(0)
te = np.empty(0)
dte = np.empty(0)
ti = np.empty(0)
dti = np.empty(0)
# psipne is the grid of psi_pol on which ne&dne above is recorded
psipne = np.empty(0)
psipni = np.empty(0)
psipte = np.empty(0)
psipti = np.empty(0)
for i in np.array(range(nr)):
temp = sdata[i+1].split()
psipne = np.append(psipne,float(temp[0]))
ne = np.append(ne,float(temp[1]))
dne = np.append(dne,float(temp[2]))
temp = sdata[nr+i+2].split()
psipte = np.append(psipte,float(temp[0]))
te = np.append(te,float(temp[1]))
dte = np.append(dte,float(temp[2]))
temp = sdata[2*nr+i+3].split()
psipni = np.append(psipni,float(temp[0]))
ni = np.append(ni,float(temp[1]))
dni = np.append(dni,float(temp[2]))
temp = sdata[3*nr+i+4].split()
psipti = np.append(psipti,float(temp[0]))
ti = np.append(ti,float(temp[1]))
dti = np.append(dti,float(temp[2]))
return psipne,ne,psipte,te,psipni,ni,psipti,ti