Skip to content

Commit

Permalink
feat(isource): add support for ngspice current sources (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulk29 authored Nov 26, 2024
1 parent 6c31948 commit 098b8b8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tools/ngspice/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,43 @@ impl Schematic<Ngspice> for DcVsource {
Ok(())
}
}

/// A current source.
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash, PartialEq, Eq, Block)]
#[substrate(io = "TwoTerminalIo")]
pub enum Isource {
/// A dc current source.
Dc(Decimal),
/// A pulse current source.
Pulse(Pulse),
}

impl Isource {
/// Creates a new DC current source.
pub fn dc(value: Decimal) -> Self {
Self::Dc(value)
}

/// Creates a new pulse current source.
pub fn pulse(value: Pulse) -> Self {
Self::Pulse(value)
}
}

impl ExportsNestedData for Isource {
type NestedData = ();
}

impl Schematic<Ngspice> for Isource {
fn schematic(
&self,
io: &<<Self as Block>::Io as HardwareType>::Bundle,
cell: &mut CellBuilder<Ngspice>,
) -> substrate::error::Result<Self::NestedData> {
let mut prim = PrimitiveBinding::new(Primitive::Isource(*self));
prim.connect("P", io.p);
prim.connect("N", io.n);
cell.set_primitive(prim);
Ok(())
}
}
33 changes: 33 additions & 0 deletions tools/ngspice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::sync::Arc;
use crate::blocks::Vsource;
use crate::tran::Tran;
use arcstr::ArcStr;
use blocks::Isource;
use cache::error::TryInnerError;
use cache::CacheableWithState;
use error::*;
Expand Down Expand Up @@ -45,13 +46,16 @@ pub enum Primitive {
Spice(spice::Primitive),
/// A voltage source with ports "1" and "2".
Vsource(Vsource),
/// A current source with ports "1" and "2".
Isource(Isource),
}

impl Primitive {
fn ports(&self) -> Vec<ArcStr> {
match self {
Primitive::Spice(prim) => prim.ports(),
Primitive::Vsource(_) => vec!["1".into(), "2".into()],
Primitive::Isource(_) => vec!["1".into(), "2".into()],
}
}
}
Expand Down Expand Up @@ -753,6 +757,35 @@ impl HasSpiceLikeNetlist for Ngspice {
}
Ok(name)
}
Primitive::Isource(isource) => {
let name = arcstr::format!("I{}", name);
write!(out, "{}", name)?;
for port in ["P", "N"] {
for part in connections.remove(port).unwrap() {
write!(out, " {}", part)?;
}
}
match isource {
Isource::Dc(dc) => {
write!(out, " DC {}", dc)?;
}
Isource::Pulse(pulse) => {
write!(
out,
" PULSE({} {} {} {} {} {} {} {})",
pulse.val0,
pulse.val1,
pulse.delay.unwrap_or_default(),
pulse.rise.unwrap_or_default(),
pulse.fall.unwrap_or_default(),
pulse.width.unwrap_or_default(),
pulse.period.unwrap_or_default(),
pulse.num_pulses.unwrap_or_default(),
)?;
}
}
Ok(name)
}
}
}
}

0 comments on commit 098b8b8

Please sign in to comment.