Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix load file in local mode #962

Merged
merged 9 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/ansys/mapdl/core/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,18 +524,37 @@ def _load_file(self, fname):

"""
if self._mapdl._local: # pragma: no cover
if not os.path.exists(fname):
raise FileNotFoundError(f"The file {fname} could not be found.")
base_fname = os.path.basename(fname)
if not os.path.exists(fname) and base_fname not in self._mapdl.list_files():
raise FileNotFoundError(
f"The file {fname} could not be found in the Python working directory ('{os.getcwd()}')"
f"nor in the MAPDL working directory ('{self._mapdl.directory}')."
)

elif os.path.exists(fname) and base_fname in self._mapdl.list_files():
warn(
f"The file '{base_fname} is present in both, the python working directory ('{os.getcwd()}')"
"and in the MAPDL working directory ('{self._mapdl.directory}'). "
"Using the one in the MAPDL directory.\n"
"If you prefer to use the file in the Python directory, you can use `mapdl.upload` before this command to upload it."
)

elif os.path.exists(fname) and base_fname not in self._mapdl.list_files():
self._mapdl.upload(fname)

elif not os.path.exists(fname) and base_fname in self._mapdl.list_files():
pass

else:
if not os.path.exists(fname) and fname not in self._mapdl.list_files():
raise FileNotFoundError(
f"The file {fname} could not be found in the local client or remote working directory."
)
if os.path.exists(fname):
self._mapdl.upload(fname)
fname = os.path.basename(fname)

return fname
# Simplifying name for MAPDL reads it.
return os.path.basename(fname)

def stiff(self, dtype=np.double, fname="file.full", asarray=False):
"""Load the stiffness matrix from a full file.
Expand Down
43 changes: 43 additions & 0 deletions tests/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@
from scipy import sparse

from ansys.mapdl.core.errors import ANSYSDataTypeError
from ansys.mapdl.core.launcher import get_start_instance
import ansys.mapdl.core.math as apdl_math
from ansys.mapdl.core.misc import random_string

# skip entire module unless HAS_GRPC
pytestmark = pytest.mark.skip_grpc

skip_in_cloud = pytest.mark.skipif(
not get_start_instance(),
reason="""
Must be able to launch MAPDL locally. Remote execution does not allow for
directory creation.
""",
)


@pytest.fixture(scope="module")
def mm(mapdl):
Expand Down Expand Up @@ -522,6 +532,39 @@ def test_repr(mm):
assert mm._status == repr(mm)


def test__load_file(mm, tmpdir): # pragma: no cover
# generating dummy file
# mm._mapdl._local = True # Uncomment to test locally.
if not mm._mapdl._local:
return True

fname_ = random_string() + ".file"
fname = str(tmpdir.mkdir("tmpdir").join(fname_))

## Checking non-exists
with pytest.raises(FileNotFoundError):
assert fname_ == mm._load_file(fname)

with open(fname, "w") as fid:
fid.write("# Dummy")

## Checking case where the file is only in python folder
assert fname_ not in mm._mapdl.list_files()
assert fname_ == mm._load_file(fname)
assert fname_ in mm._mapdl.list_files()

## Checking case where the file is in both.
with pytest.warns():
assert fname_ == mm._load_file(fname)

## Checking the case where the file is only in the MAPDL folder
os.remove(fname)
assert fname_ == mm._load_file(fname)
assert not os.path.exists(fname)
assert fname_ in mm._mapdl.list_files()
mm._mapdl._local = False


def test_status(mm, capsys):
assert mm.status() is None
captured = capsys.readouterr()
Expand Down