|
1 | 1 | using ModelingToolkit, Test, DifferentialEquations
|
2 | 2 |
|
3 |
| -@parameters t a b |
| 3 | +@parameters t |
4 | 4 | @variables u(t)
|
5 | 5 | D = Differential(t)
|
6 | 6 |
|
@@ -29,5 +29,40 @@ i8 = findfirst(==(8.0), sol[:t])
|
29 | 29 | @test sol.u[i8+1][1] > 20.0
|
30 | 30 | @test ctx1[1] == 40.0
|
31 | 31 |
|
| 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 | + |
32 | 67 |
|
33 | 68 |
|
0 commit comments