def __init__(self, simulator='ngspice', verbose=True, netlist_path=None, sky130lib=None): ''' Instantiate the object ''' if sky130lib is None: self.sky130lib = os.environ.get('SKY130LIB', 'sky130_fd_pr') else: self.sky130lib = sky130lib # store the setup information internally self.config = {} self.simulation = {} self.config['simulator'] = {'executable': simulator, 'shared': False} self.config['verbose'] = verbose # create an ngspice shared object self.ngspice = NgSpiceShared.new_instance() # if provided read in the base netlist if netlist_path: self.read_netlist_file(netlist_path) self.plot_init = False # define some limits self.limits = {'phase_margin': 45, 'gain_margin': 10}
def __init__(self, verbose=True, netlist_path=None, pdk_path=None): ''' Instantiate the object ''' self.config = {} self.config['simulator'] = { 'executable': 'ngspice', # 'shared' : True, 'shared': False, 'silent': False } self.config['verbose'] = verbose # create an ngspice shared object self.ngspice = NgSpiceShared.new_instance()
import PySpice.Logging.Logging as Logging logger = Logging.setup_logging() from PySpice.Spice.NgSpice.Shared import NgSpiceShared ngspice = NgSpiceShared.new_instance() # print(ngspice.exec_command('version -f')) # print(ngspice.exec_command('print all')) # print(ngspice.exec_command('devhelp')) # print(ngspice.exec_command('devhelp resistor')) circuit = ''' .title Voltage Multiplier .SUBCKT 1N4148 1 2 * R1 1 2 5.827E+9 D1 1 2 1N4148 * .MODEL 1N4148 D + IS = 4.352E-9 + N = 1.906 + BV = 110 + IBV = 0.0001 + RS = 0.6458 + CJO = 7.048E-13 + VJ = 0.869 + M = 0.03 + FC = 0.5 + TT = 3.48E-9
def check_installation(self): """Tool to check PySpice is correctly installed. """ import ctypes.util print('OS:', sys.platform) print() print('Environments:') for _ in ( 'PATH', 'LD_LIBRARY_PATH', 'PYTHONPATH', 'NGSPICE_LIBRARY_PATH', 'SPICE_LIB_DIR', 'SPICE_EXEC_DIR', 'SPICE_ASCIIRAWFILE', 'SPICE_SCRIPTS', 'NGSPICE_MEAS_PRECISION', 'SPICE_NO_DATASEG_CHECK', 'NGSPICE_INPUT_DIR', ): print(_, os.environ.get(_, 'undefined')) print() if 'VIRTUAL_ENV' in os.environ: print('On Virtual Environment:') for _ in ('VIRTUAL_ENV', ): print(_, os.environ.get(_, 'undefined')) print() if 'CONDA_PREFIX' in os.environ: print('On Anaconda:') for _ in ( # not specific 'CONDA_EXE', 'CONDA_PYTHON_EXE', # 'CONDA_SHLVL', # shell level, 1 in conda else 0 # '_CE_CONDA', # empty # specific 'CONDA_DEFAULT_ENV', 'CONDA_PREFIX', # 'CONDA_PROMPT_MODIFIER', ): print(_, os.environ.get(_, 'undefined')) print() try: print('Load PySpice module') import PySpice print('loaded {} version {}'.format(PySpice.__file__, PySpice.__version__)) print() except ModuleNotFoundError: print('PySpice module not found') return import PySpice.Logging.Logging as Logging logger = Logging.setup_logging(logging_level='INFO') from PySpice.Config import ConfigInstall from PySpice.Spice import NgSpice from PySpice.Spice.NgSpice import NGSPICE_SUPPORTED_VERSION from PySpice.Spice.NgSpice.Shared import NgSpiceShared print('ngspice supported version:', NGSPICE_SUPPORTED_VERSION) print() ############################################## message = os.linesep.join(( 'NgSpiceShared configuration is', ' NgSpiceShared.NGSPICE_PATH = {0.NGSPICE_PATH}', ' NgSpiceShared.LIBRARY_PATH = {0.LIBRARY_PATH}', )) print(message.format(NgSpiceShared)) print() ############################################## cwd = Path(os.curdir).resolve() print('Working directory:', cwd) print() locale_ngspice = cwd.joinpath( 'ngspice-{}'.format(NGSPICE_SUPPORTED_VERSION)) if locale_ngspice.exists() and locale_ngspice.is_dir(): print('Found local ngspice:') for root, _, filenames in os.walk(locale_ngspice, followlinks=True): for filename in filenames: print(root, filename) print() ngspice_module_path = Path(NgSpice.__file__).parent print('NgSpice:', ngspice_module_path) for root, _, filenames in os.walk(ngspice_module_path): for filename in filenames: print(root, filename) print() ############################################## if ConfigInstall.OS.on_windows: print('OS is Windows') library = NgSpiceShared.LIBRARY_PATH elif ConfigInstall.OS.on_osx: print('OS is OSX') library = 'ngspice' elif ConfigInstall.OS.on_linux: print('OS is Linux') library = 'ngspice' else: raise NotImplementedError library_path = ctypes.util.find_library(library) print('Found in library search path: {}'.format(library_path)) ############################################## print('\nLoad NgSpiceShared') ngspice = NgSpiceShared.new_instance(verbose=True) if ConfigInstall.OS.on_linux: # For Linux see DLOPEN(3) # Apparently there is no simple way to get the path of the loaded library ... # But we can look in the process maps pid = os.getpid() maps_path = '/proc/{}/maps'.format(pid) with open(maps_path) as fh: for line in fh: if '.so' in line and 'ngspice' in line: parts = [x for x in line.split() if x] path = parts[-1] print('loaded {}'.format(path)) break print() if ngspice.spinit_not_found: print('WARNING: spinit was not found') print() message = os.linesep.join(( 'Ngspice version is {0.ngspice_version}', ' has xspice: {0.has_xspice}', ' has cider {0.has_cider}', )) print(message.format(ngspice)) print() command = 'version -f' print('> ' + command) print(ngspice.exec_command(command)) print() circuit_test = CircuitTest() circuit_test.test_spinit() print('PySpice should work as expected')
def check_installation(self): """Tool to check PySpice is correctly installed. """ import ctypes.util try: print('Load PySpice module') import PySpice print('loaded {} version {}'.format(PySpice.__file__, PySpice.__version__)) except ModuleNotFoundError: print('PySpice module not found') return import PySpice.Logging.Logging as Logging logger = Logging.setup_logging(logging_level='INFO') from PySpice.Config import ConfigInstall from PySpice.Spice.NgSpice.Shared import NgSpiceShared ############################################## message = ''' NgSpiceShared configuration is NgSpiceShared.NGSPICE_PATH = {0.NGSPICE_PATH} NgSpiceShared.LIBRARY_PATH = {0.LIBRARY_PATH} ''' print(message.format(NgSpiceShared)) ############################################## if ConfigInstall.OS.on_windows: print('OS is Windows') library = NgSpiceShared.LIBRARY_PATH elif ConfigInstall.OS.on_osx: print('OS is OSX') library = 'ngspice' elif ConfigInstall.OS.on_linux: print('OS is Linux') library = 'ngspice' else: raise NotImplementedError library_path = ctypes.util.find_library(library) print('Found in library search path: {}'.format(library_path)) ############################################## print('\nLoad NgSpiceShared') ngspice = NgSpiceShared(verbose=True) if ConfigInstall.OS.on_linux: # For Linux see DLOPEN(3) # Apparently there is no simple way to get the path of the loaded library ... # But we can look in the process maps pid = os.getpid() maps_path = '/proc/{}/maps'.format(pid) with open(maps_path) as fh: for line in fh: if '.so' in line and 'ngspice' in line: parts = [x for x in line.split() if x] path = parts[-1] print('loaded {}'.format(path)) break message = ''' Ngspice version is {0.ngspice_version} has xspice: {0.has_xspice} has cider {0.has_cider} ''' print(message.format(ngspice)) command = 'version -f' print('> ' + command) print(ngspice.exec_command(command)) print() print('PySpice should work as expected')
#r# #r# This example explains how to use the NgSpice binding. #r# #################################################################################################### import PySpice.Logging.Logging as Logging logger = Logging.setup_logging() #################################################################################################### from PySpice.Spice.NgSpice.Shared import NgSpiceShared #################################################################################################### ngspice = NgSpiceShared.new_instance() print(ngspice.exec_command('version -f')) print(ngspice.exec_command('print all')) print(ngspice.exec_command('devhelp')) print(ngspice.exec_command('devhelp resistor')) circuit = ''' .title Voltage Multiplier .SUBCKT 1N4148 1 2 * R1 1 2 5.827E+9 D1 1 2 1N4148 * .MODEL 1N4148 D