Skip to content

Commit 8f67d41

Browse files
FredAnsakaszynski
andauthored
New option to automatically create a SciPy Array + Fix for Float Spar… (#568)
* New option to automatically create a SciPy Array + Fix for Float Sparse Matrices * Update ansys/mapdl/core/math.py Co-authored-by: Alex Kaszynski <[email protected]> * Update ansys/mapdl/core/math.py Co-authored-by: Alex Kaszynski <[email protected]> * Apply suggestions from code review * Apply suggestions from code review Co-authored-by: Alex Kaszynski <[email protected]>
1 parent 7152e5e commit 8f67d41

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

ansys/mapdl/core/math.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
MYCTYPE = {np.int32: 'I',
2121
np.int64: 'L',
22-
np.single: 'S',
22+
np.single: 'F',
2323
np.double: 'D',
2424
np.complex64: 'C',
2525
np.complex128: 'Z'}
@@ -410,7 +410,7 @@ def matrix(self, matrix, name=None, triu=False):
410410
return AnsDenseMat(name, self._mapdl)
411411

412412
def load_matrix_from_file(self, dtype=np.double, fname="file.full",
413-
mat_id="STIFF"):
413+
mat_id="STIFF", asarray=False):
414414
"""Import a matrix from an existing FULL file.
415415
416416
Parameters
@@ -436,16 +436,21 @@ def load_matrix_from_file(self, dtype=np.double, fname="file.full",
436436
* ``"K"``_RE - Real part of the stiffness matrix
437437
* ``"K"``_IM - Imaginary part of the stiffness matrix
438438
439+
asarray : bool, optional
440+
Return a `scipy` array rather than an APDLMath matrix.
439441
440442
"""
441443
name = id_generator()
442444
self._mapdl._log.info("Calling MAPDL to extract the %s matrix from %s",
443445
mat_id, fname)
444446
self._mapdl.run(f"*SMAT,{name},{MYCTYPE[dtype]},IMPORT,FULL,{fname},{mat_id}",
445447
mute=True)
446-
return AnsSparseMat(name, self._mapdl)
448+
ans_sparse_mat = AnsSparseMat(name, self._mapdl)
449+
if asarray:
450+
return self._mapdl._mat_data(ans_sparse_mat.id)
451+
return ans_sparse_mat
447452

448-
def stiff(self, dtype=np.double, fname="file.full"):
453+
def stiff(self, dtype=np.double, fname="file.full", asarray=False):
449454
"""Load the stiffness matrix from a full file.
450455
451456
Parameters
@@ -456,6 +461,9 @@ def stiff(self, dtype=np.double, fname="file.full"):
456461
fname : str, optional
457462
Filename to read the matrix from.
458463
464+
asarray : bool, optional
465+
Return a `scipy` array rather than an APDLMath matrix.
466+
459467
Examples
460468
--------
461469
>>> k = mapdl.math.stiff()
@@ -468,9 +476,9 @@ def stiff(self, dtype=np.double, fname="file.full"):
468476
<60x60 sparse matrix of type '<class 'numpy.float64'>'
469477
with 1734 stored elements in Compressed Sparse Row format>
470478
"""
471-
return self.load_matrix_from_file(dtype, fname, "STIFF")
479+
return self.load_matrix_from_file(dtype, fname, "STIFF", asarray)
472480

473-
def mass(self, dtype=np.double, fname="file.full"):
481+
def mass(self, dtype=np.double, fname="file.full", asarray=False):
474482
"""Load the mass matrix from a full file.
475483
476484
Parameters
@@ -481,6 +489,9 @@ def mass(self, dtype=np.double, fname="file.full"):
481489
fname : str, optional
482490
Filename to read the matrix from.
483491
492+
asarray : bool, optional
493+
Return a `scipy` array rather than an APDLMath matrix.
494+
484495
Examples
485496
--------
486497
>>> m = mapdl.math.mass()
@@ -494,9 +505,9 @@ def mass(self, dtype=np.double, fname="file.full"):
494505
<60x60 sparse matrix of type '<class 'numpy.float64'>'
495506
with 1734 stored elements in Compressed Sparse Row format>
496507
"""
497-
return self.load_matrix_from_file(dtype, fname, "MASS")
508+
return self.load_matrix_from_file(dtype, fname, "MASS", asarray)
498509

499-
def damp(self, dtype=np.double, fname="file.full"):
510+
def damp(self, dtype=np.double, fname="file.full", asarray=False):
500511
"""Load the damping matrix from the full file
501512
502513
Parameters
@@ -507,6 +518,9 @@ def damp(self, dtype=np.double, fname="file.full"):
507518
fname : str, optional
508519
Filename to read the matrix from.
509520
521+
asarray : bool, optional
522+
Return a `scipy` array rather than an APDLMath matrix.
523+
510524
Examples
511525
--------
512526
>>> m = mapdl.math.damp()
@@ -521,9 +535,9 @@ def damp(self, dtype=np.double, fname="file.full"):
521535
with 1734 stored elements in Compressed Sparse Row format>
522536
523537
"""
524-
return self.load_matrix_from_file(dtype, fname, "DAMP")
538+
return self.load_matrix_from_file(dtype, fname, "DAMP", asarray)
525539

526-
def get_vec(self, dtype=np.double, fname="file.full", mat_id="RHS"):
540+
def get_vec(self, dtype=np.double, fname="file.full", mat_id="RHS", asarray=False):
527541
"""Load a vector from a file.
528542
529543
Parameters
@@ -543,6 +557,9 @@ def get_vec(self, dtype=np.double, fname="file.full", mat_id="RHS"):
543557
* ``"BACK"`` - nodal mapping vector (internal to user)
544558
* ``"FORWARD"`` - nodal mapping vector (user to internal)
545559
560+
asarray : bool, optional
561+
Return a `scipy` array rather than an APDLMath matrix.
562+
546563
Returns
547564
--------
548565
ansys.mapdl.math.AnsVec
@@ -560,7 +577,10 @@ def get_vec(self, dtype=np.double, fname="file.full", mat_id="RHS"):
560577
mat_id, fname)
561578
self._mapdl.run(f"*VEC,{name},{MYCTYPE[dtype]},IMPORT,FULL,{fname},{mat_id}",
562579
mute=True)
563-
return AnsVec(name, self._mapdl)
580+
ans_vec = AnsVec(name, self._mapdl)
581+
if asarray:
582+
return self._mapdl._vec_data(ans_vec.id)
583+
return ans_vec
564584

565585
def set_vec(self, data, name=None):
566586
"""Push a numpy array or Python list to the MAPDL Memory
@@ -593,7 +613,7 @@ def set_vec(self, data, name=None):
593613
self._set_vec(name, data)
594614
return AnsVec(name, self._mapdl)
595615

596-
def rhs(self, dtype=np.double, fname="file.full"):
616+
def rhs(self, dtype=np.double, fname="file.full", asarray=False):
597617
"""Return the load vector from a full file.
598618
599619
Parameters
@@ -604,6 +624,9 @@ def rhs(self, dtype=np.double, fname="file.full"):
604624
fname : str, optional
605625
Filename to read the vector from. Defaults to ``"file.full"``.
606626
627+
asarray : bool, optional
628+
Return a `scipy` array rather than an APDLMath matrix.
629+
607630
Returns
608631
-------
609632
ansys.mapdl.math.AnsVec
@@ -615,7 +638,7 @@ def rhs(self, dtype=np.double, fname="file.full"):
615638
APDLMath Vector Size 126
616639
617640
"""
618-
return self.get_vec(dtype, fname, "RHS")
641+
return self.get_vec(dtype, fname, "RHS", asarray)
619642

620643
def svd(self, mat, thresh='', sig='', v='', **kwargs):
621644
"""Apply an SVD Algorithm on a matrix.
@@ -993,7 +1016,10 @@ def _send_sparse(self, mname, arr, sym, dtype, chunk_size):
9931016

9941017
# data vector
9951018
dataname = f'{mname}_DATA'
996-
self.set_vec(arr.data, dataname)
1019+
ans_vec = self.set_vec(arr.data, dataname)
1020+
if dtype is None:
1021+
info = self._mapdl._data_info(ans_vec.id)
1022+
dtype = ANSYS_VALUE_TYPE[info.stype]
9971023

9981024
# indptr vector
9991025
indptrname = f'{mname}_IND'
@@ -1006,10 +1032,9 @@ def _send_sparse(self, mname, arr, sym, dtype, chunk_size):
10061032
self.set_vec(idx, indxname)
10071033

10081034
flagsym = 'TRUE' if sym else 'FALSE'
1009-
self._mapdl.run(f'*SMAT,{mname},D,ALLOC,CSR,{indptrname},{indxname},'
1035+
self._mapdl.run(f'*SMAT,{mname},{MYCTYPE[dtype]},ALLOC,CSR,{indptrname},{indxname},'
10101036
f'{dataname},{flagsym}')
10111037

1012-
10131038
class ApdlMathObj:
10141039
"""Common class for MAPDL Math objects"""
10151040

0 commit comments

Comments
 (0)