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)
"""Script generated by ansys-mapdl-core version 0.70.dev0"""

from ansys.mapdl.core import launch_mapdl
mapdl = launch_mapdl(loglevel="WARNING", print_com=True, check_parameter_names=False)

mapdl.com("ANSYS MEDIA REL. 2023R2 (05/12/2023) REF. VERIF. MANUAL: REL. 2023R2")
mapdl.run("FINISH")
mapdl.run("/VERIFY,VM45")
mapdl.prep7()
mapdl.title("VM45, NATURAL FREQUENCY OF A SPRING-MASS SYSTEM")
mapdl.com("VIBRATION THEORY AND APPLICATIONS, THOMSON, 2ND PRINTING, PAGE 6, EX. 1.2-2")
mapdl.antype("MODAL")
mapdl.modopt("LANB", 1)
mapdl.et(1, "COMBIN14", "", "", 2)  # TWO-DIMENSIONAL LONGITUDINAL SPRING
mapdl.et(2, "MASS21", "", "", 4)  # TWO-DIMENSIONAL MASS
mapdl.r(1, 48)
mapdl.r(2, .006477)
mapdl.n(1)
mapdl.n(2, "", 1)
mapdl.e(1, 2)
mapdl.type(2)
mapdl.real(2)
mapdl.e(2)
mapdl.outpr("ALL", 1)
mapdl.outres("ALL", 0)
mapdl.d(1, "ALL")
mapdl.d(2, "UX")
mapdl.finish()
mapdl.slashsolu()
mapdl.solve()
mapdl.get("FREQ", "MODE", 1, "FREQ")
mapdl.dim("LABEL", "CHAR", 1, 2)
mapdl.dim("VALUE", "", 1, 3)
mapdl.run("LABEL(1,1) = '      F,'")
mapdl.run("LABEL(1,2) = ' (Hz)   '")
mapdl.vfill("VALUE(1,1)", "DATA", 13.701)
mapdl.vfill("VALUE(1,2)", "DATA", "FREQ")
mapdl.vfill("VALUE(1,3)", "DATA", "ABS(FREQ/13.701)")
mapdl.com("")
with mapdl.non_interactive:
    mapdl.run("/OUT,vm45,vrt")
    mapdl.com("------------------- VM45 RESULTS COMPARISON ---------------")
    mapdl.com("")
    mapdl.com("|   TARGET   |   Mechanical APDL   |   RATIO")
    mapdl.com("")
    mapdl.run("*VWRITE,LABEL(1,1),LABEL(1,2),VALUE(1,1),VALUE(1,2),VALUE(1,3)")
    mapdl.run("(1X,A8,A8,'   ',F10.3,'  ',F14.3,'   ',1F15.3)")
    mapdl.com("-----------------------------------------------------------")

    mapdl.run("/OUT")
    mapdl.run("/GOPR")
    mapdl.finish()
    mapdl.starlist("vm45", "vrt")


mapdl.exit()

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)
"""Script generated by ansys-mapdl-core version 0.70.dev0"""

from ansys.mapdl.core import launch_mapdl
mapdl = launch_mapdl(loglevel="WARNING", print_com=True, check_parameter_names=False)
mapdl.clear() # Clearing session

mapdl.com("ANSYS MEDIA REL. 2023R2 (05/12/2023) REF. VERIF. MANUAL: REL. 2023R2")
mapdl.run("FINISH")
mapdl.run("/VERIFY,VM45")
mapdl.prep7()
mapdl.title("VM45, NATURAL FREQUENCY OF A SPRING-MASS SYSTEM")
mapdl.com("VIBRATION THEORY AND APPLICATIONS, THOMSON, 2ND PRINTING, PAGE 6, EX. 1.2-2")
mapdl.antype("MODAL")
mapdl.modopt("LANB", 1)
mapdl.et(1, "COMBIN14", "", "", 2)  # TWO-DIMENSIONAL LONGITUDINAL SPRING
mapdl.et(2, "MASS21", "", "", 4)  # TWO-DIMENSIONAL MASS
mapdl.r(1, 48)
mapdl.r(2, .006477)
mapdl.n(1)
mapdl.n(2, "", 1)
mapdl.e(1, 2)
mapdl.type(2)
mapdl.real(2)
mapdl.e(2)
mapdl.outpr("ALL", 1)
mapdl.outres("ALL", 0)
mapdl.d(1, "ALL")
mapdl.d(2, "UX")
mapdl.finish()
mapdl.slashsolu()
mapdl.solve()
mapdl.get("FREQ", "MODE", 1, "FREQ")
mapdl.dim("LABEL", "CHAR", 1, 2)
mapdl.dim("VALUE", "", 1, 3)
mapdl.run("LABEL(1,1) = '      F,'")
mapdl.run("LABEL(1,2) = ' (Hz)   '")
mapdl.vfill("VALUE(1,1)", "DATA", 13.701)
mapdl.vfill("VALUE(1,2)", "DATA", "FREQ")
mapdl.vfill("VALUE(1,3)", "DATA", "ABS(FREQ/13.701)")
mapdl.com("")
with mapdl.non_interactive:
    mapdl.run("/OUT,vm45,vrt")
    mapdl.com("------------------- VM45 RESULTS COMPARISON ---------------")
    mapdl.com("")
    mapdl.com("|   TARGET   |   Mechanical APDL   |   RATIO")
    mapdl.com("")
    mapdl.run("*VWRITE,LABEL(1,1),LABEL(1,2),VALUE(1,1),VALUE(1,2),VALUE(1,3)")
    mapdl.run("(1X,A8,A8,'   ',F10.3,'  ',F14.3,'   ',1F15.3)")
    mapdl.com("-----------------------------------------------------------")

    mapdl.run("/OUT")
    mapdl.run("/GOPR")
    mapdl.finish()
    mapdl.starlist("vm45", "vrt")


mapdl.exit()

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))
"""Script generated by ansys-mapdl-core version 0.70.dev0"""

from ansys.mapdl.core import launch_mapdl
mapdl = launch_mapdl(loglevel="WARNING", print_com=True)
mapdl.clear() # Clearing session

mapdl.com("ANSYS MEDIA REL. 2023R2 (05/12/2023) REF. VERIF. MANUAL: REL. 2023R2")
mapdl.run("FINISH")
mapdl.run("/VERIFY,VM45")
mapdl.prep7()
mapdl.title("VM45, NATURAL FREQUENCY OF A SPRING-MASS SYSTEM")
mapdl.com("VIBRATION THEORY AND APPLICATIONS, THOMSON, 2ND PRINTING, PAGE 6, EX. 1.2-2")
mapdl.antype("MODAL")
mapdl.modopt("LANB", 1)
mapdl.et(1, "COMBIN14", "", "", 2)  # TWO-DIMENSIONAL LONGITUDINAL SPRING
mapdl.et(2, "MASS21", "", "", 4)  # TWO-DIMENSIONAL MASS
mapdl.r(1, 48)
mapdl.r(2, .006477)
mapdl.n(1)
mapdl.n(2, "", 1)
mapdl.e(1, 2)
mapdl.type(2)
mapdl.real(2)
mapdl.e(2)
mapdl.outpr("ALL", 1)
mapdl.outres("ALL", 0)
mapdl.d(1, "ALL")
mapdl.d(2, "UX")
mapdl.finish()
mapdl.slashsolu()
mapdl.solve()
mapdl.get("FREQ", "MODE", 1, "FREQ")
mapdl.dim("LABEL", "CHAR", 1, 2)
mapdl.dim("VALUE", "", 1, 3)
mapdl.run("LABEL(1,1) = '      F,'")
mapdl.run("LABEL(1,2) = ' (Hz)   '")
mapdl.vfill("VALUE(1,1)", "DATA", 13.701)
mapdl.vfill("VALUE(1,2)", "DATA", "FREQ")
mapdl.vfill("VALUE(1,3)", "DATA", "ABS(FREQ/13.701)")
mapdl.com("")
with mapdl.non_interactive:
    mapdl.run("/OUT,vm45,vrt")
    mapdl.com("------------------- VM45 RESULTS COMPARISON ---------------")
    mapdl.com("")
    mapdl.com("|   TARGET   |   Mechanical APDL   |   RATIO")
    mapdl.com("")
    mapdl.run("*VWRITE,LABEL(1,1),LABEL(1,2),VALUE(1,1),VALUE(1,2),VALUE(1,3)")
    mapdl.run("(1X,A8,A8,'   ',F10.3,'  ',F14.3,'   ',1F15.3)")
    mapdl.com("-----------------------------------------------------------")

    mapdl.run("/OUT")
    mapdl.run("/GOPR")
    mapdl.finish()
    mapdl.starlist("vm45", "vrt")

mapdl.exit()

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.

$ 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:

$ echo -e "/prep7\nblock,0,1,0,1,0,1" | pymapdl convert -oc
mapdl.prep7()
mapdl.block(0, 1, 0, 1, 0, 1)

Total running time of the script: (0 minutes 0.066 seconds)