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.

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

Convert an APDLMath Vector into an NumPy Array

First, allocate a APDLMath vector with 10 doubles

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

Out:

YZFBDU :
 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

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.

pv = apdl_vec.asarray()
print(pv)

Out:

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

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

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

Out:

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

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

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

Out:

1.0
3.1622776601683795

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

For example, the norm method can be performed within MAPDL

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

Out:

3.1622776601683795 3.1622776601683795

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'.

mm.set_vec(pv, 'NewVec')

# verify this vector exists
print(mm)

Out:

APDLMATH PARAMETER STATUS-  (      2 PARAMETERS DEFINED)

  Name                   Type            Mem. (MB)       Dims            Workspace

   NEWVEC                VEC             0.000           10              1
   YZFBDU                VEC             0.000           10              1

Create a Python handle to this vector by specifying its name

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

Out:

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

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

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

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

Out:

HNBXHH:
 [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]]

You can load numpy array to APDL with the matrix method

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)

Out:

BNAROH

Load this matrix from APDL and verify it is identical

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

Out:

True

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

Gallery generated by Sphinx-Gallery