geometry.Geometry.add_field#

Geometry.add_field(data, type=None, name=None)#

Add Field data to the Geometry.

Field data are scalar or vectorial data defined over the Geometry. This convenience function creates a Field object with the specified data and adds it to the Geometry object’s fields dict.

Parameters#

data: array_like

The field data. See Field for details.

type: str, optional

The field type. If None, it is automatically inferred:

  • if data length matches number of nodes → ‘node’

  • if data length matches number of elements → ‘elem’

name: str

The field name. See Field for details. This name is also used as key to store the Field in the fields dict.

Returns#

Field

The constructed and stored Field object.

See Also#

Examples#

>>> from hellotriangle.mesh import Mesh
>>> M = Mesh(eltype='quad4').subdivide(2)
>>> fld1 = M.add_field(np.asarray([0,1,1,2]), type='elem', name='elemnr')
>>> print(fld1)
Field 'elemnr', type 'elem', shape (4,), n_nodes=9, n_elems=4, nplex=4
[0 1 1 2]
>>> fld2 = M.convert_field('elemnr', totype='node', new_name='onnodes')
>>> print(fld2)
Field 'onnodes', type 'node', shape (9,), n_nodes=9, n_elems=4, nplex=4
[0.  0.5 1.  0.5 1.  1.5 1.  1.5 2. ]
>>> print(M.field_report())
Field 'elemnr', fldtype 'elem', dtype int64, shape (4,)
Field 'onnodes', fldtype 'node', dtype float64, shape (9,)
>>> M.del_field('elemnr')
>>> print(M.field_report())
Field 'onnodes', fldtype 'node', dtype float64, shape (9,)