Skip to content

Commit 2945af3

Browse files
germa89akaszynski
andcommitted
Avoiding nodes mapping in PyMAPDL. (#1005)
* Adding sanity check and default dtype to `get_vec` * Removing not allowed keywords. * Ensuring returning type when using `asarray` argument * Adding unit tests * Fixing unit tests * Adding asarray arg to test. * Update src/ansys/mapdl/core/math.py Co-authored-by: Alex Kaszynski <[email protected]>
1 parent 58077cc commit 2945af3

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/ansys/mapdl/core/math.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,6 @@ def load_matrix_from_file(
445445
* ``"STIFF"`` - Stiffness matrix
446446
* ``"MASS"`` - Mass matrix
447447
* ``"DAMP"`` - Damping matrix
448-
* ``"NOD2BCS"`` - Mapping vector relating the full set of
449-
nodal DOFs to the subset that the solver uses
450-
* ``"USR2BCS"`` - Mapping vector relating the full set of
451-
external nodal DOFs to the subset that the solver uses
452448
* ``"GMAT"`` - Constraint equation matrix
453449
* ``"K_RE"`` - Real part of the stiffness matrix
454450
* ``"K_IM"`` - Imaginary part of the stiffness matrix
@@ -472,8 +468,8 @@ def load_matrix_from_file(
472468
"STIFF",
473469
"MASS",
474470
"DAMP",
475-
"NOD2BCS",
476-
"USR2BCS",
471+
# "NOD2BCS", # Not allowed since #990
472+
# "USR2BCS",
477473
"GMAT",
478474
"K_RE",
479475
"K_IM",
@@ -648,7 +644,7 @@ def damp(self, dtype=np.double, fname="file.full", asarray=False):
648644
fname = self._load_file(fname)
649645
return self.load_matrix_from_file(dtype, fname, "DAMP", asarray)
650646

651-
def get_vec(self, dtype=np.double, fname="file.full", mat_id="RHS", asarray=False):
647+
def get_vec(self, dtype=None, fname="file.full", mat_id="RHS", asarray=False):
652648
"""Load a vector from a file.
653649
654650
Parameters
@@ -664,8 +660,10 @@ def get_vec(self, dtype=np.double, fname="file.full", mat_id="RHS", asarray=Fals
664660
665661
* ``"RHS"`` - Load vector
666662
* ``"GVEC"`` - Constraint equation constant terms
667-
* ``"BACK"`` - nodal mapping vector (internal to user)
663+
* ``"BACK"`` - nodal mapping vector (internal to user).
664+
If this is used, the default ``dtype`` is ``np.int32``.
668665
* ``"FORWARD"`` - nodal mapping vector (user to internal)
666+
If this is used, the default ``dtype`` is ``np.int32``.
669667
asarray : bool, optional
670668
Return a `scipy` array rather than an APDLMath matrix.
671669
@@ -686,13 +684,24 @@ def get_vec(self, dtype=np.double, fname="file.full", mat_id="RHS", asarray=Fals
686684
"Call MAPDL to extract the %s vector from the file %s", mat_id, fname
687685
)
688686

687+
if mat_id.upper() not in ["RHS", "GVEC", "BACK", "FORWARD"]:
688+
raise ValueError(
689+
f"The 'mat_id' value ({mat_id}) is not allowed."
690+
'Only "RHS", "GVEC", "BACK", or "FORWARD" are allowed.'
691+
)
692+
693+
if mat_id.upper() in ["BACK", "FORWARD"] and not dtype:
694+
dtype = np.int32
695+
else:
696+
dtype = np.double
697+
689698
fname = self._load_file(fname)
690699
self._mapdl.run(
691700
f"*VEC,{name},{MYCTYPE[dtype]},IMPORT,FULL,{fname},{mat_id}", mute=True
692701
)
693702
ans_vec = AnsVec(name, self._mapdl)
694703
if asarray:
695-
return self._mapdl._vec_data(ans_vec.id)
704+
return self._mapdl._vec_data(ans_vec.id).astype(dtype, copy=False)
696705
return ans_vec
697706

698707
def set_vec(self, data, name=None):

tests/test_math.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,19 @@ def test_solve_py(mapdl, mm, cube_solve):
388388
assert np.allclose(rhs0, rhs1)
389389

390390

391+
@pytest.mark.parametrize(
392+
"vec_type", ["RHS", "BACK", pytest.param("dummy", marks=pytest.mark.xfail)]
393+
)
394+
def test_get_vec(mapdl, mm, cube_solve, vec_type):
395+
if vec_type.upper() == "BACK":
396+
vec = mm.get_vec(mat_id=vec_type, asarray=True) # To test asarray arg.
397+
assert vec.dtype == np.int32
398+
else:
399+
vec = mm.get_vec(mat_id=vec_type).asarray()
400+
assert vec.dtype == np.double
401+
assert vec.shape
402+
403+
391404
def test_get_vector(mm):
392405
vec = mm.ones(10)
393406
arr = vec.asarray()

0 commit comments

Comments
 (0)