@@ -11,21 +11,53 @@ struct ReactionSystem
1111 ps
1212end
1313
14- function Base. convert (:: Type{<:ODESystem} ,rs:: ReactionSystem )
14+ # TODO : Make it do the combinatorics stuff
15+ reaction_expr (reactants) = * (reactants... )
16+
17+ function essemble_drift (rs)
1518 D = Differential (rs. iv)
1619 eqs = [D (x) ~ 0 for x in rs. dvs]
1720
1821 for rx in rs. rxs
1922 for reactant in rx. reactants
2023 i = findfirst (x-> x. op == reactant. op,rs. dvs)
21- eqs[i] = Equation (eqs[i]. lhs,eqs[i]. rhs - reactant)
24+ eqs[i] = Equation (eqs[i]. lhs,eqs[i]. rhs - rx. rate * reaction_expr (rx. reactants))
25+ end
26+
27+ for product in rx. products
28+ i = findfirst (x-> x. op == product. op,rs. dvs)
29+ eqs[i] = Equation (eqs[i]. lhs,eqs[i]. rhs + rx. rate * reaction_expr (rx. reactants))
30+ end
31+ end
32+ eqs
33+ end
34+
35+ function essemble_diffusion (rs)
36+ eqs = Expression[Constant (0 ) for x in rs. dvs, y in rs. rxs]
37+ @show size (eqs)
38+
39+ for (j,rx) in enumerate (rs. rxs)
40+ for reactant in rx. reactants
41+ i = findfirst (x-> x. op == reactant. op,rs. dvs)
42+ eqs[i,j] -= sqrt (rx. rate) * reaction_expr (rx. reactants)
2243 end
2344
2445 for product in rx. products
2546 i = findfirst (x-> x. op == product. op,rs. dvs)
26- eqs[i] = Equation (eqs[i] . lhs, + (eqs[i] . rhs, rx. reactants... ) )
47+ eqs[i,j] += sqrt (rx . rate) * reaction_expr ( rx. reactants)
2748 end
2849 end
50+ eqs
51+ end
2952
53+ function Base. convert (:: Type{<:ODESystem} ,rs:: ReactionSystem )
54+ eqs = essemble_drift (rs)
3055 ODESystem (eqs,rs. iv,rs. dvs,rs. ps)
3156end
57+
58+ function Base. convert (:: Type{<:SDESystem} ,rs:: ReactionSystem )
59+ D = Differential (rs. iv)
60+ eqs = essemble_drift (rs)
61+ noiseeqs = essemble_diffusion (rs)
62+ SDESystem (eqs,noiseeqs,rs. iv,rs. dvs,rs. ps)
63+ end
0 commit comments