Skip to content
SensorsTest.md

SensorsTest

A test circuit with a resistor and capacitor in series, driven by a sinusoidal voltage source, instrumented with voltage, current, and power sensors.

This component models an electrical R-C series circuit. A VoltageSource, whose voltage is determined by a Sine signal generator, drives a Resistor (R=1 Ohm) and a Capacitor (C=1 Farad) connected in series. The circuit is completed by a Ground component. Several sensors are included to monitor the circuit's behavior: a CurrentSensor measures the current flowing through the series R-C combination, a VoltageSensor measures the voltage across the Capacitor, and a PowerSensor measures the instantaneous power associated with the Capacitor. The sine wave generator (source) provides an input signal defined by V(t)=offset+amplitudesin(2πfrequencyt), where offset=1, amplitude=10, and frequency=5 Hz. The primary purpose is to test the dynamic response of these sensor components in a simple circuit.

Usage

SensorsTest()

Behavior

[source.y(t)=voltage.V(t)connect(voltage+p,resistor+p)connect(resistor+n,currentsensor+p)connect(currentsensor+n,powersensor+pc)connect(powersensor+nc,capacitor+p)connect(capacitor+n,voltage+n,ground+g)connect(capacitor+p,voltagesensor+p,powersensor+pv)connect(capacitor+n,voltagesensor+n,powersensor+nv)source.y(t)=ifelse(source.start_time<t,source.offset+source.amplitudesin(source.phase+6.2832source.frequency(source.start_time+t)),source.offset)voltage.v(t)=voltage.p.v(t)voltage.n.v(t)voltage.i(t)=voltage.p.i(t)voltage.p.i(t)+voltage.n.i(t)=0voltage.v(t)=voltage.uVvoltage.V(t)resistor.v(t)=resistor.n.v(t)+resistor.p.v(t)resistor.i(t)=resistor.p.i(t)resistor.p.i(t)+resistor.n.i(t)=0resistor.v(t)=resistor.Rresistor.i(t)capacitor.v(t)=capacitor.p.v(t)capacitor.n.v(t)capacitor.i(t)=capacitor.p.i(t)capacitor.p.i(t)+capacitor.n.i(t)=0capacitor.Cdcapacitor.v(t)dt=capacitor.i(t)ground.g.v(t)=0voltage_sensor.p.i(t)=0voltage_sensor.n.i(t)=0voltage_sensor.v(t)=voltage_sensor.p.v(t)voltage_sensor.n.v(t)current_sensor.p.v(t)=current_sensor.n.v(t)current_sensor.p.i(t)=current_sensor.i(t)current_sensor.n.i(t)=current_sensor.i(t)power_sensor.power(t)=power_sensor.voltage_sensor.v(t)power_sensor.current_sensor.i(t)connect(pv,voltagesensor+p)connect(voltagesensor+n,nv)connect(pc,currentsensor+p)connect(currentsensor+n,nc)power_sensor.voltage_sensor.p.i(t)=0power_sensor.voltage_sensor.n.i(t)=0power_sensor.voltage_sensor.v(t)=power_sensor.voltage_sensor.n.v(t)+power_sensor.voltage_sensor.p.v(t)power_sensor.current_sensor.p.v(t)=power_sensor.current_sensor.n.v(t)power_sensor.current_sensor.p.i(t)=power_sensor.current_sensor.i(t)power_sensor.current_sensor.n.i(t)=power_sensor.current_sensor.i(t)]

Source

dyad
# A test circuit with a resistor and capacitor in series, driven by a sinusoidal voltage source, instrumented with voltage, current, and power sensors.
#
# This component models an electrical R-C series circuit. A `VoltageSource`, whose
# voltage is determined by a `Sine` signal generator, drives a `Resistor` (R=1 Ohm)
# and a `Capacitor` (C=1 Farad) connected in series. The circuit is completed by a
# `Ground` component. Several sensors are included to monitor the circuit's
# behavior: a `CurrentSensor` measures the current flowing through the series R-C
# combination, a `VoltageSensor` measures the voltage across the `Capacitor`, and
# a `PowerSensor` measures the instantaneous power associated with the `Capacitor`.
# The sine wave generator (`source`) provides an input signal defined by
# $V(t) = \text{offset} + \text{amplitude} \cdot \sin(2 \pi \cdot \text{frequency} \cdot t)$,
# where offset=1, amplitude=10, and frequency=5 Hz. The primary purpose is to
# test the dynamic response of these sensor components in a simple circuit.
test component SensorsTest
  # Signal generator providing a sinusoidal voltage waveform.
  source = BlockComponents.Sine(offset=1, amplitude=10, frequency=5)
  # Ideal voltage source whose output is controlled by the 'source' signal.
  voltage = VoltageSource()
  # Electrical resistor with a fixed resistance value.
  resistor = Resistor(R=1)
  # Electrical capacitor with a fixed capacitance value.
  capacitor = Capacitor(C=1)
  # Electrical ground reference (0V).
  ground = Ground()
  # Sensor to measure voltage difference between its 'p' and 'n' terminals.
  voltage_sensor = VoltageSensor()
  # Sensor to measure current flowing through it from 'p' to 'n'.
  current_sensor = CurrentSensor()
  # Sensor to measure electrical power, based on voltage (pv, nv) and current (pc, nc) measurements.
  power_sensor = PowerSensor()
relations
  connect(source.y, voltage.V)
  connect(voltage.p, resistor.p)
  connect(resistor.n, current_sensor.p)
  connect(current_sensor.n, power_sensor.pc)
  connect(power_sensor.nc, capacitor.p)
  connect(capacitor.n, voltage.n, ground.g)
  connect(capacitor.p, voltage_sensor.p, power_sensor.pv)
  connect(capacitor.n, voltage_sensor.n, power_sensor.nv)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 20,
        "initial": {"capacitor.v": 10},
        "expect": {
          "final": {
            "current_sensor.i": "0.31784799",
            "voltage_sensor.v": "0.682152",
            "power_sensor.power": "0.216820646"
          }
        }
      }
    }
  }
}
end
Flattened Source
dyad
# A test circuit with a resistor and capacitor in series, driven by a sinusoidal voltage source, instrumented with voltage, current, and power sensors.
#
# This component models an electrical R-C series circuit. A `VoltageSource`, whose
# voltage is determined by a `Sine` signal generator, drives a `Resistor` (R=1 Ohm)
# and a `Capacitor` (C=1 Farad) connected in series. The circuit is completed by a
# `Ground` component. Several sensors are included to monitor the circuit's
# behavior: a `CurrentSensor` measures the current flowing through the series R-C
# combination, a `VoltageSensor` measures the voltage across the `Capacitor`, and
# a `PowerSensor` measures the instantaneous power associated with the `Capacitor`.
# The sine wave generator (`source`) provides an input signal defined by
# $V(t) = \text{offset} + \text{amplitude} \cdot \sin(2 \pi \cdot \text{frequency} \cdot t)$,
# where offset=1, amplitude=10, and frequency=5 Hz. The primary purpose is to
# test the dynamic response of these sensor components in a simple circuit.
test component SensorsTest
  # Signal generator providing a sinusoidal voltage waveform.
  source = BlockComponents.Sine(offset=1, amplitude=10, frequency=5)
  # Ideal voltage source whose output is controlled by the 'source' signal.
  voltage = VoltageSource()
  # Electrical resistor with a fixed resistance value.
  resistor = Resistor(R=1)
  # Electrical capacitor with a fixed capacitance value.
  capacitor = Capacitor(C=1)
  # Electrical ground reference (0V).
  ground = Ground()
  # Sensor to measure voltage difference between its 'p' and 'n' terminals.
  voltage_sensor = VoltageSensor()
  # Sensor to measure current flowing through it from 'p' to 'n'.
  current_sensor = CurrentSensor()
  # Sensor to measure electrical power, based on voltage (pv, nv) and current (pc, nc) measurements.
  power_sensor = PowerSensor()
relations
  connect(source.y, voltage.V)
  connect(voltage.p, resistor.p)
  connect(resistor.n, current_sensor.p)
  connect(current_sensor.n, power_sensor.pc)
  connect(power_sensor.nc, capacitor.p)
  connect(capacitor.n, voltage.n, ground.g)
  connect(capacitor.p, voltage_sensor.p, power_sensor.pv)
  connect(capacitor.n, voltage_sensor.n, power_sensor.nv)
metadata {
  "Dyad": {
    "tests": {
      "case1": {
        "stop": 20,
        "initial": {"capacitor.v": 10},
        "expect": {
          "final": {
            "current_sensor.i": "0.31784799",
            "voltage_sensor.v": "0.682152",
            "power_sensor.power": "0.216820646"
          }
        }
      }
    }
  }
}
end


Test Cases

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

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

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

Test Case case1

julia
@mtkbuild model_case1 = SensorsTest()
u0_case1 = [model_case1.capacitor.v => 10]
prob_case1 = ODEProblem(model_case1, u0_case1, (0, 20))
sol_case1 = solve(prob_case1)
retcode: Success
Interpolation: 3rd order Hermite
t: 260-element Vector{Float64}:
  0.0
  0.05010103301073373
  0.0946087415384399
  0.1547779984639581
  0.21599802888572972
  0.2961800292855764
  0.3648333700644
  0.4445976597608447
  0.5320248713713668
  0.645331117296668

 19.473359833916902
 19.542409231002495
 19.615280251672313
 19.685905296490144
 19.75176360818496
 19.821772721570877
 19.899303953335025
 19.964273786046352
 20.0
u: 260-element Vector{Vector{Float64}}:
 [10.0]
 [9.873766252034635]
 [9.791971077475885]
 [8.924307746030344]
 [8.234084949901694]
 [8.24603506951831]
 [7.317565921913987]
 [6.929603455972936]
 [6.634921745318827]
 [5.850526707067355]

 [1.2208137865018391]
 [1.0656565910840579]
 [0.7230160263792365]
 [1.2918991859072264]
 [0.9725847505760823]
 [0.7602173433783846]
 [1.3182061937910738]
 [0.853161575643345]
 [0.6821520068556803]