PyMAPDL APDLMath Basic Operations

This tutorial shows how you can use pymapdl to use APDL math for basic operations on APDLMath vectors and matrices in the APDL memory workspace.

The ansys.mapdl.math submodule gives access to APDLMath features inside PyMAPDL.

import numpy as np

from ansys.mapdl.core import launch_mapdl

# Start MAPDL as a service and create an APDLMath object.
mapdl = launch_mapdl()
mm = mapdl.math

Create and Manipulate Vectors

Create 2 APDLMath vectors of size 5. \(\vec{v}\) is initialized with ones, $vec{w}$ is filled with random values

Corresponding APDLMath commands - *VEC,V,D,ALLOC,5 - *INIT,V,CONST,1 - *VEC,W,D,ALLOC,5 - *INIT,W,RAND

v = mm.ones(5)
w = mm.rand(5)
print(w)

Out:

IVBBDY :
 Size : 5
  4.170e-01   9.972e-01   7.203e-01   9.326e-01   1.144e-04      <       5

Use operators on vectors

Just like numpy PyMAPDL APDLMath vectors can be have most of the standard operators (e.g. +, -, +=, -=, *=)

Here we form \(\vec{z}=\vec{v}+\vec{w}\)

Then we compute \(\|z\|_2\) (the default norm is nrm2, but you can use .norm(‘nrm1’) or .norm(‘nrminf’) for different normals. See help(z.norm) for additional details.

APDLMath Commands: - *VEC,Z,D,COPY,V - *AXPY,1,,W,1,,Z - *NRM,Z,,nrmval

z = v + w
z.norm()

Out:

3.7001650749377593

Methods

Alternatively you can use methods, following the numpy standards. Available methods are:

  • mm.add()

  • mm.subtract()

  • mm.dot()

Equivalent operator: z = v + w

Equivalent APDLMath Commands: - *VEC,Z,D,COPY,V - *AXPY,1,,W,1,,Z

z = mm.add(v, w)
z.norm()

Out:

3.7001650749377593

Subtraction

Equivalent operator: z = v - w

Equivalent APDLMath Commands: - *VEC,Z,D,COPY,V - *AXPY,-1,,W,1,,Z

z = mm.subtract(v, w)
print(z)

Out:

NUVJWM :
 Size : 5
  5.830e-01   2.815e-03   2.797e-01   6.744e-02   9.999e-01      <       5

Dot product of 2 vectors

Equivalent APDLMath Command: *DOT,V,W,dotval

vw = mm.dot(v, w)
print("Dot product :", str(vw))

Out:

Dot product : 3.0672030387213454

Perform an in-place operations (without copying vectors)

In-Place Addition

MAPDL Commands: - *AXPY,1,,V,1,,Z - *PRINT,Z

v += v
print(v)

Out:

HUVRND :
 Size : 5
  2.000e+00   2.000e+00   2.000e+00   2.000e+00   2.000e+00      <       5

In-Place Multiplication

MAPDL Command: *SCAL,v,2

v *= 2
print(v)

Out:

HUVRND :
 Size : 5
  4.000e+00   4.000e+00   4.000e+00   4.000e+00   4.000e+00      <       5

In-Place Multiplication

v /= 2.
print(v)

Out:

HUVRND :
 Size : 5
  2.000e+00   2.000e+00   2.000e+00   2.000e+00   2.000e+00      <       5

Working with Dense Matrices

Allocate two dense matrices with random values.

MAPDL Commands:

  • *DMAT,m1,D,ALLOC,4,5

  • *INIT,m1,RAND

  • *DMAT,m1,D,ALLOC,4,5

  • *INIT,m1,CONST,1

m1 = mm.rand(4, 5)
m2 = mm.ones(4, 5)
m1, m2

Out:

(Dense APDLMath Matrix (4, 5), Dense APDLMath Matrix (4, 5))

Add these 2 dense matrices, and scale the result matrix.

Mapdl Commands - *DMAT,m3,D,COPY,m1 - *AXPY,1,,m2,1,,m3

m3 = m1 + m2
print(m3)

m3 *= 2
print(m3)

Out:

XGFECP:
 [1,1]: 1.417e+00 [1,2]: 1.000e+00 [1,3]: 1.147e+00 [1,4]: 1.186e+00 [1,5]: 1.397e+00
 [2,1]: 1.997e+00 [2,2]: 1.128e+00 [2,3]: 1.236e+00 [2,4]: 1.388e+00 [2,5]: 1.936e+00
 [3,1]: 1.720e+00 [3,2]: 1.302e+00 [3,3]: 1.092e+00 [3,4]: 1.346e+00 [3,5]: 1.539e+00
 [4,1]: 1.933e+00 [4,2]: 1.999e+00 [4,3]: 1.397e+00 [4,4]: 1.670e+00 [4,5]: 1.846e+00
XGFECP:
 [1,1]: 2.834e+00 [1,2]: 2.000e+00 [1,3]: 2.294e+00 [1,4]: 2.373e+00 [1,5]: 2.794e+00
 [2,1]: 3.994e+00 [2,2]: 2.256e+00 [2,3]: 2.472e+00 [2,4]: 2.776e+00 [2,5]: 3.871e+00
 [3,1]: 3.441e+00 [3,2]: 2.605e+00 [3,3]: 2.185e+00 [3,4]: 2.691e+00 [3,5]: 3.078e+00
 [4,1]: 3.865e+00 [4,2]: 3.998e+00 [4,3]: 2.793e+00 [4,4]: 3.339e+00 [4,5]: 3.693e+00

*Transpose* a Matrix

m4 = m3.T
print(m4)

Out:

POZTXK:
 [1,1]: 2.834e+00 [1,2]: 3.994e+00 [1,3]: 3.441e+00 [1,4]: 3.865e+00
 [2,1]: 2.000e+00 [2,2]: 2.256e+00 [2,3]: 2.605e+00 [2,4]: 3.998e+00
 [3,1]: 2.294e+00 [3,2]: 2.472e+00 [3,3]: 2.185e+00 [3,4]: 2.793e+00
 [4,1]: 2.373e+00 [4,2]: 2.776e+00 [4,3]: 2.691e+00 [4,4]: 3.339e+00
 [5,1]: 2.794e+00 [5,2]: 3.871e+00 [5,3]: 3.078e+00 [5,4]: 3.693e+00

As for vectors, methods are also available as an alternative to operators.

m3 = mm.add(m1, m2)
print(m3)

Out:

WHHBXM:
 [1,1]: 1.417e+00 [1,2]: 1.000e+00 [1,3]: 1.147e+00 [1,4]: 1.186e+00 [1,5]: 1.397e+00
 [2,1]: 1.997e+00 [2,2]: 1.128e+00 [2,3]: 1.236e+00 [2,4]: 1.388e+00 [2,5]: 1.936e+00
 [3,1]: 1.720e+00 [3,2]: 1.302e+00 [3,3]: 1.092e+00 [3,4]: 1.346e+00 [3,5]: 1.539e+00
 [4,1]: 1.933e+00 [4,2]: 1.999e+00 [4,3]: 1.397e+00 [4,4]: 1.670e+00 [4,5]: 1.846e+00

Compute a matrix vector multiplication

mw = m3.dot(m4)
print(mw)

Out:

ULHHPN:
 [1,1]: 1.536e+01 [1,2]: 1.945e+01 [1,3]: 1.748e+01 [1,4]: 2.180e+01
 [2,1]: 1.945e+01 [2,2]: 2.492e+01 [2,3]: 2.220e+01 [2,4]: 2.746e+01
 [3,1]: 1.748e+01 [3,2]: 2.220e+01 [3,3]: 2.005e+01 [3,4]: 2.508e+01
 [4,1]: 2.180e+01 [4,2]: 2.746e+01 [4,3]: 2.508e+01 [4,4]: 3.176e+01

APDLMath matrices can be identified by printing, viewing their types, or with using the __repr__ method by simply typing out the variable

APDLMath Matrix

type(m1)
print(m1)
m1

Out:

SGBGSV:
 [1,1]: 4.170e-01 [1,2]: 1.144e-04 [1,3]: 1.468e-01 [1,4]: 1.863e-01 [1,5]: 3.968e-01
 [2,1]: 9.972e-01 [2,2]: 1.281e-01 [2,3]: 2.361e-01 [2,4]: 3.879e-01 [2,5]: 9.355e-01
 [3,1]: 7.203e-01 [3,2]: 3.023e-01 [3,3]: 9.234e-02 [3,4]: 3.456e-01 [3,5]: 5.388e-01
 [4,1]: 9.326e-01 [4,2]: 9.990e-01 [4,3]: 3.966e-01 [4,4]: 6.697e-01 [4,5]: 8.463e-01

Dense APDLMath Matrix (4, 5)

APDLMath Vector

type(w)
print(w)
w

Out:

IVBBDY :
 Size : 5
  4.170e-01   9.972e-01   7.203e-01   9.326e-01   1.144e-04      <       5

APDLMath Vector Size 5

Numpy methods on APDLMath objects

Regardless of the underlying APDLMath object type, you are generally able to perform most numpy or scipy operations on these arrays. You can do this one of two ways. First, you can convert a matrix to a numpy array:

apdl_mat = mm.rand(5, 5)
np_mat = apdl_mat.asarray()
print(np_mat)

Out:

[[4.17021999e-01 1.28124448e-01 9.23385957e-02 6.69746040e-01
  4.19194519e-01]
 [9.97184808e-01 3.02332568e-01 3.96580726e-01 3.96767469e-01
  3.13273513e-01]
 [7.20324489e-01 9.99040516e-01 1.86260211e-01 9.35539073e-01
  6.85219501e-01]
 [9.32557361e-01 1.46755893e-01 3.87910740e-01 5.38816732e-01
  5.24548163e-01]
 [1.14381197e-04 2.36088976e-01 3.45560725e-01 8.46310918e-01
  2.04452249e-01]]

Alternatively, you can simply use numpy to compute the max of the array

This works because PyMAPDL copies over the matrix to the local python memory and then computes the max using numpy.

print(np.max(apdl_mat))

Out:

0.9990405155112967

This works for most numpy operations, but keep in mind that operations that are supported within MAPDL (such as adding or multiplying arrays) will compute much faster as the data is not copied.

apdl_arr = mm.rand(5, 5)
np_array = apdl_mat.asarray()
print(np.allclose(apdl_mat, np_array))

Out:

True

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

Gallery generated by Sphinx-Gallery