← Back to team overview

yade-users team mailing list archive

Re: [Question #697227]: Calibrate material parameter

 

Question #697227 on Yade changed:
https://answers.launchpad.net/yade/+question/697227

    Status: Open => Answered

Jan Stránský proposed the following answer:
>> create a (python?) program running simulations, evaluate results, updating input parameters.
> -How can I do that, which command do I need add to run it again with the new parameters?

there are maaany options.

1) "external" program
- read original template script, replace input parameters
- run simulation (using .e.g python subprocess module)
- extract saved data
- update input parameters
- repeat

2) using O.reset() inside a loop

.....

a MWE for approach 1:

### simulation.py.in
from yade import plot
radius = {radius}
b = sphere((0,0,0),radius)
O.bodies.append(b)
mass = sum(b.state.mass for b in O.bodies)
plot.addData(mass=mass)
plot.saveDataTxt("m.dat")
###

### runner.py
import subprocess
yade = "yadedaily"
radii = [1,2,3] # input data to simulate

with open("simulation.py.in") as f:
    scriptIn = f.read() # "template"

for r in radii:
    script = scriptIn.format(radius=r) # replaced input parameters
    with open("simulation.py","w") as f:
        f.write(script)
    cmd = "{} -n -x simulation.py {}".format(yade,r)
    p = subprocess.Popen(cmd.split(),stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
    p.wait()
    with open("m.dat") as f:
        f.readline()
        m = f.readline().strip()
    print("radius",r,"mass",m)
###

run as
python runner.py

instead of
radii = [1,2,3]
for r in radii: ...
you can of course implement more interesting logic (otherwise yade-batch makes more sense for fixed set of parameters)

Cheers
Jan

-- 
You received this question notification because your team yade-users is
an answer contact for Yade.