Skip to content

Interpolation with the model macro #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions docs/src/tutorials/input_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using Plots

function MassSpringDamper(; name)
@named input = RealInput()
@variables f(t)=0 x(t)=0 dx(t)=0 ddx(t)=0
@variables f(t) x(t)=0 dx(t)=0 ddx(t)
@parameters m=10 k=1000 d=1

eqs = [f ~ input.u
Expand Down Expand Up @@ -74,6 +74,35 @@ sol = solve(prob)
plot(sol)
```

Note that in the case of the `Interpolation` block, the `data` and the `time` act like
structural parameters.

As such, we can also build the interpolation object outside of the model

```@example interpolation_block
my_interpolation = LinearInterpolation(df.data, df.time)

@mtkmodel MassSpringDamperSystem2 begin
@components begin
src = Interpolation(itp=my_interpolation)
clk = ContinuousClock()
model = MassSpringDamper()
end
@equations begin
connect(src.input, clk.output)
connect(src.output, model.input)
end
end;
@mtkbuild sys = MassSpringDamperSystem2()

prob = ODEProblem(sys, [], (0, df.time[end]))
sol = solve(prob, Tsit5())
plot(sol)
```

Note that the interpolation is constructed outside of the model, so we cannot use `remake` to change the
data. For that usecase, see the `ParametrizedInterpolation`.

## `ParametrizedInterpolation` Block

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.
Expand Down Expand Up @@ -145,7 +174,7 @@ plot(sol2)
```

!!! note

Note that when changing the data, the length of the new data must be the same as the length of the original data.

## Custom Component with External Data
Expand Down
10 changes: 8 additions & 2 deletions src/Blocks/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -755,10 +755,12 @@ such as `LinearInterpolation`, `ConstantInterpolation` or `CubicSpline`.
"""
function Interpolation(interp_type, u, x, args...; name)
itp = interp_type(u, x, args...)
Interpolation(itp; name)
Interpolation(; itp, name)
end

function Interpolation(itp; name)
@deprecate Interpolation(itp; name) Interpolation(; itp, name)

function Interpolation(; itp, name)
@parameters (interpolator::typeof(itp))(..) = itp
@named input = RealInput()
@named output = RealOutput()
Expand Down Expand Up @@ -868,3 +870,7 @@ function ParametrizedInterpolation(
systems = [input, output],
name)
end

function ParametrizedInterpolation(; interp_type, u::AbstractVector, x::AbstractVector, name)
ParametrizedInterpolation(interp_type, u, x; name)
end
38 changes: 38 additions & 0 deletions test/Blocks/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,44 @@ end
@test SciMLBase.successful_retcode(sol)
end

@testset "Interpolation in model macro" begin

function MassSpringDamper(; name)
@named input = RealInput()
@variables f(t) x(t)=0 dx(t)=0 ddx(t)
@parameters m=10 k=1000 d=1

eqs = [f ~ input.u
ddx * 10 ~ k * x + d * dx + f
D(x) ~ dx
D(dx) ~ ddx]

ODESystem(eqs, t; name, systems = [input])
end

table_data = [1.0, 2.0, 3.0]
table_bkp = [0.0, 0.5, 1.0]
itp = LinearInterpolation(table_data, table_bkp)

@mtkmodel model_with_lut begin
@components begin
src = Interpolation(itp)
clk = ContinuousClock()
model = MassSpringDamper()
end
@equations begin
connect(src.input, clk.output)
connect(src.output, model.input)
end
end;
@mtkbuild sys = model_with_lut()

prob = ODEProblem(sys, [], (0.0, 1))
sol = solve(prob, Tsit5())

@test SciMLBase.successful_retcode(sol)
end

@testset "ParametrizedInterpolation" begin
@variables y(t) = 0
u = rand(15)
Expand Down
Loading