diff --git a/src/ansys/mapdl/core/logging.py b/src/ansys/mapdl/core/logging.py index dcb7315307..4707174b4f 100644 --- a/src/ansys/mapdl/core/logging.py +++ b/src/ansys/mapdl/core/logging.py @@ -182,9 +182,7 @@ def process(self, msg, kwargs): # This are the extra parameters sent to log kwargs["extra"][ "instance_name" - ] = ( - self.extra.get_name() - ) # here self.extra is the argument pass to the log records. + ] = self.extra.name # here self.extra is the argument pass to the log records. return msg, kwargs def log_to_file(self, filename=FILE_NAME, level=LOG_LEVEL): @@ -476,7 +474,7 @@ def _add_mapdl_instance_logger(self, name, mapdl_instance, level): instance_logger = PymapdlCustomAdapter( self._make_child_logger(name, level), mapdl_instance ) - elif isinstance(name, None): + elif not name: # pragma: no cover instance_logger = PymapdlCustomAdapter( self._make_child_logger("NO_NAMED_YET", level), mapdl_instance ) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 5ddb76ae2f..7219825e06 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -150,6 +150,7 @@ def __init__( **start_parm, ): """Initialize connection with MAPDL.""" + self._name = None # For naming the instance. self._show_matplotlib_figures = True # for testing self._query = None self._exited = False @@ -187,7 +188,7 @@ def __init__( # Setting up loggers self._log = logger.add_instance_logger( - self._name, self, level=loglevel + self.name, self, level=loglevel ) # instance logger # adding a file handler to the logger if log_file: @@ -269,9 +270,12 @@ def inner_wrapper(*args, **kwargs): setattr(self, name, wrap_bc_listing_function(func)) @property - def _name(self): # pragma: no cover - """Implemented by child class""" - raise NotImplementedError("Implemented by child class") + def name(self): # pragma: no cover + raise NotImplementedError("Implemented by child classes.") + + @name.setter + def name(self, name): # pragma: no cover + raise AttributeError("The name of an instance cannot be changed.") @property def queries(self): @@ -3587,5 +3591,5 @@ def _raise_output_errors(self, response): # However, exceptions are recorded in the global logger which do not record # information of the instances name, hence we edit the error message. raise MapdlRuntimeError( - f"\n\nError in instance {self._name}\n\n" + error_message + f"\n\nError in instance {self.name}\n\n" + error_message ) diff --git a/src/ansys/mapdl/core/mapdl_console.py b/src/ansys/mapdl/core/mapdl_console.py index 9314bb6e17..df8196ba76 100644 --- a/src/ansys/mapdl/core/mapdl_console.py +++ b/src/ansys/mapdl/core/mapdl_console.py @@ -235,6 +235,8 @@ def kill(self): self._log.debug("Killed process %d", self._process.pid) @property - def _name(self): + def name(self): """Instance unique identifier.""" - return f"Console_PID_{self._process.pid}" + if not self._name: + self._name = f"Console_PID_{self._process.pid}" + return self._name diff --git a/src/ansys/mapdl/core/mapdl_corba.py b/src/ansys/mapdl/core/mapdl_corba.py index ce8c6b200d..3a52b7209c 100644 --- a/src/ansys/mapdl/core/mapdl_corba.py +++ b/src/ansys/mapdl/core/mapdl_corba.py @@ -401,8 +401,10 @@ def _close_output(self): self._outfile = None @property - def _name(self): + def name(self): """Instance unique identifier.""" - if hasattr(self, "_corba_key"): - return f"CORBA_PID_{self._corba_key}" - return f"CORBA_INSTANCE_{id(self)}" + if not self._name: + if hasattr(self, "_corba_key"): + self._name = f"CORBA_PID_{self._corba_key}" + self._name = f"CORBA_INSTANCE_{id(self)}" + return self._name diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 74cb82d034..0181ca0467 100755 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -2322,13 +2322,13 @@ def cmatrix( super().cmatrix(symfac, condname, numcond, grndkey, capname, **kwargs) @property - def _name(self): + def name(self): """Instance unique identifier.""" - if self._ip or self._port: - return f"GRPC_{self._ip}:{self._port}" - return f"GRPC_instance_{id(self)}" - - def get_name(self): + if not self._name: + if self._ip or self._port: + self._name = f"GRPC_{self._ip}:{self._port}" + else: + self._name = f"GRPC_instance_{id(self)}" return self._name @property diff --git a/src/ansys/mapdl/core/parameters.py b/src/ansys/mapdl/core/parameters.py index 715856267e..80af200cca 100644 --- a/src/ansys/mapdl/core/parameters.py +++ b/src/ansys/mapdl/core/parameters.py @@ -321,14 +321,17 @@ def __getitem__(self, key): raise IndexError("%s not a valid parameter_name" % key) parm = parameters[key] - if parm["type"] in ["ARRAY", "TABLE"]: + if parm["type"] in ["ARRAY", "TABLE"]: # Array case try: return self._get_parameter_array(key, parm["shape"]) except ValueError: # allow a second attempt return self._get_parameter_array(key, parm["shape"]) - - return parm["value"] + else: + if "grpc" in self._mapdl.name.lower() and parm["type"] not in ["CHARACTER"]: + return self._mapdl.scalar_param(key) # Only works with numbers + else: + return parm["value"] def __setitem__(self, key, value): """Set a parameter""" diff --git a/tests/test_commands.py b/tests/test_commands.py index eb90b58ca3..7e998eb556 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -351,7 +351,7 @@ def test_inquire_functions(mapdl, func): ARGS_INQ_FUNC[each_arg] for each_arg in func_args if each_arg not in ["self"] ] output = func_(*args) - if "GRPC" in mapdl._name: + if "GRPC" in mapdl.name: assert isinstance(output, (float, int)) else: assert isinstance(output, str) diff --git a/tests/test_logging.py b/tests/test_logging.py index ac90437dfb..b55b8c3292 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -277,7 +277,7 @@ def test_log_to_file(tmpdir): def test_log_instance_name(mapdl): # verify we can access via an instance name - LOG[mapdl._name] == mapdl._log + LOG[mapdl.name] == mapdl._log def test_instance_log_to_file(mapdl, tmpdir): diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 072c1117c5..a322e5548f 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -138,9 +138,16 @@ def warns_in_cdread_error_log(mapdl): @pytest.mark.skip_grpc def test_internal_name_grpc(mapdl): - assert str(mapdl._ip) in mapdl._name - assert str(mapdl._port) in mapdl._name - assert "GRPC" in mapdl._name + + assert str(mapdl._ip) in mapdl.name + assert str(mapdl._port) in mapdl.name + assert "GRPC" in mapdl.name + + assert mapdl.name + assert mapdl.name == mapdl._name + + with pytest.raises(AttributeError): + mapdl.name = "asfd" def test_jobname(mapdl, cleared): @@ -649,6 +656,8 @@ def test_set_get_parameters(mapdl, parm): if isinstance(parm, str): assert mapdl.parameters[parm_name] == parm + elif isinstance(parm, (int, float)): + assert np.allclose(mapdl.parameters[parm_name], parm) else: # For the cases where shape is (X,) # Empty second dimension parm = np.array(parm) diff --git a/tests/test_parameters.py b/tests/test_parameters.py index 62639c6ef7..40dcb14716 100644 --- a/tests/test_parameters.py +++ b/tests/test_parameters.py @@ -198,3 +198,29 @@ def test_contain_iter(mapdl, cleared): mapdl.parameters["THREE"] = 3.0 assert hasattr(mapdl.parameters, "__iter__") assert sorted(["TWO", "THREE"]) == [each for each in mapdl.parameters] + + +@pytest.mark.parametrize("number", [1 / 3, 1 / 7, 0.0181681816816816168168168]) +def test_double_parameter_get(mapdl, number): + # Running grpc method + mapdl.parameters["value"] = number + + precision_single = 9 + precision_double = 12 + assert np.around(mapdl.parameters["value"], precision_single) == np.around( + number, precision_single + ) + assert np.around(mapdl.parameters["value"], precision_double) == np.around( + number, precision_double + ) + + # to use the alternative method (single precision) + mapdl_name = mapdl._name + mapdl._name = "dummy" + assert np.around(mapdl.parameters["value"], precision_single) == np.around( + number, precision_single + ) + assert np.around(mapdl.parameters["value"], precision_double) != np.around( + number, precision_double + ) + mapdl._name = mapdl_name