I have decided to start migrating my simulations from LTSpice to Kicad/ngspice. Missing the .step command is a hard blow, but scripting options in ngspice should make up for that. However, I cant find out, if the .control section is fully supported in the Kicad interface. I have noticed:
The .control section that I have added to the schematic canvas is regularly removed when I enter the simulator
Several commands are ignored
I have read comments that .control statements loops are not possible
So my question is, is the .control section considered supported in the Kicad/ngspice implementation? Or should I rather adopt a flow where I export my netlist and run ngspice externally when not using very simple simulations?
The handling of ngspice in KiCad is somewhat special:
KiCad is using the ngspice shared library (simulator in a dll, so). The netlist is sent to ngspice.dll upon entering the simulator window, a .control section is executed immediately, upon pushing the Eeschema ‘run’ button a command bg_run is sent to ngspice and the simulation is run in a second thread, data are accessed and plotted during the simulation. User defined signals, fft etc. are started only after the simulation has finished. ngspice.dll has some restrictions, for example it does not have any graphical interface. Graphics is handled by Eeschema. Thus the command ‘plot’ is not available. Simulation capability of ngspice however is fully supported.
That’s the background for any further action.
A command ‘set controlswait’ in the .control section will postpone any following command of the section to after the simulation has finished.
Looping is possible, simulation commands like ‘run’ may be executed. However, the data thus generated, are not visible to the Eeschema graphics facilities (something in need of a change in the future).
Instead of putting the .control section onto the Eeschema canvas, it is possible to just give an ‘.include fipath/filename’ command to load a ngspice script from a file. An example (KiCad6) is given in Simulation examples for KiCad/Eeschema/ngspicePass Labs F5 Class A Audio Amplifier.
Plotting is possible via ngspice command ‘gnuplot tmpfile, vec 1, vec2 …’ , or writing a PS or SVG file. A viewer may be started with a ‘shell …’ command.
Creating loops in plain ngspice is explained in ngspice control language tutorial.
Running external ngspice is supported by KiCad. .control loops may be formulated to support both internal and external simulation, each with its own capabilities. Please have a look at my various KiCad8 simulation examples given in Simulation examples for KiCad8/Eeschema/ngspice .
Do you see different behaviour of .control sections when embedding them in an external file via an .include statement? For example, that they wont be deleted by the Kicad simulator?
Hmm, it certainly happened a few times, when I tried things out yesterday. I will definitely post it, if it happens again. But if it is not a common issue, then Im not too concerned.
If I enter the simulator and press play, then it will open a simulation settings window despite the “.dc” statement on the canvas. When I enter the simulation settings in the simulator, the resulting netlist is:
So it has removed the entire entry - not just the “.dc” statement.
I have tried it a few times now. It appears a bit random if it removes the entry or not, but mostly it seems to be connected to the creation of the simulations settings.
Putting a simulation command onto the canvas, like .dc V1 0 1 0.1
seems to be not really supported anymore since the introduction of the New or Edit Analysis Tabs. The behaviour is inconsistent.
Some sort of synchronization or exclusion (command on the canvas: no tabs are used) should happen.
This is worth another bug report, best with a well formulated ‘specification’ of what should happen.
If the .control section is reliable in KiCAD, then I will try to make it work for me.
I looked through your examples. I cant find an example, that runs multiple simulations and plot them in a single window - similar to the “3. Looping a simulation, altering the supply voltage” in the ngspice control section tutorial. Is there an example for KiCAD, that has this type of loop?
Fiddled around with it. I am experiencing some random behavior. The assignment of variables seems to work sometimes and sometimes not. Also, running multiple simulations does not always update variables between simulations - despite using “set controlswait”
This circuit runs every time, but not always plotting every result.
However. I had warnings the “valr1” was zero length or undefined. I had to create the schematic, close Kicad, then reopen Kicad, reopen the schematic and run the simulation again. This never happens when I run ngspice externally from the created netlist. When I run ngspice externally with the generated netlist, it will always work correctly.