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 wrong string argument in APDL logger #878

Merged
merged 6 commits into from
Feb 16, 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
6 changes: 3 additions & 3 deletions ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ def open_apdl_log(self, filename, mode="w"):

self._apdl_log = open(filename, mode=mode, buffering=1) # line buffered
self._apdl_log.write(
"! APDL script generated using ansys.mapdl.core {pymapdl.__version__}\n"
f"! APDL log script generated using PyMapdl (ansys.mapdl.core {pymapdl.__version__})\n"
)

@supress_logging
Expand Down Expand Up @@ -2246,13 +2246,13 @@ def run(self, command, write_to_log=True, mute=None, **kwargs):

if command[:3].upper() in INVAL_COMMANDS:
exception = RuntimeError(
'Invalid pymapdl command "%s"\n\n%s'
'Invalid PyMAPDL command "%s"\n\n%s'
% (command, INVAL_COMMANDS[command[:3].upper()])
)
raise exception
elif command[:4].upper() in INVAL_COMMANDS:
exception = RuntimeError(
'Invalid pymapdl command "%s"\n\n%s'
'Invalid PyMAPDL command "%s"\n\n%s'
% (command, INVAL_COMMANDS[command[:4].upper()])
)
raise exception
Expand Down
57 changes: 42 additions & 15 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,29 +476,56 @@ def test_corba_apdl_logging_start(tmpdir):


def test_apdl_logging(mapdl, tmpdir):
filename = str(tmpdir.mkdir("tmpdir").join("tmp.inp"))
if mapdl._apdl_log is None:
mapdl.open_apdl_log(filename, mode="w")
mapdl._close_apdl_log()
tmp_dir = tmpdir.mkdir("tmpdir")
file_name = "tmp_logger.log"
file_path = str(tmp_dir.join(file_name))

# Checking there is no apdl_logger
if mapdl._apdl_log is not None:
mapdl._close_apdl_log()

assert mapdl._apdl_log is None
assert file_name not in os.listdir()

# test append mode
mapdl.open_apdl_log(filename, mode="a")
# Setting logger
mapdl.open_apdl_log(file_path, 'w')
assert file_name in os.listdir(tmp_dir)

# don't allow to double log
# don't allow double logger:
with pytest.raises(RuntimeError):
mapdl.open_apdl_log(filename, mode="w")
mapdl.open_apdl_log(file_name, mode="w")

# Testing
mapdl.prep7()
mapdl.k(1, 0, 0, 0)
mapdl.k(2, 1, 0, 0)
mapdl.k(3, 1, 1, 0)
mapdl.k(4, 0, 1, 0)
mapdl.com('This is a comment')

#Testing non-interactive
with mapdl.non_interactive:
mapdl.com('This is a non-interactive command')
mapdl.slashsolu()
mapdl.prep7()

mapdl._apdl_log.flush()
with open(file_path, 'r') as fid:
log = fid.read()

assert 'APDL' in log
assert 'ansys.mapdl.core' in log
assert 'PyMapdl' in log
assert '/COM' in log
assert 'This is a comment' in log
assert 'This is a non-interactive command' in log
assert '/SOLU' in log

# Closing
mapdl._close_apdl_log()
mapdl.com('This comment should not appear in the logger')

with open(file_path, 'r') as fid:
log = fid.read()

out = open(mapdl._apdl_log.name).read().strip().split()[-5:]
assert "PREP7" in out[0]
assert "K,4,0,1,0" in out[-1]
assert 'This comment should not appear in the logger' not in log
assert file_name in os.listdir(tmp_dir)


def test_nodes(tmpdir, cleared, mapdl):
Expand Down