StochSS / GillesPy2

Modeling toolkit for biochemical simulation
http://gillespy2.readthedocs.io/
GNU General Public License v3.0
72 stars 32 forks source link

Uniform timespans support / 'Long' time simulations #425

Closed kkeywan closed 4 years ago

kkeywan commented 4 years ago

Dear Team

Thank you for this project.

Currently, I try to simulate SSA trajectories of simple biomolecular networks using this package and everything works fine. However, when I increase the timespan for which I want to simulate, I get an InvalidModelError (StochKit only supports uniform timespans), although my timespan is uniform (created by numpy.linspace(0, int(1e6), int(1e7 + 1) ) ).

I figured out that the problem is due to numerical instabilities. For such long timespans, the uniform check in model.py line 550 does not give the desired result and hence the simulation will not be performed, although my timespan fulfills the requirement of being uniform.

Thanks

seanebum commented 4 years ago

Hi @kkeywan,

Unfortunately the comparison of floating point values can be very difficult! The easiest way around this would be to provide support for non-uniform timespans (even though this timespan is technically uniform), which is something we may be looking at in the future. In the meanwhile, here are a couple of ways that you can still run that simulation:

# Option 1: single call  
solver = SSACSolver(model)  
results = solver.run(model=model, t=int(1e6), increment=0.1)

Option 1 lets you call the solver directly and select an increment, rather than a timespan vector.

# Option 2: loop  
import numpy as np  
model.timespan(np.linspace(0, 100, 1001))  
solver = solver=SSACSolver(model)  
results = model.run(solver=solver)  
for i in range(2, 10001):
    results = model.run(solver=solver, resume=results, t=(100*i))

*Recommended: Option 2 lets you iteratively simulate 1000 data points at a time by utilizing the resume feature, as 10 million data points per species is going to be a lot in memory, so approaching that in chunks will most likely work out a lot better. Please let me know if this works for you!