-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoAxi.py
203 lines (170 loc) · 7.53 KB
/
VideoAxi.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
# Author: Vatsal Sanjay
# Physics of Fluids
# Last updated: Jul 24, 2024
import numpy as np
import os
import subprocess as sp
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.ticker import StrMethodFormatter
import multiprocessing as mp
from functools import partial
import argparse # Add at top with other imports
import matplotlib.colors as mcolors
custom_colors = ["white", "#DA8A67", "#A0522D", "#400000"]
custom_cmap = mcolors.LinearSegmentedColormap.from_list("custom_hot", custom_colors)
matplotlib.rcParams['font.family'] = 'serif'
matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'
def gettingFacets(filename,includeCoat='true'):
exe = ["./getFacet2D", filename, includeCoat]
p = sp.Popen(exe, stdout=sp.PIPE, stderr=sp.PIPE)
stdout, stderr = p.communicate()
temp1 = stderr.decode("utf-8")
temp2 = temp1.split("\n")
segs = []
skip = False
if (len(temp2) > 1e2):
for n1 in range(len(temp2)):
temp3 = temp2[n1].split(" ")
if temp3 == ['']:
skip = False
pass
else:
if not skip:
temp4 = temp2[n1+1].split(" ")
r1, z1 = np.array([float(temp3[1]), float(temp3[0])])
r2, z2 = np.array([float(temp4[1]), float(temp4[0])])
segs.append(((r1, z1),(r2, z2)))
segs.append(((-r1, z1),(-r2, z2)))
segs.append(((r1, -z1),(r2, -z2)))
segs.append(((-r1, -z1),(-r2, -z2)))
skip = True
return segs
def gettingfield(filename, zmin, zmax, rmax, nr):
exe = ["./getData-elastic-scalar2D", filename, str(0.), str(0.), str(zmax), str(rmax), str(nr)]
p = sp.Popen(exe, stdout=sp.PIPE, stderr=sp.PIPE)
stdout, stderr = p.communicate()
temp1 = stderr.decode("utf-8")
temp2 = temp1.split("\n")
# print(temp2) #debugging
Rtemp, Ztemp, D2temp, veltemp, taupTemp = [],[],[],[],[]
for n1 in range(len(temp2)):
temp3 = temp2[n1].split(" ")
if temp3 == ['']:
pass
else:
Ztemp.append(float(temp3[0]))
Rtemp.append(float(temp3[1]))
D2temp.append(float(temp3[2]))
veltemp.append(float(temp3[3]))
taupTemp.append(float(temp3[4]))
R = np.asarray(Rtemp)
Z = np.asarray(Ztemp)
D2 = np.asarray(D2temp)
vel = np.asarray(veltemp)
taup = np.asarray(taupTemp)
nz = int(len(Z)/nr)
# print("nr is %d %d" % (nr, len(R))) # debugging
print("nz is %d" % nz)
R.resize((nz, nr))
Z.resize((nz, nr))
D2.resize((nz, nr))
vel.resize((nz, nr))
taup.resize((nz, nr))
return R, Z, D2, vel, taup, nz
# ----------------------------------------------------------------------------------------------------------------------
def process_timestep(ti, folder, nGFS, GridsPerR, rmin, rmax, zmin, zmax, lw):
t = 0.01 * ti
place = f"intermediate/snapshot-{t:.4f}"
name = f"{folder}/{int(t*1000):08d}.png"
if not os.path.exists(place):
print(f"{place} File not found!")
return
if os.path.exists(name):
print(f"{name} Image present!")
return
segs1 = gettingFacets(place)
segs2 = gettingFacets(place, 'false')
if not segs1 and not segs2:
print(f"Problem in the available file {place}")
return
nr = int(GridsPerR * rmax)
R, Z, taus, vel, taup, nz = gettingfield(place, zmin, zmax, rmax, nr)
zminp, zmaxp, rminp, rmaxp = Z.min(), Z.max(), R.min(), R.max()
# Plotting
AxesLabel, TickLabel = 50, 20
fig, ax = plt.subplots()
fig.set_size_inches(19.20, 10.80)
ax.plot([0, 0], [zmin, zmax], '-.', color='grey', linewidth=lw)
ax.plot([rmin, rmax], [0., 0.], '--', color='grey', linewidth=lw)
ax.plot([rmin, rmin], [zmin, zmax], '-', color='black', linewidth=lw)
ax.plot([rmin, rmax], [zmin, zmin], '-', color='black', linewidth=lw)
ax.plot([rmin, rmax], [zmax, zmax], '-', color='black', linewidth=lw)
ax.plot([rmax, rmax], [zmin, zmax], '-', color='black', linewidth=lw)
line_segments = LineCollection(segs2, linewidths=4, colors='green', linestyle='solid')
ax.add_collection(line_segments)
line_segments = LineCollection(segs1, linewidths=4, colors='blue', linestyle='solid')
ax.add_collection(line_segments)
cntrl1 = ax.imshow(taus, cmap="hot_r", interpolation='Bilinear', origin='lower', extent=[-rminp, -rmaxp, zminp, zmaxp], vmax=3.0, vmin=-3.0)
cntrl1 = ax.imshow(taus, cmap="hot_r", interpolation='Bilinear', origin='lower', extent=[-rminp, -rmaxp, -zminp, -zmaxp], vmax=3.0, vmin=-3.0)
# TODO: fixme the colorbar bounds for taup must be set manually based on the simulated case.
cntrl2 = ax.imshow(taup, interpolation='Bilinear', cmap=custom_cmap, origin='lower', extent=[rminp, rmaxp, zminp, zmaxp], vmax=3.0, vmin=-3.0)
cntrl2 = ax.imshow(taup, interpolation='Bilinear', cmap=custom_cmap, origin='lower', extent=[rminp, rmaxp, -zminp, -zmaxp], vmax=3.0, vmin=-3.0)
ax.set_aspect('equal')
ax.set_xlim(rmin, rmax)
ax.set_ylim(zmin, zmax)
ax.set_title(f'$t\\Omega_c$ = {t:4.3f}', fontsize=TickLabel)
l, b, w, h = ax.get_position().bounds
# Left colorbar
cb1 = fig.add_axes([l-0.04, b, 0.03, h])
c1 = plt.colorbar(cntrl1, cax=cb1, orientation='vertical')
c1.set_label(r'$\log_{10}\left(\|\mathcal{D}\|\right)$', fontsize=TickLabel, labelpad=5)
c1.ax.tick_params(labelsize=TickLabel)
c1.ax.yaxis.set_ticks_position('left')
c1.ax.yaxis.set_label_position('left')
c1.ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,.1f}'))
# Right colorbar
cb2 = fig.add_axes([l+w+0.01, b, 0.03, h])
c2 = plt.colorbar(cntrl2, cax=cb2, orientation='vertical')
c2.ax.tick_params(labelsize=TickLabel)
c2.set_label(r'$\log_{10}\left(\text{tr}\left(\mathcal{A}\right)-1\right)$', fontsize=TickLabel)
c2.ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,.2f}'))
ax.axis('off')
plt.savefig(name, bbox_inches="tight")
plt.close()
def main():
# Get number of CPUs from command line argument, or use all available
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--CPUs', type=int, default=mp.cpu_count(), help='Number of CPUs to use')
parser.add_argument('--nGFS', type=int, default=1550, help='Number of restart files to process')
parser.add_argument('--ZMAX', type=float, default=1.05, help='Maximum Z value')
parser.add_argument('--RMAX', type=float, default=8.0, help='Maximum R value')
parser.add_argument('--ZMIN', type=float, default=-1.05, help='Minimum Z value')
args = parser.parse_args()
CPUStoUse = args.CPUs
nGFS = args.nGFS
ZMAX = args.ZMAX
RMAX = args.RMAX
ZMIN = args.ZMIN
num_processes = CPUStoUse
rmin, rmax, zmin, zmax = [-RMAX, RMAX, ZMIN, ZMAX]
GridsPerR = 100
lw = 2
folder = 'Video'
if not os.path.isdir(folder):
os.makedirs(folder)
# Create a pool of worker processes
with mp.Pool(processes=num_processes) as pool:
# Create partial function with fixed arguments
process_func = partial(process_timestep,
folder=folder, nGFS=nGFS,
GridsPerR=GridsPerR, rmin=rmin, rmax=rmax,
zmin=zmin, zmax=zmax, lw=lw)
# Map the process_func to all timesteps
pool.map(process_func, range(nGFS))
if __name__ == "__main__":
main()