Skip to content
InterpolationTableTest.md

InterpolationTableTest

Tests interpolation by applying linear interpolation to a dataset of squares.

This component creates an interpolation object configured with linear interpolation and a dataset containing time values from 0 to 1 and their corresponding squared values. The current simulation time is used as the input to the interpolation, and the interpolated output is assigned to the variable y. The metadata includes a test case that runs until time=1 and verifies the y signal.

Usage

InterpolationTableTest()

Parameters:

NameDescriptionUnitsDefault value
datasetDataset containing time values from 0 to 1 and their squares, with 'ts' as independent variable and 'data' as dependent variableDyadDataset(hcat(0:0.1:1, square(0:0.1:1)), dependent_vars=["data"], independent_var="ts")

Variables

NameDescriptionUnits
yOutput variable that receives the interpolated value

Behavior

interp.u(t)=tinterp.y(t)=y(t)interp.y(t)=interp.interpolator(interp.u(t))

Source

dyad
# Tests interpolation by applying linear interpolation to a dataset of squares.
#
# This component creates an interpolation object configured with linear interpolation and a dataset
# containing time values from 0 to 1 and their corresponding squared values. The current simulation
# time is used as the input to the interpolation, and the interpolated output is assigned to the
# variable `y`. The metadata includes a test case that runs until time=1 and verifies the `y` signal.
test component InterpolationTableTest
  # Interpolation object that performs linear interpolation on the dataset
  interp = Interpolation(interpolation_type=LinearInterpolation, dataset=dataset)
  # Dataset containing time values from 0 to 1 and their squares, with 'ts' as independent variable and 'data' as dependent variable
  structural parameter dataset::DyadDataset = DyadDataset(hcat(0:0.1:1, square(0:0.1:1)), dependent_vars=["data"], independent_var="ts")
  # Output variable that receives the interpolated value
  variable y::Real
relations
  interp.u = time
  interp.y = y
metadata {"Dyad": {"tests": {"case1": {"stop": 1, "expect": {"signals": ["y"]}}}}}
end
Flattened Source
dyad
# Tests interpolation by applying linear interpolation to a dataset of squares.
#
# This component creates an interpolation object configured with linear interpolation and a dataset
# containing time values from 0 to 1 and their corresponding squared values. The current simulation
# time is used as the input to the interpolation, and the interpolated output is assigned to the
# variable `y`. The metadata includes a test case that runs until time=1 and verifies the `y` signal.
test component InterpolationTableTest
  # Interpolation object that performs linear interpolation on the dataset
  interp = Interpolation(interpolation_type=LinearInterpolation, dataset=dataset)
  # Dataset containing time values from 0 to 1 and their squares, with 'ts' as independent variable and 'data' as dependent variable
  structural parameter dataset::DyadDataset = DyadDataset(hcat(0:0.1:1, square(0:0.1:1)), dependent_vars=["data"], independent_var="ts")
  # Output variable that receives the interpolated value
  variable y::Real
relations
  interp.u = time
  interp.y = y
metadata {"Dyad": {"tests": {"case1": {"stop": 1, "expect": {"signals": ["y"]}}}}}
end


Test Cases

This is setup code, that must be run before each test case.

julia
using BlockComponents
using ModelingToolkit, OrdinaryDiffEqDefault
using Plots
using CSV, DataFrames

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
"/home/actions-runner-10/.julia/packages/BlockComponents/77kIK/test/snapshots"

Test Case case1

julia
@mtkbuild model_case1 = InterpolationTableTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 1))
sol_case1 = solve(prob_case1)
retcode: Success
Interpolation: 1st order linear
t: 2-element Vector{Float64}:
 0.0
 1.0
u: 2-element Vector{Vector{Float64}}:
 []
 []
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "InterpolationTableTest_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.y], width=2, label="Actual value of y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of y")
end

plt