Field#
- class ansys.dpf.core.field.Field(nentities=0, nature=natures.vector, location='Nodal', field=None, server=None)#
Represents the main simulation data container.
This can be evaluated data from the
Operator
class or created by a factory and directly by an instance of this class.A field’s data is always associated to its scoping (entities associated to each value) and support (subset of the model where the data is), making the field a self-describing piece of data.
The field’s scoping defines the order of the data, for example: the first ID in the
scoping
identifies to which entity the firstentity data
belongs.For more information, see the Fields container and fields documentation section.
- Parameters:
nentities (int, optional) – Number of entities reserved. The default is
0
.nature (
ansys.dpf.core.common.natures
, optional) – Nature of the field.location (str, optional) –
Location of the field. Options are in
locations
dpf.locations.nodal
dpf.locations.elemental
dpf.locations.elemental_nodal
…
field (Field, ansys.grpc.dpf.field_pb2.Field, ctypes.c_void_p, optional) – Field message generated from a gRPC stub, or returned by DPF’s C clients.
server (
ansys.dpf.core.server
, optional) – Server with the channel connected to the remote or local instance. The default isNone
, in which case an attempt is made to use the global server.
Examples
Create a field from scratch.
>>> from ansys.dpf.core import fields_factory >>> from ansys.dpf.core import locations >>> from ansys.dpf import core as dpf >>> field_with_classic_api = dpf.Field() >>> field_with_classic_api.location = locations.nodal >>> field_with_factory = fields_factory.create_scalar_field(10)
Extract a displacement field from a transient result file.
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> transient = examples.download_transient_result() >>> model = dpf.Model(transient) >>> disp = model.results.displacement() >>> fields_container = disp.outputs.fields_container() >>> field = fields_container[0] >>> len(field) 11460 >>> field.component_count 3 >>> field.elementary_data_count 3820
Create a displacement field.
>>> from ansys.dpf import core as dpf >>> import numpy as np >>> my_field = dpf.Field(10, dpf.natures.vector,dpf.locations.nodal) >>> my_field.data = np.zeros(30) >>> my_field.scoping.ids = range(1,11)
Set data.
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> transient = examples.download_transient_result() >>> model = dpf.Model(transient) >>> disp = model.results.displacement() >>> fields_container = disp.outputs.fields_container() >>> field = fields_container[0] >>> field.data[2] DPFArray([-0.00672665, -0.03213735, 0.00016716]...
Accessing data with a custom order.
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> transient = examples.download_transient_result() >>> model = dpf.Model(transient) >>> ids_order = [2,3] >>> stress = model.results.stress(mesh_scoping=dpf.Scoping( ... ids=ids_order, location=dpf.locations.nodal)) >>> fields_container = stress.outputs.fields_container() >>> field = fields_container[0] >>> field.scoping.ids DPFArray([3, 2]... >>> field.data DPFArray([[ 3755059.33333333, -2398534.3515625 , -27519072.33333333, 2194748.65625 , 8306637.58333333, 2018637.03125 ], [ 2796852.09375 , -992492.62304688, 22519752.625 , -1049027.46875 , 10846776.1875 , 4119072.3125 ]]... >>> field.get_entity_data_by_id(2) DPFArray([[ 2796852.09375 , -992492.62304688, 22519752.625 , -1049027.46875 , 10846776.1875 , 4119072.3125 ]]... >>> field.get_entity_data_by_id(3) DPFArray([[ 3755059.33333333, -2398534.3515625 , -27519072.33333333, 2194748.65625 , 8306637.58333333, 2018637.03125 ]]...
- as_local_field()#
Create a deep copy of the field that can be accessed and modified locally.
This method allows you to access and modify the local copy of the field without sending a request to the server. It should be used in a
with
statement so that the local field is released and the data is sent to the server in one action. If it is not used in awith
statement,<release_data> Field.release_data()
should be used to update the field.Warning
If this as_local_field method is not used as a context manager in a
with
statement or if the method release_data() is not called, the data will not be updated.- Returns:
local_field
- Return type:
Examples
>>> from ansys.dpf import core as dpf >>> num_entities = 3 >>> field_to_local = dpf.fields_factory.create_3d_vector_field(num_entities, location=dpf.locations.elemental_nodal) >>> with field_to_local.as_local_field() as f: ... for i in range(1,num_entities+1): ... f.append([[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]],i) ... f.get_entity_data(i-1),[[0.1*i,0.2*i, 0.3*i],[0.1*i,0.2*i, 0.3*i]] (DPFArray([[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]), [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3]]) (DPFArray([[0.2, 0.4, 0.6], [0.2, 0.4, 0.6]]), [[0.2, 0.4, 0.6], [0.2, 0.4, 0.6]]) (DPFArray([[0.3, 0.6, 0.9], [0.3, 0.6, 0.9]]), [[0.30000000000000004, 0.6000000000000001, 0.8999999999999999], [0.30000000000000004, 0.6000000000000001, 0.8999999999999999]])
- property location#
Field location.
- Returns:
Location string, Options are in
locations
.- Return type:
str
Examples
Location for a stress field evaluated at nodes.
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> model = dpf.Model(examples.download_transient_result()) >>> s_op = model.results.stress() >>> s_fc = s_op.outputs.fields_container() >>> field = s_fc[0] >>> field.location 'ElementalNodal'
- property component_count#
Number of components in each elementary data of the field.
- Returns:
Number of components in each elementary data of the field.
- Return type:
int
- property elementary_data_count#
Number of elementary data in the field.
- Returns:
Number of elementary data in the field.
- Return type:
int
- property size#
Length of the data vector.
The length is equal to the number of elementary data times the number of components.
- Returns:
Length of the data vector.
- Return type:
int
- property shell_layers#
Order of the shell layers.
- Return type:
- get_entity_data(index: int)#
Retrieves the elementary data of the scoping’s index in an array.
- Return type:
numpy.ndarray
Examples
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> transient = examples.download_transient_result() >>> model = dpf.Model(transient) >>> stress_op = model.results.stress() >>> fields_container = stress_op.outputs.fields_container() >>> fields_container[0].get_entity_data(0) DPFArray([[-3.27795062e+05, 1.36012200e+06, 1.49090608e+08, -4.88688900e+06, 1.43038560e+07, 1.65455040e+07], [-4.63817550e+06, 1.29312225e+06, 1.20411832e+08, -6.06617800e+06, 2.34829700e+07, 1.77231120e+07], [-2.35684860e+07, -3.53474400e+07, 2.01501168e+08, -5.23361700e+06, -2.88789280e+07, -6.16478200e+06], [-3.92756960e+07, -2.72369280e+07, 1.81454016e+08, -3.75441450e+06, -3.62480300e+06, -3.26075620e+07], [ 1.63554530e+07, 2.83190520e+07, 1.05084256e+08, -1.30219020e+07, 5.19906719e+05, 8.82430200e+06], [ 1.80755620e+07, 5.25578750e+06, 7.76211600e+07, -7.53063750e+06, 2.44717000e+06, 2.92675125e+06], [ 9.25567760e+07, 8.15244320e+07, 2.77157632e+08, -1.48489875e+06, 5.89250600e+07, 2.05608920e+07], [ 6.70443680e+07, 8.70343440e+07, 2.73050464e+08, -2.48670150e+06, 1.52268930e+07, 6.09583280e+07]]...
- get_entity_data_by_id(id: int)#
Retrieve the data of the scoping’s ID in the parameter of the field in an array.
- Returns:
Data based on the scoping ID.
- Return type:
numpy.ndarray
Examples
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> transient = examples.download_transient_result() >>> model = dpf.Model(transient) >>> stress_op = model.results.stress() >>> fields_container = stress_op.outputs.fields_container() >>> fields_container[0].get_entity_data_by_id(391) DPFArray([[-3.27795062e+05, 1.36012200e+06, 1.49090608e+08, -4.88688900e+06, 1.43038560e+07, 1.65455040e+07], [-4.63817550e+06, 1.29312225e+06, 1.20411832e+08, -6.06617800e+06, 2.34829700e+07, 1.77231120e+07], [-2.35684860e+07, -3.53474400e+07, 2.01501168e+08, -5.23361700e+06, -2.88789280e+07, -6.16478200e+06], [-3.92756960e+07, -2.72369280e+07, 1.81454016e+08, -3.75441450e+06, -3.62480300e+06, -3.26075620e+07], [ 1.63554530e+07, 2.83190520e+07, 1.05084256e+08, -1.30219020e+07, 5.19906719e+05, 8.82430200e+06], [ 1.80755620e+07, 5.25578750e+06, 7.76211600e+07, -7.53063750e+06, 2.44717000e+06, 2.92675125e+06], [ 9.25567760e+07, 8.15244320e+07, 2.77157632e+08, -1.48489875e+06, 5.89250600e+07, 2.05608920e+07], [ 6.70443680e+07, 8.70343440e+07, 2.73050464e+08, -2.48670150e+06, 1.52268930e+07, 6.09583280e+07]]...
- append(data, scopingid)#
Add an entity data to the existing data.
- Parameters:
data (list of int, double, or array) – Data in the entity.
scopingid (int) – ID of the scoping.
Examples
>>> from ansys.dpf.core import fields_factory >>> field = fields_factory.create_3d_vector_field(2) >>> field.append([1.,2.,3.],1) >>> field.append([1.,2.,3.],2) >>> field.data DPFArray([[1., 2., 3.], [1., 2., 3.]]... >>> field.scoping.ids ...[1, 2]...
- to_nodal()#
Convert the field to one with a
Nodal
location.This method is valid only when the field’s current location is
ElementalNodal
orElemental
.- Returns:
nodal_field – with
location=='Nodal'
.- Return type:
- plot(shell_layers=None, deform_by=None, scale_factor=1.0, **kwargs)#
Plot the field or fields container on the mesh support if it exists.
Warning
This method is primarily added out of convenience as plotting directly from the field can be slower than extracting the meshed region and plotting the field on top of that. It is more efficient to plot with:
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> transient = examples.download_transient_result() >>> model = dpf.Model(transient) >>> mesh = model.metadata.meshed_region >>> disp = model.results.displacement() >>> fields_container = disp.outputs.fields_container() >>> field = fields_container[0] >>> mesh.plot(field)
- Parameters:
shell_layers (shell_layers, optional) – Enum used to set the shell layers if the model to plot contains shell elements. The default is
None
.deform_by (Field, Result, Operator, optional) – Used to deform the plotted mesh. Must output a 3D vector field. Defaults to None.
scale_factor (float, optional) – Scaling factor to apply when warping the mesh. Defaults to 1.0.
**kwargs (optional) – Additional keyword arguments for the plotter. For additional keyword arguments, see
help(pyvista.plot)
.
- resize(nentities, datasize)#
Allocate memory.
- Parameters:
nentities (int) – Number of IDs in the scoping.
datasize (int) – Size of the data vector.
- property unit#
Units for the field.
- Returns:
Units for the field.
- Return type:
str
Examples
Units for a displacement field.
>>> from ansys.dpf import core as dpf >>> my_field = dpf.Field(10, dpf.natures.vector,dpf.locations.nodal) >>> my_field.unit = "m" >>> my_field.unit 'm'
- property dimensionality#
Dimensionality represents the shape of the elementary data contained in the field.
- Returns:
dimensionality – Nature and size of the elementary data.
- Return type:
- property name#
Name of the field.
- property field_definition#
Field information, including its location, unit, dimensionality, and shell layers.
- Return type:
- property time_freq_support#
Time frequency support of the field.
- Return type:
- property meshed_region#
Meshed region of the field.
- Return type:
- property data#
Data in the field as an array.
- Returns:
Data in the field.
- Return type:
numpy.ndarray
Notes
Print a progress bar.
- property data_as_list#
Data in the field as a Python list.
- Returns:
List of the data in the field.
- Return type:
List
Notes
Print a progress bar.
Examples
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> transient = examples.download_transient_result() >>> model = dpf.Model(transient) >>> disp = model.results.displacement() >>> fields_container = disp.outputs.fields_container() >>> field = fields_container[0] >>> # field.data_as_list
- property elementary_data_shape#
Numpy-like shape of the field.
- property scoping#
Scoping specifying where the data is.
Each entity data is on a given scoping ID.
- Returns:
scoping
- Return type:
Examples
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> transient = examples.download_transient_result() >>> model = dpf.Model(transient) >>> stress_op = model.results.stress() >>> fields_container = stress_op.outputs.fields_container() >>> scoping = fields_container[0].scoping >>> scoping.location 'Elemental' >>> scoping.id(3) 586 >>> #The fourth elementary data of the field corresponds to >>> #the element id number 586 in the mesh
- property shape#
Numpy-like shape of the field.
- Return type:
tuple
Examples
Shape of a stress field.
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> model = dpf.Model(examples.download_transient_result()) >>> s_op =model.results.stress() >>> s_fc = s_op.outputs.fields_container() >>> field = s_fc[0] >>> field.shape (5720, 6)
- min()#
Retrieve the component-wise minimum over this field.
- Returns:
min – Component-wise minimum field.
- Return type:
- max()#
Retrieve the component-wise maximum over this field.
- Returns:
max – Component-wise maximum field.
- Return type:
- deep_copy(server=None)#
Create a deep copy of the field’s data on a given server.
This method can be useful for passing data from one server instance to another.
- Parameters:
server (
ansys.dpf.core.server
, optional) – Server with the channel connected to the remote or local instance. The default isNone
, in which case an attempt is made to use the global server.- Returns:
field_copy
- Return type:
Examples
>>> from ansys.dpf import core as dpf >>> from ansys.dpf.core import examples >>> transient = examples.download_transient_result() >>> model = dpf.Model(transient) >>> disp = model.results.displacement() >>> fields_container = disp.outputs.fields_container() >>> field = fields_container[0] >>> other_server = dpf.start_local_server(as_global=False) >>> deep_copy = field.deep_copy(server=other_server)