Skip to content

Commit

Permalink
Fix mesh_scoping_factory.named_selection_scoping server argument (#2005)
Browse files Browse the repository at this point in the history
* Handle the server argument for mesh_scoping_factory.py/named_selection_scoping.

* Add test

* Fix error

* Fix deepcopy call

* Fix QA
  • Loading branch information
PProfizi authored Jan 14, 2025
1 parent c2e57d6 commit e4f9aff
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/ansys/dpf/core/mesh_scoping_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,4 @@ def named_selection_scoping(
A scoping containing the IDs of the entities in the named selection.
The location depends on the type of entities targeted by the named selection.
"""
return model.metadata.named_selection(named_selection_name)
return model.metadata.named_selection(named_selection=named_selection_name, server=server)
33 changes: 26 additions & 7 deletions src/ansys/dpf/core/meshed_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@

"""MeshedRegion."""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING: # pragma: nocover
from ansys.dpf.core.server_types import AnyServerType
from ansys.dpf.core.scoping import Scoping

import traceback
import warnings

Expand Down Expand Up @@ -378,36 +386,47 @@ def _get_available_named_selections(self):
named_selections.append(self._api.meshed_region_get_named_selection_name(self, index))
return named_selections

def named_selection(self, named_selection):
"""
Scoping containing the list of nodes or elements in the named selection.
def named_selection(
self,
named_selection: str,
server: AnyServerType = None,
) -> Scoping:
"""Scoping containing the list of nodes or elements in the named selection.
Parameters
----------
named_selection : str
named_selection:
Name of the named selection.
server:
Server on which to create the scoping if different from the server of the model.
Returns
-------
named_selection : Scoping
named_selection:
A scoping containing the IDs of the entities in the named selection.
The location depends on the type of entities targeted by the named selection.
"""
if server_meet_version("2.1", self._server):
out = self._api.meshed_region_get_named_selection_scoping(self, named_selection)
return scoping.Scoping(scoping=out, server=self._server)
out_scoping = scoping.Scoping(scoping=out, server=self._server)
else:
if hasattr(self, "_stream_provider"):
from ansys.dpf.core.dpf_operator import Operator

op = Operator("scoping_provider_by_ns", server=self._server)
op.connect(1, named_selection)
op.connect(3, self._stream_provider, 0)
return op.get_output(0, types.scoping)
out_scoping = op.get_output(0, types.scoping)
else:
raise Exception(
"Getting a named selection from a meshed region is "
"only implemented for meshed region created from a "
"model for server version 2.0. Please update your server."
)
if server:
# Copy the scoping to another server
out_scoping = out_scoping.deep_copy(server=server)
return out_scoping

@version_requires("3.0")
def set_named_selection_scoping(self, named_selection_name, scoping):
Expand Down
22 changes: 17 additions & 5 deletions src/ansys/dpf/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
"""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING: # pragma: nocover
from ansys.dpf.core.server_types import AnyServerType
from ansys.dpf.core.scoping import Scoping

from ansys import dpf
from ansys.dpf.core import Operator
from ansys.dpf.core.common import types
Expand Down Expand Up @@ -587,19 +595,23 @@ def available_named_selections(self):
"""
return self.meshed_region.available_named_selections

def named_selection(self, named_selection):
def named_selection(self, named_selection: str, server: AnyServerType = None) -> Scoping:
"""Scoping containing the list of nodes or elements in the named selection.
Parameters
----------
named_selection : str
name of the named selection
named_selection:
Name of the named selection.
server:
Server on which to create the scoping if different from the server of the model.
Returns
-------
named_selection : :class:`ansys.dpf.core.scoping.Scoping`
named_selection:
A scoping containing the IDs of the entities in the named selection.
The location depends on the type of entities targeted by the named selection.
"""
return self.meshed_region.named_selection(named_selection)
return self.meshed_region.named_selection(named_selection=named_selection, server=server)

def _build_connector(self):
return DataSourcesOrStreamsConnector(self)
11 changes: 11 additions & 0 deletions tests/test_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
from ansys.dpf.core import fields_container_factory
from ansys.dpf.core import fields_factory
from ansys.dpf.core import mesh_scoping_factory
from ansys.dpf.core import server
from ansys.dpf.core import server_factory
from ansys.dpf.core import time_freq_scoping_factory
from ansys.dpf.core.common import locations

Expand Down Expand Up @@ -297,3 +299,12 @@ def test_named_selection_scoping(model_with_ns):
scop = mesh_scoping_factory.named_selection_scoping("SELECTION", model)
assert scop is not None
assert len(scop.ids) != 0


def test_named_selection_scoping_with_deepcopy(model_with_ns):
model = Model(model_with_ns)
server_2 = server.start_local_server(config=server_factory.AvailableServerConfigs.GrpcServer)
scop = mesh_scoping_factory.named_selection_scoping("SELECTION", model, server_2)
assert scop is not None
assert len(scop.ids) != 0
assert scop._server == server_2

0 comments on commit e4f9aff

Please sign in to comment.