.. 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/scipy_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_scipy_sparse_matrix.py>` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_gallery_examples_01-apdlmath-examples_scipy_sparse_matrix.py: APDLMath Sparse Matrices and SciPy Sparse Matrices ------------------------------------------------------------------- This tutorial will show how to get APDLMath sparse matrices from FULL files to SciPy Sparse Matrices. .. GENERATED FROM PYTHON SOURCE LINES 10-19 .. code-block:: default import matplotlib.pylab as plt from ansys.mapdl.core import launch_mapdl from ansys.mapdl.core.examples import vmfiles mapdl = launch_mapdl() mm = mapdl.math .. GENERATED FROM PYTHON SOURCE LINES 20-22 Load and solve verification manual example 153. Then load the stiffness matrix into APDLmath. .. GENERATED FROM PYTHON SOURCE LINES 22-26 .. code-block:: default out = mapdl.input(vmfiles["vm153"]) 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 27-29 Copy this APDLMath Sparse Matrix to a SciPy CSR matrix and plot the graph of the sparse matrix .. GENERATED FROM PYTHON SOURCE LINES 29-33 .. code-block:: default pk = k.asarray() plt.spy(pk) .. image-sg:: /examples/gallery_examples/01-apdlmath-examples/images/sphx_glr_scipy_sparse_matrix_001.png :alt: scipy sparse matrix :srcset: /examples/gallery_examples/01-apdlmath-examples/images/sphx_glr_scipy_sparse_matrix_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none <matplotlib.lines.Line2D object at 0x7f785d18d8e0> .. GENERATED FROM PYTHON SOURCE LINES 34-41 You can access the 3 vectors that describe this sparse matrix with. - ``pk.data`` - ``pk.indices`` - ``pk.indptr`` See the ``scipy`` documentation of the csr matrix at `scipy.sparse.csr_matrix <https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html>`_ for additional details. .. GENERATED FROM PYTHON SOURCE LINES 41-47 .. code-block:: default print(pk.data[:10]) print(pk.indices[:10]) print(pk.indptr[:10]) .. rst-class:: sphx-glr-script-out .. code-block:: none [ 0.57249304 0.56369167 -0.28624652 -0.28184583 -0.24789676 -0.24408565 -0.14312326 -0.14092292 0.77576289 -0.37033122] [ 0 1 4 7 10 13 73 76 1 4] [ 0 8 19 31 42 49 55 60 63 71] .. GENERATED FROM PYTHON SOURCE LINES 48-53 ### Create a APDLMath Sparse Matrix from a SciPy Sparse CSR Matrix Here, we transfer the ``scipy`` CSR matrix back to MAPDL. While this example uses a matrix that was originally within MAPDL, you can load any CSR matrix to MAPDL. .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: default my_mat = mm.matrix(pk, "my_mat", triu=True) my_mat .. rst-class:: sphx-glr-script-out .. code-block:: none Sparse APDLMath Matrix (126, 126) .. GENERATED FROM PYTHON SOURCE LINES 58-60 Check initial matrix ``k`` and ``my_mat`` are exactly the sames: We compute the norm of the difference, should be zero .. GENERATED FROM PYTHON SOURCE LINES 60-65 .. code-block:: default msub = k - my_mat mm.norm(msub) .. rst-class:: sphx-glr-script-out .. code-block:: none 0.0 .. GENERATED FROM PYTHON SOURCE LINES 66-77 CSR Representation in MAPDL ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Printing the list of objects in the MAPDL space, we find: - 2 SMAT objects, corresponding to the ``k``, ``MSub`` matrices, - with encrypted names - The ``my_mat`` SMAT object. Its size is zero, because the 3 - vectors are stored separately - the 3 vectors of the CSR my_mat structure: ``MY_MAT_PTR``, ``MY_MAT_IND`` - and ``MY_MAT_DATA`` .. GENERATED FROM PYTHON SOURCE LINES 77-81 .. code-block:: default mm.status() .. rst-class:: sphx-glr-script-out .. code-block:: none APDLMATH PARAMETER STATUS- ( 6 PARAMETERS DEFINED) Name Type Mem. (MB) Dims Workspace KJQCBY SMAT 0.011 [126:126] 1 MY_MAT SMAT 0.000 [126:126] 1 SSNCEY SMAT 0.011 [126:126] 1 MY_MAT_DATA VEC 0.006 738 1 MY_MAT_IND VEC 0.001 127 1 MY_MAT_PTR VEC 0.003 738 1 .. GENERATED FROM PYTHON SOURCE LINES 82-87 MAPDL Python Matrix Correspondence ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To determine which MAPDL object corresponds to which Python object, access the id property of the Python object. .. GENERATED FROM PYTHON SOURCE LINES 87-93 .. code-block:: default print("name(k)=" + k.id) print("name(my_mat)=" + my_mat.id) print("name(msub)=" + msub.id) .. rst-class:: sphx-glr-script-out .. code-block:: none name(k)=SSNCEY name(my_mat)=my_mat name(msub)=KJQCBY .. GENERATED FROM PYTHON SOURCE LINES 94-97 Stop mapdl ~~~~~~~~~~ .. GENERATED FROM PYTHON SOURCE LINES 97-98 .. code-block:: default mapdl.exit() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.813 seconds) .. _sphx_glr_download_examples_gallery_examples_01-apdlmath-examples_scipy_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: scipy_sparse_matrix.py <scipy_sparse_matrix.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: scipy_sparse_matrix.ipynb <scipy_sparse_matrix.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_