Skip to content

Commit 0126ad9

Browse files
authored
Fix MPREAD not having effect (#977)
* Improving docstring * Improving MPWRITE docstring * Adding wrapper to mpread and mpwrite * Moving math._load_file to misc.load_file * Adding unit tests * Masking input inside MPREAD. * Patching mpread * Adding unit tests * Fixin unit test * Fixing not implemented error * Extending coverage
1 parent 63b4bd5 commit 0126ad9

File tree

5 files changed

+121
-60
lines changed

5 files changed

+121
-60
lines changed

src/ansys/mapdl/core/_commands/preproc/materials.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -691,12 +691,12 @@ def mpread(self, fname="", ext="", lib="", **kwargs):
691691
----------
692692
fname
693693
File name and directory path (248 characters maximum,
694-
including directory). If you do not specify the LIB
694+
including directory). If you do not specify the ``LIB``
695695
option, the default directory is the current working
696-
directory. If you specify the LIB option, the default is
696+
directory. If you specify the ``LIB`` option, the default is
697697
the following search path: the current working directory,
698-
the user's home directory, MPLIB_DIR (as specified by the
699-
/MPLIB,READ,PATH command) and /ansys_dir/matlib (as
698+
the user's home directory, ``MPLIB_DIR`` (as specified by the
699+
``/MPLIB,READ,PATH`` command) and ``/ansys_dir/matlib`` (as
700700
defined by installation). If you use the default for your
701701
directory, you can use all 248 characters for the file
702702
name.
@@ -706,25 +706,25 @@ def mpread(self, fname="", ext="", lib="", **kwargs):
706706
707707
lib
708708
Reads material library files previously written with the
709-
MPWRITE command. (See the description of the LIB option
710-
for the MPWRITE command.) The only allowed value for LIB
711-
is LIB.
709+
MPWRITE command. (See the description of the ``LIB`` option
710+
for the ``MPWRITE`` command.) The only allowed value for ``LIB``
711+
is ``LIB``.
712712
713713
Notes
714714
-----
715-
Material properties written to a file without the LIB option
715+
Material properties written to a file without the ``LIB`` option
716716
do not support nonlinear properties. Also, properties written
717-
to a file without the LIB option are restored in the same
717+
to a file without the ``LIB`` option are restored in the same
718718
material number as originally defined. To avoid errors, use
719-
MPREAD with the LIB option only when reading files written
720-
using MPWRITE with the LIB option.
719+
``MPREAD`` with the ``LIB`` option only when reading files written
720+
using MPWRITE with the ``LIB`` option.
721721
722-
If you omit the LIB option for MPREAD, this command supports
722+
If you omit the ``LIB`` option for ``MPREAD``, this command supports
723723
only linear properties.
724724
725725
Material numbers are hardcoded. If you write a material file
726-
without specifying the LIB option, then read that file in
727-
using the MPREAD command with the LIB option, the ANSYS
726+
without specifying the ``LIB`` option, then read that file in
727+
using the ``MPREAD`` command with the ``LIB`` option, the ANSYS
728728
program will not write the file to a new material number.
729729
Instead, it will write the file to the "old" material number
730730
(the number specified on the MPWRITE command that created the
@@ -855,9 +855,9 @@ def mpwrite(self, fname="", ext="", lib="", mat="", **kwargs):
855855
----------
856856
fname
857857
File name and directory path (248 characters maximum, including
858-
directory). If you do not specify the LIB option, the default
859-
directory is the current working directory. If you specify LIB and
860-
you have specified a material library directory (via the /MPLIB
858+
directory). If you do not specify the ``LIB`` option, the default
859+
directory is the current working directory. If you specify ``LIB`` and
860+
you have specified a material library directory (via the ``/MPLIB``
861861
command), that directory is the default. Otherwise, the default is
862862
the current working directory. If you use the default for your
863863
directory, you can use all 248 characters for the file name.
@@ -867,18 +867,18 @@ def mpwrite(self, fname="", ext="", lib="", mat="", **kwargs):
867867
ext
868868
Filename extension (eight-character maximum).
869869
870-
If you omit the LIB option, the default extension is
871-
MP. If you specify the LIB option, the default extension
870+
If you omit the ``LIB`` option, the default extension is
871+
MP. If you specify the ``LIB`` option, the default extension
872872
is units_MPL, where units is the system of units currently
873-
in use. (See the description of the /UNITS command.) For
874-
example, if /UNITS is set to BIN, the extension defaults
873+
in use. (See the description of the ``/UNITS`` command.) For
874+
example, if ``/UNITS`` is set to BIN, the extension defaults
875875
to BIN_MPL.
876876
877877
lib
878-
The only value allowed for this field is the string "LIB."
878+
The only value allowed for this field is the string ``"LIB"``.
879879
880-
The LIB option indicates that you wish to have properties
881-
associated with the material (MAT) written to the
880+
The ``LIB`` option indicates that you wish to have properties
881+
associated with the material (``MAT``) written to the
882882
specified material library file using the material library
883883
file format. The material library file format is
884884
ASCII-text-based ANSYS command input. Certain commands
@@ -888,15 +888,15 @@ def mpwrite(self, fname="", ext="", lib="", mat="", **kwargs):
888888
file independent of the material number in effect when the
889889
file was written; this enables you to restore the
890890
properties into the ANSYS database using the material
891-
number of your choice. The LIB option also enables you to
891+
number of your choice. The ``LIB`` option also enables you to
892892
save both linear and nonlinear properties. If you omit the
893-
LIB option, you can save linear properties only.
893+
``LIB`` option, you can save linear properties only.
894894
895895
mat
896896
Specifies the material to be written to the named material library
897897
file. There is no default; you must either specify a material or
898-
omit the MAT argument. Even if you specify a MAT value, the ANSYS
899-
program ignores it if the LIB argument is not specified.
898+
omit the ``MAT`` argument. Even if you specify a ``MAT`` value, the ANSYS
899+
program ignores it if the ``LIB`` argument is not specified.
900900
901901
Notes
902902
-----

src/ansys/mapdl/core/mapdl.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from ansys.mapdl.core.inline_functions import Query
3131
from ansys.mapdl.core.misc import (
3232
last_created,
33+
load_file,
3334
random_string,
3435
run_as_prep7,
3536
supress_logging,
@@ -2890,3 +2891,15 @@ def _check_parameter_name(self, param_name):
28902891
"Hence its use is not recommended outside them."
28912892
"You might run in unexpected behaviours, for example, parameters not being show in `mapdl.parameters`."
28922893
)
2894+
2895+
@wraps(Commands.mpread)
2896+
def mpread(self, fname="", ext="", lib="", **kwargs):
2897+
if lib:
2898+
raise NotImplementedError(
2899+
"The option 'lib' is not supported by the MAPDL gRPC server."
2900+
)
2901+
2902+
fname_ = fname + "." + ext
2903+
fname = load_file(self, fname_)
2904+
self._log.info("Bypassing 'MPREAD' with 'INPUT'.")
2905+
return self.input(fname)

src/ansys/mapdl/core/math.py

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from ansys.api.mapdl.v0 import mapdl_pb2 as pb_types
1212
import numpy as np
1313

14+
from ansys.mapdl.core.misc import load_file
15+
1416
from .check_version import VersionError, meets_version, version_requires
1517
from .common_grpc import ANSYS_VALUE_TYPE, DEFAULT_CHUNKSIZE, DEFAULT_FILE_CHUNK_SIZE
1618
from .errors import ANSYSDataTypeError, protect_grpc
@@ -523,38 +525,7 @@ def _load_file(self, fname):
523525
If the file is local, it will be uploaded.
524526
525527
"""
526-
if self._mapdl._local: # pragma: no cover
527-
base_fname = os.path.basename(fname)
528-
if not os.path.exists(fname) and base_fname not in self._mapdl.list_files():
529-
raise FileNotFoundError(
530-
f"The file {fname} could not be found in the Python working directory ('{os.getcwd()}')"
531-
f"nor in the MAPDL working directory ('{self._mapdl.directory}')."
532-
)
533-
534-
elif os.path.exists(fname) and base_fname in self._mapdl.list_files():
535-
warn(
536-
f"The file '{base_fname} is present in both, the python working directory ('{os.getcwd()}')"
537-
"and in the MAPDL working directory ('{self._mapdl.directory}'). "
538-
"Using the one in the MAPDL directory.\n"
539-
"If you prefer to use the file in the Python directory, you can use `mapdl.upload` before this command to upload it."
540-
)
541-
542-
elif os.path.exists(fname) and base_fname not in self._mapdl.list_files():
543-
self._mapdl.upload(fname)
544-
545-
elif not os.path.exists(fname) and base_fname in self._mapdl.list_files():
546-
pass
547-
548-
else:
549-
if not os.path.exists(fname) and fname not in self._mapdl.list_files():
550-
raise FileNotFoundError(
551-
f"The file {fname} could not be found in the local client or remote working directory."
552-
)
553-
if os.path.exists(fname):
554-
self._mapdl.upload(fname)
555-
556-
# Simplifying name for MAPDL reads it.
557-
return os.path.basename(fname)
528+
return load_file(self._mapdl, fname)
558529

559530
def stiff(self, dtype=np.double, fname="file.full", asarray=False):
560531
"""Load the stiffness matrix from a full file.

src/ansys/mapdl/core/misc.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,49 @@ def wrapper(*args, **kwargs):
332332
func(*args, **kwargs)
333333

334334
return wrapper
335+
336+
337+
def load_file(mapdl, fname):
338+
"""
339+
Provide a file to the MAPDL instance.
340+
341+
If in local:
342+
Checks if the file exists, if not, it raises a ``FileNotFound`` exception
343+
344+
If in not-local:
345+
Check if the file exists locally or in the working directory, if not, it will raise a ``FileNotFound`` exception.
346+
If the file is local, it will be uploaded.
347+
348+
"""
349+
if mapdl._local: # pragma: no cover
350+
base_fname = os.path.basename(fname)
351+
if not os.path.exists(fname) and base_fname not in mapdl.list_files():
352+
raise FileNotFoundError(
353+
f"The file {fname} could not be found in the Python working directory ('{os.getcwd()}')"
354+
f"nor in the MAPDL working directory ('{mapdl.directory}')."
355+
)
356+
357+
elif os.path.exists(fname) and base_fname in mapdl.list_files():
358+
warn(
359+
f"The file '{base_fname} is present in both, the python working directory ('{os.getcwd()}')"
360+
f"and in the MAPDL working directory ('{mapdl.directory}'). "
361+
"Using the one in the MAPDL directory.\n"
362+
"If you prefer to use the file in the Python directory, you can use `mapdl.upload` before this command to upload it."
363+
)
364+
365+
elif os.path.exists(fname) and base_fname not in mapdl.list_files():
366+
mapdl.upload(fname)
367+
368+
elif not os.path.exists(fname) and base_fname in mapdl.list_files():
369+
pass
370+
371+
else:
372+
if not os.path.exists(fname) and fname not in mapdl.list_files():
373+
raise FileNotFoundError(
374+
f"The file {fname} could not be found in the local client or remote working directory."
375+
)
376+
if os.path.exists(fname):
377+
mapdl.upload(fname)
378+
379+
# Simplifying name for MAPDL reads it.
380+
return os.path.basename(fname)

tests/test_mapdl.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,3 +1467,34 @@ def test_seltol(mapdl, value):
14671467
assert "SELECT TOLERANCE=" in mapdl.seltol(value)
14681468
else:
14691469
assert "SELECT TOLERANCE SET TO DEFAULT" == mapdl.seltol(value)
1470+
1471+
1472+
def test_mpfunctions(mapdl, cube_solve, capsys):
1473+
mapdl.prep7()
1474+
1475+
# check writing to file
1476+
fname = "test"
1477+
ext = "mp1"
1478+
1479+
mapdl.mpwrite(fname, ext)
1480+
assert f"{fname}.{ext}" in mapdl.list_files()
1481+
1482+
nuxy = 0.3
1483+
ex = 0.2100000e12
1484+
1485+
# Reding file in remote
1486+
mapdl.clear()
1487+
mapdl.prep7()
1488+
output = mapdl.mpread(fname, ext)
1489+
assert "PROPERTY TEMPERATURE TABLE NUM. TEMPS= 1" in output
1490+
assert "TEMPERATURE TABLE ERASED." in output
1491+
assert np.allclose(mapdl.get_value("NUXY", "1", "TEMP", 0), nuxy)
1492+
assert np.allclose(mapdl.get_value("EX", 1, "TEMP", 0), ex)
1493+
1494+
# Test non-existing file
1495+
with pytest.raises(FileNotFoundError):
1496+
mapdl.mpread(fname="dummy", ext="dummy")
1497+
1498+
# Test not implemented error
1499+
with pytest.raises(NotImplementedError):
1500+
mapdl.mpread(fname="dummy", ext="dummy", lib="something")

0 commit comments

Comments
 (0)