|
| 1 | +from common.parameters import SimulationParameters |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from matplotlib.animation import FuncAnimation, FFMpegWriter |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | + |
| 9 | +class AnimationPlotter(): |
| 10 | + ''' |
| 11 | + Plotting class with FFMPEG video support. |
| 12 | + Caution: The plotter is static and can be a bit of a chore to display everything correctly. |
| 13 | + You have to edit the code for each setup you want to display. |
| 14 | + ''' |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def plot_3D(pressure_field: np.ndarray, sim_param: SimulationParameters, title: str = '', interval=0, video_output: bool = False, file_name: str = '', source_zyx: tuple = None, direction: np.character = [None, 'x', 'y', 'z'][1]): |
| 18 | + ''' |
| 19 | + Plots 3D domain in real-time with video output. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + pressure_field : ndarray |
| 24 | + Pressure field data. |
| 25 | + sim_param : SimulationParameters |
| 26 | + Instance of simulation parameter class. |
| 27 | + title : str |
| 28 | + Title of the plot. |
| 29 | + interval : int |
| 30 | + Delay between frames in milliseconds. |
| 31 | + video_output: bool |
| 32 | + Displays the video on screen. |
| 33 | + file_name : str |
| 34 | + File name of video to write on disk. |
| 35 | + zyx : tuple |
| 36 | + Z, Y, X of source location. |
| 37 | + direction : char |
| 38 | + Direction. Either None, x, y or z. |
| 39 | + |
| 40 | + Returns |
| 41 | + ------- |
| 42 | + tuple |
| 43 | + Animation and FuncAnimation instances. |
| 44 | + ''' |
| 45 | + |
| 46 | + plt.close() # close any existing plots |
| 47 | + |
| 48 | + if source_zyx is not None: |
| 49 | + (z, y, x) = source_zyx |
| 50 | + fig, (X, Y, Z) = plt.subplots(nrows=3, ncols=1, figsize=(10, 10)) |
| 51 | + |
| 52 | + animation = None |
| 53 | + if direction is not None: |
| 54 | + p_t = list() |
| 55 | + if direction == 'x': |
| 56 | + for frame in pressure_field: |
| 57 | + p_t.append(frame[z, y, :]) |
| 58 | + if direction == 'y': |
| 59 | + for frame in pressure_field: |
| 60 | + p_t.append(frame[z, :, x]) |
| 61 | + if direction == 'z': |
| 62 | + for frame in pressure_field: |
| 63 | + p_t.append(frame[:, y, x]) |
| 64 | + |
| 65 | + animation = AnimationPlotter.plot_1D( |
| 66 | + p_t, sim_param, interval=0, video_output=False, file_name='') |
| 67 | + |
| 68 | + p_x = [pressure_field[i][:, :, x] for i in range(len(pressure_field))] |
| 69 | + p_y = [pressure_field[i][:, y, :] for i in range(len(pressure_field))] |
| 70 | + p_z = [pressure_field[i][z, :, :] for i in range(len(pressure_field))] |
| 71 | + |
| 72 | + fig.suptitle(title, fontsize=14, fontweight='bold') |
| 73 | + text = fig.text(0.1, 0.9, '', # X, Y; 1-top or right |
| 74 | + verticalalignment='center', horizontalalignment='center', |
| 75 | + color='green', fontsize=15) |
| 76 | + |
| 77 | + k = np.max([np.min(np.abs([pressure_field])), |
| 78 | + np.max(np.abs([pressure_field]))]) |
| 79 | + k = 0.5*k |
| 80 | + ma = k |
| 81 | + mi = -k |
| 82 | + |
| 83 | + colormap = ['Greys', 'seismic', 'coolwarm', 'twilight'][1] |
| 84 | + im_x = X.imshow(np.zeros_like( |
| 85 | + p_x[0]), vmin=mi, vmax=ma, aspect='equal', cmap=colormap) |
| 86 | + im_y = Y.imshow(np.zeros_like( |
| 87 | + p_y[0]), vmin=mi, vmax=ma, aspect='equal', cmap=colormap) |
| 88 | + im_z = Z.imshow(np.zeros_like( |
| 89 | + p_z[0]), vmin=mi, vmax=ma, aspect='equal', cmap=colormap) |
| 90 | + |
| 91 | + # Color Bar |
| 92 | + fig.subplots_adjust(right=0.85) |
| 93 | + cbar_ax = fig.add_axes([0.88, 0.15, 0.04, 0.7]) |
| 94 | + fig.colorbar(im_x, cax=cbar_ax) |
| 95 | + |
| 96 | + def init_func(): |
| 97 | + X.set_title('YZ-Plane') |
| 98 | + Y.set_title('ZX-Plane') |
| 99 | + Z.set_title('XY-Plane') |
| 100 | + |
| 101 | + def update_plot(time_step): |
| 102 | + time = sim_param.delta_t * time_step |
| 103 | + text.set_text("Time: %.2f sec" % time) |
| 104 | + im_x.set_data(p_x[time_step]) |
| 105 | + im_y.set_data(p_y[time_step]) |
| 106 | + im_z.set_data(p_z[time_step]) |
| 107 | + return [im_x, im_y, im_z] |
| 108 | + |
| 109 | + # keep the reference |
| 110 | + func_animation = FuncAnimation( |
| 111 | + fig, |
| 112 | + update_plot, |
| 113 | + frames=range(sim_param.number_of_samples), |
| 114 | + init_func=init_func, |
| 115 | + interval=interval, # Delay between frames in milliseconds |
| 116 | + blit=False) |
| 117 | + if video_output: |
| 118 | + AnimationPlotter.write_video(func_animation, file_name) |
| 119 | + return [animation, func_animation] |
| 120 | + else: |
| 121 | + pass |
| 122 | + |
| 123 | + @staticmethod |
| 124 | + def plot_2D(pressure_field: np.ndarray, sim_param: SimulationParameters, interval: int = 0, video_output: bool = False, file_name: str = ''): |
| 125 | + ''' |
| 126 | + Plots 3D domain in real-time with video output. |
| 127 | +
|
| 128 | + Parameters |
| 129 | + ---------- |
| 130 | + pressure_field : ndarray |
| 131 | + Pressure field data. |
| 132 | + sim_param : SimulationParameters |
| 133 | + Instance of simulation parameter class. |
| 134 | + interval : int |
| 135 | + Delay between frames in milliseconds. |
| 136 | + video_output: bool |
| 137 | + Displays the video on screen. |
| 138 | + file_name : str |
| 139 | + File name of video to write on disk. |
| 140 | + |
| 141 | + Returns |
| 142 | + ------- |
| 143 | + FuncAnimation |
| 144 | + FuncAnimation instance. |
| 145 | + ''' |
| 146 | + |
| 147 | + fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 5)) |
| 148 | + |
| 149 | + fig.suptitle("Time: %.2f sec" % 0) |
| 150 | + |
| 151 | + mi = np.min(-np.abs([pressure_field])) |
| 152 | + ma = np.max(np.abs([pressure_field])) |
| 153 | + |
| 154 | + im = ax.imshow(np.zeros_like(pressure_field[0]), vmin=mi, vmax=ma) |
| 155 | + |
| 156 | + # Color Bar |
| 157 | + fig.subplots_adjust(right=0.85) |
| 158 | + cbar_ax = fig.add_axes([0.88, 0.15, 0.04, 0.7]) |
| 159 | + fig.colorbar(im, cax=cbar_ax) |
| 160 | + |
| 161 | + def init_func(): |
| 162 | + ''' |
| 163 | + No implementation. |
| 164 | + ''' |
| 165 | + pass |
| 166 | + |
| 167 | + def update_plot(time_step): |
| 168 | + time = sim_param.dt * time_step |
| 169 | + fig.suptitle("Time: %.2f sec" % time) |
| 170 | + im.set_data(pressure_field[time_step]) |
| 171 | + return [im] |
| 172 | + |
| 173 | + # keep the reference |
| 174 | + animation = FuncAnimation(fig, |
| 175 | + update_plot, |
| 176 | + frames=sim_param.time_steps, |
| 177 | + init_func=init_func, |
| 178 | + interval=interval, # Delay between frames in milliseconds |
| 179 | + blit=False) |
| 180 | + if video_output: |
| 181 | + AnimationPlotter.write_video(animation, file_name) |
| 182 | + return animation |
| 183 | + |
| 184 | + @staticmethod |
| 185 | + def plot_1D(p_field_t: np.ndarray, sim_param: SimulationParameters, interval: int = 0, video_output: bool = False, file_name: str = ''): |
| 186 | + ''' |
| 187 | + Plots 3D domain in real-time with video output. |
| 188 | +
|
| 189 | + Parameters |
| 190 | + ---------- |
| 191 | + pressure_field : ndarray |
| 192 | + Pressure field data. |
| 193 | + sim_param : SimulationParameters |
| 194 | + Instance of simulation parameter class. |
| 195 | + interval : int |
| 196 | + Delay between frames in milliseconds. |
| 197 | + video_output: bool |
| 198 | + Displays the video on screen. |
| 199 | + file_name : str |
| 200 | + File name of video to write on disk. |
| 201 | + |
| 202 | + Returns |
| 203 | + ------- |
| 204 | + FuncAnimation |
| 205 | + FuncAnimation instance. |
| 206 | + ''' |
| 207 | + fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(5, 5)) |
| 208 | + |
| 209 | + fig.suptitle("Time: %.2f sec" % 0) |
| 210 | + |
| 211 | + k = np.max([np.min(np.abs([p_field_t])), np.max(np.abs([p_field_t]))]) |
| 212 | + k = 0.5*k |
| 213 | + ma = k |
| 214 | + mi = -k |
| 215 | + |
| 216 | + ln, = ax.plot(0, 0) |
| 217 | + |
| 218 | + def init_func(): |
| 219 | + ax.set_ylim(mi, ma) |
| 220 | + ax.set_xlim(0, len(p_field_t[0])) |
| 221 | + ln.set_xdata(np.arange(len(p_field_t[0]))) |
| 222 | + |
| 223 | + def update_plot(time_step): |
| 224 | + time = sim_param.delta_t * time_step |
| 225 | + fig.suptitle("Time: %.2f sec" % time) |
| 226 | + ln.set_ydata(p_field_t[time_step]) |
| 227 | + return [ln] |
| 228 | + |
| 229 | + animation = FuncAnimation(fig, |
| 230 | + update_plot, |
| 231 | + frames=range( |
| 232 | + sim_param.number_of_samples), |
| 233 | + init_func=init_func, |
| 234 | + interval=interval, # Delay between frames in milliseconds |
| 235 | + blit=False) |
| 236 | + if video_output: |
| 237 | + AnimationPlotter.write_video(animation, file_name) |
| 238 | + return animation |
| 239 | + |
| 240 | + @staticmethod |
| 241 | + def write_video(animation: FuncAnimation, file_name: str): |
| 242 | + ''' |
| 243 | + Writes video to disk. |
| 244 | +
|
| 245 | + Parameters |
| 246 | + ---------- |
| 247 | + animation : FuncAnimation |
| 248 | + FuncAnimation instance, contains the animation. |
| 249 | + file_name : str |
| 250 | + Name of the file to be written on disk. |
| 251 | + ''' |
| 252 | + |
| 253 | + writervideo = FFMpegWriter(fps=60) |
| 254 | + fileloc = "videos/" |
| 255 | + filename = file_name + '_' + datetime.now().strftime("%d-%m-%Y_%H-%M-%S") + ".mp4" |
| 256 | + animation.save(fileloc+filename, |
| 257 | + dpi=300, |
| 258 | + writer=writervideo) |
0 commit comments