statescale#

SnapshotModel(snapshots[, point_data, ...])

A model with point-, cell- and field-data at snapshots and with methods to interpolate the data at points of interest.

ModelResult([point_data, cell_data, field_data])

Snapshot model result data.

Detailed API Description

class statescale.SnapshotModel(snapshots, point_data=None, cell_data=None, field_data=None, kernel='griddata', **kwargs)[source]#

A model with point-, cell- and field-data at snapshots and with methods to interpolate the data at points of interest.

Parameters:
  • snapshots (np.array) – Snapshots of shape (n_snapshots, n_dim). Note that a signal, used for the evaluation of the data, must have equal n_dim.

  • point_data (list of dict or dict or None, optional) – A dict of point data. The lengths of all arrays must be equal and of shape (n_snapshots, …). If a list of dict is given, each item must hold the dict of a single snapshot.

  • cell_data (list of dict or dict or None, optional) – A dict of cell data. The lengths of all arrays must be equal and of shape (n_snapshots, …). If a list of dict is given, each item must hold the dict of a single snapshot.

  • field_data (dict or None, optional) – A dict of time-independent field data.

  • kernel (str, optional) – The kernel to be used for interpolation. Either "surrogate" or "griddata". Default is "griddata".

  • **kwargs (dict, optional) – Additional keyword arguments for the calibration of the kernel. See SurrogateKernel and GriddataKernel for more details.

Notes

Naming conventions#

Symbol

Description

x_si

Snapshots

d_s...

Time-dependent data at snapshots with arbitrary trailing axes

x_ai

Signal

d_a...

Data for the signal (with arbitrary trailing axes)

Indices#

Index

Description

s

s-th snapshot

i

i-th vector component of snapshot / signal

a

a-th (time-) step of signal

...

optional arbritrary trailing axes

Examples

A minimal example. Snapshots must have shapes (n_snapshots, n_dim), point- and cell-data (n_snapshots, ...) and the dimension of the signal must be compatible with snapshots, i.e. (n_steps, n_dim). The second dimension of snapshots and the signal are optional, 1d-arrays are also supported. The model result will be of shape (n_steps, ...).

  1. Array-based input data

    import numpy as np
    import statescale
    
    snapshots = np.linspace(0, 1, num=3).reshape(-1, 1)  # 3 snapshots, 1 parameter
    point_data = {"displacement": np.random.rand(3, 9, 3)}  # 3 snapshots, 9 points, 3 dim
    cell_data = {"strain": np.random.rand(3, 4, 6)}  # 3 snapshots, 4 cells, 6 dim
    field_data = {"id": 1001}  # time-independent data
    
    model = statescale.SnapshotModel(
        snapshots=snapshots,
        point_data=point_data,
        cell_data=cell_data,
        field_data=field_data,
        # kernel="surrogate",  # use a POD surrogate model
        # modes=(2, 10),  # min- and max no. of modes for surrogate model
        # threshold=0.999,  # min. energy threshold for surrogate model
    )
    
    signal = np.linspace(0, 1, num=20).reshape(-1, 1)  # 20 items, 1 parameter
    
    # a `ModelResult` object with `point_data`, `cell_data` and `field_data`.
    res = model.evaluate(signal)
    
  2. List-based input data

    If the data is list-based, the model can also import lists of dicts, with per- snapshot list items. Model results also support indexing and a conversion to lists of dicts.

    import numpy as np
    import statescale
    
    point_data = [
        {"displacement": np.random.rand(6, 2)},  # 1. snapshot, 6 points, 2 dim
        {"displacement": np.random.rand(6, 2)},  # 2. snapshot, 6 points, 2 dim
        {"displacement": np.random.rand(6, 2)},  # 3. snapshot, 6 points, 2 dim
    ]
    cell_data = [
        {"strain": np.random.rand(4, 2, 2)},  # 1. snapshot, 4 cells, (2, 2) dim
        {"strain": np.random.rand(4, 2, 2)},  # 2. snapshot, 4 cells, (2, 2) dim
        {"strain": np.random.rand(4, 2, 2)},  # 3. snapshot, 4 cells, (2, 2) dim
    ]
    
    model = statescale.SnapshotModel(
        snapshots=snapshots,
        point_data=point_data,
        cell_data=cell_data,
        field_data=field_data,
    )
    
    # `point_data`, `cell_data` and `field_data` for step 5 of the signal.
    res_5 = model.evaluate(signal)[5]
    

Any NumPy-function may be applied to the model result data on all time-dependent arrays. E.g., the mean over all cells (here, the first axis) of the cell-data is evaluated by:

res_5_mean = res_5.apply(
    np.mean, on_point_data=False, on_cell_data=True
)(axis=0)

See also

statescale.kernels.SurrogateKernel

A surrogate kernel.

statescale.kernels.GriddataKernel

A griddata kernel.

evaluate(xi, method='griddata', **kwargs)[source]#

Evaluate the point- and cell-data at xi.

Parameters:
  • xi (np.array) – Points at which to interpolate data.

  • method (str, optional) – Use "griddata" or "rbf". Default is "griddata".

  • **kwargs (dict, optional) – Optional keyword-arguments are passed to the evaluate() method of the kernel. See SurrogateKernel and GriddataKernel for more details.

Returns:

The model result with attributes for time-dependent point_data, cell_data and time-independent field_data.

Return type:

ModelResult

evaluate_cell_data(xi, indices=None, axis=None, method='griddata', **kwargs)[source]#

Evaluate cell-data at xi.

Parameters:
  • xi (np.array) – Points at which to interpolate data.

  • indices (array_like or None, optional) – The indices of the values to extract. Also allow scalars for indices. Default is None.

  • axis (int or None, optional) – The axis over which to select values. By default, the flattened input array is used. Default is None.

  • method (str, optional) – Use "griddata" or "rbf". Default is "griddata".

  • **kwargs (dict, optional) – Optional keyword-arguments are passed to the evaluate() method of the kernel. See SurrogateKernel and GriddataKernel for more details.

Returns:

The model result with attributes for time-dependent (empty) point_data, cell_data and time-independent field_data.

Return type:

ModelResult

evaluate_point_data(xi, indices=None, axis=None, method='griddata', **kwargs)[source]#

Evaluate point-data at xi.

Parameters:
  • xi (np.array) – Points at which to interpolate data.

  • indices (array_like or None, optional) – The indices of the values to extract. Also allow scalars for indices. Default is None.

  • axis (int or None, optional) – The axis over which to select values. By default, the flattened input array is used. Default is None.

  • method (str, optional) – Use "griddata" or "rbf". Default is "griddata".

  • **kwargs (dict, optional) – Optional keyword-arguments are passed to the evaluate() method of the kernel. See SurrogateKernel and GriddataKernel for more details.

Returns:

The model result with attributes for time-dependent point_data, (empty) cell_data and time-independent field_data.

Return type:

ModelResult

classmethod from_kernel(kernel)[source]#

Return a model with the loaded kernel.

classmethod load_kernel(filename)[source]#

Return a model with the loaded kernel file.

classmethod load_model(filename)[source]#

Return a model, loaded from a file.

save_kernel(filename='kernel.npz')[source]#

Save the kernel to a file.

save_model(filename='model.npz')[source]#

Save the model to a file.

class statescale.ModelResult(point_data: list | dict | None = None, cell_data: list | dict | None = None, field_data: dict | None = None)[source]#

Snapshot model result data.

property T#

Return a model result with the data transposed.

apply(func, on_point_data=False, on_cell_data=False, on_field_data=False)[source]#

Apply any function to the arrays of all or selected data dicts.

Parameters:
  • func (callable) – The function to be applied.

  • on_point_data (bool, optional) – A flag to apply the function on the point data. Default is False.

  • on_cell_data (bool, optional) – A flag to apply the function on the cell data. Default is False.

  • on_field_data (bool, optional) – A flag to apply the function on the field data. Default is False.

Returns:

apply_func – A transformed function, which applies the given function to all arrays.

param *args:

Additional arguments are passed to the given function.

type *args:

tuple, optional

param **kwargs:

Additional arguments are passed to the given function.

type **kwargs:

dict, optional

Return type:

callable

Notes

By default, only the time-dependent point- and cell-data arrays are modified.

Examples

>>> import numpy as np
>>> import statescale
>>>
>>> res = statescale.Modelresult(point_data={"u": np.random.rand(25, 100, 8, 3)})
>>> out = res.apply(np.mean, on_point_data=True)(axis=-2)
>>>
>>> out.point_data["u"].shape
(25, 100, 3)
as_view(mesh=None, field=None, inplace=False, update=None, **kwargs)[source]#

Return a view on a given felupe.Mesh or felupe.FieldContainer with added point- and cell-data.

Parameters:
  • mesh (felupe.Mesh or None, optional) – A mesh which is used to apply the data. Default is None.

  • field (felupe.FieldContainer or None, optional) – A field container which is used to apply the data. Default is None.

  • inplace (bool, optional) – A flag to modify the given field inplace. Default is False.

  • update (str or None, optional) – The key of the point data to be used for updating the values of the first field. If None, the field values are not updated. Default is None.

  • **kwargs (dict, optional) – Additional arguments are passed to felupe.FieldContainer.view().

Returns:

view – A view on the mesh or field with the model result data. The pyvista. UnstructuredGrid is available via felupe.ViewMesh.mesh or felupe.ViewField.mesh.

Return type:

felupe.ViewMesh or felupe.ViewField

cell_data: list | dict | None = None#
field_data: dict | None = None#
point_data: list | dict | None = None#