PrincipleStrainPython

From KitwarePublic
Revision as of 23:48, 28 July 2016 by Wascott (talk | contribs)
Jump to navigationJump to search

<source lang="python">

  1. ParaView Programmable Filter script. ParaView 5.0.1.
  2. This code will read in an eigenvector, calculate an
  3. eigenvalue, and place it into three variables.
  4. Be sure to correct the input variables below. Also, note that
  5. the code uses ZX, not XZ.
  6. This code only works with any multiblock or vtk
  7. datasets (including ones with only one block - i.e.,
  8. Exodus datasets as input).
  9. Usage: Run the Programmable Filter.
  10. Cut and paste this file in the section named "Script".
  11. Leave "Output Data Set Type" as "Same as Input".
  12. Click Apply button
  13. Written by Jeff Mauldin and Alan Scott

import numpy as np

def process_composite_dataset(input0):

 # Pick up input arrays
 xxar = input0.CellData["EPSXX"]
 xyar = input0.CellData["EPSXY"]
 zxar = input0.CellData["EPSZX"]
 yyar = input0.CellData["EPSYY"]
 yzar = input0.CellData["EPSYZ"]
 zzar = input0.CellData["EPSZZ"]
 #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( 
       np.array( 
         [ [xxarsub, xyarsub, zxarsub],
           [xyarsub, yyarsub, yzarsub],
           [zxarsub, yzarsub, zzarsub] ] ),
             (2,0,1))
   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")


def process_unstructured_dataset(input0):

 # Pick up input arrays
 xxar = input0.CellData["EPSXX"]
 xyar = input0.CellData["EPSXY"]
 zxar = input0.CellData["EPSZX"]
 yyar = input0.CellData["EPSYY"]
 yzar = input0.CellData["EPSYZ"]
 zzar = input0.CellData["EPSZZ"]
 #print `xxar`
 #print len(xxar.Arrays)
 # Transpose and calculate the principle strain.
 strain = np.transpose( 
       np.array( 
         [ [xxar, xyar, zxar],
           [xyar, yyar, yzar],
           [zxar, yzar, zzar] ] ),
             (2,0,1))
 principal_strain = np.linalg.eigvalsh(strain)


 #ps0 = principal_strain[:,0]
 #print "ps0 len: " + str(len(ps0))
 # Finally, move the temp arrays to output arrays
 output.CellData.append(principal_strain[:,0],
    "principal_strain_0")
 output.CellData.append(principal_strain[:,1],
    "principal_strain_1")
 output.CellData.append(principal_strain[:,2],
    "principal_strain_2")


input0 = inputs[0]

if input0.IsA("vtkCompositeDataSet"):

 process_composite_dataset(input0)

elif input0.IsA("vtkUnstructuredGrid"):

 process_unstructured_dataset(input0)

else:

 print "Bad dataset type for this script"
  1. print "\n"

</source>