Skip to content

Commit e092e06

Browse files
authored
Double precision parameter retrieving (#1274)
* Using gRPC method for getting parameter * Adding unit tests * Refactoring name parameter in mapdl class. * Updating unit test for the name parameter * Using grpc method only for floats. * fixing unit tests * Using attribute exception * Removing all old calls to _name * avoiding coverage.
1 parent e53e694 commit e092e06

File tree

10 files changed

+73
-29
lines changed

10 files changed

+73
-29
lines changed

src/ansys/mapdl/core/logging.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,7 @@ def process(self, msg, kwargs):
182182
# This are the extra parameters sent to log
183183
kwargs["extra"][
184184
"instance_name"
185-
] = (
186-
self.extra.get_name()
187-
) # here self.extra is the argument pass to the log records.
185+
] = self.extra.name # here self.extra is the argument pass to the log records.
188186
return msg, kwargs
189187

190188
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):
476474
instance_logger = PymapdlCustomAdapter(
477475
self._make_child_logger(name, level), mapdl_instance
478476
)
479-
elif isinstance(name, None):
477+
elif not name: # pragma: no cover
480478
instance_logger = PymapdlCustomAdapter(
481479
self._make_child_logger("NO_NAMED_YET", level), mapdl_instance
482480
)

src/ansys/mapdl/core/mapdl.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ def __init__(
150150
**start_parm,
151151
):
152152
"""Initialize connection with MAPDL."""
153+
self._name = None # For naming the instance.
153154
self._show_matplotlib_figures = True # for testing
154155
self._query = None
155156
self._exited = False
@@ -187,7 +188,7 @@ def __init__(
187188

188189
# Setting up loggers
189190
self._log = logger.add_instance_logger(
190-
self._name, self, level=loglevel
191+
self.name, self, level=loglevel
191192
) # instance logger
192193
# adding a file handler to the logger
193194
if log_file:
@@ -269,9 +270,12 @@ def inner_wrapper(*args, **kwargs):
269270
setattr(self, name, wrap_bc_listing_function(func))
270271

271272
@property
272-
def _name(self): # pragma: no cover
273-
"""Implemented by child class"""
274-
raise NotImplementedError("Implemented by child class")
273+
def name(self): # pragma: no cover
274+
raise NotImplementedError("Implemented by child classes.")
275+
276+
@name.setter
277+
def name(self, name): # pragma: no cover
278+
raise AttributeError("The name of an instance cannot be changed.")
275279

276280
@property
277281
def queries(self):
@@ -3587,5 +3591,5 @@ def _raise_output_errors(self, response):
35873591
# However, exceptions are recorded in the global logger which do not record
35883592
# information of the instances name, hence we edit the error message.
35893593
raise MapdlRuntimeError(
3590-
f"\n\nError in instance {self._name}\n\n" + error_message
3594+
f"\n\nError in instance {self.name}\n\n" + error_message
35913595
)

src/ansys/mapdl/core/mapdl_console.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ def kill(self):
235235
self._log.debug("Killed process %d", self._process.pid)
236236

237237
@property
238-
def _name(self):
238+
def name(self):
239239
"""Instance unique identifier."""
240-
return f"Console_PID_{self._process.pid}"
240+
if not self._name:
241+
self._name = f"Console_PID_{self._process.pid}"
242+
return self._name

src/ansys/mapdl/core/mapdl_corba.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,10 @@ def _close_output(self):
401401
self._outfile = None
402402

403403
@property
404-
def _name(self):
404+
def name(self):
405405
"""Instance unique identifier."""
406-
if hasattr(self, "_corba_key"):
407-
return f"CORBA_PID_{self._corba_key}"
408-
return f"CORBA_INSTANCE_{id(self)}"
406+
if not self._name:
407+
if hasattr(self, "_corba_key"):
408+
self._name = f"CORBA_PID_{self._corba_key}"
409+
self._name = f"CORBA_INSTANCE_{id(self)}"
410+
return self._name

src/ansys/mapdl/core/mapdl_grpc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2322,13 +2322,13 @@ def cmatrix(
23222322
super().cmatrix(symfac, condname, numcond, grndkey, capname, **kwargs)
23232323

23242324
@property
2325-
def _name(self):
2325+
def name(self):
23262326
"""Instance unique identifier."""
2327-
if self._ip or self._port:
2328-
return f"GRPC_{self._ip}:{self._port}"
2329-
return f"GRPC_instance_{id(self)}"
2330-
2331-
def get_name(self):
2327+
if not self._name:
2328+
if self._ip or self._port:
2329+
self._name = f"GRPC_{self._ip}:{self._port}"
2330+
else:
2331+
self._name = f"GRPC_instance_{id(self)}"
23322332
return self._name
23332333

23342334
@property

src/ansys/mapdl/core/parameters.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,17 @@ def __getitem__(self, key):
321321
raise IndexError("%s not a valid parameter_name" % key)
322322

323323
parm = parameters[key]
324-
if parm["type"] in ["ARRAY", "TABLE"]:
324+
if parm["type"] in ["ARRAY", "TABLE"]: # Array case
325325
try:
326326
return self._get_parameter_array(key, parm["shape"])
327327
except ValueError:
328328
# allow a second attempt
329329
return self._get_parameter_array(key, parm["shape"])
330-
331-
return parm["value"]
330+
else:
331+
if "grpc" in self._mapdl.name.lower() and parm["type"] not in ["CHARACTER"]:
332+
return self._mapdl.scalar_param(key) # Only works with numbers
333+
else:
334+
return parm["value"]
332335

333336
def __setitem__(self, key, value):
334337
"""Set a parameter"""

tests/test_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ def test_inquire_functions(mapdl, func):
351351
ARGS_INQ_FUNC[each_arg] for each_arg in func_args if each_arg not in ["self"]
352352
]
353353
output = func_(*args)
354-
if "GRPC" in mapdl._name:
354+
if "GRPC" in mapdl.name:
355355
assert isinstance(output, (float, int))
356356
else:
357357
assert isinstance(output, str)

tests/test_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def test_log_to_file(tmpdir):
277277

278278
def test_log_instance_name(mapdl):
279279
# verify we can access via an instance name
280-
LOG[mapdl._name] == mapdl._log
280+
LOG[mapdl.name] == mapdl._log
281281

282282

283283
def test_instance_log_to_file(mapdl, tmpdir):

tests/test_mapdl.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,16 @@ def warns_in_cdread_error_log(mapdl):
138138

139139
@pytest.mark.skip_grpc
140140
def test_internal_name_grpc(mapdl):
141-
assert str(mapdl._ip) in mapdl._name
142-
assert str(mapdl._port) in mapdl._name
143-
assert "GRPC" in mapdl._name
141+
142+
assert str(mapdl._ip) in mapdl.name
143+
assert str(mapdl._port) in mapdl.name
144+
assert "GRPC" in mapdl.name
145+
146+
assert mapdl.name
147+
assert mapdl.name == mapdl._name
148+
149+
with pytest.raises(AttributeError):
150+
mapdl.name = "asfd"
144151

145152

146153
def test_jobname(mapdl, cleared):
@@ -649,6 +656,8 @@ def test_set_get_parameters(mapdl, parm):
649656

650657
if isinstance(parm, str):
651658
assert mapdl.parameters[parm_name] == parm
659+
elif isinstance(parm, (int, float)):
660+
assert np.allclose(mapdl.parameters[parm_name], parm)
652661
else:
653662
# For the cases where shape is (X,) # Empty second dimension
654663
parm = np.array(parm)

tests/test_parameters.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,29 @@ def test_contain_iter(mapdl, cleared):
198198
mapdl.parameters["THREE"] = 3.0
199199
assert hasattr(mapdl.parameters, "__iter__")
200200
assert sorted(["TWO", "THREE"]) == [each for each in mapdl.parameters]
201+
202+
203+
@pytest.mark.parametrize("number", [1 / 3, 1 / 7, 0.0181681816816816168168168])
204+
def test_double_parameter_get(mapdl, number):
205+
# Running grpc method
206+
mapdl.parameters["value"] = number
207+
208+
precision_single = 9
209+
precision_double = 12
210+
assert np.around(mapdl.parameters["value"], precision_single) == np.around(
211+
number, precision_single
212+
)
213+
assert np.around(mapdl.parameters["value"], precision_double) == np.around(
214+
number, precision_double
215+
)
216+
217+
# to use the alternative method (single precision)
218+
mapdl_name = mapdl._name
219+
mapdl._name = "dummy"
220+
assert np.around(mapdl.parameters["value"], precision_single) == np.around(
221+
number, precision_single
222+
)
223+
assert np.around(mapdl.parameters["value"], precision_double) != np.around(
224+
number, precision_double
225+
)
226+
mapdl._name = mapdl_name

0 commit comments

Comments
 (0)