Skip to content

Commit 474f48c

Browse files
committed
Add PropertyField.plot
Signed-off-by: paul.profizi <[email protected]>
1 parent 64c9f82 commit 474f48c

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/ansys/dpf/core/property_field.py

+53
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22
PropertyField
33
=============
44
"""
5+
from __future__ import annotations
56

67
import numpy as np
8+
from typing import Union, TYPE_CHECKING
9+
if TYPE_CHECKING:
10+
from ansys.dpf.core.results import Result
11+
from ansys.dpf.core.field import Field
12+
from ansys.dpf.core.dpf_operator import Operator
13+
from ansys.dpf.core.meshed_region import MeshedRegion
714
from ansys.dpf.core.check_version import version_requires
815
from ansys.dpf.core.common import natures, locations, _get_size_of_list
916
from ansys.dpf.core import scoping, dimensionality
@@ -17,6 +24,7 @@
1724
dpf_array,
1825
dpf_vector,
1926
)
27+
from ansys.dpf.core.plotter import DpfPlotter
2028

2129

2230
class PropertyField(_FieldBase):
@@ -342,6 +350,51 @@ def name(self, value):
342350
self._field_definition, name=value
343351
)
344352

353+
def plot(
354+
self,
355+
meshed_region: MeshedRegion,
356+
deform_by: Union[Field, Result, Operator, None] = None,
357+
scale_factor: float = 1.0,
358+
**kwargs,
359+
):
360+
"""Plot the PropertyField on a MeshedRegion.
361+
362+
Parameters
363+
----------
364+
meshed_region:
365+
The mesh to plot the property field onto.
366+
deform_by: Field, Result, Operator, optional
367+
Used to deform the plotted mesh. Must output a 3D vector field.
368+
Defaults to None.
369+
scale_factor:
370+
Scaling factor to apply when warping the mesh.
371+
**kwargs: optional
372+
Additional keyword arguments for the plotter. For additional keyword
373+
arguments, see ``help(pyvista.plot)``.
374+
375+
Examples
376+
--------
377+
Plot the displacement field from an example file.
378+
379+
>>> import ansys.dpf.core as dpf
380+
>>> from ansys.dpf.core import examples
381+
>>> model = dpf.Model(examples.find_multishells_rst())
382+
>>> mesh = model.metadata.meshed_region
383+
>>> pf = mesh.property_field(property_name="mat")
384+
>>> pf.plot(meshed_region=mesh)
385+
"""
386+
pl = DpfPlotter(**kwargs)
387+
pl.add_field(
388+
field=self,
389+
meshed_region=meshed_region,
390+
deform_by=deform_by,
391+
scale_factor=scale_factor,
392+
show_axes=kwargs.pop("show_axes", True),
393+
**kwargs,
394+
)
395+
kwargs.pop("notebook", None)
396+
return pl.show_figure(**kwargs)
397+
345398

346399
class _LocalPropertyField(_LocalFieldBase, PropertyField):
347400
"""Caches the internal data of a field so that it can be modified locally.

tests/test_plotter.py

+8
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ def test_mesh_property_field_plot(multishells):
7373
mesh.plot(pf)
7474

7575

76+
@pytest.mark.skipif(not HAS_PYVISTA, reason="Please install pyvista")
77+
def test_property_field_plot(multishells):
78+
model = core.Model(multishells)
79+
mesh = model.metadata.meshed_region
80+
pf = mesh.property_field(property_name="mat")
81+
pf.plot(meshed_region=mesh)
82+
83+
7684
@pytest.mark.skipif(not HAS_PYVISTA, reason="Please install pyvista")
7785
def test_plotter_on_mesh(allkindofcomplexity):
7886
model = Model(allkindofcomplexity)

0 commit comments

Comments
 (0)