Resetting and starting simulations from the beginning

Asked by drasszkusz

Hello,

I would like to ask that if there is a way to run simulations like batch mode but without using the actual batch mode function.
In more details:
1. I would like to run a simulation which will stop at a given time.
2. Then I would like to save a data.
3. After, I would like to reload/reset the simulation and change one thing (ie. density of a particle).
4. Then run the simulation again and save the same data.

I have created an MWE using the bouncing sphere example. In this example, I don't change any parameter. I just run the simulation for 1 sec, then I print the number of the current run, then reset and start again.
My problem is that the simulation won't reload and won't start automatically again. I have tried O.loadTmp() instead of O.reload() but had no success.

-----------MWE-----------

O.bodies.append(
        [
                # fixed: particle's position in space will not change (support)
                sphere(center=(0, 0, 0), radius=.5, fixed=True),
                # this particles is free, subject to dynamics
                sphere((0, 0, 2), .5)
        ]
)

O.engines = [
        ForceResetter(),
        InsertionSortCollider([Bo1_Sphere_Aabb()]),
        InteractionLoop(
                [Ig2_Sphere_Sphere_ScGeom()], # collision geometry
                [Ip2_FrictMat_FrictMat_FrictPhys()], # collision "physics"
                [Law2_ScGeom_FrictPhys_CundallStrack()] # contact law -- apply forces
        ),
        # Apply gravity force to particles. damping: numerical dissipation of energy.
        NewtonIntegrator(gravity=(0, 0, -9.81), damping=0.1),
        PyRunner(command='stop()',realPeriod=.5)
]

O.dt = .5e-4 * PWaveTimeStep()

index=0
def stop():
   global index
   if O.time>1:
       index=index+1
       print(str(index)+'. run')
       O.reload()
       #O.loadTmp()
       O.run()

# save the simulation, so that it can be reloaded later, for experimentation
O.saveTmp()
O.run()

-----------End of MWE----------

Thank you in advance for your help !
D

Question information

Language:
English Edit question
Status:
Solved
For:
Yade Edit question
Assignee:
No assignee Edit question
Solved by:
Jan Stránský
Solved:
Last query:
Last reply:
Revision history for this message
drasszkusz (drasszkusz) said :
#1

Sorry, I forgot to add that I'm using Yade 20230525-7264~94b01fd~jammy1 (yadedaily) on an Ubuntu 22.04.2 LTS.

Revision history for this message
drasszkusz (drasszkusz) said :
#3

Thank you for the general answer, but it won't solve the problem mentioned above.

Revision history for this message
Best Jan Stránský (honzik) said :
#4

Hello,

> run simulations like batch mode but without using the actual batch mode

Batch mode is exactly for this purposes.
If you know the values in advance, it should be the preferred method.
If you need to change the values dynamically, then some custom solution is maybe better (also a "glue" python script could be created, creating tables, running yade-batch and evaluating results).

> My problem is that the simulation won't reload and won't start automatically again

See [1] for some information.
You cannot do "simply" these kind of operations from within O.engines

> I have tried O.loadTmp() instead of O.reload() but had no success.

Please be specific, what / how you have tried.

If you want to change some parameters, depending on actual use case, probably O.saveTmp / O.loadTmp is not the way to go, as the original value is probably already saved and it is better to create the simulation from the scratch.

A few possible approaches:

1)
### using O.saveTmp, O.loadTml and thread as in [1]
O.bodies.append(sphere(center=(0,0,0),radius=1))
O.engines = [
        ForceResetter(),
        InsertionSortCollider([Bo1_Sphere_Aabb()]),
        InteractionLoop(
                [Ig2_Sphere_Sphere_ScGeom()],
                [Ip2_FrictMat_FrictMat_FrictPhys()],
                [Law2_ScGeom_FrictPhys_CundallStrack()]
        ),
        NewtonIntegrator(),
        PyRunner(command='stop()',virtPeriod=10)
]
O.saveTmp()

def restart(dt=1):
    O.loadTmp()
    O.dt = dt
    O.run()

index=0
def stop():
   global index
   index = index + 1
   print(f"{index}. run, dt={O.dt}")
   O.pause()
   if index > 10:
       return
   import _thread
   _thread.start_new_thread(restart, (O.dt+1,))

restart()
###

2)
### "from scratch" each time
index=0
params = {
    "dt": 1,
}

def stop():
   global index
   index = index + 1
   O.pause()
   print(f"{index}. run, dt={O.dt}")
   params["dt"] = index + 1

def simulation():
    O.bodies.append(sphere(center=(0,0,0),radius=1))
    O.engines = [
            ForceResetter(),
            InsertionSortCollider([Bo1_Sphere_Aabb()]),
            InteractionLoop(
                    [Ig2_Sphere_Sphere_ScGeom()],
                    [Ip2_FrictMat_FrictMat_FrictPhys()],
                    [Law2_ScGeom_FrictPhys_CundallStrack()]
            ),
            NewtonIntegrator(),
            PyRunner(command='stop()',virtPeriod=10)
    ]
    O.saveTmp()
    O.dt = params["dt"]
    O.run()
    O.wait()

while index < 10:
    dt = index + 1
    simulation()
###

Surely there are more options and/or combinations of them

Cheers
Jan

[1] https://yade-dem.org/doc/user.html#checkpoints

Revision history for this message
Jan Stránský (honzik) said :
#5

>> The steps on how to reset and start simulations from the beginning vary depending on the simulation software that you are using. However, here are some general steps that you can follow:
>> ...
>
> Thank you for the general answer, but it won't solve the problem mentioned above.

It was a (probably AI generated) spam

Revision history for this message
drasszkusz (drasszkusz) said :
#6

Thanks Jan Stránský, that solved my question.