@typecad/ngspice v0.1.3
ngspice Simulation for typeCAD
Use typeCAD to design your hardware and run simulations with ngspice with the same codebase.
Getting Started
Prerequisites
Installation
Run the following command in your project directory, where your typeCAD package.json file is located:
npm i @typecad/ngspice
Usage
Below is a simple voltage-divider circuit.
import { Schematic, Power } from '@typecad/typecad';
import { Resistor } from '@typecad/passives/0603';
import { ngspiceSimulator } from '@typecad/ngspice';
let typecad = new Schematic('vdiv');
let r1 = new Resistor({ value: '1k', simulation: { include: true } });
let r2 = new Resistor({ value: '1k', simulation: { include: true } });
let vin = new Power({ power: r1.pin(1), gnd: r2.pin(2), voltage: 3.3 });
let ngspice = new ngspiceSimulator(typecad, vin);
typecad.named('in').net(r1.pin(1));
typecad.named('vdiv').net(r1.pin(2), r2.pin(1));
typecad.named('gnd').net(r2.pin(2));
typecad.create(r1, r2);
ngspice.op();
Notes
- The
ngspiceSimulator
constructor takes a schematic and all power source as parameters, ie. if more than one power source is used, they can all be passed.
let ngspice = new ngspiceSimulator(typecad, battery, regulated_3v3);
- to include a component, fill the
simulation
property of the component with{ include: true }
ensure the
value
field contains a valid ngspice expression (no spaces, proper units, etc.).only
named
nets are included in the simulationgnd
is a special net in ngspice and is associated with ground or0
DC Analysis
- to run a DC analysis, call the
op
method on the simulator
ngspice.op();
The output from the above example is:
š¶ļø running ngspice
āāāāāāāāāāāāāāāāāāāāāā¬āāāāāāāāāāāāāāāā¬āāāāāāāāāāāāāāāāāāāāā
ā Variable ā Type ā Value ā
āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāā¤
ā r1:power ā power ā 2.7225 mW ā
āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāā¤
ā v(in) ā voltage ā 3.3000 V ā
āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāā¤
ā i(r1) ā current ā 1.6500 mA ā
āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāā¤
ā i(r2) ā current ā 1.6500 mA ā
āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāā¤
ā r2:power ā power ā 2.7225 mW ā
āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāā¤
ā i(v1) ā current ā -1.6500 mA ā
āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāā¤
ā v(vdiv) ā voltage ā 1.6500 V ā
āāāāāāāāāāāāāāāāāāāāāā“āāāāāāāāāāāāāāāā“āāāāāāāāāāāāāāāāāāāāā
Each component is listed with its power and current use and each node is listed with its voltage.
Transient Analysis
- To run a transient analysis, call the
tran
method on the simulator
ngspice.tran('1us', '100ms');
There are additional parameters that can be passed to the tran
method and are explained best in the ngspice documentation, section 11.3.10 .TRAN: Transient Analysis.
The output from the above example opens an ngspice plot window for each component and all nodes.