Perform sparse factorization and solve operations#

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

# Perform required imports and start PyAnsys
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Perform required imports.

from ansys.mapdl.core.examples import vmfiles
import matplotlib.pyplot as plt

import ansys.math.core.math as pymath

# Start PyAnsys Math as a server.
mm = pymath.AnsMath()

Factorize and solve sparse linear systems#

Run a MAPDL solve to create a Full file. This code uses a model from the official verification manual.

After a solve command, the FULL file contains the assembled stiffness matrix, mass matrix, and load vector.

out = mm._mapdl.input(vmfiles["vm152"])

List files in current directory#

List the files in current directory.

mm._mapdl.list_files()
['SCRATCH', 'TABLE_1', '__tmp_sys_out_zvkmdsittt__', 'anstmp', 'cleanup-ansys-6b4e94231b59-547.sh', 'file.DSP', 'file.emat', 'file.esav', 'file.full', 'file.ldhi', 'file.mlv', 'file.mntr', 'file.mode', 'file.rdb', 'file.rst', 'file.rstp', 'file.stat', 'file0.DSP', 'file0.emat', 'file0.err', 'file0.esav', 'file0.full', 'file0.log', 'file0.mlv', 'file0.mode', 'file0.page', 'file0.r001', 'file0.rst', 'file000.jpg', 'file001.jpg', 'file002.jpg', 'file003.jpg', 'file004.jpg', 'file1.DSP', 'file1.emat', 'file1.err', 'file1.esav', 'file1.full', 'file1.log', 'file1.mlv', 'file1.mode', 'file1.out', 'file1.page', 'file1.r001', 'file1.rst', 'vm152.vrt', 'vm153.vrt']

Extract stiffness matrix#

Extract the stiffness matrix from the FULL file in a sparse matrix format. For help on the stiff function, use the help(mm.stiff) command.

Copy AnsMath sparse matrix to SciPy CSR matrix and plot#

Copy the AnsMath sparse matrix to a SciPy CSR matrix. Then, plot the graph of the sparse matrix.

pk = k.asarray()
plt.spy(pk, color="orange", markersize=3)
plt.title("AnsMath sparse matrix")
plt.show()
AnsMath sparse matrix

Get a copy of sparse matrix as a NumPy array#

Get a copy of the k sparse matrix as a NumPy array

ky = k.asarray()
ky
<Compressed Sparse Row sparse matrix of dtype 'float64'
    with 77 stored elements and shape (27, 27)>

Extract load vector from FULL file and print norm#

Extract the load vector from the FULL file and print the norm of this vector.

b = mm.rhs(fname=fullfile, name="B")
b.norm()
0.12181823800246581

Get a copy of load vector as a NumPy array#

Get a copy of the load vector as a NumPy array.

by = b.asarray()

Factorize stiffness matrix#

Factorize the stiffness matrix using PyAnsys Math.

s = mm.factorize(k)

Solve linear system#

Solve the linear system.

x = s.solve(b)

Check accuracy of solution#

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: 3.014095088656435e-16

Get a summary of allocated objects#

Get a summary of all allocated AnsMath objects.

mm.status()
APDLMATH PARAMETER STATUS-  (      5 PARAMETERS DEFINED)

  Name                   Type            Mem. (MB)       Dims            Workspace

   K                     SMAT            0.001           [27:27]         1
   B                     VEC             0.000           27              1
   JWVTFF                VEC             0.000           27              1
   XSNVPR                VEC             0.000           27              1
   ZYIOCH                LSENGINE        --              --              1

Delete all AnsMath objects#

Delete all AnsMath objects.

mm.free()

Stop PyAnsys Math#

Stop PyAnsys Math.

mm._mapdl.exit()
/home/runner/work/pyansys-math/pyansys-math/.venv/lib/python3.10/site-packages/ansys/mapdl/core/launcher.py:818: UserWarning: The environment variable 'PYMAPDL_START_INSTANCE' is set, hence the argument 'start_instance' is overwritten.
  warnings.warn(

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

Gallery generated by Sphinx-Gallery