Installing and using ngspice - an opensource simulator

Member
Joined 2011
Paid Member
Dear Holger,

the "write" patch to ignore zero sized current vectors
astx_ngspice_write_ignore_currents.diff
is included in zip file from post #30.

I'm not sure, if this is the correct way to fix this problem, but it works for me. Maybe you can hav a look on it. Of course you are right - we should post patches to ngspice/patches!

Sorry for the confusion.

BR, Toni
 
Member
Joined 2011
Paid Member
... using the adi.lib from ltspice we get 78 usable opamp models. Here the open loop gain results...
opamp stepping to the max ... :) 3 results shown here for example

Code:
 toni@tnserver01:~/pcb/simulations/ngspice/opamp> cat step_opamps_loopgain.sh    
#!/bin/bash
#
# (c) astx (at) aws-it.at, 2018 - 2019
# and (c) others - see DIYaudio thread [URL="https://www.diyaudio.com/forums/software-tools/331656-installing-using-ngspice-opensource-simulator.html"]Installing and using ngspice - an opensource simulator[/URL]
#
# $Id: step_opamp_devices.sh 1579 2019-01-17 11:34:34Z sta $
#
echo ""; echo ""
echo "#-----------------------------------------------------------------------"
echo "NOTE: For successful run you need at least ngspice 30+ pre-master"
echo "    astx_ngspice_write_ignore_currents.diff"
echo "After simulation run you can further explore the result data"
echo "Use 'quit' to exit simulation"
echo ""
echo "#-----------------------------------------------------------------------"
echo "If you want to run opamp stepping simulation now enter 'y' <ENTER>"
echo "#-----------------------------------------------------------------------"

read a
if [ "$a" == "y" ]; then

        # change this to your preferred editor. vi nano joe ...
        export SPICE_EDITOR="joe"

        # temporary net list file
        mynet="opamp_loopgain.net"


        chead="\n.control\n"
        cend="\n.endc\n"

        echo -en "${chead}" > load_result_files0.inc
        echo -en "${chead}" > load_result_files1.inc
        echo -en "${chead}" > load_result_files2.inc

        tianplots=''
        count=1
        for opamp in $(cat adi_opamps.lst)
        do
                opamp=$(echo $opamp|tr 'A-Z' 'a-z')
                # load schematic and create a fresh netlist
                gnetlist -g spice-sdb -o $mynet opamp_loopgain.sch
                # replace %MYOPA% from schematic by $opamp
                sed -i -r -e "s/\%MYOPA\%/${opamp}/g" $mynet
                # e.g. add/set some circuit params or options be4 simulation run
                echo -en "\n*.options savecurrents\n.control\n\tset opamp="${opamp}"\n\techo "OPA \$opamp"\n.endc\n\n" > $mynet.inc
                ngspice -i $mynet.inc $mynet
                # generate load result files listing
                echo "load ngspice_ac_${opamp}.data" >> load_result_files1.inc
                echo "load ngspice_vars_${opamp}.data" >> load_result_files2.inc
                tianplots="${tianplots}ac${count} "
                count=$(($count+1))
        done

        echo -en "${cend}" >> load_result_files1.inc
        echo -en "${cend}" >> load_result_files2.inc
        echo -en "set tianplots=( ${tianplots})${cend}" >> load_result_files0.inc

        # load (no netlist!) results only from stepping and display e.g. fft, fourier and input, output voltages
        ngspice opamp_loopgain_show.ngspice

        # reset terminal if ngspice terminates unexpectedly
        reset
fi
 

Attachments

  • opamp_loopgain_adi_lib.zip
    513.9 KB · Views: 66
  • ad_opamps_adi_lib.txt
    542 bytes · Views: 56
  • opamp_openloop_gain_ac1_device=ad795_result.png
    opamp_openloop_gain_ac1_device=ad795_result.png
    7.9 KB · Views: 166
  • opamp_openloop_gain_ac17_device=ad8538_result.png
    opamp_openloop_gain_ac17_device=ad8538_result.png
    6.1 KB · Views: 160
  • opamp_openloop_gain_ac78_device=op77_result.png
    opamp_openloop_gain_ac78_device=op77_result.png
    7.5 KB · Views: 161
Member
Joined 2011
Paid Member
... another 48 opamps ex adi1.lib from ltspice working with ngspice ...
some examples shown here ...
Schematic which has been used for open loop gain simulations.


BR, Toni
 

Attachments

  • ad_opamps_adi1_lib.txt
    334 bytes · Views: 60
  • opamp_loopgain_adi1_lib.zip
    323.3 KB · Views: 57
  • opamp_openloop_gain_ac1_device=ad549_result.png
    opamp_openloop_gain_ac1_device=ad549_result.png
    7.1 KB · Views: 163
  • opamp_openloop_gain_ac13_device=ad8508_result.png
    opamp_openloop_gain_ac13_device=ad8508_result.png
    7.1 KB · Views: 154
  • opamp_openloop_gain_ac43_device=op4177_result.png
    opamp_openloop_gain_ac43_device=op4177_result.png
    6.8 KB · Views: 74
  • opamp_loopgain_schematic.png
    opamp_loopgain_schematic.png
    144.8 KB · Views: 86
Last edited:
Member
Joined 2011
Paid Member
Just tested the ltspice netlist of sa2015 - with some editing rules it should be possible to nearly natively use the interim generated ".net" file when you do at least an "op" run.
Using a small helper script it should be possible to use ltspice as schematic editor only ...
(e.g.: the VDMOS devices in LTSpice have 4 connections whereas in ngspice they have 3 connections - the substrat connection needs to be removed)

BR, Toni
 
Member
Joined 2011
Paid Member
ngspice has no variable string handling like substring or similar (or I can't simply find it)

We can extend the limited ngspice string handling using a scripting language via the "shell" command to do some simple string operations like get value from a key/value pair or upper/lowercase a string.

An example
Code:
.control
        * NOTE: this needs a ngspice patch to get nice png output where gnuplot makes a auto exit
        set gnuplot_terminal=png/quit
        foreach pl $tianplots
                setplot $pl

                * get curplottitle and extract device name using a shell command which splits key/value pair by '='
                *                   <cmd> <resultfile> <new_varname> <input_data>
                shell ./shellcmd.sh getval getval.result nolc_dev "$curplottitle"
                * run the generated ngspice script to feed the set variable
                getval.result
                shell ./shellcmd.sh uc uc.result nolc_ucdev "$nolc_dev"
                uc.result
                echo "OPA=$nolc_ucdev"
                gnuplot opamp_openloop_gain_{$pl}_{$nolc_dev}_result vdb(av) phase ylimit -200 200
        end
...
The shell helper "shellcmd.sh " could be in any scripting language e.g. a shell script:
Code:
#!/bin/bash

if [ "$1" == "" ]; then
        echo "Warning: need 4 parameters: <cmd> <resultfile> <new_varname> <input_data>"
        exit
fi

cmd="$1"
resultfile="$2"
varname="$3"
data="$4"
echo "cmd=<$cmd> resultfile=<$resultfile> new_varname=<$varname> input_data=<$data>"
case $cmd in
        getval)
                result=$(echo ${data}|cut -d'=' -f2-99999999)
                ;;
        getkey)
                result=$(echo ${data}|cut -d'=' -f1)
                ;;
        lc)
                result=$(echo ${data}|tr 'A-Z' 'a-z')
                ;;
        uc)
                result=$(echo ${data}|tr 'a-z' 'A-Z')
                ;;
        *)
                echo "No such command <$cmd>!"
                exit
                ;;
esac
echo -en "\n.control\nset ${varname}="${result}"\n.endc\n" > $resultfile
In my example I use the shell helper to extract the value from the loaded plot data header:
Code:
shell ./shellcmd.sh getval getval.result nolc_dev "$curplottitle"
Code:
Title: device=ada4627
Date: Tue Jan 29 17:03:55  2019
Plotname: ac data
Flags: complex
No. Variables: 3
No. Points: 2088
Variables:
        0       frequency       frequency grid=3
        1       av      notype
        2       phase   notype
Example output in ngspice
Code:
cmd=<uc> resultfile=<uc.result> new_varname=<nolc_ucdev> input_data=<op747>

Circuit: 

OPA=OP747
writing plot to file opamp_openloop_gain_ac47_op747_result.plt
cmd=<getval> resultfile=<getval.result> new_varname=<nolc_dev> input_data=<device=op777>

Circuit: 

cmd=<uc> resultfile=<uc.result> new_varname=<nolc_ucdev> input_data=<op777>

Circuit: 

OPA=OP777
writing plot to file opamp_openloop_gain_ac48_op777_result.plt
Unfortunately ngspice auto-converts variable names AND content if we source them in the generated small script. A patch helps to avoid this, if the variable name is prefixed using 'nolc_' .

A second patch allows me to run gnuplot in batch mode without showing any x11-window to silently generate png graphs in a loop.

Attached 3 small ngspice patches which I currently need against ngspice-30+ pre-master. Maybe Holger can have a look into it to implement the patches.

  • ngspice_add_gnuplot_png_batch_option.diff
  • ngspice_allow_mixed_case_vars_using_prefix.diff
  • astx_ngspice_write_ignore_currents.diff
BR, Toni
 

Attachments

  • astx_ngspice_patches.zip
    2 KB · Views: 56
Last edited:
ngspice already has a LTSPICE compatibility mode. See chapt. 16.13.6.

Currently only the simple diode is supported.

I would add fixes to other differences directly in the source code at inpcom.c. Then you won't need any script. What are these netlist differences? Could you give me a list? So it is

VDMOS has 4 terminals (3 and 4 the same node)

What else ?
 
Member
Joined 2011
Paid Member
Dear Holger,

attached a perl converter script, a complete amplifier project and some output logs to get a feeling, how much action is necessary to get a ltspice netlist running.

Some explanations:

  • ako_problems.txt: this ako let the ngspice die.
    I have had to disable the ako models in kean9...txt

  • convert.log: result of my perl script - self explaining
Start the simulation with "ngspice tran_test.ngspice"
You get some errors of unknown numbers because many libs contain a "mfg=string" parameter in the model definition. Ignore this with "y". The simulation drops additional warnings about unknown parameters but succeeds.

keantoken: maybe you want to try one of your projects too. ltspice "op" run generates the netlist - as long as the op result window is open, the temporary netlist should be on disk available,

BR, Toni
 

Attachments

  • ltspice_to_ngspice.zip
    109.6 KB · Views: 71
There is also the Quite Universal Circuit Simulator "QUCS-Studio" that is not spice but seems useful.

< QucsStudio Homepage >

I have played around somewhat and I kinda liked it. I have no very deep experience with it. It looks like it can inter-operate with ng-spice, Spice-opus and uses octave as post processor after simulation. It can import spice models and Verilog-A and can export to KICAD.

The style is more like ADS and it is more RF-oriented. QUCS without -studio seems to be an outdated version.

It can do harmonic balance simulation, required for nonlinear noise, that's what interested me. The interoperability with spice could be more interesting for the people here. Installation is easy. There is none. Just unzip the file somewhere and call up the .exe.

regards, Gerhard
 
Last edited:
Member
Joined 2011
Paid Member
Dear Gerhard,


thx for the Information but it seems to be off topic. Can't find any information that qucsstudio is using ngspice in any way. Also it is not opensource and only available for Windows - see this statement from qucsstudio homepage:
QucsStudio is a Qucs fork that stems from the original project founder. It's freely available for Windows and not open-source. But it offers by far the most features and is updated regularly.
BTW: Qucs without "studio" is an active project, open source, supports ngspice and xyce - have a look at source code mirror: GitHub - Qucs/qucs: Qucs Project official mirror

BR, Toni
 
Last edited:
concerning ako

The PSPICE manual says:
Code:
.MODEL <model name> [AKO: <reference model name>]
+ <model type>
plus other optional parameters. <model type> is required, but missing in kean9.bjt.txt, where I see only
Code:
.model MMBT6429 AKO:MPSA18_kn
with model type npn missing.

LTSPICE seems to treat this differently, as found in Undocumented LTspice - LTwiki-Wiki for LTspice

o.k., this requires an LTSPICE update to ngspice.
 
Have you checked that the AKO statement works in LTspice? I may have made a mistake here, I seem to remember encountering the problem. Might be different in different versions of LTspice.

EDIT:

Okay, this is interesting. The MMBT6429 AKO statement works when placed on a schematic but does not appear in the drop down transistor list when selecting a model. So whether it is my mistake or an LTspice feature is open to interpretation, but for full compatibility the NPN card needs to be added to to the AKO statement when it is in a library.
 
Last edited:
Hmm. This is the only syntax which shows up in the transistor selector:

.model MMBT6429 NPN AKO:MPSA18_kn

Notice the NPN card comes before the AKO card, not after.

If the NPN card goes after the AKO card, it is interpreted as modifying the original model, IE if you use PNP it will override NPN and change the polarity of the transistor. But the LTspice model selector ignores models that don't include the NPN card before the AKO statement, which I suspect is an oversight, so I will report it to the LTspice developer.
 
Just to understand:
So
.model MMBT6429 NPN AKO:MPSA18_kn

is the correct notation? And not the one you have used in kean9.bjt.txt or one can find in Undocumented LTspice - LTwiki-Wiki for LTspice, that is

.model MMBT6429 AKO:MPSA18_kn

Or are both of the above allowed? If this becomes clear, I indeed might add a LTSPICE switch to read both and translate them to the PSPICE notation used in ngspice.

Well, frivolous would be adding something without having the spec clarified.