-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplotting.py
More file actions
388 lines (278 loc) · 9.96 KB
/
Copy pathplotting.py
File metadata and controls
388 lines (278 loc) · 9.96 KB
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
from math import exp, pi, floor
from geometry import get_R_Z, get_flux_surfaces
from matplotlib import pyplot as plt
from profiles import profiles, evaluate_all_profiles
from reactivities import bosch_hale_reactivity, sadler_van_belle_reactivity
from velocity import sample_isotropic
from mpl_toolkits import mplot3d
import numpy as np
def plot_plasma_profiles( params, save=None, show=False ):
'''
Plots plasma density, temperature and reactivity against normalised minor radius.
'''
rho_array, n_array, T_array, R_array = evaluate_all_profiles( params )
fig, (ax1,ax2,ax3) = plt.subplots(3, 1,sharex=True)
ax1.plot(rho_array, n_array, color='k', linewidth=1.2)
ax1.set_ylabel(r'$N_{i}$ $[m^{-3}]$')
ax2.plot(rho_array, T_array, color='k', linewidth=1.2)
ax2.set_ylabel(r'$T_{i}$ $[keV]$')
ax3.plot(rho_array, R_array, color='k', linewidth=1.2)
ax3.set_xlabel(r'$\rho$')
ax3.set_ylabel(r'$R_{vol}$ $[m^{-3}s^{-1}]$')
if( save is not None ):
plt.savefig(save)
if( show ):
plt.show()
plt.close()
def plot_plasma_profiles_2D( params, save=None, show=False ):
'''
Plots plasma density and temperature on a poloidal cross section.
'''
# Define grid resolution
R_resolution = 50
Z_resolution = 50
# Loop over (rho,alpha) with fine spacing and bin function values to average
nres_rho = 1000
nres_alpha = 1000
# First generate some contours
ncontours = 5
cont_Rs, cont_Zs = get_flux_surfaces(ncontours,params)
# Get Bounds
Rmin = np.amin( cont_Rs ) * 0.90
Rmax = np.amax( cont_Rs ) * 1.05
Zmin = np.amin( cont_Zs ) * 1.10
Zmax = np.amax( cont_Zs ) * 1.10
dR = (Rmax-Rmin) / float(R_resolution)
dZ = (Zmax-Zmin) / float(Z_resolution)
# Get axes
Raxis = np.linspace( Rmin + 0.5*dR, Rmax - 0.5*dR, R_resolution )
Zaxis = np.linspace( Zmin + 0.5*dZ, Zmax - 0.5*dZ, Z_resolution )
# Build profile arrays
ni = np.zeros((R_resolution,Z_resolution))
Ti = np.zeros((R_resolution,Z_resolution))
# Count points in each cell for averaging
count = np.zeros((R_resolution,Z_resolution))
for i in range(nres_rho):
rho = 1.0 * i / (float(nres_rho) - 1.0)
for j in range(nres_alpha):
alpha = 2.0 * pi * j / (float(nres_alpha) - 1.0)
# Get R, Z values
R, Z = get_R_Z( rho, alpha, params )
# Get profile values
ni_, Ti_ = profiles( rho, params )
# Bin values
iR = floor( (R - Rmin) / dR )
iZ = floor( (Z - Zmin) / dZ )
ni[iR,iZ] += ni_
Ti[iR,iZ] += Ti_
count[iR,iZ] += 1.0
# Calculate average values
ni = ni / count
Ti = Ti / count
# Plot density
fig, ax = plt.subplots()
fig.set_figwidth(3.5)
CS = ax.contourf(Raxis, Zaxis, ni.transpose(), 20, cmap=plt.cm.OrRd)
cbar = fig.colorbar(CS)
ax.set_aspect('equal')
# Plot contours
for i in range(ncontours-1):
plt.plot( cont_Rs[i,:], cont_Zs[i,:], '--', c='black')
plt.plot( cont_Rs[-1,:], cont_Zs[-1,:], c='black')
plt.xlabel('R [m]')
plt.ylabel('Z [m]')
if( save ):
plt.savefig(params['output_dir']+'/ni_2D.pdf')
if( params['show-plots'] ):
plt.show()
plt.close()
# Plot temperature
fig, ax = plt.subplots()
fig.set_figwidth(3.5)
CS = ax.contourf(Raxis, Zaxis, Ti.transpose(), 20, cmap=plt.cm.OrRd)
cbar = fig.colorbar(CS)
ax.set_aspect('equal')
# Plot contours
for i in range(ncontours-1):
plt.plot( cont_Rs[i,:], cont_Zs[i,:], '--', c='black')
plt.plot( cont_Rs[-1,:], cont_Zs[-1,:], c='black')
plt.xlabel('R [m]')
plt.ylabel('Z [m]')
if( save ):
plt.savefig(params['output_dir']+'/Ti_2D.pdf')
if( params['show-plots'] ):
plt.show()
plt.close()
def plot_samples( R_array, Z_array, params, save=False ):
'''
Plots sampled points on equilibrium.
'''
fig, ax = plt.subplots()
fig.set_figwidth(3.5)
# First generate some contours
ncontours = 5
cont_Rs, cont_Zs = get_flux_surfaces(ncontours,params,nalpha=100)
# Get Bounds
Rmin = min( np.amin( cont_Rs ), np.amin( R_array ) ) * 0.90
Rmax = max( np.amax( cont_Rs ), np.amax( R_array ) ) * 1.05
Zmin = min( np.amin( cont_Zs ), np.amin( Z_array ) ) * 1.10
Zmax = max( np.amax( cont_Zs ), np.amax( Z_array ) ) * 1.10
print()
print('Generating plot of samples...')
print()
# Define grid resolution
R_resolution = 50
Z_resolution = 50
dR = (Rmax-Rmin) / float(R_resolution)
dZ = (Zmax-Zmin) / float(Z_resolution)
# Get axes
Raxis = np.linspace( Rmin + 0.5*dR, Rmax - 0.5*dR, R_resolution )
Zaxis = np.linspace( Zmin + 0.5*dZ, Zmax - 0.5*dZ, Z_resolution )
# Build array
rates = np.zeros((R_resolution,Z_resolution))
for i in range(len(R_array)):
R = R_array[i]
Z = Z_array[i]
iR = floor( (R - Rmin) / dR )
iZ = floor( (Z - Zmin) / dZ )
rates[iR,iZ] += 1
# Normalise to 1
rates = rates / np.max(rates)
# Plot density
CS = ax.contourf(Raxis, Zaxis, rates.transpose(), 20, cmap=plt.cm.OrRd)
ax.set_aspect('equal')
cbar = fig.colorbar(CS)
# Plot contours
for i in range(ncontours-1):
plt.plot( cont_Rs[i,:], cont_Zs[i,:], '--', c='black')
plt.plot( cont_Rs[-1,:], cont_Zs[-1,:], c='black')
plt.xlabel('R [m]')
plt.ylabel('Z [m]')
if( save ):
plt.savefig(params['output_dir']+'/samples.pdf')
if( params['show-plots'] ):
plt.show()
plt.close()
def plot_cdf(grid, cdf, xlabel, title, save=None, show=False):
'''
Just plots a single CDF.
'''
plt.figure()
plt.plot( grid, cdf )
plt.xlabel(xlabel)
plt.ylabel('CDF')
plt.title(title)
if( show ):
plt.show()
if( save is not None ):
plt.savefig(save)
plt.close()
def check_cdf(rho_grid, original_cdf, sampled_cdf, params, save=True):
'''
Checks that the sampled CDF against rho matches the original CDF calculated from the reactivity.
'''
plt.figure()
plt.plot(rho_grid, sampled_cdf, 'black', label= 'Sampled CDF')
plt.plot(rho_grid, original_cdf, 'blue', label='Original CDF')
plt.xlabel('Rho')
plt.ylabel('CDF')
plt.legend()
if( save ):
plt.savefig(params['output_dir']+'/CDF_check.pdf')
if( params['show-plots'] ):
plt.show()
plt.close()
def plot_reactivity(params,save=False):
'''
Plots the reactivity being used on linear and log scales.
'''
Ts = np.linspace(0,100,101)
if( params['bosch-hale'] ):
Rs = np.array( [ bosch_hale_reactivity( x ) for x in Ts ])
label = 'Bosch-Hale Reactivity'
else:
Rs = np.array( [ sadler_van_belle_reactivity( x ) for x in Ts ])
label = 'Sadler-Van-Belle Reactivity'
# Linear plot
plt.figure()
plt.plot(Ts, Rs, color='black', linewidth=1.2)
plt.xlabel(r'$T_{i} [keV]$')
plt.ylabel(r'$<\sigma v>$ $[m^{3}s^{-1}]$')
plt.title(label)
if( save ):
plt.savefig( params['output_dir']+'/Reactivity.pdf' )
if( params['show-plots'] ):
plt.show()
plt.close()
# Log plot
plt.figure()
plt.plot(Ts, Rs, color='black', linewidth=1.2)
plt.xlabel(r'$T_{i} [keV]$')
plt.ylabel(r'$<\sigma v>$ $[m^{3}s^{-1}]$')
plt.title(label)
plt.yscale('log')
if( save ):
plt.savefig( params['output_dir']+'/Reactivity_log.pdf' )
if( params['show-plots'] ):
plt.show()
plt.close()
def compare_reactivities(params,save=False):
'''
Compares the Bosch-Hale and Sadler-Van-Belle reactivity fits.
Also plots data included in the Bosch-Hale paper to confirm correctness.
'''
Ts = np.linspace(0,100,101)
BH = np.array( [ bosch_hale_reactivity( x ) for x in Ts ])
SVB = np.array( [ sadler_van_belle_reactivity( x ) for x in Ts ])
# Paper quoted values
BH_x = [0.2,0.3,0.4,0.5,0.6,0.7,0.8,1.0,1.3,1.5,1.8,2.0,2.5,3.0,4.0,5.0,6.0,8.0,10.0,12.0,15.0,20.0,30.0,40.0,50.0]
BH_y = [1.254e-26,7.292e-25,9.344e-24,5.697e-23,2.253e-22,6.740e-22,1.662e-21,6.857e-21,2.546e-20,6.923e-20,1.539e-19,2.977e-19,8.425e-19,
1.867e-18,5.974e-18,1.366e-17,2.554e-17,6.222e-17,1.136e-16,1.747e-16,2.740e-16,4.330e-16,6.681e-16,7.998e-16,8.649e-16]
BH_y = [ x/1.0e6 for x in BH_y]
# Linear plot
plt.figure()
plt.plot(Ts, BH, color='black', linewidth=1.2, label='Bosch-Hale')
plt.plot(BH_x,BH_y, color='red', linewidth=1.2, label='Bosch-Hale (paper)')
plt.plot(Ts, SVB, color='blue', linewidth=1.2, label='Sadler-Van-Belle')
plt.xlabel(r'$T_{i} [keV]$')
plt.ylabel(r'$<\sigma v>$ $[m^{3}s^{-1}]$')
plt.legend(loc="lower right")
if( save ):
plt.savefig( params['output_dir']+'/Reactivity_comparison.pdf' )
if( params['show-plots'] ):
plt.show()
plt.close()
# Log plot
plt.figure()
plt.plot(Ts, BH, color='black', linewidth=1.2, label='Bosch-Hale')
plt.plot(BH_x,BH_y, color='red', linewidth=1.2, label='Bosch-Hale (paper)')
plt.plot(Ts, SVB, color='blue', linewidth=1.2, label='Sadler-Van-Belle')
plt.xlabel(r'$T_{i} [keV]$')
plt.ylabel(r'$<\sigma v>$ $[m^{3}s^{-1}]$')
plt.legend(loc="lower right")
plt.yscale('log')
if( save ):
plt.savefig( params['output_dir']+'/Reactivity_comparison_log.pdf' )
if( params['show-plots'] ):
plt.show()
plt.close()
def test_isotropic_sampling():
'''
Test isotropic sampling by sampling and plotting some points.
'''
fig = plt.figure()
ax = plt.axes(projection='3d')
x = []
y = []
z = []
for i in range(1000):
v = sample_isotropic()
x.append(v[0])
y.append(v[1])
z.append(v[2])
ax.scatter(x, y, z, marker='o')
ax.set_xlabel('Vx')
ax.set_ylabel('Vy')
ax.set_zlabel('Vz')
plt.show()
plt.close()