symbench / spice-completion

Exploratory code for learning to autocomplete spice netlists
1 stars 1 forks source link

PySpice reads Capacitor as BehavioralCapacitor #1

Open umesh-timalsina opened 3 years ago

umesh-timalsina commented 3 years ago

While working on https://github.com/symbench/electric-circuits/issues/23, I found that the following netlist (Taken from the dataset repository), the capacitor elements are read as BehaviorialCapacitor. I am not sure how it effects the training process, but I am bringing this issue to light here @brollb.

Example Netlist (LT001_TA05.net):

* Z:\mnt\spice-netlists\LT1001_TA05.asc
XU1 N001 N005 +V -V N003 LT1001
R1 N002 N001 5K
R3 N002 0 25K
C1 N002 0 1�
C2 N003 N005 1000p
R4 N005 N004 5K
V1 N004 0 PWL(0 0 10 -25)
Rload N006 -V 1K
V2 +V 0 5
V3 -V 0 -35
M1 N002 N003 N006 N006 2N7002
.model NMOS NMOS
.model PMOS PMOS
.lib C:\users\brian\My Documents\LTspiceXVII\lib\cmp\standard.mos
.tran 10
* LT1001 - Precision Operational Amplifier\nPrecision Current Source
* Note:\n  If the simulation model is not found please update with the "Sync Release" command from the "Tools" menu.\n  It remains the customer's responsibility to verify proper and reliable operation in the actual application.\n  Component substitution and printed circuit board layout may significantly affect circuit performance or reliability.\n  Contact your local sales representative for assistance. This circuit is distributed to customers only for use with LTC parts.\n  Copyright � 2015 Linear Technology Inc. All rights reserved.
.lib LTC.lib
.backanno
.end

Parsing this netlist using the SpiceParser in PySpice:

>>> from PySpice.Spice.Parser import SpiceParser
>>> text = open('./path_to/LT001_TA05.net', 'rb').read().decode('utf-8', 'ignore')
>>> text
'* Z:\\mnt\\spice-netlists\\LT1001_TA05.asc\nXU1 N001 N005 +V -V N003 LT1001\nR1 N002 N001 5K\nR3 N002 0 25K\nC1 N002 0 1µ\nC2 N003 N005 1000p\nR4 N005 N004 5K\nV1 N004 0 PWL(0 0 10 -25)\nRload N006 -V 1K\nV2 +V 0 5\nV3 -V 0 -35\nM1 N002 N003 N006 N006 2N7002\n.model NMOS NMOS\n.model PMOS PMOS\n.lib C:\\users\\brian\\My Documents\\LTspiceXVII\\lib\\cmp\\standard.mos\n.tran 10\n* LT1001 - Precision Operational Amplifier\\nPrecision Current Source\n* Note:\\n  If the simulation model is not found please update with the "Sync Release" command from the "Tools" menu.\\n  It remains the customer\'s responsibility to verify proper and reliable operation in the actual application.\\n  Component substitution and printed circuit board layout may significantly affect circuit performance or reliability.\\n  Contact your local sales representative for assistance. This circuit is distributed to customers only for use with LTC parts.\\n  Copyright � 2015 Linear Technology Inc. All rights reserved.\n.lib LTC.lib\n.backanno\n.end\n'
>>> parser = SpiceParser(source=text)
Parser ignored: .lib C:\users\brian\My Documents\LTspiceXVII\lib\cmp\standard.mos
Parser ignored: .tran 10
Parser ignored: .lib LTC.lib
Parser ignored: .backanno
>>> ckt = parser.build_circuit()
>>> ckt.elements
odict_values([SubCircuitElement XU1, Resistor R1, Resistor R3, BehavioralCapacitor C1, BehavioralCapacitor C2, Resistor R4, VoltageSource V1, Resistor Rload, VoltageSource V2, VoltageSource V3, Mosfet M1])
>>> from PySpice.Unit import *
>>> as_F(ckt['C1'].capacitance_expression)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/umesh/anaconda3/lib/python3.7/site-packages/PySpice/Unit/Unit.py", line 643, in validate
    return prefixed_unit.new_value(value)
  File "/home/umesh/anaconda3/lib/python3.7/site-packages/PySpice/Unit/Unit.py", line 856, in new_value
    return [self._value_ctor(self, x) for x in value]
  File "/home/umesh/anaconda3/lib/python3.7/site-packages/PySpice/Unit/Unit.py", line 856, in <listcomp>
    return [self._value_ctor(self, x) for x in value]
  File "/home/umesh/anaconda3/lib/python3.7/site-packages/PySpice/Unit/Unit.py", line 894, in __init__
    self._value = float(value)
ValueError: could not convert string to float: 'µ'
>>> 
brollb commented 3 years ago

It looks like it still generates the correct netlist when converting it back to a string.

Have you checked out the docs for the BehavioralCapacitor in PySpice? (It's not obvious to me that this is an issue.)

umesh-timalsina commented 3 years ago

According to my understanding, capacitance of a BehaviorialCapacitor can be defined by an expression rather than a fixed capacitance value. There's an issue in PySpice, which we it think we should track. Is BehaviorialCapacitor a component category used for training? And will labeling a Capacitor as BehaviorialCapacitor make any difference?