Skip to content

Commit 12a64c2

Browse files
fix: allow numpy types for parameters (#3720)
* feat: allowing numpy types for setting parameters * docs: adding comment * chore: adding changelog file 3720.fixed.md [dependabot-skip] * fix: parameters length to 32+ * feat: update to 32 chars variable * fix: test --------- Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 062ac32 commit 12a64c2

File tree

5 files changed

+73
-18
lines changed

5 files changed

+73
-18
lines changed

doc/changelog.d/3720.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix: allow numpy types for parameters

src/ansys/mapdl/core/mapdl_core.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888

8989
from ansys.mapdl.core.post import PostProcessing
9090

91+
MAX_PARAM_CHARS = 32
92+
9193
DEBUG_LEVELS = Literal["DEBUG", "INFO", "WARNING", "ERROR"]
9294

9395
VALID_DEVICES = ["PNG", "TIFF", "VRML", "TERM", "CLOSE"]
@@ -2491,12 +2493,14 @@ def _check_parameter_name(self, param_name):
24912493

24922494
param_name = param_name.strip()
24932495

2494-
match_valid_parameter_name = r"^[a-zA-Z_][a-zA-Z\d_\(\),\s\%]{0,31}$"
2496+
match_valid_parameter_name = (
2497+
r"^[a-zA-Z_][a-zA-Z\d_\(\),\s\%]{0," + f"{MAX_PARAM_CHARS-1}" + r"}$"
2498+
)
24952499
# Using % is allowed, because of substitution, but it is very likely MAPDL will complain.
24962500
if not re.search(match_valid_parameter_name, param_name):
24972501
raise ValueError(
2498-
f"The parameter name `{param_name}` is an invalid parameter name."
2499-
"Only letters, numbers and `_` are permitted, up to 32 characters long."
2502+
f"The parameter name `{param_name}` is an invalid parameter name. "
2503+
f"Only letters, numbers and `_` are permitted, up to {MAX_PARAM_CHARS} characters long. "
25002504
"It cannot start with a number either."
25012505
)
25022506

@@ -2520,7 +2524,7 @@ def _check_parameter_name(self, param_name):
25202524

25212525
# Using leading underscored parameters
25222526
match_reserved_leading_underscored_parameter_name = (
2523-
r"^_[a-zA-Z\d_\(\),\s_]{1,31}[a-zA-Z\d\(\),\s]$"
2527+
r"^_[a-zA-Z\d_\(\),\s_]{1," + f"{MAX_PARAM_CHARS}" + r"}[a-zA-Z\d\(\),\s]$"
25242528
)
25252529
# If it also ends in underscore, this won't be triggered.
25262530
if re.search(match_reserved_leading_underscored_parameter_name, param_name):

src/ansys/mapdl/core/parameters.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
from ansys.mapdl.core.errors import MapdlRuntimeError
4040
from ansys.mapdl.core.mapdl import MapdlBase
41+
from ansys.mapdl.core.mapdl_core import MAX_PARAM_CHARS
4142
from ansys.mapdl.core.misc import supress_logging
4243

4344
ROUTINE_MAP = {
@@ -354,7 +355,7 @@ def __repr__(self):
354355
value_str = str(info["value"])
355356
else:
356357
continue
357-
lines.append("%-32s : %s" % (key, value_str))
358+
lines.append(f"%-{MAX_PARAM_CHARS}s : %s" % (key, value_str))
358359
return "\n".join(lines)
359360

360361
def __getitem__(self, key):
@@ -493,22 +494,27 @@ def _set_parameter(self, name, value):
493494
----------
494495
name : str
495496
An alphanumeric name used to identify this parameter. Name
496-
may be up to 32 characters, beginning with a letter and
497-
containing only letters, numbers, and underscores.
497+
may be up to 32 character or the value given in
498+
:attr:`ansys.mapdl.core.mapdl_core.MAX_PARAM_CHARS`, beginning with
499+
a letter and containing only letters, numbers, and underscores.
498500
Examples: ``"ABC" "A3X" "TOP_END"``.
499501
500502
"""
501-
if not isinstance(value, (str, int, float)):
503+
if not isinstance(value, (str, int, float, np.integer, np.floating)):
502504
raise TypeError("``Parameter`` must be either a float, int, or string")
503505

504-
if isinstance(value, str) and len(value) >= 32:
505-
raise ValueError("Length of ``value`` must be 32 characters or less")
506+
if isinstance(value, str) and len(value) > MAX_PARAM_CHARS:
507+
raise ValueError(
508+
f"Length of ``value`` must be {MAX_PARAM_CHARS} characters or less"
509+
)
506510

507511
if not isinstance(name, str):
508512
raise TypeError("``name`` must be a string")
509513

510-
if len(name) >= 32:
511-
raise ValueError("Length of ``name`` must be 32 characters or less")
514+
if len(name) > MAX_PARAM_CHARS:
515+
raise ValueError(
516+
f"Length of ``name`` must be {MAX_PARAM_CHARS} characters or less"
517+
)
512518

513519
# delete the parameter if it exists as an array
514520
parm = self._parm
@@ -830,8 +836,8 @@ def interp_star_status(status):
830836
# line will contain either a character, scalar, or array
831837
name = items[0]
832838
if len(items) == 2 or "CHARACTER" in items[-1].upper():
833-
name = line[:32].strip()
834-
value = line.replace(items[-1], "")[33:].strip()
839+
name = line[:MAX_PARAM_CHARS].strip()
840+
value = line.replace(items[-1], "")[(MAX_PARAM_CHARS + 1) :].strip()
835841
parameters[name] = {"type": "CHARACTER", "value": value}
836842

837843
elif len(items) == 3:

tests/test_mapdl.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,15 +779,20 @@ def test_set_parameters_string_spaces(mapdl, cleared):
779779

780780

781781
def test_set_parameters_too_long(mapdl, cleared):
782+
from ansys.mapdl.core.mapdl_core import MAX_PARAM_CHARS
783+
784+
parm_name = "a" * (MAX_PARAM_CHARS + 1)
782785
with pytest.raises(
783-
ValueError, match="Length of ``name`` must be 32 characters or less"
786+
ValueError,
787+
match=f"The parameter name `{parm_name}` is an invalid parameter name.* {MAX_PARAM_CHARS} characters long",
784788
):
785-
mapdl.parameters["a" * 32] = 2
789+
mapdl.parameters[parm_name] = 2
786790

787791
with pytest.raises(
788-
ValueError, match="Length of ``value`` must be 32 characters or less"
792+
ValueError,
793+
match=f"Length of ``value`` must be {MAX_PARAM_CHARS} characters or less",
789794
):
790-
mapdl.parameters["asdf"] = "a" * 32
795+
mapdl.parameters["asdf"] = "a" * (MAX_PARAM_CHARS + 1)
791796

792797

793798
def test_builtin_parameters(mapdl, cleared):

tests/test_parameters.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,3 +493,42 @@ def test_failing_get_routine(mapdl, caplog, value):
493493
assert routine == ROUTINE_MAP[0]
494494

495495
mapdl.logger.setLevel(prev_level)
496+
497+
498+
@pytest.mark.parametrize(
499+
"parameter",
500+
[
501+
"asdf",
502+
"32_chars_length",
503+
1,
504+
1.0,
505+
np.array([1, 2, 3]),
506+
np.array([1, 3])[0],
507+
np.array([1.0, 2.2, 3.5]),
508+
np.array([1.03, 3.9])[0],
509+
np.array([1.4, 2.3], dtype=np.int32),
510+
np.array([1.4, 2.3], dtype=np.int32)[0],
511+
np.array([1.4, 2.3], dtype=np.int64),
512+
np.array([1.4, 2.3], dtype=np.int64)[0],
513+
],
514+
)
515+
def test_parameter_types(mapdl, cleared, parameter):
516+
mapdl.parameters["temp_arr"] = parameter
517+
518+
if isinstance(parameter, np.ndarray):
519+
# Reshaping arrays until #3717 is closed
520+
assert np.allclose(
521+
parameter.reshape((-1, 1)), mapdl.parameters["temp_arr"].reshape((-1, 1))
522+
)
523+
else:
524+
assert parameter == mapdl.parameters["temp_arr"]
525+
526+
if isinstance(parameter, (int, np.integer)):
527+
# All numbers in MAPDL are stored as float.
528+
assert isinstance(mapdl.parameters["temp_arr"], float)
529+
530+
elif isinstance(parameter, (float, np.floating)):
531+
assert isinstance(mapdl.parameters["temp_arr"], float)
532+
533+
else:
534+
assert isinstance(mapdl.parameters["temp_arr"], type(parameter))

0 commit comments

Comments
 (0)