From a09c6376ee6d7d0871193a8bef776e0b79e9b996 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Thu, 4 Jun 2020 10:17:30 +0200
Subject: [PATCH] Added ParaView plugin for assigning material parameters.

Originally written by Alireza Hassanzadegan.
---
 pre/paraview/AssignMaterialParameter.py       | 89 +++++++++++++++++++
 .../AssignMaterialParameterExample.csv        | 17 ++++
 2 files changed, 106 insertions(+)
 create mode 100644 pre/paraview/AssignMaterialParameter.py
 create mode 100644 pre/paraview/AssignMaterialParameterExample.csv

diff --git a/pre/paraview/AssignMaterialParameter.py b/pre/paraview/AssignMaterialParameter.py
new file mode 100644
index 0000000..f2c27e2
--- /dev/null
+++ b/pre/paraview/AssignMaterialParameter.py
@@ -0,0 +1,89 @@
+# Copyright (c) Alireza Hassanzadegan <alireza.hassanzadegan@bge.de>
+# This program is free ; you can redistribute it and/or modify it
+#
+# Refactored as ParaView VTKPythonAlgorithm by Lars Bilke <lars.bilke@ufz.de>.
+
+# Assigning material parameters
+#
+# To use this filter:
+#
+# 1. Convert the model to VTK format (e.g. using meshio) and load your Mesh.vtu
+#    in Paraview.
+# 2. Load this filter as a Plugin (Tools / Manage Plugins / Load New)
+# 3. Apply this filter (AssignMaterialParameter) to the mesh
+# 4. In the filter properties set the property "Data File" to your input csv
+#    file. Pay attention to file paths, input file format(CSV) and separators
+#    (delimiter must be;)
+# 4. Material parameters are assigned based on the materialIDs (MID in Table)
+#    as a keyword.
+
+import os
+from paraview.util.vtkAlgorithm import *
+
+@smproxy.filter()
+@smproperty.input(name="Input", port_index=0)
+@smdomain.datatype(dataTypes=["vtkUnstructuredGrid"], composite_data_supported=False)
+class AssignMaterialParameter(VTKPythonAlgorithmBase):
+  def __init__(self):
+      VTKPythonAlgorithmBase.__init__(self, nInputPorts=1, nOutputPorts=1, outputType="vtkUnstructuredGrid")
+      self._csv_file = ""
+
+  def FillInputPortInformation(self, port, info):
+    if port == 0:
+      info.Set(self.INPUT_REQUIRED_DATA_TYPE(), "vtkUnstructuredGrid")
+    return 1
+
+  def RequestData(self, request, inInfoVec, outInfoVec):
+    from paraview import vtk
+    import time
+    startTime = time.time()
+    pdi = vtk.vtkUnstructuredGrid.GetData(inInfoVec[0], 0)
+    pdo = vtk.vtkUnstructuredGrid.GetData(outInfoVec, 0)
+    pdo.ShallowCopy(pdi)
+
+    # Assign MaterialIDs
+    numcells = pdi.GetNumberOfCells()
+    celldata = pdi.GetCellData()
+    cellmid = celldata.GetArray("MaterialIDs")
+    matIdsRange = cellmid.GetRange()
+    numlines = int(matIdsRange[1])+1
+
+    # Load data
+    firstline =True
+    FileName=os.path.normcase(self._csv_file)
+    f = open(FileName)
+    # Read header
+    if firstline:
+      firstline = False
+      header=f.readline().split(";")
+
+    # How many materialparameter
+    numparam=int(len(header))
+
+    # Restore data as List in List
+    datalist = []
+    for line in f:
+      data = line.split(";")
+      datalist.append(data)
+
+    # Assign properties to CellData
+    for j in range(1, numparam): # skip first column (MID)
+      dataArray = vtk.vtkDoubleArray()
+      dataArray.SetNumberOfComponents(1)
+      dataArray.SetName(header[j])
+      for i in range(numlines):
+        for k in range(numcells):
+          t = int(cellmid.GetTuple1(k))
+          dataArray.InsertNextValue(float(datalist[t][j]))
+      pdo.GetCellData().AddArray(dataArray)
+
+    print('Material parameters were assigned. It took %8.2f seconds.' %(time.time() - startTime))
+    return 1
+
+  @smproperty.stringvector(name="Data File", number_of_elements="1")
+  def SetCSV(self, value):
+    if os.path.isfile(value) and os.access(value, os.R_OK):
+      self._csv_file = value
+      self.Modified()
+    else:
+      print("File does not exist: ", value)
diff --git a/pre/paraview/AssignMaterialParameterExample.csv b/pre/paraview/AssignMaterialParameterExample.csv
new file mode 100644
index 0000000..4d75532
--- /dev/null
+++ b/pre/paraview/AssignMaterialParameterExample.csv
@@ -0,0 +1,17 @@
+MID;Rho;YoungModulus;PoissonRatio;Cohesion;FrictionAngle;DilatancyAngle;TransitionAngle;TensionCutOffParameter
+0;8750;1.17E+11;0.3;1.00E+10;0;0;0;0
+1;8751;1.17E+12;0.3;1.00E+10;0;0;0;0
+2;8752;1.17E+13;0.3;1.00E+10;0;0;0;0
+3;2070;5.20E+09;0.27;1.00E+10;0;0;0;0
+4;2700;5.00E+10;0.25;1.00E+10;0;0;0;0
+5;2700;5.00E+10;0.25;1.00E+10;0;0;0;0
+6;7800;2.10e11;0.3;1.00E+10;0;0;0;0
+7;7800;2.10e11;0.3;1.00E+10;0;0;0;0
+8;2173;2.65E+08;0.18;1.00E+10;0;0;0;0
+9;1800;1.38e7;0.29;1.00E+10;0;0;0;0
+10;1800;1.38e7;0.29;1.00E+10;0;0;0;0
+11;1800;1.38e7;0.29;1.00E+10;0;0;0;0
+12;1800;1.38e7;0.29;1.00E+10;0;0;0;0
+13;1800;1.38e7;0.29;1.00E+10;0;0;0;0
+14;1800;1.38e7;0.29;1.00E+10;0;0;0;0
+15;1800;1.38e7;0.29;1.00E+10;0;0;0;0
\ No newline at end of file
-- 
GitLab