From a6efcff282f6e2fd85d482ce76055fd7501681fe Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:18:23 +0100 Subject: [PATCH 01/10] fix: increase timeout for checking output file --- src/ansys/mapdl/core/mapdl_grpc.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 466317f181..a0e965e3e0 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -119,6 +119,9 @@ SESSION_ID_NAME = "__PYMAPDL_SESSION_ID__" +DEFAULT_TIME_STEP_STREAM_NT = 500 +DEFAULT_TIME_STEP_STREAM_POSIX = 100 + # Retry policy for gRPC calls. SERVICE_DEFAULT_CONFIG = { # see https://github.com/grpc/proposal/blob/master/A6-client-retries.md#retry-policy-capabilities @@ -1930,8 +1933,11 @@ def input( return response.strip() # otherwise, not verbose - if time_step_stream is None: - time_step_stream = 50 + if time_step_stream is None and os.name == "nt": + time_step_stream = 500 + elif time_step_stream is None: + time_step_stream = 100 + metadata = [ ("time_step_stream", str(time_step_stream)), ("chunk_size", str(chunk_size)), From 9cc7bb0343b254b603cf610274df20319bd08fb3 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:55:20 +0100 Subject: [PATCH 02/10] feat: implement global defaults OS dependent. --- src/ansys/mapdl/core/mapdl_grpc.py | 72 +++++++++++++++++++----------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index a0e965e3e0..78d166d275 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -119,6 +119,7 @@ SESSION_ID_NAME = "__PYMAPDL_SESSION_ID__" +DEFAULT_TIME_STEP_STREAM = None DEFAULT_TIME_STEP_STREAM_NT = 500 DEFAULT_TIME_STEP_STREAM_POSIX = 100 @@ -1072,7 +1073,8 @@ def _send_command(self, cmd: str, mute: bool = False) -> Optional[str]: def _send_command_stream(self, cmd, verbose=False) -> str: """Send a command and expect a streaming response""" request = pb_types.CmdRequest(command=cmd) - metadata = [("time_step_stream", "100")] + time_step = self._get_time_step_stream() + metadata = [("time_step_stream", str(time_step))] stream = self._stub.SendCommandS(request, metadata=metadata) response = [] for item in stream: @@ -1773,13 +1775,14 @@ def input( execution time. Due to stability issues, the default time_step_stream is - dependent on verbosity. The defaults are: + dependent on the OS MAPDL is running on. The defaults are: - - ``verbose=True``: ``time_step_stream=500`` - - ``verbose=False``: ``time_step_stream=50`` + - Windows: ``time_step_stream=500`` + - Linux: ``time_step_stream=100`` These defaults will be ignored if ``time_step_stream`` is - manually set. + manually set. See the *Examples* section to learn how to change + the default value globally. orig_cmd : str, optional Original command. There are some cases, were input is @@ -1829,6 +1832,11 @@ def input( >>> with mapdl.non_interactive: mapdl.run("/input,inputtrigger,inp") # This inputs 'myinput.inp' + You can also change them globably using: + + >>> from ansys.mapdl.core import mapdl_grpc + >>> mapdl_grpc.DEFAULT_TIME_STEP_STREAM=100 # in milliseconds + """ # Checking compatibility # Checking the user is not reusing old api: @@ -1909,18 +1917,14 @@ def input( # are unclear filename = self._get_file_path(fname, progress_bar) - if time_step_stream is not None: - if time_step_stream <= 0: - raise ValueError("``time_step_stream`` must be greater than 0``") + time_step_stream = self._get_time_step_stream(time_step_stream) - if verbose: - if time_step_stream is None: - time_step_stream = 500 - metadata = [ - ("time_step_stream", str(time_step_stream)), - ("chunk_size", str(chunk_size)), - ] + metadata = [ + ("time_step_stream", str(time_step_stream)), + ("chunk_size", str(chunk_size)), + ] + if verbose: request = pb_types.InputFileRequest(filename=filename) strouts = self._stub.InputFileS(request, metadata=metadata) responses = [] @@ -1932,16 +1936,8 @@ def input( response = "\n".join(responses) return response.strip() - # otherwise, not verbose - if time_step_stream is None and os.name == "nt": - time_step_stream = 500 - elif time_step_stream is None: - time_step_stream = 100 - - metadata = [ - ("time_step_stream", str(time_step_stream)), - ("chunk_size", str(chunk_size)), - ] + ## + # Otherwise, not verbose # since we can't directly run /INPUT, we have to write a # temporary input file that tells MAPDL to read the input @@ -2015,6 +2011,32 @@ def input( return output + def _get_time_step_stream( + self, time_step: Optional[Union[int, float]] = None + ) -> str: + """Return the time step for checking if MAPDL is done writing the + output to the file which later will be returned as response + """ + if time_step is None: + if DEFAULT_TIME_STEP_STREAM is not None: + time_step = DEFAULT_TIME_STEP_STREAM + elif self.platform == "windows": + time_step = DEFAULT_TIME_STEP_STREAM_NT + elif self.platform == "linux": + time_step = DEFAULT_TIME_STEP_STREAM_POSIX + else: + raise ValueError( + f"The MAPDL platform ('{self.platform}') is not recognaised." + ) + + else: + if time_step_stream <= 0: + raise ValueError( + "``time_step_stream`` argument must be greater than 0``" + ) + + return time_step + def _get_file_path(self, fname: str, progress_bar: bool = False) -> str: """Find files in the Python and MAPDL working directories. From b85cacbe71b433dc94dd92594ea1c491be4b8d2e Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 16 Dec 2024 17:16:51 +0100 Subject: [PATCH 03/10] feat: adding logging --- src/ansys/mapdl/core/mapdl_core.py | 1 + src/ansys/mapdl/core/mapdl_grpc.py | 1 + 2 files changed, 2 insertions(+) diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index 3d745a09ee..3a4536f761 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -2913,6 +2913,7 @@ def _check_mapdl_os(self): self._platform = "windows" else: # pragma: no cover raise MapdlRuntimeError("Unknown platform: {}".format(platform)) + self.logger.debug(f"MAPDL is running on {self._platform} OS.") def _check_on_docker(self): """Check if MAPDL is running on docker.""" diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 78d166d275..86737da974 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -2035,6 +2035,7 @@ def _get_time_step_stream( "``time_step_stream`` argument must be greater than 0``" ) + self.logger.debug(f"The time_step_stream argument is set to: {time_step}") return time_step def _get_file_path(self, fname: str, progress_bar: bool = False) -> str: From 881dfa3cd4b69b356d8d8942f21c717986082950 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:18:23 +0100 Subject: [PATCH 04/10] fix: increase timeout for checking output file --- src/ansys/mapdl/core/mapdl_grpc.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index c8682296b4..d3401106bb 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -119,6 +119,9 @@ SESSION_ID_NAME = "__PYMAPDL_SESSION_ID__" +DEFAULT_TIME_STEP_STREAM_NT = 500 +DEFAULT_TIME_STEP_STREAM_POSIX = 100 + # Retry policy for gRPC calls. SERVICE_DEFAULT_CONFIG = { # see https://github.com/grpc/proposal/blob/master/A6-client-retries.md#retry-policy-capabilities @@ -1935,8 +1938,11 @@ def input( return response.strip() # otherwise, not verbose - if time_step_stream is None: - time_step_stream = 50 + if time_step_stream is None and os.name == "nt": + time_step_stream = 500 + elif time_step_stream is None: + time_step_stream = 100 + metadata = [ ("time_step_stream", str(time_step_stream)), ("chunk_size", str(chunk_size)), From e60c38d81106a5302a9224af070cf06f44de7482 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:55:20 +0100 Subject: [PATCH 05/10] feat: implement global defaults OS dependent. --- src/ansys/mapdl/core/mapdl_grpc.py | 72 +++++++++++++++++++----------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index d3401106bb..b716182824 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -119,6 +119,7 @@ SESSION_ID_NAME = "__PYMAPDL_SESSION_ID__" +DEFAULT_TIME_STEP_STREAM = None DEFAULT_TIME_STEP_STREAM_NT = 500 DEFAULT_TIME_STEP_STREAM_POSIX = 100 @@ -1072,7 +1073,8 @@ def _send_command(self, cmd: str, mute: bool = False) -> Optional[str]: def _send_command_stream(self, cmd, verbose=False) -> str: """Send a command and expect a streaming response""" request = pb_types.CmdRequest(command=cmd) - metadata = [("time_step_stream", "100")] + time_step = self._get_time_step_stream() + metadata = [("time_step_stream", str(time_step))] stream = self._stub.SendCommandS(request, metadata=metadata) response = [] for item in stream: @@ -1778,13 +1780,14 @@ def input( execution time. Due to stability issues, the default time_step_stream is - dependent on verbosity. The defaults are: + dependent on the OS MAPDL is running on. The defaults are: - - ``verbose=True``: ``time_step_stream=500`` - - ``verbose=False``: ``time_step_stream=50`` + - Windows: ``time_step_stream=500`` + - Linux: ``time_step_stream=100`` These defaults will be ignored if ``time_step_stream`` is - manually set. + manually set. See the *Examples* section to learn how to change + the default value globally. orig_cmd : str, optional Original command. There are some cases, were input is @@ -1834,6 +1837,11 @@ def input( >>> with mapdl.non_interactive: mapdl.run("/input,inputtrigger,inp") # This inputs 'myinput.inp' + You can also change them globably using: + + >>> from ansys.mapdl.core import mapdl_grpc + >>> mapdl_grpc.DEFAULT_TIME_STEP_STREAM=100 # in milliseconds + """ # Checking compatibility # Checking the user is not reusing old api: @@ -1914,18 +1922,14 @@ def input( # are unclear filename = self._get_file_path(fname, progress_bar) - if time_step_stream is not None: - if time_step_stream <= 0: - raise ValueError("``time_step_stream`` must be greater than 0``") + time_step_stream = self._get_time_step_stream(time_step_stream) - if verbose: - if time_step_stream is None: - time_step_stream = 500 - metadata = [ - ("time_step_stream", str(time_step_stream)), - ("chunk_size", str(chunk_size)), - ] + metadata = [ + ("time_step_stream", str(time_step_stream)), + ("chunk_size", str(chunk_size)), + ] + if verbose: request = pb_types.InputFileRequest(filename=filename) strouts = self._stub.InputFileS(request, metadata=metadata) responses = [] @@ -1937,16 +1941,8 @@ def input( response = "\n".join(responses) return response.strip() - # otherwise, not verbose - if time_step_stream is None and os.name == "nt": - time_step_stream = 500 - elif time_step_stream is None: - time_step_stream = 100 - - metadata = [ - ("time_step_stream", str(time_step_stream)), - ("chunk_size", str(chunk_size)), - ] + ## + # Otherwise, not verbose # since we can't directly run /INPUT, we have to write a # temporary input file that tells MAPDL to read the input @@ -2020,6 +2016,32 @@ def input( return output + def _get_time_step_stream( + self, time_step: Optional[Union[int, float]] = None + ) -> str: + """Return the time step for checking if MAPDL is done writing the + output to the file which later will be returned as response + """ + if time_step is None: + if DEFAULT_TIME_STEP_STREAM is not None: + time_step = DEFAULT_TIME_STEP_STREAM + elif self.platform == "windows": + time_step = DEFAULT_TIME_STEP_STREAM_NT + elif self.platform == "linux": + time_step = DEFAULT_TIME_STEP_STREAM_POSIX + else: + raise ValueError( + f"The MAPDL platform ('{self.platform}') is not recognaised." + ) + + else: + if time_step_stream <= 0: + raise ValueError( + "``time_step_stream`` argument must be greater than 0``" + ) + + return time_step + def _get_file_path(self, fname: str, progress_bar: bool = False) -> str: """Find files in the Python and MAPDL working directories. From 10a726834e468b1568ceafbfe1a525608f14c460 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Mon, 16 Dec 2024 17:16:51 +0100 Subject: [PATCH 06/10] feat: adding logging --- src/ansys/mapdl/core/mapdl_core.py | 1 + src/ansys/mapdl/core/mapdl_grpc.py | 1 + 2 files changed, 2 insertions(+) diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index ff50e89414..94626cf9c3 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -2919,6 +2919,7 @@ def _check_mapdl_os(self): self._platform = "windows" else: # pragma: no cover raise MapdlRuntimeError("Unknown platform: {}".format(platform)) + self.logger.debug(f"MAPDL is running on {self._platform} OS.") def _check_on_docker(self): """Check if MAPDL is running on docker.""" diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index b716182824..9b7810b2b1 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -2040,6 +2040,7 @@ def _get_time_step_stream( "``time_step_stream`` argument must be greater than 0``" ) + self.logger.debug(f"The time_step_stream argument is set to: {time_step}") return time_step def _get_file_path(self, fname: str, progress_bar: bool = False) -> str: From 0030a08254d83c72f1964af3dac2cd91c6a7d8eb Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Wed, 8 Jan 2025 11:49:58 +0000 Subject: [PATCH 07/10] chore: adding changelog file 3642.fixed.md [dependabot-skip] --- doc/changelog.d/3642.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/3642.fixed.md diff --git a/doc/changelog.d/3642.fixed.md b/doc/changelog.d/3642.fixed.md new file mode 100644 index 0000000000..526ce0df6e --- /dev/null +++ b/doc/changelog.d/3642.fixed.md @@ -0,0 +1 @@ +fix: timeout for file checking \ No newline at end of file From 561005499f4cc7fa39318c5b3575857aae0dc71f Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:09:49 +0100 Subject: [PATCH 08/10] fix: wrong variable --- src/ansys/mapdl/core/mapdl_grpc.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 9b7810b2b1..8f44fe4a66 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -2035,12 +2035,10 @@ def _get_time_step_stream( ) else: - if time_step_stream <= 0: - raise ValueError( - "``time_step_stream`` argument must be greater than 0``" - ) + if time_step <= 0: + raise ValueError("``time_step`` argument must be greater than 0``") - self.logger.debug(f"The time_step_stream argument is set to: {time_step}") + self.logger.debug(f"The time_step argument is set to: {time_step}") return time_step def _get_file_path(self, fname: str, progress_bar: bool = False) -> str: From 9f5d42140696e15d4685e496137d7c70bddf538d Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:10:27 +0100 Subject: [PATCH 09/10] tests: adding tests --- tests/test_grpc.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/test_grpc.py b/tests/test_grpc.py index 808de4afab..6d0aaf6fc2 100644 --- a/tests/test_grpc.py +++ b/tests/test_grpc.py @@ -25,6 +25,7 @@ import re import shutil import sys +from unittest.mock import patch import grpc import pytest @@ -682,3 +683,32 @@ def _null_close_process(): mapdl.prep7(mapdl) mapdl._exited = False # Restoring + + +@pytest.mark.parametrize("platform", ["linux", "windows", "error"]) +def test__get_time_step_stream(mapdl, platform): + with patch("ansys.mapdl.core.mapdl_grpc.MapdlGrpc.platform", platform): + from ansys.mapdl.core import mapdl_grpc + + if platform == "linux": + DEFAULT_TIME_STEP_STREAM = mapdl_grpc.DEFAULT_TIME_STEP_STREAM_POSIX + elif platform == "windows": + DEFAULT_TIME_STEP_STREAM = mapdl_grpc.DEFAULT_TIME_STEP_STREAM_NT + else: + with pytest.raises(ValueError, match="The MAPDL platform"): + mapdl._get_time_step_stream() + + return # Early exit + + assert DEFAULT_TIME_STEP_STREAM == mapdl._get_time_step_stream() + + mapdl_grpc.DEFAULT_TIME_STEP_STREAM = 200 + assert mapdl_grpc.DEFAULT_TIME_STEP_STREAM == mapdl._get_time_step_stream() + mapdl_grpc.DEFAULT_TIME_STEP_STREAM = None + + assert 700 == mapdl._get_time_step_stream(700) + + with pytest.raises( + ValueError, match="``time_step`` argument must be greater than 0``" + ): + mapdl._get_time_step_stream(-700) From 219bb1b170a5b471b3b83a64eac27e26d22f70f8 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:14:46 +0100 Subject: [PATCH 10/10] ci: not hiding log --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 90ecdf738f..9f5c9b0d5f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -151,6 +151,8 @@ jobs: python-package-name: ${{ env.PACKAGE_NAME }} dev-mode: ${{ github.ref != 'refs/heads/main' }} upload-reports: True + hide-log: false + docs-build: name: "Build documentation"