ansys / pymapdl

Pythonic interface to MAPDL
https://mapdl.docs.pyansys.com
MIT License
424 stars 120 forks source link

Using overloaded operators #1162

Open germa89 opened 2 years ago

germa89 commented 2 years ago

After seeing #1149 , I thought we could use something similar:

'N,1,1,1' > mapdl # instead of mapdl.run('N,1,1,1')

and maybe:

"input.dat" >> mapdl # instead of mapdl.input("input.dat")

I'm not 100% convinced, so some feedback is appreciated.

germa89 commented 2 years ago

Pinging @akaszynski @koubaa @FredAns @pmaroneh for visibility

koubaa commented 2 years ago

@germa89 I definitely will miss being able to write MAPDL commands in a shell style without objects or function invocation.

Why not just go all the way and implement an MAPDL shell in python using cmdloop. I can show you how.

germa89 commented 2 years ago

@koubaa I like the idea of getting rid of mapdl. in each command. I would like to test the idea of a shell like PyMAPDL.

Even more, I guess cmdloop could be also used with a context manager so we can have something like:

with mapdl:
    n(1,1,1,1) #instead of mapdl.n
    e() #instead of mapdl.e()

It think this will be quite useful too.

koubaa commented 2 years ago

@germa89 actually cmdloop is more of an interactive thing that would behave more like this:

>>> from ansys.mapdl.core import launch_mapdl
>>> mapdl = launch_mapdl()
>>> mapdl.interpreter()
APDL > /prep7

 /OUTPUT FILE= anstmp
*** MAPDL - ENGINEERING ANALYSIS SYSTEM  RELEASE 2023 R1          23.1BETA ***
 Ansys Mechanical Enterprise
 00000000  VERSION=LINUX x64     07:25:41  JUN 01, 2022 CP=     10.118

 ** WARNING: PRE-RELEASE VERSION OF MAPDL 23.1BETA
  ANSYS,INC TESTING IS NOT COMPLETE - CHECK RESULTS CAREFULLY **

          ***** MAPDL ANALYSIS DEFINITION (PREP7) *****
MAPDL > n,1,1,1,1

 /OUTPUT FILE= anstmp
1
MAPDL > exitloop()
>>> mapdl.nlist()
 /OUTPUT FILE= anstmp
LIST ALL SELECTED NODES.   DSYS=      0

 *** MAPDL - ENGINEERING ANALYSIS SYSTEM  RELEASE 2023 R1          23.1BETA ***
 Ansys Mechanical Enterprise
 00000000  VERSION=LINUX x64     14:59:04  MAY 31, 2022 CP=      8.715

 ** WARNING: PRE-RELEASE VERSION OF MAPDL 23.1BETA
  ANSYS,INC TESTING IS NOT COMPLETE - CHECK RESULTS CAREFULLY **

    NODE        X             Y             Z           THXY     THYZ     THZX
        1   1.0000        1.0000        1.0000          0.00     0.00     0.00
koubaa commented 2 years ago

@germa89 Another way closer to what you suggest is to use a contextmanager to override globals during the context

with mapdl.context(globals()):
    n(1,1,1,1)

where mapdl.context is a ContextManager that makes a cached copy of globals(), then adds all commands bound to itself to it. when the context manager exits, it restores the cached globals. this means any variable defined within the context would be removed upon exiting.

FredAns commented 2 years ago

Do we want to be able (optionally) to create a buffer of commands and send them in one call at the end ?

koubaa commented 2 years ago

@FredAns this can be done like this:

buffer = """command1
command2
command3
command4
"""
mapdl.run(buffer)
germa89 commented 2 years ago

@koubaa @FredAns

You can send multiple MAPDL commands by using mapdl.input_strings.

I believe mapdl.run will complain if you have \n in the input.

Additionally, if you want to buffer PyMAPDL commands, and then send them all at once, you can use the non_interactive context manager.

germa89 commented 2 years ago

Pinging @akaszynski for feedback.