Skip to content

Commit

Permalink
implement library support for X element
Browse files Browse the repository at this point in the history
  • Loading branch information
FabriceSalvaire committed Jan 26, 2024
1 parent 770b473 commit b4774f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
23 changes: 22 additions & 1 deletion PySpice/Spice/BasicElement.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
IntKeyParameter,
ModelPositionalParameter,
)
from . import Library
from .StringTools import join_list, join_dict
from .unit import str_spice

Expand Down Expand Up @@ -164,7 +165,27 @@ class SubCircuitElement(NPinElement):

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

def __init__(self, netlist: 'Netlist', name: str, subcircuit_name, *nodes, **parameters) -> None:
def __init__(
self,
netlist: 'Netlist',
name: str,
subcircuit_name: str | Library.Subcircuit,
*nodes,
**parameters,
) -> None:
if isinstance(subcircuit_name, Library.Subcircuit):
subcircuit = subcircuit_name
subcircuit_name = subcircuit.name
pins = {}
for pin in subcircuit.pin_names:
_ = parameters.pop(pin, None)
if _ is None:
raise ValueError(f"Missing pin {pin} for subcircuit {subcircuit_name}")
pins[pin] = _
nodes = subcircuit.map_nodes(**pins)
# isinstance(netlist, Circuit)
if hasattr(netlist, 'include'):
netlist.include(subcircuit)
super().__init__(netlist, name, nodes, subcircuit_name)
# Fixme: match parameters to subcircuit
self.parameters = parameters
Expand Down
5 changes: 2 additions & 3 deletions examples/diode/diode-characteristic-curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@
circuit.V('input', 'in', circuit.gnd, 10@u_V)
circuit.R(1, 'in', 'out', 1@u_Ω) # not required for simulation
D1N4148 = spice_library['1N4148']
circuit.include(D1N4148)
# circuit.include(D1N4148)
# circuit.X('D1', '1N4148', 'out', circuit.gnd)
diode_nodes = D1N4148.map_nodes(anode=0, cathode='out')
circuit.X('D1', D1N4148.name, *diode_nodes)
circuit.X('D1', D1N4148, cathode='out', anode=circuit.gnd)

#r# We simulate the circuit at these temperatures: 0, 25 and 100 °C.

Expand Down

0 comments on commit b4774f7

Please sign in to comment.