### Tutorial

Topic
Finite Element Analysis
Level
Intermediates
Time to complete
30 minutes
Author
Bernd
0.17.9477 or above
Example File(s)

## Introduction

This example is meant to show how a simple Finite Element Analysis (FEA) in FreeCADs FEM Module is done by python. The model from FEM_CalculiX_Cantilever_3D will be taken for this example.

### Requirements

• FreeCAD version = 0.17.9477 or above.
• Al thought due to the continuing changes in FEM module it is recommended to use the latest development snapshot of FreeCAD for python scripted FEM analysis's.
• A set up FEM module. Check FEM_CalculiX_Cantilever_3D

## Let's start

### New document and the part to analyze

```# new document
doc = App.newDocument("Scripted_CalculiX_Cantilever3D")
#
# part
import Part
box_obj.Height = box_obj.Width = 1000
box_obj.Length = 8000
#
# see how our part looks like
# ```

### Analysis and analysis objects

```# see how our part looks like
#
# analysis
import FemAnalysis
analysis_object = FemAnalysis.makeFemAnalysis('Analysis')
#
# solver
import FemSolverCalculix
solver_object = FemSolverCalculix.makeFemSolverCalculix('CalculiX')
solver_object.GeometricalNonlinearity = 'linear'
solver_object.MatrixSolverType = 'default'
solver_object.IterationsControlParameterTimeUse = False
doc.Analysis.Member = doc.Analysis.Member + [solver_object]
#
# material
import FemMaterial
material_object = FemMaterial.makeFemMaterial('MechanicalMaterial')
mat = material_object.Material
mat['Name'] = "Steel-Generic"
mat['YoungsModulus'] = "210000 MPa"
mat['PoissonRatio'] = "0.30"
mat['Density'] = "7900 kg/m^3"
material_object.Material = mat
doc.Analysis.Member = doc.Analysis.Member + [material_object]
#
# fixed_constraint
fixed_constraint.References = [(doc.Box, "Face1")]
doc.Analysis.Member = doc.Analysis.Member + [fixed_constraint]
#
# force_constraint
force_constraint.References = [(doc.Box, "Face2")]
force_constraint.Force = 9000000.0
force_constraint.Direction = (doc.Box, ["Edge5"])
force_constraint.Reversed = True
doc.Analysis.Member = doc.Analysis.Member + [force_constraint]
# ```

### FEM mesh

```# mesh
import Fem
femmesh = Fem.FemMesh()
#
# nodes
#
# elements
femmesh.addVolume([40, 19, 10, 20, 168, 106, 196, 171, 66, 107], 149)
femmesh.addVolume([10, 31, 30, 40, 150, 79, 149, 196, 128, 129], 150)
femmesh.addVolume([38, 17, 18, 47, 163, 62, 166, 140, 197, 198], 151)
femmesh.addVolume([32, 41, 11, 12, 130, 199, 152, 165, 200, 56], 152)
femmesh.addVolume([12, 32, 41, 42, 165, 130, 200, 201, 143, 92], 153)
femmesh.addVolume([42, 21, 12, 22, 172, 110, 201, 185, 68, 123], 154)
femmesh.addVolume([10, 31, 40, 11, 150, 128, 196, 55, 151, 202], 155)
femmesh.addVolume([11, 12, 41, 21, 56, 200, 199, 109, 110, 173], 156)
femmesh.addVolume([20, 41, 11, 40, 170, 199, 108, 171, 91, 202], 157)
femmesh.addVolume([20, 41, 21, 11, 170, 173, 67, 108, 199, 109], 158)
femmesh.addVolume([32, 11, 41, 31, 152, 199, 130, 80, 151, 131], 159)
femmesh.addVolume([11, 10, 20, 40, 55, 107, 108, 202, 196, 171], 160)
femmesh.addVolume([38, 17, 47, 37, 163, 197, 140, 86, 162, 141], 161)
femmesh.addVolume([38, 18, 49, 48, 166, 204, 203, 145, 205, 206], 162)
femmesh.addVolume([46, 15, 45, 36, 207, 208, 96, 139, 159, 137], 163)
femmesh.addVolume([47, 16, 46, 37, 209, 210, 97, 141, 161, 138], 164)
femmesh.addVolume([18, 4, 2, 49, 122, 64, 63, 204, 189, 190], 165)
femmesh.addVolume([18, 17, 27, 47, 62, 120, 121, 198, 197, 183], 166)
femmesh.addVolume([38, 18, 48, 47, 166, 205, 145, 140, 198, 98], 167)
femmesh.addVolume([12, 23, 42, 22, 111, 175, 201, 123, 69, 185], 168)
femmesh.addVolume([26, 47, 27, 17, 180, 183, 73, 119, 197, 120], 169)
femmesh.addVolume([50, 29, 9, 6, 211, 147, 212, 195, 146, 53], 170)
femmesh.addVolume([27, 48, 18, 47, 182, 205, 121, 183, 98, 198], 171)
femmesh.addVolume([8, 7, 39, 50, 103, 89, 167, 192, 193, 213], 172)
femmesh.addVolume([40, 9, 39, 30, 214, 215, 90, 129, 148, 126], 173)
femmesh.addVolume([42, 21, 41, 12, 172, 173, 92, 201, 110, 200], 174)
femmesh.addVolume([50, 9, 29, 39, 212, 147, 211, 213, 215, 127], 175)
femmesh.addVolume([29, 7, 50, 39, 125, 193, 211, 127, 89, 213], 176)
femmesh.addVolume([31, 11, 41, 40, 151, 199, 131, 128, 202, 91], 177)
femmesh.addVolume([47, 16, 37, 17, 209, 161, 141, 197, 61, 162], 178)
femmesh.addVolume([40, 9, 30, 10, 214, 148, 129, 196, 54, 149], 179)
femmesh.addVolume([2, 38, 49, 1, 164, 203, 190, 100, 87, 191], 180)
femmesh.addVolume([2, 38, 18, 49, 164, 166, 63, 190, 203, 204], 181)
femmesh.addVolume([48, 49, 38, 3, 206, 203, 145, 99, 188, 142], 182)
femmesh.addVolume([38, 49, 1, 3, 203, 191, 87, 142, 188, 88], 183)
femmesh.addVolume([49, 4, 3, 48, 189, 102, 188, 206, 184, 99], 184)
femmesh.addVolume([28, 48, 4, 18, 187, 184, 75, 124, 205, 122], 185)
femmesh.addVolume([49, 18, 4, 48, 204, 122, 189, 206, 205, 184], 186)
femmesh.addVolume([7, 50, 5, 29, 193, 194, 76, 125, 211, 77], 187)
femmesh.addVolume([50, 6, 5, 29, 195, 101, 194, 211, 146, 77], 188)
femmesh.addVolume([50, 9, 19, 6, 212, 105, 216, 195, 53, 104], 189)
femmesh.addVolume([50, 19, 9, 39, 216, 105, 212, 213, 169, 215], 190)
femmesh.addVolume([50, 19, 8, 6, 216, 65, 192, 195, 104, 52], 191)
femmesh.addVolume([40, 9, 10, 19, 214, 54, 196, 168, 105, 106], 192)
femmesh.addVolume([51, 44, 34, 14, 217, 144, 218, 219, 220, 156], 193)
femmesh.addVolume([51, 24, 44, 14, 221, 186, 217, 219, 114, 220], 194)
femmesh.addVolume([25, 15, 24, 51, 116, 115, 71, 222, 223, 221], 195)
femmesh.addVolume([43, 12, 32, 13, 224, 165, 133, 225, 57, 153], 196)
femmesh.addVolume([43, 12, 23, 42, 224, 111, 174, 93, 201, 175], 197)
femmesh.addVolume([43, 12, 13, 23, 224, 57, 225, 174, 111, 112], 198)
femmesh.addVolume([43, 12, 42, 32, 224, 201, 93, 133, 165, 143], 199)
femmesh.addVolume([34, 13, 14, 44, 155, 58, 156, 144, 226, 220], 200)
femmesh.addVolume([14, 24, 15, 51, 114, 115, 59, 219, 221, 223], 201)
femmesh.addVolume([23, 24, 14, 44, 70, 114, 113, 176, 186, 220], 202)
femmesh.addVolume([33, 32, 13, 43, 81, 153, 154, 132, 133, 225], 203)
femmesh.addVolume([34, 33, 13, 43, 82, 154, 155, 134, 132, 225], 204)
femmesh.addVolume([35, 14, 15, 51, 157, 59, 158, 227, 219, 223], 205)
femmesh.addVolume([25, 45, 15, 51, 177, 208, 116, 222, 228, 223], 206)
femmesh.addVolume([44, 43, 13, 23, 94, 225, 226, 176, 174, 112], 207)
femmesh.addVolume([35, 34, 14, 51, 83, 156, 157, 227, 218, 219], 208)
femmesh.addVolume([46, 15, 36, 16, 207, 159, 139, 210, 60, 160], 209)
femmesh.addVolume([36, 35, 15, 45, 84, 158, 159, 137, 135, 208], 210)
femmesh.addVolume([37, 36, 16, 46, 85, 160, 161, 138, 139, 210], 211)
femmesh.addVolume([25, 26, 16, 46, 72, 118, 117, 179, 181, 210], 212)
femmesh.addVolume([47, 16, 17, 26, 209, 61, 197, 180, 118, 119], 213)
femmesh.addVolume([47, 16, 26, 46, 209, 118, 180, 97, 210, 181], 214)
femmesh.addVolume([27, 28, 18, 48, 74, 124, 121, 182, 187, 205], 215)
femmesh.addVolume([35, 34, 51, 45, 83, 218, 227, 135, 136, 228], 216)
femmesh.addVolume([13, 14, 44, 23, 58, 220, 226, 112, 113, 176], 217)
femmesh.addVolume([44, 25, 24, 51, 178, 71, 186, 217, 222, 221], 218)
femmesh.addVolume([44, 45, 25, 51, 95, 177, 178, 217, 228, 222], 219)
femmesh.addVolume([46, 15, 16, 25, 207, 60, 210, 179, 116, 117], 220)
femmesh.addVolume([46, 15, 25, 45, 207, 116, 179, 96, 208, 177], 221)
femmesh.addVolume([50, 8, 19, 39, 192, 65, 216, 213, 167, 169], 222)
femmesh.addVolume([35, 51, 15, 45, 227, 223, 158, 135, 228, 208], 223)
femmesh.addVolume([34, 44, 43, 13, 144, 94, 134, 155, 226, 225], 224)
femmesh.addVolume([51, 44, 45, 34, 217, 95, 228, 218, 144, 136], 225)
femmesh.addVolume([9, 29, 39, 30, 147, 127, 215, 148, 78, 126], 226)
femmesh.addVolume([40, 9, 19, 39, 214, 105, 168, 90, 215, 169], 227)
#
# add it to the analysis
femmesh_obj.FemMesh = femmesh
doc.Analysis.Member = doc.Analysis.Member + [doc.Box_Mesh]
# ```

### Recompute

```# recompute
doc.recompute()
# ```

### Run the analysis

```# run the analysis
import FemGui
import FemToolsCcx
fea = FemToolsCcx.FemToolsCcx()
fea.update_objects()
message = fea.check_prerequisites()
if not message:
fea.reset_all()
fea.run()
else:
FreeCAD.Console.PrintError("Houston, we have a problem! {}\n".format(message))  # in report view
print("Houston, we have a problem! {}\n".format(message))  # in python console
# ```

### Show the results

```# run the analysis
for m in analysis_object.Member:
if m.isDerivedFrom('Fem::FemResultObject'):
result_object = m
break
#
femmesh_obj.ViewObject.setNodeDisplacementByVectors(result_object.NodeNumbers, result_object.DisplacementVectors)
femmesh_obj.ViewObject.applyDisplacement(10)
# ```

##### Scripting FEM mesh objects

Scripting the Netgen mesh object was tried here: http://forum.freecadweb.org/viewtopic.php?f=18&t=16944#p134519 , but has some limitations. On the contrary the GMSH mesh object fully supports python scripting. See forum topic http://forum.freecadweb.org/viewtopic.php?f=18&t=20087

## Appendix

Have Fun!

Other languages:
English • ‎italiano