@@ -336,3 +336,45 @@ one must still use a vector
336
336
``` julia
337
337
discrete_events = [[2.0 ] => [v ~ - v]]
338
338
```
339
+
340
+ ## Saving discrete values
341
+
342
+ Time-dependent parameters which are updated in callbacks are termed as discrete variables.
343
+ ModelingToolkit enables automatically saving the timeseries of these discrete variables,
344
+ and indexing the solution object to obtain the saved timeseries. Consider the following
345
+ example:
346
+
347
+ ``` @example events
348
+ @variables x(t)
349
+ @parameters c(t)
350
+
351
+ @mtkbuild sys = ODESystem(
352
+ D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [1.0 => [c ~ c + 1]])
353
+
354
+ prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0])
355
+ sol = solve(prob, Tsit5())
356
+ sol[c]
357
+ ```
358
+
359
+ The solution object can also be interpolated with the discrete variables
360
+
361
+ ``` @example events
362
+ sol([1.0, 2.0], idxs = [c, c * cos(x)])
363
+ ```
364
+
365
+ Note that only time-dependent parameters will be saved. If we repeat the above example with
366
+ this change:
367
+
368
+ ``` @example events
369
+ @variables x(t)
370
+ @parameters c
371
+
372
+ @mtkbuild sys = ODESystem(
373
+ D(x) ~ c * cos(x), t, [x], [c]; discrete_events = [1.0 => [c ~ c + 1]])
374
+
375
+ prob = ODEProblem(sys, [x => 0.0], (0.0, 2pi), [c => 1.0])
376
+ sol = solve(prob, Tsit5())
377
+ sol.ps[c] # sol[c] will error, since `c` is not a timeseries value
378
+ ```
379
+
380
+ It can be seen that the timeseries for ` c ` is not saved.
0 commit comments