ecell / ecell4_base

An integrated software environment for multi-algorithm, multi-timescale, multi-spatial-representation simulation of various cellular phenomena
https://ecell4.e-cell.org/
GNU General Public License v3.0
63 stars 23 forks source link

Improve the way to change a model during simulations #306

Closed kaizu closed 5 years ago

kaizu commented 5 years ago

Provide the better way to switch models.

The current way is as follows:

w = f.create_world()
w.bind_to(first_model)

sim = f.create_simulator(w)
sim.run(duration1)

sim = f.create_simulator(second_model, w)
sim.initialize()
sim.run(duration2)

print(w.t())  #=> duration1 + duration2

What I expect is something like:

w = f.create_world()
w.bind_to(first_model)

sim = f.create_simulator(w)
sim.run(duration1)

sim.switch(second_model)
sim.run(duration2)
kaizu commented 5 years ago

The code below already works. It looks simple enough.

w = f.create_world()
w.bind_to(first_model)

f.create_simulator(first_model, w).run(duration1)
f.create_simulator(second_model, w).run(duration2)

print(w.t())  #=> duration1 + duration2

The member function of factories could be simplified more.

w = f.create_world()
w.bind_to(first_model)

f.simulator(first_model, w).run(duration1)
f.simulator(second_model, w).run(duration2)
kaizu commented 5 years ago

By the way, switch is unavailable because it's a reserved word in C++.

kaizu commented 5 years ago

Finally, the following is an example to change rates.

from ecell4 import *

def create_model(kf, kr):
    with reaction_rules():
        ~A == A | (kf, kr)
    return get_model()

m1 = create_model(1.0, 1.0)
m2 = create_model(0.0, 1.0)

f = gillespie.GillespieFactory()
w = f.create_world(ones() * cbrt(100))
w.bind_to(m1)

obs = FixedIntervalNumberObserver(0.1, ["A"])

f.create_simulator(m1, w).run(10.0, obs)
f.create_simulator(m2, w).run(10.0, obs)

show(obs)