|
1 | 1 | import matplotlib.pyplot as plt |
2 | 2 | import numpy as np |
3 | 3 | from matplotlib.patches import Polygon |
| 4 | +from matplotlib.animation import FuncAnimation |
4 | 5 |
|
5 | 6 | from rocketpy.mathutils.function import Function |
6 | 7 |
|
7 | | -from .plot_helpers import show_or_save_plot |
| 8 | +from .plot_helpers import show_or_save_plot, show_or_save_animation |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class _TankPlots: |
@@ -180,6 +181,77 @@ def fluid_center_of_mass(self, filename=None): |
180 | 181 | ax.legend(["Liquid", "Gas", "Total"]) |
181 | 182 | show_or_save_plot(filename) |
182 | 183 |
|
| 184 | + def animate_fluid_volume(self, filename=None, fps=30): |
| 185 | + """Animates the liquid and gas volumes inside the tank as a function of time. |
| 186 | +
|
| 187 | + Parameters |
| 188 | + ---------- |
| 189 | + filename : str | None, optional |
| 190 | + The path the animation should be saved to. By default None, in which |
| 191 | + case the animation will be shown instead of saved. Supported file |
| 192 | + ending is: .gif |
| 193 | + fps : int, optional |
| 194 | + Frames per second for the animation. Default is 30. |
| 195 | +
|
| 196 | + Returns |
| 197 | + ------- |
| 198 | + matplotlib.animation.FuncAnimation |
| 199 | + The created animation object. |
| 200 | + """ |
| 201 | + |
| 202 | + t_start, t_end = self.flux_time |
| 203 | + times = np.linspace(t_start, t_end, 200) |
| 204 | + |
| 205 | + liquid_values = self.tank.liquid_volume.get_value(times) |
| 206 | + gas_values = self.tank.gas_volume.get_value(times) |
| 207 | + |
| 208 | + fig, ax = plt.subplots() |
| 209 | + |
| 210 | + ax.set_xlim(times[0], times[-1]) |
| 211 | + ax.set_ylim(0, max(liquid_values.max(), gas_values.max()) * 1.1) |
| 212 | + |
| 213 | + ax.set_xlabel("Time (s)") |
| 214 | + ax.set_ylabel("Volume (m³)") |
| 215 | + ax.set_title("Liquid/Gas Volume Evolution") |
| 216 | + (line_liquid,) = ax.plot([], [], lw=2, color="blue", label="Liquid Volume") |
| 217 | + (line_gas,) = ax.plot([], [], lw=2, color="red", label="Gas Volume") |
| 218 | + |
| 219 | + (point_liquid,) = ax.plot([], [], "ko") |
| 220 | + (point_gas,) = ax.plot([], [], "ko") |
| 221 | + |
| 222 | + ax.legend() |
| 223 | + |
| 224 | + def init(): |
| 225 | + for item in (line_liquid, line_gas, point_liquid, point_gas): |
| 226 | + item.set_data([], []) |
| 227 | + return line_liquid, line_gas, point_liquid, point_gas |
| 228 | + |
| 229 | + def update(frame_index): |
| 230 | + # Liquid part |
| 231 | + line_liquid.set_data( |
| 232 | + times[: frame_index + 1], liquid_values[: frame_index + 1] |
| 233 | + ) |
| 234 | + point_liquid.set_data([times[frame_index]], [liquid_values[frame_index]]) |
| 235 | + |
| 236 | + # Gas part |
| 237 | + line_gas.set_data(times[: frame_index + 1], gas_values[: frame_index + 1]) |
| 238 | + point_gas.set_data([times[frame_index]], [gas_values[frame_index]]) |
| 239 | + |
| 240 | + return line_liquid, line_gas, point_liquid, point_gas |
| 241 | + |
| 242 | + animation = FuncAnimation( |
| 243 | + fig, |
| 244 | + update, |
| 245 | + frames=len(times), |
| 246 | + init_func=init, |
| 247 | + interval=1000 / fps, |
| 248 | + blit=True, |
| 249 | + ) |
| 250 | + |
| 251 | + show_or_save_animation(animation, filename, fps=fps) |
| 252 | + |
| 253 | + return animation |
| 254 | + |
183 | 255 | def all(self): |
184 | 256 | """Prints out all graphs available about the Tank. It simply calls |
185 | 257 | all the other plotter methods in this class. |
|
0 commit comments