Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

name 'MapdlRuntimeError' is not defined when trying to use mapdl.noninteractive #2209

Closed
4 tasks done
Artyom-K opened this issue Jul 26, 2023 · 5 comments · Fixed by #2214
Closed
4 tasks done

name 'MapdlRuntimeError' is not defined when trying to use mapdl.noninteractive #2209

Artyom-K opened this issue Jul 26, 2023 · 5 comments · Fixed by #2214

Comments

@Artyom-K
Copy link

🤓 Before submitting the issue

🔍 Description of the bug

NameError: name 'MapdlRuntimeError' is not defined when using inline function inside of with mapdl.non_interactive

🕵️ Steps To Reproduce

Initially I had a loop

for i in nlist:
    x = mapdl.queries.nx(i)
    ....

It was very slow so I wanted to try the following

with mapdl.non_interactive:
    for i in nlist:
        x = mapdl.queries.nx(i)
        ...

Now I get this error

File C:\ProgramData\Anaconda3\envs\ansys2\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File \my_file.py:68
    x = mapdl.queries.nx(i)

  File C:\ProgramData\Anaconda3\envs\ansys2\Lib\site-packages\ansys\mapdl\core\inline_functions\component_queries.py:142 in nx
    return self._run_query(f"NX({n})", integer=False)

  File C:\ProgramData\Anaconda3\envs\ansys2\Lib\site-packages\ansys\mapdl\core\inline_functions\core.py:62 in _run_query
    raise MapdlRuntimeError(

NameError: name 'MapdlRuntimeError' is not defined

💻 Which Operating System are you using?

None

🐍 Which Python version are you using?

3.11

📝 PyMAPDL Report

Show the Report!

C:\ProgramData\Anaconda3\envs\ansys2\Lib\site-packages\pyvista\utilities\__init__.py:69: PyVistaDeprecationWarning: The `pyvista.utilities` module has been deprecated. `_get_sg_image_scraper` is now imported as: `from pyvista.plotting.utilities import _get_sg_image_scraper`.
  warnings.warn(
C:\ProgramData\Anaconda3\envs\ansys2\Lib\site-packages\pyvista\utilities\__init__.py:69: PyVistaDeprecationWarning: The `pyvista.utilities` module has been deprecated. `GPUInfo` is now imported as: `from pyvista.report import GPUInfo`.
  warnings.warn(
-------------------------------------------------------------------------------


PyMAPDL Software and Environment Report


Packages Requirements
*********************

Core packages
-------------
ansys.mapdl.core    : 0.65.0
numpy               : 1.25.1
appdirs             : 1.4.4
scipy               : 1.11.1
grpc                : Package not found
ansys.api.mapdl.v0  : Package not found
ansys.mapdl.reader  : 0.52.18
google.protobuf     : Package not found

Optional packages
-----------------
matplotlib          : 3.7.2
pyvista             : 0.40.1
pyiges              : 0.3.1
tqdm                : 4.65.0


Ansys Installation
******************
Version   Location
------------------
222       D:\Program Files\ANSYS Inc\v222


Ansys Environment Variables
***************************
ANSYS222_DIR                   D:\Program Files\ANSYS Inc\v222\ANSYS
ANSYS_SYSDIR                   winx64
ANSYS_SYSDIR32                 win32
AWP_LOCALE222                  en-us
AWP_ROOT222                    D:\Program Files\ANSYS Inc\v222
CADOE_LIBDIR222                D:\Program Files\ANSYS Inc\v222\CommonFiles\Language\en-us

📝 Installed packages

Show the installed packages!

alabaster==0.7.13
ansys-api-mapdl==0.5.1
ansys-api-platform-instancemanagement==1.0.0b3
ansys-dpf-core==0.8.1
ansys-dpf-gate==0.3.1
ansys-dpf-gatebin==0.3.1
ansys-dpf-post==0.4.0
ansys-grpc-dpf==0.7.1
ansys-mapdl-core==0.65.0
ansys-mapdl-reader==0.52.18
ansys-platform-instancemanagement==1.1.1
ansys-tools-path==0.3.1
appdirs==1.4.4
arrow==1.2.3
astroid==2.15.6
asttokens==2.2.1
atomicwrites==1.4.1
attrs==23.1.0
autopep8==2.0.2
Babel==2.12.1
backcall==0.2.0
bcrypt==4.0.1
beautifulsoup4==4.12.2
binaryornot==0.4.4
black==23.7.0
bleach==6.0.0
cachetools==5.3.1
certifi==2023.5.7
cffi==1.15.1
chardet==5.1.0
charset-normalizer==3.2.0
click==8.1.4
cloudpickle==2.2.1
colorama==0.4.6
comm==0.1.3
contourpy==1.1.0
cookiecutter==2.2.3
cryptography==41.0.2
cycler==0.11.0
debugpy==1.6.7
decorator==5.1.1
defusedxml==0.7.1
diff-match-patch==20230430
dill==0.3.6
docstring-to-markdown==0.12
docutils==0.20.1
executing==1.2.0
fastjsonschema==2.17.1
flake8==6.0.0
fonttools==4.41.0
google-api-core==2.11.1
google-api-python-client==2.94.0
google-auth==2.22.0
google-auth-httplib2==0.1.0
googleapis-common-protos==1.59.1
grpcio==1.56.0
httplib2==0.22.0
idna==3.4
imagesize==1.4.1
importlib-metadata==6.8.0
inflection==0.5.1
intervaltree==3.1.0
ipykernel==6.24.0
ipython==8.14.0
ipython-genutils==0.2.0
isort==5.12.0
jaraco.classes==3.3.0
jedi==0.18.2
jellyfish==1.0.0
Jinja2==3.1.2
jsonschema==4.18.2
jsonschema-specifications==2023.6.1
jupyter_client==8.3.0
jupyter_core==5.3.1
jupyterlab-pygments==0.2.2
keyring==24.2.0
kiwisolver==1.4.4
lazy-object-proxy==1.9.0
MarkupSafe==2.1.3
matplotlib==3.7.2
matplotlib-inline==0.1.6
mccabe==0.7.0
mistune==3.0.1
more-itertools==9.1.0
mypy-extensions==1.0.0
nbclient==0.8.0
nbconvert==7.6.0
nbformat==5.9.1
nest-asyncio==1.5.6
numpy==1.25.1
numpydoc==1.5.0
packaging==23.1
pandocfilters==1.5.0
paramiko==3.2.0
parso==0.8.3
pathspec==0.11.1
pexpect==4.8.0
pickleshare==0.7.5
Pillow==10.0.0
platformdirs==3.8.1
pluggy==1.2.0
pooch==1.7.0
prompt-toolkit==3.0.39
protobuf==3.20.3
protoc-gen-swagger==0.1.0
psutil==5.9.5
ptyprocess==0.7.0
pure-eval==0.2.2
pyansys-tools-versioning==0.4.0
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycodestyle==2.10.0
pycparser==2.21
pydocstyle==6.3.0
pyflakes==3.0.1
Pygments==2.15.1
pyiges==0.3.1
pylint==2.17.4
pylint-venv==3.0.2
pyls-spyder==0.4.0
PyNaCl==1.5.0
pyparsing==3.0.9
PyQt5==5.15.9
PyQt5-Qt5==5.15.2
PyQt5-sip==12.12.1
PyQtWebEngine==5.15.6
PyQtWebEngine-Qt5==5.15.2
python-dateutil==2.8.2
python-lsp-black==1.3.0
python-lsp-jsonrpc==1.0.0
python-lsp-server==1.7.4
python-slugify==8.0.1
pytoolconfig==1.2.5
pyvista==0.40.1
pywin32==306
pywin32-ctypes==0.2.2
PyYAML==6.0
pyzmq==25.1.0
QDarkStyle==3.1
qstylizer==0.2.2
QtAwesome==1.2.3
qtconsole==5.4.3
QtPy==2.3.1
referencing==0.29.1
requests==2.31.0
rope==1.9.0
rpds-py==0.8.10
rsa==4.9
Rtree==1.0.1
scipy==1.11.1
scooby==0.7.2
six==1.16.0
snowballstemmer==2.2.0
sortedcontainers==2.4.0
soupsieve==2.4.1
Sphinx==7.0.1
sphinxcontrib-applehelp==1.0.4
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.1
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
spyder==5.4.3
spyder-kernels==2.4.4
stack-data==0.6.2
text-unidecode==1.3
textdistance==4.5.0
three-merge==0.1.1
tinycss2==1.2.1
tomli==2.0.1
tomlkit==0.11.8
tornado==6.3.2
tqdm==4.65.0
traitlets==5.9.0
ujson==5.8.0
uritemplate==4.1.1
urllib3==1.26.16
vtk==9.2.6
watchdog==3.0.0
wcwidth==0.2.6
webencodings==0.5.1
whatthepatch==1.0.5
wrapt==1.15.0
yapf==0.40.1
zipp==3.16.1

📝 Logger output file

Show the logger output file.

# PASTE HERE THE CONTENT OF THE LOGGER OUTPUT FILE.

@mikerife
Copy link

Hi @Artyom-K
I'm not exactly sure why but putting a Python loop inside of a 'with mapdl.interactive:' will result in an infinite loop. Maybe @germa89 can weigh in here.

You could instead write an MAPDL input file then read that in, but then you would need to retrieve the node X values collected. I think it might be better to instead gather the nodes to an array and parse through that for the needed information. Please try this example:

from ansys.mapdl.core import launch_mapdl
import numpy as np
import os
path = os.getcwd()

mapdl = launch_mapdl(run_location=path)

mapdl.clear()
mapdl.prep7()
mapdl.n(1,0,0,0)
mapdl.n(2,1,0,0)
mapdl.n(3,2,0,0)
mapdl.n(4,3,0,0)
mapdl.n(5,4,0,0)
mapdl.n(6,5,0,0)
mapdl.n(7,6,0,0)

n_list = [2, 4, 7]


node = mapdl.mesh.nodes
node_num =mapdl.mesh.nnum

nodes_x = []
for i in n_list:
    print("node " +str(i)+ " x is " + str(node[np.where(node_num == i)[0][0]][0]))
    nodes_x.append(node[np.where(node_num == i)[0][0]][0])
nodes_x

Which will result in:

image

Then don't forget the mapdl.exit()
Mike

@germa89
Copy link
Collaborator

germa89 commented Jul 28, 2023

Hi @Artyom-K

The queries module is not compatible with mapdl.non_interactive.

However you are right, that the MapdlRuntimeError import is missing.

If I were you, and needed speed, I would just do:

nodes = mapdl.mesh.nodes
def nx(i):
    return nodes[i,0]

def ny(i):
    return nodes[i,1]

def nz(i):
    return nodes[i,2]

# and then:
for i in nlist:
    x = nx(i)

This way you avoid multiple calls to the MAPDL instance.

@germa89
Copy link
Collaborator

germa89 commented Jul 28, 2023

Hi @mikerife

I would like to suggest a small improvement to your snippet:

>>> # Assuming this are the node ids you want to return 
>>> n_list = [2, 4, 7]  # mapdl indexing
>>> n_list_ = np.array(n_list) - 1  # python indexing

>>> node = mapdl.mesh.nodes
>>> nodes_x = node[n_list_]
array([[1., 0., 0.],
       [3., 0., 0.],
       [6., 0., 0.]])

@mikerife
Copy link

@germa89 Thanks! I'll have to dig into that more - I'm wondering if that will work if there are gaps in the node numbering (which is always a concern of mine). Mike

@germa89
Copy link
Collaborator

germa89 commented Jul 28, 2023

@germa89 Thanks! I'll have to dig into that more - I'm wondering if that will work if there are gaps in the node numbering (which is always a concern of mine). Mike

It is a VERY valid concern honestly.

In case the nodes are not contiguous:

from ansys.mapdl.core import launch_mapdl
import numpy as np
import os
path = os.getcwd()

mapdl = launch_mapdl(run_location=path)

mapdl.clear()
mapdl.prep7()
# node ids are the same as the x coordinates
mapdl.n(1,1,0,0)
mapdl.n(2,2,0,0)
mapdl.n(3,3,0,0)
mapdl.n(6,6,0,0)
mapdl.n(7,7,0,0)
mapdl.n(10,10,0,0)
mapdl.n(12,12,0,0)

n_list = [2, 7, 12]

nnum = mapdl.mesh.nnum
nodes = mapdl.mesh.nodes

indices = np.in1d(nnum, n_list)
mynodes = nodes[indices,:]

assert np.allclose(mynodes[:,0], n_list)

You will love numpy.in1d from now on! 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants