Skip to content

Commit 15272b4

Browse files
germa89akaszynski
andauthored
Fix/Making-/nopr-unsupported (#632)
* Added procedure for silently omitting some commands (`/NOPR` for example. * Added test cases for invalid commands and invalid silent commands. * Format fixing * Apply suggestions from code review Co-authored-by: Alex Kaszynski <[email protected]>
1 parent 3bdd5a9 commit 15272b4

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

ansys/mapdl/core/mapdl.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
self.run('(F20.12)')
4646
"""
4747

48+
## Invalid commands in interactive mode.
4849
INVAL_COMMANDS = {
4950
"*VWR": VWRITE_REPLACEMENT,
5051
"*CFO": "Run CFOPEN as ``non_interactive``",
@@ -53,7 +54,29 @@
5354
"/EOF": "Unsupported command. Use ``exit`` to stop the server.",
5455
"*ASK": "Unsupported command. Use python ``input`` instead.",
5556
"*IF": "Use a python ``if`` or run as non_interactive",
56-
"CMATRIX": "Use as non_interactive",
57+
"CMAT": "Use as non_interactive",
58+
}
59+
60+
## Soft-invalid commands
61+
# Invalid commands in interactive mode but their execution is just ignored.
62+
# The correspondent command is replaced by a comment using the command '\COM'
63+
# and a warning is recorded in the logger
64+
#
65+
# This commands can still be executed in ``non_interactive`` mode or using
66+
# ``Mapdl._run`` method.
67+
#
68+
# Format of the message:
69+
# f"{CMD} is ignored: {INVAL_COMMANDS_SILENT[CMD]}.
70+
#
71+
# NOTE
72+
# Obtain the command from the string supplied using
73+
#
74+
# string.split(',')[0].upper()
75+
#
76+
# This way to get the command is different from the one used in ``INVAL_COMMANDS``.
77+
#
78+
INVAL_COMMANDS_SILENT = {
79+
"/NOPR": "Suppressing console output is not recommended, use ``Mute`` parameter instead. This command is disabled in interactive mode."
5780
}
5881

5982
PLOT_COMMANDS = ["NPLO", "EPLO", "KPLO", "LPLO", "APLO", "VPLO", "PLNS", "PLES"]
@@ -2045,19 +2068,29 @@ def run(self, command, write_to_log=True, **kwargs):
20452068
# https://github.com/pyansys/pymapdl/issues/380
20462069
command = "/CLE,NOSTART"
20472070

2071+
# Invalid commands silently ignored.
2072+
cmd_ = command.split(',')[0].upper()
2073+
if cmd_ in INVAL_COMMANDS_SILENT:
2074+
msg = f"{cmd_} is ignored: {INVAL_COMMANDS_SILENT[cmd_]}."
2075+
self._log.info(msg)
2076+
2077+
# This very likely won't be recorded anywhere.
2078+
# But just in case, adding info as a comment
2079+
command = f"/COM, PyAnsys: {msg}" # Using '!' makes the output of '_run' empty
2080+
20482081
if self._store_commands:
20492082
self._stored_commands.append(command)
20502083
return
20512084
elif command[:3].upper() in INVAL_COMMANDS:
20522085
exception = RuntimeError(
20532086
'Invalid pymapdl command "%s"\n\n%s'
2054-
% (command, INVAL_COMMANDS[command[:3]])
2087+
% (command, INVAL_COMMANDS[command[:3].upper()])
20552088
)
20562089
raise exception
20572090
elif command[:4].upper() in INVAL_COMMANDS:
20582091
exception = RuntimeError(
20592092
'Invalid pymapdl command "%s"\n\n%s'
2060-
% (command, INVAL_COMMANDS[command[:4]])
2093+
% (command, INVAL_COMMANDS[command[:4].upper()])
20612094
)
20622095
raise exception
20632096
elif write_to_log and self._apdl_log is not None:

tests/test_mapdl.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,3 +610,22 @@ def test_coriolis(mapdl, cleared):
610610
assert "GYROSCOPIC DAMPING MATRIX WILL BE CALCULATED" in resp
611611
assert "ROTATING DAMPING MATRIX ACTIVATED" in resp
612612
assert "PRINT ROTOR MASS SUMMARY ACTIVATED" in resp
613+
614+
615+
def test_inval_commands(mapdl, cleared):
616+
"""Test the output of invalid commands"""
617+
cmds = ["*END", "*vwrite", "/eof", "cmatrix"]
618+
for each_cmd in cmds:
619+
with pytest.raises(RuntimeError):
620+
mapdl.run(each_cmd)
621+
622+
623+
def test_inval_commands_silent(mapdl, tmpdir, cleared):
624+
assert mapdl.run("parm = 'asdf'") # assert it is not empty
625+
mapdl.nopr()
626+
assert mapdl.run("parm = 'asdf'") # assert it is not empty
627+
628+
assert not mapdl._run('/nopr') # setting /nopr and assert it is empty
629+
assert not mapdl.run("parm = 'asdf'") # assert it is not empty
630+
631+
mapdl._run('/gopr') # getting settings back

0 commit comments

Comments
 (0)