Skip to content

Commit

Permalink
Merge pull request #442 from isaacsas/var_to_name
Browse files Browse the repository at this point in the history
Add var_to_name field
  • Loading branch information
isaacsas committed Nov 8, 2021
2 parents aa3bc60 + 1d05909 commit 9190fb3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Breaking updates and feature summaries across releases

## Catalyst unreleased (master branch)
- **BREAKING:** Added support for `@unpack observable_variable = rn` and `rn.observable_variable`. This requires a new inner constructor definition.

## Catalyst 10.0
- `ReactionSystem(rxs::Vector{Reaction}, t)` should now work and will infer the
Expand Down
6 changes: 5 additions & 1 deletion src/networkapi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1083,8 +1083,12 @@ function validate(rs::ReactionSystem, info::String="")
end
end
timeunits = get_unit(get_iv(rs))
rateunits = specunits / timeunits

# no units for species, time or parameters then assume validated
(specunits in (MT.unitless,nothing)) && (timeunits in (MT.unitless,nothing)) &&
MT.all_dimensionless(get_ps(rs)) && return true

rateunits = specunits / timeunits
for rx in get_eqs(rs)
rxunits = get_unit(rx.rate)
for (i,sub) in enumerate(rx.substrates)
Expand Down
42 changes: 30 additions & 12 deletions src/reactionsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ struct ReactionSystem{U <: Union{Nothing,MT.AbstractSystem}} <: MT.AbstractTimeD
states::Vector
"""Parameter variables. Must not contain the independent variable."""
ps::Vector
"""Maps Symbol to corresponding variable."""
var_to_name::Dict{Symbol,Any}
"""Equations for observed variables."""
observed::Vector{Equation}
"""The name of the system"""
Expand All @@ -204,24 +206,19 @@ struct ReactionSystem{U <: Union{Nothing,MT.AbstractSystem}} <: MT.AbstractTimeD
"""Non-`Reaction` equations that further constrain the system"""
constraints::U

function ReactionSystem(eqs, iv, states, ps, observed, name, systems, defaults, connection_type, csys;
function ReactionSystem(eqs, iv, states, ps, var_to_name, observed, name, systems, defaults, connection_type, csys;
checks::Bool=true, skipvalue=false)
iv′ = value(iv)
states′ = skipvalue ? states : value.(MT.scalarize(states))
ps′ = skipvalue ? ps : value.(MT.scalarize(ps))

if checks
check_variables(states′, iv′)
check_parameters(ps′, iv′)
# check_units(eqs) # disable as check the newly generated system below
check_variables(states, iv)
check_parameters(ps, iv)
end
rs = new{typeof(csys)}(collect(eqs), iv, states, ps, observed, name, systems, defaults, connection_type, csys)
rs = new{typeof(csys)}(eqs, iv, states, ps, var_to_name, observed, name, systems, defaults, connection_type, csys)
checks && validate(rs)
rs
end
end

function ReactionSystem(eqs, iv, species, ps;
function ReactionSystem(eqs, iv, states, ps;
observed = [],
systems = [],
name = nothing,
Expand All @@ -232,10 +229,31 @@ function ReactionSystem(eqs, iv, species, ps;
checks = true,
constraints = nothing,
skipvalue = false)

name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro"))
sysnames = nameof.(systems)
(length(unique(sysnames)) == length(sysnames)) ||
throw(ArgumentError("System names must be unique."))

ReactionSystem(eqs, iv, species, ps, observed, name, systems, defaults, connection_type,
constraints; checks = checks, skipvalue = skipvalue)
if !(isempty(default_u0) && isempty(default_p))
Base.depwarn("`default_u0` and `default_p` are deprecated. Use `defaults` instead.", :ReactionSystem, force=true)
end
defaults = MT.todict(defaults)
defaults = Dict{Any,Any}(value(k) => value(v) for (k, v) in pairs(defaults))

iv′ = value(iv)
states′ = skipvalue ? states : value.(MT.scalarize(states))
ps′ = skipvalue ? ps : value.(MT.scalarize(ps))
eqs′ = (eqs isa Vector) ? eqs : collect(eqs)

var_to_name = Dict()
MT.process_variables!(var_to_name, defaults, states′)
MT.process_variables!(var_to_name, defaults, ps′)
MT.collect_var_to_name!(var_to_name, (eq.lhs for eq in observed))

ReactionSystem(eqs′, iv′, states′, ps′, var_to_name, observed, name, systems,
defaults, connection_type, constraints;
checks = checks, skipvalue = skipvalue)
end

function ReactionSystem(rxs::Vector{<:Reaction}, iv; kwargs...)
Expand Down
9 changes: 8 additions & 1 deletion test/reactionsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,4 +282,11 @@ js = convert(JumpSystem,rs; combinatoric_ratelaws=false)
rxs = [Reaction(x*t*A*B+y, [A], nothing)]
@named rs1 = ReactionSystem(rxs, t, [A,B], [x,y])
@named rs2 = ReactionSystem(rxs, t)
@test Catalyst.isequal_ignore_names(rs1,rs2)
@test Catalyst.isequal_ignore_names(rs1,rs2)

@variables t, L(t), H(t)
obs = [Equation(L, 2*x + y)]
@named rs3 = ReactionSystem(rxs, t; observed=obs)
L2 = L
@unpack L = rs3
@test isequal(L,L2)

0 comments on commit 9190fb3

Please sign in to comment.