Macro export transient FEM results

From FreeCAD Documentation
Revision as of 10:25, 30 August 2019 by Luftschraube (talk | contribs) (Created page with "{{Macro |Name=export_transient_FEM_results |Description=Export multiple FEM results from a transient analysis for post-processing in ParaView. |Author=Luftschraube |Version=0....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Generic macro icon. Create your personal icon with the same name of the macro export_transient_FEM_results

Description
Export multiple FEM results from a transient analysis for post-processing in ParaView.

Macro version: 0.1
Last modified: 2019-08-30
FreeCAD version: All
Author: Luftschraube
Author
Luftschraube
Download
None
Links
Macro Version
0.1
Date last modified
2019-08-30
FreeCAD Version(s)
All
Default shortcut
None
See also
None

Description

This macro exports multiple FEM result objects from a transient analysis to the VTK format and generates a PVU file which can be used to load the results directly into ParaView for post-processing.

Background

A transient analysis in FreeCAD ends up with a bunch of FEM result objects, one for each timestamp. The individual results can be investigated directly in FreeCAD and be post-processed using pipelines. However, the possibilities within FreeCAD are still limited, especially for transient analyses. A better tool for post-processing and visualisation is ParaView. You can export single FEM result objects from FreeCAD as a .vtk or .vtu file, which can then be opened with ParaView. Unfortunately, the FEM workbench doesn't support the export of multiple .vtk files at one time (yet). That's where this macro comes into play.

How to use (experienced users)

Run the macro on a FreeCAD project that includes several FEM result objects from a transient analysis. Besides the .FCStd file, a new folder 'vtk-export' will be created, containing the individual results (.vtu files), and a .pvu file that can be opened from ParaView.

How to use (step-by-step with example)

As an example, the bending of a aluminium/steel bimetal strip is used. A step-by-step guide to create the sample file is given here, or you can download the file in the download section on this page.

With the example file opened, we go to Macro > Macros..., select "ExportTransientResults_190830.FCMacro" (or whatever name you saved it as) and execute it. The macro will now create a subfolder 'vtk-export' besides the .FCStd file. Depending on the number and size of the result objects, this may take some time. In the Report View (View > Report View), we should see 'Macro finished', if eveything went fine - or some error messages. (Note: Sometimes messages like 'PropertyFloatList NOT exported to vtk' appear, but I was able to work with the VTK files anyway...) In the subfolder 'vtk-export', we will find .vtu files, one for each result set for each timestamp. Additionally, a .pvd file is created, which tells ParaView which result set belongs to which timestamp.

Now, we open ParaView and go to File > Open... and open the .pvd file. In the 'Properties' tab, we click 'Apply' to load the results. In the 'Information' tab we will see a list of Index and Value, corresponding to the timestamps of the results that we just imported. (Of course, it always makes sense to check if the times given here are correct or if something strange happened during the export.) From here, we can use ParaView to play around with the results. Since ParaView offers a lot of possibilities, please refer to the appropriate documentations around the internet.

[developing...]


Downloads

Code

#!/usr/bin/python
# -*- coding: utf-8 -*-

#
# Macro to export multiple FEM results objects from a transient analysis for
# post-processing in ParaView.
#
# Created by <Luftschraube> under the GNU Lesser General Public License (LGPL)
#
# Version 0.1 (30.08.2019)
#

import FreeCAD
import Fem
import feminout.importVTKResults
import os
from sys import exit

FreeCAD.Console.PrintMessage(">>>---------Macro started\n")

#Get the file path and name of the active document 
p = FreeCAD.ActiveDocument.FileName
FilePath = "/".join(p.split("/")[:-1])
FileName = p.split("/")[-1].split(".")[0]
FreeCAD.Console.PrintMessage("File path:" + FilePath + "\n")
FreeCAD.Console.PrintMessage("File name:" + FileName + "\n")
FreeCAD.Console.PrintMessage("\n<<<----------Macro finished with errors\n")

#Get FEM result objects into a list
FreeCAD.Console.PrintMessage("Looking for result objects...")
FEMResultObjects = []

for obj in App.ActiveDocument.Objects:
	if obj.TypeId == "Fem::FemResultObjectPython":	
		FEMResultObjects.append(obj.Label)
		#FreeCAD.Console.PrintMessage(obj.Label + '\n')

NumberOfResultObjects = len(FEMResultObjects)
FreeCAD.Console.PrintMessage(str(NumberOfResultObjects) + " found\n")

if NumberOfResultObjects > 0:
	
	#Export result objects as .vtu file and generate .pvu file
	ExportFolderName = "vtk-export"
	ExportOutputPath = FilePath + "/" + ExportFolderName
	if not os.path.exists(ExportOutputPath):
		os.makedirs(ExportOutputPath)
		FreeCAD.Console.PrintMessage(ExportOutputPath + " has been created\n")
	else:
		FreeCAD.Console.PrintMessage(ExportOutputPath + " already exists\n")
	
	PVUFile = open(ExportOutputPath + "/" + FileName + ".pvd","w")
	PVUFile.write("<VTKFile type=\"Collection\" version=\"1.0\" byte_order=\"LittleEndian\" header_type=\"UInt64\">\n\t<Collection>\n")
	
	n = 0
	for ExportObject in sorted(FEMResultObjects):
		n = n + 1
		
		#Read and format time stamp (e.g., 12.37 s = 0001237)
		FreeCADResultLabelParts = ExportObject.split("_")
		PreDecimal = FreeCADResultLabelParts[-3]
		PostDecimal = FreeCADResultLabelParts[-2]

		if len(PostDecimal) == 1:
			PostDecimal = PostDecimal + str(0)
		TimeValue = float(PreDecimal) + float(PostDecimal)/100
		TimeString = str(int(round(TimeValue*100,0))).zfill(7)
	
		#For some reason, the VTK exporter expects a list (although only one object is processed at once...) - so build a list with the current object
		ExportObjectList = []
		ExportObjectList.append(FreeCAD.ActiveDocument.getObject(ExportObject))
		FileNameOUT = FileName + "_" + TimeString + ".vtu"
		FilePath = ExportOutputPath + "/" + FileNameOUT
		FreeCAD.Console.PrintMessage(FilePath + " is to be created\n")
		feminout.importVTKResults.export(ExportObjectList,FilePath)
	
		PVUFile.write("\t\t<DataSet timestep=\"" + str(TimeValue) + "\" file=\"" + FileNameOUT + "\"/>\n")
	
	#Close the .pvu file
	PVUFile.write("\t</Collection>\n</VTKFile>")
	PVUFile.close()
else:
	FreeCAD.Console.PrintMessage("Nothing to export!")

FreeCAD.Console.PrintMessage('\n<<<----------Macro finished\n')