@@ -6,7 +6,7 @@ D = Differential(t)
6
6
7
7
eqs = [ D (u) ~ - u ]
8
8
9
- affect1! (integ, ctx; u ) = integ. u[u] += 10
9
+ affect1! (integ, u, p, ctx ) = integ. u[u . u] += 10
10
10
11
11
@named sys = ODESystem (eqs, t, [u], [], discrete_events= [[4.0 , ]=> (affect1!, [u], [], nothing )])
12
12
prob = ODEProblem (sys, [u=> 10.0 ], (0 , 10.0 ))
@@ -15,8 +15,8 @@ i4 = findfirst(==(4.0), sol[:t])
15
15
@test sol. u[i4+ 1 ][1 ] > 10.0
16
16
17
17
# context
18
- function affect2! (integ, ctx; u )
19
- integ. u[u] += ctx[1 ]
18
+ function affect2! (integ, u, p, ctx )
19
+ integ. u[u. u ] += ctx[1 ]
20
20
ctx[1 ] *= 2
21
21
end
22
22
ctx1 = [10.0 , ]
@@ -30,9 +30,9 @@ i8 = findfirst(==(8.0), sol[:t])
30
30
@test ctx1[1 ] == 40.0
31
31
32
32
# parameter
33
- function affect3! (integ, ctx; u, a )
34
- integ. u[u] += integ. p[a]
35
- integ. p[a] *= 2
33
+ function affect3! (integ, u, p, ctx )
34
+ integ. u[u. u ] += integ. p[p . a]
35
+ integ. p[p . a] *= 2
36
36
end
37
37
38
38
@parameters a = 10.0
@@ -46,9 +46,9 @@ i8 = findfirst(==(8.0), sol[:t])
46
46
@test sol. u[i8+ 1 ][1 ] > 20.0
47
47
48
48
# rename parameter
49
- function affect3! (integ, ctx; u, b )
50
- integ. u[u] += integ. p[b]
51
- integ. p[b] *= 2
49
+ function affect3! (integ, u, p, ctx )
50
+ integ. u[u. u ] += integ. p[p . b]
51
+ integ. p[p . b] *= 2
52
52
end
53
53
54
54
@named sys = ODESystem (eqs, t, [u], [a], discrete_events= [[4.0 , 8.0 ]=> (affect3!, [u], [a=> :b ], nothing )])
@@ -61,8 +61,21 @@ i8 = findfirst(==(8.0), sol[:t])
61
61
@test sol. u[i8+ 1 ][1 ] > 20.0
62
62
63
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 )
64
+ @variables v (t)
65
+ @test_throws ErrorException ODESystem (eqs, t, [u], [a], discrete_events= [[4.0 , 8.0 ]=> (affect3!, [u, v => :u ], [a], nothing )]; name= :sys )
65
66
67
+ @named resistor = ODESystem (D (v) ~ v, t, [v], [])
66
68
69
+ # nested namespace
70
+ ctx = [0 ]
71
+ function affect4! (integ, u, p, ctx)
72
+ ctx[1 ] += 1
73
+ @test u. resistor₊v == 1
74
+ end
75
+ s1 = compose (ODESystem (Equation[], t, [], [], name= :s1 , discrete_events= 1.0 => (affect4!, [resistor. v], [], ctx)), resistor)
76
+ s2 = structural_simplify (s1)
77
+ prob = ODEProblem (s2, [resistor. v=> 10.0 ], (0 , 2.01 ))
78
+ sol = solve (prob, Tsit5 ())
79
+ @test ctx[1 ] == 2
67
80
68
81
0 commit comments