Skip to content

Commit 04874b8

Browse files
PProfiziansys-akarcher
authored andcommitted
fix(plotter): merge fields of fields_container before plot (#2414)
1 parent f83797b commit 04874b8

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/ansys/dpf/core/fields_container.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,12 @@ def plot(self, label_space: dict = None, **kwargs):
547547
if label_space is None:
548548
label_space = {}
549549
fields = self.get_fields(label_space=label_space)
550-
for f in fields:
551-
plt.add_field(field=f, **kwargs)
550+
# Fields with same support will override each other so we first merge them
551+
merge_op = dpf.core.operators.utility.merge_fields()
552+
for i, f in enumerate(fields):
553+
merge_op.connect(i, f)
554+
merged_field = merge_op.eval()
555+
plt.add_field(field=merged_field, **kwargs)
552556
return plt.show_figure(**kwargs)
553557

554558
def animate(

tests/test_plotter.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,39 @@ def test_fields_container_plot(allkindofcomplexity):
168168
disp_fc.plot()
169169

170170

171+
@pytest.mark.skipif(not HAS_PYVISTA, reason="Please install pyvista")
172+
def test_fields_container_plot_same_mesh(multishells):
173+
import numpy as np
174+
175+
fc = core.FieldsContainer()
176+
f1 = core.fields_factory.create_scalar_field(num_entities=1, location=core.locations.elemental)
177+
f1.append(data=[2.0], scopingid=1)
178+
f2 = core.fields_factory.create_scalar_field(num_entities=1, location=core.locations.elemental)
179+
f2.append(data=[4.0], scopingid=2)
180+
fc.add_label(label="id", default_value=0)
181+
fc.add_field({"id": 1}, f1)
182+
fc.add_field({"id": 2}, f2)
183+
184+
mesh = core.meshed_region.MeshedRegion(num_nodes=6, num_elements=2)
185+
arr = np.array(
186+
[
187+
[0.0, 0.0, 0.0],
188+
[1.0, 0.0, 0.0],
189+
[1.0, 1.0, 0.0],
190+
[0.0, 1.0, 0.0],
191+
[0.0, 2.0, 0.0],
192+
[1.0, 2.0, 0.0],
193+
]
194+
)
195+
coord = core.field_from_array(arr)
196+
mesh.set_coordinates_field(coordinates_field=coord)
197+
mesh.elements.add_shell_element(id=1, connectivity=[0, 1, 2, 3])
198+
mesh.elements.add_shell_element(id=2, connectivity=[2, 3, 4, 5])
199+
f1.meshed_region = mesh
200+
f2.meshed_region = mesh
201+
fc.plot()
202+
203+
171204
@pytest.mark.skipif(not HAS_PYVISTA, reason="Please install pyvista")
172205
def test_field_elemental_plot(allkindofcomplexity):
173206
model = Model(allkindofcomplexity)

0 commit comments

Comments
 (0)