Skip to content

Commit e4f9aff

Browse files
authored
Fix mesh_scoping_factory.named_selection_scoping server argument (#2005)
* Handle the server argument for mesh_scoping_factory.py/named_selection_scoping. * Add test * Fix error * Fix deepcopy call * Fix QA
1 parent c2e57d6 commit e4f9aff

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

src/ansys/dpf/core/mesh_scoping_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@ def named_selection_scoping(
120120
A scoping containing the IDs of the entities in the named selection.
121121
The location depends on the type of entities targeted by the named selection.
122122
"""
123-
return model.metadata.named_selection(named_selection_name)
123+
return model.metadata.named_selection(named_selection=named_selection_name, server=server)

src/ansys/dpf/core/meshed_region.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222

2323
"""MeshedRegion."""
2424

25+
from __future__ import annotations
26+
27+
from typing import TYPE_CHECKING
28+
29+
if TYPE_CHECKING: # pragma: nocover
30+
from ansys.dpf.core.server_types import AnyServerType
31+
from ansys.dpf.core.scoping import Scoping
32+
2533
import traceback
2634
import warnings
2735

@@ -378,36 +386,47 @@ def _get_available_named_selections(self):
378386
named_selections.append(self._api.meshed_region_get_named_selection_name(self, index))
379387
return named_selections
380388

381-
def named_selection(self, named_selection):
382-
"""
383-
Scoping containing the list of nodes or elements in the named selection.
389+
def named_selection(
390+
self,
391+
named_selection: str,
392+
server: AnyServerType = None,
393+
) -> Scoping:
394+
"""Scoping containing the list of nodes or elements in the named selection.
384395
385396
Parameters
386397
----------
387-
named_selection : str
398+
named_selection:
388399
Name of the named selection.
400+
server:
401+
Server on which to create the scoping if different from the server of the model.
389402
390403
Returns
391404
-------
392-
named_selection : Scoping
405+
named_selection:
406+
A scoping containing the IDs of the entities in the named selection.
407+
The location depends on the type of entities targeted by the named selection.
393408
"""
394409
if server_meet_version("2.1", self._server):
395410
out = self._api.meshed_region_get_named_selection_scoping(self, named_selection)
396-
return scoping.Scoping(scoping=out, server=self._server)
411+
out_scoping = scoping.Scoping(scoping=out, server=self._server)
397412
else:
398413
if hasattr(self, "_stream_provider"):
399414
from ansys.dpf.core.dpf_operator import Operator
400415

401416
op = Operator("scoping_provider_by_ns", server=self._server)
402417
op.connect(1, named_selection)
403418
op.connect(3, self._stream_provider, 0)
404-
return op.get_output(0, types.scoping)
419+
out_scoping = op.get_output(0, types.scoping)
405420
else:
406421
raise Exception(
407422
"Getting a named selection from a meshed region is "
408423
"only implemented for meshed region created from a "
409424
"model for server version 2.0. Please update your server."
410425
)
426+
if server:
427+
# Copy the scoping to another server
428+
out_scoping = out_scoping.deep_copy(server=server)
429+
return out_scoping
411430

412431
@version_requires("3.0")
413432
def set_named_selection_scoping(self, named_selection_name, scoping):

src/ansys/dpf/core/model.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727
2828
"""
2929

30+
from __future__ import annotations
31+
32+
from typing import TYPE_CHECKING
33+
34+
if TYPE_CHECKING: # pragma: nocover
35+
from ansys.dpf.core.server_types import AnyServerType
36+
from ansys.dpf.core.scoping import Scoping
37+
3038
from ansys import dpf
3139
from ansys.dpf.core import Operator
3240
from ansys.dpf.core.common import types
@@ -587,19 +595,23 @@ def available_named_selections(self):
587595
"""
588596
return self.meshed_region.available_named_selections
589597

590-
def named_selection(self, named_selection):
598+
def named_selection(self, named_selection: str, server: AnyServerType = None) -> Scoping:
591599
"""Scoping containing the list of nodes or elements in the named selection.
592600
593601
Parameters
594602
----------
595-
named_selection : str
596-
name of the named selection
603+
named_selection:
604+
Name of the named selection.
605+
server:
606+
Server on which to create the scoping if different from the server of the model.
597607
598608
Returns
599609
-------
600-
named_selection : :class:`ansys.dpf.core.scoping.Scoping`
610+
named_selection:
611+
A scoping containing the IDs of the entities in the named selection.
612+
The location depends on the type of entities targeted by the named selection.
601613
"""
602-
return self.meshed_region.named_selection(named_selection)
614+
return self.meshed_region.named_selection(named_selection=named_selection, server=server)
603615

604616
def _build_connector(self):
605617
return DataSourcesOrStreamsConnector(self)

tests/test_factories.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
from ansys.dpf.core import fields_container_factory
2929
from ansys.dpf.core import fields_factory
3030
from ansys.dpf.core import mesh_scoping_factory
31+
from ansys.dpf.core import server
32+
from ansys.dpf.core import server_factory
3133
from ansys.dpf.core import time_freq_scoping_factory
3234
from ansys.dpf.core.common import locations
3335

@@ -297,3 +299,12 @@ def test_named_selection_scoping(model_with_ns):
297299
scop = mesh_scoping_factory.named_selection_scoping("SELECTION", model)
298300
assert scop is not None
299301
assert len(scop.ids) != 0
302+
303+
304+
def test_named_selection_scoping_with_deepcopy(model_with_ns):
305+
model = Model(model_with_ns)
306+
server_2 = server.start_local_server(config=server_factory.AvailableServerConfigs.GrpcServer)
307+
scop = mesh_scoping_factory.named_selection_scoping("SELECTION", model, server_2)
308+
assert scop is not None
309+
assert len(scop.ids) != 0
310+
assert scop._server == server_2

0 commit comments

Comments
 (0)