aboutsummaryrefslogtreecommitdiffstats
path: root/mkcuts_dvpT.py
blob: 319b7fa1e9bdf68e4f2ae6d019514df6cbf642bc (plain)
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
"""
Make X and Z Cuts:
- Density
- Velocity
- Pressure
- Temperature (Sound Speed)
"""

import matplotlib as mpl
from pymses import RamsesOutput
from pymses.filters import CellsToPoints
from pymses.utils import constants as C
from pymses.analysis.visualization import Camera, ScalarOperator, SliceMap, \
                                          FractionOperator
import matplotlib.pyplot as plt
import numpy as np
import sys
import argparse

"""
Main Routine. Define Cameras. Take Slices. Plot.
"""
def main():

    # Parse Arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("ilo", type=int, help='First Output', default=1)
    parser.add_argument("ihi", type=int, help='Last Output', default=1)
    parser.add_argument("-d", action="store_true", help='Show Density Cuts')
    parser.add_argument("-v", action="store_true", help='Show Velocity Cuts')
    parser.add_argument("-p", action="store_true", help='Show Pressure Cuts')
    parser.add_argument("-T", action="store_true", help='Show Temperature Cuts')
    parser.add_argument("-lT", action="store_true", help='Show Log10 Temperature Cuts')
    parser.add_argument("--save", action="store_true", help='Save Figures')
    parser.add_argument("--show", action="store_true", help='Show Figures')
    parser.add_argument("--px", type=int, \
                        default=512, choices=[256, 512, 1024, 2048], \
                        help='Maximum Map Size')
    args = parser.parse_args()

    # Sanity Check
    if not args.save and not args.show:
        print "At least one of --save or --show is required."
        sys.exit(0)
    if args.ilo != args.ihi and args.show:
        print "Can only show single output."
        sys.exit(0)

    # Build Output List
    iouts = range(args.ilo, args.ihi + 1)

    # Define Cameras
    camZ = Camera(center=[0.5, 0.5, 0.5], line_of_sight_axis='z',
        region_size=[1., 1.], up_vector='y', map_max_size=args.px, log_sensitive=True)
    camX = Camera(center=[0.5, 0.5, 0.5], line_of_sight_axis='x',
        region_size=[1., 1.], up_vector='y', map_max_size=args.px, log_sensitive=True)

    # Functions to Get Data
    func_rho = lambda dset: dset["rho"]
    func_vel = lambda dset: np.sqrt(np.sum(dset["vel"]**2, axis=1))
    # func_vel = lambda dset: dset["vel"][:,0]
    func_pre = lambda dset: dset["P"]

    # Operator to Get Data
    op_rho = ScalarOperator(func_rho)
    op_vel = ScalarOperator(func_vel)
    op_pre = ScalarOperator(func_pre)
    op_cs2 = FractionOperator(func_pre, func_rho)

    # Init Master Lists
    rhoZall = []
    rhoXall = []
    velZall = []
    velXall = []
    preZall = []
    preXall = []
    TZall = []
    TXall = []

    print ""
    print "// Loading Snapshots"
    print ""

    # Load Snapshots
    for iout in iouts:

        # Give Feedback
        print ""
        print "**********************************"
        print "// Creating Cuts for Output %05d." % iout
        print "**********************************"
        print ""

        # Link AMR Data
        output = RamsesOutput(".", iout)
        source = output.amr_source(["rho", "vel", "P"])

        # Compute Slice Maps
        rhoZ = SliceMap(source, camZ, op_rho, z=0.0)
        rhoX = SliceMap(source, camX, op_rho, z=0.0)
        velZ = SliceMap(source, camZ, op_vel, z=0.0)
        velX = SliceMap(source, camX, op_vel, z=0.0)
        preZ = SliceMap(source, camZ, op_pre, z=0.0)
        preX = SliceMap(source, camX, op_pre, z=0.0)
        cs2Z = SliceMap(source, camZ, op_cs2, z=0.0)
        cs2X = SliceMap(source, camX, op_cs2, z=0.0)

        # Convert Human Mind Parsable Units Pt 1 - (CGS)
        rhoZ *= output.info["unit_density"].express(C.g_cc)
        rhoX *= output.info["unit_density"].express(C.g_cc)
        velZ *= output.info["unit_velocity"].express(C.cm / C.s)
        velX *= output.info["unit_velocity"].express(C.cm / C.s)
        preZ *= output.info["unit_pressure"].express(C.barye)
        preX *= output.info["unit_pressure"].express(C.barye)

        # Convert Human Mind Parsable Units Pt 2 - (km/s)
        # Sound Speed
        factor = output.info["unit_pressure"].express(C.barye) / \
                 output.info["unit_density"].express(C.g_cc)
        cs2Z *= factor / 100.**2. / 1000.**2.
        cs2X *= factor / 100.**2. / 1000.**2.
        # Total Flow Speed
        velZ *= 1.0 / 100. / 1000.
        velX *= 1.0 / 100. / 1000.

        # Compute Temperature
        gamma = 1.4
        TX = (cs2X * 100.**2. * 1000.**2.) * 2. * C.mH.express(C.kg) / \
             gamma / C.kB.express(C.cm**2 * C.kg / C.s**2 / C.K)
        TZ = (cs2Z * 100.**2. * 1000.**2.) * 2. * C.mH.express(C.kg) / \
             gamma / C.kB.express(C.cm**2 * C.kg / C.s**2 / C.K)

        # Append to Master List
        rhoZall.append(rhoZ)
        rhoXall.append(rhoX)
        velZall.append(velZ)
        velXall.append(velX)
        preZall.append(preZ)
        preXall.append(preX)
        TZall.append(TZ)
        TXall.append(TX)

    print ""
    print "// Scanning Snapshots"
    print ""

    # Scan Snapshots for MinMax
    first = True
    for ii, iout in enumerate(iouts):

        # Load from Master List
        rhoZ = rhoZall[ii]
        rhoX = rhoXall[ii]
        velZ = velZall[ii]
        velX = velXall[ii]
        preZ = preZall[ii]
        preX = preXall[ii]
        TZ = TZall[ii]
        TX = TXall[ii]

        # MinMax
        if first:
            rhoZmax = np.nanmax(rhoZ); rhoZmin = np.nanmin(rhoZ)
            rhoXmax = np.nanmax(rhoX); rhoXmin = np.nanmin(rhoX)
            velZmax = np.nanmax(velZ); velZmin = np.nanmin(velZ)
            velXmax = np.nanmax(velX); velXmin = np.nanmin(velX)
            preZmax = np.nanmax(preZ); preZmin = np.nanmin(preZ)
            preXmax = np.nanmax(preX); preXmin = np.nanmin(preX)
            TZmax = np.nanmax(TZ); TZmin = np.nanmin(TZ)
            TXmax = np.nanmax(TX); TXmin = np.nanmin(TX)
            first = False
        else:
            # Max
            if np.nanmax(rhoZ) > rhoZmax: rhoZmax = np.nanmax(rhoZ)
            if np.nanmax(rhoX) > rhoXmax: rhoXmax = np.nanmax(rhoX)
            if np.nanmax(preZ) > preZmax: preZmax = np.nanmax(preZ)
            if np.nanmax(preX) > preXmax: preXmax = np.nanmax(preX)
            if np.nanmax(velZ) > velZmax: velZmax = np.nanmax(velZ)
            if np.nanmax(velX) > velXmax: velXmax = np.nanmax(velX)
            if np.nanmax(TZ) > TZmax: TZmax = np.nanmax(TZ)
            if np.nanmax(TX) > TXmax: TXmax = np.nanmax(TX)
            # Min
            if np.nanmin(rhoZ) > rhoZmin: rhoZmin = np.nanmin(rhoZ)
            if np.nanmin(rhoX) > rhoXmin: rhoXmin = np.nanmin(rhoX)
            if np.nanmin(preZ) > preZmin: preZmin = np.nanmin(preZ)
            if np.nanmin(preX) > preXmin: preXmin = np.nanmin(preX)
            if np.nanmin(velZ) > velZmin: velZmin = np.nanmin(velZ)
            if np.nanmin(velX) > velXmin: velXmin = np.nanmin(velX)
            if np.nanmin(TZ) > TZmin: TZmin = np.nanmin(TZ)
            if np.nanmin(TX) > TXmin: TXmin = np.nanmin(TX)

    print ""
    print "// Plotting Snapshots"
    print ""

    # Plot Snapshots
    for ii, iout in enumerate(iouts):
        
        # Load from Master List
        rhoZ = rhoZall[ii]
        rhoX = rhoXall[ii]
        velZ = velZall[ii]
        velX = velXall[ii]
        preZ = preZall[ii]
        preX = preXall[ii]
        TZ = TZall[ii]
        TX = TXall[ii]

        # Log10?
        rhoZ = np.log10(rhoZ)
        rhoX = np.log10(rhoX)
        preZ = np.log10(preZ)
        preX = np.log10(preX)
        lTX = np.log10(TX)
        lTZ = np.log10(TZ)

        # Mark Arrays vs. NaN
        rhoZ = np.ma.masked_array(rhoZ, mask=np.isnan(rhoZ))
        rhoX = np.ma.masked_array(rhoX, mask=np.isnan(rhoX))
        velZ = np.ma.masked_array(velZ, mask=np.isnan(velZ))
        velX = np.ma.masked_array(velX, mask=np.isnan(velX))
        preZ = np.ma.masked_array(preZ, mask=np.isnan(preZ))
        preX = np.ma.masked_array(preX, mask=np.isnan(preX))

        # Set Masked Colormap
        # cmap = mpl.cm.hot
        cmap = mpl.cm.jet
        cmap.set_bad([0.5, 0.5, 0.5])

        # Colormaps:
        # http://matplotlib.org/examples/pylab_examples/show_colormaps.html
        # rho_cm = 'hot'
        # vel_cm = 'hot'
        # pre_cm = 'hot'
        rho_cm = cmap
        vel_cm = cmap
        pre_cm = cmap
        tmp_cm = cmap

        # Compute Image Extent
        ext = output.info["boxlen"] * np.array([-0.5, 0.5, -0.5, 0.5])

        # Plot Density
        if args.d:
            f1 = plt.figure(1)
            plt.imshow(rhoZ, cmap=rho_cm, extent=ext, interpolation='none')
            plt.colorbar()
            plt.grid()
            plt.title('Log10 Density Cut [g/cc], t=%.2f' % output.info["time"])
            plt.xlabel('X [AU]')
            plt.ylabel('Y [AU]')

            f2 = plt.figure(2)
            plt.imshow(rhoX, cmap=rho_cm, extent=ext, interpolation='none')
            plt.colorbar()
            plt.grid()
            plt.title('Log10 Density Cut [g/cc], t=%.2f' % output.info["time"])
            plt.xlabel('Y [AU]')
            plt.ylabel('Z [AU]')

        # Plot Velocity
        if args.v:
            f3 = plt.figure(3)
            plt.imshow(velZ, cmap=vel_cm, extent=ext)
            plt.colorbar()
            plt.grid()
            plt.title('Total Flow Speed Cut [km/s], t=%.2f' % output.info["time"])
            plt.xlabel('X [AU]')
            plt.ylabel('Y [AU]')

            f4 = plt.figure(4)
            plt.imshow(velX, cmap=vel_cm, extent=ext)
            plt.colorbar()
            plt.grid()
            plt.title('Total Flow Speed Cut [km/s], t=%.2f' % output.info["time"])
            plt.xlabel('Y [AU]')
            plt.ylabel('Z [AU]')

        # Plot Pressure
        if args.p:
            f5 = plt.figure(5)
            plt.imshow(preZ, cmap=pre_cm, extent=ext, interpolation='none')
            plt.colorbar()
            plt.grid()
            plt.title('Log10 Pressure Cut [debye], t=%.2f' % output.info["time"])
            plt.xlabel('X [AU]')
            plt.ylabel('Y [AU]')

            f6 = plt.figure(6)
            plt.imshow(preX, cmap=pre_cm, extent=ext, interpolation='none')
            plt.colorbar()
            plt.grid()
            plt.title('Log10 Pressure Cut [debye], t=%.2f' % output.info["time"])
            plt.xlabel('Y [AU]')
            plt.ylabel('Z [AU]')

        # Plot Temperature
        if args.T:
            f7 = plt.figure(7)
            plt.imshow(TZ, cmap=tmp_cm, extent=ext, interpolation='none')
            plt.colorbar()
            plt.grid()
            plt.title('Temperature Cut [K], t=%.2f' % output.info["time"])
            plt.xlabel('X [AU]')
            plt.ylabel('Y [AU]')

            f8 = plt.figure(8)
            plt.imshow(TX, cmap=tmp_cm, extent=ext, interpolation='none')
            plt.colorbar()
            plt.grid()
            plt.title('Temperature Cut [K], t=%.2f' % output.info["time"])
            plt.xlabel('Y [AU]')
            plt.ylabel('Z [AU]')

        # Plot Log10 of Temperature
        if args.lT:
            f9 = plt.figure(9)
            plt.imshow(lTZ, cmap=tmp_cm, extent=ext, interpolation='none')
            plt.colorbar()
            plt.grid()
            plt.title('Log10 Temperature Cut [K], t=%.2f' % output.info["time"])
            plt.xlabel('X [AU]')
            plt.ylabel('Y [AU]')

            f10 = plt.figure(10)
            plt.imshow(lTX, cmap=tmp_cm, extent=ext, interpolation='none')
            plt.colorbar()
            plt.grid()
            plt.title('Log10 Temperature Cut [K], t=%.2f' % output.info["time"])
            plt.xlabel('Y [AU]')
            plt.ylabel('Z [AU]')

        # Save Figures
        if args.save:
            if args.d:
                f1.savefig('cut_dxy_%05d.png' % iout)
                f2.savefig('cut_dzy_%05d.png' % iout)
            if args.v:
                f3.savefig('cut_vxy_%05d.png' % iout)
                f4.savefig('cut_vzy_%05d.png' % iout)
            if args.p:
                f5.savefig('cut_pxy_%05d.png' % iout)
                f6.savefig('cut_pzy_%05d.png' % iout)
            if args.T:
                f7.savefig('cut_Txy_%05d.png' % iout)
                f8.savefig('cut_Tzy_%05d.png' % iout)
            if args.lT:
                f9.savefig('cut_logTxy_%05d.png' % iout)
                f10.savefig('cut_logTzy_%05d.png' % iout)

        # Show Figures?
        if args.show:
            plt.show()

        # Close Figures
        if args.d:
            plt.close(f1)
            plt.close(f2)
        if args.v:
            plt.close(f3)
            plt.close(f4)
        if args.p:
            plt.close(f5)
            plt.close(f6)
        if args.T:
            plt.close(f7)
            plt.close(f8)
        if args.lT:
            plt.close(f9)
            plt.close(f10)

"""
Jump into Main().
"""
if __name__ == "__main__":
    main()