Skip to content

Commit 18f5d3a

Browse files
authored
Merge pull request #797 from SciML/handle_different_paraemter_types
Handle different parameters types
2 parents 6589937 + 461b138 commit 18f5d3a

File tree

7 files changed

+539
-207
lines changed

7 files changed

+539
-207
lines changed

src/reactionsystem.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ struct ReactionSystem{V <: NetworkProperties} <:
500500
"""Dependent unknown variables representing species"""
501501
species::Vector{BasicSymbolic{Real}}
502502
"""Parameter variables. Must not contain the independent variable."""
503-
ps::Vector{BasicSymbolic{Real}}
503+
ps::Vector{Any}
504504
"""Maps Symbol to corresponding variable."""
505505
var_to_name::Dict{Symbol, Any}
506506
"""Equations for observed variables."""
@@ -546,6 +546,11 @@ struct ReactionSystem{V <: NetworkProperties} <:
546546
name, systems, defaults, connection_type, nps, cls, cevs, devs,
547547
metadata = nothing, complete = false; checks::Bool = true)
548548

549+
# Checks that all parameters have the appropriate Symbolics type.
550+
for p in ps
551+
(p isa Symbolics.BasicSymbolic) || error("Parameter $p is not a `BasicSymbolic`. This is required.")
552+
end
553+
549554
# unit checks are for ODEs and Reactions only currently
550555
nonrx_eqs = Equation[eq for eq in eqs if eq isa Equation]
551556
if checks && isempty(sivs)

src/spatial_reaction_systems/spatial_reactions.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,28 @@ function check_spatial_reaction_validity(rs::ReactionSystem, tr::TransportReacti
9292
end
9393

9494
# Checks that the species does not exist in the system with different metadata.
95-
if any([isequal(tr.species, s) && !isequivalent(tr.species, s) for s in species(rs)])
95+
if any(isequal(tr.species, s) && !isequivalent(tr.species, s) for s in species(rs))
9696
error("A transport reaction used a species, $(tr.species), with metadata not matching its lattice reaction system. Please fetch this species from the reaction system and used in transport reaction creation.")
9797
end
98-
if any([isequal(rs_p, tr_p) && !equivalent_metadata(rs_p, tr_p)
99-
for rs_p in parameters(rs), tr_p in Symbolics.get_variables(tr.rate)])
98+
if any(isequal(rs_p, tr_p) && !isequivalent(rs_p, tr_p)
99+
for rs_p in parameters(rs), tr_p in Symbolics.get_variables(tr.rate))
100100
error("A transport reaction used a parameter with metadata not matching its lattice reaction system. Please fetch this parameter from the reaction system and used in transport reaction creation.")
101101
end
102102

103103
# Checks that no edge parameter occur among rates of non-spatial reactions.
104-
if any([!isempty(intersect(Symbolics.get_variables(r.rate), edge_parameters)) for r in reactions(rs)])
104+
if any(!isempty(intersect(Symbolics.get_variables(r.rate), edge_parameters)) for r in reactions(rs))
105105
error("Edge paramter(s) were found as a rate of a non-spatial reaction.")
106106
end
107107
end
108-
equivalent_metadata(p1, p2) = isempty(setdiff(p1.metadata, p2.metadata, [Catalyst.EdgeParameter => true]))
109108

110109
# Since MTK's "isequal" ignores metadata, we have to use a special function that accounts for this.
111110
# This is important because whether something is an edge parameter is defined in metadata.
111+
const ep_metadata = Catalyst.EdgeParameter => true
112112
function isequivalent(sym1, sym2)
113-
!isequal(sym1, sym2) && (return false)
114-
(sym1.metadata != sym2.metadata) && (return false)
113+
isequal(sym1, sym2) || (return false)
114+
any((md1 != ep_metadata) && !(md1 in sym2.metadata) for md1 in sym1.metadata) && (return false)
115+
any((md2 != ep_metadata) && !(md2 in sym1.metadata) for md2 in sym2.metadata) && (return false)
116+
(typeof(sym1) != typeof(sym2)) && (return false)
115117
return true
116118
end
117119

test/dsl/dsl_options.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
# Fetch packages.
66
using Catalyst, ModelingToolkit, OrdinaryDiffEq, Plots, Test
7+
using Symbolics: unwrap
78

89
# Sets the default `t` to use.
910
t = default_t()
@@ -344,6 +345,50 @@ let
344345
@test merge(ModelingToolkit.defaults(rn28), defs28) == ModelingToolkit.defaults(rn27)
345346
end
346347

348+
# Tests that parameter type designation works.
349+
let
350+
# Creates a model.
351+
rn = @reaction_network begin
352+
@parameters begin
353+
k1
354+
l1
355+
k2::Float64 = 2.0
356+
l2::Float64
357+
k3::Int64 = 2, [description="A parameter"]
358+
l3::Int64
359+
k4::Float32, [description="Another parameter"]
360+
l4::Float32
361+
k5::Rational{Int64}
362+
l5::Rational{Int64}
363+
end
364+
(k1,l1), X1 <--> Y1
365+
(k2,l2), X2 <--> Y2
366+
(k3,l3), X3 <--> Y3
367+
(k4,l4), X4 <--> Y4
368+
(k5,l5), X5 <--> Y5
369+
end
370+
371+
# Checks parameter types.
372+
@test unwrap(rn.k1) isa SymbolicUtils.BasicSymbolic{Real}
373+
@test unwrap(rn.l1) isa SymbolicUtils.BasicSymbolic{Real}
374+
@test unwrap(rn.k2) isa SymbolicUtils.BasicSymbolic{Float64}
375+
@test unwrap(rn.l2) isa SymbolicUtils.BasicSymbolic{Float64}
376+
@test unwrap(rn.k3) isa SymbolicUtils.BasicSymbolic{Int64}
377+
@test unwrap(rn.l3) isa SymbolicUtils.BasicSymbolic{Int64}
378+
@test unwrap(rn.k4) isa SymbolicUtils.BasicSymbolic{Float32}
379+
@test unwrap(rn.l4) isa SymbolicUtils.BasicSymbolic{Float32}
380+
@test unwrap(rn.k5) isa SymbolicUtils.BasicSymbolic{Rational{Int64}}
381+
@test unwrap(rn.l5) isa SymbolicUtils.BasicSymbolic{Rational{Int64}}
382+
383+
# Checks that other parameter properties are assigned properly.
384+
@test !ModelingToolkit.hasdefault(unwrap(rn.k1))
385+
@test ModelingToolkit.getdefault(unwrap(rn.k2)) == 2.0
386+
@test ModelingToolkit.getdefault(unwrap(rn.k3)) == 2
387+
@test ModelingToolkit.getdescription(unwrap(rn.k3)) == "A parameter"
388+
@test ModelingToolkit.getdescription(unwrap(rn.k4)) == "Another parameter"
389+
@test !ModelingToolkit.hasdescription(unwrap(rn.k5))
390+
end
391+
347392
### Observables ###
348393

349394
# Test basic functionality.

0 commit comments

Comments
 (0)