.param command and formula evaluation

Hello All,
being familiar with spice, usually prefer to create netlists in the bootstrapping way like this:

from PySpice.Spice.Parser import SpiceParser

#######################################

import os
os.linesep="\n"

source=’’’
.title abc

V1 inx 0 DC 2V
R1 inx out 1k
R2 out 0 2k
.end
‘’’

parser = SpiceParser(source=source)
circuit = parser.build_circuit()

simulator = circuit.simulator(temperature=25, nominal_temperature=25)
#analysis = simulator.dc(V1=slice(Vmin, Vmax,Vstep))
analysis = simulator.operating_point()

for node in analysis.nodes.values():
print(‘Node {}: {:5.2f} V’.format(str(node), float(node)))

######################################

However in Spice Macromodels global parameters should be possible like a netlist in this way:
######################################

source=’’’
.title abc
.param res=1k
V1 inx 0 DC 2V
R1 inx out {res}
R2 out 0 2k
.end
‘’’
… and so on…

Anyway, the parser does not seem to recognize neither the “.param” nor the curled braces.

Is there a workaround for this?

Kind rgs Martin

Actually the parser is quite incomplete, there is some improvements in a PR to be merged. I think it will be hard to implement a full parser due to the lack of grammar. I am afraid we have to look in the NgSpice code to learn it.

However you can try the .raw feature to pass raw spice and just get outputs.

Hi, can you give me an example how this may work?

I just added an example …

circuit = Circuit('Test')

#r# Pass raw Spice definitions to a circuit, aka netlist, content is inserted at the beginning of
#r# the netlist.
circuit.raw_spice = '''
Vinput in 0 10V
R1 in out 9kOhm
'''

#r# Pass element parameters as raw Spice, content is concatenated with `R2 out 0`
circuit.R(2, 'out', 0, raw_spice='1k')

print(circuit)

This works:

circuit = Circuit(‘Test’)

circuit.raw_spice = ‘’’
.param x=5000
V1 in 0 10
R1 in out 500
R2 out 0 1000
R3 out 0 {x}
‘’’

print(circuit)

simulator = circuit.simulator(temperature=25, nominal_temperature=25)
analysis = simulator.operating_point()

print(analysis.branches)

z=analysis.branches