diff --git a/post/merge-scalar-data-arrays/README.md b/post/merge-scalar-data-arrays/README.md index 8e713203a11a28ca837d203d1c8f2ab3d22cca64..caafecc4d3294736f86e51211ac18ee80c1cbf5d 100644 --- a/post/merge-scalar-data-arrays/README.md +++ b/post/merge-scalar-data-arrays/README.md @@ -6,3 +6,17 @@ two-component vector field. The script 'mergeDataArraysIntoOne' takes arbitrary number of scalar fields from a vtu file and uses them as the components of a vector field that will be created. + +# Merge prefixed scalar fields (mechanics) + +All fields starting with a given prefix and ending with `_xx`, `_yy`, `_zz`, +`_xy`, `_yz`, and `_xz` will be merged into a single vectorial array with four +or six components depending on the given dimension. +This is useful to combine arrays like `sigma_xx`, _etc._ to single `sigma` +array. + +Example usage: +```sh +mergePrefixedArrays.py 2 sigma input.vtu output.vtu +mergePrefixedArrays.py 3 epsilon input.vtu output.vtu +``` diff --git a/post/merge-scalar-data-arrays/mergePrefixedArrays.py b/post/merge-scalar-data-arrays/mergePrefixedArrays.py new file mode 100755 index 0000000000000000000000000000000000000000..67ab531108a61744d88caa6f1909fd98ec28f260 --- /dev/null +++ b/post/merge-scalar-data-arrays/mergePrefixedArrays.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python2 + +from vtk import * +from sys import argv, exit + +if len(argv) != 5: + print("errorneous number of args.") + exit(1) + +dimension = int(argv[1]) +if dimension != 2 and dimension != 3: + print("wrong dimension", dimension) + exit(1) + +field_name = argv[2] + +input_filename = argv[3] +output_filename = argv[4] + +r = vtkXMLUnstructuredGridReader() +r.SetFileName(input_filename) +r.Update() + +m = r.GetOutput() +pd = m.GetPointData() + +tensor = vtkDoubleArray() +tensor.SetName(field_name) +tensor.SetNumberOfComponents({2 : 4, 3 : 6}[dimension]) +tensor.SetNumberOfTuples(m.GetNumberOfPoints()) + +for i in range(m.GetNumberOfPoints()): + xx = pd.GetArray(field_name + '_xx').GetValue(i) + yy = pd.GetArray(field_name + '_yy').GetValue(i) + zz_array = pd.GetArray(field_name + '_zz') + zz = 0 if zz_array is None else zz_array.GetValue(i) + xy = pd.GetArray(field_name + '_xy').GetValue(i) + + if dimension == 2: + tensor.SetTuple4(i, xx, yy, zz, xy) + elif dimension == 3: + xz_array = pd.GetArray(field_name + '_xz') + xz = 0 if xz_array is None else xz_array.GetValue(i) + yz_array = pd.GetArray(field_name + '_yz') + yz = 0 if yz_array is None else yz_array.GetValue(i) + + tensor.SetTuple6(i, xx, yy, zz, xy, xz, yz) + +pd.RemoveArray(field_name + '_xx') +pd.RemoveArray(field_name + '_yy') +pd.RemoveArray(field_name + '_zz') +pd.RemoveArray(field_name + '_xy') +pd.RemoveArray(field_name + '_xz') +pd.RemoveArray(field_name + '_yz') +pd.AddArray(tensor) + +w = vtkXMLUnstructuredGridWriter() +w.SetFileName(output_filename) +w.SetInputData(m) +w.Update() \ No newline at end of file