Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Example: Electrical Circuits

Lets consider the following low-pass LRC filter circuit:

    +---L---+---C---+
    |       |       |
V_s =       R       |
    |       |       |
    +-------+-------+

The circuit consists of a resistor \(R\), an inductor \(L\), and a capacitor \(C\) connected to a voltage source \(V_s\). The voltage across the resistor \(V\) is given by Ohm's law:

\[ V = R i_R \]

where \(i_R\) is the current flowing through the resistor. The voltage across the inductor is \(V_s - V\), so the equation for an ideal inductor is:

\[ L \frac{di_L}{dt} = V_s - V \]

where \(di_L/dt\) is the rate of change of current with respect to time. The voltage across the capacitor is the same as the voltage across the resistor and the equation for an ideal capacitor is:

\[ \frac{dV}{dt} = \frac{i_C}{C} \]

where \(i_C\) is the current flowing through the capacitor. The sum of the currents flowing into and out of the top-center node of the circuit must be zero according to Kirchhoff's current law:

\[ i_L = i_R + i_C \]

Thus we have a system of two differential equations and two algebraic equations that describe the evolution of the currents through the resistor, inductor, and capacitor; and the voltage across the resistor. We can write these equations in the general form:

\[ M \frac{d\mathbf{y}}{dt} = \mathbf{f}(\mathbf{y}, t) \]

where

\[ \mathbf{y} = \begin{bmatrix} i_R \\ i_L \\ i_C \\ V \end{bmatrix} \]

and

\[ \mathbf{f}(\mathbf{y}, t) = \begin{bmatrix} V - R i_R \\ \frac{V_s - V}{L} \\ i_L - i_R - i_C \\ \frac{i_C}{C} \end{bmatrix} \]

The mass matrix \(M\) has one on the diagonal for the differential equations and zero for the algebraic equations.

\[ M = \begin{bmatrix} 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} \]

Instead of providing the mass matrix explicitly, the DiffSL language specifies the multiplication of the mass matrix with the derivative term, \(M \frac{d\mathbf{y}}{dt}\), which is given by:

\[ M \frac{d\mathbf{y}}{dt} = \begin{bmatrix} 0 \\ \frac{di_L}{dt} \\ 0 \\ \frac{dV}{dt} \end{bmatrix} \]

The initial conditions for the system are:

\[ \mathbf{y}(0) = \begin{bmatrix} 0 \\ 0 \\ 0 \\ 0 \end{bmatrix} \]

The voltage source \(V_s\) acts as a forcing function for the system, and we can specify this as sinusoidal function of time.

\[ V_s(t) = V_0 \sin(\omega t) \]

where \(V_0\) is the amplitude and \(\omega\) is the angular frequency of the source. The example uses \(R = 100\), \(L = 1\), \(C = 10^{-3}\), \(V_0 = 10\) and \(\omega = 100\). The cut-off frequency of this filter is approximately \(1/\sqrt{LC} \approx 31.6\) rad/s. The source frequency is higher than the cut-off frequency, so the circuit filters the source signal.

We can solve this system of equations using diffsol and plot the current \(i_R\) through the resistor as a function of time.

use diffsol::{CraneliftJitModule, MatrixCommon, OdeBuilder, OdeSolverMethod};
use plotly::{common::Mode, layout::Axis, layout::Layout, Plot, Scatter};
use std::fs;
type M = diffsol::NalgebraMat<f64>;
type CG = CraneliftJitModule;
type LS = diffsol::NalgebraLU<f64>;

fn main() {
    let problem = OdeBuilder::<M>::new()
        .build_from_diffsl::<CG>(
            "
        R { 100.0 } L { 1.0 } C { 0.001 } V0 { 10 } omega { 100.0 }
        Vs { V0 * sin(omega * t) }
        u_i {
            iR = 0,
            iL = 0,
            iC = 0,
            V = 0,
        }
        dudt_i {
            diRdt = 0,
            diLdt = 0,
            diCdt = 0,
            dVdt = 0,
        }
        M_i {
            0,
            diLdt,
            0,
            dVdt,
        }
        F_i {
            V - R * iR,
            (Vs - V) / L,
            iL - iR - iC,
            iC / C,
        }
        out_i {
            iR,
        }
    ",
        )
        .unwrap();
    let mut solver = problem.bdf::<LS>().unwrap();
    let (ys, ts, _stop_reason) = solver.solve(1.0).unwrap();

    let ir: Vec<_> = ys.inner().row(0).into_iter().copied().collect();
    let t: Vec<_> = ts.into_iter().collect();

    let ir = Scatter::new(t.clone(), ir).mode(Mode::Lines);

    let mut plot = Plot::new();
    plot.add_trace(ir);

    let layout = Layout::new()
        .x_axis(Axis::new().title("t"))
        .y_axis(Axis::new().title("current"));
    plot.set_layout(layout);
    let plot_html = plot.to_inline_html(Some("electrical-circuit"));
    fs::write("../src/primer/images/electrical-circuit.html", plot_html)
        .expect("Unable to write file");
}