From 29ba6da12b791c36a978621bc5fd7e7c9412f18e Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 26 Mar 2024 15:04:50 -0400 Subject: [PATCH 1/2] fix test warnings --- test/constant_rate.jl | 4 ++-- test/ensemble_uniqueness.jl | 16 ++++++++-------- test/extinction_test.jl | 6 +++--- test/longtimes_test.jl | 2 +- test/monte_carlo_test.jl | 4 ++-- test/saveat_regression.jl | 4 ++-- test/spatial/diffusion.jl | 4 ++-- test/spatial/spatial_majump.jl | 12 ++++++------ test/variable_rate.jl | 22 +++++++++++----------- 9 files changed, 37 insertions(+), 37 deletions(-) diff --git a/test/constant_rate.jl b/test/constant_rate.jl index b4189d41..86c237c0 100644 --- a/test/constant_rate.jl +++ b/test/constant_rate.jl @@ -33,7 +33,7 @@ nums = Int[] @time for i in 1:10000 local jump_prob = JumpProblem(prob, Direct(), jump, jump2; rng = rng) local sol = solve(jump_prob, FunctionMap()) - push!(nums, sol[end]) + push!(nums, sol.u[end]) end @test mean(nums) - 45 < 1 @@ -47,7 +47,7 @@ nums = Int[] @time for i in 1:10000 local jump_prob = JumpProblem(prob, Direct(), jump, jump2; rng = rng) local sol = solve(jump_prob, FunctionMap()) - push!(nums, sol[2]) + push!(nums, sol.u[2]) end @test sum(nums .== 0) / 10000 - 0.33 < 0.02 diff --git a/test/ensemble_uniqueness.jl b/test/ensemble_uniqueness.jl index f5334a42..72dfdd91 100644 --- a/test/ensemble_uniqueness.jl +++ b/test/ensemble_uniqueness.jl @@ -9,12 +9,12 @@ u0 = [0] prob = DiscreteProblem(u0, (0.0, 100.0)) jump_prob = JumpProblem(prob, Direct(), j1, j2; rng = rng) sol = solve(EnsembleProblem(jump_prob), FunctionMap(), trajectories = 3) -@test Array(sol[1]) !== Array(sol[2]) -@test Array(sol[1]) !== Array(sol[3]) -@test Array(sol[2]) !== Array(sol[3]) -@test eltype(sol[1].u[1]) == Int +@test Array(sol.u[1]) !== Array(sol.u[2]) +@test Array(sol.u[1]) !== Array(sol.u[3]) +@test Array(sol.u[2]) !== Array(sol.u[3]) +@test eltype(sol.u[1].u[1]) == Int sol = solve(EnsembleProblem(jump_prob), SSAStepper(), trajectories = 3) -@test Array(sol[1]) !== Array(sol[2]) -@test Array(sol[1]) !== Array(sol[3]) -@test Array(sol[2]) !== Array(sol[3]) -@test eltype(sol[1].u[1]) == Int +@test Array(sol.u[1]) !== Array(sol.u[2]) +@test Array(sol.u[1]) !== Array(sol.u[3]) +@test Array(sol.u[2]) !== Array(sol.u[3]) +@test eltype(sol.u[1].u[1]) == Int diff --git a/test/extinction_test.jl b/test/extinction_test.jl index f489dfe0..41e03498 100644 --- a/test/extinction_test.jl +++ b/test/extinction_test.jl @@ -58,15 +58,15 @@ sol = solve(jprob, SSAStepper(), callback = cb, save_end = false) @test sol.t[end] < 1000.0 # test terminate -function extinction_condition(u, t, integrator) +function extinction_condition2(u, t, integrator) u[1] == 1 end -function extinction_affect!(integrator) +function extinction_affect!2(integrator) (saved, savedexactly) = savevalues!(integrator, true) terminate!(integrator) nothing end -cb = DiscreteCallback(extinction_condition, extinction_affect!, +cb = DiscreteCallback(extinction_condition2, extinction_affect!2, save_positions = (false, false)) dprob = DiscreteProblem(u0, (0.0, 1000.0), rates) jprob = JumpProblem(dprob, Direct(), majump; save_positions = (false, false), rng = rng) diff --git a/test/longtimes_test.jl b/test/longtimes_test.jl index af6fe178..7c787b4f 100644 --- a/test/longtimes_test.jl +++ b/test/longtimes_test.jl @@ -13,4 +13,4 @@ dt = tspan[2] / 1000 dprob = DiscreteProblem(u0, tspan, p) jprob = JumpProblem(dprob, Direct(), maj, save_positions = (false, false), rng = rng) sol = solve(jprob, SSAStepper(), saveat = tspan[1]:dt:tspan[2]) -@test length(unique(sol[(end - 10):end][:])) > 1 +@test length(unique(sol.u[(end - 10):end][:])) > 1 diff --git a/test/monte_carlo_test.jl b/test/monte_carlo_test.jl index f9f6495e..0563ea07 100644 --- a/test/monte_carlo_test.jl +++ b/test/monte_carlo_test.jl @@ -12,11 +12,11 @@ jump_prob = JumpProblem(prob, Direct(), jump; rng = rng) monte_prob = EnsembleProblem(jump_prob) sol = solve(monte_prob, SRIW1(), EnsembleSerial(), trajectories = 3, save_everystep = false, dt = 0.001, adaptive = false) -@test sol[1].t[2] != sol[2].t[2] != sol[3].t[2] +@test sol.u[1].t[2] != sol.u[2].t[2] != sol.u[3].t[2] jump = ConstantRateJump(rate, affect!) jump_prob = JumpProblem(prob, Direct(), jump, save_positions = (true, false), rng = rng) monte_prob = EnsembleProblem(jump_prob) sol = solve(monte_prob, SRIW1(), EnsembleSerial(), trajectories = 3, save_everystep = false, dt = 0.001, adaptive = false) -@test sol[1].t[2] != sol[2].t[2] != sol[3].t[2] +@test sol.u[1].t[2] != sol.u[2].t[2] != sol.u[3].t[2] diff --git a/test/saveat_regression.jl b/test/saveat_regression.jl index 745eaa0e..03665d76 100644 --- a/test/saveat_regression.jl +++ b/test/saveat_regression.jl @@ -18,7 +18,7 @@ sol = JumpProcesses.solve(EnsembleProblem(jprob), SSAStepper(), saveat = ts, trajectories = Nsims) for i in 1:length(sol) - NA .+= sol[i][1, :] + NA .+= sol.u[i][1, :] end for i in 1:length(ts) @@ -33,7 +33,7 @@ sol = JumpProcesses.solve(EnsembleProblem(jprob), SSAStepper(), trajectories = N for i in 1:Nsims for n in 1:length(ts) - NA[n] += sol[i](ts[n])[1] + NA[n] += sol.u[i](ts[n])[1] end end diff --git a/test/spatial/diffusion.jl b/test/spatial/diffusion.jl index c5ab25f6..c014b5c5 100644 --- a/test/spatial/diffusion.jl +++ b/test/spatial/diffusion.jl @@ -45,7 +45,7 @@ starting_state = zeros(Int, length(u0), num_nodes) center_node = trunc(Int, (num_nodes + 1) / 2) starting_state[:, center_node] = copy(u0) tspan = (0.0, tf) -prob = DiscreteProblem(starting_state, tspan, []) +prob = DiscreteProblem(starting_state, tspan) hopping_rate = diffusivity * (linear_size / domain_size)^2 hopping_constants = [hopping_rate for i in starting_state] @@ -162,7 +162,7 @@ for ci in CartesianIndices(hopping_constants) end starting_state = 25 * ones(Int, length(u0), num_nodes) tspan = (0.0, 10.0) -prob = DiscreteProblem(starting_state, tspan, []) +prob = DiscreteProblem(starting_state, tspan) jp = JumpProblem(prob, NSM(), majumps, hopping_constants = hopping_constants, spatial_system = grid, save_positions = (false, false), rng = rng) diff --git a/test/spatial/spatial_majump.jl b/test/spatial/spatial_majump.jl index 3327e515..12cae1d6 100644 --- a/test/spatial/spatial_majump.jl +++ b/test/spatial/spatial_majump.jl @@ -116,8 +116,8 @@ for spatial_jump_prob in uniform_jump_problems end # birth and death zero outside of center site -f(u, p, t) = L * u - diagm([0.0, 0.0, death_rate, 0.0, 0.0]) * u + [0.0, 0.0, 1.0, 0.0, 0.0] -ode_prob = ODEProblem(f, zeros(num_nodes), tspan) +f2(u, p, t) = L * u - diagm([0.0, 0.0, death_rate, 0.0, 0.0]) * u + [0.0, 0.0, 1.0, 0.0, 0.0] +ode_prob = ODEProblem(f2, zeros(num_nodes), tspan) sol = solve(ode_prob, Tsit5()) solution = solve(non_uniform_jump_problems[1], SSAStepper()) @@ -129,8 +129,8 @@ for (i, d) in enumerate(diff) end # birth everywhere, death only at center site -f(u, p, t) = L * u - diagm([0.0, 0.0, death_rate, 0.0, 0.0]) * u + ones(num_nodes) -ode_prob = ODEProblem(f, zeros(num_nodes), tspan) +f3(u, p, t) = L * u - diagm([0.0, 0.0, death_rate, 0.0, 0.0]) * u + ones(num_nodes) +ode_prob = ODEProblem(f3, zeros(num_nodes), tspan) sol = solve(ode_prob, Tsit5()) solution = solve(non_uniform_jump_problems[2], SSAStepper()) @@ -142,8 +142,8 @@ for (i, d) in enumerate(diff) end # birth on left end, death on right end -f(u, p, t) = L * u - diagm([0.0, 0.0, 0.0, 0.0, death_rate]) * u + [1.0, 0.0, 0.0, 0.0, 0.0] -ode_prob = ODEProblem(f, zeros(num_nodes), tspan) +f4(u, p, t) = L * u - diagm([0.0, 0.0, 0.0, 0.0, death_rate]) * u + [1.0, 0.0, 0.0, 0.0, 0.0] +ode_prob = ODEProblem(f4, zeros(num_nodes), tspan) sol = solve(ode_prob, Tsit5()) solution = solve(non_uniform_jump_problems[3], SSAStepper()) diff --git a/test/variable_rate.jl b/test/variable_rate.jl index dc0de037..3f2b3853 100644 --- a/test/variable_rate.jl +++ b/test/variable_rate.jl @@ -40,8 +40,8 @@ sol = solve(jump_prob, Rosenbrock23()) # @show sol[end] # display(sol[end]) -@test maximum([sol[i][2] for i in 1:length(sol)]) <= 1e-12 -@test maximum([sol[i][3] for i in 1:length(sol)]) <= 1e-12 +@test maximum([sol.u[i][2] for i in 1:length(sol)]) <= 1e-12 +@test maximum([sol.u[i][3] for i in 1:length(sol)]) <= 1e-12 g = function (du, u, p, t) du[1] = u[1] @@ -52,8 +52,8 @@ jump_prob = JumpProblem(prob, Direct(), jump, jump2; rng = rng) sol = solve(jump_prob, SRIW1()) -@test maximum([sol[i][2] for i in 1:length(sol)]) <= 1e-12 -@test maximum([sol[i][3] for i in 1:length(sol)]) <= 1e-12 +@test maximum([sol.u[i][2] for i in 1:length(sol)]) <= 1e-12 +@test maximum([sol.u[i][3] for i in 1:length(sol)]) <= 1e-12 function ff(du, u, p, t) if p == 0 @@ -95,16 +95,16 @@ jump = ConstantRateJump(rate2, affect2!) jump_prob = JumpProblem(prob, Direct(), jump; rng = rng) sol = solve(jump_prob, Tsit5()) sol(4.0) -sol[4] +sol.u[4] -rate2(u, p, t) = u[1] +rate2b(u, p, t) = u[1] affect2!(integrator) = (integrator.u[1] = integrator.u[1] / 2) -jump = VariableRateJump(rate2, affect2!) +jump = VariableRateJump(rate2b, affect2!) jump2 = deepcopy(jump) jump_prob = JumpProblem(prob, Direct(), jump, jump2; rng = rng) sol = solve(jump_prob, Tsit5()) sol(4.0) -sol[4] +sol.u[4] function g2(du, u, p, t) du[1] = u[1] @@ -114,7 +114,7 @@ prob = SDEProblem(f2, g2, [0.2], (0.0, 10.0)) jump_prob = JumpProblem(prob, Direct(), jump, jump2; rng = rng) sol = solve(jump_prob, SRIW1()) sol(4.0) -sol[4] +sol.u[4] function f3(du, u, p, t) du .= u @@ -151,7 +151,7 @@ function drift(x, p, t) return p * x end -function rate2(x, p, t) +function rate2c(x, p, t) return 3 * max(0.0, x[1]) end @@ -160,7 +160,7 @@ function affect!2(integrator) end x0 = rand(2) prob = ODEProblem(drift, x0, (0.0, 10.0), 2.0) -jump = VariableRateJump(rate2, affect!2) +jump = VariableRateJump(rate2c, affect!2) jump_prob = JumpProblem(prob, Direct(), jump) # test to check lack of dependency graphs is caught in Coevolve for systems with non-maj From 8fe7753659d955403c15a61749f437ce50a2445f Mon Sep 17 00:00:00 2001 From: Sam Isaacson Date: Tue, 26 Mar 2024 15:18:40 -0400 Subject: [PATCH 2/2] format --- test/spatial/spatial_majump.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/spatial/spatial_majump.jl b/test/spatial/spatial_majump.jl index 12cae1d6..6afcd367 100644 --- a/test/spatial/spatial_majump.jl +++ b/test/spatial/spatial_majump.jl @@ -116,7 +116,9 @@ for spatial_jump_prob in uniform_jump_problems end # birth and death zero outside of center site -f2(u, p, t) = L * u - diagm([0.0, 0.0, death_rate, 0.0, 0.0]) * u + [0.0, 0.0, 1.0, 0.0, 0.0] +function f2(u, p, t) + L * u - diagm([0.0, 0.0, death_rate, 0.0, 0.0]) * u + [0.0, 0.0, 1.0, 0.0, 0.0] +end ode_prob = ODEProblem(f2, zeros(num_nodes), tspan) sol = solve(ode_prob, Tsit5()) @@ -142,7 +144,9 @@ for (i, d) in enumerate(diff) end # birth on left end, death on right end -f4(u, p, t) = L * u - diagm([0.0, 0.0, 0.0, 0.0, death_rate]) * u + [1.0, 0.0, 0.0, 0.0, 0.0] +function f4(u, p, t) + L * u - diagm([0.0, 0.0, 0.0, 0.0, death_rate]) * u + [1.0, 0.0, 0.0, 0.0, 0.0] +end ode_prob = ODEProblem(f4, zeros(num_nodes), tspan) sol = solve(ode_prob, Tsit5())