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

fix: allow numpy types for parameters #3720

Merged
merged 8 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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/3720.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix: allow numpy types for parameters
12 changes: 8 additions & 4 deletions src/ansys/mapdl/core/mapdl_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@

from ansys.mapdl.core.post import PostProcessing

MAX_PARAM_CHARS = 32

DEBUG_LEVELS = Literal["DEBUG", "INFO", "WARNING", "ERROR"]

VALID_DEVICES = ["PNG", "TIFF", "VRML", "TERM", "CLOSE"]
Expand Down Expand Up @@ -2517,12 +2519,14 @@ def _check_parameter_name(self, param_name):

param_name = param_name.strip()

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

Expand All @@ -2546,7 +2550,7 @@ def _check_parameter_name(self, param_name):

# Using leading underscored parameters
match_reserved_leading_underscored_parameter_name = (
r"^_[a-zA-Z\d_\(\),\s_]{1,31}[a-zA-Z\d\(\),\s]$"
r"^_[a-zA-Z\d_\(\),\s_]{1," + f"{MAX_PARAM_CHARS}" + r"}[a-zA-Z\d\(\),\s]$"
)
# If it also ends in underscore, this won't be triggered.
if re.search(match_reserved_leading_underscored_parameter_name, param_name):
Expand Down
26 changes: 16 additions & 10 deletions src/ansys/mapdl/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

from ansys.mapdl.core.errors import MapdlRuntimeError
from ansys.mapdl.core.mapdl import MapdlBase
from ansys.mapdl.core.mapdl_core import MAX_PARAM_CHARS
from ansys.mapdl.core.misc import supress_logging

ROUTINE_MAP = {
Expand Down Expand Up @@ -354,7 +355,7 @@
value_str = str(info["value"])
else:
continue
lines.append("%-32s : %s" % (key, value_str))
lines.append(f"%-{MAX_PARAM_CHARS}s : %s" % (key, value_str))

Check warning on line 358 in src/ansys/mapdl/core/parameters.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/parameters.py#L358

Added line #L358 was not covered by tests
return "\n".join(lines)

def __getitem__(self, key):
Expand Down Expand Up @@ -493,22 +494,27 @@
----------
name : str
An alphanumeric name used to identify this parameter. Name
may be up to 32 characters, beginning with a letter and
containing only letters, numbers, and underscores.
may be up to 32 character or the value given in
:attr:`ansys.mapdl.core.mapdl_core.MAX_PARAM_CHARS`, beginning with
a letter and containing only letters, numbers, and underscores.
Examples: ``"ABC" "A3X" "TOP_END"``.

"""
if not isinstance(value, (str, int, float)):
if not isinstance(value, (str, int, float, np.integer, np.floating)):
raise TypeError("``Parameter`` must be either a float, int, or string")

if isinstance(value, str) and len(value) >= 32:
raise ValueError("Length of ``value`` must be 32 characters or less")
if isinstance(value, str) and len(value) > MAX_PARAM_CHARS:
raise ValueError(
f"Length of ``value`` must be {MAX_PARAM_CHARS} characters or less"
)

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

if len(name) >= 32:
raise ValueError("Length of ``name`` must be 32 characters or less")
if len(name) > MAX_PARAM_CHARS:
raise ValueError(

Check warning on line 515 in src/ansys/mapdl/core/parameters.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/parameters.py#L515

Added line #L515 was not covered by tests
f"Length of ``name`` must be {MAX_PARAM_CHARS} characters or less"
)

# delete the parameter if it exists as an array
parm = self._parm
Expand Down Expand Up @@ -830,8 +836,8 @@
# line will contain either a character, scalar, or array
name = items[0]
if len(items) == 2 or "CHARACTER" in items[-1].upper():
name = line[:32].strip()
value = line.replace(items[-1], "")[33:].strip()
name = line[:MAX_PARAM_CHARS].strip()
value = line.replace(items[-1], "")[(MAX_PARAM_CHARS + 1) :].strip()
parameters[name] = {"type": "CHARACTER", "value": value}

elif len(items) == 3:
Expand Down
13 changes: 9 additions & 4 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,15 +780,20 @@ def test_set_parameters_string_spaces(mapdl, cleared):


def test_set_parameters_too_long(mapdl, cleared):
from ansys.mapdl.core.mapdl_core import MAX_PARAM_CHARS

parm_name = "a" * (MAX_PARAM_CHARS + 1)
with pytest.raises(
ValueError, match="Length of ``name`` must be 32 characters or less"
ValueError,
match=f"The parameter name `{parm_name}` is an invalid parameter name.* {MAX_PARAM_CHARS} characters long",
):
mapdl.parameters["a" * 32] = 2
mapdl.parameters[parm_name] = 2

with pytest.raises(
ValueError, match="Length of ``value`` must be 32 characters or less"
ValueError,
match=f"Length of ``value`` must be {MAX_PARAM_CHARS} characters or less",
):
mapdl.parameters["asdf"] = "a" * 32
mapdl.parameters["asdf"] = "a" * (MAX_PARAM_CHARS + 1)


def test_builtin_parameters(mapdl, cleared):
Expand Down
39 changes: 39 additions & 0 deletions tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,42 @@ def test_failing_get_routine(mapdl, caplog, value):
assert routine == ROUTINE_MAP[0]

mapdl.logger.setLevel(prev_level)


@pytest.mark.parametrize(
"parameter",
[
"asdf",
"32_chars_length",
1,
1.0,
np.array([1, 2, 3]),
np.array([1, 3])[0],
np.array([1.0, 2.2, 3.5]),
np.array([1.03, 3.9])[0],
np.array([1.4, 2.3], dtype=np.int32),
np.array([1.4, 2.3], dtype=np.int32)[0],
np.array([1.4, 2.3], dtype=np.int64),
np.array([1.4, 2.3], dtype=np.int64)[0],
],
)
def test_parameter_types(mapdl, cleared, parameter):
mapdl.parameters["temp_arr"] = parameter

if isinstance(parameter, np.ndarray):
# Reshaping arrays until #3717 is closed
assert np.allclose(
parameter.reshape((-1, 1)), mapdl.parameters["temp_arr"].reshape((-1, 1))
)
else:
assert parameter == mapdl.parameters["temp_arr"]

if isinstance(parameter, (int, np.integer)):
# All numbers in MAPDL are stored as float.
assert isinstance(mapdl.parameters["temp_arr"], float)

elif isinstance(parameter, (float, np.floating)):
assert isinstance(mapdl.parameters["temp_arr"], float)

else:
assert isinstance(mapdl.parameters["temp_arr"], type(parameter))
Loading