Use APDLMath To Perform Sparse Factorization and Solve Operations

Using APDLMath, you can solve linear systems of equations based on sparse or dense matrices.

from ansys.mapdl.core import launch_mapdl
from ansys.mapdl.core.examples import vmfiles

# Start MAPDL as a service and create an APDLMath object.
mapdl = launch_mapdl()
mm = mapdl.math

Factorize and Solve Sparse Linear Systems

First, run a MAPDL solve to create a .full file We use a model from the official verification manual.

After a solve command, the full contains the assemblied stiffness matrix, mass matrix, and the load vector.

out = mapdl.input(vmfiles['vm153'])

List the files in current directory

mapdl.list_files()

Out:

['HexBeam.cdb', 'PRSMEMB.DSP', 'PRSMEMB.emat', 'PRSMEMB.esav', 'PRSMEMB.full', 'PRSMEMB.mntr', 'PRSMEMB.mode', 'PRSMEMB.rst', 'PRSMEMB000.jpg', 'PRSMEMB001.jpg', 'PRSMEMB002.jpg', 'PRSMEMB003.jpg', 'PRSMEMB004.jpg', 'PRSMEMB005.jpg', 'PRSMEMB006.jpg', 'PRSMEMB007.jpg', 'PRSMEMB008.jpg', 'PRSMEMB009.jpg', 'PRSMEMB010.jpg', 'PRSMEMB011.jpg', 'SCRATCH', 'SOLVIT.MAC', 'TABLE_1', 'TABLE_2', '__tmp_sys_out__', '_input_tmp_.inp', '_input_tmp_.out', '_tmp.iges', 'anaconda-post.log', 'anstmp', 'ansys_inc', 'bin', 'bkvevbojef.txt', 'bracket.iges', 'dev', 'etc', 'file.DSP', 'file.err', 'file.esav', 'file.full', 'file.ldhi', 'file.lock', 'file.log', 'file.mlv', 'file.mntr', 'file.mode', 'file.page', 'file.r001', 'file.rdb', 'file.rth', 'file.stat', 'home', 'lib', 'lib64', 'media', 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'scugkmexsx.txt', 'srv', 'sys', 'tmp', 'tmp.cdb', 'usr', 'var', 'vm1.dat', 'vm1.vrt', 'vm153.dat', 'vm153.vrt', 'wing.dat']

Extract the Stiffness matrix from the FULL file, in a sparse matrix format.

You can get help on the stiff function with help(mm.stiff)

Printout the dimensions of this Sparse Matrix

k = mm.stiff(fname='PRSMEMB.full')
k

Out:

Sparse APDLMath Matrix (126, 126)

Get a copy of the K Sparse Matrix as a Numpy Array

ky = k.asarray()
ky

Out:

<126x126 sparse matrix of type '<class 'numpy.float64'>'
    with 738 stored elements in Compressed Sparse Row format>

Extract the load vector from the FULL file.

Printout the norm of this vector.

b = mm.rhs(fname='PRSMEMB.full')
b.norm()

Out:

3.472960080375275e-05

Get a copy of the load vector as a numpy array

by = b.asarray()

Factorize the Stifness Matrix using the MAPDL DSPARSE solver

s = mm.factorize(k)

Solve the linear system

x = s.solve(b)

Print the norm of the solution vector

x.norm()

Out:

5.815928297030501e-08

We check the accuracy of the solution, by verifying that

\(KX - B = 0\)

kx = k.dot(x)
kx -= b
print('Residual error:', kx.norm()/b.norm())

Out:

Residual error: 1.5667414206515824e-15

Summary of all allocated APDLMath Objects

mm.status()

Out:

APDLMATH PARAMETER STATUS-  (      5 PARAMETERS DEFINED)

  Name                   Type            Mem. (MB)       Dims            Workspace

   FEHGAB                SMAT            0.011           [126:126]               1
   CTKIYM                VEC             0.001           126             1
   HBEPSM                VEC             0.001           126             1
   WSPCQL                VEC             0.001           126             1
   PEJZOP                LSENGINE        --              --              1

Delete all APDLMath Objects

mm.free()

Total running time of the script: ( 0 minutes 0.806 seconds)

Gallery generated by Sphinx-Gallery