Skip to content

Commit 6f8ee7e

Browse files
author
dd
committed
added tests
1 parent 5fa998e commit 6f8ee7e

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

test/funcaffect.jl

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using ModelingToolkit, Test, DifferentialEquations
22

3-
@parameters t a b
3+
@parameters t
44
@variables u(t)
55
D = Differential(t)
66

@@ -29,5 +29,40 @@ i8 = findfirst(==(8.0), sol[:t])
2929
@test sol.u[i8+1][1] > 20.0
3030
@test ctx1[1] == 40.0
3131

32+
# parameter
33+
function affect3!(integ, ctx; u, a)
34+
integ.u[u] += integ.p[a]
35+
integ.p[a] *= 2
36+
end
37+
38+
@parameters a = 10.0
39+
@named sys = ODESystem(eqs, t, [u], [a], discrete_events=[[4.0, 8.0]=>(affect3!, [u], [a], nothing)])
40+
prob = ODEProblem(sys, [u=> 10.0], (0, 10.0))
41+
42+
sol = solve(prob, Tsit5())
43+
i4 = findfirst(==(4.0), sol[:t])
44+
@test sol.u[i4+1][1] > 10.0
45+
i8 = findfirst(==(8.0), sol[:t])
46+
@test sol.u[i8+1][1] > 20.0
47+
48+
# rename parameter
49+
function affect3!(integ, ctx; u, b)
50+
integ.u[u] += integ.p[b]
51+
integ.p[b] *= 2
52+
end
53+
54+
@named sys = ODESystem(eqs, t, [u], [a], discrete_events=[[4.0, 8.0]=>(affect3!, [u], [a=> :b], nothing)])
55+
prob = ODEProblem(sys, [u=> 10.0], (0, 10.0))
56+
57+
sol = solve(prob, Tsit5())
58+
i4 = findfirst(==(4.0), sol[:t])
59+
@test sol.u[i4+1][1] > 10.0
60+
i8 = findfirst(==(8.0), sol[:t])
61+
@test sol.u[i8+1][1] > 20.0
62+
63+
# same name
64+
@test_throws ErrorException ODESystem(eqs, t, [u], [a], discrete_events=[[4.0, 8.0]=>(affect3!, [u], [a=> :u], nothing)]; name=:sys)
65+
66+
3267

3368

0 commit comments

Comments
 (0)