Skip to content

Commit 69ab932

Browse files
committed
adding event support and docs, test rebase
1 parent e40cd68 commit 69ab932

File tree

3 files changed

+96
-5
lines changed

3 files changed

+96
-5
lines changed

docs/src/basics/MTKModel_Connector.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ equations.
3232
- `@parameters`: for specifying the symbolic parameters
3333
- `@structural_parameters`: for specifying non-symbolic parameters
3434
- `@variables`: for specifying the unknowns
35+
- `@continuous_events`: for specifying a list of continuous events
36+
- `@discrete_events`: for specifying a list of discrete events
3537

3638
Let's explore these in more detail with the following example:
3739

@@ -177,11 +179,65 @@ getdefault(model_c3.model_a.k_array[2])
177179

178180
- List all the equations here
179181

182+
180183
#### `@defaults` begin block
181184

182185
- Default values can be passed as pairs.
183186
- This is equivalent to passing `defaults` argument to `ODESystem`.
184187

188+
#### `@continuous_events` begin block
189+
190+
- Defining continuous events as described [here](https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/#Continuous-Events).
191+
- If this block is not defined in the model, no continuous events will be added.
192+
193+
```@example mtkmodel-example
194+
using ModelingToolkit
195+
196+
@mtkmodel M begin
197+
@parameters begin
198+
k
199+
end
200+
@variables begin
201+
x(t)
202+
y(t)
203+
end
204+
@equations begin
205+
x ~ k * D(x)
206+
D(y) ~ -k
207+
end
208+
@continuous_events begin
209+
[x ~ 1.5] => [x ~ 5, y ~ 5]
210+
[t ~ 4] => [x ~ 10]
211+
end
212+
end
213+
```
214+
215+
#### `@discrete_events` begin block
216+
217+
- Defining discrete events as described [here](https://docs.sciml.ai/ModelingToolkit/stable/basics/Events/#Discrete-events-support).
218+
- If this block is not defined in the model, no discrete events will be added.
219+
220+
```@example mtkmodel-example
221+
using ModelingToolkit
222+
223+
@mtkmodel M begin
224+
@parameters begin
225+
k
226+
end
227+
@variables begin
228+
x(t)
229+
y(t)
230+
end
231+
@equations begin
232+
x ~ k * D(x)
233+
D(y) ~ -k
234+
end
235+
@discrete_events begin
236+
(t == 1.5) => [x ~ x + 5, y ~ 5]
237+
end
238+
end
239+
```
240+
185241
#### A begin block
186242

187243
- Any other Julia operations can be included with dedicated begin blocks.
@@ -380,4 +436,4 @@ Using ternary operator or if-elseif-else statement, conditional initial guesses
380436
p = flag ? 1 : 2
381437
end
382438
end
383-
```
439+
```

src/systems/model_parsing.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,15 @@ function _model_macro(mod, name, expr, isconnector)
118118
isconnector && push!(exprs.args,
119119
:($Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___"))))
120120

121-
!(c_evts==[]) && push!(exprs.args,
122-
:($Setfield.@set!(var"#___sys___".continuous_events=$SymbolicContinuousCallback.([$(c_evts...)]))))
121+
!(c_evts == []) && push!(exprs.args,
122+
:($Setfield.@set!(var"#___sys___".continuous_events=$SymbolicContinuousCallback.([
123+
$(c_evts...),
124+
]))))
123125

124-
!(d_evts==[]) && push!(exprs.args,
125-
:($Setfield.@set!(var"#___sys___".discrete_events=$SymbolicDiscreteCallback.([$(d_evts...)]))))
126+
!(d_evts == []) && push!(exprs.args,
127+
:($Setfield.@set!(var"#___sys___".discrete_events=$SymbolicDiscreteCallback.([
128+
$(d_evts...),
129+
]))))
126130

127131
f = if length(where_types) == 0
128132
:($(Symbol(:__, name, :__))(; name, $(kwargs...)) = $exprs)
@@ -779,6 +783,7 @@ function parse_discrete_events!(d_evts, dict, body)
779783
dict[:discrete_events] = []
780784
Base.remove_linenums!(body)
781785
for arg in body.args
786+
push!(d_evts, arg)
782787
push!(dict[:discrete_events], readable_code.(d_evts)...)
783788
end
784789
end

test/model_parsing.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,36 @@ end
426426
@test A.structure[:components] == [[:cc, :C]]
427427
end
428428

429+
@testset "Event handeling in MTKModel" begin
430+
@mtkmodel M begin
431+
@variables begin
432+
x(t)
433+
y(t)
434+
z(t)
435+
end
436+
@equations begin
437+
x ~ -D(x)
438+
D(y) ~ 0
439+
D(z) ~ 0
440+
end
441+
@continuous_events begin
442+
[x ~ 1.5] => [x ~ 5, y ~ 1]
443+
end
444+
@discrete_events begin
445+
(t == 1.5) => [x ~ x + 5, z ~ 2]
446+
end
447+
end
448+
449+
@mtkbuild model = M()
450+
u0 = [model.x => 10, model.y => 0, model.z => 0]
451+
452+
prob = ODEProblem(model, u0, (0, 5.0))
453+
sol = solve(prob, tstops = [1.5])
454+
455+
@test isequal(sol[model.y][end], 1.0)
456+
@test isequal(sol[model.z][end], 2.0)
457+
end
458+
429459
# Ensure that modules consisting MTKModels with component arrays and icons of
430460
# `Expr` type and `unit` metadata can be precompiled.
431461
module PrecompilationTest

0 commit comments

Comments
 (0)