NgspiceTclBridge is a Tcl 9 C extension for running ngspice as a shared library inside a Tcl application. A script can supply a SPICE netlist, issue simulator commands and read results directly from memory. It can wait for an analysis to finish or let ngspice run in the background while a Tk application displays incoming samples. The Tcl package name is ngspicetclbridge.
::ngspicetclbridge::new loads the ngspice shared library and returns a simulator handle. Use $sim circuit -string to load a multiline netlist, or $sim circuit to pass a Tcl list containing one line per element. A handle can be reused: loading a new circuit removes the previous one.
Two equal resistors divide a 2 V supply, giving 1 V at node out. The example loads the circuit from a Tcl string, runs an operating-point analysis, and reads the output voltage. Set library to the filename of your installed ngspice shared library.
package require ngspicetclbridge
set library /path/to/libngspice.so
set sim [::ngspicetclbridge::new $library]
try {
$sim circuit -string {Voltage divider
V1 in 0 2
R1 in out 1k
R2 out 0 1k
.op
.end}
::ngspicetclbridge::run $sim
set samples [$sim asyncvector out -output list]
puts [format {V(out) = %.3f V} [lindex $samples 0]]
# V(out) = 1.000 V
} finally {
$sim destroy
}asyncvector copies one signal from the current ngspice plot. To inspect the signal names collected for a circuit, use dict keys $sim vectors while the handle is open.
With rbc-tk9 support, the bridge can return real or complex vectors. Choose the interface according to how long the data should live.
For example, the following can replace the sample-reading lines in the voltage-divider example. It saves every signal under a fresh vector name in ::results. Keep the dictionary to use the waveforms elsewhere, then destroy the vectors when finished.
set saved [::ngspicetclbridge::readVecsAsync -output vector -namespace ::results -names #auto $sim]
dict for {signal vector} $saved {
puts [format {%s: %d samples} $signal [$vector length]]
}
::rbc::vector destroy {*}[dict values $saved]For mixed-signal simulations, the bridge can collect digital node states separately from analog voltages and currents. After loading an XSPICE circuit, select its digital nodes with $sim eventnodes, run the simulation, and retrieve the history with $sim eventdata. Digital values retain ngspice's logic-state and drive-strength strings.
For example, $sim eventnodes -nodes {clk out} -mode changes records event reports for two nodes. Use snapshot for a table of node states at each accepted transient output point, or pernode for separate node histories. This feature requires the ngspice raw event API and the code models used by the circuit.
NgspiceTclBridge 0.5 requires Tcl 9, argparse 0.70 or later, Tcllib's textutil::adjust package, and an ngspice shared library. Pass the library filename to new: typically libngspice.so on Linux, ngspice.dll on Windows, or libngspice.dylib on macOS. The simulator is installed separately from the bridge.
The rolling release provides installation archives and SHA-256 checksums for Linux GCC, Windows MSYS2/UCRT64 and Windows MSVC x64. These configurations are tested with Tcl 9.1.0 and RBC vector support.
For a source build on Linux or in an MSYS2 UCRT64 shell:
./configure make make install
The default build supports Tcl list output. Add --with-rbc=/path/to/rbc for live vectors and vector snapshots, using RBC 0.8.1 or later. Use --with-tcl=/path/to/tcl/lib to select a Tcl installation. For MSVC, use the supplied win/makefile.vc.
Public commands also provide interactive help:
package require ngspicetclbridge ::ngspicetclbridge::new -help ::ngspicetclbridge::run -help
See also SpiceGenTcl, which uses the bridge for its shared ngspice backend, and tclsimrawreader for reading results saved to SPICE raw files.