|
1 | 1 | # Running Models with Discrete Data |
2 | 2 |
|
3 | | -There are 3 ways to include data as part of a model. |
| 3 | +There are 4 ways to include data as part of a model. |
4 | 4 |
|
5 | | - 1. using `ModelingToolkitStandardLibrary.Blocks.ParametrizedInterpolation` & `DataInterpolations` |
6 | | - 2. using a custom component with external data (not recommended) |
7 | | - 3. using `ModelingToolkitStandardLibrary.Blocks.SampledData` (legacy) |
| 5 | + 1. using `ModelingToolkitStandardLibrary.Blocks.InterpolationBlock` |
| 6 | + 2. using `ModelingToolkitStandardLibrary.Blocks.ParametrizedInterpolationBlock` |
| 7 | + 3. using a custom component with external data (not recommended) |
| 8 | + 4. using `ModelingToolkitStandardLibrary.Blocks.SampledData` (legacy) |
8 | 9 |
|
9 | 10 | This tutorial demonstrate each case and explain the pros and cons of each. |
10 | 11 |
|
11 | | -## `ParametrizedInterpolation` Component |
| 12 | +## `InterpolationBlock` Component |
12 | 13 |
|
13 | | -The `ModelingToolkitStandardLibrary.Blocks.ParametrizedInterpolation` component is easy to use and is performant. |
14 | | -It allows one to change the underlying data without rebuilding the model as the data is represented via vector parameters. |
15 | | -The `ParametrizedInterpolation` is compatible with interpolation types from `DataInterpolation`. |
| 14 | +The `ModelingToolkitStandardLibrary.Blocks.InterpolationBlock` component is easy to use and is performant. |
| 15 | +It is simlar to using callable paramterers, but it provides a block interface and a `RealOutput` connector. |
| 16 | +The `InterpolationBlock` is compatible with interpolation types from `DataInterpolation`. |
| 17 | +Here is an example on how to use it |
| 18 | + |
| 19 | +```@example interpolation_block |
| 20 | +using ModelingToolkit |
| 21 | +using ModelingToolkit: t_nounits as t, D_nounits as D |
| 22 | +using ModelingToolkitStandardLibrary.Blocks |
| 23 | +using DataInterpolations |
| 24 | +using OrdinaryDiffEq |
| 25 | +using Plots |
| 26 | +
|
| 27 | +function System(data, time; name) |
| 28 | + @named src = InterpolationBlock(LinearInterpolation, data, time) |
| 29 | +
|
| 30 | + vars = @variables f(t)=0 x(t)=0 dx(t)=0 ddx(t)=0 |
| 31 | + pars = @parameters m=10 k=1000 d=1 |
| 32 | +
|
| 33 | + eqs = [f ~ src.output.u |
| 34 | + ddx * 10 ~ k * x + d * dx + f |
| 35 | + D(x) ~ dx |
| 36 | + D(dx) ~ ddx] |
| 37 | +
|
| 38 | + ODESystem(eqs, t, vars, pars; systems = [src], name) |
| 39 | +end |
| 40 | +
|
| 41 | +dt = 4e-4 |
| 42 | +time = 0:dt:0.1 |
| 43 | +data = sin.(2 * pi * time * 100) # example data |
| 44 | +
|
| 45 | +@named system = System(data, time) |
| 46 | +sys = structural_simplify(system) |
| 47 | +prob = ODEProblem(sys, [], (0, time[end])) |
| 48 | +sol = solve(prob) |
| 49 | +plot(sol) |
| 50 | +``` |
| 51 | + |
| 52 | +## `ParametrizedInterpolationBlock` Component |
| 53 | + |
| 54 | +The `ModelingToolkitStandardLibrary.Blocks.ParametrizedInterpolationBlock` component is similar to `InterpolationBlock`, but as the name suggests, it is parametrized by the data, allowing one to change the underlying data without rebuilding the model as the data is represented via vector parameters. |
| 55 | +The `ParametrizedInterpolationBlock` is compatible with interpolation types from `DataInterpolation`. |
16 | 56 | Here is an example on how to use it |
17 | 57 |
|
18 | 58 | ```@example parametrized_interpolation |
|
0 commit comments