# Performing 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', 'LatheCutter.anf', '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_adrhkfvpcj__', '__tmp_sys_out_ayautcpzaq__', '__tmp_sys_out_csxfakijfp__', '__tmp_sys_out_dkumuphatp__', '__tmp_sys_out_dqwhcktetg__', '__tmp_sys_out_eqayjiufmu__', '__tmp_sys_out_evfxeruvgp__', '__tmp_sys_out_fezoaqqoqu__', '__tmp_sys_out_fhrrgjefyq__', '__tmp_sys_out_hrkwtfokfi__', '__tmp_sys_out_ibtdnjhwxn__', '__tmp_sys_out_idmlricmdr__', '__tmp_sys_out_kcsxajcgtm__', '__tmp_sys_out_kfgaqqpeqs__', '__tmp_sys_out_knxpdtdhoj__', '__tmp_sys_out_kpmmuhblev__', '__tmp_sys_out_lgucivchsm__', '__tmp_sys_out_lnxizgsotz__', '__tmp_sys_out_lygcpgaseh__', '__tmp_sys_out_nngtuwowhk__', '__tmp_sys_out_oegezofnxy__', '__tmp_sys_out_owpiciqgwi__', '__tmp_sys_out_oyoztdnnsi__', '__tmp_sys_out_qpmvwaeanw__', '__tmp_sys_out_rmkxkhnhzq__', '__tmp_sys_out_rotoolusrp__', '__tmp_sys_out_skbgfptplm__', '__tmp_sys_out_sqbncvltyt__', '__tmp_sys_out_vldmwzpjge__', '__tmp_sys_out_wbeeqlrrxb__', '__tmp_sys_out_whyonswsbf__', '__tmp_sys_out_xrzkwxalfj__', '__tmp_sys_out_xtqoqavpsi__', '__tmp_sys_out_ydusultvgl__', '__tmp_sys_out_zexjhvbylz__', '__tmp_sys_out_zgulouuoed__', '_tmp.iges', 'anaconda-post.log', 'anstmp', 'ansys_inc', 'bin', 'bracket.iges', 'dev', 'dzbaascbhr.txt', 'etc', 'file.DSP', 'file.ans_log', 'file.err', 'file.esav', 'file.full', 'file.grph', 'file.ldhi', 'file.lock', 'file.log', 'file.mlv', 'file.mntr', 'file.mode', 'file.page', 'file.r001', 'file.rdb', 'file.rth', 'file.stat', 'file000.png', 'file001.png', 'home', 'lib', 'lib64', 'media', 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'sluutocvhs.txt', 'srv', 'sys', 'tmp', 'tmp.cdb', 'tmp_bejdtgoitw.inp', 'tmp_eohupwcjyl.inp', 'tmp_gscaahnrke.inp', 'tmp_nhwihohano.inp', 'tmp_ocveyfqjhf.inp', 'tmp_ovmfqnfeuv.inp', 'tmp_ppekdshjjq.inp', 'tmp_qehckzlilz.inp', 'tmp_uiwwpbsknw.inp', 'tmp_uoamhckeui.inp', 'usr', 'var', 'vm1.dat', 'vm1.vrt', 'vm153.dat', 'vm153.vrt', 'wing.dat', 'zlbbjemubl.txt']

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.8159282970301164e-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.9359819446794456e-15

Summary of all allocated APDLMath Objects

mm.status()

Out:

APDLMATH PARAMETER STATUS-  (      5 PARAMETERS DEFINED)

Name                   Type            Mem. (MB)       Dims            Workspace

RXJUIS                SMAT            0.011           [126:126]               1
PQYUZE                VEC             0.001           126             1
SVNILG                VEC             0.001           126             1
TVZHXY                VEC             0.001           126             1
VFZGVS                LSENGINE        --              --              1

Delete all APDLMath Objects

mm.free()

stop mapdl

mapdl.exit()

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

Gallery generated by Sphinx-Gallery