Skip to content

Commit 1fdfc71

Browse files
committed
docs: update docs to ParametrizedInterpolation
1 parent b90daac commit 1fdfc71

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

docs/src/tutorials/input_component.md

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

33
There are 3 ways to include data as part of a model.
44

5-
1. using `ModelingToolkitStandardLibrary.Blocks.TimeVaryingFunction`
6-
2. using a custom component with external data
7-
3. using `ModelingToolkitStandardLibrary.Blocks.SampledData`
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)
88

99
This tutorial demonstrate each case and explain the pros and cons of each.
1010

11-
## `TimeVaryingFunction` Component
11+
## `ParametrizedInterpolation` Component
1212

13-
The `ModelingToolkitStandardLibrary.Blocks.TimeVaryingFunction` component is easy to use and is performant. However the data is locked to the `ODESystem` and can only be changed by building a new `ODESystem`. Therefore, running a batch of data would not be efficient. Below is an example of how to use the `TimeVaryingFunction` with `DataInterpolations` to build the function from sampled discrete data.
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`.
16+
Here is an example on how to use it
1417

15-
```julia
18+
```@example parametrized_interpolation
1619
using ModelingToolkit
1720
using ModelingToolkit: t_nounits as t, D_nounits as D
1821
using ModelingToolkitStandardLibrary.Blocks
1922
using DataInterpolations
2023
using OrdinaryDiffEq
24+
using Plots
2125
22-
function System(f; name)
23-
src = TimeVaryingFunction(f)
26+
function System(data, time; name)
27+
@named src = ParametrizedInterpolation(LinearInterpolation, data, time)
2428
2529
vars = @variables f(t)=0 x(t)=0 dx(t)=0 ddx(t)=0
2630
pars = @parameters m=10 k=1000 d=1
@@ -37,21 +41,28 @@ dt = 4e-4
3741
time = 0:dt:0.1
3842
data = sin.(2 * pi * time * 100) # example data
3943
40-
f = LinearInterpolation(data, time)
41-
42-
@named system = System(f)
44+
@named system = System(data, time)
4345
sys = structural_simplify(system)
4446
prob = ODEProblem(sys, [], (0, time[end]))
45-
sol = solve(prob, ImplicitEuler())
47+
sol = solve(prob)
48+
plot(sol)
49+
```
50+
51+
If we want to run a new data set, this requires only remaking the problem and solving again
52+
```@example parametrized_interpolation
53+
prob2 = remake(prob, p = [sys.src.data => ones(length(data))])
54+
sol2 = solve(prob2)
55+
plot(sol2)
4656
```
4757

48-
If we want to run a new data set, this requires building a new `LinearInterpolation` and `ODESystem` followed by running `structural_simplify`, all of which takes time. Therefore, to run several pieces of data it's better to re-use an `ODESystem`. The next couple methods will demonstrate how to do this.
58+
!!! note
59+
Note that when changing the data, the length of the new data must be the same as the lenght of the original data.
4960

5061
## Custom Component with External Data
5162

5263
The below code shows how to include data using a `Ref` and registered `get_sampled_data` function. This example uses a very basic function which requires non-adaptive solving and sampled data. As can be seen, the data can easily be set and changed before solving.
5364

54-
```julia
65+
```@example custom_component_external_data
5566
const rdata = Ref{Vector{Float64}}()
5667
5768
# Data Sets
@@ -105,9 +116,9 @@ Additional code could be added to resolve this issue, for example by using a `Re
105116

106117
To resolve the issues presented above, the `ModelingToolkitStandardLibrary.Blocks.SampledData` component can be used which allows for a resusable `ODESystem` and self contained data which ensures a solution which remains valid for it's lifetime. Now it's possible to also parallelize the call to `solve()`.
107118

108-
```julia
119+
```@example sampled_data_component
109120
function System(; name)
110-
src = SampledData(Float64)
121+
@named src = SampledData(Float64)
111122
112123
vars = @variables f(t)=0 x(t)=0 dx(t)=0 ddx(t)=0
113124
pars = @parameters m=10 k=1000 d=1
@@ -121,9 +132,9 @@ function System(; name)
121132
end
122133
123134
@named system = System()
124-
sys = structural_simplify(system)
135+
sys = structural_simplify(system, split=false)
125136
s = complete(system)
126-
prob = ODEProblem(sys, [], (0, time[end]); tofloat = false)
137+
prob = ODEProblem(sys, [], (0, time[end]); tofloat = false, use_union=true)
127138
defs = ModelingToolkit.defaults(sys)
128139
129140
function get_prob(data)

0 commit comments

Comments
 (0)