-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpx_utilities.py
More file actions
471 lines (424 loc) · 11.3 KB
/
px_utilities.py
File metadata and controls
471 lines (424 loc) · 11.3 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import plotly.graph_objects as go
import numpy as np
import stretchmap_utilities as su
import sham_utilities
from plotly.subplots import make_subplots
import hydrostatic as hy
style_target = "dash"
density_color = "blue"
energy_color = "orange"
pressure_color = "green"
soundspeed_color = "magenta"
i = 0
density_i = i
i += 2
energy_i = i
i += 1
soundspeed_i = i
i += 2
pressure_i = i
i += 2
scatter3d_i = i
dotsize = 8
# import plotly.io as pio
# pio.templates.default = "plotly_dark"
def get_rho_values(data, mpart):
H = data["hpart"]
Rho = mpart * (1.2 / H) ** 3
return Rho
def format_param(key, value):
if key in ["y0", "n"]:
valuestr = value
elif key in ["mtot_target"]:
valuestr = f"{value:.3f}"
elif isinstance(value, float):
if value > 10 or value < 0.1:
valuestr = f"{value:.1e}"
else:
valuestr = f"{value:.1f}"
else:
valuestr = value
return valuestr
def format_inputparams(input_params):
string = ""
for key, value in input_params.items():
if isinstance(value, dict):
line = ""
line += f"{key}[name]={format_param("name",value["name"])} <br>"
for param, param_value in value["values"].items():
line += f"{key}[{param}]={format_param(param,param_value)} <br>"
else:
valuestr = format_param(key, value)
line = f"{key}: {valuestr} <br>"
string += line
return string
def px_3d_and_rho(model, ctx, img_path, rhotarget, input_params, unit):
"""
I don't normalize here, please be sure that rhotarget is normalized
:param model: Description
:param ctx: Description
:param img_path: Description
:param rhotarget: Description
:param input_params: Description
"""
eos = input_params["eos"]
data = ctx.collect_data()
t = model.get_time()
mpart = model.get_particle_mass()
Pos = data["xyz"]
U_int = data["uint"]
R = np.linalg.norm(Pos, axis=1)
Np = Pos.shape[0]
Rho = get_rho_values(data, mpart)
haspressure = False
if "pressure" in data:
haspressure = True
pressure_data = data["pressure"]
# haspressure = False
nrows = 2
ncols = 2
fig = make_subplots(
rows=nrows,
cols=ncols,
specs=[
[{"secondary_y": True}, {"type": "scatter3d", "rowspan": 2}],
[{"secondary_y": haspressure}, {}],
],
horizontal_spacing=0.2,
vertical_spacing=0.05,
column_widths=[0.6, 0.4],
subplot_titles=(
"Density and Energy profiles",
"Soundspeed and Pressure profiles",
"",
),
)
rhotab = rhotarget[1]
rtab = rhotarget[0]
# ! Density profile
fig.add_trace(
go.Scatter(
x=R,
y=Rho,
mode="markers",
name="rhoSPH",
marker=dict(color=density_color, size=dotsize),
opacity=0.6,
),
row=1,
col=1,
secondary_y=False,
)
fig.add_trace(
go.Scatter(
x=rtab,
y=rhotab,
mode="lines",
name="Initial target",
line=dict(color=density_color, dash=style_target),
),
row=1,
col=1,
secondary_y=False,
)
fig.add_trace(
go.Scatter(
x=R,
y=U_int,
mode="markers",
name="uint",
marker=dict(color=energy_color, size=dotsize),
opacity=0.6,
),
row=1,
col=1,
secondary_y=True,
)
P_cs_func = su.get_p_and_cs_func(eos, unit)
mask = rhotab != 0
P_target, cs_target = P_cs_func(rhotab[mask])
cs_data = data["soundspeed"]
# ! Soundspeed profile
fig.add_trace(
go.Scatter(
x=R,
y=cs_data,
mode="markers",
marker=dict(color=soundspeed_color, size=dotsize),
name="soundspeed",
opacity=0.6,
),
row=2,
col=1,
secondary_y=False,
)
fig.add_trace(
go.Scatter(
x=rtab[mask],
y=cs_target,
mode="lines",
line=dict(color=soundspeed_color, dash=style_target),
showlegend=False,
),
row=2,
col=1,
secondary_y=False,
)
# ! Pressure profile
if haspressure:
fig.add_trace(
go.Scatter(
x=R,
y=pressure_data,
mode="markers",
marker=dict(color=pressure_color, size=dotsize),
name="pressure",
opacity=0.6,
),
row=2,
col=1,
secondary_y=True,
)
fig.add_trace(
go.Scatter(
x=rtab[mask],
y=P_target,
mode="lines",
line=dict(color=pressure_color, dash=style_target),
showlegend=False,
),
row=2,
col=1,
secondary_y=True,
)
# ! 3D scatter
fig.add_trace(
go.Scatter3d(
x=Pos[:, 0],
y=Pos[:, 1],
z=Pos[:, 2],
mode="markers",
name="",
marker=dict(
color=Rho,
colorscale="Viridis",
colorbar=dict(
title="Density",
tickfont=dict(size=24),
xanchor="left",
exponentformat="e",
),
cmin=0,
cmax=np.max(Rho),
opacity=0.3,
size=4,
),
showlegend=False,
),
col=2,
row=1,
)
fig.update_xaxes(title_text=r"$r / R_\odot$", row=1, col=1)
for pos in ([1, 1], [2, 1]):
row, col = pos
fig.update_xaxes(
row=row,
col=col,
title_font=dict(size=20),
tickfont=dict(size=16),
exponentformat="e",
)
fig.update_xaxes(row=1, col=1, title_text=r"$r/R_\odot$")
fig.update_xaxes(row=2, col=1, title_text=r"$r/R_\odot$")
fig.update_yaxes(
row=1,
col=1,
title_text=r"$\rho$",
title_font=dict(color=density_color),
tickfont=dict(color=density_color),
secondary_y=False,
)
fig.update_yaxes(
row=1,
col=1,
title_text=r"$u$",
title_font=dict(color=energy_color),
tickfont=dict(color=energy_color),
secondary_y=True,
)
fig.update_yaxes(
row=2,
col=1,
title_text=r"$c_s$",
title_font=dict(color=soundspeed_color),
tickfont=dict(color=soundspeed_color),
)
if haspressure:
fig.update_yaxes(
row=2,
col=1,
title_text=r"$P$",
title_font=dict(color=pressure_color),
tickfont=dict(color=pressure_color),
secondary_y=True,
)
fig.update_layout(
scene=dict(
xaxis=dict(title=""),
yaxis=dict(title=""),
zaxis=dict(title=""),
)
)
fig.update_layout(
height=860,
width=1920,
title_text=f"{img_path} t={t:.2e}",
annotations=[
dict(
text=f"{Np} particles, {Np*mpart:.1e} solar masses",
showarrow=False,
xref="paper",
yref="paper",
x=0.5,
y=1.03,
xanchor="center",
yanchor="bottom",
font=dict(size=12, color="gray"),
),
dict(
text=format_inputparams(input_params),
align="left",
showarrow=False,
xref="paper",
yref="paper",
x=0.65,
y=0.1,
bordercolor="black",
borderwidth=1,
),
],
font=dict(family="Courier New, monospace"),
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="center",
x=0.25,
),
)
# fig.add_annotation(
# text="Density profile",
# xref="paper",
# yref="paper",
# x=0, # left column center (approx)
# y=0.98,
# showarrow=False,
# font=dict(size=16),
# )
# fig.add_annotation(
# text="Soundspeed and Pressure profiles",
# xref="paper",
# yref="paper",
# x=0, # right/top area (approx) adjust as required
# y=0.5,
# showarrow=False,
# font=dict(size=16),
# )
return fig
def update_px_3d_and_rho(fig, model, ctx, img_path, input_params):
data = ctx.collect_data()
t = model.get_time()
mpart = model.get_particle_mass()
Pos = data["xyz"]
U_int = data["uint"]
R = np.linalg.norm(Pos, axis=1)
Rho = get_rho_values(data, mpart)
Np = Pos.shape[0]
mtot_target = input_params["mtot_target"]
fig.data[density_i].update(
x=R,
y=Rho,
)
fig.data[energy_i].update(
x=R,
y=U_int,
)
cs_data = data["soundspeed"]
fig.data[soundspeed_i].update(
x=R,
y=cs_data,
)
fig.update_yaxes(range=[0, None], row=1, col=1, secondary_y=False)
fig.update_yaxes(range=[0, None], row=1, col=1, secondary_y=True)
fig.update_yaxes(range=[0, None], row=2, col=1, secondary_y=False)
if "pressure" in data:
pressure_data = data["pressure"]
print("pressure", pressure_data)
fig.data[pressure_i].update(
x=R,
y=pressure_data,
)
fig.update_yaxes(
range=[0, None],
row=2,
col=1,
secondary_y=True,
)
# Trace 2 : Scatter3D
fig.data[scatter3d_i].update(
x=Pos[:, 0],
y=Pos[:, 1],
z=Pos[:, 2],
marker_color=Rho,
# marker=dict(
# cmin=0,
# cmax=np.max(Rho),
# ),
)
fig.update_yaxes(range=[0, 1.1 * np.max(Rho)], row=1, col=1)
fig.update_layout(
title_text=f"{img_path} t={t:.2e}",
annotations=[
dict(
text=f"{Np} particles, {Np*mpart:.1e} solar masses",
showarrow=False,
xref="paper",
yref="paper",
x=0.5,
y=1.03,
xanchor="center",
yanchor="bottom",
font=dict(size=12, color="gray"),
),
dict(
text=format_inputparams(input_params),
align="left",
showarrow=False,
xref="paper",
yref="paper",
x=0.55,
y=0.5,
bordercolor="black",
borderwidth=1,
),
],
)
return fig
def movie(pattern_png, filemp4, fps):
import ffmpeg
ffmpeg.input(pattern_png, pattern_type="glob", framerate=fps).output(
filemp4,
vcodec="libx264",
crf=18,
preset="medium",
r=fps,
pix_fmt="yuv420p",
movflags="faststart",
).overwrite_output().run()
def compute_fps(inputparams):
"""
so that the duration of the movie is 4 sec
"""
nb_dumps = inputparams["nb_dumps"]
# tf = inputparams["tf"]
return int(nb_dumps / 4)