.. 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/use_numpy_arrays.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_use_numpy_arrays.py>`
        to download the full example code

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

.. _sphx_glr_examples_gallery_examples_01-apdlmath-examples_use_numpy_arrays.py:


Manipulate APDLMath vectors or dense matrices as NumPy Arrays
-------------------------------------------------------------

This example demonstrates how to exchange data between MAPDL and
Python via NumPy arrays.

.. note::
    This example requires Ansys 2021R2.

.. GENERATED FROM PYTHON SOURCE LINES 12-22

.. code-block:: default

    import numpy as np

    from ansys.mapdl.core import launch_mapdl

    # Start MAPDL as a service and disable all but error messages.
    # Create an APDLMath object.
    mapdl = launch_mapdl()
    mm = mapdl.math









.. GENERATED FROM PYTHON SOURCE LINES 23-26

Convert an APDLMath Vector into an NumPy Array
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
First, allocate a APDLMath vector with 10 doubles

.. GENERATED FROM PYTHON SOURCE LINES 26-30

.. code-block:: default


    apdl_vec = mm.ones(10)
    print(apdl_vec)





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

 .. code-block:: none

    CSTCXC :
     Size : 10
      1.000e+00   1.000e+00   1.000e+00   1.000e+00   1.000e+00      <       5
      1.000e+00   1.000e+00   1.000e+00   1.000e+00   1.000e+00      <       10




.. GENERATED FROM PYTHON SOURCE LINES 31-35

Then create an numpy array from this APDLMath vector.

Note that these are two separate objects: memory is
duplicated. Modifying one object does not modify its clone.

.. GENERATED FROM PYTHON SOURCE LINES 35-39

.. code-block:: default

    pv = apdl_vec.asarray()
    print(pv)






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

 .. code-block:: none

    [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]




.. GENERATED FROM PYTHON SOURCE LINES 40-42

You can then manipulate this numpy array with all existing numpy
features

.. GENERATED FROM PYTHON SOURCE LINES 42-46

.. code-block:: default

    pv = (pv + 1) ** 2
    print(pv)






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

 .. code-block:: none

    [4. 4. 4. 4. 4. 4. 4. 4. 4. 4.]




.. GENERATED FROM PYTHON SOURCE LINES 47-49

Alternatively, the APDLMath object can be operated on directly with
numpy with the numpy methods.

.. GENERATED FROM PYTHON SOURCE LINES 49-52

.. code-block:: default

    print(np.max(apdl_vec))
    print(np.linalg.norm(apdl_vec))





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

 .. code-block:: none

    1.0
    3.1622776601683795




.. GENERATED FROM PYTHON SOURCE LINES 53-57

Note that some methods have APDL corollaries, and these methods are
more efficient if performed within MAPDL.

For example, the norm method can be performed within MAPDL

.. GENERATED FROM PYTHON SOURCE LINES 57-59

.. code-block:: default

    print(apdl_vec.norm(), np.linalg.norm(apdl_vec))





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

 .. code-block:: none

    3.1622776601683795 3.1622776601683795




.. GENERATED FROM PYTHON SOURCE LINES 60-64

Copy a NumPy Array to an APDLMath vector
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can push back any numpy vector or 2D array to MAPDL.  This
creates a new APDLMath, which in this case is named ``'NewVec'``.

.. GENERATED FROM PYTHON SOURCE LINES 64-70

.. code-block:: default

    mm.set_vec(pv, "NewVec")

    # verify this vector exists
    print(mm)






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

 .. code-block:: none

    APDLMATH PARAMETER STATUS-  (      2 PARAMETERS DEFINED)

      Name                   Type            Mem. (MB)       Dims            Workspace

       CSTCXC                VEC             0.000           10              1
       NEWVEC                VEC             0.000           10              1




.. GENERATED FROM PYTHON SOURCE LINES 71-72

Create a Python handle to this vector by specifying its name

.. GENERATED FROM PYTHON SOURCE LINES 72-76

.. code-block:: default

    v2 = mm.vec(name="NewVec")
    print(v2)






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

 .. code-block:: none

    NEWVEC :
     Size : 10
      4.000e+00   4.000e+00   4.000e+00   4.000e+00   4.000e+00      <       5
      4.000e+00   4.000e+00   4.000e+00   4.000e+00   4.000e+00      <       10




.. GENERATED FROM PYTHON SOURCE LINES 77-83

Dense Numpy Arrays
~~~~~~~~~~~~~~~~~~
The same features apply to dense APDL matrices and numpy arrays.

Allow allocate an APDLMath Dense Matrix and convert it to a numpy
array

.. GENERATED FROM PYTHON SOURCE LINES 83-91

.. code-block:: default

    apdl_mat = mm.rand(3, 3)
    np_arr = apdl_mat.asarray()

    assert np.allclose(apdl_mat, np_arr)
    print(apdl_mat)
    print(np_arr)






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

 .. code-block:: none

    MKELJV: 
     [1,1]: 4.170e-01 [1,2]: 9.326e-01 [1,3]: 3.023e-01 
     [2,1]: 9.972e-01 [2,2]: 1.144e-04 [2,3]: 9.990e-01 
     [3,1]: 7.203e-01 [3,2]: 1.281e-01 [3,3]: 1.468e-01
    [[4.17021999e-01 9.32557361e-01 3.02332568e-01]
     [9.97184808e-01 1.14381197e-04 9.99040516e-01]
     [7.20324489e-01 1.28124448e-01 1.46755893e-01]]




.. GENERATED FROM PYTHON SOURCE LINES 92-93

You can load numpy array to APDL with the matrix method

.. GENERATED FROM PYTHON SOURCE LINES 93-100

.. code-block:: default

    np_rand = np.random.random((4, 4))
    ans_mat = mm.matrix(np_rand)

    # print the autogenerated name of the this matrix
    print(ans_mat.id)






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

 .. code-block:: none

    RAYPGE




.. GENERATED FROM PYTHON SOURCE LINES 101-102

Load this matrix from APDL and verify it is identical

.. GENERATED FROM PYTHON SOURCE LINES 102-106

.. code-block:: default

    from_ans = ans_mat.asarray()
    print(np.allclose(from_ans, np_rand))






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

 .. code-block:: none

    True




.. GENERATED FROM PYTHON SOURCE LINES 107-110

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


.. GENERATED FROM PYTHON SOURCE LINES 110-111

.. code-block:: default

    mapdl.exit()








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

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


.. _sphx_glr_download_examples_gallery_examples_01-apdlmath-examples_use_numpy_arrays.py:

.. only:: html

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




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

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

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

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


.. only:: html

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

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