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

Avoiding running get_array and non_interactive raising exceptions #2484

Merged
merged 7 commits into from
Nov 8, 2023
43 changes: 40 additions & 3 deletions src/ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,20 @@
self._parent()._store_commands = True

def __exit__(self, *args):
self._parent()._log.debug("Exiting non-interactive mode")
self._parent()._flush_stored()
self._parent()._store_commands = False

if args[0] is not None:
# An exception was raised, let's exit now without flushing
self._parent()._log.debug(
"An exception was found in the `non_interactive` environment. "
"Hence the commands are not flushed."
)
return None
else:
# No exception so let's flush.
self._parent()._log.debug("Exiting non-interactive mode")
self._parent()._flush_stored()

class _save_selection:
"""Save the selection and returns to it when exiting"""

Expand Down Expand Up @@ -3827,7 +3837,20 @@
-0.00178402, -0.01234851, 0.01234851, -0.01234851])

"""
arr = self._get_array(entity, entnum, item1, it1num, item2, it2num, kloop)
parm_name = kwargs.get("parm", None)

if self._store_commands:
raise MapdlRuntimeError(
"Cannot use `mapdl.get_array` when in `non_interactive` mode, "
"since it does not return anything until the `non_interactive` context "
"manager is finished.\n"
"Exit `non_interactive` mode before using this method.\n\n"
"Alternatively you can use `mapdl.vget` to specify the name of the MAPDL parameter where to store the retrieved value."
)

arr = self._get_array(
entity, entnum, item1, it1num, item2, it2num, kloop, **kwargs
)

# edge case where corba refuses to return the array
ntry = 0
Expand All @@ -3853,6 +3876,16 @@
"""Uses the VGET command to get an array from ANSYS"""
parm_name = kwargs.pop("parm", None)

if self._store_commands and not parm_name:
raise MapdlRuntimeError(

Check warning on line 3880 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L3879-L3880

Added lines #L3879 - L3880 were not covered by tests
"Cannot use `mapdl._get_array` when in `non_interactive` mode, "
"since it does not return anything until the `non_interactive` context "
"manager is finished.\n"
"Exit `non_interactive` mode before using this method.\n\n"
"Alternatively you can use `mapdl.vget` or use the `parm` kwarg in "
"`mapdl._get_array` to specify the name of the MAPDL parameter where to store the retrieved value. In any case, this function will return `None`"
)

if parm_name is None:
parm_name = "__vget_tmp_%d__" % self._vget_arr_counter
self._vget_arr_counter += 1
Expand All @@ -3869,6 +3902,10 @@
mute=False,
)

if self._store_commands:

Check warning on line 3905 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L3905

Added line #L3905 was not covered by tests
# Return early
return None

Check warning on line 3907 in src/ansys/mapdl/core/mapdl.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl.py#L3907

Added line #L3907 was not covered by tests

# check if empty array
if "the dimension number 1 is 0" in out:
return np.empty(0)
Expand Down
6 changes: 4 additions & 2 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2040,8 +2040,10 @@ def _get(

if self._store_commands:
raise MapdlRuntimeError(
"Cannot use gRPC enabled ``GET`` when in non_interactive mode. "
"Exit non_interactive mode before using this method."
"Cannot use `mapdl.get_value` when in `non_interactive` mode. "
"Exit non_interactive mode before using this method.\n\n"
"Alternatively you can use `mapdl.get` to specify the name of "
"the MAPDL parameter where to store the retrieved value.\n"
)

cmd = f"{entity},{entnum},{item1},{it1num},{item2},{it2num},{item3}, {it3num}, {item4}, {it4num}"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2117,3 +2117,10 @@ def test_saving_selection_context(mapdl, cube_solve):

assert "nod_selection_4".upper() not in mapdl.cmlist()
assert "nod_selection_4" not in mapdl.components


def test_get_array_non_interactive(mapdl, solved_box):
mapdl.allsel()
with pytest.raises(MapdlRuntimeError):
with mapdl.non_interactive:
mapdl.get_array("asdf", "2")
Loading