pub struct OperatingPoint {
pub unknowns: Vec<String>,
pub x: Vec<f64>,
pub diode_names: Vec<String>,
pub diode_z: Vec<(f64, f64)>,
pub diode_raw_ioff: Vec<f64>,
}Fields§
§unknowns: Vec<String>§x: Vec<f64>§diode_names: Vec<String>Resolved (z1, z2) for each diode, in the same iteration order as the diodes map
passed to solve_dc (a BTreeMap, so alphabetical by name) — useful for reporting
which segment each diode ended up in.
diode_z: Vec<(f64, f64)>§diode_raw_ioff: Vec<f64>Each diode’s true (unscaled) resolved current delta_on*z2 - delta_br*z1, same order
as diode_names/diode_z. Needed as history by simulate_transient’s trapezoidal
step (see its doc comment); exposed here rather than recomputed by every caller.
Implementations§
Source§impl OperatingPoint
impl OperatingPoint
Sourcepub fn value(&self, unknown: &str) -> Option<f64>
pub fn value(&self, unknown: &str) -> Option<f64>
Examples found in repository?
examples/boost_pid_closed_loop.rs (line 32)
16fn main() {
17 let netlist = "V1 vin 0 12\nD1 vx 0 idealswitchmodel\nD2 vx vout dmodel\nL1 vin vx 100u\nC1 vout 0 100u\nR1 vout 0 50";
18 let switch = IdealSwitch::new(0.01, IdealDiode::new(0.0, -1e6, 0.0, 1e6, 0.0));
19 let mut ideal_switches = BTreeMap::new();
20 ideal_switches.insert("D1".to_string(), switch);
21 let mut diodes = BTreeMap::new();
22 diodes.insert(
23 "D2".to_string(),
24 IdealDiode::new(0.0, -100.0, 0.0, 0.6, 100.0),
25 );
26
27 let reference = 24.0;
28 let pid = Pid::new(0.01, 20.0, 0.0, 1000.0).unwrap();
29 let controller = pid.to_state_space();
30
31 let switching_freq = 100_000.0;
32 let measure = |point: &dae_runtime::OperatingPoint| point.value("V(vout)").unwrap();
33 let pwm = move |duty: f64, t: f64| {
34 let duty = duty.clamp(0.0, 1.0);
35 let carrier = sawtooth_carrier(t, switching_freq);
36 let state = if carrier < duty {
37 GateState::On
38 } else {
39 GateState::Off
40 };
41 BTreeMap::from([("D1".to_string(), state)])
42 };
43
44 let dt = 1e-7;
45 let t_final = 0.010;
46
47 let trace = simulate_closed_loop(
48 netlist,
49 Dialect::Ngspice,
50 &diodes,
51 &ideal_switches,
52 &controller,
53 move |_| reference,
54 measure,
55 pwm,
56 (0.0, 1.0),
57 0.01,
58 None,
59 t_final,
60 dt,
61 )
62 .unwrap();
63
64 let out_path = "boost_pid_elspice_pwl_out.csv";
65 let mut f = std::fs::File::create(out_path).unwrap();
66 writeln!(f, "t,V(vout),duty").unwrap();
67 for (t, point, duty) in &trace {
68 writeln!(f, "{t},{},{}", point.value("V(vout)").unwrap(), duty).unwrap();
69 }
70 println!("wrote {out_path}");
71}More examples
examples/llc_validation.rs (line 139)
55fn main() {
56 let netlist = "V1 vin 0 400\n\
57 D1 vin vx idealswitchmodel\n\
58 D2 vx 0 idealswitchmodel\n\
59 Rs1 vin vx 1k\n\
60 Cs1 vin vx 1n\n\
61 Rs2 vx 0 1k\n\
62 Cs2 vx 0 1n\n\
63 Cr vx vr 22n\n\
64 Lr vr vp 100u\n\
65 Lm vp 0 400u\n\
66 Lpri vp 0 1000u\n\
67 Lsec1 vy 0 10u\n\
68 Lsec2 0 vz 10u\n\
69 K1 Lpri Lsec1 0.99\n\
70 K2 Lpri Lsec2 0.99\n\
71 K3 Lsec1 Lsec2 0.99\n\
72 D3 vy vout dmodel\n\
73 D4 vz vout dmodel\n\
74 Cout vout 0 470u\n\
75 Rout vout 0 20";
76
77 // D1/D2 stand in for the ideal half-bridge switches S1/S2 (RON=10m, no body diode of
78 // their own in the reference decks): body-diode threshold set unreachably high so
79 // gate=off means true full blocking, with a tiny nonzero g_off (see module doc, the
80 // dead-time spike finding).
81 let ideal_switch = IdealSwitch::new(0.01, IdealDiode::new(0.0, -1e6, 1e-6, 1e6, 0.0));
82 let mut ideal_switches = BTreeMap::new();
83 ideal_switches.insert("D1".to_string(), ideal_switch);
84 ideal_switches.insert("D2".to_string(), ideal_switch);
85 // D3/D4 are the real rectifier diodes (D_IDEAL: IS=1e-14, N=1, RS=10m); g_on=1/RS=100.
86 let mut diodes = BTreeMap::new();
87 diodes.insert(
88 "D3".to_string(),
89 IdealDiode::new(0.0, -100.0, 0.0, 0.6, 100.0),
90 );
91 diodes.insert(
92 "D4".to_string(),
93 IdealDiode::new(0.0, -100.0, 0.0, 0.6, 100.0),
94 );
95
96 // Matches the Xyce/ngspice decks' PULSE sources: S1 (D1) on for the first 4.8us of each
97 // 10us period; S2 (D2) on for the complementary window, phase-shifted by 5us -- ~0.2us
98 // dead time on each edge.
99 let period = 10e-6;
100 let on_width = 4.8e-6;
101 let gate_signal = move |name: &str, t: f64| {
102 let phase_offset = if name == "D2" { 5e-6 } else { 0.0 };
103 let tau = (t - phase_offset).rem_euclid(period);
104 if tau < on_width {
105 GateState::On
106 } else {
107 GateState::Off
108 }
109 };
110
111 let dt = 1e-8; // matches the .tran 10n step in both reference decks
112 let t_final = 1e-3; // 100 switching periods, matches both reference decks
113
114 let start = Instant::now();
115 let trace = simulate_transient_with_ideal_switches(
116 netlist,
117 Dialect::Ngspice,
118 &diodes,
119 &ideal_switches,
120 gate_signal,
121 0.01,
122 None,
123 t_final,
124 dt,
125 )
126 .unwrap();
127 let elapsed = start.elapsed();
128
129 println!(
130 "general-simulator LLC open-loop: {} steps in {:.3}s ({:.0} steps/s)",
131 trace.len(),
132 elapsed.as_secs_f64(),
133 trace.len() as f64 / elapsed.as_secs_f64()
134 );
135
136 let tail = &trace[trace.len() / 10..]; // last 90% (skip only the initial transient)
137 let avg_vout: f64 = tail
138 .iter()
139 .map(|(_, p)| p.value("V(vout)").unwrap())
140 .sum::<f64>()
141 / tail.len() as f64;
142 println!("avg Vout (last 90%): {avg_vout:.4}");
143
144 // CSV export (t, V(vout), V(vx)) for the cross-simulator waveform plot -- see the internal
145 // boost/LLC-vs-Xyce/ngspice validation experiment's own writeup.
146 // Same `t,unknown1,unknown2,...` column convention as general-simulator-cli's own CSV output.
147 let out_path = "llc_elspice_pwl_out.csv";
148 let mut f = std::fs::File::create(out_path).unwrap();
149 writeln!(f, "t,V(vout),V(vx)").unwrap();
150 for (t, point) in &trace {
151 writeln!(
152 f,
153 "{t},{},{}",
154 point.value("V(vout)").unwrap(),
155 point.value("V(vx)").unwrap()
156 )
157 .unwrap();
158 }
159 println!("wrote {out_path}");
160}Trait Implementations§
Source§impl Clone for OperatingPoint
impl Clone for OperatingPoint
Source§impl Debug for OperatingPoint
impl Debug for OperatingPoint
Source§impl PartialEq for OperatingPoint
impl PartialEq for OperatingPoint
impl StructuralPartialEq for OperatingPoint
Auto Trait Implementations§
impl Forget for OperatingPoint
impl Freeze for OperatingPoint
impl Move for OperatingPoint
impl RefUnwindSafe for OperatingPoint
impl Send for OperatingPoint
impl Sync for OperatingPoint
impl Unpin for OperatingPoint
impl UnsafeUnpin for OperatingPoint
impl UnwindSafe for OperatingPoint
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more