Skip to content

Commit

Permalink
feat: add ParametrizedInterpolation
Browse files Browse the repository at this point in the history
Co-authored-by: Fredrik Bagge Carlson <[email protected]>
  • Loading branch information
SebastianM-C and baggepinnen committed Aug 2, 2024
1 parent 31114a0 commit f46198d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[weakdeps]
DataInterpolations = "82cc6244-b520-54b8-b5a6-8a565e85f1d0"

[extensions]
ModelingToolkitStandardLibraryDataInterpolationsExt = "DataInterpolations"

[compat]
Aqua = "0.8"
ChainRulesCore = "1.18"
ControlSystemsBase = "1.4"
DataInterpolations = "4.6"
DataInterpolations = "4.6, 5, 6"
DiffEqBase = "6.147"
IfElse = "0.1"
LinearAlgebra = "1.10"
Expand Down
10 changes: 10 additions & 0 deletions ext/ModelingToolkitStandardLibraryDataInterpolationsExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module ModelingToolkitStandardLibraryDataInterpolationsExt

using ModelingToolkitStandardLibrary
using ModelingToolkitStandardLibrary.Blocks.Symbolics
using DataInterpolations

@register_symbolic ModelingToolkitStandardLibrary.Blocks.apply_interpolation(
t, i::DataInterpolations.AbstractInterpolation)

end
2 changes: 1 addition & 1 deletion src/Blocks/Blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export Log, Log10
include("math.jl")

export Constant, TimeVaryingFunction, Sine, Cosine, ContinuousClock, Ramp, Step, ExpSine,
Square, Triangular, Parameter, SampledData
Square, Triangular, Parameter, SampledData, ParametrizedInterpolation
include("sources.jl")

export Limiter, DeadZone, SlewRateLimiter
Expand Down
25 changes: 25 additions & 0 deletions src/Blocks/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -730,3 +730,28 @@ end
function SampledData(; name, buffer, sample_time, circular_buffer)
SampledData(SampledDataType.vector_based; name, buffer, sample_time, circular_buffer)
end

# This needs to be extend for interpolation types
apply_interpolation(t, interp) = interp(t)

@register_symbolic build_interpolation(
interpolation_type::UnionAll, u::AbstractArray, x::AbstractArray)
build_interpolation(interpolation_type, u, x) = interpolation_type(u, x)

function ParametrizedInterpolation(interp_type::T, u, x, t = t; name) where {T}
@parameters data[1:length(x)] = u
@parameters ts[1:length(x)] = x
@parameters interpolation_type::T=interp_type [tunable = false]
@parameters interpolator::interp_type

@named output = RealOutput()

eqs = [output.u ~ apply_interpolation(t, interpolator)]

ODESystem(eqs, t, [], [u, x, interpolation_type, interpolator];
parameter_dependencies = [
interpolator => build_interpolation(interpolation_type, u, x)
],
systems = [output],
name)
end

0 comments on commit f46198d

Please sign in to comment.