Skip to content

Commit 42c1e0f

Browse files
committed
adding event support and docs, test
1 parent fbcb985 commit 42c1e0f

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

docs/src/basics/MTKModel_Connector.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ equations.
3030
- `@parameters`: for specifying the symbolic parameters
3131
- `@structural_parameters`: for specifying non-symbolic parameters
3232
- `@variables`: for specifying the states
33+
- `@continuous_events`: for specifying a list of continuous events
34+
- `@discrete_events`: for specifying a list of discrete events
3335

3436
Let's explore these in more detail with the following example:
3537

@@ -162,6 +164,59 @@ getdefault(model_c4.model_a.k_array[2])
162164

163165
- List all the equations here
164166

167+
#### `@continuous_events` begin block
168+
169+
- Defining continuous events as described [here](https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/#Continuous-Events).
170+
- If this block is not defined in the model, no continuous events will be added.
171+
172+
```@example mtkmodel-example
173+
using ModelingToolkit
174+
175+
@mtkmodel M begin
176+
@parameters begin
177+
k
178+
end
179+
@variables begin
180+
x(t)
181+
y(t)
182+
end
183+
@equations begin
184+
x ~ k * D(x)
185+
D(y) ~ -k
186+
end
187+
@continuous_events begin
188+
[x ~ 1.5] => [x ~ 5, y ~ 5]
189+
[t ~ 4] => [x ~ 10]
190+
end
191+
end
192+
```
193+
194+
#### `@discrete_events` begin block
195+
196+
- Defining discrete events as described [here](https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/#Discrete-events-support).
197+
- If this block is not defined in the model, no discrete events will be added.
198+
199+
```@example mtkmodel-example
200+
using ModelingToolkit
201+
202+
@mtkmodel M begin
203+
@parameters begin
204+
k
205+
end
206+
@variables begin
207+
x(t)
208+
y(t)
209+
end
210+
@equations begin
211+
x ~ k * D(x)
212+
D(y) ~ -k
213+
end
214+
@discrete_events begin
215+
(t == 1.5) => [x ~ x + 5, y ~ 5]
216+
end
217+
end
218+
```
219+
165220
#### A begin block
166221

167222
- Any other Julia operations can be included with dedicated begin blocks.
@@ -351,4 +406,4 @@ Using ternary operator or if-elseif-else statement, conditional initial guesses
351406
p = flag ? 1 : 2
352407
end
353408
end
354-
```
409+
```

src/systems/model_parsing.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ function _model_macro(mod, name, expr, isconnector)
9292
gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) :
9393
GUIMetadata(GlobalRef(mod, name))
9494

95-
9695
sys = :($ODESystem($Equation[equations...], $iv, variables, parameters;
9796
name, systems, gui_metadata = $gui_metadata))
9897

@@ -105,12 +104,15 @@ function _model_macro(mod, name, expr, isconnector)
105104
isconnector && push!(exprs.args,
106105
:($Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___"))))
107106

108-
!(c_evts==[]) && push!(exprs.args,
109-
:($Setfield.@set!(var"#___sys___".continuous_events=$SymbolicContinuousCallback.([$(c_evts...)]))))
110-
111-
!(d_evts==[]) && push!(exprs.args,
112-
:($Setfield.@set!(var"#___sys___".discrete_events=$SymbolicDiscreteCallback.([$(d_evts...)]))))
107+
!(c_evts == []) && push!(exprs.args,
108+
:($Setfield.@set!(var"#___sys___".continuous_events=$SymbolicContinuousCallback.([
109+
$(c_evts...),
110+
]))))
113111

112+
!(d_evts == []) && push!(exprs.args,
113+
:($Setfield.@set!(var"#___sys___".discrete_events=$SymbolicDiscreteCallback.([
114+
$(d_evts...),
115+
]))))
114116

115117
f = :($(Symbol(:__, name, :__))(; name, $(kwargs...)) = $exprs)
116118
:($name = $Model($f, $dict, $isconnector))
@@ -636,6 +638,7 @@ function parse_discrete_events!(d_evts, dict, body)
636638
dict[:discrete_events] = []
637639
Base.remove_linenums!(body)
638640
for arg in body.args
641+
push!(d_evts, arg)
639642
push!(dict[:discrete_events], readable_code.(d_evts)...)
640643
end
641644
end

test/model_parsing.jl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,36 @@ end
326326
@test A.structure[:components] == [[:cc, :C]]
327327
end
328328

329+
@testset "Event handeling in MTKModel" begin
330+
@mtkmodel M begin
331+
@variables begin
332+
x(t)
333+
y(t)
334+
z(t)
335+
end
336+
@equations begin
337+
x ~ -D(x)
338+
D(y) ~ 0
339+
D(z) ~ 0
340+
end
341+
@continuous_events begin
342+
[x ~ 1.5] => [x ~ 5, y ~ 1]
343+
end
344+
@discrete_events begin
345+
(t == 1.5) => [x ~ x + 5, z ~ 2]
346+
end
347+
end
348+
349+
@mtkbuild model = M()
350+
u0 = [model.x => 10, model.y => 0, model.z => 0]
351+
352+
prob = ODEProblem(model, u0, (0, 5.0))
353+
sol = solve(prob, tstops = [1.5])
354+
355+
@test isequal(sol[model.y][end], 1.0)
356+
@test isequal(sol[model.z][end], 2.0)
357+
end
358+
329359
# Ensure that modules consisting MTKModels with component arrays and icons of
330360
# `Expr` type and `unit` metadata can be precompiled.
331361
module PrecompilationTest
@@ -538,4 +568,4 @@ end
538568

539569
@test Equation[ternary_true.ternary_parameter_true ~ 0] == equations(ternary_true)
540570
@test Equation[ternary_false.ternary_parameter_false ~ 0] == equations(ternary_false)
541-
end
571+
end

0 commit comments

Comments
 (0)