.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/gallery_examples/01-apdlmath-examples/solve_sparse_matrix.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_gallery_examples_01-apdlmath-examples_solve_sparse_matrix.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_gallery_examples_01-apdlmath-examples_solve_sparse_matrix.py:


Performing Sparse Factorization and Solve Operations
----------------------------------------------------

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

.. GENERATED FROM PYTHON SOURCE LINES 9-17

.. code-block:: default

    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









.. GENERATED FROM PYTHON SOURCE LINES 18-26

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.


.. GENERATED FROM PYTHON SOURCE LINES 26-28

.. code-block:: default

    out = mapdl.input(vmfiles["vm153"])








.. GENERATED FROM PYTHON SOURCE LINES 29-31

List the files in current directory


.. GENERATED FROM PYTHON SOURCE LINES 31-33

.. code-block:: default

    mapdl.list_files()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    ['11_blades_mode_1_ND_0.csv', 'BeforeMapping.db', 'ExampleMapping.db', 'HexBeam.cdb', 'Mapping.db', '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_xztubsvxaf__', '_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.rsx', 'file.rth', 'file.stat', 'file000.jpg', 'file000.png', 'file001.png', 'file002.png', 'file003.png', 'file004.png', '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', 'vm153.vrt', 'vm5.vrt']



.. GENERATED FROM PYTHON SOURCE LINES 34-41

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


.. GENERATED FROM PYTHON SOURCE LINES 41-44

.. code-block:: default

    k = mm.stiff(fname="PRSMEMB.full")
    k





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Sparse APDLMath Matrix (126, 126)



.. GENERATED FROM PYTHON SOURCE LINES 45-47

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


.. GENERATED FROM PYTHON SOURCE LINES 47-50

.. code-block:: default

    ky = k.asarray()
    ky





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <126x126 sparse matrix of type '<class 'numpy.float64'>'
    	with 738 stored elements in Compressed Sparse Row format>



.. GENERATED FROM PYTHON SOURCE LINES 51-55

Extract the load vector from the ``FULL`` file.

Printout the norm of this vector.


.. GENERATED FROM PYTHON SOURCE LINES 55-58

.. code-block:: default

    b = mm.rhs(fname="PRSMEMB.full")
    b.norm()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    3.472960080375275e-05



.. GENERATED FROM PYTHON SOURCE LINES 59-61

Get a copy of the load vector as a numpy array


.. GENERATED FROM PYTHON SOURCE LINES 61-63

.. code-block:: default

    by = b.asarray()








.. GENERATED FROM PYTHON SOURCE LINES 64-66

Factorize the Stifness Matrix using the MAPDL DSPARSE solver


.. GENERATED FROM PYTHON SOURCE LINES 66-68

.. code-block:: default

    s = mm.factorize(k)








.. GENERATED FROM PYTHON SOURCE LINES 69-71

Solve the linear system


.. GENERATED FROM PYTHON SOURCE LINES 71-73

.. code-block:: default

    x = s.solve(b)








.. GENERATED FROM PYTHON SOURCE LINES 74-76

Print the **norm** of the solution vector


.. GENERATED FROM PYTHON SOURCE LINES 76-78

.. code-block:: default

    x.norm()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    5.815928297030328e-08



.. GENERATED FROM PYTHON SOURCE LINES 79-83

We check the accuracy of the solution, by verifying that

:math:`KX - B = 0`


.. GENERATED FROM PYTHON SOURCE LINES 83-87

.. code-block:: default

    kx = k.dot(x)
    kx -= b
    print("Residual error:", kx.norm() / b.norm())





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Residual error: 1.7052976316939208e-15




.. GENERATED FROM PYTHON SOURCE LINES 88-90

Summary of all allocated APDLMath Objects


.. GENERATED FROM PYTHON SOURCE LINES 90-92

.. code-block:: default

    mm.status()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    APDLMATH PARAMETER STATUS-  (      5 PARAMETERS DEFINED)

      Name                   Type            Mem. (MB)       Dims            Workspace

       NBZSPJ                SMAT            0.011           [126:126]               1
       RMTGCR                VEC             0.001           126             1
       TTFCYO                VEC             0.001           126             1
       TZJMNX                VEC             0.001           126             1
       RIATWL                LSENGINE        --              --              1




.. GENERATED FROM PYTHON SOURCE LINES 93-95

Delete all APDLMath Objects


.. GENERATED FROM PYTHON SOURCE LINES 95-98

.. code-block:: default

    mm.free()









.. GENERATED FROM PYTHON SOURCE LINES 99-102

Stop mapdl
~~~~~~~~~~


.. GENERATED FROM PYTHON SOURCE LINES 102-103

.. code-block:: default

    mapdl.exit()








.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_examples_gallery_examples_01-apdlmath-examples_solve_sparse_matrix.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example




    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: solve_sparse_matrix.py <solve_sparse_matrix.py>`

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: solve_sparse_matrix.ipynb <solve_sparse_matrix.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_