Parametrized FEM study

About the development of the FEM module/workbench.

Moderator: bernd

Brucesc2
Posts: 11
Joined: Tue May 17, 2016 8:38 pm

Parametrized FEM study

Postby Brucesc2 » Wed Aug 10, 2016 12:25 pm

Hi,

Im looking to create a parametrized study using the FEM module. My first and only idea to do this is to use the Macro editor and create a script somehow. A number of questions arose because it sounded simpler than it was:

1. How to activate the Analysis?
2. How to find the methods to start the simulation?
3. How to keep and store the results, then how to show the results?

I just started using the FEM Module and the python script but somewhat it confuses me and I am stuck how to find methods for the GUI objects, by using dir, but it only gives the "common view-object methods" as I see it.

Although this could be a more "python" aimed topic, it has still somewhat with fundamental FEM studies that professional uses for design evaluations.

This is the original post, I add the comments from PrzemoF:
I'd suggest to start a new topic and take a look at the test for FEM wb [1]. It does full analysis except instead running CalculiX makes a mock calculations.

[1] https://github.com/FreeCAD/FreeCAD/blob ... TestFem.py
Best Regards,
Markus
User avatar
bernd
Posts: 8043
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Parametrized FEM study

Postby bernd » Wed Aug 10, 2016 2:14 pm

How to solve CalculiX analysis by python. This are just the same defs called by the gui buttons.
Start FreeCAD --> change to start wb --> load 3D FEM example

Code: Select all

import FemGui
FemGui.setActiveAnalysis(FreeCAD.ActiveDocument.Analysis)
import FemToolsCcx
fea = FemToolsCcx.FemToolsCcx()
fea.update_objects()
fea.check_prerequisites() # a string is returned, the string should be empty
fea.reset_all()
fea.run()
fea.load_results()
User avatar
bernd
Posts: 8043
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Parametrized FEM study

Postby bernd » Wed Aug 10, 2016 2:36 pm

How to make the 3D example by python ...

Start FreeCAD --> change to start wb --> load 3D FEM example
delete the FEM objects ...

Code: Select all

App.ActiveDocument.removeObject("MechanicalMaterial")
App.ActiveDocument.removeObject("FemConstraintForce")
App.ActiveDocument.removeObject("FemConstraintFixed")
App.ActiveDocument.removeObject("CalculiX")
App.ActiveDocument.removeObject("CalculiX_static_results")
App.ActiveDocument.removeObject("Analysis")
recreate the objects by python ...

Code: Select all

doc = App.ActiveDocument

# analysis
import FemAnalysis
FemAnalysis.makeFemAnalysis('Analysis')

# solver
solver_object = FemSolverCalculix.makeFemSolverCalculix('CalculiX')
solver_object.GeometricalNonlinearity = 'linear'
solver_object.SteadyState = True
solver_object.MatrixSolverType = 'default'
solver_object.IterationsControlParameterTimeUse = False
doc.Analysis.Member = doc.Analysis.Member + [solver_object]

# material
import MechanicalMaterial
material_object = MechanicalMaterial.makeMechanicalMaterial('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 = doc.addObject("Fem::ConstraintFixed", "FemConstraintFixed")
fixed_constraint.References = [(doc.Box, "Face1")]
doc.Analysis.Member = doc.Analysis.Member + [fixed_constraint]

# force_constraint
force_constraint = doc.addObject("Fem::ConstraintForce", "FemConstraintForce")
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]

# mesh
doc.Analysis.Member = doc.Analysis.Member + [doc.Box_Mesh]

# recompute doc
doc.recompute()
how to solve see last post ... result will be the same as before ...
Brucesc2
Posts: 11
Joined: Tue May 17, 2016 8:38 pm

Re: Parametrized FEM study

Postby Brucesc2 » Sun Aug 14, 2016 1:52 pm

Thanks, could you give me an example of how to find the python method from the code so that I could find them myself? Or is it all the code as supplied in that git-version available in the python console?
Im thinking also to include how to make the mesh in python, and in the end maybe a simple GUI.

BR
Markus
User avatar
bernd
Posts: 8043
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Parametrized FEM study

Postby bernd » Mon Aug 15, 2016 5:18 am

Brucesc2 wrote:Thanks, could you give me an example of how to find the python method from the code so that I could find them myself?
- check the module TestFem.py in source code, https://github.com/FreeCAD/FreeCAD/blob ... TestFem.py
- while working with FreeCAD Gui watch the python console. Lot's of commands are printed
- ask here for help.

- actually all the commands should be on the wiki at the commands linked on FEM Module but most of them are missing.
- The commands linked on Draft Module and Arch Module are great examples how it should look like ...
User avatar
bernd
Posts: 8043
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Parametrized FEM study

Postby bernd » Mon Aug 15, 2016 5:26 am

to print the supported FEM document object types you could use:

Code: Select all

App.newDocument()
module = 'Fem'
FreeCADGui.doCommand('import ' + module)
for s in sorted(App.ActiveDocument.supportedTypes()):
    if s.startswith(module):
        print s

Code: Select all

>>> import Fem
>>> for s in sorted(App.ActiveDocument.supportedTypes()):
...     if s.startswith(module):
...         print s
... 
Fem::Constraint
Fem::ConstraintBearing
Fem::ConstraintContact
Fem::ConstraintDisplacement
Fem::ConstraintFixed
Fem::ConstraintFluidBoundary
Fem::ConstraintForce
Fem::ConstraintGear
Fem::ConstraintHeatflux
Fem::ConstraintInitialTemperature
Fem::ConstraintPlaneRotation
Fem::ConstraintPressure
Fem::ConstraintPulley
Fem::ConstraintTemperature
Fem::DocumentObject
Fem::FeaturePython
Fem::FemAnalysis
Fem::FemAnalysisPython
Fem::FemMeshObject
Fem::FemMeshShapeNetgenObject
Fem::FemMeshShapeObject
Fem::FemPostClipFilter
Fem::FemPostCutFilter
Fem::FemPostFilter
Fem::FemPostFunction
Fem::FemPostFunctionProvider
Fem::FemPostObject
Fem::FemPostPipeline
Fem::FemPostPlaneFunction
Fem::FemPostScalarClipFilter
Fem::FemPostSphereFunction
Fem::FemPostWarpVectorFilter
Fem::FemResultObject
Fem::FemSetElementsObject
Fem::FemSetFacesObject
Fem::FemSetGeometryObject
Fem::FemSetNodesObject
Fem::FemSetObject
Fem::FemSolverObject
Fem::FemSolverObjectPython
>>> 
To get the supported attributes of an document object:
- select the object in tree browser
- in selection view right click on it --> to python console --> you have it saved in obj
- in python console just do a:

Code: Select all

obj.PropertiesList
for the solver object you get

Code: Select all

>>> obj = App.getDocument("Unnamed2").getObject("CalculiX")
>>> obj.PropertiesList
['AnalysisType', 'EigenmodeHighLimit', 'EigenmodeLowLimit', 'EigenmodesCount', 'ExpressionEngine', 'GeometricalNonlinearity', 'IterationsControlParameterCutb', 'IterationsControlParameterIter', 'IterationsControlParameterTimeUse', 'IterationsMaximum', 'Label', 'MatrixSolverType', 'Proxy', 'SolverType', 'SteadyState', 'TimeEnd', 'TimeInitialStep', 'WorkingDir']
>>> 
How to set an property see one of the posts before.

hope this helps, bernd
User avatar
bernd
Posts: 8043
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Parametrized FEM study

Postby bernd » Mon Aug 15, 2016 5:29 am

Brucesc2 wrote:Im thinking also to include how to make the mesh in python, and in the end maybe a simple GUI.
FEM Mesh
Brucesc2
Posts: 11
Joined: Tue May 17, 2016 8:38 pm

Re: Parametrized FEM study

Postby Brucesc2 » Tue Aug 16, 2016 9:44 pm

Hey,

Is there anyway to access the netgen object to start the execute command? How can I set the shape for example? I can only access the ViewProviderFemMesh and not its not really needed for my application, but Im thinking to develop a more user friendly approach as well and see if I could contribute. The attached example can be used, the test example uses a solid cone and mesh (named cone_mesh) as input (the solid cone needs to be selected when running the script)

https://github.com/FreeCAD/FreeCAD/blob ... Object.cpp

BR
Markus
Attachments
test.txt
(3.4 KiB) Downloaded 58 times
User avatar
bernd
Posts: 8043
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Parametrized FEM study

Postby bernd » Wed Aug 17, 2016 10:25 am

try:

Code: Select all

import Part
box = Part.makeBox(10,10,10)
box_obj = App.ActiveDocument.addObject('Part::Box', 'MyBox')
femmesh_obj = App.ActiveDocument.addObject('Fem::FemMeshShapeNetgenObject', 'MyMesh')
femmesh_obj.Shape = box_obj
femmesh_obj.MaxSize = 1
App.ActiveDocument.recompute()
femmesh_obj.FemMesh
ickby
Posts: 2905
Joined: Wed Oct 05, 2011 7:36 am

Re: Parametrized FEM study

Postby ickby » Wed Aug 17, 2016 10:41 am

Hm it seems it would be nice to have a "python only" workflow for all FEM calculations, without having any document objects. Like the Mesh object which allows pure python handling, the same would be nice for material, constraitns, calculation, postproccesing etc.
Normally in FreeCAD every functionality is provided in a python interface of some kind (like FEM:Mesh object or Part::Shape) and the document objects only wrap those python functionality. I think one day we should do that for FEM too.