Skip to content

Commit

Permalink
refactor: clean mapdl inprocess and move mute to MapdlCore (#3220)
Browse files Browse the repository at this point in the history
* clean mapdl inprocess and move mute to MapdlCore

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Adding changelog entry: 3220.changed.md

* rename backend to a more explicit name

* chore: adding changelog file 3220.added.md

* move _session_id use in mapdl_grpc

* make _MapdlCore an abstract class with name being an abstract getter

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix name getter in mapdl_console

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* rename arguments of mapdl_inprocess input to match those of mapdl_core

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add _name in mapdl_console and mapdl_grpc

* mapdl_inprocess: rename dir to dir_

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix format

* remove use of abstract class

* remove useless underscore

* Update the image cache

* codecov ignore mapdl_inprocess

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: pyansys-ci-bot <[email protected]>
Co-authored-by: German <[email protected]>
Co-authored-by: pyansys-ci-bot <[email protected]>
Co-authored-by: Gryfenfer97 <[email protected]>
  • Loading branch information
6 people authored Sep 16, 2024
1 parent 9922991 commit 1b82d34
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 36 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3220.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
refactor: clean mapdl inprocess and move mute to MapdlCore
3 changes: 2 additions & 1 deletion src/ansys/mapdl/core/mapdl_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def __init__(
self._auto_continue = True
self._continue_on_error = False
self._process = None
self._name = None
self._launch(start_parm)
super().__init__(
loglevel=loglevel,
Expand Down Expand Up @@ -315,7 +316,7 @@ def kill(self):
self._log.warning("Unable to kill process %d", self._process.pid)
self._log.debug("Killed process %d", self._process.pid)

@property
@MapdlBase.name.getter
def name(self):
"""Instance unique identifier."""
if not self._name:
Expand Down
22 changes: 10 additions & 12 deletions src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ def __init__(
):
"""Initialize connection with MAPDL."""
atexit.register(self.__del__) # registering to exit properly
self._name = None # For naming the instance.
self._show_matplotlib_figures = True # for testing
self._query = None
self._exited: bool = False
Expand All @@ -253,6 +252,7 @@ def __init__(
self._file_type_for_plots = file_type_for_plots
self._default_file_type_for_plots = file_type_for_plots
self._version = None # cached version
self._mute = False

if _HAS_PYVISTA:
if use_vtk is not None: # pragma: no cover
Expand Down Expand Up @@ -330,6 +330,9 @@ def __init__(

self._info = Information(self)

def _after_run(self, _command: str) -> None:
pass

@property
def allow_ignore(self):
"""Invalid commands will be ignored rather than exceptions
Expand Down Expand Up @@ -373,6 +376,9 @@ def allow_ignore(self, value):
)
self._ignore_errors = bool(value)

def _before_run(self, _command: str) -> None:
pass

@property
def chain_commands(self):
"""Chain several mapdl commands.
Expand Down Expand Up @@ -2156,14 +2162,6 @@ def run(
self._stored_commands.append(command)
return

# Actually sending the message
if self._session_id is not None:
self._check_session_id()
else:
# For some reason the session hasn't been created
if self.is_grpc:
self._create_session()

if mute is None:
if hasattr(self, "mute"):
mute = self.mute
Expand Down Expand Up @@ -2225,6 +2223,8 @@ def run(
# Edge case. `\title, 'par=1234' `
self._check_parameter_name(param_name)

self._before_run(command)

short_cmd = parse_to_short_cmd(command)
text = self._run(command, verbose=verbose, mute=mute)

Expand All @@ -2236,9 +2236,7 @@ def run(
self.show(self.default_file_type_for_plots)
text = self._run(command, verbose=verbose, mute=mute)

if command[:4].upper() == "/CLE" and self.is_grpc:
# We have reset the database, so we need to create a new session id
self._create_session()
self._after_run(command)

if mute:
return
Expand Down
16 changes: 14 additions & 2 deletions src/ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def __init__(
remove_temp_dir_on_exit = remove_temp_files
remove_temp_files = None

self._name: Optional[str] = None
self._session_id_: Optional[str] = None
self._checking_session_id_: bool = False
self.__distributed: Optional[bool] = None
Expand Down Expand Up @@ -411,7 +412,6 @@ def __init__(
self._health_response_queue: Optional["Queue"] = None
self._exiting: bool = False
self._exited: Optional[bool] = None
self._mute: bool = False
self._db: Optional[MapdlDb] = None
self.__server_version: Optional[str] = None
self._state: Optional[grpc.Future] = None
Expand Down Expand Up @@ -489,6 +489,18 @@ def __init__(

self._create_session()

def _after_run(self, command: str) -> None:
if command[:4].upper() == "/CLE":
# We have reset the database, so we need to create a new session id
self._create_session()

def _before_run(self, _command: str) -> None:
if self._session_id is not None:
self._check_session_id()
else:
# For some reason the session hasn't been created
self._create_session()

def _create_process_stds_queue(self, process=None):
from ansys.mapdl.core.launcher import (
_create_queue_for_std, # Avoid circular import error
Expand Down Expand Up @@ -2955,7 +2967,7 @@ def cmatrix(
# non-interactive and there's no output to return
super().cmatrix(symfac, condname, numcond, grndkey, capname, **kwargs)

@property
@MapdlBase.name.getter
def name(self) -> str:
"""Instance unique identifier."""
if not self._name:
Expand Down
52 changes: 31 additions & 21 deletions src/ansys/mapdl/core/mapdl_inprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,32 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from typing import Optional, Protocol
from typing import Protocol

from ansys.mapdl.core.mapdl import MapdlBase


class _Backend(Protocol):
def run_command(self) -> str: ...
def run_command(self, command: str, verbose: bool, mute: bool) -> str: ...

def input_file(
self,
filename: str,
extension: str,
directory: str,
line: int,
log: int,
mute: bool,
) -> str: ...


class MapdlInProcess(MapdlBase):
def __init__(self, backend: _Backend):
def __init__(self, in_process_backend: _Backend):
super().__init__(
loglevel="WARNING", use_vtk=False, log_apdl=None, print_com=False
)
self._backend = backend
self._in_process_backend = in_process_backend
self._cleanup: bool = True
self._name: str = "MapdlInProcess"
self._session_id: Optional[str] = None
self._mute: bool = False

def _run(self, command: str, verbose: bool = False, mute: bool = False) -> str:
if not command.strip():
Expand All @@ -47,19 +54,22 @@ def _run(self, command: str, verbose: bool = False, mute: bool = False) -> str:
if len(command) > 639:
raise ValueError("Maximum command length mut be less than 640 characters")

return self._backend.run_command(command, verbose, mute).strip()

@property
def name(self) -> str:
return self._name

@name.setter
def name(self, name) -> None:
self._name = name
return self._in_process_backend.run_command(command, verbose, mute).strip()

def _check_session_id(self) -> None:
pass
def input(
self,
fname: str = "",
ext: str = "",
dir_: str = "",
line: str = "",
log: str = "",
mute: bool = False,
**_,
):
return self._in_process_backend.input_file(
fname, ext, dir_, int(line or 0), int(log or 0), mute
)

def __repr__(self):
info = super().__repr__()
return info
@MapdlBase.name.getter
def name(self) -> str:
return "MapdlInProcess"

0 comments on commit 1b82d34

Please sign in to comment.