Skip to content
/ ngspicejs Public

JS frontend to ngspice electronics simulation software

Notifications You must be signed in to change notification settings

dvhx/ngspicejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ngspicejs

JS frontend to ngspice electronics simulation software

Goals

  • use existing ngspice simulation engine
  • better glue language (JS via libv8)
  • better error messages
  • ability to catch stupid errors
  • built-in code linter and beautifier
  • web-search friendly documentation (instead of one 723-page long PDF)
  • overall less frustrating user experience

Example

#!/usr/bin/env ngspicejs
// linter: ngspicejs-lint
"use strict";

// Simple common emitter amplifier 22x
battery('U1', 1, 0, 9);
sinewave('U2', 2, 0).v(0.01).f(196);
var r1 = resistor('R1', 1, 3, '68000');
var r2 = resistor('R2', 3, 0, '4700');
resistor('R3', 1, 4, '20k');
resistor('R4', 6, 0, '330');
resistor('R5', 5, 0, '50k');
capacitor('C1', 2, 3, '10u');
capacitor('C2', 4, 5, '10u');
npn('T1', 4, 3, 6, 'BC547');

// transient analysis, chart, measure gain
var t = tran().run().chart(['V(2)', 'V(5)']);
t.last_chart.gif('hello_transient.gif');
var g = t.gain(2, 5);
echo('gain', g.toFixed(1));

// change R1 and R2 and find maximal gain
var x = [], y = [], gain = [], max = g;
series_e12(10, '1M').forEach(a => {
    series_e12(10, '1M').forEach(b => {
        r1.r(a);
        r2.r(b);
        x.push(a);
        y.push(b);
        g = tran().run().gain(2, 5);
        gain.push(g);
        if (g > max) {
            max = g;
            echo('r1', a, 'r2', b, 'gain', g.toFixed(3));
        }
    });
});

// Show chart and save it as gif
chart_scatter(x, y, gain)
    .title('Common emitter gain depending on R1 and R2')
    .log_x(true)
    .log_y(true)
    .label_x('R1')
    .label_y('R2')
    .size(12)
    .show()
    .gif('hello_gain.gif');

This script will print:

Transient analysis chart printed in terminal

gain 22.5
r1 1000 r2 82 gain 23.259
r1 1200 r2 100 gain 26.096
r1 1500 r2 120 gain 26.986
r1 1800 r2 150 gain 30.305
r1 2200 r2 180 gain 31.201
r1 2700 r2 220 gain 32.232
r1 3300 r2 270 gain 33.296
r1 3900 r2 330 gain 34.940
r1 5600 r2 470 gain 35.260
r1 390000 r2 39000 gain 35.740

Scatter chart of gain depending on R1 and R2

Requirements

sudo apt-get install ngspice ngspice-dev libnode-dev libasound2-dev libreadline-dev

Compilation

In ngspicejs directory run:

make clean all

Once compiled create ~/bin folder, add it to PATH environment variable (e.g. in ~/.profile, this may require to restart the terminal) and then as a normal user run:

make install

It will create symlinks in ~/bin that will points to ngspicejs, ngspicejs-linter and ngspicejs-beautify. Test it:

./hello.ngjs

If you don't see charts enable SIXEL support in your terminal.

Sixel support (graphics in terminal)

To allow displaying sixel graphics in xterm, you need to enable sixel support first:

echo "XTerm*decTerminalID: vt340" >> $HOME/.Xresources
echo "XTerm*numColorRegisters: 256" >> $HOME/.Xresources
echo "XTerm*disallowedWindowOps: 1,2,3,4,5,6,7,8,9,11,13,19,20,21,GetSelection,SetSelection,SetWinLines,SetXprop" >> $HOME/.Xresources
xrdb $HOME/.Xresources

API

ngspicejs API is described in separate file. In code you can use api(anything) to print API of anything, for example:

api(resistor);
api(resistor());
api(globalThis);
api(tran);
api(tran());

Help

You can use built-in function help(query) to perform full text searches, for example:

help('how to measure gain');
help('how to do fft of an ammeter');

More examples

Support

You can support development on Patreon or you can hire me via Upwork

About

JS frontend to ngspice electronics simulation software

Resources

Stars

Watchers

Forks

Packages

No packages published