NgspiceTclBridge

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.

Running simulations

::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.

  • Run and wait. ::ngspicetclbridge::run $sim starts the analysis, waits for completion and publishes the buffered samples before returning. This is convenient for parameter sweeps and fitting loops.
  • Run in the background. $sim command bg_run starts ngspice's background worker. The bridge buffers incoming samples and publishes live vector updates on the Tcl thread. A Tk application can use its normal event loop. A script can also call $sim flush to publish pending samples without processing unrelated GUI or timer events.
  • Inspect results and messages. $sim vectors returns a dictionary keyed by ngspice signal name. By default, each value is a Tcl list of samples. Complex samples are represented as {real imaginary} pairs. $sim messages retrieves simulator output and diagnostics.

Example: finding the operating point of a voltage divider

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.

Live plots and independent snapshots

With rbc-tk9 support, the bridge can return real or complex vectors. Choose the interface according to how long the data should live.

  • Live vectors belong to the simulator handle. Create it with -output vector, then use $sim vectors to obtain the vector commands. An RBC graph can bind to them and redraw as samples arrive. $sim vectors -clear empties the vectors while preserving their commands and graph bindings.
  • Snapshots belong to the caller. $sim asyncvector copies one signal, and ::ngspicetclbridge::readVecsAsync copies all signals in the current plot. Snapshots remain available after the next run or after $sim destroy. A list-output handle can also produce vector snapshots.

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]

XSPICE digital events

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.

Installation and supported platforms

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.

Documentation and examples

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.