Skip to content
InterpolationJuliaHubDatasetTest.md

InterpolationJuliaHubDatasetTest

Test time-dependent interpolation of JuliaHub dataset values.

Creates a time-based interpolation from a JuliaHub dataset, integrating the interpolated values to produce the output variable.

The component loads time-series data from a specified JuliaHub dataset and uses quadratic interpolation to approximate the data at simulation time points. The derivative of the output variable is set equal to the interpolated value, causing the output to be the integral of the dataset values.

Usage

InterpolationJuliaHubDatasetTest()

Parameters:

NameDescriptionUnitsDefault value
datasetReference to a dataset stored on JuliaHub, specifying the collection, dataset name, and which variables to extractDyadDataset("juliasimtutorials", "circuit_data", dependent_vars=["ampermeter.i(t)"], independent_var="timestamp")

Variables

NameDescriptionUnits
yOutput variable that integrates the interpolated dataset values

Behavior

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

Source

dyad
# Test time-dependent interpolation of JuliaHub dataset values.
#
# Creates a time-based interpolation from a JuliaHub dataset, integrating the interpolated values to
# produce the output variable.
#
# The component loads time-series data from a specified JuliaHub dataset and uses quadratic
# interpolation to approximate the data at simulation time points. The derivative of the output variable
# is set equal to the interpolated value, causing the output to be the integral of the dataset values.
test component InterpolationJuliaHubDatasetTest
  # Interpolation block configured to use quadratic interpolation on the specified dataset
  interp = Interpolation(interpolation_type=QuadraticInterpolation, dataset=dataset)
  # Reference to a dataset stored on JuliaHub, specifying the collection, dataset name, and which variables to extract
  structural parameter dataset::DyadDataset = DyadDataset("juliasimtutorials", "circuit_data", dependent_vars=["ampermeter.i(t)"], independent_var="timestamp")
  # Output variable that integrates the interpolated dataset values
  variable y::Real
relations
  interp.u = time
  interp.y = der(y)
  initial y = 0
metadata {
  "Dyad": {
    "tests": {"case1": {"start": 0.001, "stop": 0.1, "expect": {"signals": ["y"]}}}
  }
}
end
Flattened Source
dyad
# Test time-dependent interpolation of JuliaHub dataset values.
#
# Creates a time-based interpolation from a JuliaHub dataset, integrating the interpolated values to
# produce the output variable.
#
# The component loads time-series data from a specified JuliaHub dataset and uses quadratic
# interpolation to approximate the data at simulation time points. The derivative of the output variable
# is set equal to the interpolated value, causing the output to be the integral of the dataset values.
test component InterpolationJuliaHubDatasetTest
  # Interpolation block configured to use quadratic interpolation on the specified dataset
  interp = Interpolation(interpolation_type=QuadraticInterpolation, dataset=dataset)
  # Reference to a dataset stored on JuliaHub, specifying the collection, dataset name, and which variables to extract
  structural parameter dataset::DyadDataset = DyadDataset("juliasimtutorials", "circuit_data", dependent_vars=["ampermeter.i(t)"], independent_var="timestamp")
  # Output variable that integrates the interpolated dataset values
  variable y::Real
relations
  interp.u = time
  interp.y = der(y)
  initial y = 0
metadata {
  "Dyad": {
    "tests": {"case1": {"start": 0.001, "stop": 0.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 = InterpolationJuliaHubDatasetTest()
u0_case1 = []
prob_case1 = ODEProblem(model_case1, u0_case1, (0.001, 0.1))
sol_case1 = solve(prob_case1)
retcode: Success
Interpolation: 3rd order Hermite
t: 26-element Vector{Float64}:
 0.001
 0.0011
 0.0021
 0.003029706504017924
 0.004116185208502843
 0.005721015242577112
 0.00942467041935931
 0.013718572193584161
 0.01718872828700834
 0.021154280698337247

 0.061647639833527106
 0.06635974244008444
 0.07153853926760174
 0.07629315435591906
 0.08179208664364576
 0.0866586774302142
 0.09168499449396698
 0.09662923472666178
 0.1
u: 26-element Vector{Vector{Float64}}:
 [0.0]
 [-9.425664399209995e-7]
 [-7.376177510326693e-6]
 [-7.180282821727483e-6]
 [2.841175073879014e-7]
 [1.8102583039637763e-5]
 [1.7745869180185366e-5]
 [-3.101686547247973e-6]
 [2.8513079984653242e-5]
 [-1.1015602416352474e-6]

 [-7.607144307879818e-6]
 [2.2040691366223896e-5]
 [-7.265535481954188e-6]
 [2.0696484943196212e-5]
 [-8.962353466517714e-6]
 [2.369327603151941e-5]
 [-8.343239927696313e-6]
 [2.36301559369883e-5]
 [8.695356728813012e-6]
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "InterpolationJuliaHubDatasetTest_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