Eigenvalue Calculation using Python Programming Filter


I want to calculate the principal strains of a 3D data. Here’s my workflow:

  1. Use ComputeDerivatives to get the strain tensor from the displacement.
  2. Use ‘Programming Filter’ and the code is from Paraview Wiki:


import numpy as np
from vtk.numpy_interface import algorithms as algs
from paraview.vtk.numpy_interface import dataset_adapter as dsa

def process_composite_dataset(input0):
  # Pick up input arrays
  xxar = input0.CellData["Strain_0"]
  xyar = input0.CellData["Strain_1"]
  zxar = input0.CellData["Strain_6"]
  yyar = input0.CellData["Strain_4"]
  yzar = input0.CellData["Strain_7"]
  zzar = input0.CellData["Strain_8"]

  #print `xxar`
  #print len(xxar.Arrays)

  # Set output arrays to same type as input array.  
  # Do a multiply to make sure we don't just have a 
  #   pointer to the original.
  outarray0 = xxar*0.5
  outarray1 = xxar*0.5
  outarray2 = xxar*0.5

  # Run a for loop over all blocks
  numsubarrays = len(xxar.Arrays)
  for ii in range(0, numsubarrays):

    # pick up input arrays for each block.
    xxarsub = xxar.Arrays[ii]
    xyarsub = xyar.Arrays[ii]
    zxarsub = zxar.Arrays[ii]
    yyarsub = yyar.Arrays[ii]
    yzarsub = yzar.Arrays[ii]
    zzarsub = zzar.Arrays[ii]

    #print `xxarsub`

    # Transpose and calculate the principle strain.
    strain = np.transpose( 
          [ [xxarsub, xyarsub, zxarsub],
            [xyarsub, yyarsub, yzarsub],
            [zxarsub, yzarsub, zzarsub] ] ),

    principal_strain = np.linalg.eigvalsh(strain)

    # Move principle strain to temp output arrays for this block
    outarray0.Arrays[ii] = principal_strain[:,0]
    outarray1.Arrays[ii] = principal_strain[:,1]
    outarray2.Arrays[ii] = principal_strain[:,2]

  #ps0 = principal_strain[:,0]
  #print "ps0 len: " + str(len(ps0))

  # Finally, move the temp arrays to output arrays
  output.CellData.append(outarray0, "principal_strain_0")
  output.CellData.append(outarray1, "principal_strain_1")
  output.CellData.append(outarray2, "principal_strain_2")

However, when I clicked “Apply”, this Error showed up:

ValueError: axes don't match array
Traceback (most recent call last):
  File "<string>", line 22, in <module>
  File "<string>", line 123, in RequestData
  File "<string>", line 95, in process_unstructured_dataset
  File "<__array_function__ internals>", line 5, in transpose
  File "C:\Program Files\ParaView 5.10.1-MPI-Windows-Python3.9-msvc2017-AMD64\bin\Lib\site-packages\numpy\core\fromnumeric.py", line 660, in transpose
    return _wrapfunc(a, 'transpose', axes)
  File "C:\Program Files\ParaView 5.10.1-MPI-Windows-Python3.9-msvc2017-AMD64\bin\Lib\site-packages\numpy\core\fromnumeric.py", line 57, in _wrapfunc
    return bound(*args, **kwds)...

Does anyone know what’s wrong here?