ansys / pymapdl

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

/INPUT bugged in PyMAPDL #1749

Closed iceblink-ansys closed 1 year ago

iceblink-ansys commented 1 year ago

Before submitting the issue

Description of the bug

There are several things wrong with implementation of /INPUT in PyMAPDL:

Steps To Reproduce

In normal mapdl:

/POST1
/INPUT,FAT_NS,cdb

This returns

 /INPUT FILE= FAT_NS.cdb  LINE=       0
 CMBLOCK read of NODE component FAT_NS    completed

In PyMapdl:

from ansys.mapdl.core import launch_mapdl
mapdl = launch_mapdl()
mapdl.post1()
mapdl.input(fname='FAT_NS', ext='cdb')

This generates an uncaught exception:

CRITICAL -  -  logging - handle_exception - Uncaught exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/HPC_A/OF_ANSS/Users/iceblink/pyAnsysEnv/lib/python3.7/site-packages/ansys/mapdl/core/_commands/session/run_controls.py", line 269, in input
    return self.run(f"/INPUT,{fname},{ext},{dir},{line},{log}", **kwargs)
  File "/HPC_A/OF_ANSS/Users/iceblink/pyAnsysEnv/lib/python3.7/site-packages/ansys/mapdl/core/mapdl.py", line 2732, in run
    raise MapdlCommandIgnoredError(text)
ansys.mapdl.core.errors.MapdlCommandIgnoredError: File FAT_NS.cdb does not exist.    The /INPUT command is ignored.
 *** WARNING ***                         CP =       0.308   TIME= 12:59:31
 File FAT_NS.cdb does not exist.
  The /INPUT command is ignored.

Ignore these messages by setting allow_ignore=True

When using:

import os
mapdl.input(fname='FAT_NS', ext='cdb', dir=os.getcwd())

the command works as expected

Which Operating System are you using?

Linux

Which Python version are you using?

3.7

PyMAPDL Report

Show the Report! ```text ------------------------------------------------------------------------------- PyMAPDL Software and Environment Report Packages Requirements ********************* Core packages ------------- ansys.mapdl.core : 0.63.4 numpy : 1.21.6 appdirs : 1.4.4 scipy : 1.7.3 grpc : Package not found ansys.api.mapdl.v0 : Package not found ansys.mapdl.reader : 0.52.4 google.protobuf : Package not found pexpect : 4.8.0 Optional packages ----------------- matplotlib : 3.5.3 pyvista : 0.37.0 pyiges : 0.2.1 tqdm : 4.64.1 ansys_corba : 0.1.1 Ansys Installation ****************** Version Location ------------------ 182 /ansys_inc/v182 Ansys Environment Variables *************************** ANSYSLMD_LICENSE_FILE 1055@10.220.11.141:1055@10.220.11.142:1055@129.73.171.37 ANSYSCL_TIMEOUT_CONNECT 300 EBVERSIONANSYS 2022 ANSYSLI_TIMEOUT_CONNECT 300 EBDEVELANSYS /HPC_A/system/eb/common/software/ANSYS/2022R2/easybuild/ANSYS-2022R2-easybuild-devel EBROOTANSYS /HPC_A/system/eb/common/software/ANSYS/2022R2 ```

Installed packages

Show the installed packages! ```text aiohttp==3.8.3 aiosignal==1.3.1 ansys-api-mapdl==0.5.1 ansys-api-platform-instancemanagement==1.0.0b3 ansys-corba==0.1.1 ansys-dpf-core==0.7.1 ansys-dpf-gate==0.3.0 ansys-dpf-gatebin==0.3.0 ansys-dpf-post==0.2.5 ansys-grpc-dpf==0.7.0 ansys-mapdl-core==0.63.4 ansys-mapdl-reader==0.52.4 ansys-platform-instancemanagement==1.0.2 appdirs==1.4.4 async-timeout==4.0.2 asynctest==0.13.0 attrs==22.1.0 cachetools==5.2.0 certifi==2022.12.7 charset-normalizer==2.1.1 cycler==0.11.0 fonttools==4.38.0 frozenlist==1.3.3 geomdl==5.3.1 google-api-core==2.11.0 google-api-python-client==2.70.0 google-auth==2.15.0 google-auth-httplib2==0.1.0 googleapis-common-protos==1.57.0 grpcio==1.51.1 httplib2==0.21.0 idna==3.4 imageio==2.22.4 importlib-metadata==5.1.0 kiwisolver==1.4.4 matplotlib==3.5.3 multidict==6.0.3 numpy==1.21.6 packaging==22.0 pexpect==4.8.0 Pillow==9.3.0 pooch==1.6.0 protobuf==3.20.3 protoc-gen-swagger==0.1.0 psutil==5.9.4 ptyprocess==0.7.0 pyasn1==0.4.8 pyasn1-modules==0.2.8 pyiges==0.2.1 pyparsing==3.0.9 python-dateutil==2.8.2 pyvista==0.37.0 requests==2.28.1 rsa==4.9 scipy==1.7.3 scooby==0.7.0 six==1.16.0 tqdm==4.64.1 typing_extensions==4.4.0 uritemplate==4.1.1 urllib3==1.26.13 vtk==9.2.2 wslink==1.9.2 yarl==1.8.2 zipp==3.11.0 ```

Logger output file

Show the logger output file. ```text Getting 404 error on the link https://mapdl.docs.pyansys.com/troubleshoot/index.html ```
mikerife commented 1 year ago

Hi @iceblink-ansys This is not a bug as the PyMAPDL 'input' is not a direct implementation of the APDL command /INPUT. There are aspects of MAPDL as a Service that will be changing how we can interact with MAPDL that is pretty different than what we (traditional MAPDL users) are used to. This is one of those; if you want the PyMAPDL input command to act like the APDL /INPUT command there is the "orig_cmd" flag that can be set.

If those are really the commands used to launch MAPDL instance (that you gave), then the working directory was a temp/scratch folder and the cdb file was not present in the working directory. You could use the PyMAPDL 'upload' command to copy the file to the MAPDL working directory. https://mapdl.docs.pyansys.com/release/0.63/contribution_and_api/_autosummary/ansys.mapdl.core.mapdl_grpc.MapdlGrpc.upload.html

Mike

iceblink-ansys commented 1 year ago

Hi Mike,

Thanks for the quick reply, as always!

If the command works differently (which I don't mind), then at least the documentation should show how it works?

The cdb file was present in the working directory, which is proven by the command completing succesfully after adding the dir=os.getcwd(). (Maybe the error output could be changed to show where it was searching for the file?) But the remark in the documentation about using CDREAD instead may be applicable here. I'm blindly copying an APDL script, which is maybe not ideal. But I am an APDL novice, so don't want to stray too far off the beaten path.

mikerife commented 1 year ago

Hi @iceblink-ansys By 'working directory' I mean the directory that MAPDL is using to store its files. Which can be different than the folder where we are using PyMAPDL. For a simple test you can run this to verify:

from ansys.mapdl.core import launch_mapdl
mapdl = launch_mapdl()
mapdl.inquire('','directory')

That should return a folder in your TEMP folder (location taken from the TEMP environment variable) with the form "ansys_xxxx" where 'xxxx' is a randomly generated string of characters. Using the 'dir' option like you did allowed the MPADL instance to find the cdb file in the folder you were running PyMAPDL from.

The documentation could definitely be enhanced.

germa89 commented 1 year ago

To be consistent, I think "slashinput" may be a better method name than "input"

The "slash" in the method name is only there when its absence might cause confusion. For example, there is "/solu" and "solu". Because of that, it is needed "/slashsolu". It is difficult to have simple method names, but so far the convention is that one: no use "slash/star" unless it is really needed.

The documentation is not consistent with the Python code.

The function mapdl.input is "overloaded" or extended in the MapdlGrpc class. In fact, in this class, we are not really calling the APDL /input command, rather a gRPC method which does something similar, but not exactly the same. This is why some of the extra keyword being added. You can see the details here: https://github.com/pyansys/pymapdl/blob/8e8ae3f7e9f469d930365f37360566c4a66110c9/src/ansys/mapdl/core/mapdl_grpc.py#L1348-L1358

This is a common approach consistent with good practices I believe. That why also when you use help you get the right method signature.

>>> help(mapdl.input)
Help on method input in module ansys.mapdl.core.mapdl_grpc:

input(fname, verbose=False, progress_bar=False, time_step_stream=None, chunk_size=512, orig_cmd='/INP', write_to_log=True, **kwargs) method of ansys.mapdl.core.mapdl_grpc.MapdlGrpc instance
    Stream a local input file to a remote mapdl instance.
    Stream the response back and deserialize the output.

    Parameters
    ----------
    fname : str
   ...

The documentation does not show what APDL command it is linked to

I agree, this should be better presented.

When using the PyMAPDL input method I get an error when importing a CDB file when not specifying dir=

This is interesting. If the file is in the working directory, it should be automatically uploaded. I should look at this more in detail.

Maybe the error output could be changed to show where it was searching for the file?) But the remark in the documentation about using CDREAD instead may be applicable here.

I agree. Some improvements should be made on this regard.

mapdl.inquire('','directory')

You can also just use mapdl.directory to print the MAPDL working directory or to set it (equivalent to /CWD).

I will work on a better docstring for the mapdl.input function. Thank you @iceblink-ansys

iceblink-ansys commented 1 year ago

Thanks for the replies, interesting stuff. Indeed the mapd.directory is different from os.getcwd(), and points to /tmp/ansys_/ See also https://github.com/pyansys/pymapdl/issues/1163

germa89 commented 1 year ago

@iceblink-ansys

Are you using the grpc mode?? Because I believe you are using corba. Am I right??

Because if you are using corba, there is no mapdl.upload, hence you need to specify the working directory most of the cases (unless it is the MAPDL working directory).

If you use mode='grpc', PyMAPDL will take care of uploading the file to the instance if it is in the working directory.