Note
Go to the end to download the full example code
How to create Animations#
Section author: Florian Zill (Helmholtz Centre for Environmental Research GmbH - UFZ)
To demonstrate the creation of an animated plot we use a component transport example from the ogs benchmark gallery (https://www.opengeosys.org/docs/benchmarks/hydro-component/elder/).
import numpy as np
from ogstools.meshplotlib import examples, setup
from ogstools.meshplotlib.animation import animate
from ogstools.propertylib import Scalar
setup.reset()
mesh_series = examples.meshseries_CT_2D
# alternatively:
# from ogstools.meshlib import MeshSeries
# mesh_series = MeshSeries("filepath/filename_pvd_or_xdmf")
Let’s use fixed scale limits to prevent rescaling during the animation.
setup.p_min = 0
setup.p_max = 100
You can choose which timesteps to render by passing either an int array corresponding to the indices, or a float array corresponding to the timevalues to render. If a requested timevalue is not part of the timeseries it will be interpolated. In this case every second frame will be interpolated.
timevalues = np.linspace(
mesh_series.timevalues[0], mesh_series.timevalues[-1], num=25
)
Now, let’s animate the saturation solution. A timescale at the top indicates existing timesteps and the position of the current timevalue. Note that rendering many frames in conjunction with large meshes might take a really long time.
titles = [f"{tv/(365.25*86400):.1f} yrs" for tv in timevalues]
si = Scalar("Si", "", "%", "Saturation")
anim = animate(mesh_series, si, timevalues, titles)
# the animation can be saved (as mp4) like so:
# from ogstools.meshplotlib.animation import save_animation
# save_animation(anim, "Saturation", fps=5)
Total running time of the script: (0 minutes 12.252 seconds)