# 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["vm152"])


List the files in current directory

mapdl.list_files()

['11_blades_mode_1_ND_0.csv', 'BeforeMapping.db', 'ExampleMapping.db', 'HexBeam.cdb', 'Mapping.db', 'SCRATCH', 'TABLE_1', '__tmp_sys_out_akdppfffyq__', '_tmp.iges', 'anaconda-post.log', 'anstmp', 'ansys_inc', 'baseModel.cdb', 'baseModel.iges', 'bin', 'bracket.iges', 'dev', 'etc', 'file.DSP', 'file.ans_log', 'file.emat', 'file.err', 'file.esav', 'file.full', 'file.ldhi', 'file.log', 'file.mlv', 'file.mntr', 'file.mode', 'file.page', 'file.r001', 'file.rdb', 'file.rst', 'file.rstp', 'file.rsx', 'file.rth', 'file.stat', 'file000.jpg', 'file000.png', 'file001.jpg', 'file001.png', 'file002.jpg', 'file002.png', 'file003.jpg', 'file003.png', 'file004.jpg', 'file004.png', 'file005.jpg', 'file005.png', 'file006.png', 'file007.png', 'file008.png', 'home', 'lib', 'lib64', 'mappedHI.dat', 'media', 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'sector.cdb', 'srv', 'sys', 'tmp', 'tmp.cdb', 'usr', 'var', 'vm1.vrt', 'vm152.vrt', 'vm153.vrt', 'vm5.vrt']


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

fullfile = mapdl.jobname + ".full"
k = mm.stiff(fname=fullfile)
k

Sparse APDLMath Matrix (27, 27)


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

ky = k.asarray()
ky

<27x27 sparse matrix of type '<class 'numpy.float64'>'
with 77 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=fullfile)
b.norm()

0.12181823800246586


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()

7.774533362578086e-06


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())

Residual error: 5.251548690945302e-16


Summary of all allocated APDLMath Objects

mm.status()

APDLMATH PARAMETER STATUS-  (      5 PARAMETERS DEFINED)

Name                   Type            Mem. (MB)       Dims            Workspace

PZAGHZ                SMAT            0.001           [27:27]         1
FKMCZT                VEC             0.000           27              1
UAEQEZ                VEC             0.000           27              1
SJTJEA                LSENGINE        --              --              1


Delete all APDLMath Objects

mm.free()


## Stop mapdl#

mapdl.exit()


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

Gallery generated by Sphinx-Gallery