Note
Go to the end to download the full example code.
Variable presets and data transformation#
Section author: Florian Zill (Helmholtz Centre for Environmental Research GmbH - UFZ)
ogstools.variables
provides classes (Scalar, Vector, Matrix) which
encapsulate unit handling and data transformation for simplified processing of
mesh data. There are also several predefined variables.
import ogstools as ogs
from ogstools import examples
ogs.variables.get_dataframe()
Scalar, Vector and Matrix inherit from the class Variable with its
transform()
function.
This function converts the argument from data_unit to output_unit and
applies a function if specified. In this case we convert from K to °C:
ogs.variables.temperature.transform(273.15, strip_unit=False)
You can also create your own variables by creating a Scalar, Vector or Matrix variable. The following doesn’t do any unit conversion.
custom_temperature = ogs.variables.Scalar(
data_name="temperature", data_unit="K", output_unit="K"
)
custom_temperature.transform(273.15, strip_unit=False)
Or use existing presets as a template and replace some parameters:
custom_temperature = ogs.variables.temperature.replace(output_unit="°F")
custom_temperature.transform(273.15, strip_unit=False)
Components of Vector variables and Matrix variables can be accessed with
bracket indexing. Vector
variables
should be of length 2 or 3 corresponding to the dimension.
Matrix
variables likewise should be of
length 4 [xx, yy, zz, xy] or 6 [xx, yy, zz, xy, yz, xz].
ogs.variables.displacement[1].transform([0.01, 0.02, 0.03], strip_unit=False)
ogs.variables.strain["xx"].transform(
[0.01, 0.02, 0.03, 0.04, 0.05, 0.06], strip_unit=False
)
Magnitude of a 2D displacement vector:
ogs.variables.displacement.magnitude.transform([0.03, 0.04], strip_unit=False)
We suggest specifying the variables and their transformations once.
These can be reused in different kind of post processing. When plotting
with ogstools.plot
we can use these presets to simplify the
task of processing the data (e.g. calculate the von Mises stress):
fig = ogs.plot.contourf(
examples.load_mesh_mechanics_2D(), ogs.variables.stress.von_Mises
)
Have a look at Plotting the data for more examples.
Total running time of the script: (0 minutes 0.254 seconds)