PySpice-org / PySpice

Simulate electronic circuit using Python and the Ngspice / Xyce simulators
https://pyspice.fabrice-salvaire.fr
GNU General Public License v3.0
641 stars 170 forks source link

Unable to use external current source with ngspice_shared #255

Open krillin666 opened 3 years ago

krillin666 commented 3 years ago

Hello, I am trying to plug an external current source into a circuit I made with Pyspice. I first wanted to test the one in this example. I modified the code such that the plug source was a current one and not a voltage. However I haven't had any success because every attempt I make to solve I get the error "parameter value out of range or the wrong type".

I thought that maybe the current values were not being generated properly and as such were causing the given error. However, when I added print statements to the MyNgSpiceShared class I noticed (see output bellow) that the get_isrc_data function is not even called.

Output

BEFORE FUNCTIONS Inside init 2020-09-08 17:22:13,453 - PySpice.Spice.NgSpice.Shared.NgSpiceShared.load_circuit - ERROR - Circuit: Current Divider Error on line 4 : iinput 1 0 dc 0 external parameter value out of range or the wrong type Traceback (most recent call last):

The code I am using is bellow:

import math

import matplotlib.pyplot as plt

import PySpice.Logging.Logging as Logging
logger = Logging.setup_logging()
import locale
locale.setlocale(locale.LC_NUMERIC, 'C')
from PySpice.Probe.Plot import plot
from PySpice.Spice.Netlist import Circuit
from PySpice.Spice.NgSpice.Shared import NgSpiceShared
from PySpice.Unit import *

class MyNgSpiceShared(NgSpiceShared):
    print("BEFORE FUNCTIONS")

    def __init__(self, amplitude, frequency, **kwargs):

        super().__init__(**kwargs)

        self._amplitude = amplitude
        self._pulsation = float(frequency.pulsation)
        print("Inside __init__")

    def get_vsrc_data(self, voltage, time, node, ngspice_id):
        self._logger.debug('ngspice_id-{} get_vsrc_data @{} node {}'.format(ngspice_id, time, node))
        print("INSIDE get_vsrc_data")
        return 0

    def get_isrc_data(self, current, time, node, ngspice_id):

        print("INSIDE get_isrc_data")
        self._logger.debug('ngspice_id-{} get_isrc_data @{} node {}'.format(ngspice_id, time, node))
        current[0] = self._amplitude * math.sin(self._pulsation * time)

        print(current)
        return 0

circuit = Circuit('Current Divider')

amplitude = u_A(10)
frequency = 50@u_Hz
circuit.R(1, 1, circuit.gnd, 2@u_kΩ)
circuit.R(2, 1, circuit.gnd, 1@u_kΩ)

circuit.I('input', 1, circuit.gnd, "dc 0 external") # Fixme: current value
for resistance in (circuit.R1, circuit.R2):
    resistance.minus.add_current_probe(circuit) # to get positive value

ngspice_shared = MyNgSpiceShared(amplitude=amplitude, frequency=frequency, send_data=False)
simulator = circuit.simulator(temperature=25, nominal_temperature=25,
                              simulator='ngspice-shared', ngspice_shared=ngspice_shared)
period = float(frequency.period)
analysis = simulator.transient(step_time=period/200, end_time=period*2)

Thank you for your time.

thejackal360 commented 2 years ago

This appears to be an ngspice issue. I fixed the bug on SourceForge and made a merge request:

https://sourceforge.net/p/ngspice/ngspice/merge-requests/13/

Try this fix. Let me know if you need any help compiling libngspice.so from source.

justincui commented 1 year ago

Issue: cannot run this example in windows 10 : https://pyspice.fabrice-salvaire.fr/releases/v1.5/examples/ngspice-shared/external-source.html?highlight=external#

###################### environment (windows10, anaconda) #######################

>python --version
Python 3.9.16

>conda list | grep spice
packages in environment at Anaconda3\envs\spice:
ngspice                   38                   h57928b3_0    conda-forge
ngspice-exe               38                   h295a5eb_0    conda-forge
ngspice-lib               38                   h295a5eb_0    conda-forge
pyspice                   1.5              py39hcbf5309_1    conda-forge

###############################To reproduce #####################################

  1. In ipython, run the python code of the example directly. It just prints the following output and then ipython quits silently: 2023-02-06 12:03:37,546 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.WARNING - Unsupported Ngspice version 38
  2. add logger.setLevel('DEBUG') and run the code in pycharm. The output is:
    
    Anaconda3\envs\spice\python.exe x:/bbb/main.py
    2023-02-06 12:15:14,798 - PySpice.Spice.Netlist - Netlist.DEBUG - Element don't define a PINS attribute
    2023-02-06 12:15:14,798 - PySpice.Spice.Netlist - Netlist.DEBUG - FixedPinElement don't define a PINS attribute
    2023-02-06 12:15:14,799 - PySpice.Spice.Netlist - Netlist.DEBUG - SubCircuitElement don't define a PINS attribute
    2023-02-06 12:15:14,799 - PySpice.Spice.Netlist - Netlist.DEBUG - Resistor don't define a PINS attribute
    2023-02-06 12:15:14,802 - PySpice.Spice.Netlist - Netlist.DEBUG - SemiconductorResistor don't define a PINS attribute
    2023-02-06 12:15:14,802 - PySpice.Spice.Netlist - Netlist.DEBUG - BehavioralResistor don't define a PINS attribute
    2023-02-06 12:15:14,803 - PySpice.Spice.Netlist - Netlist.DEBUG - Capacitor don't define a PINS attribute
    2023-02-06 12:15:14,803 - PySpice.Spice.Netlist - Netlist.DEBUG - SemiconductorCapacitor don't define a PINS attribute
    2023-02-06 12:15:14,803 - PySpice.Spice.Netlist - Netlist.DEBUG - BehavioralCapacitor don't define a PINS attribute
    2023-02-06 12:15:14,803 - PySpice.Spice.Netlist - Netlist.DEBUG - Inductor don't define a PINS attribute
    2023-02-06 12:15:14,803 - PySpice.Spice.Netlist - Netlist.DEBUG - BehavioralInductor don't define a PINS attribute
    2023-02-06 12:15:14,805 - PySpice.Spice.Netlist - Netlist.DEBUG - CoupledInductor don't define a PINS attribute
    2023-02-06 12:15:14,805 - PySpice.Spice.Netlist - Netlist.DEBUG - VoltageControlledSwitch don't define a PINS attribute
    2023-02-06 12:15:14,805 - PySpice.Spice.Netlist - Netlist.DEBUG - CurrentControlledSwitch don't define a PINS attribute
    2023-02-06 12:15:14,805 - PySpice.Spice.Netlist - Netlist.DEBUG - VoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,806 - PySpice.Spice.Netlist - Netlist.DEBUG - CurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,806 - PySpice.Spice.Netlist - Netlist.DEBUG - VoltageControlledCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,806 - PySpice.Spice.Netlist - Netlist.DEBUG - VoltageControlledVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,806 - PySpice.Spice.Netlist - Netlist.DEBUG - CurrentControlledCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,806 - PySpice.Spice.Netlist - Netlist.DEBUG - CurrentControlledVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,807 - PySpice.Spice.Netlist - Netlist.DEBUG - BehavioralSource don't define a PINS attribute
    2023-02-06 12:15:14,807 - PySpice.Spice.Netlist - Netlist.DEBUG - NonLinearVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,807 - PySpice.Spice.Netlist - Netlist.DEBUG - NonLinearCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,807 - PySpice.Spice.Netlist - Netlist.DEBUG - JunctionFieldEffectTransistor don't define a PINS attribute
    2023-02-06 12:15:14,808 - PySpice.Spice.Netlist - Netlist.DEBUG - Mesfet don't define a PINS attribute
    2023-02-06 12:15:14,808 - PySpice.Spice.Netlist - Netlist.DEBUG - LosslessTransmissionLine don't define a PINS attribute
    2023-02-06 12:15:14,808 - PySpice.Spice.Netlist - Netlist.DEBUG - LossyTransmission don't define a PINS attribute
    2023-02-06 12:15:14,809 - PySpice.Spice.Netlist - Netlist.DEBUG - CoupledMulticonductorLine don't define a PINS attribute
    2023-02-06 12:15:14,810 - PySpice.Spice.Netlist - Netlist.DEBUG - SingleLossyTransmissionLine don't define a PINS attribute
    2023-02-06 12:15:14,810 - PySpice.Spice.Netlist - Netlist.DEBUG - XSpiceElement don't define a PINS attribute
    2023-02-06 12:15:14,810 - PySpice.Spice.Netlist - Netlist.DEBUG - GSSElement don't define a PINS attribute
    2023-02-06 12:15:14,827 - PySpice.Spice.Netlist - Netlist.DEBUG - SinusoidalVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,827 - PySpice.Spice.Netlist - Netlist.DEBUG - SinusoidalCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,827 - PySpice.Spice.Netlist - Netlist.DEBUG - AcLine don't define a PINS attribute
    2023-02-06 12:15:14,827 - PySpice.Spice.Netlist - Netlist.DEBUG - PulseVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,827 - PySpice.Spice.Netlist - Netlist.DEBUG - PulseCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,827 - PySpice.Spice.Netlist - Netlist.DEBUG - ExponentialVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,828 - PySpice.Spice.Netlist - Netlist.DEBUG - ExponentialCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,828 - PySpice.Spice.Netlist - Netlist.DEBUG - PieceWiseLinearVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,828 - PySpice.Spice.Netlist - Netlist.DEBUG - PieceWiseLinearCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,828 - PySpice.Spice.Netlist - Netlist.DEBUG - SingleFrequencyFMVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,828 - PySpice.Spice.Netlist - Netlist.DEBUG - SingleFrequencyFMCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,829 - PySpice.Spice.Netlist - Netlist.DEBUG - AmplitudeModulatedVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,829 - PySpice.Spice.Netlist - Netlist.DEBUG - AmplitudeModulatedCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,829 - PySpice.Spice.Netlist - Netlist.DEBUG - RandomVoltageSource don't define a PINS attribute
    2023-02-06 12:15:14,829 - PySpice.Spice.Netlist - Netlist.DEBUG - RandomCurrentSource don't define a PINS attribute
    2023-02-06 12:15:14,936 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.DEBUG - locale LC_NUMERIC is not forced to C
    2023-02-06 12:15:15,006 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.DEBUG - Load library C:\Users\justin.cui\Anaconda3\envs\spice\lib\site-packages\PySpice\Spice\NgSpice\Spice64_dll\dll-vs\ngspice.dll
    2023-02-06 12:15:15,104 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ******'
    2023-02-06 12:15:15,105 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** ngspice-38 shared library'
    2023-02-06 12:15:15,105 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** Creation Date: Nov  1 2022   12:49:46'
    2023-02-06 12:15:15,105 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ******'
    2023-02-06 12:15:15,111 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.DEBUG - Execute command: version -f
    2023-02-06 12:15:15,111 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ******'
    2023-02-06 12:15:15,111 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** ngspice-38 : Circuit level simulation program'
    2023-02-06 12:15:15,111 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** The U. C. Berkeley CAD Group'
    2023-02-06 12:15:15,111 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** Copyright 1985-1994, Regents of the University of California.'
    2023-02-06 12:15:15,111 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** Copyright 2001-2022, The ngspice team.'
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** Please get your ngspice manual from http://ngspice.sourceforge.net/docs.html'
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** Please file your bug-reports at http://ngspice.sourceforge.net/bugrep.html'
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** Creation Date: Nov  1 2022   12:49:46'
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout **'
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** CIDER 1.b1 (CODECS simulator) included'
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** XSPICE extensions included'
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b"stdout ** Relevant compilation options (refer to user's manual):"
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** OpenMP multithreading for BSIM3, BSIM4 enabled'
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** X11 interface not compiled into ngspice'
    2023-02-06 12:15:15,112 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout **'
    2023-02-06 12:15:15,113 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ** ngspice shared library.'
    2023-02-06 12:15:15,113 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout ******'
    2023-02-06 12:15:15,113 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.DEBUG - Ngspice version 38 with extensions: CIDER, XSPICE
    2023-02-06 12:15:15,113 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.WARNING - Unsupported Ngspice version 38
    2023-02-06 12:15:15,113 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.DEBUG - Execute command: set nomoremode
    2023-02-06 12:15:15,140 - PySpice.Spice.NgSpice.Simulation.NgSpiceSharedCircuitSimulator - Simulation.DEBUG - desk
    .title Voltage Divider
    Vinput input 0 dc 0 external
    R1 input output 10kOhm
    R2 output 0 1kOhm
    .options TEMP = 25C
    .options TNOM = 25C
    .tran 0.0001s 0.04s 0s
    .end

2023-02-06 12:15:15,140 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.DEBUG - Execute command: destroy all 2023-02-06 12:15:15,140 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.DEBUG - ngSpice_Circ .title Voltage Divider Vinput input 0 dc 0 external R1 input output 10kOhm R2 output 0 1kOhm .options TEMP = 25C .options TNOM = 25C .tran 0.0001s 0.04s 0s .end

2023-02-06 12:15:15,152 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout Note: No compatibility mode selected!' 2023-02-06 12:15:15,153 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout Circuit: Voltage Divider' 2023-02-06 12:15:15,153 - PySpice.Spice.NgSpice.Shared.NgSpiceShared - Shared.DEBUG - Execute command: run 2023-02-06 12:15:15,154 - PySpice.Spice.NgSpice.Shared - Shared.DEBUG - b'stdout Doing analysis at TEMP = 25.000000 and TNOM = 25.000000'

Process finished with exit code -1073741819 (0xC0000005)