.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/solve_sparse_matrix.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_solve_sparse_matrix.py: Perform sparse factorization and solve operations ------------------------------------------------- Using PyAnsys Math, you can solve linear systems of equations based on sparse or dense matrices. .. GENERATED FROM PYTHON SOURCE LINES 31-43 .. code-block:: Python # 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() .. GENERATED FROM PYTHON SOURCE LINES 44-52 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. .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python out = mm._mapdl.input(vmfiles["vm152"]) .. GENERATED FROM PYTHON SOURCE LINES 55-59 List files in current directory ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ List the files in current directory. .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python mm._mapdl.list_files() .. rst-class:: sphx-glr-script-out .. code-block:: none ['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'] .. GENERATED FROM PYTHON SOURCE LINES 62-71 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. Print dimensions of sparse matrix ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Print the dimensions of the sparse matrix. .. GENERATED FROM PYTHON SOURCE LINES 71-76 .. code-block:: Python fullfile = mm._mapdl.jobname + ".full" k = mm.stiff(fname=fullfile, name="K") k .. rst-class:: sphx-glr-script-out .. code-block:: none AnsMath sparse matrix (27, 27) .. GENERATED FROM PYTHON SOURCE LINES 77-81 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. .. GENERATED FROM PYTHON SOURCE LINES 81-87 .. code-block:: Python pk = k.asarray() plt.spy(pk, color="orange", markersize=3) plt.title("AnsMath sparse matrix") plt.show() .. image-sg:: /examples/images/sphx_glr_solve_sparse_matrix_001.png :alt: AnsMath sparse matrix :srcset: /examples/images/sphx_glr_solve_sparse_matrix_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 88-91 Get a copy of sparse matrix as a NumPy array ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Get a copy of the ``k`` sparse matrix as a NumPy array .. GENERATED FROM PYTHON SOURCE LINES 91-95 .. code-block:: Python ky = k.asarray() ky .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 96-100 Extract load vector from FULL file and print norm ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Extract the load vector from the FULL file and print the norm of this vector. .. GENERATED FROM PYTHON SOURCE LINES 100-103 .. code-block:: Python b = mm.rhs(fname=fullfile, name="B") b.norm() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.12181823800246581 .. GENERATED FROM PYTHON SOURCE LINES 104-107 Get a copy of load vector as a NumPy array ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Get a copy of the load vector as a NumPy array. .. GENERATED FROM PYTHON SOURCE LINES 107-110 .. code-block:: Python by = b.asarray() .. GENERATED FROM PYTHON SOURCE LINES 111-115 Factorize stiffness matrix ~~~~~~~~~~~~~~~~~~~~~~~~~~ Factorize the stiffness matrix using PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 115-117 .. code-block:: Python s = mm.factorize(k) .. GENERATED FROM PYTHON SOURCE LINES 118-121 Solve linear system ~~~~~~~~~~~~~~~~~~~ Solve the linear system. .. GENERATED FROM PYTHON SOURCE LINES 121-124 .. code-block:: Python x = s.solve(b) .. GENERATED FROM PYTHON SOURCE LINES 125-128 Print norm** of solution vector ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Print the norm of the solution vector. .. GENERATED FROM PYTHON SOURCE LINES 128-131 .. code-block:: Python x.norm() .. rst-class:: sphx-glr-script-out .. code-block:: none 7.774533362578086e-06 .. GENERATED FROM PYTHON SOURCE LINES 132-136 Check accuracy of solution ~~~~~~~~~~~~~~~~~~~~~~~~~~ Check the accuracy of the solution by verifying that :math:`KX - B = 0`. .. GENERATED FROM PYTHON SOURCE LINES 136-141 .. code-block:: Python kx = k.dot(x) kx -= b print("Residual error:", kx.norm() / b.norm()) .. rst-class:: sphx-glr-script-out .. code-block:: none Residual error: 3.014095088656435e-16 .. GENERATED FROM PYTHON SOURCE LINES 142-145 Get a summary of allocated objects ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Get a summary of all allocated AnsMath objects. .. GENERATED FROM PYTHON SOURCE LINES 145-148 .. code-block:: Python mm.status() .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 149-152 Delete all AnsMath objects ~~~~~~~~~~~~~~~~~~~~~~~~~~ Delete all AnsMath objects. .. GENERATED FROM PYTHON SOURCE LINES 152-155 .. code-block:: Python mm.free() .. GENERATED FROM PYTHON SOURCE LINES 156-159 Stop PyAnsys Math ~~~~~~~~~~~~~~~~~ Stop PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 159-161 .. code-block:: Python mm._mapdl.exit() .. rst-class:: sphx-glr-script-out .. code-block:: none /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( .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.636 seconds) .. _sphx_glr_download_examples_solve_sparse_matrix.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: solve_sparse_matrix.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: solve_sparse_matrix.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: solve_sparse_matrix.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_