app
Adom Fields
Public Made by Adomby adom
Adom Fields: where the current and the heat go on your board.
← Commit history
src/terminals.rs.origdeleted−62@@ -1,62 +0,0 @@-//! Explicit signed pin currents. Positive enters copper; negative leaves it.-use crate::board::{Board, Pad};-use serde::{Deserialize, Serialize};-use std::collections::BTreeSet;--#[derive(Clone, Debug, Deserialize, Serialize)]-#[serde(deny_unknown_fields)]-pub struct Terminal {- pub reference: String,- pub pad: String,- pub amps: f64,-}--pub fn resolve<'a>(board: &'a Board, net: &str, amps: f64, rows: &[Terminal])- -> Result<Vec<(&'a Pad, f64)>, String>-{- if rows.is_empty() { return Err(format!("{net}: explicit terminals must not be empty")); }- let mut seen = BTreeSet::new();- let mut result = Vec::new();- let (mut positive, mut negative) = (0.0, 0.0);- for t in rows {- if !t.amps.is_finite() || !seen.insert((&t.reference, &t.pad)) {- return Err(format!("{net}: terminal currents must be finite and pin assignments unique"));- }- let pads: Vec<_> = board.pads.iter().filter(|p| p.reference == t.reference && p.pad == t.pad).collect();- if pads.len() != 1 || pads[0].net != net {- return Err(format!("{net}: {}.{} must identify exactly one pad on this net", t.reference, t.pad));- }- positive += t.amps.max(0.0);- negative += (-t.amps).max(0.0);- result.push((pads[0], t.amps));- }- let tolerance = 1e-9_f64.max(amps.abs() * 1e-8);- if !amps.is_finite() || amps < 0.0 || (positive - amps).abs() > tolerance || (negative - amps).abs() > tolerance {- return Err(format!("{net}: sources ({positive} A) and sinks ({negative} A) must each equal load ({amps} A)"));- }- Ok(result)-}--#[cfg(test)]-mod tests {- use super::*;- fn board() -> Board {- Board { pads: [("J1", "1", "V"), ("F1", "1", "V"), ("R1", "1", "V"), ("J2", "1", "GND")].into_iter().map(|(r,p,n)| Pad { reference:r.into(), pad:p.into(), net:n.into(), x:0., y:0., layers:vec!["F.Cu".into()], area:1., func:String::new() }).collect(), ..Board::default() }- }- fn row(reference: &str, amps: f64) -> Terminal { Terminal { reference:reference.into(), pad:"1".into(), amps } }- #[test] fn main_and_sense_branches_preserve_assigned_currents() {- let b=board();- let r=resolve(&b,"V",3.,&[row("J1",3.),row("F1",-2.9999),row("R1",-0.0001)]).unwrap();- assert_eq!(r[1].0.reference,"F1"); assert_eq!(r[2].1,-0.0001);- }- #[test] fn reject_wrong_net_missing_duplicate_and_unbalanced_terminals() {- let b=board();- for rows in [vec![],vec![row("J1",3.),row("J2",-3.)],vec![row("J1",3.),row("MISSING",-3.)],vec![row("J1",3.),row("F1",-2.)],vec![row("J1",3.),row("F1",-1.5),row("F1",-1.5)],vec![row("J1",f64::NAN),row("F1",-3.)]] {- assert!(resolve(&b,"V",3.,&rows).is_err());- }- }- #[test] fn reject_ambiguous_repeated_pad_number() {- let mut b=board(); b.pads.push(b.pads[0].clone());- assert!(resolve(&b,"V",3.,&[row("J1",3.),row("F1",-3.)]).is_err());- }-}