|
1 | 1 | struct Reaction |
2 | 2 | rate |
3 | | - reactants |
4 | | - products |
| 3 | + reactants::Vector{Operation} |
| 4 | + products::Vector{Operation} |
5 | 5 | end |
6 | 6 |
|
7 | | -struct ReactionSystem |
8 | | - rxs |
9 | | - iv |
10 | | - dvs |
11 | | - ps |
| 7 | +struct ReactionSystem <: AbstractSystem |
| 8 | + eqs::Vector{Reaction} |
| 9 | + iv::Variable |
| 10 | + states::Vector{Variable} |
| 11 | + ps::Vector{Variable} |
| 12 | + name::Symbol |
| 13 | + systems::Vector{ReactionSystem} |
| 14 | +end |
| 15 | + |
| 16 | +function ReactionSystem(eqs,iv,dvs,ps; |
| 17 | + systems = ReactionSystem[], |
| 18 | + name=gensym(:ReactionSystem)) |
| 19 | + ReactionSystem(eqs,iv,convert.(Variable,dvs),convert.(Variable,ps),name,systems) |
12 | 20 | end |
13 | 21 |
|
14 | 22 | # TODO: Make it do the combinatorics stuff |
15 | 23 | reaction_expr(reactants) = *(reactants...) |
16 | 24 |
|
17 | 25 | function essemble_drift(rs) |
18 | | - D = Differential(rs.iv) |
19 | | - eqs = [D(x) ~ 0 for x in rs.dvs] |
| 26 | + D = Differential(rs.iv()) |
| 27 | + eqs = [D(x(rs.iv())) ~ 0 for x in rs.states] |
20 | 28 |
|
21 | | - for rx in rs.rxs |
| 29 | + for rx in rs.eqs |
22 | 30 | for reactant in rx.reactants |
23 | | - i = findfirst(x->x.op == reactant.op,rs.dvs) |
| 31 | + i = findfirst(x->x == reactant.op,rs.states) |
24 | 32 | eqs[i] = Equation(eqs[i].lhs,eqs[i].rhs - rx.rate * reaction_expr(rx.reactants)) |
25 | 33 | end |
26 | 34 |
|
27 | 35 | for product in rx.products |
28 | | - i = findfirst(x->x.op == product.op,rs.dvs) |
| 36 | + i = findfirst(x->x == product.op,rs.states) |
29 | 37 | eqs[i] = Equation(eqs[i].lhs,eqs[i].rhs + rx.rate * reaction_expr(rx.reactants)) |
30 | 38 | end |
31 | 39 | end |
32 | 40 | eqs |
33 | 41 | end |
34 | 42 |
|
35 | 43 | function essemble_diffusion(rs) |
36 | | - eqs = Expression[Constant(0) for x in rs.dvs, y in rs.rxs] |
| 44 | + eqs = Expression[Constant(0) for x in rs.states, y in rs.eqs] |
37 | 45 |
|
38 | | - for (j,rx) in enumerate(rs.rxs) |
| 46 | + for (j,rx) in enumerate(rs.eqs) |
39 | 47 | for reactant in rx.reactants |
40 | | - i = findfirst(x->x.op == reactant.op,rs.dvs) |
| 48 | + i = findfirst(x->x == reactant.op,rs.states) |
41 | 49 | eqs[i,j] = -sqrt(rx.rate) * reaction_expr(rx.reactants) |
42 | 50 | end |
43 | 51 |
|
44 | 52 | for product in rx.products |
45 | | - i = findfirst(x->x.op == product.op,rs.dvs) |
| 53 | + i = findfirst(x->x == product.op,rs.states) |
46 | 54 | eqs[i,j] = sqrt(rx.rate) * reaction_expr(rx.reactants) |
47 | 55 | end |
48 | 56 | end |
|
51 | 59 |
|
52 | 60 | function Base.convert(::Type{<:ODESystem},rs::ReactionSystem) |
53 | 61 | eqs = essemble_drift(rs) |
54 | | - ODESystem(eqs,rs.iv,rs.dvs,rs.ps) |
| 62 | + ODESystem(eqs,rs.iv,rs.states,rs.ps,name=rs.name, |
| 63 | + systems=convert.(ODESystem,rs.systems)) |
55 | 64 | end |
56 | 65 |
|
57 | 66 | function Base.convert(::Type{<:SDESystem},rs::ReactionSystem) |
58 | | - D = Differential(rs.iv) |
59 | 67 | eqs = essemble_drift(rs) |
60 | 68 | noiseeqs = essemble_diffusion(rs) |
61 | | - SDESystem(eqs,noiseeqs,rs.iv,rs.dvs,rs.ps) |
| 69 | + SDESystem(eqs,noiseeqs,rs.iv,rs.states,rs.ps, |
| 70 | + name=rs.name,systems=convert.(SDESystem,rs.systems)) |
62 | 71 | end |
0 commit comments