#!/usr/bin/env python # coding: utf-8 # Netlist Manipulations # ===================== # # This example shows how to manipulate netlist. # # In[ ]: import PySpice.Logging.Logging as Logging logger = Logging.setup_logging() from PySpice import Circuit, SubCircuitFactory from PySpice.Unit import * class SubCircuit1(SubCircuitFactory): NAME = 'sub_circuit1' NODES = ('n1', 'n2') def __init__(self): super().__init__() self.R(1, 'n1', 'n2', 1@u_Ω) self.R(2, 'n1', 'n2', 2@u_Ω) # Let define a circuit. # # In[ ]: circuit = Circuit('Test') # When we add an element to a circuit, we can get a reference to it or # ignore it: # # In[ ]: C1 = circuit.C(1, 0, 1, 1@u_uF) circuit.C(2, 1, 2, 2@u_uF) circuit.subcircuit(SubCircuit1()) circuit.X('1', 'sub_circuit1', 2, 0) # We can get back an element of a circuit using its name, either as a # class attribute or using the dictionary interface: # # In[ ]: C1 = circuit.C1 C1 = circuit['C1'] # and modify it # # In[ ]: C1.capacitance = 10@u_F # To get the SPICE netlist of a citcuit, we just have to convert it to a # string: # # In[ ]: print(circuit) # str(circuit) is implicit here # same apply to an element # # In[ ]: print(C1) # We can disable an element in the circuit # # In[ ]: C1.enabled = False print(circuit) # We can clone a circuit to another one # # In[ ]: circuit2 = circuit.clone(title='A clone') # title is optional print(circuit2) # We can remove an element # # In[ ]: C2 = circuit2.C2.detach() print(circuit2)