-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Constraints, flattening, and extending for ReactionSystems #423
Conversation
Some simple examples rn = @reaction_network rn begin
(k1,k2), A <--> B
end k1 k2
@parameters a,b
@variables t,A(t),C(t)
D = Differential(t)
eqs = [D(C) ~ -b*C + a*A]
@named osys = ODESystem(eqs,t,[A,C],[a,b])
rn2 = extend(osys,rn)
rnodes = convert(ODESystem,rn2)
# if there are actually ODEs in the ODESystem we error
@test_throws ErrorException convert(NonlinearSystem,rn2)
# no ODEs below, so can represent as ODE or Nonlinear systems and convert to either
eqs = [0 ~ -a*A + C, 0 ~ -b*C + a*A]
@named nlsys = NonlinearSystem(eqs,[A,C],[a,b])
rn2 = extend(nlsys,rn)
rnodes = convert(ODESystem,rn2)
rnnlsys = convert(NonlinearSystem,rn2)
@named nlsys = ODESystem(eqs,t,[A,C],[a,b])
rn2 = extend(nlsys,rn)
rnodes = convert(ODESystem,rn2)
rnnlsys = convert(NonlinearSystem,rn2) |
With I'll probably tackle the |
Perhaps a better example, which I plan to put in a tutorial: function repressed_gene(; name)
@parameters t α₀ α K n δ β μ
@variables m(t) P(t) R(t)
rxs = [
Reaction(α₀, nothing, [m]),
Reaction(α / (1 + (R/K)^n), nothing, [m]),
Reaction(δ, [m], nothing),
Reaction(β, [m], [m,P]),
Reaction(μ, [P], nothing)
]
specs = [m,P,R]
pars = [α₀,α,K,n,δ,β,μ]
ReactionSystem(rxs, t, specs, pars; name=name)
end
@named sys₁ = repressed_gene()
@named sys₂ = repressed_gene()
@named sys₃ = repressed_gene()
connections = [sys₁.R ~ sys₃.P,
sys₂.R ~ sys₁.P,
sys₃.R ~ sys₂.P]
@named csys = NonlinearSystem(connections, [sys₁.R,sys₃.P,sys₂.R,sys₁.P,sys₃.R,sys₂.P],[])
@named repressilator = ReactionSystem(t; constraints=csys, systems=[sys₁,sys₂,sys₃])
# or
@named repressilator = ReactionSystem(t; systems=[sys₁,sys₂,sys₃])
@named repressilator = extend(csys, repressilator) |
The repressilator example is really elegant! Have been lagging a bit on developments in this direction, but looking forward to playing around with it. |
Thanks! BTW, do you know if there is a way we could make the macro work in such a function? i.e. something like function repressed_gene(; name)
@reaction_network $name begin
...
end
end It would be cool if we could use the DSL to generate the core gene module in this way. |
I think it should be possible, but uncertain how to get it working. E.g. I tried name = :repressed_gene
rg = @reaction_network :($(name)) begin
(p,d), 0 <--> X
end p d
rg.name but got an error
there are some meta-programming stuff going on here which I don't fully remember how to deal with. It would probably be possible to make a function |
@TorkelE I think I just added that feature in. Could you take a look at the changes in |
I think that looks good. Admittedly I haven't done metaprogramming in a while, so a bit rusty when it comes to these things. I'll play around with things a bit when it is merged as well in case I were to find anything, but I'm not worried. |
Once this gets merged I'm planning to add a tutorial using all this stuff. |
Enables
extend
ingReactionSystem
s with algebraic and ODE constraints. While we currently can have sub-systems of algebraic (or even ODE) constraints, this is annoying for specifying algebraic equations to couple systems (since everything needs to get parent scoped). i.e. imagine wanting to hook three systems representing genes together, conceptually this is not necessarily a hierarchical system, but more a collection of parallel systems with coupling equations one level up. (This wouldn't necessarily be an issue if we could make the top-level system aNonlinearSystem
, but currently MT requires sub-systems to have the same type, so top-level systems must beReactionSystem
s.)This PR allows attaching to a
ReactionSystem
an internal constraint system, which is considered at the same scope as theReactionSystem
. In this way we can logicallyextend
ReactionSystem
s with equations and/or otherReactionSystem
s (we already supportcompose
). For a simplealias_elimination
of basicA ~ B
type equations we could thenflatten
the full hierarchy of systems into oneReactionSystem
and one internalsys.constraint
system, and then apply the elimination.Still TODO:get_constraints(network)
and support a parallel constraint subsystemflatten
extend
ODESystem
s as constraints, and error if then try to convert to NonlinearSystem.