Skip to content

Using Datasets with Interpolation ​

This tutorial shows how you can integrate real-world data into your Dyad models using datasets and Interpolation blocks. You'll learn to create time-varying inputs from CSV files and other data sources to make your simulations more realistic.

Prerequisites ​

Before you start this tutorial, you should:

Setting Up Your Project ​

Step 1: Create a Component Library ​

First, create a new component library for this tutorial:

  1. Open the Command Palette (⇧ ⌘ P on macOS, Ctrl-Shift-P on Windows)

  2. Type Dyad and select Dyad: Create Component Library

  3. Name your library DatasetTutorial

Step 2: Add Dependencies ​

In the Julia REPL, add the required packages:

julia
using Pkg
Pkg.add(["BlockComponents", "DyadData", "Plots"])

Step 3: Create Sample Data ​

For this tutorial, we'll create a simple CSV file with time-varying temperature data. Create a folder named assets inside your component library and a file called temperature_profile.csv inside the assets folder:

csv
time,temperature
0.0,295.15
300.0,310.15
600.0,298.15
900.0,305.15
1200.0,293.15

This represents a temperature profile over 20 minutes (1200 seconds) with values in Kelvin.

Building a Thermal Control System ​

Create a new file dyad/thermal_system.dyad with a complete thermal control system that uses your dataset as a temperature setpoint. Here’s how your library directory should look like at this stage:

.
└── DatasetTutorial/
    ├── dyad/
    │   └── thermal_system.dyad    # Dyad component and analysis definitions
    ├── assets/
    │   └── temperature_profile.csv    # CSV data file
    ├── src/
    │   ├── DatasetTutorial.jl
    │   └── generated/
    ├── test/
    └── Project.toml   # Project dependencies (BlockComponents, DyadData, etc.)

In the dyad/thermal_system.dyad file, add the following:

dyad
using BlockComponents: Interpolation, InterpolationType, LimPID
using DyadData: DyadTimeseries

component ThermalSystem
  # Temperature setpoint from dataset
  setpoint_source = BlockComponents.Interpolation(
    interpolation_type = BlockComponents.InterpolationType.LinearInterpolation(),
    dataset = DyadData.DyadTimeseries("dyad://DatasetTutorial/temperature_profile.csv",
                         independent_var = "time",
                         dependent_vars = ["temperature"])
  )

  # PID controller with limited output
  controller = BlockComponents.LimPID(
    k = 50.0,         # Controller gain
    Ti = 30.0,        # Integral time
    y_max = 100.0,    # Maximum heater power (W)
    y_min = 0.0       # Minimum heater power (W)
  )

  # Simple thermal parameters
  parameter C::Real = 500.0  # Thermal capacitance (J/K)
  parameter G::Real = 2.0    # Thermal conductance (W/K)
  parameter T_amb::Temperature = 293.15  # Ambient temperature (K)

  # System variables
  variable setpoint::Temperature
  variable T::Temperature  # Current temperature
  variable Q::Power        # Heating power

relations
  # Get setpoint from dataset
  setpoint_source.u = time
  setpoint_source.y = setpoint

  # Initial temperature
  initial T = T_amb

  # Connect PID controller
  controller.u_s = setpoint
  controller.u_m = T
  controller.y = Q

  # Simple thermal dynamics: C*dT/dt = Q - G*(T - T_amb)
  C * der(T) = Q - G * (T - T_amb)
end

You should also add an analysis to test the thermal system. In the same file:

dyad
analysis ThermalSystemAnalysis
  extends TransientAnalysis(stop = 1200.0)
  model = ThermalSystem()
end

Running the Simulation ​

In the Julia REPL:

@example
using DatasetTutorial

# Run the thermal system analysis
result = ThermalSystemAnalysis()

# Plot the results
using Plots
plot(result)

You should see a plot showing how the thermal system responds to the dataset-driven setpoint. The plot will show multiple variables including the setpoint from your CSV data, the actual temperature response, and the PID controller output.

Visualizing System Performance ​

For a more detailed visualization of the thermal system behavior, you can create custom plots:

julia
# Run the thermal system
result = ThermalSystemAnalysis()

# Create a detailed plot
using DyadInterface: artifacts
using Plots

sol = artifacts(result, :RawSolution)
model = artifacts(result, :SimplifiedSystem)

plt = plot(sol.t, sol[model.setpoint],
     label="Setpoint (from dataset)",
     xlabel="Time (s)",
     linewidth=2,
     linestyle=:dash,
     color=:blue)
plot!(plt, sol.t, sol[model.T],
      label="Temperature",
      linewidth=2,
      color=:red)
plot!(plt, sol.t, sol[model.Q],
      label="Heater Power",
      linewidth=1,
      color=:green,
      ylabel="Power (W)")

title!(plt, "Dataset-Driven Thermal Control System")
plt

This visualization shows how your dataset drives the system behavior, how the thermal model responds to the time-varying setpoint, and how the PID controller adjusts the heater power to maintain temperature control.

Different Interpolation Methods ​

You can experiment with different interpolation methods by changing the interpolation_type:

dyad
# Step-wise constant interpolation
constant_interp = BlockComponents.Interpolation(
  interpolation_type = BlockComponents.InterpolationType.ConstantInterpolation(),
  dataset = DyadData.DyadTimeseries("dyad://DatasetTutorial/temperature_profile.csv",
                       independent_var = "time",
                       dependent_vars = ["temperature"])
)

# Smooth cubic spline interpolation
smooth_interp = BlockComponents.Interpolation(
  interpolation_type = BlockComponents.InterpolationType.CubicSpline(),
  dataset = DyadData.DyadTimeseries("dyad://DatasetTutorial/temperature_profile.csv",
                       independent_var = "time",
                       dependent_vars = ["temperature"])
)

Each method affects how the data is interpolated between points:

  • InterpolationType.LinearInterpolation(): Straight lines between data points

  • InterpolationType.ConstantInterpolation(): Step changes at data points

  • InterpolationType.CubicSpline(): Smooth curves through data points

Using Different Data Sources ​

You can load datasets from various sources:

dyad
# Local CSV file
local_dataset = DyadData.DyadTimeseries("file://path/to/local/data.csv",
                           independent_var = "time",
                           dependent_vars = ["value"])

# JuliaHub dataset
juliahub_dataset = DyadData.DyadTimeseries("dyad+juliahub://juliahub.com/datasets/username/dataset_name",
                         independent_var = "time",
                         dependent_vars = ["measurement"])

# Package-bundled data using the `dyad://` URI (recommended)
package_dataset = DyadData.DyadTimeseries("dyad://MyLibrary/experiment.csv",
                             independent_var = "timestamp",
                             dependent_vars = ["temperature", "pressure"])

Next Steps ​

You've learned how to:

  • Set up datasets for interpolation in Dyad models

  • Create interpolation components that use real data

  • Build systems that respond to dataset-driven inputs

  • Visualize how datasets influence system behavior

For more advanced dataset usage, including parameter calibration and model validation, see the Working with Data manual section.

Try modifying the thermal system parameters or creating your own dataset to see how different data profiles affect system behavior. You can also explore using multiple datasets simultaneously or creating more complex control strategies based on your data.