Hello,
Is there a way to interpolate within a ModelingToolkit’s (MTK) component model using the parameters/variables of the model? Interpolations.jl seems to struggle with Num types, and from the example in the documentation (Composing Ordinary Differential Equations · ModelingToolkit.jl) I understand that it is only possible to use pre-interpolated data that is then fed to the model as a time-variable function (?).
To illustrate this idea, I include a simple example using t as coordinate to obtain the interpolated variable.
using ModelingToolkit
using DifferentialEquations
using Interpolations
# Function to interpolate a time series
function interpolateTimeSeries(t, time, values)
interpolator = CubicSplineInterpolation(time, values)
interpolatedValue = interpolator(t)
end
@register interpolateTimeSeries(t, time, values)
# Source to produce the output of a timeseries table
function lookupTable(;name, time, values)
@parameters t
@variables output(t)
equations = [output ~ interpolateTimeSeries(t, time, values)]
ODESystem(equations, t, output, []; name=name)
end
############################## lookupTable source model #################
# Data
time = LinRange(1, 100, 100)
values = randn(100)
# Instantiate lookup-table source model
@named sourceLookupTable = lookupTable(time = time, values = values)
simpSource = structural_simplify(sourceLookupTable)
u0 = [sourceLookupTable => ]
# Define the problem
prob = ODEProblem(simpSource, u0, (0.0, 100.0))
sol = solve(prob, Tsit5())
This produces the following error:
ERROR: LoadError: TypeError: non-boolean (Num) used in boolean context
Stacktrace:
[1] inbounds_index at $PATH\.julia\packages\Interpolations\qHlUr\src\extrapolation\extrapolation.jl:108 [inlined]
[2] inbounds_position at $PATH\.julia\packages\Interpolations\qHlUr\src\extrapolation\extrapolation.jl:99 [inlined]
[3] Extrapolation at $PATH\.julia\packages\Interpolations\qHlUr\src\extrapolation\extrapolation.jl:48 [inlined]
[4] interpolateTimeSeries(::Num, ::LinRange{Float64}, ::Array{Float64,1}) at $PATH\SourcesSinks.jl:10
[5] lookupTable(; name::Symbol, time::LinRange{Float64}, values::Array{Float64,1}) at $PATH\SourcesSinks.jl:22
[6] top-level scope at $PATH\SourcesSinks.jl:34
[7] include(::String) at .\client.jl:457
[8] top-level scope at REPL[7]:1
in expression starting at $PATH\SourcesSinks.jl:34