Skip to content

Commit f46198d

Browse files
feat: add ParametrizedInterpolation
Co-authored-by: Fredrik Bagge Carlson <[email protected]>
1 parent 31114a0 commit f46198d

4 files changed

+43
-2
lines changed

Project.toml

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1111
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1212
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1313

14+
[weakdeps]
15+
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"
16+
17+
[extensions]
18+
ModelingToolkitStandardLibraryDataInterpolationsExt = "DataInterpolations"
19+
1420
[compat]
1521
Aqua = "0.8"
1622
ChainRulesCore = "1.18"
1723
ControlSystemsBase = "1.4"
18-
DataInterpolations = "4.6"
24+
DataInterpolations = "4.6, 5, 6"
1925
DiffEqBase = "6.147"
2026
IfElse = "0.1"
2127
LinearAlgebra = "1.10"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module ModelingToolkitStandardLibraryDataInterpolationsExt
2+
3+
using ModelingToolkitStandardLibrary
4+
using ModelingToolkitStandardLibrary.Blocks.Symbolics
5+
using DataInterpolations
6+
7+
@register_symbolic ModelingToolkitStandardLibrary.Blocks.apply_interpolation(
8+
t, i::DataInterpolations.AbstractInterpolation)
9+
10+
end

src/Blocks/Blocks.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export Log, Log10
1616
include("math.jl")
1717

1818
export Constant, TimeVaryingFunction, Sine, Cosine, ContinuousClock, Ramp, Step, ExpSine,
19-
Square, Triangular, Parameter, SampledData
19+
Square, Triangular, Parameter, SampledData, ParametrizedInterpolation
2020
include("sources.jl")
2121

2222
export Limiter, DeadZone, SlewRateLimiter

src/Blocks/sources.jl

+25
Original file line numberDiff line numberDiff line change
@@ -730,3 +730,28 @@ end
730730
function SampledData(; name, buffer, sample_time, circular_buffer)
731731
SampledData(SampledDataType.vector_based; name, buffer, sample_time, circular_buffer)
732732
end
733+
734+
# This needs to be extend for interpolation types
735+
apply_interpolation(t, interp) = interp(t)
736+
737+
@register_symbolic build_interpolation(
738+
interpolation_type::UnionAll, u::AbstractArray, x::AbstractArray)
739+
build_interpolation(interpolation_type, u, x) = interpolation_type(u, x)
740+
741+
function ParametrizedInterpolation(interp_type::T, u, x, t = t; name) where {T}
742+
@parameters data[1:length(x)] = u
743+
@parameters ts[1:length(x)] = x
744+
@parameters interpolation_type::T=interp_type [tunable = false]
745+
@parameters interpolator::interp_type
746+
747+
@named output = RealOutput()
748+
749+
eqs = [output.u ~ apply_interpolation(t, interpolator)]
750+
751+
ODESystem(eqs, t, [], [u, x, interpolation_type, interpolator];
752+
parameter_dependencies = [
753+
interpolator => build_interpolation(interpolation_type, u, x)
754+
],
755+
systems = [output],
756+
name)
757+
end

0 commit comments

Comments
 (0)