Skip to content

Commit 9ff41ff

Browse files
akaszynskijgd10
andauthored
Use gRPC functionality for inline functions (#639)
* fix use gRPC functionality * refactor component_queries * Made integer bool and added return type Not sure why the parameter "integer" was a string in the default but treated as a bool in the code. So I've made it a bool. Additionally it didn't make sense for it to have a default value because there's an equal number of integers versus floats. It would be better to have to specify which is being requested whenever the method is called. * simplified param parsing and changed selection status docstring * altered all component queries to use new changes * renamed class to reflect new query-running responsibilities * refactored everything now except for selection queries (inc docstrings * refactored all remaining selection queries * add edge case testing * remove unused import * cannot use mute on line_geometry fixture Co-authored-by: jgd10 <[email protected]>
1 parent 17a32a3 commit 9ff41ff

File tree

11 files changed

+204
-269
lines changed

11 files changed

+204
-269
lines changed

ansys/mapdl/core/inline_functions/component_queries.py

Lines changed: 58 additions & 106 deletions
Large diffs are not rendered by default.

ansys/mapdl/core/inline_functions/connectivity_queries.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from .core import _ParameterParsing
1+
from .core import _QueryExecution
22

33

4-
class _ConnectivityQueries(_ParameterParsing):
4+
class _ConnectivityQueries(_QueryExecution):
55
_mapdl = None
66

77
def nelem(self, e, npos) -> int:
@@ -40,8 +40,7 @@ def nelem(self, e, npos) -> int:
4040
>>> positions
4141
[2, 14, 17, 5, 53, 63, 99, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
4242
"""
43-
response = self._mapdl.run(f"_=NELEM({e},{npos})")
44-
return self._parse_parameter_integer_response(response)
43+
return self._run_query(f'NELEM({e},{npos})', integer=True)
4544

4645
def enextn(self, n, loc) -> int:
4746
"""Returns the ``loc`` element connected to node ``n``.
@@ -80,5 +79,4 @@ def enextn(self, n, loc) -> int:
8079
>>> elements
8180
[61, 71]
8281
"""
83-
response = self._mapdl.run(f"_=ENEXTN({n},{loc})")
84-
return self._parse_parameter_integer_response(response)
82+
return self._run_query(f'ENEXTN({n},{loc})', integer=True)

ansys/mapdl/core/inline_functions/core.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import warnings
22
from enum import IntEnum
3+
from typing import Union
4+
5+
QUERY_NAME = "__QUERY_PARM__"
36

47

58
class SelectionStatus(IntEnum):
@@ -29,11 +32,12 @@ class SelectionStatus(IntEnum):
2932
1
3033
3134
We can use ``Query.nsel`` to interrogate the selection status
32-
of the node. The response is an ``enum.IntEnum`` object. If
33-
you query a node that does not exist, it will return a status
34-
``SelectionStatus.UNDEFINED``.
35+
of the node. We can get the ``Query`` object from the
36+
``mapdl.queries`` property. The response is an
37+
``enum.IntEnum`` object. If you query a node that does not
38+
exist, it will return a status ``SelectionStatus.UNDEFINED``.
3539
36-
>>> q = Query(mapdl)
40+
>>> q = mapdl.queries
3741
>>> q.nsel(n1)
3842
<SelectionStatus.SELECTED: 1>
3943
>>> mapdl.nsel('NONE')
@@ -48,8 +52,35 @@ class SelectionStatus(IntEnum):
4852
SELECTED = 1
4953

5054

51-
class _ParameterParsing:
55+
class _QueryExecution:
56+
57+
def _run_query(self, command: str, integer: bool) -> Union[int, float]:
58+
# import here to avoid circular import
59+
from ansys.mapdl.core.mapdl_grpc import MapdlGrpc
60+
61+
# non_interactive mode won't work with these commands
62+
if self._mapdl._store_commands:
63+
raise RuntimeError(
64+
"Inline MAPDL functions are incompatible with the "
65+
"non_interactive mode."
66+
)
67+
68+
# use the underlying gRPC method if available to avoid parsing the string
69+
resp = self._mapdl._run(f"{QUERY_NAME}={command}")
70+
if isinstance(self._mapdl, MapdlGrpc):
71+
value = self._mapdl.scalar_param(QUERY_NAME)
72+
if value is None:
73+
raise RuntimeError(resp)
74+
if integer:
75+
return int(value)
76+
return value
77+
else:
78+
if integer:
79+
return self._parse_parameter_integer_response(resp)
80+
return self._parse_parameter_float_response(resp)
81+
5282
def _parse_parameter_integer_response(self, response) -> int:
83+
"""Parse integer response."""
5384
return int(self._parse_parameter_float_response(response))
5485

5586
@staticmethod

ansys/mapdl/core/inline_functions/geometry_queries.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from .core import _ParameterParsing
1+
from .core import _QueryExecution
22

33

4-
class _AngleQueries(_ParameterParsing):
4+
class _AngleQueries(_QueryExecution):
55
_mapdl = None
66

77
def anglen(self, n1, n2, n3) -> float:
@@ -42,8 +42,7 @@ def anglen(self, n1, n2, n3) -> float:
4242
>>> angle*180./pi
4343
90.0
4444
"""
45-
response = self._mapdl.run(f"_=ANGLEN({n1},{n2},{n3})")
46-
return self._parse_parameter_float_response(response)
45+
return self._run_query(f'ANGLEN({n1},{n2},{n3})', integer=False)
4746

4847
def anglek(self, k1, k2, k3) -> float:
4948
"""Return the angle between 3 keypoints where ``k1`` is the vertex.
@@ -84,11 +83,10 @@ def anglek(self, k1, k2, k3) -> float:
8483
>>> angle*180./pi
8584
45.0
8685
"""
87-
response = self._mapdl.run(f"_=ANGLEK({k1},{k2},{k3})")
88-
return self._parse_parameter_float_response(response)
86+
return self._run_query(f'ANGLEK({k1},{k2},{k3})', integer=False)
8987

9088

91-
class _AreaQueries(_ParameterParsing):
89+
class _AreaQueries(_QueryExecution):
9290
_mapdl = None
9391

9492
def areand(self, n1, n2, n3) -> float:
@@ -121,8 +119,7 @@ def areand(self, n1, n2, n3) -> float:
121119
>>> area = mapdl.queries.areand(n1, n2, n3)
122120
0.5
123121
"""
124-
response = self._mapdl.run(f"_=AREAND({n1},{n2},{n3})")
125-
return self._parse_parameter_float_response(response)
122+
return self._run_query(f'AREAND({n1},{n2},{n3})', integer=False)
126123

127124
def areakp(self, k1, k2, k3) -> float:
128125
"""Area of the triangle with vertices at keypoints ``k1``, ``k2``, and ``k3``.
@@ -154,11 +151,10 @@ def areakp(self, k1, k2, k3) -> float:
154151
>>> mapdl.queries.areakp(k1, k2, k3)
155152
0.2545584412
156153
"""
157-
response = self._mapdl.run(f"_=AREAKP({k1},{k2},{k3})")
158-
return self._parse_parameter_float_response(response)
154+
return self._run_query(f'AREAKP({k1},{k2},{k3})', integer=False)
159155

160156

161-
class _DistanceQueries(_ParameterParsing):
157+
class _DistanceQueries(_QueryExecution):
162158
_mapdl = None
163159

164160
def distnd(self, n1, n2) -> float:
@@ -188,8 +184,7 @@ def distnd(self, n1, n2) -> float:
188184
>>> mapdl.queries.distnd(n1, n2)
189185
1.0
190186
"""
191-
response = self._mapdl.run(f"_=DISTND({n1},{n2})")
192-
return self._parse_parameter_float_response(response)
187+
return self._run_query(f'DISTND({n1},{n2})', integer=False)
193188

194189
def distkp(self, k1, k2) -> float:
195190
"""Compute the distance between keypoints ``k1`` and ``k2``.
@@ -222,5 +217,4 @@ def distkp(self, k1, k2) -> float:
222217
>>> sqrt(2)
223218
1.4142135623730951
224219
"""
225-
response = self._mapdl.run(f"_=DISTKP({k1},{k2})")
226-
return self._parse_parameter_float_response(response)
220+
return self._run_query(f'DISTKP({k1},{k2})', integer=False)

ansys/mapdl/core/inline_functions/inline_functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ class Query(
7777
--------
7878
In this example we construct a solid box and mesh it. Then we use
7979
the ``Query`` methods ``nx``, ``ny``, and ``nz`` to find the
80-
cartesian coordinates of the first node.
80+
cartesian coordinates of the first node. We can access these
81+
through the ``mapdl.queries`` property.
8182
8283
>>> from ansys.mapdl.core import launch_mapdl
83-
>>> from ansys.mapdl.core.inline_functions import Query
8484
>>> mapdl = launch_mapdl()
8585
>>> mapdl.prep7()
8686
>>> mapdl.et(1, 'SOLID5')
8787
>>> mapdl.block(0, 10, 0, 20, 0, 30)
8888
>>> mapdl.esize(2)
8989
>>> mapdl.vmesh('ALL')
90-
>>> q = Query(mapdl)
90+
>>> q = mapdl.queries
9191
>>> q.nx(1), q.ny(1), q.nz(1)
9292
0.0 20.0 0.0
9393
"""

ansys/mapdl/core/inline_functions/line_queries.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from .core import _ParameterParsing
1+
from .core import _QueryExecution
22

33

4-
class _LineFractionCoordinateQueries(_ParameterParsing):
4+
class _LineFractionCoordinateQueries(_QueryExecution):
55
_mapdl = None
66

77
def lx(self, n: int, lfrac: float) -> float:
@@ -29,19 +29,17 @@ def lx(self, n: int, lfrac: float) -> float:
2929
Here we construct a line between the coordinates ``(0, 0, 0)``
3030
and ``(1, 2, 3)`` then find the X-coordinate halfway along the line.
3131
32-
>>> from ansys.mapdl.core.inline_functions import Query
3332
>>> from ansys.mapdl.core import launch_mapdl
3433
>>> mapdl = launch_mapdl()
3534
>>> mapdl.prep7()
3635
>>> k0 = mapdl.k(1, 0, 0, 0)
3736
>>> k1 = mapdl.k(2, 1, 2, 3)
3837
>>> l0 = mapdl.l(k0, k1)
39-
>>> q = Query(mapdl)
38+
>>> q = mapdl.queries
4039
>>> q.lx(l0, 0.5)
4140
0.5
4241
"""
43-
response = self._mapdl.run(f"_=LX({n},{lfrac})")
44-
return self._parse_parameter_float_response(response)
42+
return self._run_query(f'LX({n}, {lfrac})', integer=False)
4543

4644
def ly(self, n: int, lfrac: float) -> float:
4745
"""Y-coordinate of line ``n`` at length fraction ``lfrac``.
@@ -68,19 +66,17 @@ def ly(self, n: int, lfrac: float) -> float:
6866
Here we construct a line between the coordinates ``(0, 0, 0)``
6967
and ``(1, 2, 3)`` then find the Y-coordinate halfway along the line.
7068
71-
>>> from ansys.mapdl.core.inline_functions import Query
7269
>>> from ansys.mapdl.core import launch_mapdl
7370
>>> mapdl = launch_mapdl()
7471
>>> mapdl.prep7()
7572
>>> k0 = mapdl.k(1, 0, 0, 0)
7673
>>> k1 = mapdl.k(2, 1, 2, 3)
7774
>>> l0 = mapdl.l(k0, k1)
78-
>>> q = Query(mapdl)
75+
>>> q = mapdl.queries
7976
>>> q.ly(l0, 0.5)
8077
1.0
8178
"""
82-
response = self._mapdl.run(f"_=LY({n},{lfrac})")
83-
return self._parse_parameter_float_response(response)
79+
return self._run_query(f'LY({n}, {lfrac})', integer=False)
8480

8581
def lz(self, n: int, lfrac: float) -> float:
8682
"""Z-coordinate of line ``n`` at length fraction ``lfrac``.
@@ -107,22 +103,20 @@ def lz(self, n: int, lfrac: float) -> float:
107103
Here we construct a line between the coordinates ``(0, 0, 0)``
108104
and ``(1, 2, 3)`` then find the Z-coordinate halfway along the line.
109105
110-
>>> from ansys.mapdl.core.inline_functions import Query
111106
>>> from ansys.mapdl.core import launch_mapdl
112107
>>> mapdl = launch_mapdl()
113108
>>> mapdl.prep7()
114109
>>> k0 = mapdl.k(1, 0, 0, 0)
115110
>>> k1 = mapdl.k(2, 1, 2, 3)
116111
>>> l0 = mapdl.l(k0, k1)
117-
>>> q = Query(mapdl)
112+
>>> q = mapdl.queries
118113
>>> q.lz(l0, 0.5)
119114
1.5
120115
"""
121-
response = self._mapdl.run(f"_=LZ({n},{lfrac})")
122-
return self._parse_parameter_float_response(response)
116+
return self._run_query(f'LZ({n}, {lfrac})', integer=False)
123117

124118

125-
class _LineFractionSlopeQueries(_ParameterParsing):
119+
class _LineFractionSlopeQueries(_QueryExecution):
126120
_mapdl = None
127121

128122
def lsx(self, n: int, lfrac: float) -> float:
@@ -151,19 +145,17 @@ def lsx(self, n: int, lfrac: float) -> float:
151145
Here we construct a line between the coordinates ``(0, 0, 0)``
152146
and ``(1, 2, 2)`` then find the X-slope halfway along the line.
153147
154-
>>> from ansys.mapdl.core.inline_functions import Query
155148
>>> from ansys.mapdl.core import launch_mapdl
156149
>>> mapdl = launch_mapdl()
157150
>>> mapdl.prep7()
158151
>>> k0 = mapdl.k(1, 0, 0, 0)
159152
>>> k1 = mapdl.k(2, 1, 2, 2)
160153
>>> l0 = mapdl.l(k0, k1)
161-
>>> q = Query(mapdl)
154+
>>> q = mapdl.queries
162155
>>> q.lsx(l0, 0.5)
163156
0.3333333333
164157
"""
165-
response = self._mapdl.run(f"_=LSX({n},{lfrac})")
166-
return self._parse_parameter_float_response(response)
158+
return self._run_query(f'LSX({n}, {lfrac})', integer=False)
167159

168160
def lsy(self, n: int, lfrac: float) -> float:
169161
"""Y-slope of line ``n`` at length fraction ``lfrac``.
@@ -191,19 +183,17 @@ def lsy(self, n: int, lfrac: float) -> float:
191183
Here we construct a line between the coordinates ``(0, 0, 0)``
192184
and ``(1, 2, 2)`` then find the Y-slope halfway along the line.
193185
194-
>>> from ansys.mapdl.core.inline_functions import Query
195186
>>> from ansys.mapdl.core import launch_mapdl
196187
>>> mapdl = launch_mapdl()
197188
>>> mapdl.prep7()
198189
>>> k0 = mapdl.k(1, 0, 0, 0)
199190
>>> k1 = mapdl.k(2, 1, 2, 2)
200191
>>> l0 = mapdl.l(k0, k1)
201-
>>> q = Query(mapdl)
192+
>>> q = mapdl.queries
202193
>>> q.lsy(l0, 0.5)
203194
0.6666666667
204195
"""
205-
response = self._mapdl.run(f"_=LSY({n},{lfrac})")
206-
return self._parse_parameter_float_response(response)
196+
return self._run_query(f'LSY({n}, {lfrac})', integer=False)
207197

208198
def lsz(self, n: int, lfrac: float) -> float:
209199
"""Z-slope of line ``n`` at length fraction ``lfrac``.
@@ -231,16 +221,14 @@ def lsz(self, n: int, lfrac: float) -> float:
231221
Here we construct a line between the coordinates ``(0, 0, 0)``
232222
and ``(1, 2, 2)`` then find the Z-slope halfway along the line.
233223
234-
>>> from ansys.mapdl.core.inline_functions import Query
235224
>>> from ansys.mapdl.core import launch_mapdl
236225
>>> mapdl = launch_mapdl()
237226
>>> mapdl.prep7()
238227
>>> k0 = mapdl.k(1, 0, 0, 0)
239228
>>> k1 = mapdl.k(2, 1, 2, 2)
240229
>>> l0 = mapdl.l(k0, k1)
241-
>>> q = Query(mapdl)
230+
>>> q = mapdl.queries
242231
>>> q.lsz(l0, 0.5)
243232
0.6666666667
244233
"""
245-
response = self._mapdl.run(f"_=LSZ({n},{lfrac})")
246-
return self._parse_parameter_float_response(response)
234+
return self._run_query(f'LSZ({n}, {lfrac})', integer=False)

ansys/mapdl/core/inline_functions/nearest_queries.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from .core import _ParameterParsing
1+
from .core import _QueryExecution
22

33

4-
class _EntityNearestEntityQueries(_ParameterParsing):
4+
class _EntityNearestEntityQueries(_QueryExecution):
55
_mapdl = None
66

77
def nnear(self, n: int) -> int:
@@ -37,9 +37,7 @@ def nnear(self, n: int) -> int:
3737
>>> node_number, nearest_node
3838
(112, 103)
3939
"""
40-
response = self._mapdl.run(f"_=NNEAR({n})")
41-
integer = self._parse_parameter_integer_response(response)
42-
return integer
40+
return self._run_query(f'NNEAR({n})', integer=True)
4341

4442
def knear(self, k: int) -> int:
4543
"""Returns the selected keypoint nearest keypoint `k`.
@@ -71,9 +69,7 @@ def knear(self, k: int) -> int:
7169
>>> q.knear(k1) == k2
7270
True
7371
"""
74-
response = self._mapdl.run(f"_=KNEAR({k})")
75-
integer = self._parse_parameter_integer_response(response)
76-
return integer
72+
return self._run_query(f'KNEAR({k})', integer=True)
7773

7874
def enearn(self, n: int) -> int:
7975
"""Returns the selected element nearest node `n`.
@@ -110,6 +106,4 @@ def enearn(self, n: int) -> int:
110106
>>> node_number, nearest_element
111107
(112, 22)
112108
"""
113-
response = self._mapdl.run(f"_=ENEARN({n})")
114-
integer = self._parse_parameter_integer_response(response)
115-
return integer
109+
return self._run_query(f'ENEARN({n})', integer=True)

0 commit comments

Comments
 (0)