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

refactor: clean mapdl inprocess and move mute to MapdlCore #3220

Merged
merged 30 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7c312b0
clean mapdl inprocess and move mute to MapdlCore
Gryfenfer97 Jun 26, 2024
b3a6e94
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 26, 2024
2895789
Adding changelog entry: 3220.changed.md
pyansys-ci-bot Jun 26, 2024
3adb0df
rename backend to a more explicit name
Gryfenfer97 Jun 26, 2024
4a87c50
Merge branch 'main' into maint/clean-mapdl-inprocess
germa89 Jul 3, 2024
220f669
chore: adding changelog file 3220.added.md
pyansys-ci-bot Jul 3, 2024
e69fb65
Merge branch 'main' into maint/clean-mapdl-inprocess
germa89 Jul 8, 2024
912c39d
Merge branch 'main' into maint/clean-mapdl-inprocess
germa89 Jul 15, 2024
a97084b
move _session_id use in mapdl_grpc
Gryfenfer97 Jul 31, 2024
b915c9f
make _MapdlCore an abstract class with name being an abstract getter
Gryfenfer97 Jul 31, 2024
5b5ea0d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 31, 2024
82e45bc
fix name getter in mapdl_console
Gryfenfer97 Jul 31, 2024
f99841a
Merge branch 'maint/clean-mapdl-inprocess' of https://github.com/ansy…
Gryfenfer97 Jul 31, 2024
0a5e8fb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 31, 2024
63a8e26
rename arguments of mapdl_inprocess input to match those of mapdl_core
Gryfenfer97 Aug 1, 2024
9e7c506
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 1, 2024
c4596b2
add _name in mapdl_console and mapdl_grpc
Gryfenfer97 Aug 1, 2024
0479915
mapdl_inprocess: rename dir to dir_
Gryfenfer97 Aug 1, 2024
2d46f72
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 1, 2024
3cfa7fc
fix format
Gryfenfer97 Aug 7, 2024
eec37e7
remove use of abstract class
Gryfenfer97 Aug 7, 2024
0a30af5
remove useless underscore
Gryfenfer97 Aug 7, 2024
80526b5
Merge branch 'main' into maint/clean-mapdl-inprocess
Gryfenfer97 Aug 22, 2024
9e3ff7e
Update the image cache
Gryfenfer97 Aug 22, 2024
d89b32f
Merge branch 'main' into maint/clean-mapdl-inprocess
germa89 Aug 26, 2024
dbf4a89
Merge branch 'main' into maint/clean-mapdl-inprocess
Gryfenfer97 Aug 28, 2024
b303c9e
codecov ignore mapdl_inprocess
Gryfenfer97 Aug 28, 2024
f136412
Merge branch 'main' into maint/clean-mapdl-inprocess
germa89 Sep 12, 2024
4758e5a
Merge branch 'main' into maint/clean-mapdl-inprocess
koubaa Sep 13, 2024
f23754d
Merge branch 'main' into maint/clean-mapdl-inprocess
koubaa Sep 16, 2024
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
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
Gryfenfer97 marked this conversation as resolved.
Show resolved Hide resolved
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 @@
):
"""Initialize connection with MAPDL."""
atexit.register(self.__del__) # registering to exit properly
self._name = None # For naming the instance.
germa89 marked this conversation as resolved.
Show resolved Hide resolved
self._show_matplotlib_figures = True # for testing
self._query = None
self._exited: bool = False
Expand All @@ -253,6 +252,7 @@
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 @@

self._info = Information(self)

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

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_core.py#L334

Added line #L334 was not covered by tests

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

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

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_core.py#L380

Added line #L380 was not covered by tests

@property
def chain_commands(self):
"""Chain several mapdl commands.
Expand Down Expand Up @@ -2156,14 +2162,6 @@
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 @@
# 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 @@
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

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_inprocess.py#L23

Added line #L23 was not covered by tests

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: ...

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_inprocess.py#L29

Added line #L29 was not covered by tests

def input_file(

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_inprocess.py#L31

Added line #L31 was not covered by tests
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):

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_inprocess.py#L43

Added line #L43 was not covered by tests
super().__init__(
loglevel="WARNING", use_vtk=False, log_apdl=None, print_com=False
)
self._backend = backend
self._in_process_backend = in_process_backend

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_inprocess.py#L47

Added line #L47 was not covered by tests
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 @@
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()

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_inprocess.py#L57

Added line #L57 was not covered by tests

def _check_session_id(self) -> None:
pass
def input(

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_inprocess.py#L59

Added line #L59 was not covered by tests
self,
fname: str = "",
ext: str = "",
dir_: str = "",
line: str = "",
log: str = "",
mute: bool = False,
**_,
):
return self._in_process_backend.input_file(

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_inprocess.py#L69

Added line #L69 was not covered by tests
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"

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

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/mapdl_inprocess.py#L73-L75

Added lines #L73 - L75 were not covered by tests
Loading