@@ -104,7 +104,7 @@ function affect6!(integ, u,p,ctx)
104
104
integ. p[p. C] *= 200
105
105
end
106
106
107
- function Capacitor (; name, C = 1.0 )
107
+ function Capacitor2 (; name, C = 1.0 )
108
108
@named oneport = OnePort ()
109
109
@unpack v, i = oneport
110
110
ps = @parameters C = C
@@ -115,9 +115,9 @@ function Capacitor(; name, C = 1.0)
115
115
extend (ODESystem (eqs, t, [], ps; name = name, continuous_events= [[v ~ 0.3 ]=> (affect6!, [v], [C], nothing )]), oneport)
116
116
end
117
117
118
- # hyerarchical - result should be identical
118
+ # hierarchical - result should be identical
119
119
120
- @named capacitor2 = Capacitor (C = C)
120
+ @named capacitor2 = Capacitor2 (C = C)
121
121
122
122
rc_eqs2 = [connect (source. p, resistor. p)
123
123
connect (resistor. n, capacitor2. p)
@@ -135,3 +135,52 @@ u0 = [capacitor2.v => 0.0
135
135
prob2 = ODEProblem (sys2, u0, (0 , 10.0 ))
136
136
sol2 = solve (prob2, Rodas4 ())
137
137
@test all (sol2[rc_model2. capacitor2. v] .== sol[rc_model. capacitor. v])
138
+
139
+
140
+ # bouncing ball
141
+
142
+ # DiffEq implementation
143
+
144
+ function f_ (du,u,p,t)
145
+ du[1 ] = u[2 ]
146
+ du[2 ] = - p
147
+ end
148
+
149
+ function condition_ (u,t,integrator) # Event when event_f(u,t) == 0
150
+ u[1 ]
151
+ end
152
+
153
+ function affect_! (integrator)
154
+ integrator. u[2 ] = - integrator. u[2 ]
155
+ end
156
+
157
+ cb_ = ContinuousCallback (condition_,affect_!)
158
+
159
+ u0 = [50.0 ,0.0 ]
160
+ tspan = (0.0 ,15.0 )
161
+ p = 9.8
162
+ prob_ = ODEProblem (f_,u0,tspan,p)
163
+ sol_ = solve (prob_,Tsit5 (),callback= cb_)
164
+
165
+ # same - with MTK
166
+ sts = @variables y (t), v (t)
167
+ par = @parameters g
168
+ bb_eqs = [
169
+ D (y) ~ v
170
+ D (v) ~ - g
171
+ ]
172
+
173
+ function bb_affect! (integ, u, p, ctx)
174
+ integ. u[u. v] = - integ. u[u. v]
175
+ end
176
+
177
+ @named bb_model = ODESystem (bb_eqs, t, sts, par, continuous_events= [[y ~ 0 ] => (bb_affect!, [v], [], nothing )])
178
+
179
+ bb_sys = structural_simplify (bb_model)
180
+ u0 = [v => 0.0 , y => 50.0 , g=> 9.8 ]
181
+
182
+ bb_prob = ODEProblem (bb_sys, u0, (0 , 15.0 ))
183
+ bb_sol = solve (bb_prob, Tsit5 ())
184
+
185
+ @test bb_sol[y] ≈ map (u -> u[1 ], sol_. u)
186
+ @test bb_sol[v] ≈ map (u -> u[2 ], sol_. u)
0 commit comments