Skip to content

Manipulate a circuit

This guide loads a netlist, locates instances, updates parameters and references, validates the result, and writes a new netlist.

Load the source

from nimphel.readers import SpectreReader

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

The same manipulation API applies to circuits constructed directly in Python.

Look up one reference

mosfet = circuit.get("M5")
mosfet.params["w"] = "2u"
mosfet.params["l"] = "65n"

get(ref) returns exactly one instance in the current scope. It raises KeyError when the reference is absent.

Use recursive lookup cautiously:

mosfet = circuit.get("M5", recursive=True)

Nested scopes can contain the same local reference. In that case recursive get raises LookupError instead of choosing an arbitrary instance.

Find several instances

from nimphel import ComponentKind

mosfets = circuit.find(kind=ComponentKind.MOSFET)
model_instances = circuit.find(target="nmos_65nm")
nested_mosfets = circuit.find(
    kind=ComponentKind.MOSFET,
    recursive=True,
)

Filters are combined. The result preserves circuit traversal order:

selected = circuit.find(
    target="nmos_65nm",
    kind=ComponentKind.MOSFET,
)

Ordinary Python performs bulk edits:

for instance in selected:
    instance.params["w"] = "2u"

Rename safely

Owned references are managed by their container:

renamed = circuit.rename("M5", "M20")

assert renamed.ref == "M20"
assert circuit.get("M20") is renamed

Directly assigning instance.ref while it is owned raises ElementOwnershipError. The container operation keeps duplicate detection and future reference allocation consistent.

Remove an instance

removed = circuit.remove("R3")

assert removed.owner is None

The removed instance retains its data and can be inspected, modified, cloned, or added elsewhere. Automatic allocation remains monotonic and does not silently reuse R3.

Replace an instance

from nimphel import Component, ComponentKind

resistor = Component(
    target="resistor",
    pins=("p", "n"),
    kind=ComponentKind.RESISTOR,
)

replacement = resistor(("new_input", 0), r="10k")
old = circuit.replace("R1", replacement)

assert replacement.ref == "R1"
assert old.owner is None

A replacement without an explicit reference inherits the old reference and position. An explicitly referenced replacement is checked for collisions.

Walk all elements

for element in circuit.walk(recursive=True):
    print(type(element).__name__)

walk() yields the canonical source order. Recursive traversal enters registered subcircuit definitions.

Clone before reuse

An owned element cannot be inserted into another circuit:

copy = circuit.get("M20").clone()
other_circuit.add(copy)

Calling a Component is different: every call already creates a fresh instance, so component reuse never requires cloning.

Validate and write

from nimphel.writers import SpectreWriter

circuit.validate_integrity()

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

The writer preserves supported functional meaning and current element order, but it does not preserve original whitespace or comments.

Complete pattern

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

reader = SpectreReader()
writer = SpectreWriter()

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

for instance in circuit.find(
    kind=ComponentKind.MOSFET,
    recursive=True,
):
    instance.params["w"] = "2u"

circuit.rename("V1", "VDD_SOURCE")
circuit.validate_integrity()

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