Coverage for planit/impacttools.py: 83%
303 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-11 15:42 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-11 15:42 +0000
1"""
2 planit impact class and analysis tools
3"""
5from .globaldefs import *
6from .snaptools import Snapshot
7from . import plotting
9import numpy as npy
10import scipy
11import glob
12import os
13import matplotlib
14import matplotlib.pyplot as plt
15import cmasher
17# for movie:
18from IPython import display
19import matplotlib.animation
22class ImpSnapshot(Snapshot):
23 pass
26class Impact:
27 """
28 Gadget/Swift impact class
30 A wrapper for holding multiple snapshots
32 load()
33 plotseq()
34 attrmov()
35 """
36 def __init__(self):
37 #self.targ = None
38 #self.proj = None
39 #self.v = None
40 #self.b = None
41 self.nsnaps = 0
42 self.snap = self.data = None
44 def load(self,loc,thermo=False,inter=1,compress=True,code='swift',ndigits=4,prefix='snapshot',sep='_',prefix2=None,flist=None):
45 Nf2 = Nf = 0
46 increment1 = increment2 = 1
47 flist2 = []
48 loc = str(loc)
49 if loc[-1] != '/':
50 loc = loc + '/'
51 files2 = npy.empty(0)
52 if flist:
53 files = files1 = flist
54 self.nsnaps = len(flist)
55 else:
56 if code=='swift':
57 flist = sorted(glob.glob(loc+prefix+sep+'[0-9]*.hdf5'))
58 if prefix2:
59 flist2 = sorted(glob.glob(loc+prefix2+sep+'[0-9]*.hdf5'))
60 else:
61 flist = sorted(glob.glob(loc+prefix+sep+'[0-9]*'))
62 if prefix2:
63 flist2 = sorted(glob.glob(loc+prefix2+sep+'[0-9]*'))
64 Nf1 = [(flist[x].split('/')[-1]).split(sep)[1].split('.')[0] for x in range(len(flist))]
65 if prefix2:
66 Nf2 = [(flist2[x].split('/')[-1]).split(sep)[1].split('.')[0] for x in range(len(flist2))]
67 if len(Nf1)>0:
68 Nf1 = sorted(npy.array(Nf1).astype(int))
69 increment1 = int(Nf1[1] - Nf1[0])
70 Nf = int(Nf1[-1])
71 else:
72 Nf = 0
73 if prefix2 and len(Nf2)>0:
74 Nf2 = sorted(npy.array(Nf2).astype(int))
75 increment2 = int(Nf2[1]-Nf2[0])
76 Nf2 = int(Nf2[-1])
77 else:
78 Nf2 = 0
79 print('Loading', int(Nf/increment1 + Nf2/increment2), 'files...')
80 self.nsnaps = len(flist)+len(flist2)
81 if self.nsnaps>2:
82 if Nf2>0:
83 files2 = npy.arange(0,Nf2+increment2,inter*increment2)
84 files1 = npy.arange(0,Nf+increment1,inter*increment1)
85 files = npy.append(files1,files2)
86 elif self.nsnaps>0:
87 files = npy.array([0,Nf])
88 else:
89 files = []
91 self.data = npy.ndarray((len(files),),dtype=object)
93 for i in range(len(self.data)):
94 honly = True
95 if i==0 or i==len(self.data)-1:
96 honly = False
97 self.data[i] = ImpSnapshot()
98 if code=='swift' or code=='Swift':
99 if i<len(files1):
100 self.data[i].load(loc+prefix+sep+'{:>0{width}d}.hdf5'.format(int(files[i]),width=ndigits),headonly=honly,thermo=thermo,compress=compress)
101 else:
102 self.data[i].load(loc+prefix2+sep+'{:>0{width}d}.hdf5'.format(int(files[i]),width=ndigits),headonly=honly,thermo=thermo,compress=compress)
103 else:
104 if i<len(files1):
105 self.data[i].load(loc+prefix+sep+'{:>0{width}d}'.format(int(files[i]),width=ndigits),headonly=honly,thermo=thermo,compress=compress)
106 else:
107 self.data[i].load(loc+prefix2+sep+'{:>0{width}d}'.format(int(files[i]),width=ndigits),headonly=honly,thermo=thermo,compress=compress)
109 self.snap = self.data
112 def plotseq(self, n=4, ptype='materials', type=None, seq=None, times=None, scale='Mm', potmin=False, tcut = 3600., zoom=1., focus='potmin', cmap=None):
113 """
114 tcut -- time cut for pre-contact treatment
115 """
116 if seq is None: # and times is None:
117 if self.nsnaps>n:
118 seq = npy.logspace(0,npy.log10(len(self.data)-1),n).astype(int)
119 else:
120 seq = npy.arange(self.nsnaps)
121 elif seq is not None:
122 n = len(seq)
124 if type: # for backwards compatibility
125 ptype = type
127 scf = plotting.get_scale_fac(scale)
128 axlim = 4*int(npy.ceil((self.data[0].x.max()-self.data[0].x.min())/scf/8.))
129 zcut = axlim/200.*scf
131 # region to grid/plot
132 #zmin=-axlim/10.
133 #zmax=axlim/10.
135 axlim /= zoom
137 # number of cells for grid
138 Ng = 1601j
139 #Ngz = 21j
141 #zi=npy.linspace(zmin,zmax,int(Ngz.imag))
142 #X,Y,Z = npy.mgrid[-axlim:axlim:(Ng),-axlim:axlim:(Ng),zmin:zmax:(Ngz)]
143 X,Y = npy.mgrid[-axlim:axlim:(Ng),-axlim:axlim:(Ng)]
144 Z = npy.full_like(X, 0)
146 if potmin: # for backwards compatibility
147 focus='potmin'
149 if not cmap:
150 cmap = plotting.get_default_colormap(ptype)
151 if ptype in plotting.phase_type:
152 cmap = matplotlib.colors.ListedColormap(cmap.colors[[0,1,2,3,4,5],:])
153 #cmap = matplotlib.colors.ListedColormap(cmap.colors[0,1,2,3,4,5])
155 fig = plt.figure(figsize=(10,6))
157 for i in range(len(seq)):
158 j = seq[i]
159 plt.subplot(1,n,i+1,aspect='equal')
161 if ptype in plotting.materials_type:
162 im = plotting.plot_snapshot_scatter(self.data[j],cmap=cmap,plotQ=-(self.data[j].id/PROJ_ID_OFFSET).astype(int),ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,focus=focus)
163 elif ptype in plotting.density_type:
164 im = plotting.plot_snapshot_fluid(self.data[j],X,Y,Z,ax=plt.gca(),cmap=cmap,ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=0.5*self.data[0].vel.min(),focus=focus)
165 elif ptype in plotting.entropy_type:
166 im = plotting.plot_snapshot_fluid(self.data[j],X,Y,Z,plotQ=self.data[j].S/1e7,ax=plt.gca(),cmap=cmap,ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=0.5*self.data[0].vel.min(),focus=focus)
167 elif ptype in plotting.temperature_type:
168 im = plotting.plot_snapshot_fluid(self.data[j],X,Y,Z,plotQ=self.data[j].T,ax=plt.gca(),cmap=cmap,ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=0.5*self.data[0].vel.min(),focus=focus)
169 elif ptype in plotting.pressure_type:
170 im = plotting.plot_snapshot_fluid(self.data[j],X,Y,Z,plotQ=self.data[j].P/1e9,ax=plt.gca(),cmap=cmap,ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=0.5*self.data[0].vel.min(),focus=focus)
171 elif ptype in plotting.phase_type:
172 im = plotting.plot_snapshot_scatter(self.data[j],cmap=cmap,plotQ=self.data[j].phase,ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,focus=focus)
174 if i>0:
175 plt.gca().set_yticklabels([])
176 plt.gca().set_ylabel('')
178 if i == len(seq)-1:
179 tlab = plt.gca().texts[0]
180 tlab.set_text(tlab.get_text()+' hrs')
182 if ptype not in plotting.materials_type:
183 if i == len(seq)-1:
184 pbox=plt.gca().get_position()
185 xw = 0.25
186 cbar_ax = fig.add_axes([pbox.x1-xw, pbox.y1*1.1, xw, 0.015])
187 cbar = fig.colorbar(im, cax=cbar_ax, orientation='horizontal')
188 plotting.format_colorbar_axis(cbar, ptype)
189 plt.subplots_adjust(wspace=0, hspace=0)
190 #plt.show(block=False)
191 return fig
194 def attrmov(self, n=40, ptype='materials', seq=None, times=None, scale='Mm', focus='potmin', fps=12, zoom=1., cmap = None, dpi=400, bitrate=5000, tcut = 3600.):
195 if seq is None: #and times is None:
196 if self.nsnaps>n:
197 seq = npy.logspace(0,npy.log10(len(self.data)-1),n).astype(int)
198 else:
199 seq = npy.arange(self.nsnaps-1)
200 elif seq is not None:
201 n = len(seq)
203 if ptype not in [*plotting.materials_type,*plotting.density_type,*plotting.entropy_type,*plotting.phase_type,*plotting.pressure_type,*plotting.temperature_type]:
204 raise ValueError('Unknown plot type:',ptype)
206 scf = plotting.get_scale_fac(scale)
207 axlim = 1.5*int(npy.ceil((self.data[0].x.max()-self.data[0].x.min())/scf/8.))/zoom #4*
208 zcut = axlim/200.*scf
210 # number of cells for grid
211 Ng = 1201j
212 #Ngz = 21j
214 # region to grid/plot
215 #zmin=-axlim/10.
216 #zmax=axlim/10.
217 #zlim = zmax/5.*scf
219 #zi=npy.linspace(zmin,zmax,int(Ngz.imag))
220 #X,Y,Z = npy.mgrid[-axlim:axlim:(Ng),-axlim:axlim:(Ng),zmin:zmax:(Ngz)]
221 X,Y = npy.mgrid[-axlim:axlim:(Ng),-axlim:axlim:(Ng)]
222 Z = npy.full_like(X, 0)
225 if not cmap:
226 cmap = plotting.get_default_colormap(ptype)
227 if ptype in plotting.phase_type:
228 cmap = matplotlib.colors.ListedColormap(cmap.colors[[0,1,2,3,4,5],:])
230 j = 0
231 def attrmovfunc(j):
232 plt.clf()
233 plt.minorticks_on()
235 if ptype in plotting.materials_type:
236 im = plotting.plot_snapshot_scatter(self.data[j],cmap=cmap,plotQ=-(self.data[j].id/PROJ_ID_OFFSET).astype(int),ax=plt.gca(),ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,focus=focus)
237 elif ptype in plotting.phase_type:
238 im = plotting.plot_snapshot_scatter(self.data[j],cmap=cmap,plotQ=self.data[j].phase,ax=plt.gca(),ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,focus=focus)
239 elif ptype in plotting.density_type:
240 im = plotting.plot_snapshot_fluid(self.data[j],X,Y,Z,plotQ=self.data[j].rho,ax=plt.gca(),cmap=cmap,ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=0.5*self.data[0].vel.min(),focus=focus)
241 elif ptype in plotting.entropy_type:
242 im = plotting.plot_snapshot_fluid(self.data[j],X,Y,Z,plotQ=self.data[j].S/1e7,ax=plt.gca(),cmap=cmap,ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=0.5*self.data[0].vel.min(),focus=focus)
243 elif ptype in plotting.temperature_type:
244 im = plotting.plot_snapshot_fluid(self.data[j],X,Y,Z,plotQ=self.data[j].T,ax=plt.gca(),cmap=cmap,ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=0.5*self.data[0].vel.min(),focus=focus)
245 elif ptype in plotting.pressure_type:
246 im = plotting.plot_snapshot_fluid(self.data[j],X,Y,Z,plotQ=self.data[j].P/1e9,ax=plt.gca(),cmap=cmap,ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=0.5*self.data[0].vel.min(),focus=focus)
248 if ptype not in plotting.materials_type:
249 pbox = plt.gca().get_position()
250 cbar_ax = fig.add_axes([pbox.x1*1.02, pbox.y0, 0.035, 1.0*(pbox.y1-pbox.y0)])
251 cbar = fig.colorbar(im, cax=cbar_ax, orientation='vertical')
252 plotting.format_colorbar_axis(cbar, ptype, y=True)
254 if n>30 and j!=0 and j!=len(self.data)-1:
255 self.data[j].freedata()
257 fig = plt.figure(figsize=(3.5,2.8))
259 if len(seq)==1:
260 attrmovfunc(seq[0])
261 plt.subplots_adjust(top=0.98,bottom=0.11,left=0.17,right=0.8)
262 plt.show(block=False)
263 return fig
264 else:
265 if matplotlib.animation.FFMpegWriter.isAvailable():
266 filetype = '.mp4'
267 else:
268 filetype = '.gif'
269 if ptype in plotting.materials_type:
270 movfile = os.path.dirname(self.data[0].file)+'/materials'+filetype
271 elif ptype in plotting.density_type:
272 movfile = os.path.dirname(self.data[0].file)+'/density'+filetype
273 elif ptype in plotting.entropy_type:
274 movfile = os.path.dirname(self.data[0].file)+'/entropy'+filetype
275 elif ptype in plotting.phase_type:
276 movfile = os.path.dirname(self.data[0].file)+'/phase'+filetype
277 elif ptype in plotting.pressure_type:
278 movfile = os.path.dirname(self.data[0].file)+'/pressure'+filetype
279 elif ptype in plotting.temperature_type:
280 movfile = os.path.dirname(self.data[0].file)+'/temperature'+filetype
281 else:
282 movfile = os.path.dirname(self.data[0].file)+'/attrmov'+filetype
283 anim=matplotlib.animation.FuncAnimation(fig,attrmovfunc,seq)
284 plt.subplots_adjust(top=0.975,bottom=0.105,left=0.17,right=0.8)
285 ##anim.save(movfile,dpi=200,writer=matplotlib.animation.PillowWriter(fps=fps,bitrate=1000)) #writer='ffmpeg' # 800 # 9000 fps=fps,bitrate=1000
286 anim.save(movfile,dpi=dpi,fps=fps,bitrate=bitrate) #writer='ffmpeg' # 800 # 9000 fps=fps,bitrate=1000
287 #plt.show()
288 plt.close()
290 html=display.Video(movfile,embed=True,width=500)
291 return html
295def multiplotseq(imps, n=4, types='materials', seqs=None, times=None, scale='Mm', potmin=False, zoom=1., uppercaselab=False, focus='potmin', tcut=3600.):
296 if (not isinstance(imps,list)) and (not isinstance(types,list)):
297 return imps.plotseq(n=n,ptype=types,seq=seqs,times=times,scale=scale,focus=focus,zoom=zoom)
298 elif not isinstance(imps,list):
299 imps=[imps,]
300 elif not isinstance(types,list):
301 types=[types,]
302 if seqs is None: # and times is None:
303 if imps[0].nsnaps>n:
304 seqs = [npy.logspace(0,npy.log10(len(imps[0].data)-1),n).astype(int),]
305 else:
306 seqs = [npy.arange(imps[0].nsnaps),]
307 elif not isinstance(seqs,list):
308 n = len(seqs)
309 seqs = [seqs,]
310 #elif seqs:
311 n = [len(s) for s in seqs]
312 if not all(x==n[0] for x in n):
313 print('Warning: unequal sequences will produce ugly plots')
315 if not isinstance(zoom,list):
316 zoom=[zoom,]
318 scf = plotting.get_scale_fac(scale)
320 # number of cells for grid
321 Ng = 1201j
322 #Ngz = 15j
324 if potmin:
325 focus='potmin'
327 m = 1
328 k = 0
329 leftcol = None
330 toprow = None
332 fig = plt.figure()#figsize=(10,4))
333 fig.set_figwidth(8.)
334 fig.set_figheight(8./max(n)*(len(imps)*len(types)) + 1.*len(imps)**1.75)#-0.1
335 gs = fig.add_gridspec(ncols=max(n), nrows=len(imps)*len(types),wspace=0,hspace=0,right=0.98,top=0.96)
336 #print(max(n),len(imps)*len(types))
338 for imp, seq, zoomfac in zip(imps, seqs, zoom):#, types):
339 axlim = 2*int(npy.ceil((imp.data[0].x.max()-imp.data[0].x.min())/scf/8.)) #4
340 zcut = axlim/200.*scf #200
341 # region to grid/plot
342 #zmin = -axlim/10.
343 #zmax = axlim/10.
344 axlim /= zoomfac
346 #zlim = zmax/5.*scf
347 #zi = npy.linspace(zmin,zmax,int(Ngz.imag))
348 #X,Y,Z = npy.mgrid[-axlim:axlim:(Ng),-axlim:axlim:(Ng),zmin:zmax:(Ngz)]
349 X,Y = npy.mgrid[-axlim:axlim:(Ng),-axlim:axlim:(Ng)]
350 Z = npy.full_like(X, 0)
352 l = 0
354 for ptype in types:
355 n = len(seq)
356# if l==0:
357# cmap = plt.get_cmap('cmr.bubblegum').copy() #'plasma'
358# cmapphase = plt.get_cmap('plasma', 6).copy()
359# elif l==1 and len(types)>2:
360# cmap = plt.get_cmap('cmr.bubblegum').copy()#cmocean.cm.haline #plt.get_cmap('magma')#.reversed()
361# cmapphase = plt.get_cmap('cmr.bubblegum', 6).copy()
362# elif l==2 or l==1:
363# cmap = plt.get_cmap('cmr.eclipse').copy()
364# cmapphase = plt.get_cmap('cmr.eclipse', 6).copy()
365# else:
366# cmap=plt.get_cmap('plasma').copy()
367# cmapphase = plt.get_cmap('plasma', 6).copy()
368 cmap = plotting.get_default_colormap(ptype)
369 if ptype in plotting.phase_type:
370 cmap = matplotlib.colors.ListedColormap(cmap.colors[[0,1,2,3,4,5],:])
371# if ptype in plotting.density_type:
372# cmap.set_under('k')
373# else:
374# cmap.set_under('w')
376 for i in range(len(seq)):
377 j = seq[i]
378 #plt.subplot(len(imps),n,(m-1)*n + i+1,aspect='equal')
379 fig.add_subplot(gs[m-1,i],aspect='equal')
381 if imp.data[j].header.time < tcut:
382 vcut = 0.5*imp.data[0].vel.min()
384 if ptype in plotting.materials_type:
385 im = plotting.plot_snapshot_scatter(imp.data[j],cmap=cmap,plotQ=-(imp.data[j].id/PROJ_ID_OFFSET).astype(int),ax=plt.gca(),ptype=ptype,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,focus=focus)
386 elif ptype in plotting.density_type:
387 im = plotting.plot_snapshot_fluid(imp.data[j],X,Y,Z,ax=plt.gca(),cmap=cmap,ptype=ptype,plotQ=imp.data[j].rho,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=vcut,focus=focus)
388 elif ptype in plotting.pressure_type:
389 im = plotting.plot_snapshot_fluid(imp.data[j],X,Y,Z,ax=plt.gca(),cmap=cmap,ptype=ptype,plotQ=imp.data[j].P/1e9,axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,vcut=vcut,focus=focus)
390 elif ptype in plotting.entropy_type:
391 im = plotting.plot_snapshot_fluid(imp.data[j],X,Y,Z,ax=plt.gca(),cmap=cmap,ptype=ptype,plotQ=imp.data[j].S/1e7,axlim=axlim,scale=scale,zcut=zcut,focus=focus,tcut=tcut,vcut=vcut)
392 elif ptype in plotting.temperature_type:
393 im = plotting.plot_snapshot_fluid(imp.data[j],X,Y,Z,ax=plt.gca(),cmap=cmap,ptype=ptype,plotQ=imp.data[j].T,axlim=axlim,scale=scale,zcut=zcut,focus=focus,tcut=tcut,vcut=vcut)
394 elif ptype in plotting.phase_type:
395 im = plotting.plot_snapshot_scatter(imp.data[j],cmap=cmap,plotQ=imp.data[j].phase,ptype=ptype,ax=plt.gca(),axlim=axlim,scale=scale,zcut=zcut,tcut=tcut,focus=focus)
397 if i>0:
398 plt.gca().set_yticklabels([])
399 plt.gca().set_ylabel('')
400 if m != len(imps)*len(types):
401 plt.gca().set_xticklabels([])
402 plt.gca().set_xlabel('')
403 if i==0:
404 if ptype in [*plotting.density_type,*plotting.temperature_type]:
405 if uppercaselab:
406 plt.text(0.05,0.95,chr(64+m),ha='left',va='top',fontsize=9,fontweight='bold',color='w',transform=plt.gca().transAxes)
407 else:
408 plt.text(0.05,0.95,chr(96+m),ha='left',va='top',fontsize=9,fontweight='bold',color='w',transform=plt.gca().transAxes)
409 else:
410 if uppercaselab:
411 plt.text(0.05,0.95,chr(64+m),ha='left',va='top',fontsize=9,fontweight='bold',color='k',transform=plt.gca().transAxes)
412 else:
413 plt.text(0.05,0.95,chr(96+m),ha='left',va='top',fontsize=9,fontweight='bold',color='k',transform=plt.gca().transAxes)
415 ax = plt.gca()
416 if i>0:
417 colpos = ax.get_position()
418 gap = leftcol.x1-colpos.x0
419 ax.set_position([colpos.x0+gap,colpos.y0,colpos.width,colpos.height])
420 colpos = ax.get_position()
421 #else:
422 # pos = ax.get_position()
423 leftcol = ax.get_position()
424 if m>1:
425 rowpos = ax.get_position()
426 gap = toprow.y0-rowpos.y1
427 if k==1:
428 gap -= 0.04/max(1,len(imps)-1.1)#0.05
429 ax.set_position([rowpos.x0,rowpos.y0+gap,rowpos.width,rowpos.height])
430 #else:
431 # plt.subplots_adjust(wspace=0)
433 if m==1 and i==0:
434 firstplot = plt.gca()
436 if ptype not in plotting.materials_type:
437 if i == len(seq)-1 and (m==1 or m==len(imps)*len(types)):
438 pbox = ax.get_position()
439 xw = 0.33
440 if m==1:
441 cbar_ax = fig.add_axes([pbox.x1-xw, pbox.y1+0.26/max(1,2*len(imps)-1.9)*xw, xw, 0.02/max(1,2*len(imps)**0.8-1.8)])#0.27
442 #cbar_ax = fig.add_axes([pbox.x1-xw, pbox.y1+0.32*xw, xw, 0.015])
443 elif m==len(imps)*len(types):
444 pbox = firstplot.get_position()
445 #cbar_ax = fig.add_axes([pbox.x1-xw, pbox.y0-0.6/max(1,1.5*len(imps)-0.7)*xw, xw, 0.032/max(1,2*len(imps)-1.8)])#0.47
446 cbar_ax = fig.add_axes([pbox.x0, pbox.y1+0.26/max(1,2*len(imps)-1.9)*xw, xw, 0.02/max(1,2*len(imps)**0.8-1.8)])#0.47
447 cbar = fig.colorbar(im,cax=cbar_ax,orientation='horizontal')
448 plotting.format_colorbar_axis(cbar, ptype, y=False)
449 k=0
450 m+=1
451 l+=1
452 cbar_ax = None
453 toprow = ax.get_position()
454 k=1
456 #fig.set_figwidth(10)
457 #print(colpos.x0,colpos.y0,colpos.x1,colpos.y1)
458 #plt.show()
459 return fig