XSPICE elements in PySpice

Good afternoon!
I’m trying to model a simple electrical circuit with XSPICE elements. For example, the electrical circuit from Chapter 27.1 (Ngspice user manual):

from PySpice.Spice.Netlist import Circuit

test = """XSPICE
Vin1 1 0 DC 0V AC 1.0 SIN(0V 1V 1000Hz)
Ccouple 1 in 0.000001
Rzin in 0 19.35k
Aamp in aout gain_block
.model gain_block gain (gain=-3.9 out_offset=7.003)
Rzout aout coll 3.9k
Rbig coll 0 1e12
.control
run
display
tran 0.00001 0.003
plot i(Vin1)
.endc
.end
"""
circuit = Circuit(test)

simulator = circuit.simulator(temperature=27, nominal_temperature=27)
analysis = simulator.transient(step_time=0.00001, end_time=0.003)

When I try to run it I get an error:

  File ".../.local/lib/python3.7/site-packages/PySpice/Spice/RawFile.py", line 223, in _read_header_line
    raise NameError("Unexpected line: %s" % (line))
NameError: Unexpected line: Reducing trtol to 1 for xspice 'A' devices

So there are two questions:

  1. If I understand correctly, the error is caused by the ngspice message “Reducing trtol to 1 for xspice ‘A’ devices”. Is it possible to avoid this error? When adding a line “option trtol=1” after the line “.control” I get the following error:
    NameError: Expected label Title instead of No. of Data Rows
  2. Is it possible to set the value of trtol and other Simulator Variables by using any pyspice command?

I am using Spyder 4, PySpice 1.4.3 and ngspice-30 (DEFAULT_SIMULATOR = ‘ngspice-subprocess’) in the Debian linux operating system.

Thank you in advance!


Regards,
Dmitriy N.

You code is wrong

    from PySpice.Spice.Netlist import Circuit

    circuit = Circuit('XSpice Test')
    circuit.raw_spice = """
    Vin1 1 0 DC 0V AC 1.0 SIN(0V 1V 1000Hz)
    Ccouple 1 in 0.000001
    Rzin in 0 19.35k
    Aamp in aout gain_block
    .model gain_block gain (gain=-3.9 out_offset=7.003)
    Rzout aout coll 3.9k
    Rbig coll 0 1e12
    """

    print(circuit)

    simulator = circuit.simulator(temperature=27, nominal_temperature=27)
    analysis = simulator.transient(step_time=0.00001, end_time=0.003)

Thank you for your reply.
Still, it didn’t solve the problem.

from PySpice.Spice.Netlist import Circuit

circuit = Circuit('XSpice Test')
circuit.raw_spice = """
Vin1 1 0 DC 0V AC 1.0 SIN(0V 1V 1000Hz)
Ccouple 1 in 0.000001
Rzin in 0 19.35k
Aamp in aout gain_block
.model gain_block gain (gain=-3.9 out_offset=7.003)
Rzout aout coll 3.9k
Rbig coll 0 1e12

.control
option trtol=1
run
display
tran 0.00001 0.003
plot i(Vin1)
.endc
.end
"""

simulator = circuit.simulator(temperature=27, nominal_temperature=27)
analysis = simulator.transient(step_time=0.00001, end_time=0.003)

When I run this code the following error occurs:
NameError: Expected label Title instead of No. of Data Rows

If I delete the line “option trtol=1”, another error occurs:
NameError: Unexpected line: Reducing trtol to 1 for xspice 'A' devices

The same error occurs if I run the code:

from PySpice.Spice.Netlist import Circuit
circuit = Circuit('XSPICE')

circuit.SinusoidalVoltageSource('in1', '1', 0, amplitude=1, frequency=1000)
circuit.C('couple', '1', 'in1', '0.000001')
circuit.R('zin', 'in1', 0, '19.35')

circuit.model('gain_block', 'gain', gain=-3.9, out_offset=7.003)
circuit.A('amp', 'in1', 'aout', model='gain_block')
circuit.R('zout', 'aout', 'coll', '3.9k')
circuit.R('big', 'coll', 0, '1e12')

simulator = circuit.simulator(temperature=27, nominal_temperature=27)
analysis = simulator.transient(step_time=0.00001, end_time=0.003)

Is this a problem with my software? Or did I do something wrong?

Hi Dmitriy,
I was able to run Fabrices code (Windows10/Spyder IDE/Python 3.5)
Rgs Martin

Thanks for answering.
I understood the reason for my mistake.
When using raw SPICE commands, I excluded extra lines “.control … .endc” and left the line “.option trtol=1”.
When using an element description of an electrical circuit, I used the following line to set the value of the trtol variable to 1:
circuit.raw_spice = '.option trtol=1'


Regards,
Dmitriy N.