-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples.py
188 lines (146 loc) · 5.61 KB
/
examples.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
from tristanSim import TristanSim
from helperFuncs import *
import os
import matplotlib.pyplot as plt
import numpy as np
outdir = '../batchTristan'
runs = []
# runs will be a list a tristan simulations instances
# that reside in outdir
# So any run can be accessed as run4 = runs[4]
# to access the output time e.g. 4th output, run4Time3 = runs[4][3]
# NOTE: because runs is a 1D list of objects, treating
# it as a 2D list, e.g. runs[4,3] WILL NOT WORK.
runNames = [] #the directory name that automater created.
for elm in os.listdir(outdir):
elm = os.path.join(outdir, elm)
if os.path.isdir(elm):
elm = os.path.join(elm,'output')
if os.path.exists(elm):
runs.append(TristanSim(elm))
runNames.append(os.path.split(os.path.split(elm)[0])[-1])
# TristanSim is an object that exposes an API to access tristan.
# The cool thing is nothing is loaded until it is accessed for
# first time, then it is cached.
# fields are accessed via sim.output[n].ex [ex, ey, ez, bx, by, bz, dens...]
# particle values are accessed like sim.output[n].ions.x or sim.output[n].lecs.y etc..
# Here's an example of plotting the 5th time step of ex of each run
# in a 3 x 3 grid. Because of how I set up the searcher, all are at the
# same physical time.
fig = plt.figure()
axes = fig.subplots(3,3).flatten()
#print(axes)
j = 0
for run, name in zip(runs, runNames):
ax = axes[j]
istep = run[4].istep
comp = run[4].c_omp
ex = run[4].ex[0,:,:]
ax.imshow(ex,extent=(0, ex.shape[1]*istep/comp, 0, ex.shape[0]*istep/comp), origin = 'lower')
ax.set_title(name)
#plt.colorbar()
j += 1
if j == len(axes):
break
#plt.savefig('test.png')
plt.show()
### As another example, let's plot all the total electron energy
## as function of time for each run
# First get all of the unique values of c_omp, ppc and ntimes from our suite of runs.
c_omp_val = list(set([run[0].c_omp for run in runs]))
ppc_val = list(set([run[0].ppc0 for run in runs]))
ntimes_val = list(set([run[0].ntimes for run in runs]))
# Some arrays that change what the lines will look like
ms = ['.', 'x', '4', '8']
ls = ['-', '--', ':', '-.']
color = ['b', 'r', 'g', 'y']
fig = plt.figure()
for run in runs:
# we don't want to count the fast moving particles towards our KE average
plt.plot([o.time for o in run], [np.average(o.gammae[o.inde>0]-1) for o in run],
c = color[ppc_val.index(run[0].ppc0)],
linestyle = ls[ntimes_val.index(run[0].ntimes)],
marker = ms[c_omp_val.index(run[0].c_omp)], markersize = 10)
plt.show()
###
#
# EXAMPLE OF TRACKING PARTICLES
#
###
# you'll find all of the tracked particles in a trackedLecs and
# trackedIon object. The first call builds the database which may take
# awhile. It is saved afterwards. Let's plot a single tracked electron
# for these runs we didn't track the ions.
# Let's focus just on one run for simplicity
myRun = runs[0]
# plot t vs gamma for a random prtl
choice = np.random.randint(len(myRun.trackedLecs))
randPrtl = myRun.trackedLecs[choice]
# Each prtl has the following attributes: 'x', 'y', 'u', 'v', 'w',
# 'gamma', 'bx', 'by', 'bz', 'ex', 'ey', 'ez'
plt.plot(randPrtl.t, randPrtl.gamma)
plt.show()
# This is nice, but let's say you want to find the 10 highest energy
# prtls you saved.
# First sort by energy. You can pass any function here
myRun.trackedLecs.sort(lambda x: np.max(x.gamma))
# now plot the botton N
for prtl in myRun.trackedLecs[:-10]:
plt.plot(prtl.t, prtl.gamma, 'lightgray')
for prtl in myRun.trackedLecs[-10:]:
plt.plot(prtl.t, prtl.gamma, 'k')
plt.show()
# You can also apply a mask to your particle to choose ones
# matching a certain criteria. You can pass any function
# that returns a truthy value
myRun.trackedLecs.mask(lambda x: np.max(x.gamma)>10.1)
plt.subplot(211)
for prtl in myRun.trackedLecs:
plt.plot(prtl.t, prtl.gamma)
# Masks are applied successively. However you can unmask.
# Let's plot all the other prtls
myRun.trackedLecs.unmask()
myRun.trackedLecs.mask(lambda x: np.max(x.gamma)<10.1)
plt.subplot(212)
for prtl in myRun.trackedLecs:
plt.plot(prtl.t, prtl.gamma)
plt.show()
###
# Some helper functions. So far we have hist1d, avg1,
# hist2D, avg2D.
###
# Further documentation explanation may follow but for now,
# See these examples
# you can take a histogram of particle quantity as
hist1D(myRun[0].xe)
plt.show()
# admittedly this isn't much better than plt.hist, except about 20 faster.
# you can pass the hist1D any kwargs that go to plt.plot(), and you can
# pass and Axes object if you want. As in plt.plot(). hist1D returns
# as Line2D object.
# Here's a spectrum
fig, ax = plt.subplots()
xmin = min(myRun[0].gammae.min(),myRun[0].gammai.min())
xmax = max(myRun[0].gammae.max(),myRun[0].gammai.max())
hist1D(myRun[0].gammae-1, ax = ax, range=(xmin,xmax), bins=200, xscale='log', yscale='log', c='r', label ='lecs')
hist1D(myRun[0].gammai-1, ax = ax, range=(xmin,xmax), bins=200, xscale='log', yscale='log', c='b', label ='ions')
plt.xlabel('$\gamma-1$')
plt.ylabel('$EdN/dE$')
plt.legend()
plt.show()
# Much more valuable is the ability to make phase plots.
hist2D(myRun[0].xi, myRun[0].gammai-1, clabel='$f_i(p)$')
plt.xlabel('$x_i$')
plt.ylabel('$KE_i$')
plt.show()
# And you can average over a y quantity in bins of x. Here is the average
# ion energy as function of distance.
avg1D(myRun[0].xi, myRun[0].gammai-1)
plt.xlabel('$x_i$')
plt.ylabel(r'$\langle \gamma_i-1 \rangle$')
plt.show()
# Or you can do the same in 2D. (now electrons).
avg2D(myRun[0].xe, myRun[0].ye, myRun[0].gammae-1, cnorm = 'log', bins = [100,50], clabel=r'$\langle KE \rangle$')
plt.xlabel('$x_e$')
plt.ylabel('$y_e$')
plt.show()