Skip to content

Commit 4c704e0

Browse files
add ReactionSystem and start SDESystem components
1 parent a3777c3 commit 4c704e0

File tree

6 files changed

+56
-12
lines changed

6 files changed

+56
-12
lines changed

src/ModelingToolkit.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ using MacroTools
1616
import MacroTools: splitdef, combinedef, postwalk, striplines
1717
import GeneralizedGenerated
1818
using DocStringExtensions
19+
using Base: RefValue
1920

2021
"""
2122
$(TYPEDEF)
@@ -91,7 +92,7 @@ include("simplify.jl")
9192
include("utils.jl")
9293
include("direct.jl")
9394
include("domains.jl")
94-
include("systems/diffeqs/diffeqsystem.jl")
95+
include("systems/diffeqs/odesystem.jl")
9596
include("systems/diffeqs/first_order_transform.jl")
9697
include("systems/nonlinear/nonlinear_system.jl")
9798
include("systems/pde/pdesystem.jl")

src/latexify_recipes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
return lhs, rhs
2323
end
2424

25-
@latexrecipe function f(eqs::Vector{ModelingToolkit.DiffEq}; iv=:t)
25+
@latexrecipe function f(eqs::Vector{ModelingToolkit.ODEExpr}; iv=:t)
2626
# Set default option values.
2727
env --> :align
2828

src/systems/diffeqs/diagonalsdesystem.jl

Whitespace-only changes.
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
export ODESystem, ODEFunction
22

3-
4-
using Base: RefValue
5-
6-
73
isintermediate(eq::Equation) = !(isa(eq.lhs, Operation) && isa(eq.lhs.op, Differential))
84

95
function flatten_differential(O::Operation)
@@ -14,21 +10,25 @@ function flatten_differential(O::Operation)
1410
return (x, t, order + 1)
1511
end
1612

13+
<<<<<<< master:src/systems/diffeqs/diffeqsystem.jl
1714
struct DiffEq # dⁿx/dtⁿ = rhs
15+
=======
16+
struct ODEExpr # dⁿx/dtⁿ = rhs
17+
>>>>>>> add ReactionSystem and start SDESystem components:src/systems/diffeqs/odesystem.jl
1818
x::Variable
1919
n::Int
2020
rhs::Expression
2121
end
22-
function to_diffeq(eq::Equation)
22+
function Base.convert(::Type{ODEExpr},eq::Equation)
2323
isintermediate(eq) && throw(ArgumentError("intermediate equation received"))
2424
(x, t, n) = flatten_differential(eq.lhs)
2525
(isa(t, Operation) && isa(t.op, Variable) && isempty(t.args)) ||
2626
throw(ArgumentError("invalid independent variable $t"))
2727
(isa(x, Operation) && isa(x.op, Variable) && length(x.args) == 1 && isequal(first(x.args), t)) ||
2828
throw(ArgumentError("invalid dependent variable $x"))
29-
return t.op, DiffEq(x.op, n, eq.rhs)
29+
return t.op, ODEExpr(x.op, n, eq.rhs)
3030
end
31-
Base.:(==)(a::DiffEq, b::DiffEq) = isequal((a.x, a.n, a.rhs), (b.x, b.n, b.rhs))
31+
Base.:(==)(a::ODEExpr, b::ODEExpr) = isequal((a.x, a.n, a.rhs), (b.x, b.n, b.rhs))
3232

3333
"""
3434
$(TYPEDEF)
@@ -56,7 +56,7 @@ de = ODESystem(eqs)
5656
"""
5757
struct ODESystem <: AbstractSystem
5858
"""The ODEs defining the system."""
59-
eqs::Vector{DiffEq}
59+
eqs::Vector{ODEExpr}
6060
"""Independent variable."""
6161
iv::Variable
6262
"""Dependent (state) variables."""
@@ -86,7 +86,7 @@ struct ODESystem <: AbstractSystem
8686
end
8787

8888
function ODESystem(eqs)
89-
reformatted = to_diffeq.(eqs)
89+
reformatted = convert.(ODEExpr,eqs)
9090

9191
ivs = unique(r[1] for r reformatted)
9292
length(ivs) == 1 || throw(ArgumentError("one independent variable currently supported"))
@@ -102,8 +102,12 @@ function ODESystem(eqs)
102102
ODESystem(deqs, iv, dvs, ps)
103103
end
104104

105+
<<<<<<< master:src/systems/diffeqs/diffeqsystem.jl
105106
function ODESystem(deqs::AbstractVector{DiffEq}, iv, dvs, ps)
106107
tgrad = RefValue(Vector{Expression}(undef, 0))
108+
=======
109+
function ODESystem(deqs::AbstractVector{ODEExpr}, iv, dvs, ps)
110+
>>>>>>> add ReactionSystem and start SDESystem components:src/systems/diffeqs/odesystem.jl
107111
jac = RefValue(Matrix{Expression}(undef, 0, 0))
108112
Wfact = RefValue(Matrix{Expression}(undef, 0, 0))
109113
Wfact_t = RefValue(Matrix{Expression}(undef, 0, 0))
@@ -114,7 +118,7 @@ function ODESystem(deqs::AbstractVector{<:Equation}, iv, dvs, ps)
114118
_dvs = [deq.op for deq dvs]
115119
_iv = iv.op
116120
_ps = [p.op for p ps]
117-
ODESystem(getindex.(to_diffeq.(deqs),2), _iv, _dvs, _ps)
121+
ODESystem(getindex.(convert.(ODEExpr,deqs),2), _iv, _dvs, _ps)
118122
end
119123

120124
function _eq_unordered(a, b)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
struct Reaction
2+
rate
3+
reactants
4+
products
5+
end
6+
7+
struct ReactionSystem
8+
rxs
9+
iv
10+
dvs
11+
ps
12+
end
13+
14+
function Base.convert(::Type{<:ODESystem},rs::ReactionSystem)
15+
D = Differential(rs.iv)
16+
eqs = [D(x) ~ 0 for x in rs.dvs]
17+
18+
for rx in rs.rxs
19+
for reactant in rx.reactants
20+
i = findfirst(x->x.op == reactant.op,rs.dvs)
21+
eqs[i] = Equation(eqs[i].lhs,eqs[i].rhs - reactant)
22+
end
23+
24+
for product in rx.products
25+
i = findfirst(x->x.op == product.op,rs.dvs)
26+
eqs[i] = Equation(eqs[i].lhs,+(eqs[i].rhs,rx.reactants...))
27+
end
28+
end
29+
30+
ODESystem(eqs,rs.iv,rs.dvs,rs.ps)
31+
end

test/reactions.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using ModelingToolkit
2+
3+
@parameters t a b
4+
@variables x(t) y(t) z(t)
5+
rxs = [Reaction(a,[x],[y]),
6+
Reaction(b,[y],[x])]
7+
rs = ReactionSystem(rxs,t,[x,y],[a,b])
8+
sys = convert(ODESystem,rs)

0 commit comments

Comments
 (0)