-
Notifications
You must be signed in to change notification settings - Fork 0
/
diag_J.py
255 lines (165 loc) · 7.7 KB
/
diag_J.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
'''
Created on Apr 06, 2012
@author: Michael Kraus (michael.kraus@ipp.mpg.de)
'''
import argparse
import numpy as np
import matplotlib
#matplotlib.use('Cairo')
matplotlib.use('AGG')
#matplotlib.use('PDF')
import matplotlib.pyplot as plt
from matplotlib import cm, colors, gridspec
from matplotlib.ticker import MultipleLocator, FormatStrFormatter, ScalarFormatter
from imhd.diagnostics import Diagnostics
class PlotMHD2D(object):
'''
classdocs
'''
def __init__(self, diagnostics, filename, ntMax=0, nPlot=1, write=False):
'''
Constructor
'''
# matplotlib.rc('text', usetex=True)
matplotlib.rc('font', family='sans-serif', size='28')
self.prefix = filename
self.ntMax = diagnostics.nt
if self.ntMax > ntMax and ntMax > 0:
self.ntMax = ntMax
self.nPlot = nPlot
self.iTime = -1
self.diagnostics = diagnostics
self.x = np.zeros(diagnostics.nx+1)
self.y = np.zeros(diagnostics.ny+1)
self.xpc = np.zeros(diagnostics.nx+2)
self.ypc = np.zeros(diagnostics.ny+2)
self.x[0:-1] = self.diagnostics.xGrid
self.x[ -1] = self.x[-2] + self.diagnostics.hx
self.y[0:-1] = self.diagnostics.yGrid
self.y[ -1] = self.y[-2] + self.diagnostics.hy
self.xpc[0:-1] = self.x
self.xpc[ -1] = self.xpc[-2] + self.diagnostics.hx
self.xpc[:] -= 0.5 * self.diagnostics.hx
self.ypc[0:-1] = self.y
self.ypc[ -1] = self.ypc[-2] + self.diagnostics.hy
self.ypc[:] -= 0.5 * self.diagnostics.hy
self.A = np.zeros((diagnostics.nx+1, diagnostics.ny+1))
self.J = np.zeros((diagnostics.nx+1, diagnostics.ny+1))
self.PB = np.zeros((diagnostics.nx+1, diagnostics.ny+1))
# set up figure/window size
self.figure = plt.figure(num=None, figsize=(10,10))
# set up plot margins
plt.subplots_adjust(hspace=0.25, wspace=0.2)
plt.subplots_adjust(left=0.1, right=0.95, top=0.9, bottom=0.1)
# set up plot title
self.title = self.figure.text(0.5, 0.95, 't = 0.0', horizontalalignment='center', fontsize=30)
# set up tick formatter
majorFormatter = ScalarFormatter(useOffset=False)
## -> limit to 1.1f precision
majorFormatter.set_powerlimits((-1,+1))
majorFormatter.set_scientific(True)
# create axes
self.axes = plt.subplot(1,1,1)
# add data for zero timepoint and compute boundaries
self.add_timepoint()
self.update_boundaries()
# create current density plot
self.conts = self.axes.contourf(self.x, self.y, self.J.T, 51, norm=self.Jnorm, cmap=plt.get_cmap('viridis'))
# self.pcm_J = self.axes.pcolormesh(self.xpc, self.ypc, self.J.T, norm=self.Jnorm, cmap=plt.get_cmap('viridis'))
self.axes.set_xlim((self.x[0],self.x[-1]))
self.axes.set_ylim((self.y[0],self.y[-1]))
for tick in self.axes.xaxis.get_major_ticks():
tick.set_pad(12)
for tick in self.axes.yaxis.get_major_ticks():
tick.set_pad(8)
# plot
self.update()
def read_data(self):
self.A[0:-1, 0:-1] = self.diagnostics.A[:,:]
self.A[ -1, 0:-1] = self.diagnostics.A[0,:]
self.A[ :, -1] = self.A[:,0]
self.J[0:-1, 0:-1] = self.diagnostics.J[:,:]
self.J[ -1, 0:-1] = self.diagnostics.J[0,:]
self.J[ :, -1] = self.J[:,0]
self.PB[0:-1, 0:-1] = self.diagnostics.e_magnetic[:,:]
self.PB[ -1, 0:-1] = self.diagnostics.e_magnetic[0,:]
self.PB[ :, -1] = self.PB[:,0]
def update_boundaries(self):
Jmin = min(self.diagnostics.J.min(), -self.diagnostics.J.max())
Jmax = min(self.diagnostics.J.max(), -self.diagnostics.J.min())
Jdiff = (Jmax - Jmin)
if Jmin == Jmax:
Jmin -= 1.
Jmax += 1.
self.Jnorm = colors.Normalize(vmin=Jmin - 0.2*Jdiff, vmax=Jmax + 0.2*Jdiff)
self.JTicks = np.linspace(Jmin - 0.2*Jdiff, Jmax + 0.2*Jdiff, 51, endpoint=True)
PBmin = min(self.diagnostics.e_magnetic.min(), -self.diagnostics.e_magnetic.max())
PBmax = min(self.diagnostics.e_magnetic.max(), -self.diagnostics.e_magnetic.min())
PBdiff = (PBmax - PBmin)
if PBmin == PBmax:
PBmin -= .1 * PBmin
PBmax += .1 * PBmax
self.PBnorm = colors.Normalize(vmin=PBmin - 0.2*PBdiff, vmax=PBmax + 0.2*PBdiff)
self.PBTicks = np.linspace(PBmin - 0.2*PBdiff, PBmax + 0.2*PBdiff, 51, endpoint=True)
Amin = min(self.diagnostics.A.min(), -self.diagnostics.A.max())
Amax = max(self.diagnostics.A.max(), -self.diagnostics.A.min())
Adiff = Amax - Amin
self.Anorm = colors.Normalize(vmin=Amin - 0.2*Adiff, vmax=Amax + 0.2*Adiff)
# self.ATicks = np.linspace(Amin + 0.01 * Adiff, Amax - 0.01 * Adiff, 31)
self.ATicks = np.linspace(Amin + 0.01 * Adiff, Amax - 0.01 * Adiff, 51, endpoint=True)
def update(self):
if not (self.iTime == 0 or (self.iTime) % self.nPlot == 0 or self.iTime == self.ntMax):
return
self.read_data()
for coll in self.conts.collections:
self.axes.collections.remove(coll)
self.conts = self.axes.contourf(self.x, self.y, self.J.T, 51, norm=self.Jnorm, cmap=plt.get_cmap('viridis'))
# self.pcm_J.set_array(self.J.T.ravel())
plt.draw()
filename = self.prefix + str('_J_%06d' % self.iTime) + '.png'
plt.savefig(filename, dpi=300)
# filename = self.prefix + str('_J_%06d' % self.iTime) + '.pdf'
# plt.savefig(filename)
def add_timepoint(self):
self.iTime += 1
self.title.set_text('t = %1.2f' % (self.diagnostics.tGrid[self.iTime]))
class Plot(object):
'''
'''
def __init__(self, hdf5_file, nPlot=1, ntMax=0):
'''
Constructor
'''
self.diagnostics = Diagnostics(hdf5_file)
if ntMax > 0 and ntMax < self.diagnostics.nt:
self.nt = ntMax
else:
self.nt = self.diagnostics.nt
self.plot = PlotMHD2D(self.diagnostics, args.hdf5_file.replace(".hdf5", ""), self.nt, nPlot)
def update(self, itime):
self.diagnostics.read_from_hdf5(itime)
self.diagnostics.update_invariants(itime)
if itime > 0:
self.plot.add_timepoint()
self.plot.update()
def run(self):
for itime in range(1, self.nt+1):
print("it = %4i" % (itime))
self.update(itime)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Ideal MHD Solver in 2D :: Current Density Diagnostics')
parser.add_argument('hdf5_file', metavar='<run.hdf5>', type=str,
help='Run HDF5 File')
parser.add_argument('-np', metavar='i', type=int, default=1,
help='plot every i\'th frame')
parser.add_argument('-ntmax', metavar='i', type=int, default=0,
help='limit to i points in time')
args = parser.parse_args()
print
print("Replay run with " + args.hdf5_file)
print
pyvp = Plot(args.hdf5_file, ntMax=args.ntmax, nPlot=args.np)
pyvp.run()
print
print("Replay finished.")
print