Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Scoping.ids setter for int64 arrays #2040

Merged
merged 8 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ansys/dpf/core/scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ def _set_location(self, loc=locations.nodal):
def _set_ids(self, ids: IdVectorType):
"""Set the ids.

Scoping IDs are stored as int32.
Converts automatically int64 Numpy arrays to int32.

Parameters
----------
ids:
Expand All @@ -191,6 +194,9 @@ def _set_ids(self, ids: IdVectorType):
"""
if isinstance(ids, range):
ids = list(ids)
if isinstance(ids, np.ndarray):
if ids.dtype == np.int64:
rafacanton marked this conversation as resolved.
Show resolved Hide resolved
PProfizi marked this conversation as resolved.
Show resolved Hide resolved
ids = ids.astype(np.int32)
if isinstance(self._server, server_types.InProcessServer):
self._api.scoping_resize(self, len(ids))
ids_ptr = self._api.scoping_get_ids(self, len(ids))
Expand Down
10 changes: 10 additions & 0 deletions tests/test_scoping.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def test_set_get_ids_scoping(server_type):
assert np.allclose(scop.ids, ids)


def test_set_get_ids_scoping_int64_array(server_type):
# Numpy 2 switches default int precision from 32 to 64 on Windows
# This tests verifies we convert any array of int64 to int32.
scop = Scoping(server=server_type)
ids_list = [1, 2, 3, 4]
ids = np.array(ids_list, dtype=np.int64)
scop.ids = ids
assert np.allclose(scop.ids, ids_list)


def test_set_get_ids_scoping_range(server_type):
range_ids = range(1, 10)
scop = Scoping(
Expand Down
Loading