Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 14 additions & 4 deletions src/ansys/dpf/core/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,25 @@ def connect(self, inpt):
self._operator()._find_outputs_corresponding_pins(
self._python_expected_types, inpt, self._pin, corresponding_pins, input_type_name
)
# We can have a single output with multiple types compatible with this input
# if it accepts several of these types so we need to check for unique combinations
corresponding_pins = list(set(corresponding_pins))
if len(corresponding_pins) > 1:
err_str = "Pin connection is ambiguous, specify the input to connect to with:\n"
op_name = (
self._operator().specification.properties["scripting_name"]
if "scripting_name" in self._operator().specification.properties
else self._operator().name
)
err_str = f"Operator {op_name}:\n"
err_str += "Pin connection is ambiguous, specify the input to connect to with:\n"
inpt_name = inpt._operator.name
for pin in corresponding_pins:
err_str += (
" - operator.inputs."
f" - {op_name}.inputs."
+ self._spec.name
+ "(out_op."
+ f"({inpt_name}."
+ inpt._dict_outputs[pin[1]].name
+ ")"
+ ")\n"
)
err_str += "Connecting to first input in the list.\n"
warnings.warn(message=err_str)
Expand Down
29 changes: 29 additions & 0 deletions tests/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from pathlib import Path
import shutil
import types
import warnings
import weakref

import numpy
Expand Down Expand Up @@ -576,6 +577,34 @@ def test_inputs_outputs_scopings_container(allkindofcomplexity):
assert len(fc) == 4


def test_connection_to_input_is_ambiguous():
field = dpf.core.fields_factory.field_from_array(arr=[1.0, 2.0, 3.0])
field.scoping = dpf.core.mesh_scoping_factory.nodal_scoping(node_ids=[1, 2, 3])
min_max_op_1 = dpf.core.operators.min_max.min_max(field=field)
with pytest.warns(match="Pin connection is ambiguous"):
dpf.core.operators.min_max.min_max(field=min_max_op_1)


@pytest.mark.skipif(
not conftest.SERVERS_VERSION_GREATER_THAN_OR_EQUAL_TO_5_0,
reason="extract_scoping unavailable for server version < 5.0",
)
def test_connection_to_input_is_not_ambiguous():
# Ensures that connecting an operator with a single output with multiple types all compatible
# does not raise an ambiguity warning.
# Here extract_scoping has only one output of type either Scoping or ScopingsContainer
# This output was reported twice and raised an ambiguity warning despite both types being
# compatible with the input
# This behavior is now fixed and enforced by this test
field = dpf.core.fields_factory.field_from_array(arr=[1.0, 2.0, 3.0])
field.scoping = dpf.core.mesh_scoping_factory.nodal_scoping(node_ids=[1, 2, 3])
scop = dpf.core.operators.utility.extract_scoping(field_or_fields_container=field)
stress = dpf.core.operators.result.stress()
with warnings.catch_warnings():
warnings.filterwarnings(action="error", category=UserWarning, message="Operator stress:")
stress.inputs.mesh_scoping.connect(scop)


def test_inputs_outputs_meshes_container(allkindofcomplexity):
data_sources = dpf.core.DataSources(allkindofcomplexity)
model = dpf.core.Model(data_sources)
Expand Down