Skip to content

Commit b69688d

Browse files
authored
Merge pull request #1214 from AayushSabharwal/as/fix-delayscope
test: fix `DelayParentScope` test
2 parents b85475f + 86bd7c7 commit b69688d

File tree

7 files changed

+32
-26
lines changed

7 files changed

+32
-26
lines changed

src/spatial_reaction_systems/spatial_reactions.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,32 @@ function check_spatial_reaction_validity(rs::ReactionSystem, tr::TransportReacti
103103
if any(isequal(tr.species, s) && !isequivalent(tr.species, s) for s in species(rs))
104104
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 use it during transport reaction creation.")
105105
end
106-
# No `for` loop, just weird formatting by the formatter.
107106
if any(isequal(rs_p, tr_p) && !isequivalent(rs_p, tr_p)
108-
for rs_p in parameters(rs), tr_p in Symbolics.get_variables(tr.rate))
107+
for rs_p in parameters(rs), tr_p in Symbolics.get_variables(tr.rate))
109108
error("A transport reaction used a parameter with metadata not matching its lattice reaction system. Please fetch this parameter from the reaction system and use it during transport reaction creation.")
110109
end
111110

112111
# Checks that no edge parameter occurs among rates of non-spatial reactions.
113-
# No `for` loop, just weird formatting by the formatter.
114112
if any(!isempty(intersect(Symbolics.get_variables(r.rate), edge_parameters))
115-
for r in reactions(rs))
113+
for r in reactions(rs))
116114
error("Edge parameter(s) were found as a rate of a non-spatial reaction.")
117115
end
118116
end
119117

120118
# Since MTK's "isequal" ignores metadata, we have to use a special function that accounts for this.
121119
# This is important because whether something is an edge parameter is defined in metadata.
120+
# MTK potentially start adding there own metadata to system symbolic variables, to prevent breakage
121+
# for this we now also have al list of `ignored_metadata` (which MTK will add to `ReactionSystem`)
122+
# metadata. Long-term the solution is to permit the creation of spatial reactions within
123+
# a `ReactionSystem` when it is created.
122124
const ep_metadata = Catalyst.EdgeParameter => true
123-
function isequivalent(sym1, sym2)
125+
function isequivalent(sym1, sym2; ignored_metadata = [MT.SymScope])
124126
isequal(sym1, sym2) || (return false)
125-
if any((md1 != ep_metadata) && !(md1 in sym2.metadata) for md1 in sym1.metadata)
127+
if any((md1 != ep_metadata) && (md1[1] ignored_metadata) && (md1 sym2.metadata)
128+
for md1 in sym1.metadata)
126129
return false
127-
elseif any((md2 != ep_metadata) && !(md2 in sym1.metadata) for md2 in sym2.metadata)
130+
elseif any((md2 != ep_metadata) && (md2[1] ignored_metadata) && (md2 sym1.metadata)
131+
for md2 in sym2.metadata)
128132
return false
129133
elseif typeof(sym1) != typeof(sym2)
130134
return false

test/compositional_modelling/component_based_model_creation.jl

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

55
# Fetch packages.
66
using Catalyst, LinearAlgebra, OrdinaryDiffEqTsit5, SciMLNLSolve, Test
7-
using ModelingToolkit: nameof
7+
using ModelingToolkit: nameof, getname
88

99
# Sets the default `t` to use.
1010
t = default_t()
@@ -507,12 +507,12 @@ let
507507
@variables x3(t) x4(t) x5(t)
508508
x2 = ParentScope(x2)
509509
x3 = ParentScope(ParentScope(x3))
510-
x4 = DelayParentScope(x4, 2)
510+
x4 = DelayParentScope(x4)
511511
x5 = GlobalScope(x5)
512512
@parameters p1 p2 p3 p4 p5
513513
p2 = ParentScope(p2)
514514
p3 = ParentScope(ParentScope(p3))
515-
p4 = DelayParentScope(p4, 2)
515+
p4 = DelayParentScope(p4)
516516
p5 = GlobalScope(p5)
517517
rxs = [Reaction(p1, nothing, [x1]), Reaction(p2, [x2], nothing),
518518
D(x3) ~ p3, D(x4) ~ p4, D(x5) ~ p5]
@@ -530,11 +530,11 @@ let
530530
sys3 = sys3 sys2
531531
@test length(unknowns(sys3)) == 4
532532
@test any(isequal(x3), unknowns(sys3))
533-
@test any(isequal(x4), unknowns(sys3))
533+
@test any(endswith("x4") string getname, unknowns(sys3))
534534
@test length(species(sys3)) == 2
535535
@test length(parameters(sys3)) == 4
536536
@test any(isequal(p3), parameters(sys3))
537-
@test any(isequal(p4), parameters(sys3))
537+
@test any(endswith("p4") string getname, parameters(sys3))
538538
sys4 = complete(sys3)
539539
@test length(unknowns(sys3)) == 4
540540
@test length(parameters(sys4)) == 5

test/reactionsystem_core/reactionsystem.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ let
118118
kvals = Float64.(1:length(k))
119119
def_p = [k => kvals]
120120
def_u0 = [A => 0.5, B => 1.0, C => 1.5, D => 2.0]
121-
inits = (MT.Initial(A), MT.Initial(B), MT.Initial(C), MT.Initial(D)) .=> 0
122121
defs = merge(Dict(def_p), Dict(def_u0))
123-
fulldefs = merge(copy(defs), Dict(inits))
124122

125123
@named rs = ReactionSystem(rxs, t, [A, B, C, D], [k]; defaults = defs)
126124
rs = complete(rs)
@@ -133,7 +131,8 @@ let
133131

134132
# these systems add initial conditions to the defaults
135133
@test ModelingToolkit.get_defaults(odesys) ==
136-
ModelingToolkit.get_defaults(sdesys) == fulldefs
134+
ModelingToolkit.get_defaults(sdesys)
135+
@test issubset(defs, ModelingToolkit.get_defaults(odesys))
137136

138137
u0map = [A => 5.0]
139138
kvals[1] = 5.0
@@ -465,9 +464,9 @@ let
465464
rs = complete(rs)
466465
@test all(eq -> eq isa Reaction, ModelingToolkit.get_eqs(rs)[1:4])
467466
osys = complete(convert(ODESystem, rs))
468-
inits = Initial.((B, C, D, E))
469467
@test issetequal(MT.get_unknowns(osys), [B, C, D, E])
470-
@test issetequal(MT.get_ps(osys), [k1, k2, A, inits...])
468+
_ps = filter(!isinitial, MT.get_ps(osys))
469+
@test issetequal(_ps, [k1, k2, A])
471470

472471
# test nonlinear systems
473472
u0 = [1.0, 2.0, 3.0, 4.0]
@@ -497,9 +496,9 @@ let
497496
@named rs = ReactionSystem(rxs, t) # add constraint csys when supported!
498497
rs = complete(rs)
499498
ssys = complete(convert(SDESystem, rs))
500-
inits = Initial.((B, C, D, E))
501499
@test issetequal(MT.get_unknowns(ssys), [B, C, D, E])
502-
@test issetequal(MT.get_ps(ssys), [A, k1, k2, inits...])
500+
_ps = filter(!isinitial, MT.get_ps(ssys))
501+
@test issetequal(_ps, [A, k1, k2])
503502
du1 = zeros(4)
504503
du2 = zeros(4)
505504
sprob = SDEProblem(ssys, u0map, tspan, pmap; check_length = false)

test/simulation_and_solving/jacobian_construction.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ let
9696
nlprob_sjac = NonlinearProblem(rn, u0, ps; jac = true, sparse = true)
9797

9898
# Checks that Jacobians ar identical.
99+
# Approx is due to https://github.com/SciML/ModelingToolkit.jl/issues/3554.
99100
function eval_jac(prob, sparse)
100101
J = sparse ? deepcopy(prob.f.jac_prototype) : zeros(length(prob.u0), length(prob.u0))
101102
ModelingToolkit.is_time_dependent(prob) ? prob.f.jac(J, prob.u0, prob.p, 0.0) : prob.f.jac(J, prob.u0, prob.p)
102103
return J
103104
end
104105
@test eval_jac(oprob_jac, false) == eval_jac(sprob_jac, false) == eval_jac(nlprob_jac, false)
105-
@test_broken eval_jac(oprob_sjac, true) == eval_jac(sprob_sjac, true) == eval_jac(nlprob_sjac, true) # https://github.com/SciML/ModelingToolkit.jl/issues/3527
106+
@test eval_jac(oprob_sjac, true) eval_jac(sprob_sjac, true) atol = 1e-14 rtol = 1e-14
107+
@test eval_jac(oprob_sjac, true) eval_jac(nlprob_sjac, true) atol = 1e-14 rtol = 1e-14
106108
end
107109
end
108110

@@ -136,7 +138,8 @@ let
136138
jac_sparse = jac_eval(rn, u0, ps, t_val; sparse = true)
137139

138140
# Check correctness (both by converting to sparse jac to dense, and through multiplication with other matrix).
139-
@test Matrix(jac_sparse) == jac
141+
# Approx is due to https://github.com/SciML/ModelingToolkit.jl/issues/3554.
142+
@test Matrix(jac_sparse) jac atol = 1e-14 rtol = 1e-14
140143
mat = factor*rand(rng, length(u0), length(u0))
141144
@test jac_sparse * mat jac * mat
142145
end

test/simulation_and_solving/simulate_ODEs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ let
193193
ps = [:k1 => 2.0f0, :k2 => 3.0f0]
194194
oprob = ODEProblem(rn, u0, 1.0f0, ps)
195195
osol = solve(oprob, Tsit5())
196-
@test eltype(osol[:X1]) == eltype(osol[:X2]) == typeof(oprob[:X1]) == typeof(oprob[:X2]) == Float32
196+
@test_broken eltype(osol[:X1]) == eltype(osol[:X2]) == typeof(oprob[:X1]) == typeof(oprob[:X2]) == Float32 # https://github.com/SciML/ModelingToolkit.jl/issues/3553
197197
@test eltype(osol.t) == typeof(oprob.tspan[1]) == typeof(oprob.tspan[2]) == Float32
198198
end
199199

test/simulation_and_solving/simulate_SDEs.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ end
384384
### Other Tests ###
385385

386386
# Checks that solution values have types consistent with their input types.
387-
# Check that both float types are preserved in the solution (and problems), while integers are
387+
# Check that both float types are preserved in the solution (and problems), while integers are
388388
# promoted to floats.
389389
# Checks that the time types are correct (`Float64` by default or possibly `Float32`), however,
390390
# type conversion only occurs in the solution, and integer types are preserved in problems.
@@ -400,7 +400,7 @@ let
400400
@test eltype(ssol[:X1]) == eltype(ssol[:X2]) == typeof(sprob[:X1]) == typeof(sprob[:X2]) == Float64
401401
@test eltype(ssol.t) == typeof(sprob.tspan[1]) == typeof(sprob.tspan[2]) == Float64
402402

403-
# Checks that `Int64` values are promoted to `Float64`.
403+
# Checks that `Int64` values are promoted to `Float64`.
404404
u0 = [:X1 => 1, :X2 => 3]
405405
ps = [:k1 => 2, :k2 => 3]
406406
sprob = SDEProblem(rn, u0, 1, ps)
@@ -413,7 +413,7 @@ let
413413
ps = [:k1 => 2.0f0, :k2 => 3.0f0]
414414
sprob = SDEProblem(rn, u0, 1.0f0, ps)
415415
ssol = solve(sprob, ISSEM())
416-
@test eltype(ssol[:X1]) == eltype(ssol[:X2]) == typeof(sprob[:X1]) == typeof(sprob[:X2]) == Float32
416+
@test_broken eltype(ssol[:X1]) == eltype(ssol[:X2]) == typeof(sprob[:X1]) == typeof(sprob[:X2]) == Float32
417417
@test eltype(ssol.t) == typeof(sprob.tspan[1]) == typeof(sprob.tspan[2]) == Float32
418418
end
419419

test/test_functions.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function jac_eval(rs::ReactionSystem, u, p, t; combinatoric_ratelaws = true, spa
5151
prob = ODEProblem(rs, u, 0.0, p; jac = true, combinatoric_ratelaws, sparse)
5252
J = sparse ? deepcopy(prob.f.jac_prototype) : zeros(length(u), length(u))
5353
prob.f.jac(J, prob.u0, prob.p, t)
54-
@test J == prob.f.jac(prob.u0, prob.p, t)
54+
@test J prob.f.jac(prob.u0, prob.p, t) atol = 1e-14 rtol = 1e-14
5555
return J
5656
end
5757

0 commit comments

Comments
 (0)