ogstools.variables.variable module#

Defines the Scalar, Vector and Matrix Variable classes.

They serve as classes to handle common physical variables in a systematic way (e.g. temperature, pressure, displacement, …). Unit conversion is handled via pint.

class ogstools.variables.variable.Variable[source]#

Bases: object

Represent a generic mesh variable.

data_name: str#

The name of the variable data in the mesh.

data_unit: str = ''#

The unit of the variable data in the mesh.

output_unit: str = None#

The output unit of the variable.

output_name: str = None#

The output name of the variable.

symbol: str = ''#

The symbol representing this variable.

mask: str = ''#

The name of the mask data in the mesh.

func: dataclasses.InitVar[ogstools.variables.func.Function | collections.abc.Callable | None] = None#

The function to be applied on the data.

functions: list[Function]#

Contains this and all previous functions.

process_with_units: bool = False#

If true, apply the function on values with units.

cmap: Colormap | str = 'coolwarm'#

Colormap to use for plotting.

bilinear_cmap: bool = False#

Should this variable be displayed with a bilinear cmap?

categoric: bool = False#

Does this variable only have categoric values?

color: str | None = None#

Default color for plotting

property parent: Variable#

Return the Variable from which self is derived - if None return self.

property function: Function | None#

Returns the final function

property type_name: str#

Return the type of this Variable.

replace(**changes)[source]#

Create a new Variable object with modified attributes.

Be aware that there is no type check safety here. So make sure, the new attributes and values are correct.

Parameters:

changes (Any) – Attributes to be changed.

Return type:

Self

Returns:

A copy of the Variable with changed attributes.

copy(deep=True)[source]#

Return a copy of this Variable (by default a deep copy).

Return type:

Self

classmethod from_variable(variable, func, suffix=None, prefix=None, **changes)[source]#

Create a new derived variable.

func is appended to the stored functions. prefix and/or suffix are prepended to and appended to the base output_name (separated by “_”); at least one has to be set. To change other attributes pass them as keyword arguments;

Parameters:
  • variable (Variable) – The variable to base the new one on.

  • func (Callable | Function) – The function to append to the stored functions.

  • prefix (str | int | None) – Prepend this to the output_name.

  • suffix (str | int | None) – Append this to the output_name.

  • changes (Any) – Other attributes to override in the new variable.

Return type:

Self

Returns:

A new Variable with the applied changes.

Raises:

KeyError – If neither prefix nor suffix is set, or if output_name or functions are given in changes.

classmethod find(variable, data)[source]#

Return a Variable corresponding to variable for the given data.

data may be a mesh or a MeshSeries. The name is resolved against predefined variables (data_name and output_name), mesh data and attributes of data (e.g. points, timevalues). “x”, “y”, “z” and “points*” resolve to the coordinates and “t”, “time”, “timevalues” to the time values; a trailing _-suffix (e.g. "displacement_0") selects a component of a vector/matrix variable.

A Variable variable whose output_name is found in the data is returned reading that data directly (functions discarded); one whose data_name is found is returned unchanged. Otherwise a new Scalar, Vector or Matrix Variable is created depending on the shape of the data in the mesh.

Parameters:
Return type:

Variable

Returns:

A matching predefined or new Variable.

Raises:

KeyError – If the variable cannot be found in the data.

transform(data, strip_unit=True)[source]#

Return the transformed data values.

Converts the data from data_unit to output_unit and applies the transformation function of this variable. The result is returned by default without units. if strip_unit is False, a quantity is returned.

Note: If applied on a mesh or a meshseries, the data_name is read from the dataset and passed to the stored function. If applied on numeric data, it is passed to the function. if process_with_units is True data is passed to the function with units (i.e. as a pint quantity).

Return type:

ndarray | PlainQuantity

property get_output_unit: str#

Return the output unit

property aggregations: int#

Returns the number of aggregating function stored in this Variable.

aggregation_variable(func)[source]#

Return an aggregating Variable corresponding to the function.

Return type:

Self

aggregate(func, **kwargs)[source]#

Create a Variable aggregating the data with func.

Return type:

Self

normalize_aggregation(func=None)[source]#

Return a variable with exactly one aggregation function.

If all previous functions plus the optionally given one contain not exactly one aggregating function an Error is thrown.

Return type:

Self

change_axis(axis)[source]#

Variable with an updated axis of its last aggregation function.

Return type:

Self

property min: Self#

A variable relating to the minimum of this quantity.

property max: Self#

A variable relating to the maximum of this quantity.

property mean: Self#

A variable relating to the mean of this quantity.

property median: Self#

A variable relating to the median of this quantity.

property sum: Self#

A variable relating to the sum of this quantity.

property std: Self#

A variable relating to the standard deviation of this quantity.

property var: Self#

A variable relating to the variance of this quantity.

property argmin: Self#

A variable relating to the minimum index of this quantity.

property argmax: Self#

A variable relating to the maximum index of this quantity.

property time_of_min: Self#

A variable relating to the time of the minimum of this quantity.

property time_of_max: Self#

A variable relating to the time of the maximum of this quantity.

property abs: Self#

A variable relating to absolute value of this quantity.

property difference: Variable#

A variable relating to differences in this quantity.

rate(time_unit='s')[source]#

return a variable relating to rate change of this quantity.

Return type:

Self

property abs_error: Variable#

A variable relating to an absolute error of this quantity.

property rel_error: Variable#

A variable relating to a relative error of this quantity.

property anasol: Variable#

A variable relating to an analytical solution of this quantity.

is_mask()[source]#

Check if the variable is a mask.

Return type:

bool

Returns:

True if the variable is a mask, False otherwise.

get_mask()[source]#

Return a variable representing this variables mask.

Return type:

Variable

property magnitude: Variable#

Return a variable representing this variables magnitude.

convert_stress(to, biot=1.0, bishop=1.0, pressure='pressure')[source]#

Convert from effective to total stress or vice versa on a deep copy.

The conversion is applied before any other stored function. Tension is positive, the pore pressure is positive in compression:

\[\sigma' = \sigma_\mathrm{total} \pm \alpha_B \chi p \mathbf{I}\]
Parameters:
  • biot (float | str) – Biot coefficient \(\alpha_B\), either a constant or the name of a data field (e.g. set per material from the MaterialIDs) in the mesh.

  • bishop (float | str) – Bishop’s coefficient \(\chi\), either a constant or the name of a data field (e.g. a precomputed \(\chi(S_L)\)) in the mesh.

  • pressure (str) – Name of the pore pressure data field in the mesh.

Return type:

Self

__init__(data_name, data_unit='', output_unit=None, output_name=None, symbol='', mask='', func=None, functions=<factory>, process_with_units=False, cmap='coolwarm', bilinear_cmap=False, categoric=False, color=None, _parent=None)#
mask_used(mesh)[source]#

Check whether the mesh contains the mask of this variable.

Return type:

bool

get_label(split_at=None)[source]#

Create a variable label in format ‘variable_name / variable_unit’

Return type:

str

property gradient: Vector | Matrix#

Return a variable representing this variables gradient.

property divergence: Vector | Scalar#

Return a variable representing this variables divergence.

property vorticity: Vector#

Return a variable representing this variables vorticity.

property qcriterion: Vector | Scalar#

Return a variable representing this variables qcriterion.

ogstools.variables.variable.spatial_var(var_name, data)[source]#

Return a variable representing the point coordinates of a mesh.

Return type:

Scalar | Vector

ogstools.variables.variable.time_var(var_name, data)[source]#

Return a variable representing the timevalues of a MeshSeries.

Return type:

Scalar