reda_sp/model/sources/
mod.rs

1mod ac;
2mod sine;
3mod pulse;
4mod pwl;
5
6pub use ac::*;
7pub use sine::*;
8pub use pulse::*;
9pub use pwl::*;
10use reda_unit::{Current, Voltage};
11
12#[derive(Debug, Clone)]
13pub struct Source {
14    pub name: String, 
15    pub node_pos: String, 
16    pub node_neg: String, 
17    pub value: SourceValue,
18}
19
20#[derive(Debug, Clone)]
21pub enum SourceValue {
22    DcVoltage(Voltage),
23    DcCurrent(Current),
24    AcVoltage(AcVoltage),
25    AcCurrent(AcCurrent),
26    Sin(SineVoltage),
27    Pwl(PwlVoltage),
28    Pulse(PulseVoltage),
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum SourceKind {
33    Voltage,
34    Current,
35}
36
37impl Source {
38    pub fn to_spice(&self) -> String {
39        let (kind, value_spice) = match &self.value {
40            SourceValue::DcVoltage(v) => (SourceKind::Voltage, format!("DC {}", v.to_string())),
41            SourceValue::DcCurrent(c) => (SourceKind::Current, format!("DC {}", c.to_string())),
42            SourceValue::AcVoltage(ac) => (SourceKind::Voltage, ac.to_spice()),
43            SourceValue::AcCurrent(ac) => (SourceKind::Current, ac.to_spice()),
44            SourceValue::Sin(sin) => (SourceKind::Voltage, sin.to_spice()),
45            SourceValue::Pulse(pulse) => (SourceKind::Voltage, pulse.to_spice()),
46            SourceValue::Pwl(pwl) => (SourceKind::Voltage, pwl.to_spice()),
47        };
48
49        let kind = if kind == SourceKind::Voltage { 'V' } else { 'I' };
50
51        format!("{}{} {} {} {}", kind, self.name, self.node_pos, self.node_neg, value_spice)
52    }
53}