The Mesh class

class pylife.mesh.Mesh(pandas_obj)[source]

DataFrame accessor to access FEM mesh data (2D and 3D)

Raises:
  • AttributeError – if at least one of the columns x, y is missing

  • AttributeError – if the index of the DataFrame is not a two level MultiIndex with the names node_id and element_id

Notes

The Mesh describes how we expect FEM data to look like. It consists of nodes identified by node_id and elements identified by element_id. A node playing a role in several elements and an element consists of several nodes. So in the DataFrame a node_id can appear multiple times (for each element, the node is playing a role in). Likewise each element_id appears multiple times (for each node the element consists of).

The combination node_id:element_id however, is unique. So the table is indexed by a pandas.MultiIndex with the level names node_id, element_id.

See also

PlainMesh: accesses meshes without connectivity information pandas.api.extensions.register_dataframe_accessor(): concept of DataFrame accessors

Examples

For an example see hotspot.

broadcast(parameter, droplevel=None)

Broadcast the parameter to the object of self.

Parameters:

parameters (scalar, numpy array or pandas object) – The parameter to broadcast to

Returns:

parameter, object

Return type:

index aligned numerical objects

Examples

The behavior of the Broadcaster is best illustrated by examples:

  • Broadcasting pandas.Series to a scalar results in a scalar and a pandas.Series.

    obj = pd.Series([1.0, 2.0], index=pd.Index(['foo', 'bar'], name='idx'))
    obj
    
    idx
    foo    1.0
    bar    2.0
    dtype: float64
    
    parameter, obj = Broadcaster(obj).broadcast(5.0)
    
    parameter
    
    array(5.)
    
    obj
    
    idx
    foo    1.0
    bar    2.0
    dtype: float64
    
  • Broadcasting pandas.DataFrame to a scalar results in a pandas.DataFrame and a pandas.Series.

    obj = pd.DataFrame({
        'foo': [1.0, 2.0],
        'bar': [3.0, 4.0]
    }, index=pd.Index([1, 2], name='idx'))
    obj
    
    foo bar
    idx
    1 1.0 3.0
    2 2.0 4.0
    parameter, obj = Broadcaster(obj).broadcast(5.0)
    
    parameter
    
    idx
    1    5.0
    2    5.0
    dtype: float64
    
    obj
    
    foo bar
    idx
    1 1.0 3.0
    2 2.0 4.0
  • Broadcasting pandas.DataFrame to a a pandas.Series results in a pandas.DataFrame and a pandas.Series, if and only if the index name of the object is None.

    obj = pd.Series([1.0, 2.0], index=pd.Index(['tau', 'chi']))
    obj
    
    tau    1.0
    chi    2.0
    dtype: float64
    
    parameter = pd.Series([3.0, 4.0], index=pd.Index(['foo', 'bar'], name='idx'))
    parameter
    
    idx
    foo    3.0
    bar    4.0
    dtype: float64
    
    parameter, obj = Broadcaster(obj).broadcast(parameter)
    
    parameter
    
    idx
    foo    3.0
    bar    4.0
    dtype: float64
    
    obj
    
    tau chi
    idx
    foo 1.0 2.0
    bar 1.0 2.0
property connectivity

The connectivity of the mesh.

property coordinates

Returns the coordinate colums of the accessed DataFrame

Returns:

coordinates – The coordinates x, y and if 3D z of the accessed mesh

Return type:

pandas.DataFrame

property dimensions

The dimensions of the mesh (2 for 2D and 3 for 3D)

Note

If all the coordinates in z-direction are equal the mesh is considered 2D.

fail_if_key_missing(keys_to_check, msg=None)

Raise an exception if any key is missing in a self._obj object.

Parameters:
Raises:
  • AttributeError – if self._obj is neither a pandas.DataFrame nor a pandas.Series

  • AttributeError – if any of the keys is not found in the self._obj’s keys.

Notes

If self._obj is a pandas.DataFrame, all keys of keys_to_check meed to be found in the self._obj.columns.

If self._obj is a pandas.Series, all keys of keys_to_check meed to be found in the self._obj.index.

See also

get_missing_keys(), stresssignal.StressTensorVoigt

classmethod from_parameters(**kwargs)

Make a signal instance from a parameter set.

This is a convenience function to instantiate a signal from individual parameters rather than pandas objects.

A signal class like

@pd.api.extensions.register_dataframe_accessor('foo_signal')
class FooSignal(PylifeSignal):
    pass

The following two blocks are equivalent:

pd.Series({'foo': 1.0, 'bar': 2.0}).foo_signal
FooSignal.from_parameters(foo=1.0, bar=1.0)
get_missing_keys(keys_to_check)

Get a list of missing keys that are needed for a self._obj object.

Parameters:

keys_to_check (list) – A list of keys that need to be available in self._obj

Returns:

missing_keys – a list of missing keys

Return type:

list

Raises:

AttributeError – if self._obj is neither a pandas.DataFrame nor a pandas.Series

Notes

If self._obj is a pandas.DataFrame, all keys of keys_to_check not found in the self._obj.columns are returned.

If self._obj is a pandas.Series, all keys of keys_to_check not found in the self._obj.index are returned.

keys()

Get a list of missing keys that are needed for a signal object.

Returns:

keys – a pandas index of keys

Return type:

pd.Index

Raises:

AttributeError – if self._obj is neither a pandas.DataFrame nor a pandas.Series

Notes

If self._obj is a pandas.DataFrame, the self._obj.columns are returned.

If self._obj is a pandas.Series, the self._obj.index are returned.

to_pandas()

Expose the pandas object of the signal.

Returns:

pandas_object – The pandas object representing the signal

Return type:

pd.DataFrame or pd.Series

Notes

The default implementation just returns the object given when instantiating the signal class. Derived classes may return a modified object or augmented, if they store some extra information.

By default the object is not copied. So make a copy yourself, if you intent to modify it.

vtk_data()[source]

Make VTK data structure easily plot the mesh with pyVista.

Returns:

  • offsets (ndarray) – An empty numpy array as pyVista.UnstructuredGrid() still demands the argument for the offsets, even though VTK>9 does not accept it.

  • cells (ndarray) – The location of the cells describing the points in a way pyVista.UnstructuredGrid() needs it

  • cell_types (ndarray) – The VTK code for the cell types (see https://github.com/Kitware/VTK/blob/master/Common/DataModel/vtkCellType.h)

  • points (ndarray) – The coordinates of the cell points

Notes

This is a convenience function to easily plot a 3D mesh with pyVista. It prepares a data structure which can be passed to pyVista.UnstructuredGrid()

Example

>>> import pyvista as pv
>>> grid = pv.UnstructuredGrid(*our_mesh.mesh.vtk_data())
>>> plotter = pv.Plotter(window_size=[1920, 1080])
>>> plotter.add_mesh(grid, scalars=our_mesh.groupby('element_id')['val'].mean().to_numpy())
>>> plotter.show()

Note the * that needs to be added when calling pv.UnstructuredGrid().