Aggregation of Meshseries Data#

In this example we show how to aggregate data in a model over all timesteps as well as plot differences between to timesteps. For this purpose we use a component transport example from the ogs benchmark gallery (https://www.opengeosys.org/docs/benchmarks/hydro-component/elder/).

To see this benchmark results over all timesteps have a look at How to create Animations.

import numpy as np

import ogstools as ot
from ogstools import examples

mesh_series = examples.load_meshseries_CT_2D_XDMF(time_unit="a")
saturation = ot.variables.saturation

To read your own data as a mesh series you can do:

from ogstools.meshlib import MeshSeries
mesh_series = MeshSeries("filepath/filename_pvd_or_xdmf")

You can also use a variable from the available presets instead of needing to create your own: Variable presets and data transformation

You aggregate the data in MeshSeries over all timesteps given some aggregation function, e.g. np.min, np.max, np.var (see: aggregate_temporal()). The following code gets the maximum saturation for each point in the mesh over all timesteps and plots it. Note: the data in the returned mesh has a suffix equal to the aggregation functions name. The plot function will find the correct data anyway if given the original variable

mesh = mesh_series.aggregate_temporal(saturation, np.max)
fig = ot.plot.contourf(mesh, saturation)
plot aggregate

It is also possible to plot the time when the minimum or maximum occurs. However, here we have to use a new variable for the plot to handle the units correctly:

mesh = mesh_series.time_of_max(saturation)
fig = ot.plot.contourf(
    mesh, ot.variables.Scalar("max_Saturation_time", "a", "a")
)
plot aggregate

Likewise we can calculate and visualize the variance of the saturation:

mesh = mesh_series.aggregate_temporal(saturation, np.var)
fig = ot.plot.contourf(mesh, saturation)
plot aggregate

Difference between the last and the first timestep:

mesh = ot.meshlib.difference(
    mesh_series.mesh(-1), mesh_series.mesh(0), saturation
)
fig = ot.plot.contourf(mesh, saturation)
plot aggregate

It’s also possible to aggregate the data per timestep to return a timeseries of e.g. the max or mean value of a variable in the entire domain.

mean_satuarion_array = mesh_series.aggregate_spatial(saturation, np.mean)

Instead of calculating the array itself, you can also plot it directly by using methods of the Variable which correspond to the equally named numpy-functions. The following are available: min, max, mean, median, sum, std, var.

fig = mesh_series.plot_line(saturation.std)
fig.tight_layout()
plot aggregate

Total running time of the script: (0 minutes 4.368 seconds)