Skip to content

Commit

Permalink
Handle connection of Enum to operators (#2094)
Browse files Browse the repository at this point in the history
* Use Enum.value in Input.connect() and Inputs.connect()

* Add test_operator_change_shell_layers_connect_enum
  • Loading branch information
PProfizi authored Feb 18, 2025
1 parent 663d9fa commit 1b0421e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/ansys/dpf/core/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

"""Inputs."""

from enum import Enum
from textwrap import wrap
import warnings
import weakref
Expand Down Expand Up @@ -73,7 +74,7 @@ def connect(self, inpt):
Parameters
----------
inpt : str, int, double, Field, FieldsContainer, Scoping, DataSources, MeshedRegion,
inpt : str, int, double, Field, FieldsContainer, Scoping, DataSources, MeshedRegion, Enum,
Output, Outputs, Operator, os.PathLike
Input of the operator.
Expand All @@ -100,6 +101,8 @@ def connect(self, inpt):
inpt = inpt.ID
else: # Custom UnitSystem
inpt = inpt.unit_names
elif isinstance(inpt, Enum):
inpt = inpt.value

input_type_name = type(inpt).__name__
if not (input_type_name in self._python_expected_types or ["Outputs", "Output", "Any"]):
Expand Down Expand Up @@ -226,7 +229,7 @@ def connect(self, inpt):
Parameters
----------
inpt : str, int, double, bool, list[int], list[float], Field, FieldsContainer, Scoping,
inpt : str, int, double, bool, list[int], list[float], Field, FieldsContainer, Scoping, Enum,
ScopingsContainer, MeshedRegion, MeshesContainer, DataSources, CyclicSupport, Outputs, os.PathLike # noqa: E501
Input of the operator.
Expand All @@ -250,6 +253,8 @@ def connect(self, inpt):
inpt = inpt.metadata.data_sources
elif isinstance(inpt, Path):
inpt = str(inpt)
elif isinstance(inpt, Enum):
inpt = inpt.value

input_type_name = type(inpt).__name__
for input_pin in self._inputs:
Expand Down
53 changes: 53 additions & 0 deletions tests/operators/test_change_shell_layers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright (C) 2020 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Tests the utility.change_shell_layers operator

import ansys.dpf.core as dpf
from ansys.dpf.core import examples


def test_operator_change_shell_layers_connect_enum(server_type):
model = dpf.Model(
examples.download_all_kinds_of_complexity_modal(server=server_type), server=server_type
)

stress = model.results.stress()
stress.inputs.requested_location.connect("Nodal")

# Test connection through signature
change_shell_layers = dpf.operators.utility.change_shell_layers(
fields_container=stress, e_shell_layer=dpf.common.shell_layers.bottom, server=server_type
)
_ = change_shell_layers.eval()

# Test connection through Input.connect
change_shell_layers = dpf.operators.utility.change_shell_layers(server=server_type)
change_shell_layers.inputs.fields_container.connect(stress)
change_shell_layers.inputs.e_shell_layer.connect(dpf.common.shell_layers.bottom)
_ = change_shell_layers.eval()

# Test connection through Inputs.connect
change_shell_layers = dpf.operators.utility.change_shell_layers(server=server_type)
change_shell_layers.inputs.connect(stress)
change_shell_layers.inputs.connect(dpf.common.shell_layers.bottom)
_ = change_shell_layers.eval()

0 comments on commit 1b0421e

Please sign in to comment.