-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathcommon.py
More file actions
327 lines (253 loc) · 9.18 KB
/
common.py
File metadata and controls
327 lines (253 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Copyright (C) 2016 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Shared testing module"""
from collections import namedtuple
import os
import subprocess
import time
from typing import Dict, List
import psutil
from ansys.mapdl.core import LOG, Mapdl
from ansys.mapdl.core.errors import MapdlConnectionError, MapdlExitedError
from ansys.mapdl.core.launcher import (
_is_ubuntu,
get_start_instance,
is_ansys_process,
launch_mapdl,
)
PROCESS_OK_STATUS = [
psutil.STATUS_RUNNING, #
psutil.STATUS_SLEEPING, #
psutil.STATUS_DISK_SLEEP,
psutil.STATUS_DEAD,
psutil.STATUS_PARKED, # (Linux)
psutil.STATUS_IDLE, # (Linux, macOS, FreeBSD)
]
Node = namedtuple("Node", ["number", "x", "y", "z", "thx", "thy", "thz"])
Element = namedtuple(
"Element",
[
"number",
"material",
"type",
"real_const",
"coord_system",
"section",
"node_numbers",
],
)
# Set if on local
def is_on_local():
if os.environ.get("ON_LOCAL"):
return os.environ.get("ON_LOCAL").lower() == "true"
if os.environ.get("ON_REMOTE"):
return os.environ.get("ON_REMOTE").lower() == "true"
if os.environ.get("PYMAPDL_START_INSTANCE"):
return (
os.environ.get("PYMAPDL_START_INSTANCE").lower() != "false"
) # default is false
from ansys.tools.path import find_mapdl
_, rver = find_mapdl()
if rver:
return True
else:
return False
# Set if on CI
def is_on_ci():
return os.environ.get("ON_CI", "").lower() == "true"
# Set if on ubuntu
def is_on_ubuntu():
envvar = os.environ.get("ON_UBUNTU")
if envvar is not None:
return envvar.lower() == "true"
return _is_ubuntu()
def has_grpc():
envvar = os.environ.get("HAS_GRPC")
if envvar is not None:
return envvar.lower().strip() == "true"
if testing_minimal():
return True
try:
from ansys.tools.path import find_mapdl
except ModuleNotFoundError:
return True
_, rver = find_mapdl()
if rver:
rver = int(rver * 10)
return int(rver) >= 211
else:
return True # In remote mode, assume gRPC by default.
def has_dpf():
return bool(os.environ.get("DPF_PORT"))
def is_smp():
return os.environ.get("DISTRIBUTED_MODE", "smp").lower().strip() == "smp"
def support_plotting():
envvar = os.environ.get("SUPPORT_PLOTTING")
if envvar is not None:
return envvar.lower().strip() == "true"
if testing_minimal():
return False
try:
import pyvista
return pyvista.system_supports_plotting()
except ModuleNotFoundError:
return False
def is_running_on_student():
return os.environ.get("ON_STUDENT", "NO").upper().strip() in ["YES", "TRUE"]
def testing_minimal():
return os.environ.get("TESTING_MINIMAL", "NO").upper().strip() in ["YES", "TRUE"]
def debug_testing() -> bool:
if os.environ.get("PYMAPDL_DEBUG_TESTING"):
debug_testing = os.environ.get("PYMAPDL_DEBUG_TESTING")
if debug_testing.lower() in ["true", "false", "yes", "no"]:
return debug_testing.lower() in ["true", "yes"]
else:
return debug_testing
else:
return False
def is_float(s: str) -> bool:
try:
float(s)
return True
except ValueError:
return False
def is_just_floats(s: str, delimiter=None):
if delimiter is None:
entries = s.split()
else:
entries = s.split(delimiter)
if not entries:
return False
for entry in entries:
if not is_float(entry.strip()):
return False
return True
def get_details_of_nodes(mapdl_) -> Dict[int, Node]:
string = mapdl_.nlist("ALL")
rows = string.split("\n")
nodes = {}
for row in rows:
if is_just_floats(row):
row_values = [v for v in row.split(" ") if v != ""]
node = int(row_values[0])
nodes[node] = Node(node, *[float(rv) for rv in row_values[1:]])
return nodes
def get_details_of_elements(mapdl_) -> Dict[int, Node]:
string = mapdl_.elist("ALL")
# string = string.split(' ELEM ')[1]
# string = string.split('\n', 1)[1]
rows = string.split("\n")
elements = {}
for row in rows:
if is_just_floats(row):
row_values = [v for v in row.split(" ") if v != ""]
args = [int(rv) for rv in row_values[:6]]
# todo: Node numbers can go over multiple lines, which makes
# parsing them properly a real pain. So for now I'll leave
# this as is and work on a better version in the future
if len(args) == 6:
elements[args[0]] = Element(*args, node_numbers=None)
return elements
def log_test_start(mapdl: Mapdl) -> None:
"""Print the current test to the MAPDL log file and console output."""
test_name = os.environ.get(
"PYTEST_CURRENT_TEST", "**test id could not get retrieved.**"
)
mapdl.run("!")
mapdl.run(f"! PyMAPDL running test: {test_name}"[:639])
mapdl.run("!")
# To see it also in MAPDL terminal output
if len(test_name) > 75:
# terminal output is limited to 75 characters
test_name = test_name.split("::")
if len(test_name) > 2:
types_ = ["File path", "Test class", "Method"]
else:
types_ = ["File path", "Test function"]
mapdl._run("/com,Running test in:", mute=True)
for type_, name_ in zip(types_, test_name):
mapdl._run(f"/com, {type_}: {name_}", mute=True)
else:
mapdl._run(f"/com,Running test: {test_name}", mute=True)
def restart_mapdl(mapdl: Mapdl) -> Mapdl:
"""Restart MAPDL after a failed test"""
def is_exited(mapdl: Mapdl):
try:
_ = mapdl._ctrl("VERSION")
return False
except MapdlExitedError:
return True
if get_start_instance() and (is_exited(mapdl) or mapdl._exited):
# Backing up the current local configuration
local_ = mapdl._local
ip = mapdl.ip
port = mapdl.port
try:
# to connect
mapdl = Mapdl(port=port, ip=ip)
warn("MAPDL disconnected during testing, reconnected.")
except MapdlConnectionError as err:
from conftest import DEBUG_TESTING, ON_LOCAL
# Registering error.
LOG.info(str(err))
# we cannot connect.
# Kill the instance
try:
mapdl.exit()
except Exception as e:
LOG.error(f"An error occurred when killing the instance:\n{str(e)}")
# Relaunching MAPDL
mapdl = launch_mapdl(
port=mapdl._port,
override=True,
run_location=mapdl._path,
cleanup_on_exit=mapdl._cleanup,
license_server_check=False,
start_timeout=50,
loglevel="DEBUG" if DEBUG_TESTING else "ERROR",
# If the following file names are changed, update `ci.yml`.
log_apdl="pymapdl.apdl" if DEBUG_TESTING else None,
mapdl_output="apdl.out" if (DEBUG_TESTING and ON_LOCAL) else None,
)
warn("MAPDL died during testing, relaunched.")
LOG.info("Successfully re-connected to MAPDL")
# Restoring the local configuration
mapdl._local = local_
mapdl._exited = False
return mapdl
def make_sure_not_instances_are_left_open(valid_ports: List) -> None:
"""Make sure we leave no MAPDL running behind"""
if is_on_local():
for proc in psutil.process_iter():
try:
if (
psutil.pid_exists(proc.pid)
and proc.status() in PROCESS_OK_STATUS
and is_ansys_process(proc)
):
cmdline = proc.cmdline()
port = int(cmdline[cmdline.index("-port") + 1])
if port not in valid_ports:
subprocess.run(["pymapdl", "stop", "--port", f"{port}"])
time.sleep(1)
except psutil.NoSuchProcess:
continue