Skip to content

Commit e0916e8

Browse files
authored
File uploader in MPREAD and MPWRITE (#976)
* 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. * fixing unit tests * Fixing non implemented error * Extending coverage * Removed raising exception if downloading in local. It does not make sense. * Adding test for writing outside working dir in remote
1 parent 2d5d3d2 commit e0916e8

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/ansys/mapdl/core/mapdl.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,3 +2903,28 @@ def mpread(self, fname="", ext="", lib="", **kwargs):
29032903
fname = load_file(self, fname_)
29042904
self._log.info("Bypassing 'MPREAD' with 'INPUT'.")
29052905
return self.input(fname)
2906+
2907+
@wraps(Commands.mpwrite)
2908+
def mpwrite(
2909+
self,
2910+
fname="",
2911+
ext="",
2912+
lib="",
2913+
mat="",
2914+
download_file=False,
2915+
progress_bar=True,
2916+
**kwargs,
2917+
):
2918+
fname_ = fname + "." + ext
2919+
if not self._local:
2920+
if os.path.dirname(fname_):
2921+
raise IOError(
2922+
"Only writing files to the MAPDL working directory is allowed. "
2923+
f"The supplied path {fname_} is not allowed."
2924+
)
2925+
2926+
output = super().mpwrite(fname, ext, lib, mat, **kwargs)
2927+
if download_file:
2928+
self.download(os.path.basename(fname_), progress_bar=progress_bar)
2929+
2930+
return output

tests/test_mapdl.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,18 +1476,60 @@ def test_mpfunctions(mapdl, cube_solve, capsys):
14761476
fname = "test"
14771477
ext = "mp1"
14781478

1479-
mapdl.mpwrite(fname, ext)
1479+
assert f"WRITE OUT MATERIAL PROPERTY LIBRARY TO FILE=" in mapdl.mpwrite(fname, ext)
14801480
assert f"{fname}.{ext}" in mapdl.list_files()
14811481

1482-
nuxy = 0.3
1482+
# asserting downloading
1483+
ext = "mp2"
1484+
assert f"WRITE OUT MATERIAL PROPERTY LIBRARY TO FILE=" in mapdl.mpwrite(
1485+
fname, ext, download_file=True
1486+
)
1487+
assert f"{fname}.{ext}" in mapdl.list_files()
1488+
assert os.path.exists(f"{fname}.{ext}")
1489+
1490+
## Checking reading
1491+
# Uploading a local file
1492+
with open(f"{fname}.{ext}", "r") as fid:
1493+
text = fid.read()
1494+
1495+
os.remove(f"{fname}.{ext}") # remove temp file
1496+
1497+
ext = ext + "2"
1498+
fname_ = f"{fname}.{ext}"
1499+
new_nuxy = "MPDATA,NUXY, 1, 1, 0.4000000E+00,"
1500+
nuxy = float(new_nuxy.split(",")[4])
14831501
ex = 0.2100000e12
14841502

1503+
with open(fname_, "w") as fid:
1504+
fid.write(text.replace("MPDATA,NUXY, 1, 1, 0.3000000E+00,", new_nuxy))
1505+
1506+
assert fname_ not in mapdl.list_files()
1507+
mapdl.clear()
1508+
mapdl.prep7()
1509+
captured = capsys.readouterr() # To flush it
1510+
output = mapdl.mpread(fname, ext)
1511+
captured = capsys.readouterr()
1512+
assert f"Uploading {fname}.{ext}:" in captured.err
1513+
assert "PROPERTY TEMPERATURE TABLE NUM. TEMPS= 1" in output
1514+
assert "TEMPERATURE TABLE ERASED." in output
1515+
assert "0.4000000" in output
1516+
assert fname_ in mapdl.list_files()
1517+
# check if materials are read into the db
1518+
assert mapdl.get_value("NUXY", "1", "TEMP", 0) == nuxy
1519+
assert np.allclose(mapdl.get_value("EX", 1, "TEMP", 0), ex)
1520+
14851521
# Reding file in remote
1522+
fname_ = f"{fname}.{ext}"
1523+
os.remove(fname_)
1524+
assert not os.path.exists(fname_)
1525+
assert f"{fname}.{ext}" in mapdl.list_files()
1526+
14861527
mapdl.clear()
14871528
mapdl.prep7()
14881529
output = mapdl.mpread(fname, ext)
14891530
assert "PROPERTY TEMPERATURE TABLE NUM. TEMPS= 1" in output
14901531
assert "TEMPERATURE TABLE ERASED." in output
1532+
assert "0.4000000" in output
14911533
assert np.allclose(mapdl.get_value("NUXY", "1", "TEMP", 0), nuxy)
14921534
assert np.allclose(mapdl.get_value("EX", 1, "TEMP", 0), ex)
14931535

@@ -1498,3 +1540,7 @@ def test_mpfunctions(mapdl, cube_solve, capsys):
14981540
# Test not implemented error
14991541
with pytest.raises(NotImplementedError):
15001542
mapdl.mpread(fname="dummy", ext="dummy", lib="something")
1543+
1544+
# Test suppliying a dir path when in remote
1545+
with pytest.raises(IOError):
1546+
mapdl.mpwrite("/test_dir/test", "mp")

0 commit comments

Comments
 (0)