import tellurium as te
import numpy as np
r = te.loada('''
J1: S1 -> S2; k1*S1;
J2: S2 -> S3; k2*S2 - k3*S3
# J2_1: S2 -> S3; k2*S2
# J2_2: S3 -> S2; k3*S3;
J3: S3 -> S4; k4*S3;
k1 = 0.1; k2 = 0.5; k3 = 0.5; k4 = 0.5;
S1 = 100;
''')
r.integrator = 'gillespie'
r.integrator.seed = 1234
selections = ['time'] + r.getBoundarySpeciesIds() + r.getFloatingSpeciesIds()
r.integrator.variable_step_size = False
Ncol = len(r.selections)
Nsim = 30
points = 101
s_sum = np.zeros(shape=[points, Ncol])
for k in range(Nsim):
r.resetToOrigin()
s = r.simulate(0, 50, points, selections=selections)
s_sum += s
r.plot(s, alpha=0.5, show=False)
fig = te.plot(s[:,0], s_sum[:,1:]/Nsim, names=[x + ' (mean)' for x in selections[1:]], title="Stochastic simulation", xtitle="time", ytitle="concentration")