Skip to content

Commit fe6bc00

Browse files
Merge pull request #388 from SebastianM-C/interp
Interpolation with the model macro
2 parents 6244cda + 3108d0c commit fe6bc00

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

docs/src/tutorials/input_component.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ using Plots
3535
3636
function MassSpringDamper(; name)
3737
@named input = RealInput()
38-
@variables f(t)=0 x(t)=0 dx(t)=0 ddx(t)=0
38+
@variables f(t) x(t)=0 dx(t)=0 ddx(t)
3939
@parameters m=10 k=1000 d=1
4040
4141
eqs = [f ~ input.u
@@ -74,6 +74,35 @@ sol = solve(prob)
7474
plot(sol)
7575
```
7676

77+
Note that in the case of the `Interpolation` block, the `data` and the `time` act like
78+
structural parameters.
79+
80+
As such, we can also build the interpolation object outside of the model
81+
82+
```@example interpolation_block
83+
my_interpolation = LinearInterpolation(df.data, df.time)
84+
85+
@mtkmodel MassSpringDamperSystem2 begin
86+
@components begin
87+
src = Interpolation(itp=my_interpolation)
88+
clk = ContinuousClock()
89+
model = MassSpringDamper()
90+
end
91+
@equations begin
92+
connect(src.input, clk.output)
93+
connect(src.output, model.input)
94+
end
95+
end;
96+
@mtkbuild sys = MassSpringDamperSystem2()
97+
98+
prob = ODEProblem(sys, [], (0, df.time[end]))
99+
sol = solve(prob, Tsit5())
100+
plot(sol)
101+
```
102+
103+
Note that the interpolation is constructed outside of the model, so we cannot use `remake` to change the
104+
data. For that usecase, see the `ParametrizedInterpolation`.
105+
77106
## `ParametrizedInterpolation` Block
78107

79108
The `ModelingToolkitStandardLibrary.Blocks.ParametrizedInterpolation` component is similar to `Interpolation`, 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.
@@ -145,7 +174,7 @@ plot(sol2)
145174
```
146175

147176
!!! note
148-
177+
149178
Note that when changing the data, the length of the new data must be the same as the length of the original data.
150179

151180
## Custom Component with External Data

src/Blocks/sources.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,10 +755,12 @@ such as `LinearInterpolation`, `ConstantInterpolation` or `CubicSpline`.
755755
"""
756756
function Interpolation(interp_type, u, x, args...; name)
757757
itp = interp_type(u, x, args...)
758-
Interpolation(itp; name)
758+
Interpolation(; itp, name)
759759
end
760760

761-
function Interpolation(itp; name)
761+
@deprecate Interpolation(itp; name) Interpolation(; itp, name)
762+
763+
function Interpolation(; itp, name)
762764
@parameters (interpolator::typeof(itp))(..) = itp
763765
@named input = RealInput()
764766
@named output = RealOutput()
@@ -868,3 +870,7 @@ function ParametrizedInterpolation(
868870
systems = [input, output],
869871
name)
870872
end
873+
874+
function ParametrizedInterpolation(; interp_type, u::AbstractVector, x::AbstractVector, name)
875+
ParametrizedInterpolation(interp_type, u, x; name)
876+
end

test/Blocks/sources.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,44 @@ end
500500
@test SciMLBase.successful_retcode(sol)
501501
end
502502

503+
@testset "Interpolation in model macro" begin
504+
505+
function MassSpringDamper(; name)
506+
@named input = RealInput()
507+
@variables f(t) x(t)=0 dx(t)=0 ddx(t)
508+
@parameters m=10 k=1000 d=1
509+
510+
eqs = [f ~ input.u
511+
ddx * 10 ~ k * x + d * dx + f
512+
D(x) ~ dx
513+
D(dx) ~ ddx]
514+
515+
ODESystem(eqs, t; name, systems = [input])
516+
end
517+
518+
table_data = [1.0, 2.0, 3.0]
519+
table_bkp = [0.0, 0.5, 1.0]
520+
itp = LinearInterpolation(table_data, table_bkp)
521+
522+
@mtkmodel model_with_lut begin
523+
@components begin
524+
src = Interpolation(itp)
525+
clk = ContinuousClock()
526+
model = MassSpringDamper()
527+
end
528+
@equations begin
529+
connect(src.input, clk.output)
530+
connect(src.output, model.input)
531+
end
532+
end;
533+
@mtkbuild sys = model_with_lut()
534+
535+
prob = ODEProblem(sys, [], (0.0, 1))
536+
sol = solve(prob, Tsit5())
537+
538+
@test SciMLBase.successful_retcode(sol)
539+
end
540+
503541
@testset "ParametrizedInterpolation" begin
504542
@variables y(t) = 0
505543
u = rand(15)

0 commit comments

Comments
 (0)