-
Notifications
You must be signed in to change notification settings - Fork 25
/
misc.py
199 lines (158 loc) · 5.65 KB
/
misc.py
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
"""Miscellaneous functions for the DPF module."""
import platform
import glob
import os
import packaging.version
import pkg_resources
import importlib
from pkgutil import iter_modules
from ansys.dpf.gate._version import __ansys_version__
DEFAULT_FILE_CHUNK_SIZE = 524288
DYNAMIC_RESULTS = True
RETURN_ARRAYS = True
RUNTIME_CLIENT_CONFIG = None
def module_exists(module_name):
"""Check if a model exists.
Parameters
----------
module_name : str
Name of the module.
"""
return module_name in (name for loader, name, ispkg in iter_modules())
def is_float(string):
"""Check if a string can be converted to a float.
Parameters
----------
string : str
String to check.
Returns
-------
bool
``True`` if the input can be converted to a float, ``False`` if otherwise.
"""
try:
float(string)
return True
except ValueError:
return False
def is_ubuntu():
"""Check if the operating system is Ubuntu.
Returns
-------
bool
``True`` when the operating system is Ubuntu, ``False`` if otherwise.
"""
if os.name == "posix":
return "ubuntu" in platform.platform().lower()
return False
def get_ansys_path(ansys_path=None):
"""Give input path back if given, else look for ANSYS_DPF_PATH, then AWP_ROOT+__ansys_version__,
then calls for find_ansys as a last resort.
Parameters
----------
ansys_path : str
Full path to an Ansys installation to use.
Returns
-------
ansys_path : str
Full path to an Ansys installation.
"""
# If no custom path was given in input
# First check the environment variable for a custom path
if ansys_path is None:
ansys_path = os.environ.get("ANSYS_DPF_PATH")
# Then check for usual installation folders with AWP_ROOT and find_ansys
if ansys_path is None:
ansys_path = os.environ.get("AWP_ROOT" + __ansys_version__)
if ansys_path is None:
ansys_path = find_ansys()
# If still no install has been found, throw an exception
if ansys_path is None:
raise ValueError(
"Unable to locate any Ansys installation.\n"
f'Make sure the "AWP_ROOT{__ansys_version__}" environment variable '
f"is set if using ANSYS version {__ansys_version__}.\n"
"You can also manually define the path to the ANSYS installation root folder"
" of the version you want to use (vXXX folder):\n"
'- when starting the server with "start_local_server(ansys_path=*/vXXX)"\n'
'- or by setting it by default with the environment variable "ANSYS_DPF_PATH"'
)
return ansys_path
def _pythonize_awp_version(version):
if len(version) != 3:
return version
return "20" + version[0:2] + "." + version[2]
def _find_latest_ansys_versions():
awp_versions = [key[-3:] for key in os.environ.keys() if "AWP_ROOT" in key]
installed_packages_list = {}
for awp_version in awp_versions:
if not awp_version.isnumeric():
continue
ansys_path = os.environ.get("AWP_ROOT" + awp_version)
if ansys_path:
installed_packages_list[
packaging.version.parse(_pythonize_awp_version(awp_version))
] = ansys_path
installed_packages = pkg_resources.working_set
for i in installed_packages:
if "ansys-dpf-server" in i.key:
file_name = pkg_resources.to_filename(i.project_name.replace("ansys-dpf-", ""))
try:
module = importlib.import_module("ansys.dpf." + file_name)
installed_packages_list[
packaging.version.parse(module.__version__)
] = module.__path__[0]
except ModuleNotFoundError:
pass
except AttributeError:
pass
if len(installed_packages_list) > 0:
return installed_packages_list[sorted(installed_packages_list)[-1]]
def find_ansys():
"""Search for a standard ANSYS environment variable (AWP_ROOTXXX) or a standard installation
location to find the path to the latest Ansys installation.
Returns
-------
ansys_path : str
Full path to the latest version of the Ansys installation.
Examples
--------
Return path of latest Ansys version on Windows.
>>> from ansys.dpf.core.misc import find_ansys
>>> path = find_ansys()
Return path of latest Ansys version on Linux.
>>> path = find_ansys()
"""
latest_install = _find_latest_ansys_versions()
if latest_install:
return latest_install
base_path = None
if os.name == "nt":
base_path = os.path.join(os.environ["PROGRAMFILES"], "ANSYS INC")
elif os.name == "posix":
for path in ["/usr/ansys_inc", "/ansys_inc"]:
if os.path.isdir(path):
base_path = path
else:
raise OSError(f"Unsupported OS {os.name}")
if base_path is None:
return base_path
paths = glob.glob(os.path.join(base_path, "v*"))
if not paths:
return None
versions = {}
for path in paths:
ver_str = path[-3:]
if is_float(ver_str):
versions[int(ver_str)] = path
return versions[max(versions.keys())]
def is_pypim_configured():
"""Check if the environment is configured for PyPIM, without using pypim.
This method is equivalent to ansys.platform.instancemanagement.is_configured(). It's
reproduced here to avoid having hard dependencies.
Returns
-------
bool
``True`` if the environment is setup to use the PIM API, ``False`` otherwise.
"""
return "ANSYS_PLATFORM_INSTANCEMANAGEMENT_CONFIG" in os.environ