Skip to content

Commit 9d19014

Browse files
VARENNES RobinVARENNES Robin
authored andcommitted
Added option for superposing custom field contour in movies
1 parent e0baf64 commit 9d19014

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

diagnostics/simulation_diag_handler.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ def export_to_h5(self, field, path=None, filename=None, it=None, ix=None, iy=Non
412412
@staticmethod
413413
def _save_frame(params):
414414
"""Generate and save a single frame, optimized for parallel execution."""
415-
for_IA, scheme, save_folder_path, acronym_simu, it, time_slice, data_slice, data_name, cmap, vmin, vmax, Lx, Ly, dpi, fig_scale = params
415+
for_IA, scheme, save_folder_path, acronym_simu, it, time_slice, data_slice, data_name, cmap, vmin, vmax, Lx, Ly, dpi, fig_scale, contour_bool, contour_field, contour_levels = params
416416
frame_name = f'{acronym_simu}_{data_name}_{it:05d}.png'
417417
filepath = os.path.join(Path(save_folder_path), frame_name)
418418
logging.info(f'Generating frame {it}')
@@ -448,6 +448,17 @@ def _save_frame(params):
448448
ax.set_ylabel(r'y $[\rho_0]$')
449449
else:
450450
ax.axis('off')
451+
452+
if contour_bool:
453+
if contour_field is None: contour_field = data_slice
454+
if contour_levels is None:
455+
contour_levels = np.linspace(np.min(contour_field), np.max(contour_field), 10)
456+
# contour = ax.contour(contour_field, levels=contour_levels, colors='k', linewidths=0.5)
457+
contours_levels_pos = [level for level in contour_levels if level > 0]
458+
contours_levels_neg = [level for level in contour_levels if level <= 0]
459+
contour_pos = ax.contour(contour_field, levels=contours_levels_pos, colors='r', linewidths=1.5)
460+
contour_neg = ax.contour(contour_field, levels=contours_levels_neg, colors='b', linewidths=1.5)
461+
451462
fig.tight_layout()
452463
fig.savefig(buf, format='png')
453464
plt.close(fig) # Close the figure to free memory
@@ -458,19 +469,20 @@ def _save_frame(params):
458469

459470

460471
# Function to generate and save frames, with optional parallel execution
461-
def _generate_and_save_frames(self, parallel, num_cores, for_IA, scheme, save_folder_path, acronym_simu, time_frames, data_frames, data_name, cmap, vmin=None, vmax=None, Lx=256, Ly=256, dpi=128, fig_scale=1):
472+
def _generate_and_save_frames(self, parallel, num_cores, for_IA, scheme, save_folder_path, acronym_simu, time_frames, data_frames, data_name, cmap, vmin=None, vmax=None, Lx=256, Ly=256, dpi=128, fig_scale=1, contour_bool=False, contour_field=None, contour_levels=None):
462473
"""Generate and save frames, with optional parallel execution."""
463474
frames = []
464-
logging.info(f"Running in parallel using {num_cores} cores.")
465475
save_folder_path_frame = Path(save_folder_path)/f'{acronym_simu}_{data_name}_frames'
466476
save_folder_path_frame.mkdir(parents=True, exist_ok=True)
467-
args = [(for_IA, scheme, save_folder_path_frame, acronym_simu, it, time_slice, data_frames[it, :, :], data_name, cmap, vmin, vmax, Lx, Ly, dpi, fig_scale) for it, time_slice in enumerate(time_frames)]
477+
args = [(for_IA, scheme, save_folder_path_frame, acronym_simu, it, time_slice, data_frames[it, :, :], data_name, cmap, vmin, vmax, Lx, Ly, dpi, fig_scale, contour_bool, contour_field[it, :, :], contour_levels) for it, time_slice in enumerate(time_frames)]
468478

469479
if parallel:
480+
logging.info(f"Running in parallel using {num_cores} cores.")
470481
with ProcessPoolExecutor(max_workers=num_cores) as executor:
471482
frames = list(executor.map(self._save_frame, args))
472483

473484
else:
485+
logging.warning("Parallel execution is off, run sequentially.")
474486
for it, time_slice in enumerate(time_frames):
475487
frames.append(self._save_frame(args[it]))
476488

@@ -487,7 +499,7 @@ def _compile_movie(self, title, frames, save_folder_path, fps):
487499
writer.append_data(imageio.imread(frame_path))
488500
logging.info(f'Movie {title} created successfully at {save_folder_path}!')
489501

490-
def make_movie(self, field, path=None, filename=None, it_slice=None, parallel=True, num_cores=None, for_IA=False, scheme=False, cmap='plasma', vmin=None, vmax=None, fps=30, save_frames=False, custom_field_name='custom_field', fig_scale=1):
502+
def make_movie(self, field, path=None, filename=None, it_slice=None, parallel=True, num_cores=None, for_IA=False, scheme=False, cmap='plasma', vmin=None, vmax=None, fps=30, save_frames=False, custom_field_name='custom_field', fig_scale=1, contour_bool=False, contour_field=None, contour_levels=None):
491503
"""Generate a movie for a specific field.
492504
493505
Optional parallel execution.
@@ -530,21 +542,21 @@ def make_movie(self, field, path=None, filename=None, it_slice=None, parallel=Tr
530542
data_frames = field
531543
field = custom_field_name # Name for the custom field
532544

545+
if (contour_field is not None) and (isinstance(contour_field, str)):
546+
contour_field = np.array(self.get_data_slice(contour_field, it=it_slice))
547+
533548
# Nx = self.Nx
534549
# Ny = self.Ny
535550
Lx = self.Lx
536551
Ly = self.Ly
537-
# dpi = max(Nx,Ny)/12
538552
dpi = 128.
539-
# dpi = (Nx**2+Ny**2)**0.5/12
540-
# dpi = ((Nx**2+Ny**2)**0.5)*1
541553

542554
plt.rcParams.update({'font.size': 14 * (100 / dpi)})
543555
plt.rcParams.update({'axes.titlesize': 14 * (100 / dpi)})
544556
plt.rcParams.update({'axes.labelsize': 14 * (100 / dpi)})
545557

546558
# Generate frames using the data already loaded into memory
547-
frames = self._generate_and_save_frames(parallel, num_cores, for_IA, scheme, path, filename, time_frames, data_frames, field, cmap, vmin, vmax, Lx, Ly, dpi, fig_scale)
559+
frames = self._generate_and_save_frames(parallel, num_cores, for_IA, scheme, path, filename, time_frames, data_frames, field, cmap, vmin, vmax, Lx, Ly, dpi, fig_scale, contour_bool, contour_field, contour_levels)
548560

549561
# Compile the movie from the generated frames
550562
self._compile_movie(f'{filename}_{field}_movie', frames, path, fps)

0 commit comments

Comments
 (0)