Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
122c02f
Created self-contained apdl/pymapdl conversion ex
jgd10 Apr 14, 2025
b57efec
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] Apr 14, 2025
2069357
chore: adding changelog file 3840.documentation.md [dependabot-skip]
pyansys-ci-bot Apr 14, 2025
12fae1c
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] Apr 14, 2025
bbc635d
chore: adding changelog file 3840.documentation.md [dependabot-skip]
pyansys-ci-bot Apr 14, 2025
cd47afa
removed trailing whitespace
jgd10 Apr 14, 2025
234cd0c
Update examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
jgd10 Apr 14, 2025
3659e21
Update examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
jgd10 Apr 14, 2025
031c2f9
Update examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
jgd10 Apr 14, 2025
7da9398
added section about CLI
jgd10 Apr 14, 2025
07be8a6
Merge branch 'doc/example-apdl-to-pymapdl' of https://github.com/ansy…
jgd10 Apr 14, 2025
d80d652
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] Apr 14, 2025
cfbf7cc
Merge branch 'main' of https://github.com/ansys/pymapdl into doc/exam…
jgd10 Apr 15, 2025
47129a9
Merge branch 'doc/example-apdl-to-pymapdl' of https://github.com/ansy…
jgd10 Apr 15, 2025
d17899d
Merge branch 'main' of https://github.com/ansys/pymapdl into doc/exam…
jgd10 Apr 15, 2025
fb85a1a
fix: correct command line interface section header formatting
germa89 Apr 15, 2025
f401913
Merge branch 'main' of https://github.com/ansys/pymapdl into doc/exam…
jgd10 Apr 16, 2025
6dc1a7b
Merge branch 'doc/example-apdl-to-pymapdl' of https://github.com/ansy…
jgd10 Apr 16, 2025
0c7d9f4
Update examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
jgd10 Apr 16, 2025
862f210
Apply suggestions from code review
jgd10 Apr 16, 2025
6794ab1
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] Apr 16, 2025
aff35d6
fixed one stray function/method confusion
jgd10 Apr 16, 2025
ca41b9e
fix: title format
germa89 Apr 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/3840.documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docs: create self-contained apdl/pymapdl conversion example
161 changes: 161 additions & 0 deletions examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# 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.

"""
.. _ref_converting_apdl_to_pymapdl_example:

==================================
Converting APDL scripts to PyMAPDL
==================================

PyMAPDL offers two functions for converting APDL scripts to PyMAPDL scripts,

* ``convert_apdl_block()``, which works with strings
* ``convert_script()``, which works with files

This example converts a modified version of the APDL verification example 45.
"""

import tempfile

from ansys.mapdl.core import convert_apdl_block, convert_script

apdl_script = """
/COM,ANSYS MEDIA REL. 2023R2 (05/12/2023) REF. VERIF. MANUAL: REL. 2023R2
/VERIFY,VM45
/PREP7
/TITLE, VM45, NATURAL FREQUENCY OF A SPRING-MASS SYSTEM
C*** VIBRATION THEORY AND APPLICATIONS, THOMSON, 2ND PRINTING, PAGE 6, EX. 1.2-2
ANTYPE,MODAL
MODOPT,LANB,1
ET,1,COMBIN14,,,2 ! TWO-DIMENSIONAL LONGITUDINAL SPRING
ET,2,MASS21,,,4 ! TWO-DIMENSIONAL MASS
R,1,48
R,2,.006477
N,1
N,2,,1
E,1,2
TYPE,2
REAL,2
E,2
OUTPR,ALL,1
OUTRES,ALL,0
D,1,ALL
D,2,UX
FINISH
/SOLU
SOLVE
*GET,FREQ,MODE,1,FREQ
*DIM,LABEL,CHAR,1,2
*DIM,VALUE,,1,3
LABEL(1,1) = ' F,'
LABEL(1,2) = ' (Hz) '
*VFILL,VALUE(1,1),DATA,13.701
*VFILL,VALUE(1,2),DATA,FREQ
*VFILL,VALUE(1,3),DATA,ABS(FREQ/13.701)
/COM
/OUT,vm45,vrt
/COM,------------------- VM45 RESULTS COMPARISON ---------------
/COM,
/COM, | TARGET | Mechanical APDL | RATIO
/COM,
*VWRITE,LABEL(1,1),LABEL(1,2),VALUE(1,1),VALUE(1,2),VALUE(1,3)
(1X,A8,A8,' ',F10.3,' ',F14.3,' ',1F15.3)
/COM,-----------------------------------------------------------

/OUT
FINISH
*LIST,vm45,vrt
"""


###############################################################################
# Convert a string inline
# ~~~~~~~~~~~~~~~~~~~~~~~
# Calling the ``convert_apdl_block()`` function converts the supplied string to a list of translated
# lines of code.
#

result = convert_apdl_block(apdl_script)
print(result)

###############################################################################
# Quality of Life kwargs
# ~~~~~~~~~~~~~~~~~~~~~~
# This function also includes several kwargs that cover common use cases when converting
# from APDL to PyMAPDL, such as adding the necessary Python imports when ``add_imports``
# is set to ``True`` or adding the ``mapdl.exit()`` command to the end when ``auto_exit``
# is set to ``True``.
#
# Some of the most useful kwargs follow.
#
# * ``only_commands``: Convert the commands without adding any boilerplate such as ``mapdl=launch...`` or ``mapdl.exit``.
# * ``print_com``: Change ``/COM`` commands to ``print()`` commands.
# * ``clear_at_start``: Call the ``mapdl.clear()`` method after the `launch_mapdl`` function.
# * ``add_imports`` Add Python import lines at the start of the script.
# * ``auto_exit``: If ``True``, append the ``mapdl.exit()`` method to the end of the file.
# * ``cleanup_output``: If ``True``, format output using ``autopep8`` (if you have this Python package installed).
#

result = convert_apdl_block(
apdl_script, print_com=True, clear_at_start=True, add_imports=True, auto_exit=True
)
print(result)

###############################################################################
# Convert from a file
# ~~~~~~~~~~~~~~~~~~~~
# The ``convert_script`` function provides all the same functionality
# but converts from a file to a list of translated strings. Additionally,
# it provides an option for saving the result to a file automatically, which
# the ``convert_apdl_block()`` function does not provide.
#

new_file, filename = tempfile.mkstemp(suffix=".inp")
with open(filename, "w") as f:
f.write(apdl_script)
result = convert_script(
filename, print_com=True, clear_at_start=True, add_imports=True, auto_exit=True
)
print("\n".join(result))


###############################################################################
# Command-line interface
# ~~~~~~~~~~~~~~~~~~~~~~
# You can access the ``convert_script`` function in the terminal using a CLI
# (command-line interface). Assuming a virtual environment is activated,
# you can use the following command to convert a file named ``mapdl.in``
# to a ``mapdl.out`` file.
#
# .. code:: console
#
# $ pymapdl convert -f mapdl.dat --print_com --clear_at_start --add_imports --auto_exit --output mapdl.out
#
# You can even build the input on the fly using the ``echo`` command:
#
# .. code:: console
#
# $ echo -e "/prep7\nblock,0,1,0,1,0,1" | pymapdl convert -oc
# mapdl.prep7()
# mapdl.block(0, 1, 0, 1, 0, 1)
#