Saving/loading simulations

Asked by behzad

Hi,

I'm saving a simple simulation(the script is copied here) at some time step. When I quit Yade and load the save file again, the plot functions are lost. Do we need to save functions as well?

In the loaded XML file, I've got the spheres and loads on them but the function to plot is not defined.

#========================
O.reset()
from yade import utils, plot
from yade import pack, qt

id_Mat2=O.materials.append(FrictMat(young=1e4,poisson=0.3,density=2600,frictionAngle=1))
Mat2=O.materials[id_Mat2]

s1=utils.sphere([0.0,0.0,0.0],0.01,fixed=True,material=Mat2)
s2=utils.sphere([0.0,0.0,2.0e-2],0.01,fixed=False,material=Mat2)
O.bodies.append(s1)
O.bodies.append(s2)

O.engines=[
ForceResetter(),
ForceEngine(force=(0,0,-2e-3),ids=[1],label='fEngine'),
InsertionSortCollider([Bo1_Sphere_Aabb(),Bo1_Box_Aabb()]),
InteractionLoop(
[Ig2_Sphere_Sphere_ScGeom(),Ig2_Box_Sphere_ScGeom()],
[Ip2_FrictMat_FrictMat_FrictPhys()],
[Law2_ScGeom_FrictPhys_CundallStrack()]
),
NewtonIntegrator(damping=0.7,gravity=[0,0,0]),
PyRunner(command="fEngine.force=(0,0,0)", iterPeriod=5000000),
PyRunner(command='AutoData()',iterPeriod=100000)
]

def AutoData():

        plot.addData(t=O.time,d_numerical=0.02-(O.bodies[s2.id].state.pos[2]))

plot.plots={'t':('d_numerical'),}
plot.plot()

O.dt=1e-6

qt.View()

#=======================

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
behzad
Solved:
Last query:
Last reply:
Revision history for this message
Jérôme Duriez (jduriez) said :
#1

Yes, this is a feature of Yade (you may consider annoying) : all python variables / functions are not saved during a (.xml or .yade) Yade save. Hence, they do not exist once you reload.

Give a look here : https://yade-dem.org/doc/user.html#saving-python-variables

Revision history for this message
behzad (behzad-majidi) said :
#2

Alright!

So, how can we do the following in a single code;

run a model.py

run_to_reach_equilibrium

save xml

call a apply_load.py

run_to_reach_equilibrium

save xml

The idea is to be able to automatically call a python script which includes definitions of parameters and functions after a certain number of iterations and then save the model after reaching for equilibrium state.

This means, putting all the above commands in a single moodel.py and running it.
Thanks

Revision history for this message
Bruno Chareyre (bruno-chareyre) said :
#3

>this is a feature of Yade

More exactly, it is a feature of python:
me@desktop:~$ python
>>> def helloWorld(): print "hello world!"
>>> helloWorld()
hello world!
>>> quit()
me@desktop:~$ python
>>> helloWorld()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'helloWorld' is not defined

One solution is this:

me@desktop:~$ echo "def helloWorld(): print 'hello world!'" > hello.py
me@desktop:~$ python
>>> from hello import *
>>> helloWorld()
hello world!

@Behzad, the simplest way is to define addData() in the script applying the load. In many cases it makes more sense actually, because you don't need the evolution of position before the loading, or at least you don't want it duplicated in the output of each other loading.

Side points:
- Could you explain why you had the idea of saving in xml? It is a bad idea but it is seen frequently. If there is something in the documentation that pushed you toward this we should fix it. Better save as *.yade, or *.yade.gz, much faster and less disk usage.

- "from yade import utils" is useless and can be the source of troubles. Remove also the "utils." prefix in the script.

Revision history for this message
behzad (behzad-majidi) said :
#4

Yes, it makes sense for the application of loads.

- as for saving as xml, I thought it's the only option we have!

cheers,
Behzad

Revision history for this message
behzad (behzad-majidi) said :
#5

Hi,

so, how we call the loading script? It's a python script. Is there anything like call loading.py or something?

Revision history for this message
Bruno Chareyre (bruno-chareyre) said :
#6

>as for saving as xml, I thought it's the only option we have!

Why? Did you read it somewhere?

>how we call the loading script?

What do you mean? You call it just like the script generating the initial packing.
$ yade script.py
or if yade is already running:
>>> run script.py

The script will probably start with O.load("saved.yade")

Revision history for this message
Jérôme Duriez (jduriez) said :
#7

You mean calling a python script from the python interface of Yade ?
If yes, you may try:

execfile('theNameOfYourScript.py')

Revision history for this message
behzad (behzad-majidi) said :
#8

Alright, I tried bith execfile('loading.py') and run loading.py

For the moment run loading.py is what I was looking for. It's pretty useful. Because, I can load a saved simulation and then use this command to call a script to redefine the parameters and functions all together.

execfile('loading.py') is also useful because it can be included in the original script.

@Bruno: Yes, it's in loading and saving section of introduction [1]:

[1]: https://yade-dem.org/doc/introduction.html?highlight=xml

Thanks guys

Revision history for this message
Bruno Chareyre (bruno-chareyre) said :
#9

>@Bruno: Yes, it's in loading and saving section of introduction

TYVM. I just fixed it.
B

Revision history for this message
Bruno Chareyre (bruno-chareyre) said :
#10

For future reference, a method to escape the "reloading user-declared functions/variables" problem is examplified in examples/simple-scene/save-then-reload.py [1].
[1]https://github.com/yade/trunk/blob/master/examples/simple-scene/save-then-reload.py