Skip to content

Commit 351b232

Browse files
authored
fix: support NumPy array views to set data of Field (#2630)
1 parent c52ee15 commit 351b232

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/ansys/dpf/core/field_base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,8 @@ def data_as_list(self):
504504

505505
@data.setter
506506
def data(self, data):
507+
if isinstance(data, (np.ndarray, np.generic)) and data.base is not None:
508+
data = data.copy()
507509
self._set_data(data)
508510

509511
@abstractmethod

tests/test_field.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,22 @@ def test_append_data_elemental_nodal_field(allkindofcomplexity):
426426
assert np.allclose(f_new.get_entity_data(i), f.get_entity_data(i))
427427

428428

429+
def test_distinct_fields_created_from_views():
430+
points = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
431+
points_transpose = points.T
432+
433+
coordinates_field_one = dpf.core.fields_factory.create_3d_vector_field(len(points))
434+
coordinates_field_one.data = points
435+
coordinates_field_one.scoping.ids = range(1, len(coordinates_field_one.data) + 1)
436+
437+
coordinates_field_two = dpf.core.fields_factory.create_3d_vector_field(len(points_transpose))
438+
coordinates_field_two.data = points_transpose
439+
coordinates_field_two.scoping.ids = range(1, len(coordinates_field_two.data))
440+
441+
check_data_equality = (coordinates_field_one.data == coordinates_field_two.data).all()
442+
assert check_data_equality == False
443+
444+
429445
def test_str_field(stress_field):
430446
assert "Location" in str(stress_field)
431447
assert "ElementalNodal" in str(stress_field)

0 commit comments

Comments
 (0)