Parameter study

About the development of the FEM module/workbench.

Moderator: bernd

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

Parameter study

Postby HoWil » Sat Dec 31, 2016 10:39 am

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
Posts: 1872
Joined: Sun Jul 07, 2013 12:08 pm

Re: Parameter study

Postby ulrich1a » Sat Dec 31, 2016 12:42 pm

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
Posts: 831
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Parameter study

Postby HoWil » Sun Jan 01, 2017 7:34 pm

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
Posts: 8021
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Parameter study

Postby bernd » Mon Jan 02, 2017 10:47 am

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 1842 times
User avatar
bernd
Posts: 8021
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Parameter study

Postby bernd » Mon Jan 02, 2017 11:32 am

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

Re: Parameter study

Postby HoWil » Tue Jan 03, 2017 2:54 pm

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
Posts: 831
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Parameter study

Postby HoWil » Tue Jan 03, 2017 3:01 pm

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
Posts: 831
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Parameter study

Postby HoWil » Tue Jan 03, 2017 8:55 pm

Added a matplotlib plot instead of the points in draw-wb. The results look like:
results.png
results.png (36.91 KiB) Viewed 1774 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
Posts: 831
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: Parameter study

Postby HoWil » Mon Feb 06, 2017 10:16 am

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 from 2017-02-06 11-15-46.png
screenshot of the results also plotted with Plot-WB.
Screenshot from 2017-02-06 11-15-46.png (183.04 KiB) Viewed 1682 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 377 times
Running the sweep and evaluation:
running_the_sweep_and_evaluation.webm
Running the sweep and evaluation.
(945.72 KiB) Downloaded 386 times
Sample files:
FC_elmer_electrostatic_example_beam_parameter_sweep.zip
Sample files
(82.95 KiB) Downloaded 49 times
Requirements for running this example (installed Elmer, Gmsh, ...) are described in the included "explaination.txt".
Jee-Bee
Posts: 1913
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: Parameter study

Postby Jee-Bee » Mon Feb 06, 2017 10:33 am

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