Skip to content

Commit 3a34965

Browse files
Setup ReactionSystem on AbstractSystem interface
1 parent 9fe6d3b commit 3a34965

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed
Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,56 @@
11
struct Reaction
22
rate
3-
reactants
4-
products
3+
reactants::Vector{Operation}
4+
products::Vector{Operation}
55
end
66

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)
1220
end
1321

1422
# TODO: Make it do the combinatorics stuff
1523
reaction_expr(reactants) = *(reactants...)
1624

1725
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]
2028

21-
for rx in rs.rxs
29+
for rx in rs.eqs
2230
for reactant in rx.reactants
23-
i = findfirst(x->x.op == reactant.op,rs.dvs)
31+
i = findfirst(x->x == reactant.op,rs.states)
2432
eqs[i] = Equation(eqs[i].lhs,eqs[i].rhs - rx.rate * reaction_expr(rx.reactants))
2533
end
2634

2735
for product in rx.products
28-
i = findfirst(x->x.op == product.op,rs.dvs)
36+
i = findfirst(x->x == product.op,rs.states)
2937
eqs[i] = Equation(eqs[i].lhs,eqs[i].rhs + rx.rate * reaction_expr(rx.reactants))
3038
end
3139
end
3240
eqs
3341
end
3442

3543
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]
3745

38-
for (j,rx) in enumerate(rs.rxs)
46+
for (j,rx) in enumerate(rs.eqs)
3947
for reactant in rx.reactants
40-
i = findfirst(x->x.op == reactant.op,rs.dvs)
48+
i = findfirst(x->x == reactant.op,rs.states)
4149
eqs[i,j] = -sqrt(rx.rate) * reaction_expr(rx.reactants)
4250
end
4351

4452
for product in rx.products
45-
i = findfirst(x->x.op == product.op,rs.dvs)
53+
i = findfirst(x->x == product.op,rs.states)
4654
eqs[i,j] = sqrt(rx.rate) * reaction_expr(rx.reactants)
4755
end
4856
end
@@ -51,12 +59,13 @@ end
5159

5260
function Base.convert(::Type{<:ODESystem},rs::ReactionSystem)
5361
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))
5564
end
5665

5766
function Base.convert(::Type{<:SDESystem},rs::ReactionSystem)
58-
D = Differential(rs.iv)
5967
eqs = essemble_drift(rs)
6068
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))
6271
end

test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ using SafeTestsets, Test
99
@safetestset "Mass Matrix Test" begin include("mass_matrix.jl") end
1010
@safetestset "SDESystem Test" begin include("sdesystem.jl") end
1111
@safetestset "NonlinearSystem Test" begin include("nonlinearsystem.jl") end
12-
@safetestset "OptimizationSystem Test" begin include("optimizationsystem.jl") end
12+
@safetestset "OptimizationSystem Test" begin include("optimizationsystem.jl") en
13+
@safetestset "ReactionSystem Test" begin include("reactionsystem.jl") end
1314
@safetestset "Build Targets Test" begin include("build_targets.jl") end
1415
@safetestset "Domain Test" begin include("domains.jl") end
1516
@safetestset "Constraints Test" begin include("constraints.jl") end

0 commit comments

Comments
 (0)