Skip to main content

millman_theorem/
millman_theorem.rs

1use reda_spice::{netlist::Circuit, simulate::ngspice::NgSpiceShared};
2use reda_unit::u;
3
4/// https://pyspice.fabrice-salvaire.fr/releases/v1.4/examples/fundamental-laws/millman-theorem.html
5fn main_result() -> Result<(), Box<dyn std::error::Error>> {
6    let mut circuit = Circuit::new("Millman's theorem");
7    let number_of_branches = 3;
8    for i in 1..number_of_branches+1 {
9        circuit.add_dc_voltage(
10            format!("input{}", i), 
11            format!("{}", i), 
12            "0", 
13            u!(i V),
14        );
15        circuit.add_resistor(
16            format!("{}", i), 
17            format!("{}", i), 
18            "A",
19            u!(i kΩ));
20    }
21
22    let simulate = NgSpiceShared::default()?;
23    let mut simulator = circuit.simulator(simulate);
24    let analysis = simulator.run_op()?;
25
26    let node_a = analysis.get_node("A").unwrap();
27    println!("Node {}: {}", "A", node_a);
28
29    Ok(())
30}
31
32fn main() {
33    if let Err(e) = main_result() {
34        eprintln!("{}", e);
35    }
36}