Skip to content

Commit 4eea4f1

Browse files
germa89pyansys-ci-botsourcery-ai[bot]
authored
fix: update mpwrite and mpread methods to enforce 'LIB' as the only valid lib argument (#4123)
* fix: update mpwrite and mpread methods to enforce 'LIB' as the only valid lib argument * chore: adding changelog file 4123.miscellaneous.md [dependabot-skip] * Update src/ansys/mapdl/core/mapdl_extended.py Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> * fix: remove unnecessary f-string in mpwrite assertions * Update src/ansys/mapdl/core/mapdl_extended.py * fix: remove tqdm dependency check from test_mpfunctions * feat: enhance mpread command with detailed docstring and implement error handling for unsupported lib argument * Update src/ansys/mapdl/core/mapdl_extended.py * fix: remove unsupported 'lib' argument message and clean up docstring for list method * fix: update mpread method to handle local file paths correctly * fix: remove tqdm dependency check from test_mpfunctions --------- Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
1 parent 6cd4c7a commit 4eea4f1

File tree

4 files changed

+120
-42
lines changed

4 files changed

+120
-42
lines changed

doc/changelog.d/4123.miscellaneous.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix: update mpwrite and mpread methods to enforce 'lib' as the only valid lib argument

src/ansys/mapdl/core/mapdl_extended.py

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
allow_iterables_vmin,
4747
allow_pickable_entities,
4848
check_deprecated_vtk_kwargs,
49-
load_file,
5049
random_string,
5150
requires_graphics,
5251
supress_logging,
@@ -310,7 +309,7 @@ def mpwrite(
310309
self,
311310
fname="",
312311
ext="",
313-
lib="",
312+
lib="LIB",
314313
mat="",
315314
download_file=False,
316315
progress_bar=False,
@@ -324,23 +323,81 @@ def mpwrite(
324323
f"The supplied path {fname_} is not allowed."
325324
)
326325

327-
output = super().mpwrite(fname, ext, lib, mat, **kwargs)
326+
output = super().mpwrite(fname, ext, lib=lib, mat=mat, **kwargs)
328327
if download_file:
329328
self.download(os.path.basename(fname_), progress_bar=progress_bar)
330329

331330
return output
332331

333-
@wraps(_MapdlCore.mpread)
334332
def mpread(self, fname="", ext="", lib="", **kwargs):
333+
"""APDL Command: MPREAD
334+
335+
Reads a file containing material properties.
336+
337+
Parameters
338+
----------
339+
fname
340+
File name and directory path (248 characters maximum,
341+
including directory). If you do not specify the ``LIB``
342+
option, the default directory is the current working
343+
directory. If you specify the ``LIB`` option, the default is
344+
the following search path: the current working directory,
345+
the user's home directory, ``MPLIB_DIR`` (as specified by the
346+
``/MPLIB,READ,PATH`` command) and ``/ansys_dir/matlib`` (as
347+
defined by installation). If you use the default for your
348+
directory, you can use all 248 characters for the file
349+
name.
350+
351+
ext
352+
Filename extension (eight-character maximum).
353+
354+
lib
355+
Reads material library files previously written with the
356+
MPWRITE command. (See the description of the ``LIB`` option
357+
for the ``MPWRITE`` command.) The only allowed value for ``LIB``
358+
is ``LIB``.
359+
360+
.. note:: The argument "LIB" is not supported by the MAPDL gRPC server.
361+
362+
Notes
363+
-----
364+
Material properties written to a file without the ``LIB`` option
365+
do not support nonlinear properties. Also, properties written
366+
to a file without the ``LIB`` option are restored in the same
367+
material number as originally defined. To avoid errors, use
368+
``MPREAD`` with the ``LIB`` option only when reading files written
369+
using MPWRITE with the ``LIB`` option.
370+
371+
If you omit the ``LIB`` option for ``MPREAD``, this command supports
372+
only linear properties.
373+
374+
Material numbers are hardcoded. If you write a material file
375+
without specifying the ``LIB`` option, then read that file in
376+
using the ``MPREAD`` command with the ``LIB`` option, the ANSYS
377+
program will not write the file to a new material number.
378+
Instead, it will write the file to the "old" material number
379+
(the number specified on the MPWRITE command that created the
380+
file.)
381+
382+
This command is also valid in SOLUTION.
383+
"""
335384
if lib:
336385
raise NotImplementedError(
337-
"The option 'lib' is not supported by the MAPDL gRPC server."
386+
"The 'lib' argument is not supported by the MAPDL gRPC server."
338387
)
339388

340-
fname_ = fname + "." + ext
341-
fname = load_file(self, fname_)
342-
self._log.info("Bypassing 'MPREAD' with 'INPUT'.")
343-
return self.input(fname)
389+
fname = self._get_file_name(fname, ext, "mp")
390+
fname = self._get_file_path(fname, kwargs.get("progress_bar", False))
391+
file_, ext_, path_ = self._decompose_fname(fname)
392+
with self.non_interactive:
393+
# Use not interactive to avoid gRPC issues. See #975
394+
if self._local:
395+
# If local, we can use the full path
396+
super().mpread(fname=path_ / file_, ext=ext_, lib=lib, **kwargs)
397+
else:
398+
super().mpread(fname=file_, ext=ext_, lib=lib, **kwargs)
399+
400+
return self.last_response
344401

345402
@wraps(_MapdlCore.cwd)
346403
def cwd(self, *args, **kwargs):
@@ -355,19 +412,6 @@ def cwd(self, *args, **kwargs):
355412

356413
@wraps(_MapdlCore.list)
357414
def list(self, filename, ext=""):
358-
"""Displays the contents of an external, coded file.
359-
360-
APDL Command: ``/LIST``
361-
362-
Parameters
363-
----------
364-
fname : str
365-
File name and directory path. An unspecified directory
366-
path defaults to the working directory.
367-
368-
ext : str, optional
369-
Filename extension
370-
"""
371415
if hasattr(self, "_local"): # gRPC
372416
if not self._local:
373417
return self._download_as_raw(filename).decode()

tests/test_commands.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@ class Test_MAPDL_commands(TestClass):
610610
"lgwrite",
611611
"lplot",
612612
"lsread",
613+
"mpread",
614+
"mpwrite",
613615
"mwrite",
614616
"nplot",
615617
"sys",

tests/test_mapdl.py

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,33 +1492,37 @@ def test_mpfunctions(mapdl, cube_solve, capsys):
14921492
ext = "mp1"
14931493

14941494
mapdl.prep7()
1495+
mapdl.slashdelete(fname, ext) # remove file if it exists
14951496

1496-
assert f"WRITE OUT MATERIAL PROPERTY LIBRARY TO FILE=" in mapdl.mpwrite(fname, ext)
1497+
assert "WRITE OUT MATERIAL PROPERTY" in mapdl.mpwrite(fname, ext, mat=1)
14971498
assert f"{fname}.{ext}" in mapdl.list_files()
14981499

14991500
# asserting downloading
15001501
ext = "mp2"
1501-
assert f"WRITE OUT MATERIAL PROPERTY LIBRARY TO FILE=" in mapdl.mpwrite(
1502-
fname, ext, download_file=True
1502+
mapdl.slashdelete(fname, ext) # remove file if it exists
1503+
1504+
assert "WRITE OUT MATERIAL PROPERTY" in mapdl.mpwrite(
1505+
fname, ext, download_file=True, mat=1
15031506
)
15041507
assert f"{fname}.{ext}" in mapdl.list_files()
15051508
assert os.path.exists(f"{fname}.{ext}")
15061509

15071510
## Checking reading
15081511
# Uploading a local file
15091512
with open(f"{fname}.{ext}", "r") as fid:
1510-
text = fid.read()
1513+
content = fid.read()
15111514

15121515
os.remove(f"{fname}.{ext}") # remove temp file
15131516

15141517
ext = ext + "2"
1518+
mapdl.slashdelete(fname, ext) # remove file if it exists
1519+
15151520
fname_ = f"{fname}.{ext}"
1516-
new_nuxy = "MPDATA,NUXY, 1, 1, 0.4000000E+00,"
1517-
nuxy = float(new_nuxy.split(",")[4])
1521+
nuxy = float(0.40000)
15181522
ex = 0.2100000e12
15191523

15201524
with open(fname_, "w") as fid:
1521-
fid.write(text.replace("MPDATA,NUXY, 1, 1, 0.3000000E+00,", new_nuxy))
1525+
fid.write(content.replace("0.30000", str(nuxy)))
15221526

15231527
# file might be left behind from a previous test
15241528
if fname_ in mapdl.list_files():
@@ -1527,17 +1531,10 @@ def test_mpfunctions(mapdl, cube_solve, capsys):
15271531

15281532
mapdl.clear()
15291533
mapdl.prep7()
1530-
captured = capsys.readouterr() # To flush it
1531-
output = mapdl.mpread(fname, ext)
1534+
_ = capsys.readouterr() # To flush it
1535+
output = mapdl.mpread(fname, ext, progress_bar=True)
15321536
captured = capsys.readouterr()
1533-
if has_dependency("tqdm"):
1534-
# Printing uploading requires tqdm
1535-
assert f"Uploading {fname}.{ext}:" in captured.err
1536-
1537-
assert "PROPERTY TEMPERATURE TABLE NUM. TEMPS= 1" in output
1538-
assert "TEMPERATURE TABLE ERASED." in output
1539-
assert "0.4000000" in output
1540-
# check if materials are read into the db
1537+
15411538
assert mapdl.get_value("NUXY", "1", "TEMP", 0) == nuxy
15421539
assert np.allclose(mapdl.get_value("EX", 1, "TEMP", 0), ex)
15431540

@@ -1551,9 +1548,6 @@ def test_mpfunctions(mapdl, cube_solve, capsys):
15511548
mapdl.clear()
15521549
mapdl.prep7()
15531550
output = mapdl.mpread(fname, ext)
1554-
assert "PROPERTY TEMPERATURE TABLE NUM. TEMPS= 1" in output
1555-
assert "TEMPERATURE TABLE ERASED." in output
1556-
assert "0.4000000" in output
15571551
assert np.allclose(mapdl.get_value("NUXY", "1", "TEMP", 0), nuxy)
15581552
assert np.allclose(mapdl.get_value("EX", 1, "TEMP", 0), ex)
15591553

@@ -1570,6 +1564,43 @@ def test_mpfunctions(mapdl, cube_solve, capsys):
15701564
with pytest.raises(IOError):
15711565
mapdl.mpwrite("/test_dir/test", "mp")
15721566

1567+
mapdl.slashdelete(fname, ext) # remove file if it exists
1568+
1569+
1570+
def test_mpread_lib(mapdl):
1571+
mapdl.input_strings(
1572+
"""
1573+
/prep7
1574+
/units,si
1575+
TB,BH ,_MATL , 1, 20
1576+
TBTEM, 0.00000000 , 1
1577+
TBPT,, 59.5238095 , 0.200000000
1578+
TBPT,, 119.047619 , 0.400000000
1579+
TBPT,, 158.730159 , 0.550000000
1580+
TBPT,, 396.825397 , 1.15000000
1581+
TBPT,, 555.555556 , 1.30000000
1582+
TBPT,, 793.650794 , 1.40000000
1583+
TBPT,, 1587.30159 , 1.55000000
1584+
TBPT,, 3968.25397 , 1.63500000
1585+
TBPT,, 7936.50794 , 1.65500000
1586+
TBPT,, 15873.0159 , 1.67500000
1587+
TBPT,, 31746.0317 , 1.70138960
1588+
TBPT,, 63492.0635 , 1.75000000
1589+
TBPT,, 95238.0952 , 1.79000000
1590+
TBPT,, 190476.190 , 1.90980000
1591+
TBPT,, 285714.286 , 2.02960000
1592+
TBPT,, 380952.381 , 2.14950000
1593+
"""
1594+
)
1595+
mapdl.slashdelete("database", "mp")
1596+
mapdl.mpwrite("database", "mp", mat=1)
1597+
1598+
mapdl.clear()
1599+
mapdl.prep7()
1600+
mapdl.mpread("database", "mp")
1601+
mapdl.mplist()
1602+
assert mapdl.get_value("MAT", 0, "count") == 1.0
1603+
15731604

15741605
def test_mapdl_str(mapdl, cleared):
15751606
out = str(mapdl)

0 commit comments

Comments
 (0)