Note
Click here to download the full example code
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.
List the files in current directory
mapdl.list_files()
['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_hzekukiyqd__', '_tmp.iges', 'anaconda-post.log', 'anstmp', 'ansys_inc', '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.rsx', 'file.rth', 'file.stat', 'file000.jpg', 'file000.png', 'file001.png', 'home', 'lib', 'lib64', 'media', 'mnt', 'opt', 'proc', 'root', 'run', 'sbin', 'sector.cdb', 'srv', 'sys', 'tmp', 'tmp.cdb', 'usr', 'var', 'vm1.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
k = mm.stiff(fname="PRSMEMB.full")
k
Sparse APDLMath Matrix (126, 126)
Get a copy of the K Sparse Matrix as a Numpy Array
<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()
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()
5.815928297030763e-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())
Residual error: 1.1768241534178812e-15
Summary of all allocated APDLMath Objects
mm.status()
APDLMATH PARAMETER STATUS- ( 5 PARAMETERS DEFINED)
Name Type Mem. (MB) Dims Workspace
WKIJOS SMAT 0.011 [126:126] 1
JLKYVX VEC 0.001 126 1
NUSACS VEC 0.001 126 1
VMGOVD VEC 0.001 126 1
UWIBSI LSENGINE -- -- 1
Delete all APDLMath Objects
mm.free()
stop mapdl
mapdl.exit()
Total running time of the script: ( 0 minutes 1.151 seconds)