Skip to content

Commit 294cb99

Browse files
Merge pull request #284 from SciML/reaction
Setup ReactionSystem on AbstractSystem interface
2 parents 10f4b4d + 837bb1a commit 294cb99

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,15 @@ and a parameters `β₁` and `β₂`.
296296

297297

298298
```julia
299-
t = Variable(:t; known = true)() # independent variables are treated as known
300-
α = Variable(; known = true)() # parameters are known
301-
σ = Variable(; known = true) # left uncalled, since it is used as a function
302-
w = Variable(:w; known = false) # unknown, left uncalled
303-
x = Variable(:x; known = false)(t) # unknown, depends on `t`
304-
y = Variable(:y; known = false)() # unknown, no dependents
305-
z = Variable(:z; known = false)(t, α, x) # unknown, multiple arguments
306-
β₁ = Variable(, 1; known = true)() # with index 1
307-
β₂ = Variable(, 2; known = true)() # with index 2
299+
t = Variable(:t)() # independent variables are treated as known
300+
α = Variable()() # parameters are known
301+
σ = Variable() # left uncalled, since it is used as a function
302+
w = Variable(:w) # unknown, left uncalled
303+
x = Variable(:x)(t) # unknown, depends on `t`
304+
y = Variable(:y)() # unknown, no dependents
305+
z = Variable(:z)(t, α, x) # unknown, multiple arguments
306+
β₁ = Variable(, 1)() # with index 1
307+
β₂ = Variable(, 2)() # with index 2
308308

309309
expr = β₁ * x + y^α + σ(3) * (z - t) - β₂ * w(t - 1)
310310
```
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using SafeTestsets, Test
1010
@safetestset "SDESystem Test" begin include("sdesystem.jl") end
1111
@safetestset "NonlinearSystem Test" begin include("nonlinearsystem.jl") end
1212
@safetestset "OptimizationSystem Test" begin include("optimizationsystem.jl") end
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)