.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/use_numpy_arrays.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_use_numpy_arrays.py: Manipulate AnsMath vectors or dense matrices as NumPy arrays ------------------------------------------------------------ This example demonstrates how to use NumPy arrays to exchange data between PyAnsys Math and Python. .. note:: This example requires Ansys 2021 R2 or later. .. GENERATED FROM PYTHON SOURCE LINES 33-46 .. code-block:: Python # Perform required imports and start PyAnsys # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Perform required imports. import matplotlib.pyplot as plt import numpy as np import ansys.math.core.math as pymath # Start PyAnsys Math as a server. mm = pymath.AnsMath() .. GENERATED FROM PYTHON SOURCE LINES 47-50 Convert AnsMath vector into NumPy array ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Allocate an AnsMath vector with 10 doubles. .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python apdl_vec = mm.ones(10) print(apdl_vec) .. rst-class:: sphx-glr-script-out .. code-block:: none CXPWEV : 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 55-59 Create a NumPy array from this AnsMath vector. Note that these are two separate objects. Memory is duplicated. Modifying one object does not modify its clone. .. GENERATED FROM PYTHON SOURCE LINES 59-64 .. code-block:: Python 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 65-67 You can manipulate this NumPy array with all existing NumPy features. .. GENERATED FROM PYTHON SOURCE LINES 67-72 .. code-block:: Python 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 73-75 Alternatively, the AnsMath object can be operated on directly with using NumPy methods. .. GENERATED FROM PYTHON SOURCE LINES 75-79 .. code-block:: Python 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 80-84 Note that some methods have APDL corollaries, and these methods are more efficient if performed within PyAnsys Math. For example, the norm method can be performed within PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: Python 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 87-91 Copy NumPy array to an AnsMath vector ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can push back any NumPy vector or 2D array to PyAnsys Math. This creates a new AnsMath vector, which in this case is named ``"NewVec:``. .. GENERATED FROM PYTHON SOURCE LINES 91-95 .. code-block:: Python mm.set_vec(pv, "NewVec") print(mm.status()) .. rst-class:: sphx-glr-script-out .. code-block:: none APDLMATH PARAMETER STATUS- ( 2 PARAMETERS DEFINED) Name Type Mem. (MB) Dims Workspace CXPWEV VEC 0.000 10 1 NEWVEC VEC 0.000 10 1 None .. GENERATED FROM PYTHON SOURCE LINES 96-99 Create a Python handle to this vector ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Create a Python handle to this vector by specifying its name. .. GENERATED FROM PYTHON SOURCE LINES 99-103 .. code-block:: Python 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 104-109 Apply same features to dense arrays ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can apply the same features to dense APDL matrices and NumPy arrays. Allocate an AnsMath dense matrix. .. GENERATED FROM PYTHON SOURCE LINES 109-115 .. code-block:: Python apdl_mat = mm.rand(3, 3) plt.imshow(apdl_mat, cmap="YlOrBr") plt.colorbar() plt.title("AnsMath dense matrix") plt.show() .. image-sg:: /examples/images/sphx_glr_use_numpy_arrays_001.png :alt: AnsMath dense matrix :srcset: /examples/images/sphx_glr_use_numpy_arrays_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 116-117 Convert the AnsMatch dense matrix to a NumPy array. .. GENERATED FROM PYTHON SOURCE LINES 117-124 .. code-block:: Python 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 OAQNSP: [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 125-126 Use the ``matrix`` method to load the NumPy array to APDL. .. GENERATED FROM PYTHON SOURCE LINES 126-135 .. code-block:: Python np_rand = np.random.random((4, 4)) ans_mat = mm.matrix(np_rand) # Print the autogenerated name of this matrix. print(ans_mat.id) .. rst-class:: sphx-glr-script-out .. code-block:: none GQGSVS .. GENERATED FROM PYTHON SOURCE LINES 136-137 Load this matrix from APDL and verify it is identical. .. GENERATED FROM PYTHON SOURCE LINES 137-141 .. code-block:: Python 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 142-145 Stop PyAnsys Math ~~~~~~~~~~~~~~~~~ Stop PyAnsys Math. .. GENERATED FROM PYTHON SOURCE LINES 145-147 .. code-block:: Python mm._mapdl.exit() .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/ansys/mapdl/core/launcher.py:811: 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.362 seconds) .. _sphx_glr_download_examples_use_numpy_arrays.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: use_numpy_arrays.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: use_numpy_arrays.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_