Parameter study

About the development of the FEM module/workbench.

Moderator: bernd

HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Parameter study

Post by HoWil »

Dear FEM-developers,

I want to post a thought/idea for further enhancing the usability of FEM-WB for optimization of parts; its about running parameter studies.
You can think of this as having the basic cantilever-calculix-3D-FEM-example and change the applied force or a dimension according a spreadsheet. For each of this parameters is a calculation done and the results (e.g. max. displacement) is put into a new column in the same spreadsheet.
This could help for instance finding a height of the cantilever where a specific displacement/stress is not exceeded.

I found the following related topics in the forum (see below) concerning the creation of 'part-families' using the spreadsheet but here is no simulation included and no results are read back into the spreadsheet.

What do you think :?: Is this of interest for somebody else or already on the Todo of someone?
BR,
HoWil

Macro of hatari:
viewtopic.php?f=22&t=18437

Python script of ulrich1a:
viewtopic.php?f=10&t=18520
ulrich1a
Veteran
Posts: 1957
Joined: Sun Jul 07, 2013 12:08 pm

Re: Parameter study

Post by ulrich1a »

Dear FEM-user,

its time to start learning python coding, further reading:
http://www.freecadweb.org/wiki/index.ph ... _to_Python
http://www.freecadweb.org/wiki/index.ph ... g_tutorial
What I would do, if you consider the mentioned scripts, is to set up a separate macro, that uses one of those scripts and run the FEM-analysis throughout the listed part variants.
This is not as difficult for a quick and dirty written macro, but may be involved, if it has to be made universal usable with a point and click interface.

What I do, when I want to do some coding in an area of FreeCAD, that I never used before, is the following:
- search for examples (forum, macros, source code, templates, wiki)
- explore the involved objects in the python console, see here: http://www.freecadweb.org/wiki/index.ph ... nterpreter
- write a short python script (I am using Geany as editor. It has a template for a python script, so it will give you a start.)
- call the script in the python console with

Code: Select all

execfile("your_path_to your_python_script")
- explore the results of your variables in the python console and make further tests with it: applying new values to it, look which formulation gives no error message, so you can use it in your script.

This may take a little time, but should be faster, than waiting for somebody else.

I myself did not start a FEM-analysis from python so far. The question I have regarding this, does the call for the external analysis terminates, if the results are written, or do one need to write separate code to check this?

Ulrich
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Parameter study

Post by HoWil »

ulrich1a wrote:Dear FEM-user,
its time to start learning python coding, further reading:
:D I know, and I already do, but the leaning curve is still flat, means I do ask for advice as I have done.
ulrich1a wrote: What I would do, if you consider the mentioned scripts, is to set up a separate macro, that uses one of those scripts and run the FEM-analysis throughout the listed part variants.
This is not as difficult for a quick and dirty written macro, but may be involved, if it has to be made universal usable with a point and click interface.

What I do, when I want to do some coding in an area of FreeCAD, that I never used before, is the following:
- search for examples (forum, macros, source code, templates, wiki)
- explore the involved objects in the python console, see here: http://www.freecadweb.org/wiki/index.ph ... nterpreter
- write a short python script (I am using Geany as editor. It has a template for a python script, so it will give you a start.)
- call the script in the python console with

Code: Select all

execfile("your_path_to your_python_script")
- explore the results of your variables in the python console and make further tests with it: applying new values to it, look which formulation gives no error message, so you can use it in your script.

This may take a little time, but should be faster, than waiting for somebody else.

I myself did not start a FEM-analysis from python so far. The question I have regarding this, does the call for the external analysis terminates, if the results are written, or do one need to write separate code to check this?

Ulrich
Many thanks for the rest.... I will go trough this and hopefully report back some example.
BR,
HoWil
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Parameter study

Post by bernd »

start FreeCAD --> load FEM 3D example from Start WB --> activate the analysis --> run the following code:

Code: Select all

# create the forces to make a multiple analysis with
force = 1000000  # start force = 1.0 MN
fstep = 500000  # step force = 0.5 MN
forces = []
for f in range(20):
    forces.append(force)
    force+=fstep


# initialize analysis
import FemGui
FemGui.setActiveAnalysis(FreeCAD.ActiveDocument.Analysis)
import FemToolsCcx
fea = FemToolsCcx.FemToolsCcx()
fea.update_objects()
fea.check_prerequisites()
fea.reset_all()
fea.update_objects()


# run analysis multiple times
for force in forces:
    App.ActiveDocument.FemConstraintForce.Force = force
    App.ActiveDocument.recompute()
    print App.ActiveDocument.FemConstraintForce.Force
    fea.run()
    fea.load_results()


absdis = []
for o in App.ActiveDocument.Objects:
    if o.isDerivedFrom('Fem::FemResultObject'):
        # print o.Name, ' --> ', o.Stats[11]
        absdis.append(o.Stats[11])


data = []
i = 0
for f in forces:
    d = (f, absdis[i])
    i += 1
    data.append(d)


for d in data:
    print d[0], ' --> ', d[1]


# draw a diagramm with Draft
# make a new document
App.newDocument("Unnamed")
App.setActiveDocument("Unnamed")
import Draft
for d in data:
    Draft.makePoint(d[1], d[0] * 0.00001,0)

Gui.SendMsgToActiveView("ViewFit")
Each CalculiX result is one point. Since it is a linear analysis and the load is just applied in steps the diagram is a boring linear line ...
screen.jpg
screen.jpg (200.86 KiB) Viewed 3941 times
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Parameter study

Post by bernd »

This may help too: FEM_Tutorial_Python
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Parameter study

Post by HoWil »

ulrich1a wrote:The question I have regarding this, does the call for the external analysis terminates, if the results are written, or do one need to write separate code to check this?
Ulrich
Looks like I have to write it because the thing I used until now does not check it:

Code: Select all

process = QtCore.QProcess()
process.startDetached(paraview_command)
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Parameter study

Post by HoWil »

bernd wrote:start FreeCAD --> load FEM 3D example from Start WB --> activate the analysis --> run the following code:
...
Each CalculiX result is one point. Since it is a linear analysis and the load is just applied in steps the diagram is a boring linear line ...
Thank you very much!
Will look into it in detail. Looks pretty much what I want to achieve. Maybe, I will try to combine it with Spreadsheet.
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Parameter study

Post by HoWil »

Added a matplotlib plot instead of the points in draw-wb. The results look like:
results.png
results.png (36.91 KiB) Viewed 3873 times

Code: Select all

##########################################################################
# Setting up the sweep parameter 
##########################################################################

# create the forces to make a multiple analysis with
force = 1000000  # start force = 1.0 MN
fstep = 500000  # step force = 0.5 MN
forces = []
nr_steps = 10
for f in range(nr_steps):
    forces.append(force)
    force+=fstep


##########################################################################
# Initialize analysis
##########################################################################

import FemGui
FemGui.setActiveAnalysis(FreeCAD.ActiveDocument.Analysis)
import FemToolsCcx
fea = FemToolsCcx.FemToolsCcx()
fea.update_objects()
fea.check_prerequisites()
fea.reset_all()
fea.update_objects()

# run analysis multiple times
for force in forces:
    App.ActiveDocument.FemConstraintForce.Force = force
    App.ActiveDocument.recompute()
    print App.ActiveDocument.FemConstraintForce.Force
    fea.run()
    fea.load_results()

# evaluating the results ... absolute displacement
absdis = []
for o in App.ActiveDocument.Objects:
    if o.isDerivedFrom('Fem::FemResultObject'):
        # print o.Name, ' --> ', o.Stats[11]
        absdis.append(o.Stats[11])

data = []
data_abs = []
i = 0
for f in forces:
    displ_abs = absdis[i]
    d = (f, displ_abs)
    i += 1
    data.append(d)
    data_abs.append(displ_abs)

for d in data:
    print d[0], ' --> ', d[1]


##########################################################################
# Plotting the results
##########################################################################
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(forces, absdis, linestyle='-', color='gray', alpha=0.5)
ax.plot(forces, absdis, linestyle='', marker='o')
ax.set_xlabel('Applied force in kN')
xticklabels = []
xticks = ax.get_xticks()
for xt in xticks:
    xticklabels.append(str(xt/1000))

ax.set_xticklabels(xticklabels)
ax.set_ylabel('Absolute displacement in mm')

ax.grid()
plt.show()
fig.savefig('results.png')
HoWil
Veteran
Posts: 1279
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Parameter study

Post by HoWil »

Hello,

I did some scripting on running ElmerSolver and Paraview on a mesh created in FreeCAD while sweeping some parameters. The parameters are specified and changed using Alias_manager. The created "part-family"- files are than reopenen again, mesh, exported as .unv, imported with ElmerGrid, solved with ElmerSolver, evaluated using Paraview and read back into the spreadsheet.
screenshot of the results also plotted with Plot-WB.
screenshot of the results also plotted with Plot-WB.
Screenshot from 2017-02-06 11-15-46.png (183.04 KiB) Viewed 3781 times
For all interested see my demo files and the two screencasts below. I used the 3D-Calculix example and varied the height of the beam.

Preparation:
preparation.webm
Preparation of the model.
(735.22 KiB) Downloaded 438 times
Running the sweep and evaluation:
running_the_sweep_and_evaluation.webm
Running the sweep and evaluation.
(945.72 KiB) Downloaded 436 times
Sample files:
FC_elmer_electrostatic_example_beam_parameter_sweep.zip
Sample files
(82.95 KiB) Downloaded 97 times
Requirements for running this example (installed Elmer, Gmsh, ...) are described in the included "explaination.txt".
Jee-Bee
Veteran
Posts: 2566
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: Parameter study

Post by Jee-Bee »

i can't view your webm files... the container is probably filled with a wmv file...
Post Reply