Skip to content

Commit 134041d

Browse files
authored
Merge pull request #957 from SciML/tests___new___vector_valued
[v14 - Awaiting MTK fix] Tests for vector valued species/parameters
2 parents 50312ad + f4fed5a commit 134041d

File tree

1 file changed

+178
-1
lines changed

1 file changed

+178
-1
lines changed

test/upstream/mtk_problem_inputs.jl

Lines changed: 178 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,183 @@ let
158158
end
159159
end
160160

161+
### Vector Species/Parameters Tests ###
162+
163+
begin
164+
# Declares the model (with vector species/parameters, with/without default values, and observables).
165+
t = default_t()
166+
@species X(t)[1:2] Y(t)[1:2] = [10.0, 20.0] XY(t)[1:2]
167+
@parameters p[1:2] d[1:2] = [0.2, 0.5]
168+
rxs = [
169+
Reaction(p[1], [], [X[1]]),
170+
Reaction(p[2], [], [X[2]]),
171+
Reaction(d[1], [X[1]], []),
172+
Reaction(d[2], [X[2]], []),
173+
Reaction(p[1], [], [Y[1]]),
174+
Reaction(p[2], [], [Y[2]]),
175+
Reaction(d[1], [Y[1]], []),
176+
Reaction(d[2], [Y[2]], [])
177+
]
178+
observed = [XY[1] ~ X[1] + Y[1], XY[2] ~ X[2] + Y[2]]
179+
@named model_vec = ReactionSystem(rxs, t; observed)
180+
model_vec = complete(model_vec)
181+
182+
# Declares various u0 versions (scalarised and vector forms).
183+
u0_alts_vec = [
184+
# Vectors not providing default values.
185+
[X => [1.0, 2.0]],
186+
[X[1] => 1.0, X[2] => 2.0],
187+
[model_vec.X => [1.0, 2.0]],
188+
[model_vec.X[1] => 1.0, model_vec.X[2] => 2.0],
189+
[:X => [1.0, 2.0]],
190+
# Vectors providing default values.
191+
[X => [1.0, 2.0], Y => [10.0, 20.0]],
192+
[X[1] => 1.0, X[2] => 2.0, Y[1] => 10.0, Y[2] => 20.0],
193+
[model_vec.X => [1.0, 2.0], model_vec.Y => [10.0, 20.0]],
194+
[model_vec.X[1] => 1.0, model_vec.X[2] => 2.0, model_vec.Y[1] => 10.0, model_vec.Y[2] => 20.0],
195+
[:X => [1.0, 2.0], :Y => [10.0, 20.0]],
196+
# Dicts not providing default values.
197+
Dict([X => [1.0, 2.0]]),
198+
Dict([X[1] => 1.0, X[2] => 2.0]),
199+
Dict([model_vec.X => [1.0, 2.0]]),
200+
Dict([model_vec.X[1] => 1.0, model_vec.X[2] => 2.0]),
201+
Dict([:X => [1.0, 2.0]]),
202+
# Dicts providing default values.
203+
Dict([X => [1.0, 2.0], Y => [10.0, 20.0]]),
204+
Dict([X[1] => 1.0, X[2] => 2.0, Y[1] => 10.0, Y[2] => 20.0]),
205+
Dict([model_vec.X => [1.0, 2.0], model_vec.Y => [10.0, 20.0]]),
206+
Dict([model_vec.X[1] => 1.0, model_vec.X[2] => 2.0, model_vec.Y[1] => 10.0, model_vec.Y[2] => 20.0]),
207+
Dict([:X => [1.0, 2.0], :Y => [10.0, 20.0]]),
208+
# Tuples not providing default values.
209+
(X => [1.0, 2.0]),
210+
(X[1] => 1.0, X[2] => 2.0),
211+
(model_vec.X => [1.0, 2.0]),
212+
(model_vec.X[1] => 1.0, model_vec.X[2] => 2.0),
213+
(:X => [1.0, 2.0]),
214+
# Tuples providing default values.
215+
(X => [1.0, 2.0], Y => [10.0, 20.0]),
216+
(X[1] => 1.0, X[2] => 2.0, Y[1] => 10.0, Y[2] => 20.0),
217+
(model_vec.X => [1.0, 2.0], model_vec.Y => [10.0, 20.0]),
218+
(model_vec.X[1] => 1.0, model_vec.X[2] => 2.0, model_vec.Y[1] => 10.0, model_vec.Y[2] => 20.0),
219+
(:X => [1.0, 2.0], :Y => [10.0, 20.0]),
220+
]
221+
222+
# Declares various ps versions (vector forms only).
223+
p_alts_vec = [
224+
# Vectors not providing default values.
225+
[p => [1.0, 2.0]],
226+
[model_vec.p => [1.0, 2.0]],
227+
[:p => [1.0, 2.0]],
228+
# Vectors providing default values.
229+
[p => [4.0, 5.0], d => [0.2, 0.5]],
230+
[model_vec.p => [4.0, 5.0], model_vec.d => [0.2, 0.5]],
231+
[:p => [4.0, 5.0], :d => [0.2, 0.5]],
232+
# Dicts not providing default values.
233+
Dict([p => [1.0, 2.0]]),
234+
Dict([model_vec.p => [1.0, 2.0]]),
235+
Dict([:p => [1.0, 2.0]]),
236+
# Dicts providing default values.
237+
Dict([p => [4.0, 5.0], d => [0.2, 0.5]]),
238+
Dict([model_vec.p => [4.0, 5.0], model_vec.d => [0.2, 0.5]]),
239+
Dict([:p => [4.0, 5.0], :d => [0.2, 0.5]]),
240+
# Tuples not providing default values.
241+
(p => [1.0, 2.0]),
242+
(model_vec.p => [1.0, 2.0]),
243+
(:p => [1.0, 2.0]),
244+
# Tuples providing default values.
245+
(p => [4.0, 5.0], d => [0.2, 0.5]),
246+
(model_vec.p => [4.0, 5.0], model_vec.d => [0.2, 0.5]),
247+
(:p => [4.0, 5.0], :d => [0.2, 0.5]),
248+
]
249+
250+
# Declares a timespan.
251+
tspan = (0.0, 10.0)
252+
end
253+
254+
# Perform ODE simulations (singular and ensemble).
255+
let
256+
# Creates normal and ensemble problems.
257+
base_oprob = ODEProblem(model_vec, u0_alts_vec[1], tspan, p_alts_vec[1])
258+
base_sol = solve(base_oprob, Tsit5(); saveat = 1.0)
259+
base_eprob = EnsembleProblem(base_oprob)
260+
base_esol = solve(base_eprob, Tsit5(); trajectories = 2, saveat = 1.0)
261+
262+
# Simulates problems for all input types, checking that identical solutions are found.
263+
@test_broken false # Cannot remake problem (https://github.com/SciML/ModelingToolkit.jl/issues/2804).
264+
# for u0 in u0_alts_vec, p in p_alts_vec
265+
# oprob = remake(base_oprob; u0, p)
266+
# @test base_sol == solve(oprob, Tsit5(); saveat = 1.0)
267+
# eprob = remake(base_eprob; u0, p)
268+
# @test base_esol == solve(eprob, Tsit5(); trajectories = 2, saveat = 1.0)
269+
# end
270+
end
271+
272+
# Perform SDE simulations (singular and ensemble).
273+
let
274+
# Creates normal and ensemble problems.
275+
base_sprob = SDEProblem(model_vec, u0_alts_vec[1], tspan, p_alts_vec[1])
276+
base_sol = solve(base_sprob, ImplicitEM(); seed, saveat = 1.0)
277+
base_eprob = EnsembleProblem(base_sprob)
278+
base_esol = solve(base_eprob, ImplicitEM(); seed, trajectories = 2, saveat = 1.0)
279+
280+
# Simulates problems for all input types, checking that identical solutions are found.
281+
@test_broken false # Cannot remake problem (https://github.com/SciML/ModelingToolkit.jl/issues/2804).
282+
# for u0 in u0_alts_vec, p in p_alts_vec
283+
# sprob = remake(base_sprob; u0, p)
284+
# @test base_sol == solve(sprob, ImplicitEM(); seed, saveat = 1.0)
285+
# eprob = remake(base_eprob; u0, p)
286+
# @test base_esol == solve(eprob, ImplicitEM(); seed, trajectories = 2, saveat = 1.0)
287+
# end
288+
end
289+
290+
# Perform jump simulations (singular and ensemble).
291+
let
292+
# Creates normal and ensemble problems.
293+
base_dprob = DiscreteProblem(model_vec, u0_alts_vec[1], tspan, p_alts_vec[1])
294+
base_jprob = JumpProblem(model_vec, base_dprob, Direct(); rng)
295+
base_sol = solve(base_jprob, SSAStepper(); seed, saveat = 1.0)
296+
base_eprob = EnsembleProblem(base_jprob)
297+
base_esol = solve(base_eprob, SSAStepper(); seed, trajectories = 2, saveat = 1.0)
298+
299+
# Simulates problems for all input types, checking that identical solutions are found.
300+
@test_broken false # Cannot remake problem (https://github.com/SciML/ModelingToolkit.jl/issues/2804).
301+
# for u0 in u0_alts_vec, p in p_alts_vec
302+
# jprob = remake(base_jprob; u0, p)
303+
# @test base_sol == solve(base_jprob, SSAStepper(); seed, saveat = 1.0)
304+
# eprob = remake(base_eprob; u0, p)
305+
# @test base_esol == solve(eprob, SSAStepper(); seed, trajectories = 2, saveat = 1.0)
306+
# end
307+
end
308+
309+
# Solves a nonlinear problem (EnsembleProblems are not possible for these).
310+
let
311+
base_nlprob = NonlinearProblem(model_vec, u0_alts_vec[1], p_alts_vec[1])
312+
base_sol = solve(base_nlprob, NewtonRaphson())
313+
@test_broken false # Cannot remake problem (https://github.com/SciML/ModelingToolkit.jl/issues/2804).
314+
# for u0 in u0_alts_vec, p in p_alts_vec
315+
# nlprob = remake(base_nlprob; u0, p)
316+
# @test base_sol == solve(nlprob, NewtonRaphson())
317+
# end
318+
end
319+
320+
# Perform steady state simulations (singular and ensemble).
321+
let
322+
# Creates normal and ensemble problems.
323+
base_ssprob = SteadyStateProblem(model_vec, u0_alts_vec[1], p_alts_vec[1])
324+
base_sol = solve(base_ssprob, DynamicSS(Tsit5()))
325+
base_eprob = EnsembleProblem(base_ssprob)
326+
base_esol = solve(base_eprob, DynamicSS(Tsit5()); trajectories = 2)
327+
328+
# Simulates problems for all input types, checking that identical solutions are found.
329+
@test_broken false # Cannot remake problem (https://github.com/SciML/ModelingToolkit.jl/issues/2804).
330+
# for u0 in u0_alts_vec, p in p_alts_vec
331+
# ssprob = remake(base_ssprob; u0, p)
332+
# @test base_sol == solve(ssprob, DynamicSS(Tsit5()))
333+
# eprob = remake(base_eprob; u0, p)
334+
# @test base_esol == solve(eprob, DynamicSS(Tsit5()); trajectories = 2)
335+
# end
336+
end
337+
161338
### Checks Errors On Faulty Inputs ###
162339

163340
# Checks various erroneous problem inputs, ensuring that these throw errors.
@@ -244,4 +421,4 @@ let
244421
end
245422
end
246423
end
247-
end
424+
end

0 commit comments

Comments
 (0)