ignamv / ngspyce

Python bindings for ngspice simulation engine
https://ignamv.github.io/ngspyce
GNU General Public License v2.0
63 stars 13 forks source link

ngspyce using ngspice.dll under windows ? #5

Closed liubenyuan closed 7 years ago

liubenyuan commented 8 years ago

Hi,

I compile a windows x64 based ngspice.dll for ngspyce and modify line 12 of ngspyce.py as:

spice = CDLL('ngspice.dll')

I placed all ngspice binaries and ngspice.dll in D:/Tools/spice/bin, and set up the PATH environment of windows as D:\Tools\spice\bin. ngspyce can be loaded and it can find the dll!

In order to find spinit file for ngspice, I set SPICE_SCRIPTS of windows to D:\Tools\spice\share\ngspice\scripts, and modify spinit as

* For SPICE2 POLYs, edit the below line to point to the location 
* of your codemodel.

 codemodel D:/Tools/Spice/lib/ngspice/spice2poly.cm

* The other codemodels
 codemodel d:/Tools/Spice/lib/ngspice/analog.cm
 codemodel d:/Tools/Spice/lib/ngspice/digital.cm
 codemodel d:/Tools/Spice/lib/ngspice/xtradev.cm
 codemodel d:/Tools/Spice/lib/ngspice/xtraevt.cm

all the examples, lowpass_filter.py and npn.py work fine ! But it keeps complaining

ERROR:ngspice:Error: Library D:/Tools/Spice/lib/ngspice/spice2poly.cm couldn't be loaded!
ERROR:ngspice:Error: Library d:/Tools/Spice/lib/ngspice/analog.cm couldn't be loaded!
ERROR:ngspice:Error: Library d:/Tools/Spice/lib/ngspice/digital.cm couldn't be loaded!
ERROR:ngspice:Error: Library d:/Tools/Spice/lib/ngspice/xtradev.cm couldn't be loaded!
ERROR:ngspice:Error: Library d:/Tools/Spice/lib/ngspice/xtraevt.cm couldn't be loaded!

the quad.py failed because spice2poly.cm can not be loaded.

Can we solve it and enable ngspyce for windows ?

BTW: when simulating quad.py, ngspice seems to be much slower than LTSpice.

endolith commented 7 years ago

I got 4 examples to work: lowpass_filter.py and npn.py quad.py quadosc,py.

Downloaded ngspice.dll and related files from http://ngspice.sourceforge.net/shared.html

Modified ngspyce.py because of the path issue:

import os
os.chdir('C:/Users/ ... ng_start64_binaries/bin/')
spice = CDLL('ngspice.dll')

also had to install under a Python 3 environment in Anaconda because it uses things like list.clear() that are not in Python 2?

endolith commented 7 years ago

Found this http://stackoverflow.com/a/13277363/125507

and changed my code to this:

if os.name == 'nt':  # Windows
    curr_dir_before = os.getcwd()
    os.chdir('C:/Users/.../ng_start64_binaries/bin/')
    spice = CDLL('ngspice')
    os.chdir(curr_dir_before)
else:
    # Probably Linux?
    spice = CDLL('libngspice.so')

ngspice-26plus-master-32.7z also has ngspice.dll but the folder structure is different?

endolith commented 7 years ago

BTW: when simulating quad.py, ngspice seems to be much slower than LTSpice.

Maybe related to this line of spinit?:

** set the number of threads in openmp
** default (if compiled with --enable-openmp) is: 2
set num_threads=4