Skip to content

Examples

Reusable voltage divider

from nimphel import Circuit, Component, ComponentKind, Subcircuit
from nimphel.writers import SpectreWriter

resistor = Component(
    target="resistor",
    pins=("p", "n"),
    kind=ComponentKind.RESISTOR,
    defaults={"r": "1k"},
)

with Subcircuit(name="divider", ports=("vin", "vout", "gnd")) as divider:
    resistor(("vin", "vout"))
    resistor(("vout", "gnd"), r="2k")

with Circuit(name="top") as circuit:
    circuit.add(divider)
    divider(("input", "output", 0))

print(SpectreWriter().dumps(circuit))

Generate a chain

Ordinary Python iteration is the preferred construction tool:

from itertools import pairwise

with Circuit(name="chain") as chain:
    for index, (start, end) in enumerate(pairwise(range(11)), start=1):
        resistor((start, end), r=f"{index}k")

The active context assigns R1 through R10.

Generate a rectangular array

from itertools import product

cell = Component(
    target="memory_cell",
    pins=("word", "bit"),
    kind=ComponentKind.CUSTOM,
    ref_prefix="X",
)

with Circuit(name="array") as array:
    for row, column in product(range(4), range(8)):
        cell((f"word_{row}", f"bit_{column}"))

No array-specific dependency or helper is required. A dedicated helper should only be added if it introduces circuit semantics beyond the loop.

Read, modify, and write

from nimphel import ComponentKind
from nimphel.readers import SpectreReader
from nimphel.writers import SpectreWriter

reader = SpectreReader()
circuit = reader.load_path("input.scs")

for instance in circuit.instances:
    if instance.kind == ComponentKind.MOSFET:
        instance.params["w"] = "2u"

with open("output.scs", "w") as output:
    SpectreWriter().dump(circuit, output)

Readers and writers depend on the shared intermediate representation, so the same manipulation code can be reused by future formats.