AC analysis of op-amp loop gain and phase

I’m trying to follow the technique mentioned here to analyze op amp stability: https://www.youtube.com/watch?v=YYWlPFBebfc

M1 has Spice_Primitive BV and Value “V = V(/fb) / V(inm)”

However, running an AC analysis and plotting /fb_div_inm just gives me a flat line. Is this only possible in LTSpice but not ngspice? Is there some way to do it in ngspice? Swapping ‘/’ operator to ‘*’ or ‘+’ works fine, but of course doesn’t give me the fb/inm ratio…

The ngspice B source does not handle the AC data as anticpated by your circuit, because the vectors fb and inm contain complex numbers, and AC simulation means that all devices are linearized, so also the B source. Linearization implies that there is no multiplication of complex numbers.

The eeschema-ngspice interface is currently very limited. So you cannot type in equations for plotting.

ngspice will handle your stuff, but has to be involved externally. Please have a look at http://ngspice.sourceforge.net/ngspice-eeschema.html#external how this might be achieved.

Attached you will find a small eeschema project with an old-fashioned OpAmp, applying the procedure from the Youtube video.
OpAmp-OpenLoop.7z (4.3 KB) .

External ngspice is run with a script that organizes plotting. The 2 meas commands provide the 0dB frequency and the phase reserve.

Thanks! This works fine.

It’d be nice if KiCad made it simpler to use external ngspice. Right now there’s quite a few steps when changing something in the schematic and wanting to re-run the simulation…

  • click “generate netlist” button or Tools>Generate Netlist File
  • click “generate netlist”
  • click “Save”
  • click “Replace”
  • switch to Terminal
  • re-run /Applications/ngspice/bin/ngspice OpAmp_openloop.cir

I couldn’t get the “Run Simulator” button to work.
Perhaps one could have a shortcut to update the netlist file and open ngspice in a Terminal with it?

Even better would be if KiCad could use the results of “plot” command directly, would that be technically possible?

What version of KiCad are you using, what is your OS?

With my Windows or Linux setups I can involve ngspice directly as described in the link above, with macOS currently not (I have to check what is going on there).

Probably not easily. Internal ngspice does not have a plot interface. All data handling has to be provided by Eeschema. And there is no feedback possible if you use external ngspice.

Application: Eeschema
Version: (5.1.5-rc1-4-g47deb8dea), debug build
Libraries:
wxWidgets 3.0.4
libcurl/7.54.0 LibreSSL/2.6.5 zlib/1.2.11 nghttp2/1.24.1
Platform: Mac OS X (Darwin 18.5.0 x86_64), 64 bit, Little endian, wxMac
Build Info:
wxWidgets: 3.0.4 (wchar_t,STL containers,compatible with 2.8)
Boost: 1.69.0
OpenCASCADE Community Edition: 6.9.1
Curl: 7.54.0
Compiler: Clang 9.0.0 with C++ ABI 1002

How does KiCad access the vectors for plotting? Could it be made to ask ngspice about available vectors? I’m thinking that it would be awesome if one could just add control statements like

  let AB=V("/m2")/V("/m1")
  let dbAB=db(AB) 
  let phAB= 180/pi*ph(AB)

to a textbox and have those vectors show up in the internal ngspice viewer.

I found a trick to do this with internal ngspice! :slight_smile:

By adding a global netlabel with the same name as a custom calculated vector, this vector shows up in the signals list so it can be viewed by the internal plot viewer. The netlabel must be connected to a dummy resistor.

The plot won’t update when schematic is changed, so one must close and re-open the plot window, however. Not sure why or how it could be fixed.

Looking at the code, I see eeschema/dialogs/dialog_signal_list.cpp : TransferDataToWindow() calls m_exporter->GetNetIndexMap() to get the available voltages. It would be cool if we could append any vectors found in the ngspice instance that’s not already listed. Alternatively, adding a way to manually add a vector by name.

2 Likes

Issue https://gitlab.com/kicad/code/kicad/-/issues/6221 is related to your last suggestion to have all ngspice signals available for plotting.