reda_sp/model/components/
model.rs

1use std::collections::HashMap;
2use reda_unit::Number;
3
4use crate::ToSpice;
5
6#[derive(Debug, Clone)]
7pub struct Model {
8    pub name: String, 
9    pub kind: ModelKind, 
10    pub parameters: HashMap<String, Number>,
11}
12
13impl Model {
14    pub fn diode<S: Into<String>>(name: S) -> Self {
15        Self {
16            name: name.into(),
17            kind: ModelKind::Diode,
18            parameters: Default::default(),
19        }
20    }
21
22    pub fn npn<S: Into<String>>(name: S) -> Self {
23        Self {
24            name: name.into(),
25            kind: ModelKind::NPN,
26            parameters: Default::default(),
27        }
28    }
29
30    pub fn pnp<S: Into<String>>(name: S) -> Self {
31        Self {
32            name: name.into(),
33            kind: ModelKind::PNP,
34            parameters: Default::default(),
35        }
36    }
37
38    pub fn pmos<S: Into<String>>(name: S) -> Self {
39        Self {
40            name: name.into(),
41            kind: ModelKind::PMos,
42            parameters: Default::default(),
43        }
44    }
45
46    pub fn nmos<S: Into<String>>(name: S) -> Self {
47        Self {
48            name: name.into(),
49            kind: ModelKind::NMos,
50            parameters: Default::default(),
51        }
52    }
53
54    pub fn parameter<K: Into<String>, V: Into<Number>>(&mut self, key: K, val: V) {
55        self.parameters.insert(key.into(), val.into());
56    }
57}
58
59impl ToSpice for Model {
60    fn to_spice(&self) -> String {
61        let mut s = format!(".MODEL {} {} (", self.name, self.kind.to_str());
62        for (key, val) in self.parameters.iter() {
63            s.push_str(&key);
64            s.push('=');
65            s.push_str(&val.to_spice());
66        }
67        s.push(')');
68        s
69    }
70}
71
72#[derive(Debug, Clone, )]
73pub enum ModelKind {
74    Diode,
75    NPN,
76    PNP,
77    PMos,
78    NMos,
79}
80
81impl ModelKind {
82    pub fn to_str(&self) -> &'static str {
83        match self {
84            Self::Diode => "D",
85            Self::NPN => "NPN",
86            Self::PNP => "PNP",
87            Self::NMos => "NMOS",
88            Self::PMos => "PMOS",
89        }
90    }
91}