Skip to content

Commit 92d7508

Browse files
committed
Add DpfPlotter.add_scoping and scoping.plot
1 parent f5118de commit 92d7508

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

src/ansys/dpf/core/plotter.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import tempfile
3737
from typing import TYPE_CHECKING, List, Union
3838
import warnings
39+
from xml.etree.ElementInclude import include
3940

4041
import numpy as np
4142

@@ -234,6 +235,37 @@ def get_label_at_grid_point(index):
234235
)
235236
return label_actors
236237

238+
def add_scoping(
239+
self,
240+
scoping: dpf.core.Scoping,
241+
mesh: dpf.core.MeshedRegion,
242+
show_mesh: bool = False,
243+
**kwargs,
244+
):
245+
# Add the mesh to the scene with low opacity
246+
if show_mesh:
247+
self._plotter.add_mesh(mesh=mesh.grid, opacity=0.3)
248+
249+
scoping_mesh = None
250+
251+
# If the scoping is nodal, use the add_points_label method
252+
if scoping.location == locations.nodal:
253+
node_indexes = np.where(np.isin(mesh.nodes.scoping.ids, scoping.ids))[0]
254+
# grid_points = [mesh.grid.points[node_index] for node_index in node_indexes]
255+
scoping_mesh = mesh.grid.extract_points(ind=node_indexes, include_cells=False)
256+
# If the scoping is elemental, extract their edges and use active scalars to color them
257+
if scoping.location == locations.elemental:
258+
element_indexes = np.where(np.isin(mesh.elements.scoping.ids, scoping.ids))[0]
259+
scoping_mesh = mesh.grid.extract_cells(ind=element_indexes)
260+
261+
# If the scoping is faces, extract their edges and use active scalars to color them
262+
if scoping.location == locations.faces:
263+
raise NotImplementedError("Cannot plot a face scoping.")
264+
265+
# Filter kwargs
266+
kwargs_in = _sort_supported_kwargs(bound_method=self._plotter.add_mesh, **kwargs)
267+
self._plotter.add_mesh(mesh=scoping_mesh, **kwargs_in)
268+
237269
def add_field(
238270
self,
239271
field,
@@ -689,6 +721,55 @@ def add_field(
689721
**kwargs,
690722
)
691723

724+
def add_scoping(
725+
self,
726+
scoping: dpf.core.Scoping,
727+
mesh: dpf.core.MeshedRegion,
728+
show_mesh: bool = False,
729+
**kwargs,
730+
):
731+
"""Add a scoping to the plotter.
732+
733+
A mesh is required to translate the scoping into entities to plot.
734+
Tou can plot the mesh along with the scoping entities using ``show_mesh``.
735+
736+
Parameters
737+
----------
738+
scoping:
739+
Scoping with a mesh-based location and IDs of entities to plot.
740+
mesh:
741+
``MeshedRegion`` to plot the field on.
742+
show_mesh:
743+
Whether to show the mesh along with the scoping entities.
744+
**kwargs : optional
745+
Additional keyword arguments for the plotter. More information
746+
are available at :func:`pyvista.plot`.
747+
748+
Examples
749+
--------
750+
>>> from ansys.dpf import core as dpf
751+
>>> from ansys.dpf.core import examples
752+
>>> model = dpf.Model(examples.download_cfx_mixing_elbow())
753+
>>> mesh = model.metadata.meshed_region
754+
>>> node_scoping = dpf.Scoping(
755+
... location=dpf.locations.nodal,
756+
... ids=mesh.nodes.scoping.ids[0:100]
757+
...)
758+
>>> element_scoping = dpf.Scoping(
759+
... location=dpf.locations.elemental,
760+
... ids=mesh.elements.scoping.ids[0:100]
761+
...)
762+
>>> from ansys.dpf.core.plotter import DpfPlotter
763+
>>> plt = DpfPlotter()
764+
>>> plt.add_scoping(node_scoping, mesh, show_mesh=True, color="red")
765+
>>> plt.add_scoping(element_scoping, mesh, color="green")
766+
>>> plt.show_figure()
767+
768+
"""
769+
self._internal_plotter.add_scoping(
770+
scoping=scoping, mesh=mesh, show_mesh=show_mesh, **kwargs
771+
)
772+
692773
def show_figure(self, **kwargs):
693774
"""Plot the figure built by the plotter object.
694775

src/ansys/dpf/core/scoping.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,48 @@ def as_local_scoping(self):
491491
""" # noqa: E501
492492
return _LocalScoping(self)
493493

494+
def plot(self, mesh, show_mesh: bool = False, **kwargs):
495+
"""Plot the entities of the mesh corresponding to the scoping.
496+
497+
Parameters
498+
----------
499+
mesh:
500+
Mesh to use to translate the scoping into mesh entities.
501+
show_mesh:
502+
Whether to also show the mesh with low opacity.
503+
**kwargs : optional
504+
Additional keyword arguments for the plotter. More information
505+
are available at :func:`pyvista.plot`.
506+
507+
Returns
508+
-------
509+
(cpos, image):
510+
Returns what the pyvista.show() method returns based on arguments.
511+
512+
Examples
513+
--------
514+
>>> from ansys.dpf import core as dpf
515+
>>> from ansys.dpf.core import examples
516+
>>> model = dpf.Model(examples.download_cfx_mixing_elbow())
517+
>>> mesh = model.metadata.meshed_region
518+
>>> node_scoping = dpf.Scoping(
519+
... location=dpf.locations.nodal,
520+
... ids=mesh.nodes.scoping.ids[0:100]
521+
...)
522+
>>> node_scoping.plot(mesh=mesh, color="red")
523+
>>> element_scoping = dpf.Scoping(
524+
... location=dpf.locations.elemental,
525+
... ids=mesh.elements.scoping.ids[0:100]
526+
...)
527+
>>> element_scoping.plot(mesh=mesh, color="green")
528+
529+
"""
530+
from ansys.dpf.core.plotter import DpfPlotter
531+
532+
plt = DpfPlotter(**kwargs)
533+
plt.add_scoping(scoping=self, mesh=mesh, show_mesh=show_mesh, **kwargs)
534+
return plt.show_figure(**kwargs)
535+
494536

495537
class _LocalScoping(Scoping):
496538
"""Caches the internal data of the scoping so that it can be modified locally.

0 commit comments

Comments
 (0)