-
Notifications
You must be signed in to change notification settings - Fork 142
docs: create self-contained apdl/pymapdl conversion example #3840
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
Merged
Merged
Changes from 18 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 b57efec
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] 2069357
chore: adding changelog file 3840.documentation.md [dependabot-skip]
pyansys-ci-bot 12fae1c
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] bbc635d
chore: adding changelog file 3840.documentation.md [dependabot-skip]
pyansys-ci-bot cd47afa
removed trailing whitespace
jgd10 234cd0c
Update examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
jgd10 3659e21
Update examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
jgd10 031c2f9
Update examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
jgd10 7da9398
added section about CLI
jgd10 07be8a6
Merge branch 'doc/example-apdl-to-pymapdl' of https://github.com/ansy…
jgd10 d80d652
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] cfbf7cc
Merge branch 'main' of https://github.com/ansys/pymapdl into doc/exam…
jgd10 47129a9
Merge branch 'doc/example-apdl-to-pymapdl' of https://github.com/ansy…
jgd10 d17899d
Merge branch 'main' of https://github.com/ansys/pymapdl into doc/exam…
jgd10 fb85a1a
fix: correct command line interface section header formatting
germa89 f401913
Merge branch 'main' of https://github.com/ansys/pymapdl into doc/exam…
jgd10 6dc1a7b
Merge branch 'doc/example-apdl-to-pymapdl' of https://github.com/ansy…
jgd10 0c7d9f4
Update examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
jgd10 862f210
Apply suggestions from code review
jgd10 6794ab1
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] aff35d6
fixed one stray function/method confusion
jgd10 ca41b9e
fix: title format
germa89 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
161
examples/02-tips-n-tricks/06-convert-apdl-to-pymapdl.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ================================== | ||
|
|
||
| There are two methods to convert APDL scripts to PyMAPDL ones in PyMAPDL, | ||
|
|
||
| * `convert_apdl_block()` | ||
| * `convert_script()` | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| which work with strings and files respectively. | ||
|
|
||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| This example considers a modified version of the APDL verification example 45 | ||
| as the source to be converted. | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
| 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 'inline' | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # ~~~~~~~~~~~~~~~~ | ||
| # Calling `convert_apdl_block()` converts the supplied string to a list of translated | ||
| # lines of code. | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # | ||
|
|
||
| result = convert_apdl_block(apdl_script) | ||
| print(result) | ||
|
|
||
| ############################################################################### | ||
| # Quality of Life kwargs | ||
| # ~~~~~~~~~~~~~~~~~~~~~~ | ||
germa89 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # 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 an `mapdl.exit()` to the end when `auto_exit` is set to True. | ||
| # Some of the most useful commands are below. | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # | ||
| # * `only_commands` - Convert the commands without adding any boilerplate such as `mapdl=launch...` or `mapdl.exit`. | ||
| # * `print_com` - change `/COM` commands to `print()` | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # * `clear_at_start` - call `mapdl.clear()` after `launch_mapdl` | ||
| # * `add_imports` - add Python import lines at the start of the script | ||
| # * `auto_exit` - When True, this appends a `mapdl.exit()` to the end of the file | ||
| # * `cleanup_output` - when True format output using `autopep8` (if you have it installed) | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # | ||
|
|
||
| result = convert_apdl_block( | ||
| apdl_script, print_com=True, clear_at_start=True, add_imports=True, auto_exit=True | ||
| ) | ||
| print(result) | ||
|
|
||
| ############################################################################### | ||
| # Converting from file | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # ~~~~~~~~~~~~~~~~~~~~ | ||
| # The function `convert_script` is also available and covers all the same functionality | ||
| # but converts from a file to a list of translated strings, and provides the | ||
| # additional option to save the result to file automatically. This functionality | ||
| # is not available in `convert_apdl_block()`. | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # | ||
|
|
||
| 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)) | ||
|
|
||
|
|
||
jgd10 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ############################################################################### | ||
| # Command line interface | ||
| # ~~~~~~~~~~~~~~~~~~~~~~ | ||
| # Additionally the converter is also accessible via the terminal, through a CLI. | ||
| # Assuming a virtual environment is activated the following can by run to convert | ||
| # a file called `mapdl.in` into `mapdl.out`. | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # | ||
| # .. code:: console | ||
| # | ||
| # $ pymapdl convert -f mapdl.dat --print_com --clear_at_start --add_imports --auto_exit --output mapdl.out | ||
| # | ||
| # The input can even be built on the fly using `echo`: | ||
jgd10 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # | ||
| # .. 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) | ||
| # | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.