You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/tutorials/input_component.md
+29-18Lines changed: 29 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,25 +2,29 @@
2
2
3
3
There are 3 ways to include data as part of a model.
4
4
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)
8
8
9
9
This tutorial demonstrate each case and explain the pros and cons of each.
10
10
11
-
## `TimeVaryingFunction` Component
11
+
## `ParametrizedInterpolation` Component
12
12
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
14
17
15
-
```julia
18
+
```@example parametrized_interpolation
16
19
using ModelingToolkit
17
20
using ModelingToolkit: t_nounits as t, D_nounits as D
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)
46
56
```
47
57
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.
49
60
50
61
## Custom Component with External Data
51
62
52
63
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.
53
64
54
-
```julia
65
+
```@example custom_component_external_data
55
66
const rdata = Ref{Vector{Float64}}()
56
67
57
68
# Data Sets
@@ -105,9 +116,9 @@ Additional code could be added to resolve this issue, for example by using a `Re
105
116
106
117
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()`.
0 commit comments