Skip to content

Fix mesh_scoping_factory.named_selection_scoping server argument #2005

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

Merged
merged 5 commits into from
Jan 14, 2025
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
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 @@ -106,4 +106,4 @@ def named_selection_scoping(named_selection_name, model, server=None):
-------
scoping : Scoping
"""
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 @@
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)

Check warning on line 419 in src/ansys/dpf/core/meshed_region.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/dpf/core/meshed_region.py#L419

Added line #L419 was not covered by tests
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
Loading