[SOLVED]Simulation works well in ngspice 33 but not in Kicad simulator

I drew the following circuit in Eeschema and ran the simulator.


The SPICE netlist looks lie this

.title KiCad schematic
.include "OPA657.lib"
XU1 VEE VCC inv ninv out OPA657
R1 inv 0 1kohm
R2 out inv 99kohm
V2 VEE 0 -5V
V1 VCC 0 5V
V3 ninv 0 DC 0 AC 1
.option noopac 
.ac dec 10 1Hz 100MegHz 
.end

I extracted the netlist and ran in ngspice33 windows version. I get the proper output. I have placed a .spiceinit in my USERPROFILE. So the op amp model does not cause a problem there. However this should also work in Kicad simulator, because of the .spiceinit file being present. But when I try this in simulator, I get the following error.

Circuit: KiCad schematic
Original line no.: 0, new internal line no.: 224:
Undefined number [if]
Original line no.: 0, new internal line no.: 224:
Expression err: if(choice==1,1,0)
Original line no.: 0, new internal line no.: 224:
Formula() error.
choice=1;ibias=2e-12;ioffset=1e-12;ta=25;ibiasdrift=0;ioffsetdrift=0;ibiasp=2.5e-12;ibiasm=1.5e-12;choice1= if(choice==1,1,0);choice2= if(choice==2,1,0);ib= choice1*ibias+choice2*(ibiasp+ibiasm)/2 ;io= choice1*ioffset+choice2*abs(ibiasp-ibiasm);
Original line no.: 0, new internal line no.: 354:
Undefined number [temp]
Original line no.: 0, new internal line no.: 354:
Cannot compute substitute
Original line no.: 0, new internal line no.: 358:
Undefined number [temp]
Original line no.: 0, new internal line no.: 358:
Cannot compute substitute
Original line no.: 0, new internal line no.: 231:
Undefined number [if]
Original line no.: 0, new internal line no.: 231:
Expression err: if(choice==1,1,0)
Original line no.: 0, new internal line no.: 231:
Formula() error.
choice=1;gm=1.51e-01;sbf=1;itailmax_x1=3;itailmax_y1=10e-3;itailmax_x2=5;itailmax_y2=10e-3;itailmin_x1=3;itailmin_y1=10e-3;itailmin_x2=5;itailmin_y2=10e-3;choice1= if(choice==1,1,0);choice2= if(choice==2,1,0);choice3= if(choice==3,1,0);choice11= if(choice==11,1,0);itailmax_slope=(itailmax_y2-itailmax_y1)/(itailmax_x2-itailmax_x1);itailmin_slope=(itailmin_y2-itailmin_y1)/(itailmin_x2-itailmin_x1);itailmax_intcp= itailmax_y1-itailmax_slope*itailmax_x1 ;itailmin_intcp= itailmin_y1-itailmin_slope*itailmin_x1 ;
Original line no.: 0, new internal line no.: 326:
Undefined number [itailmax_slope]
Original line no.: 0, new internal line no.: 326:
Cannot compute substitute
Original line no.: 0, new internal line no.: 330:
Undefined number [itailmin_slope]
Original line no.: 0, new internal line no.: 330:
Cannot compute substitute
Original line no.: 0, new internal line no.: 334:
Undefined number [choice1]
Original line no.: 0, new internal line no.: 334:
Cannot compute substitute
Original line no.: 0, new internal line no.: 537:
Undefined number [temp]
Original line no.: 0, new internal line no.: 537:
Cannot compute substitute
Copies=346 Evals=912 Placeholders=102 Symbols=204 Errors=18
Numparam expansion errors: Problem with input file.
Error: ngspice.dll cannot recover and awaits to be detached
Note: can't find init file.
******
** ngspice-32 shared library
** Creation Date: Fri Jul 10 12:37:34 UTC 2020
******
Error: there aren't any circuits loaded.

Can somebody please help?

For ngspice33 I found that, even the ngspice33 does not recognize the .spicenit in USERPROFILE. However I added
set ngbehavior=ps
in the spinit file. That did the job there. Can someone please tell where can I find spinit in the ngspice attached in kicad.

Your output tells me that you are running ngspice-32 shared library.

What did you do to install ngspice-33 shared library?

How did you find out what is your USERPROFILE?

There is no spinit in shared ngspice within KiCad.

EDIT: To summarize:
ngspice-33 standard executable does not find .spiceinit
ngspice-32 shared lib from within Eeschema does not find .spiceinit.

The file name .spiceinit has to start with a dot. Standard Windows editors sometimes refuse to save such a file. Then please use the file named spice.rc instead (with the same contents).

The .spiceinit has to be placed in a directory pointed to by the environmental variable USERPROFILE (we talk about MS Windows!). To find out which is the directory path stored in USERPROFILE, please open a console window by commands Start->Windows System->Console (or similar, I have here only my German Windows version). Inside the console type the command ‘set’, and you will get a listing of all env variables. Search for USERPROFILE and the assiciated directory path.

1 Like

I did not use Ngspice 33 as a shared library, but rather as the standalone for windows downloaded from
here.I found my USERPROFILE by typing in
$Env:USERPROFILE
in windows powershell. But keeping a .spiceinit with the command
set ngbehavior=ps
in the USERPROFILE did not work in my standalone ngspice33 as well. I had to change the spinit to make it work. How can I implement something like that in Kicad simulator? Or where else where can I keep my .spiceinit so that the ngspice simulator can detect it? I can tell that its not detecting the .spiceinit because by giving the command

set ngbehavior=ps

in my circuit file does the job.

I have three different machines where this is running without any flaw.

Does the Power Shell set its own environmental variables?
For example on my machine here it sets a HOME variable. This HOME however is not visible in the standard console and not visible to ngspice.

So please again: check for USERPROFILE in the standard console.

1 Like

B.t.w. there is some discussion to have this step being done by Eeschema, see https://gitlab.com/kicad/code/kicad/-/issues/5810

1 Like

I checked using the standard console i.e. command prompt using the set command and it shows the same result.
USERPROFILE=C:\Users\LENOVO
However I did observe that instead of .spiceinit the name of the file saved is .spiceinit.txt because I created the file using notepad. Can that cause a problem?

 Directory of C:\Users\LENOVO

20-10-2020  16:40    <DIR>          .
20-10-2020  16:40    <DIR>          ..
20-10-2020  16:37                19 .spiceinit.txt
16-10-2020  00:16    <DIR>          3D Objects
20-10-2020  10:38    <DIR>          Desktop
20-10-2020  10:46    <DIR>          Downloads
20-10-2020  16:41    <DIR>          OneDrive
15-10-2020  21:40    <DIR>          pcbenv
20-10-2020  16:26                17 spice.rc.txt
               2 File(s)             36 bytes
               7 Dir(s)  161,578,782,720 bytes free

Windows 10 by default hides the file extensions. Because I created the files using notepad, it added a .txt extension to it and that made them unrecognizable to Kicad. I enabled viewing the extensions using View–> Options —> Change folder and search options --> View --> Disable ‘Hide extensions for known file types’. Then I removed the files using F2 key and that caused the simulation to run without errors.
Thanks for pointing to the gitlab discussion. I also support an easier way to let Eeschema handle this compatibility issue.