PySpice-org / PySpice

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

ADA4084 fails in inverting opamp #273

Open jrmagers opened 3 years ago

jrmagers commented 3 years ago

Environment (OS, Python version, PySpice version, simulator)

Windows 10, Python 3.8.3, PySpice 1.4.3

Expected Behaviour

I created a simple inverting op-amp with a gain of -2. The simulation works for AD8606 and AD8603, but fails for ADA4084. All of these were downloaded directly from ADI's website.

Here is the code to reproduce the error. You can uncomment/comment lines to switch to a different opamp model.

from PySpice.Spice.Netlist import Circuit
from PySpice.Unit import *

import matplotlib.pyplot as plt

circuit = Circuit('Inverting Amp')
#circuit.include('AD8606.cir')
#circuit.include('AD8603.cir')
circuit.include('ada4084.cir')

circuit.R(1, 'input', 'inv', 10@u_kΩ)
circuit.R(2, 'inv', 'output', 20@u_kΩ)
circuit.R(3, 'output', circuit.gnd, 100@u_MΩ)
#circuit.X(1, 'AD8606', circuit.gnd,'inv','+5V','-5V','output')
#circuit.X(1, 'AD8603', circuit.gnd,'inv','+5V','-5V','output')
circuit.X(1, 'ADA4084', circuit.gnd,'inv','+5V','-5V','output')
circuit.V(1, '+5V', circuit.gnd, 'dc 5' )
circuit.V(2, '-5V', circuit.gnd, 'dc -5' )
circuit.SinusoidalVoltageSource('3', 'input', circuit.gnd, amplitude=1, frequency=5)

print(circuit)

simulator = circuit.simulator()

analysis = simulator.transient(step_time=1@u_ms, end_time=1@u_s)

plt.plot(analysis.time,analysis.nodes['output'])
plt.plot(analysis.time,analysis.nodes['input'])

Actual Behaviour

This is the error that results when ADA4084 is used:

From cffi callback <function NgSpiceShared._send_char at 0x000001E7FCD9E280>:
Traceback (most recent call last):
  File "C:\Users\jmagers\Anaconda3\lib\site-packages\PySpice\Spice\NgSpice\Shared.py", line 617, in _send_char
    prefix, _, content = message.partition(' ')
TypeError: a bytes-like object is required, not 'str'
Error: ngspice.dll cannot recover and awaits to be detached
.title Inverting Amp
.include C:\pcb\pyspice\ada4084.cir
R1 input inv 10kOhm
R2 inv output 20kOhm
R3 output 0 100MegOhm
X1 0 inv +5V -5V output ADA4084
V1 +5V 0 dc 5
V2 -5V 0 dc -5
V3 input 0 DC 0V AC 1V SIN(0V 1V 5Hz 0s 0Hz)

Traceback (most recent call last):

  File "C:\pcb\pyspice\inverting_opamp.py", line 32, in <module>
    analysis = simulator.transient(step_time=1@u_ms, end_time=1@u_s)

  File "C:\Users\jmagers\Anaconda3\lib\site-packages\PySpice\Spice\Simulation.py", line 1166, in transient
    return self._run('transient', *args, **kwargs)

  File "C:\Users\jmagers\Anaconda3\lib\site-packages\PySpice\Spice\NgSpice\Simulation.py", line 117, in _run
    self._ngspice_shared.load_circuit(str(self))

  File "C:\Users\jmagers\Anaconda3\lib\site-packages\PySpice\Spice\NgSpice\Shared.py", line 1140, in load_circuit
    raise NameError("ngSpice_Circ returned {}".format(rc))

NameError: ngSpice_Circ returned 1

Steps to reproduce the behaviour

See attached for the opamp models and python code:

inverting_opamp.zip

holvo commented 2 years ago

There is a bug in the ADA4084 model. In line 87 V1 451 98 -0.038 ;
AD uses a non-utf-8 character between -0.038 and ;. The ngspice utf-8 syntax check fails. If you remove the characters following -0.038, the simulation will run in standard ngspice (no PySpice used).

I have to check if the ngspice utf-8 syntax check can be made optional (with the risk of a simulation failing arbitrarily).