ansys / pymechanical

Pythonic interface to Ansys Mechanical ā„¢
https://mechanical.docs.pyansys.com/
MIT License
38 stars 19 forks source link

Can't exit app embedded #919

Open jorzig1990 opened 1 month ago

jorzig1990 commented 1 month ago

šŸ” Before submitting the issue

šŸž Description of the bug

Hello,

I want to implement following workflow in pymechanical as en embedded instance: launch app embedded exit app embedded launch new app embedded

I need this workflow in a larger script in case an error occurs, such as the loss of a licence, and I need to set up a new connection automatically at a later time.

My poblem is, that I can't close the app completely and get allways an error, if I want to launch a new app.

Under Discussion I found the Topic: How to close app? #475 with following answer:

Hi @RGPATCHI @pmaroneh, I believe you can still call app.exit() for 23R2. If the version is < 241, we use ExtAPI.Application.Close(). Otherwise, we use ExtAPI.Application.Exit()

I tried all these commands, but the error message is always the same:

Exception: Cannot have more than one embedded mechanical instance!

I am using Visual Studio Code and for the execution of the code:

Run Python File in Dedicated Terminal

šŸ“ Steps to reproduce

I have tested all 3 versions from the answer in the discussion:

Test ExtAPI.Application.Exit()


import ansys.mechanical.core as mech

print()
print("Run 1")
print()

app = mech.App(version=242)
app.update_globals(globals())
print("AnsysVersion:")
print(app)

DataModel = app.DataModel
ExtAPI = app.ExtAPI
Model = app.DataModel.Project.Model
Tree = app.DataModel.Tree
Graphics = app.ExtAPI.Graphics
Materials = DataModel.Project.Model.Materials

ExtAPI.Application.Exit()

print()
print("Run 2")
print()

app = mech.App(version=242)
app.update_globals(globals())
print("AnsysVersion:")
print(app)

DataModel = app.DataModel
ExtAPI = app.ExtAPI
Model = app.DataModel.Project.Model
Tree = app.DataModel.Tree
Graphics = app.ExtAPI.Graphics
Materials = DataModel.Project.Model.Materials

Output ExtAPI.Application.Exit():


Run 1

AnsysVersion:
Ansys Mechanical [Ansys Mechanical Enterprise]
Product Version:242
Software build date: 06/03/2024 14:47:58      

Run 2

CRITICAL -  -  logging - handle_exception - Uncaught exception
Traceback (most recent call last):
  File "m:\A13\2019\MESSDATEN\SLAB\Sim\Basic\B0004\Python\Test_App_Close.py", line 25, in <module>    
    app = mech.App(version=242)
          ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\JORZIG\AppData\Local\pip_envs\pip_JOJE_py3119_ansys\Lib\site-packages\ansys\mechanical\core\embedding\app.py", line 130, in __init__    
    raise Exception("Cannot have more than one embedded mechanical instance!")
Exception: Cannot have more than one embedded mechanical instance!

Test app.exit()

import ansys.mechanical.core as mech

print()
print("Run 1")
print()

app = mech.App(version=242)
app.update_globals(globals())
print("AnsysVersion:")
print(app)

DataModel = app.DataModel
ExtAPI = app.ExtAPI
Model = app.DataModel.Project.Model
Tree = app.DataModel.Tree
Graphics = app.ExtAPI.Graphics
Materials = DataModel.Project.Model.Materials

app.exit()

print()
print("Run 2")
print()

app = mech.App(version=242)
app.update_globals(globals())
print("AnsysVersion:")
print(app)

DataModel = app.DataModel
ExtAPI = app.ExtAPI
Model = app.DataModel.Project.Model
Tree = app.DataModel.Tree
Graphics = app.ExtAPI.Graphics
Materials = DataModel.Project.Model.Materials

Output app.exit():

Run 1

AnsysVersion:
Ansys Mechanical [Ansys Mechanical Enterprise]
Product Version:242
Software build date: 06/03/2024 14:47:58

Run 2

CRITICAL -  -  logging - handle_exception - Uncaught exception
Traceback (most recent call last):
  File "m:\A13\2019\MESSDATEN\SLAB\Sim\Basic\B0004\Python\Test_App_Close.py", line 25, in <module>    
    app = mech.App(version=242)
          ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\JORZIG\AppData\Local\pip_envs\pip_JOJE_py3119_ansys\Lib\site-packages\ansys\mechanical\core\embedding\app.py", line 130, in __init__    
    raise Exception("Cannot have more than one embedded mechanical instance!")
Exception: Cannot have more than one embedded mechanical instance!

Test ExtAPI.Application.Close():

import ansys.mechanical.core as mech

print()
print("Run 1")
print()

app = mech.App(version=242)
app.update_globals(globals())
print("AnsysVersion:")
print(app)

DataModel = app.DataModel
ExtAPI = app.ExtAPI
Model = app.DataModel.Project.Model
Tree = app.DataModel.Tree
Graphics = app.ExtAPI.Graphics
Materials = DataModel.Project.Model.Materials

ExtAPI.Application.Close()

print()
print("Run 2")
print()

app = mech.App(version=242)
app.update_globals(globals())
print("AnsysVersion:")
print(app)

DataModel = app.DataModel
ExtAPI = app.ExtAPI
Model = app.DataModel.Project.Model
Tree = app.DataModel.Tree
Graphics = app.ExtAPI.Graphics
Materials = DataModel.Project.Model.Materials

Output ExtAPI.Application.Close()


Run 1

AnsysVersion:
Ansys Mechanical [Ansys Mechanical Enterprise]
Product Version:242
Software build date: 06/03/2024 14:47:58

Run 2

CRITICAL -  -  logging - handle_exception - Uncaught exception
Traceback (most recent call last):
  File "m:\A13\2019\MESSDATEN\SLAB\Sim\Basic\B0004\Python\Test_App_Close.py", line 25, in <module>    
    app = mech.App(version=242)
          ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\JORZIG\AppData\Local\pip_envs\pip_JOJE_py3119_ansys\Lib\site-packages\ansys\mechanical\core\embedding\app.py", line 130, in __init__    
    raise Exception("Cannot have more than one embedded mechanical instance!")
Exception: Cannot have more than one embedded mechanical instance!

šŸ’» Which operating system are you using?

Windows

šŸ“€ Which ANSYS version are you using?

AnsysVersion: Ansys Mechanical [Ansys Mechanical Enterprise] Product Version:242 Software build date: 06/03/2024 14:47:58

Python 3.11.9 Environment completely built with pip

šŸ Which Python version are you using?

3.11

šŸ“¦ Installed packages

about-time                            4.2.1
aiohappyeyeballs                      2.4.0
aiohttp                               3.10.5
aiosignal                             1.3.1
alabaster                             1.0.0
alive-progress                        3.1.5
annotated-types                       0.7.0
ansys-acp-core                        0.1b2
ansys-additive-core                   0.18.1
ansys-additive-widgets                0.2.1
ansys-api-acp                         0.1.0.dev9
ansys-api-additive                    1.7.2
ansys-api-dbu                         0.3.4
ansys-api-dyna                        0.3.6
ansys-api-edb                         1.0.6
ansys-api-fluent                      0.3.27
ansys-api-geometry                    0.4.6
ansys-api-mapdl                       0.5.2
ansys-api-mechanical                  0.1.2
ansys-api-meshing-prime               0.1.3
ansys-api-modelcenter                 0.3.1
ansys-api-platform-instancemanagement 1.1.0
ansys-api-pyensight                   0.4.1
ansys-api-sherlock                    0.1.27
ansys-api-systemcoupling              0.1.0
ansys-api-tools-filetransfer          0.1.0
ansys-api-workbench                   0.2.0
ansys-conceptev-core                  0.1.4
ansys-dpf-composites                  0.5.0
ansys-dpf-core                        0.12.2
ansys-dpf-post                        0.8.0
ansys-dyna-core                       0.4.15
ansys-dynamicreporting-core           0.6.0
ansys-edb-core                        0.1.6
ansys-engineeringworkflow-api         0.1.0
ansys-fluent-core                     0.22.0
ansys-geometry-core                   0.6.6
ansys-grantami-bomanalytics           2.1.1
ansys-grantami-bomanalytics-openapi   3.0.0
ansys-grantami-jobqueue               1.0.1
ansys-grantami-recordlists            1.2.1
ansys-grantami-serverapi-openapi      3.0.0
ansys-hps-client                      0.8.0
ansys-mapdl-core                      0.68.4
ansys-mapdl-reader                    0.54.1
ansys-math-core                       0.1.5
ansys-mechanical-core                 0.11.5
ansys-mechanical-env                  0.1.7
ansys-meshing-prime                   0.6.2
ansys-modelcenter-workflow            0.1.1
ansys-motorcad-core                   0.5.5
ansys-openapi-common                  2.1.1
ansys-optislang-core                  0.8.0
ansys-platform-instancemanagement     1.1.2
ansys-pyensight-core                  0.8.8
ansys-pythonnet                       3.1.0rc3
ansys-rocky-core                      0.2.0
ansys-seascape                        0.2.0
ansys-sherlock-core                   0.6.0
ansys-simai-core                      0.2.2
ansys-sound-core                      0.1.3
ansys-systemcoupling-core             0.6
ansys-tools-filetransfer              0.1.0
ansys-tools-local-product-launcher    0.1.0
ansys-tools-path                      0.6.0
ansys-tools-visualization-interface   0.4.4
ansys-turbogrid-api                   0.4.3
ansys-turbogrid-core                  0.4.1
ansys-units                           0.3.2
ansys-workbench-core                  0.6.0
anyio                                 4.4.0
appdirs                               1.4.4
arrow                                 1.3.0
asgiref                               3.8.1
astroid                               3.2.4
asttokens                             2.4.1
asyncssh                              2.17.0
atomicwrites                          1.4.1
attrs                                 24.2.0
autopep8                              2.0.4
ax-platform                           0.4.1
babel                                 2.16.0
backoff                               2.2.1
backports.entry-points-selectable     1.3.0
backports.tarfile                     1.2.0
bcrypt                                4.2.0
beartype                              0.18.5
beautifulsoup4                        4.12.3
binaryornot                           0.4.4
black                                 24.8.0
bleach                                6.1.0
bokeh                                 3.4.3
botorch                               0.11.3
build                                 1.2.2
cachetools                            5.5.0
certifi                               2024.8.30
cffi                                  1.17.1
chardet                               5.2.0
charset-normalizer                    3.3.2
click                                 8.1.7
cloudpickle                           3.0.0
clr-loader                            0.2.6
colorama                              0.4.6
comm                                  0.2.2
configparser                          7.1.0
contourpy                             1.3.0
cookiecutter                          2.6.0
cryptography                          43.0.1
cycler                                0.12.1
DateTime                              5.5
debugpy                               1.8.5
decorator                             5.1.1
defusedxml                            0.7.1
Deprecated                            1.2.14
diff-match-patch                      20230430
dill                                  0.3.8
Django                                5.1.1
docker                                7.1.0
docstring-to-markdown                 0.15
docutils                              0.21.2
elementpath                           4.5.0
executing                             2.1.0
fabric                                3.2.2
fastjsonschema                        2.20.0
filelock                              3.16.0
flake8                                7.1.1
flexcache                             0.3
flexparser                            0.3.1
fonttools                             4.53.1
fpdf2                                 2.7.9
frozenlist                            1.4.1
fsspec                                2024.9.0
geomdl                                5.3.1
glob2                                 0.7
google-api-core                       2.19.2
google-api-python-client              2.145.0
google-auth                           2.34.0
google-auth-httplib2                  0.2.0
googleapis-common-protos              1.65.0
gpytorch                              1.12
grapheme                              0.6.0
grpcio                                1.66.1
grpcio-health-checking                1.48.2
grpcio-status                         1.48.2
h11                                   0.14.0
httpcore                              1.0.5
httplib2                              0.22.0
httpx                                 0.26.0
idna                                  3.8
imagesize                             1.4.1
importlib_metadata                    8.4.0
inflection                            0.5.1
intervaltree                          3.1.0
invoke                                2.2.0
ipykernel                             6.29.5
ipython                               8.27.0
ipywidgets                            8.1.5
isort                                 5.13.2
jaraco.classes                        3.4.0
jaraco.context                        6.0.1
jaraco.functools                      4.0.2
jaxtyping                             0.2.19
jedi                                  0.19.1
jellyfish                             1.1.0
Jinja2                                3.1.4
joblib                                1.4.2
jsonschema                            4.23.0
jsonschema-specifications             2023.12.1
jupyter_client                        8.6.2
jupyter_core                          5.7.2
jupyterlab_pygments                   0.3.0
jupyterlab_widgets                    3.0.13
keyring                               25.3.0
kiwisolver                            1.4.7
linear-operator                       0.5.2
linkify-it-py                         2.0.3
lxml                                  5.3.0
Markdown                              3.7
markdown-it-py                        3.0.0
MarkupSafe                            2.1.5
marshmallow                           3.22.0
marshmallow-oneofschema               3.1.1
matplotlib                            3.9.2
matplotlib-inline                     0.1.7
mccabe                                0.7.0
mdit-py-plugins                       0.4.2
mdurl                                 0.1.2
mistune                               3.0.2
more-itertools                        10.5.0
mpmath                                1.3.0
msgpack                               1.1.0
multidict                             6.1.0
multipledispatch                      1.0.0
mypy-extensions                       1.0.0
nbclient                              0.10.0
nbconvert                             7.16.4
nbformat                              5.10.4
nest-asyncio                          1.6.0
networkx                              3.3
nh3                                   0.2.18
nltk                                  3.9.1
numpy                                 1.26.4
numpydoc                              1.8.0
opt-einsum                            3.3.0
overrides                             7.7.0
packaging                             24.1
pandas                                2.2.2
pandocfilters                         1.5.1
panel                                 1.4.4
param                                 2.1.1
paramiko                              3.4.1
parso                                 0.8.4
pathlib                               1.0.1
pathspec                              0.12.1
pexpect                               4.9.0
pickleshare                           0.7.5
pillow                                10.4.0
Pint                                  0.24.3
pip                                   24.2
pkginfo                               1.10.0
platformdirs                          4.3.2
plotly                                5.24.1
pluggy                                1.5.0
plumbum                               1.8.3
pooch                                 1.8.2
prompt_toolkit                        3.0.47
proto-plus                            1.24.0
protobuf                              3.20.3
psutil                                6.0.0
ptyprocess                            0.7.0
pure_eval                             0.2.3
pyaedt                                0.9.11
pyansys                               2024.2.4
pyansys-tools-variableinterop         0.1.1
pyansys-tools-versioning              0.6.0
pyasn1                                0.6.1
pyasn1_modules                        0.4.1
pycodestyle                           2.12.1
pycparser                             2.22
pydantic                              2.8.2
pydantic_core                         2.20.1
pydocstyle                            6.3.0
pyedb                                 0.23.0
pyflakes                              3.2.0
PyGithub                              2.4.0
Pygments                              2.18.0
pygranta                              2024.2.0
pyiges                                0.3.1
PyJWT                                 2.9.0
pylint                                3.2.7
pylint-venv                           3.0.3
pyls-spyder                           0.4.0
PyMySQL                               1.1.1
PyNaCl                                1.5.0
pyparsing                             3.1.4
pypiwin32                             223
pypng                                 0.20220715.0
pyproject_hooks                       1.1.0
PyQt5                                 5.15.11
PyQt5-Qt5                             5.15.2
PyQt5_sip                             12.15.0
PyQtWebEngine                         5.15.7
PyQtWebEngine-Qt5                     5.15.2
pyre-extensions                       0.0.31
pyro-api                              0.1.2
pyro-ppl                              1.9.1
Pyro5                                 5.15
pyspnego                              0.11.1
python-dateutil                       2.9.0.post0
python-dotenv                         1.0.1
python-lsp-black                      2.0.0
python-lsp-jsonrpc                    1.1.2
python-lsp-server                     1.12.0
python-slugify                        8.0.4
pytomlpp                              1.0.13
pytoolconfig                          1.3.1
pytwin                                0.7.0
pytz                                  2024.2
pyuca                                 1.2
pyvista                               0.43.4
pyviz_comms                           3.0.3
pywin32                               306
pywin32-ctypes                        0.2.3
PyYAML                                6.0.2
pyzmq                                 26.2.0
QDarkStyle                            3.2.3
qstylizer                             0.2.3
QtAwesome                             1.3.1
qtconsole                             5.6.0
QtPy                                  2.4.1
readme_renderer                       44.0
referencing                           0.35.1
regex                                 2024.7.24
requests                              2.32.3
requests-negotiate-sspi               0.5.2
requests_ntlm                         1.3.0
requests-toolbelt                     1.0.0
rfc3986                               2.0.0
rich                                  13.8.1
rope                                  1.13.0
rpds-py                               0.20.0
rpyc                                  6.0.0
rsa                                   4.9
Rtree                                 1.3.0
scikit-learn                          1.5.2
scikit-rf                             1.3.0
scipy                                 1.14.1
scooby                                0.10.0
semver                                3.0.2
serpent                               1.41
setuptools                            65.5.0
shutils                               0.1.0
six                                   1.16.0
sniffio                               1.3.1
snowballstemmer                       2.2.0
sortedcontainers                      2.4.0
soupsieve                             2.6
Sphinx                                8.0.2
sphinxcontrib-applehelp               2.0.0
sphinxcontrib-devhelp                 2.0.0
sphinxcontrib-htmlhelp                2.1.0
sphinxcontrib-jsmath                  1.0.1
sphinxcontrib-qthelp                  2.0.0
sphinxcontrib-serializinghtml         2.0.0
spyder                                6.0.0
spyder-kernels                        3.0.0
sqlparse                              0.5.1
sseclient-py                          1.8.0
sspilib                               0.1.0
stack-data                            0.6.3
superqt                               0.6.7
sympy                                 1.13.3
tabulate                              0.9.0
tenacity                              9.0.0
text-unidecode                        1.3
textdistance                          4.6.3
threadpoolctl                         3.5.0
three-merge                           0.1.1
tinycss2                              1.3.0
toml                                  0.10.2
tomli                                 2.0.1
tomlkit                               0.13.2
torch                                 2.4.1
tornado                               6.4.1
tqdm                                  4.66.5
traitlets                             5.14.3
trame                                 3.6.5
trame-client                          3.2.5
trame-server                          3.1.2
trame-vtk                             2.8.10
trame-vuetify                         2.7.1
twine                                 5.1.1
typeguard                             2.13.3
types-python-dateutil                 2.9.0.20240906
typing_extensions                     4.12.2
typing-inspect                        0.9.0
tzdata                                2024.1
uc-micro-py                           1.0.3
ujson                                 5.10.0
uritemplate                           4.1.1
urllib3                               1.26.20
vtk                                   9.3.1
wakepy                                0.9.1
watchdog                              5.0.2
wcwidth                               0.2.13
webencodings                          0.5.1
websockets                            13.0.1
whatthepatch                          1.0.6
widgetsnbextension                    4.0.13
WMI                                   1.5.1
wrapt                                 1.16.0
wslink                                2.1.3
xmlschema                             2.5.1
xyzservices                           2024.9.0
yapf                                  0.40.2
yarl                                  1.11.1
zipp                                  3.20.1
zope.interface                        7.0.3
dipinknair commented 1 month ago

Hi @jorzig1990 , Thanks for opening the issue. PyMechanical embedding will not allow to create multiple app in same script. but if you want to work in a new session then you can use app.new() which clear current session and create new session of Mechanical ( similar to File->New in Mechanical) . You can use like below. In this case you don't have to start another app or close the one that is created, instead use the one already created.

import ansys.mechanical.core as mech

print()
print("Run 1")
print()

app = mech.App(version=242)
app.update_globals(globals())
print("AnsysVersion:")
print(app)

DataModel = app.DataModel
ExtAPI = app.ExtAPI
Model = app.DataModel.Project.Model
Tree = app.DataModel.Tree
Graphics = app.ExtAPI.Graphics
Materials = DataModel.Project.Model.Materials

print()
print("Run 2")
print()

app.new()
print("AnsysVersion:")
print(app)

DataModel = app.DataModel
ExtAPI = app.ExtAPI
Model = app.DataModel.Project.Model
Tree = app.DataModel.Tree
Graphics = app.ExtAPI.Graphics
Materials = DataModel.Project.Model.Materials
jorzig1990 commented 1 month ago

Hi @dipinknair,

Thank you for the support.

Is there a solution to reconnect or restart with the app, if I have lost the license connection or it crashed for some other reason? Your solution works fine as long I don't have a problem with the app itself.

dipinknair commented 1 month ago

The app created in python which only stays till it crashes. No other way to connect it. You can always save your project ( .mechdb/ .mechdat) in between to make sure you can use that if your workflow crashes.