Skip to content

Commit 487f0cd

Browse files
feat: enhance array loading to handle negative values and floats (#4236)
* feat: enhance array loading to handle negative values and floats in tests * feat: add test for loading tables with negative values and floats * chore: adding changelog file 4236.added.md [dependabot-skip] --------- Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com>
1 parent b3a026c commit 487f0cd

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

doc/changelog.d/4236.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enhance array loading to handle negative values and floats

src/ansys/mapdl/core/mapdl_extended.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,10 +2872,10 @@ def load_array(self, name, array):
28722872
self._log.info(f"Generating file for table in {filename}")
28732873
np.savetxt(
28742874
filename,
2875-
array,
2875+
array.ravel(),
28762876
delimiter="",
28772877
header="File generated by PyMAPDL:load_array",
2878-
fmt="%24.18e",
2878+
fmt="%+24.18e", # adding sign.
28792879
)
28802880

28812881
if not self._local:
@@ -2888,7 +2888,7 @@ def load_array(self, name, array):
28882888
n2 = imax
28892889
n3 = kmax
28902890
self.vread(name, filename, n1=n1, n2=n2, n3=n3, label=label, nskip=1)
2891-
fmt = f"({jmax}E24.18)"
2891+
fmt = f"(1E25.18)" # Adding one extra space for the sign
28922892
logger.info("Using *VREAD with format %s in %s", fmt, filename)
28932893
self.run(fmt)
28942894

tests/test_mapdl.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,38 @@ def test_load_table(mapdl, cleared, dim_rows, dim_cols, col_header):
912912
assert np.allclose(mapdl.parameters["my_conv"], my_conv[:, 1:], atol=1e-7)
913913

914914

915+
def test_load_array_negative_and_floats(mapdl, cleared):
916+
my_array = np.array(
917+
[
918+
[0, 0.001],
919+
[120, 0.001],
920+
[130, 0.005],
921+
[-700, 0.005],
922+
[710, 0.002],
923+
[1000, -0.002],
924+
]
925+
)
926+
927+
mapdl.load_array("MY_ARRAY", my_array)
928+
assert np.allclose(mapdl.parameters["MY_ARRAY"], my_array)
929+
930+
931+
def test_load_table_negative_and_floats(mapdl, cleared):
932+
my_array = np.array(
933+
[
934+
[-100, 0.001],
935+
[-20, 0.001],
936+
[10, -0.005],
937+
[700, 0.005],
938+
[710, 0.200001],
939+
[1000, -0.002],
940+
]
941+
)
942+
943+
mapdl.load_table("MY_ARRAY", my_array)
944+
assert np.allclose(mapdl.parameters["MY_ARRAY"].ravel(), my_array[:, 1].ravel())
945+
946+
915947
def test_load_table_error_ascending_row(mapdl, cleared):
916948
my_conv = np.ones((3, 3))
917949
my_conv[1, 0] = 4

0 commit comments

Comments
 (0)