Skip to content

Commit c8cdf67

Browse files
feat: improving performance of save_selection (#3697)
* feat: improving performance of save_selection * chore: adding changelog file 3697.miscellaneous.md [dependabot-skip] * refactor: remove unnecesary calls. * fix: muting commands to hide ComponentNoData error --------- Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 35c567a commit c8cdf67

File tree

3 files changed

+145
-15
lines changed

3 files changed

+145
-15
lines changed

doc/changelog.d/3697.miscellaneous.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
feat: improving performance of save_selection

src/ansys/mapdl/core/mapdl_core.py

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@
103103
r"(\*\*\* ERROR \*\*\*).*[\r\n]+.*The distributed memory parallel solution does not support KRYLOV method",
104104
]
105105

106+
_TMP_COMP = {
107+
"KP": "cmp_kp",
108+
"LINE": "cmp_line",
109+
"AREA": "cmp_area",
110+
"VOLU": "cmp_volu",
111+
"NODE": "cmp_node",
112+
"ELEM": "cmp_elem",
113+
}
114+
115+
ENTITIES_TO_SELECTION_MAPPING = {
116+
"KP": "ksel",
117+
"LINE": "lsel",
118+
"AREA": "asel",
119+
"VOLU": "vsel",
120+
"NODE": "nsel",
121+
"ELEM": "esel",
122+
}
123+
106124
# test for png file
107125
PNG_IS_WRITTEN_TO_FILE = re.compile(
108126
"WRITTEN TO FILE"
@@ -1434,32 +1452,41 @@ def __enter__(self):
14341452

14351453
# Storing components
14361454
selection = {
1437-
"cmsel": mapdl.components.names,
1438-
# "components_type": mapdl.components.types,
1439-
"nsel": mapdl.mesh.nnum,
1440-
"esel": mapdl.mesh.enum,
1441-
"ksel": mapdl.geometry.knum,
1442-
"lsel": mapdl.geometry.lnum,
1443-
"asel": mapdl.geometry.anum,
1444-
"vsel": mapdl.geometry.vnum,
1455+
"cmsel": mapdl.components._comp,
14451456
}
1457+
id_ = random_string(5)
1458+
for each_type, each_name in _TMP_COMP.items():
1459+
each_name = f"__{each_name}{id_}__"
1460+
selection[each_type] = each_name
1461+
mapdl.cm(
1462+
each_name, each_type, mute=True
1463+
) # to hide ComponentNoData error
14461464

14471465
self.selection.append(selection)
14481466

14491467
def __exit__(self, *args):
14501468
self._parent()._log.debug("Exiting saving selection context")
1451-
selection = self.selection.pop()
1469+
14521470
mapdl = self._parent()
1471+
mapdl.allsel()
1472+
mapdl.cmsel("None")
14531473

1474+
selection = self.selection.pop()
14541475
cmps = selection.pop("cmsel")
14551476

14561477
if cmps:
1457-
mapdl.components.select(cmps)
1478+
for each_name, each_value in cmps.items():
1479+
mapdl.cmsel("a", each_name, each_value, mute=True)
1480+
1481+
for each_type, each_name in selection.items():
1482+
mapdl.cmsel("a", each_name, each_type, mute=True)
1483+
1484+
selfun = getattr(
1485+
mapdl, ENTITIES_TO_SELECTION_MAPPING[each_type.upper()]
1486+
)
1487+
selfun("s", vmin=each_name, mute=True)
14581488

1459-
for select_cmd, ids in selection.items():
1460-
if ids.size > 0:
1461-
func = getattr(mapdl, select_cmd)
1462-
func(vmin=ids)
1489+
mapdl.cmdele(each_name, mute=True)
14631490

14641491
class _chain_commands:
14651492
"""Store MAPDL commands and send one chained command."""

tests/test_mapdl.py

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,7 @@ def test_components_selection_keep_between_plots(mapdl, cube_solve):
21232123
assert "mycm" in mapdl.components
21242124

21252125

2126-
def test_saving_selection_context(mapdl, cube_solve):
2126+
def test_save_selection_1(mapdl, cube_solve):
21272127
mapdl.allsel()
21282128

21292129
for i in range(1, 4):
@@ -2221,6 +2221,108 @@ def test_saving_selection_context(mapdl, cube_solve):
22212221
assert "nod_selection_4" not in mapdl.components
22222222

22232223

2224+
def test_save_selection_2(mapdl, cleared, make_block):
2225+
from ansys.mapdl.core.mapdl_core import _TMP_COMP
2226+
2227+
n1 = 1
2228+
mapdl.nsel(vmin=n1)
2229+
assert n1 in mapdl.mesh.nnum
2230+
mapdl.cm("nodes_cm", "NODE")
2231+
assert "nodes_cm" in mapdl.components
2232+
assert n1 in mapdl.components["nodes_cm"].items
2233+
assert "NODE" == mapdl.components["nodes_cm"].type
2234+
2235+
e1 = 1
2236+
mapdl.esel(vmin=e1)
2237+
assert e1 in mapdl.mesh.enum
2238+
mapdl.cm("elem_cm", "ELEM")
2239+
assert "elem_cm" in mapdl.components
2240+
assert e1 in mapdl.components["elem_cm"].items
2241+
assert "ELEM" == mapdl.components["elem_cm"].type
2242+
2243+
kp1 = 1
2244+
mapdl.ksel(vmin=kp1)
2245+
assert kp1 in mapdl.geometry.knum
2246+
mapdl.cm("kp_cm", "kp")
2247+
assert "kp_cm" in mapdl.components
2248+
assert kp1 in mapdl.components["kp_cm"].items
2249+
assert "KP" == mapdl.components["kp_cm"].type
2250+
2251+
l1 = 1
2252+
mapdl.lsel(vmin=l1)
2253+
assert l1 in mapdl.geometry.lnum
2254+
mapdl.cm("line_cm", "line")
2255+
assert "line_cm" in mapdl.components
2256+
assert l1 in mapdl.components["line_cm"].items
2257+
assert "LINE" == mapdl.components["line_cm"].type
2258+
2259+
a1 = 1
2260+
mapdl.asel(vmin=a1)
2261+
assert a1 in mapdl.geometry.anum
2262+
mapdl.cm("area_cm", "area")
2263+
assert "area_cm" in mapdl.components
2264+
assert a1 in mapdl.components["area_cm"].items
2265+
assert "AREA" == mapdl.components["area_cm"].type
2266+
2267+
# Assert we have properly set the components
2268+
assert {
2269+
"AREA_CM": "AREA",
2270+
"ELEM_CM": "ELEM",
2271+
"KP_CM": "KP",
2272+
"LINE_CM": "LINE",
2273+
"NODES_CM": "NODE",
2274+
} == mapdl.components._comp
2275+
2276+
# additional changes to the selections
2277+
kpoints = mapdl.ksel("u", vmin=1)
2278+
lines = mapdl.lsel("a", vmin=[2, 5, 6])
2279+
areas = mapdl.asel("a", vmin=2)
2280+
nodes = mapdl.nsel("S", vmin=[4, 5])
2281+
elem = mapdl.esel("s", vmin=[1, 3])
2282+
2283+
# checking all the elements are correct
2284+
assert np.allclose(kpoints, mapdl.geometry.knum)
2285+
assert np.allclose(lines, mapdl.geometry.lnum)
2286+
assert np.allclose(areas, mapdl.geometry.anum)
2287+
assert np.allclose(nodes, mapdl.mesh.nnum)
2288+
assert np.allclose(elem, mapdl.mesh.enum)
2289+
2290+
## storing... __enter__
2291+
comp_selection = mapdl.components._comp
2292+
2293+
print("Starting...")
2294+
with mapdl.save_selection:
2295+
2296+
# do something
2297+
mapdl.allsel()
2298+
mapdl.cmsel("NONE")
2299+
mapdl.asel("NONE")
2300+
mapdl.nsel("s", vmin=[1, 2, 8, 9])
2301+
mapdl.allsel()
2302+
mapdl.vsel("none")
2303+
mapdl.lsel("a", vmin=[9])
2304+
mapdl.vsel("all")
2305+
mapdl.ksel("none")
2306+
2307+
# checks
2308+
assert np.allclose(kpoints, mapdl.geometry.knum)
2309+
assert np.allclose(lines, mapdl.geometry.lnum)
2310+
assert np.allclose(areas, mapdl.geometry.anum)
2311+
assert np.allclose(nodes, mapdl.mesh.nnum)
2312+
assert np.allclose(elem, mapdl.mesh.enum)
2313+
2314+
for each_key, each_value in comp_selection.items():
2315+
assert (
2316+
each_key in mapdl.components
2317+
), f"Component '{each_key}' is not defined/selected"
2318+
assert (
2319+
each_value == mapdl.components[each_key].type
2320+
), f"Component '{each_key}' type is not correct"
2321+
2322+
for each_tmp in _TMP_COMP.values():
2323+
assert each_tmp not in mapdl.components
2324+
2325+
22242326
def test_inquire_invalid(mapdl, cleared):
22252327
with pytest.raises(ValueError, match="Arguments of this method have changed"):
22262328
mapdl.inquire("directory")

0 commit comments

Comments
 (0)