1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Rust circuit simulator. SPICE-style MNA solver with built-in
//! semiconductor device models.
//!
//! `sindr` solves a [`Circuit`] — a list of components and a ground node —
//! and returns voltages, currents, and power for every component. The solver
//! picks the right path automatically:
//!
//! - **Linear DC** — direct solve of the MNA system.
//! - **Nonlinear DC** — Newton–Raphson when diodes, BJTs, MOSFETs, etc. are present.
//! - **Transient** — backward-Euler timestepping when capacitors, inductors,
//! or time-varying sources are present.
//! - **AC small-signal** — sinusoidal-steady-state via [`ac_analysis::solve_ac`].
//! - **DC sweep** — parameter sweep over a component value via [`fn@dc_sweep`].
//! - **Temperature sweep** — operating-point sweep over junction temperature
//! via [`temperature_sweep`].
//!
//! Device physics (diode, BJT, MOSFET, IGBT, JFET, varactor companion models)
//! live in the companion crate
//! [`sindr-devices`](https://crates.io/crates/sindr-devices). `sindr`
//! re-exports the few enums you'll typically need ([`BjtKind`],
//! [`MosfetKind`], [`JfetKind`], etc.).
//!
//! # Quick start
//!
//! Build a voltage divider, solve it, read the divided voltage:
//!
//! ```
//! use sindr::{Circuit, CircuitElement, solve_circuit};
//!
//! let circuit = Circuit {
//! ground_node: "0".into(),
//! components: vec![
//! CircuitElement::VoltageSource {
//! id: "V1".into(),
//! nodes: ["n1".into(), "0".into()],
//! voltage: 10.0,
//! waveform: None,
//! },
//! CircuitElement::Resistor {
//! id: "R1".into(),
//! nodes: ["n1".into(), "n2".into()],
//! resistance: 1_000.0,
//! },
//! CircuitElement::Resistor {
//! id: "R2".into(),
//! nodes: ["n2".into(), "0".into()],
//! resistance: 2_000.0,
//! },
//! ],
//! };
//!
//! let result = solve_circuit(&circuit).unwrap();
//!
//! // V_n2 = 10 V * R2/(R1+R2) = 10 * 2/3 ≈ 6.667 V
//! let v_n2 = result.node_voltages["n2"];
//! assert!((v_n2 - 6.6667).abs() < 1e-3);
//! ```
//!
//! # Conventions
//!
//! - **Ground node** must exist on at least one component. Its voltage is
//! defined as 0 V — every other voltage is reported relative to it.
//! - **Node names** are arbitrary strings (`"0"`, `"gnd"`, `"vcc"`, …).
//! Components share a node simply by referencing the same string.
//! - **SI units** throughout: V, A, Ω, F, H, s, K.
//! - **Sign conventions** are documented per [`CircuitElement`] variant.
//!
//! # Cargo features
//!
//! - `serde` *(default)* — `Serialize`/`Deserialize` impls on the public
//! types. Disable for embedded / no-allocator targets.
//! - `examples` — exposes the `examples` module with built-in named
//! circuits (voltage divider, BJT amp, RC transient, etc.).
//!
//! ```toml
//! [dependencies]
//! sindr = "0.1"
//!
//! # No serde:
//! sindr = { version = "0.1", default-features = false }
//! ```
//!
//! # Where to look next
//!
//! - [`Circuit`] / [`CircuitElement`] — the input format
//! - [`solve_circuit`] — the headline entry point
//! - [`SimulationResult`] — what you get back
//! - [`Waveform`] — time-varying source shapes
//! - [`SimError`] — what can go wrong
pub use ;
pub use ;
pub use ;
pub use SimError;
pub use MnaSystem;
pub use NodeMap;
pub use ;
pub use ;
pub use validate_circuit;
pub use Waveform;
// Device physics re-exports from sindr-devices — convenience for callers
// constructing CircuitElement variants without an extra crate import.
pub use BjtKind;
pub use IgbtParams;
pub use JfetKind;
pub use MosfetKind;
pub use VaractorParams;
use NodeMap as NM;
/// Solves a circuit end-to-end and returns voltages, currents, and power for
/// every component.
///
/// The solver picks the analysis path automatically based on what the circuit
/// contains:
///
/// | Circuit contains | Path |
/// |---|---|
/// | Only resistors + sources | Linear DC (single matrix solve) |
/// | Diodes / BJTs / MOSFETs / etc. | Nonlinear DC (Newton–Raphson) |
/// | Capacitors / inductors / waveforms | Transient (backward Euler) |
/// | Reactive **and** nonlinear | Transient nonlinear |
///
/// For frequency-domain analysis, use [`ac_analysis::solve_ac`] directly.
/// For parameter sweeps, see [`fn@dc_sweep`] and [`temperature_sweep`].
///
/// # Errors
///
/// Returns [`SimError`] if the circuit fails validation (no ground node,
/// floating nodes, invalid component values, etc.) or if the solver itself
/// fails to converge or produces a singular matrix. See [`SimError`] for the
/// full list.
///
/// # Example
///
/// ```
/// use sindr::{Circuit, CircuitElement, solve_circuit};
///
/// let circuit = Circuit {
/// ground_node: "0".into(),
/// components: vec![
/// CircuitElement::VoltageSource {
/// id: "V1".into(),
/// nodes: ["n1".into(), "0".into()],
/// voltage: 5.0,
/// waveform: None,
/// },
/// CircuitElement::Resistor {
/// id: "R1".into(),
/// nodes: ["n1".into(), "0".into()],
/// resistance: 1_000.0,
/// },
/// ],
/// };
///
/// let result = solve_circuit(&circuit).unwrap();
/// // I = V/R = 5 mA flowing through R1
/// let r1 = result.component_results.iter().find(|c| c.id == "R1").unwrap();
/// assert!((r1.current_through.abs() - 0.005).abs() < 1e-9);
/// ```
/// Like [`solve_circuit`] but seeds the Newton–Raphson initial guess with
/// the supplied per-node voltages (V), keyed by node name. Equivalent to
/// SPICE's `.NODESET` directive — useful when a circuit has multiple
/// operating points (e.g. Schmitt triggers, latches) or when the default
/// heuristic fails to converge.
///
/// Only consulted when the circuit contains nonlinear elements; for purely
/// linear DC and for transient analysis the seed is ignored. Unknown node
/// names in `initial_voltages` are silently skipped.
///
/// # Example
///
/// ```
/// use std::collections::HashMap;
/// use sindr::{Circuit, CircuitElement, solve_circuit_with_initial_voltages};
///
/// let circuit = Circuit {
/// ground_node: "0".into(),
/// components: vec![
/// CircuitElement::VoltageSource {
/// id: "V1".into(),
/// nodes: ["vcc".into(), "0".into()],
/// voltage: 5.0,
/// waveform: None,
/// },
/// CircuitElement::Resistor {
/// id: "R1".into(),
/// nodes: ["vcc".into(), "n1".into()],
/// resistance: 1_000.0,
/// },
/// CircuitElement::Diode {
/// id: "D1".into(),
/// nodes: ["n1".into(), "0".into()],
/// temperature: 300.15,
/// },
/// ],
/// };
///
/// let mut nodeset = HashMap::new();
/// nodeset.insert("n1".to_string(), 0.7); // hint forward-bias diode drop
/// let result = solve_circuit_with_initial_voltages(&circuit, &nodeset).unwrap();
/// assert!(result.node_voltages["n1"] > 0.5);
/// ```