From 26f12438fe9b74916f54f75fa11ab8287ea5ba2a Mon Sep 17 00:00:00 2001 From: cbellot Date: Mon, 20 Jan 2025 18:20:38 +0100 Subject: [PATCH 1/6] Operator and workflow connect numpy array --- src/ansys/dpf/core/dpf_operator.py | 8 +++++--- src/ansys/dpf/core/misc.py | 10 ++++++++++ src/ansys/dpf/core/workflow.py | 9 ++++++--- tests/test_operator.py | 8 ++++++++ tests/test_workflow.py | 14 ++++++++++++++ 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/ansys/dpf/core/dpf_operator.py b/src/ansys/dpf/core/dpf_operator.py index ed5f69182ad..d8ba123a3c2 100644 --- a/src/ansys/dpf/core/dpf_operator.py +++ b/src/ansys/dpf/core/dpf_operator.py @@ -26,6 +26,7 @@ import os import traceback import warnings +import numpy from enum import Enum from ansys.dpf.core.check_version import ( @@ -38,6 +39,7 @@ from ansys.dpf.core.inputs import Inputs from ansys.dpf.core.mapping_types import types from ansys.dpf.core.common import types_enum_to_types +from ansys.dpf.core.misc import get_array_length from ansys.dpf.core.outputs import Output, Outputs, _Outputs from ansys.dpf.core import server as server_module from ansys.dpf.core.operator_specification import Specification @@ -276,7 +278,7 @@ def connect(self, pin, inpt, pin_out=0): self._api.operator_connect_operator_output(self, pin, inpt, pin_out) elif isinstance(inpt, Output): self._api.operator_connect_operator_output(self, pin, inpt._operator, inpt._pin) - elif isinstance(inpt, list): + elif isinstance(inpt, (list, numpy.ndarray)): from ansys.dpf.core import collection if server_meet_version("3.0", self._server): @@ -284,9 +286,9 @@ def connect(self, pin, inpt, pin_out=0): self._api.operator_connect_collection_as_vector(self, pin, inpt) else: if all(isinstance(x, int) for x in inpt): - self._api.operator_connect_vector_int(self, pin, inpt, len(inpt)) + self._api.operator_connect_vector_int(self, pin, inpt, get_array_length(inpt)) else: - self._api.operator_connect_vector_double(self, pin, inpt, len(inpt)) + self._api.operator_connect_vector_double(self, pin, inpt, get_array_length(inpt)) elif isinstance(inpt, dict): from ansys.dpf.core import label_space diff --git a/src/ansys/dpf/core/misc.py b/src/ansys/dpf/core/misc.py index 13ccee9d620..e50261bd569 100644 --- a/src/ansys/dpf/core/misc.py +++ b/src/ansys/dpf/core/misc.py @@ -29,6 +29,10 @@ from pathlib import Path from pkgutil import iter_modules +from typing import Union + +import numpy + from ansys.dpf.core import errors from ansys.dpf.gate._version import __ansys_version__ from ansys.dpf.gate import load_api @@ -213,3 +217,9 @@ def is_pypim_configured(): ``True`` if the environment is setup to use the PIM API, ``False`` otherwise. """ return "ANSYS_PLATFORM_INSTANCEMANAGEMENT_CONFIG" in os.environ + + +def get_array_length(array:Union[list, numpy.ndarray]): + if isinstance(array, numpy.ndarray): + return array.size + return len(array) \ No newline at end of file diff --git a/src/ansys/dpf/core/workflow.py b/src/ansys/dpf/core/workflow.py index 2e9b8eaa357..2e44b06bf91 100644 --- a/src/ansys/dpf/core/workflow.py +++ b/src/ansys/dpf/core/workflow.py @@ -31,6 +31,8 @@ from enum import Enum from typing import Union +import numpy + from ansys import dpf from ansys.dpf.core import dpf_operator, inputs, outputs from ansys.dpf.core.check_version import ( @@ -39,6 +41,7 @@ server_meet_version_and_raise, ) from ansys.dpf.core import server as server_module +from ansys.dpf.core.misc import get_array_length from ansys.dpf.gate import ( workflow_abstract_api, workflow_grpcapi, @@ -214,7 +217,7 @@ def connect(self, pin_name, inpt, pin_out=0): self._api.work_flow_connect_operator_output(self, pin_name, inpt, pin_out) elif isinstance(inpt, dpf_operator.Output): self._api.work_flow_connect_operator_output(self, pin_name, inpt._operator, inpt._pin) - elif isinstance(inpt, list): + elif isinstance(inpt, (list, numpy.ndarray)): from ansys.dpf.core import collection if server_meet_version("3.0", self._server): @@ -222,9 +225,9 @@ def connect(self, pin_name, inpt, pin_out=0): self._api.work_flow_connect_collection_as_vector(self, pin_name, inpt) else: if all(isinstance(x, int) for x in inpt): - self._api.work_flow_connect_vector_int(self, pin_name, inpt, len(inpt)) + self._api.work_flow_connect_vector_int(self, pin_name, inpt, get_array_length(inpt)) else: - self._api.work_flow_connect_vector_double(self, pin_name, inpt, len(inpt)) + self._api.work_flow_connect_vector_double(self, pin_name, inpt, get_array_length(inpt)) elif isinstance(inpt, dict): from ansys.dpf.core import label_space diff --git a/tests/test_operator.py b/tests/test_operator.py index 124e988b503..a5308e4c5c7 100644 --- a/tests/test_operator.py +++ b/tests/test_operator.py @@ -27,6 +27,7 @@ import weakref from pathlib import Path +import numpy import numpy as np import pytest import copy @@ -92,6 +93,13 @@ def test_connect_list_operator(velocity_acceleration): fcOut = op.get_output(0, dpf.core.types.fields_container) assert fcOut.get_available_ids_for_label() == [1, 2] +def test_connect_array_operator(velocity_acceleration): + model = dpf.core.Model(velocity_acceleration) + op = model.operator("U") + op.connect(0, numpy.array([1, 2], numpy.int32)) + fcOut = op.get_output(0, dpf.core.types.fields_container) + assert fcOut.get_available_ids_for_label() == [1, 2] + def test_connect_list_operator_builtin(velocity_acceleration): model = dpf.core.Model(velocity_acceleration) diff --git a/tests/test_workflow.py b/tests/test_workflow.py index 505965f06ff..58c4d480a0d 100644 --- a/tests/test_workflow.py +++ b/tests/test_workflow.py @@ -22,6 +22,7 @@ from pathlib import Path +import numpy import numpy as np import pytest import platform @@ -137,6 +138,19 @@ def test_connect_list_workflow(velocity_acceleration, server_type): assert f_out.get_available_ids_for_label() == [1, 2] +def test_connect_array_workflow(velocity_acceleration, server_type): + wf = dpf.core.Workflow(server=server_type) + wf.progress_bar = False + model = dpf.core.Model(velocity_acceleration, server=server_type) + op = model.operator("U") + wf.add_operator(op) + wf.set_input_name("time_scoping", op, 0) + wf.set_output_name("field", op, 0) + wf.connect("time_scoping", numpy.array([1, 2], numpy.int32)) + f_out = wf.get_output("field", dpf.core.types.fields_container) + assert f_out.get_available_ids_for_label() == [1, 2] + + def test_connect_fieldscontainer_workflow(server_type): wf = dpf.core.Workflow(server=server_type) wf.progress_bar = False From a2ebfcf32e5ac24746a3a6c4d9fd509d7a39e59d Mon Sep 17 00:00:00 2001 From: cbellot Date: Tue, 21 Jan 2025 08:06:22 +0100 Subject: [PATCH 2/6] style --- src/ansys/dpf/core/dpf_operator.py | 4 +++- src/ansys/dpf/core/misc.py | 5 +++-- src/ansys/dpf/core/workflow.py | 8 ++++++-- tests/test_operator.py | 1 + 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ansys/dpf/core/dpf_operator.py b/src/ansys/dpf/core/dpf_operator.py index d8ba123a3c2..afcd30d566c 100644 --- a/src/ansys/dpf/core/dpf_operator.py +++ b/src/ansys/dpf/core/dpf_operator.py @@ -288,7 +288,9 @@ def connect(self, pin, inpt, pin_out=0): if all(isinstance(x, int) for x in inpt): self._api.operator_connect_vector_int(self, pin, inpt, get_array_length(inpt)) else: - self._api.operator_connect_vector_double(self, pin, inpt, get_array_length(inpt)) + self._api.operator_connect_vector_double( + self, pin, inpt, get_array_length(inpt) + ) elif isinstance(inpt, dict): from ansys.dpf.core import label_space diff --git a/src/ansys/dpf/core/misc.py b/src/ansys/dpf/core/misc.py index e50261bd569..5032107ff8e 100644 --- a/src/ansys/dpf/core/misc.py +++ b/src/ansys/dpf/core/misc.py @@ -219,7 +219,8 @@ def is_pypim_configured(): return "ANSYS_PLATFORM_INSTANCEMANAGEMENT_CONFIG" in os.environ -def get_array_length(array:Union[list, numpy.ndarray]): +def get_array_length(array: Union[list, numpy.ndarray]): + """Return the length of a flat array (size of numpy array or length of a list.""" if isinstance(array, numpy.ndarray): return array.size - return len(array) \ No newline at end of file + return len(array) diff --git a/src/ansys/dpf/core/workflow.py b/src/ansys/dpf/core/workflow.py index 2e44b06bf91..2efaeca2cd7 100644 --- a/src/ansys/dpf/core/workflow.py +++ b/src/ansys/dpf/core/workflow.py @@ -225,9 +225,13 @@ def connect(self, pin_name, inpt, pin_out=0): self._api.work_flow_connect_collection_as_vector(self, pin_name, inpt) else: if all(isinstance(x, int) for x in inpt): - self._api.work_flow_connect_vector_int(self, pin_name, inpt, get_array_length(inpt)) + self._api.work_flow_connect_vector_int( + self, pin_name, inpt, get_array_length(inpt) + ) else: - self._api.work_flow_connect_vector_double(self, pin_name, inpt, get_array_length(inpt)) + self._api.work_flow_connect_vector_double( + self, pin_name, inpt, get_array_length(inpt) + ) elif isinstance(inpt, dict): from ansys.dpf.core import label_space diff --git a/tests/test_operator.py b/tests/test_operator.py index a5308e4c5c7..381ec03a88f 100644 --- a/tests/test_operator.py +++ b/tests/test_operator.py @@ -93,6 +93,7 @@ def test_connect_list_operator(velocity_acceleration): fcOut = op.get_output(0, dpf.core.types.fields_container) assert fcOut.get_available_ids_for_label() == [1, 2] + def test_connect_array_operator(velocity_acceleration): model = dpf.core.Model(velocity_acceleration) op = model.operator("U") From b404250a81fe60ef12305aaa7e66e9908e41d158 Mon Sep 17 00:00:00 2001 From: Paul Profizi <100710998+PProfizi@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:51:04 +0100 Subject: [PATCH 3/6] Fix coverage for dpf_operator.py --- src/ansys/dpf/core/dpf_operator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ansys/dpf/core/dpf_operator.py b/src/ansys/dpf/core/dpf_operator.py index afcd30d566c..38295c8a04a 100644 --- a/src/ansys/dpf/core/dpf_operator.py +++ b/src/ansys/dpf/core/dpf_operator.py @@ -284,7 +284,8 @@ def connect(self, pin, inpt, pin_out=0): if server_meet_version("3.0", self._server): inpt = collection.CollectionBase.integral_collection(inpt, self._server) self._api.operator_connect_collection_as_vector(self, pin, inpt) - else: + else: # pragma: nocover + # Exclude DPF below 2022R1 from coverage if all(isinstance(x, int) for x in inpt): self._api.operator_connect_vector_int(self, pin, inpt, get_array_length(inpt)) else: From d9d331c8cd61a0780a26b72aed34a6b032379cff Mon Sep 17 00:00:00 2001 From: Paul Profizi <100710998+PProfizi@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:53:43 +0100 Subject: [PATCH 4/6] Remove code related to unsupported DPF versions in workflow.py Current client is not compatible with DPF<4.0 anyway --- src/ansys/dpf/core/workflow.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/ansys/dpf/core/workflow.py b/src/ansys/dpf/core/workflow.py index 2efaeca2cd7..3a012148b42 100644 --- a/src/ansys/dpf/core/workflow.py +++ b/src/ansys/dpf/core/workflow.py @@ -220,18 +220,8 @@ def connect(self, pin_name, inpt, pin_out=0): elif isinstance(inpt, (list, numpy.ndarray)): from ansys.dpf.core import collection - if server_meet_version("3.0", self._server): - inpt = collection.CollectionBase.integral_collection(inpt, self._server) - self._api.work_flow_connect_collection_as_vector(self, pin_name, inpt) - else: - if all(isinstance(x, int) for x in inpt): - self._api.work_flow_connect_vector_int( - self, pin_name, inpt, get_array_length(inpt) - ) - else: - self._api.work_flow_connect_vector_double( - self, pin_name, inpt, get_array_length(inpt) - ) + inpt = collection.CollectionBase.integral_collection(inpt, self._server) + self._api.work_flow_connect_collection_as_vector(self, pin_name, inpt) elif isinstance(inpt, dict): from ansys.dpf.core import label_space From 33319890a1a6bc9967d56765d91324b6da2e9cb2 Mon Sep 17 00:00:00 2001 From: Paul Profizi <100710998+PProfizi@users.noreply.github.com> Date: Tue, 21 Jan 2025 11:54:44 +0100 Subject: [PATCH 5/6] Remove code related to unsupported DPF versions in dpf_operator.py Current client does not support DPF<4.0 anyway --- src/ansys/dpf/core/dpf_operator.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/ansys/dpf/core/dpf_operator.py b/src/ansys/dpf/core/dpf_operator.py index 38295c8a04a..ee4f16d9d12 100644 --- a/src/ansys/dpf/core/dpf_operator.py +++ b/src/ansys/dpf/core/dpf_operator.py @@ -281,17 +281,8 @@ def connect(self, pin, inpt, pin_out=0): elif isinstance(inpt, (list, numpy.ndarray)): from ansys.dpf.core import collection - if server_meet_version("3.0", self._server): - inpt = collection.CollectionBase.integral_collection(inpt, self._server) - self._api.operator_connect_collection_as_vector(self, pin, inpt) - else: # pragma: nocover - # Exclude DPF below 2022R1 from coverage - if all(isinstance(x, int) for x in inpt): - self._api.operator_connect_vector_int(self, pin, inpt, get_array_length(inpt)) - else: - self._api.operator_connect_vector_double( - self, pin, inpt, get_array_length(inpt) - ) + inpt = collection.CollectionBase.integral_collection(inpt, self._server) + self._api.operator_connect_collection_as_vector(self, pin, inpt) elif isinstance(inpt, dict): from ansys.dpf.core import label_space From a68c7c74e65186f2045db6252d0dbae188090734 Mon Sep 17 00:00:00 2001 From: Paul Profizi <100710998+PProfizi@users.noreply.github.com> Date: Tue, 21 Jan 2025 16:51:56 +0100 Subject: [PATCH 6/6] Remove unused code --- src/ansys/dpf/core/dpf_operator.py | 1 - src/ansys/dpf/core/misc.py | 11 ----------- src/ansys/dpf/core/workflow.py | 1 - 3 files changed, 13 deletions(-) diff --git a/src/ansys/dpf/core/dpf_operator.py b/src/ansys/dpf/core/dpf_operator.py index ee4f16d9d12..12af7f2cdd2 100644 --- a/src/ansys/dpf/core/dpf_operator.py +++ b/src/ansys/dpf/core/dpf_operator.py @@ -39,7 +39,6 @@ from ansys.dpf.core.inputs import Inputs from ansys.dpf.core.mapping_types import types from ansys.dpf.core.common import types_enum_to_types -from ansys.dpf.core.misc import get_array_length from ansys.dpf.core.outputs import Output, Outputs, _Outputs from ansys.dpf.core import server as server_module from ansys.dpf.core.operator_specification import Specification diff --git a/src/ansys/dpf/core/misc.py b/src/ansys/dpf/core/misc.py index 5032107ff8e..13ccee9d620 100644 --- a/src/ansys/dpf/core/misc.py +++ b/src/ansys/dpf/core/misc.py @@ -29,10 +29,6 @@ from pathlib import Path from pkgutil import iter_modules -from typing import Union - -import numpy - from ansys.dpf.core import errors from ansys.dpf.gate._version import __ansys_version__ from ansys.dpf.gate import load_api @@ -217,10 +213,3 @@ def is_pypim_configured(): ``True`` if the environment is setup to use the PIM API, ``False`` otherwise. """ return "ANSYS_PLATFORM_INSTANCEMANAGEMENT_CONFIG" in os.environ - - -def get_array_length(array: Union[list, numpy.ndarray]): - """Return the length of a flat array (size of numpy array or length of a list.""" - if isinstance(array, numpy.ndarray): - return array.size - return len(array) diff --git a/src/ansys/dpf/core/workflow.py b/src/ansys/dpf/core/workflow.py index 3a012148b42..7faeb2958ab 100644 --- a/src/ansys/dpf/core/workflow.py +++ b/src/ansys/dpf/core/workflow.py @@ -41,7 +41,6 @@ server_meet_version_and_raise, ) from ansys.dpf.core import server as server_module -from ansys.dpf.core.misc import get_array_length from ansys.dpf.gate import ( workflow_abstract_api, workflow_grpcapi,