parallel_save.py
Go to the documentation of this file.
1 import numpy as np
2 import matplotlib.pyplot as plt
3 from FlowAnimation import FlowAnimation
4 from mpi4py import MPI
5 from tqdm import tqdm
6 import os
7 import sys
8 import pickle
9 
10 
11 # Load the dictionary from the pkl file
12 # Get folder location of this script
13 script_path = os.path.dirname(os.path.realpath(__file__))
14 option_dict_path = os.path.join(script_path, 'data/options.pkl')
15 with open(option_dict_path, 'rb') as f:
16  kwargs = pickle.load(f)
17 
18 # Also run ffmpeg per default unless told otherwise
19 create_movie = True
20 
21 # Get the arguments from the command line
22 # Run path, Frame_min, Frame_max, interval
23 run_path = sys.argv[1]
24 frame_min = int(sys.argv[2])
25 frame_max = int(sys.argv[3])
26 interval = int(sys.argv[4])
27 if len(sys.argv) > 5:
28  create_movie = False
29 
30 # Set up the MPI environment
31 comm = MPI.COMM_WORLD
32 rank = comm.Get_rank()
33 size = comm.Get_size()
34 
35 # Set up the figure
36 fig = plt.figure(figsize=(15, 8))
37 
38 # Set up the frame directory
39 frame_dir=f'{run_path}/frames'
40 if not os.path.exists(frame_dir):
41  os.mkdir(frame_dir)
42 
43 # Make barrier here
44 comm.Barrier()
45 
46 # Set up the animation
47 anim = FlowAnimation(run_path, fig, **kwargs)
48 
49 frames = np.arange(frame_min,frame_max)[rank::size]
50 if rank == 0:
51  for ii in tqdm(frames):
52  anim.save_frame(ii)
53 else:
54  for ii in frames:
55  anim.save_frame(ii)
56 
57 # Wait again
58 comm.Barrier()
59 
60 # Combine the frames
61 if ((rank == 0) and create_movie):
62  os.system(f'ffmpeg -y -r {interval} -start_number {frame_min} -i {frame_dir}/frame_%d.png {run_path}/flow.mp4')
src_files.FlowAnimation.FlowAnimation
Definition: FlowAnimation.py:22