Skip to content

Elements and ownership

An Element is a validated object that can appear in a circuit or subcircuit. Nimphel currently provides four main element families:

Element Purpose
Instance One placed primitive, model, or subcircuit
Model A named simulator model declaration
Subcircuit A reusable hierarchical definition
Directive Simulator-independent analysis or control intent

A Circuit is the top-level owner of these elements.

Ordered storage

Every container has one canonical elements sequence. Convenience properties filter that sequence without creating separate state:

circuit.elements
circuit.instances
circuit.models
circuit.subcircuits
circuit.directives

Element order is retained because it can affect generated netlists.

Note

Use add(), extend(), remove(), and replace() to modify a container. These methods maintain ownership and reference state.

Single-container ownership

An element can belong to only one container at a time:

from nimphel import Circuit, Component, ComponentKind, ElementOwnershipError

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

first = Circuit(name="first")
second = Circuit(name="second")
instance = first.add(resistor(("vin", 0)))

try:
    second.add(instance)
except ElementOwnershipError:
    pass

This rule prevents an edit in one circuit from unexpectedly changing another circuit through a shared object.

The owner property reports the current container:

assert instance.owner is first

Removing an instance detaches it:

removed = first.remove("R1")
assert removed.owner is None

Clone configured elements

Use a component factory when you want another new component instance. Use .clone() when you want a deep copy of an already configured element:

configured = resistor(("vin", 0), r="10k")
copy = configured.clone()

copy.params["r"] = "20k"

assert configured.params["r"] == "10k"
assert copy.params["r"] == "20k"

Cloning a subcircuit recursively clones its contents and reestablishes ownership inside the cloned definition.

Semantic equality

Element equality compares circuit data, not container ownership. Two equivalent instances compare equal even if they belong to different circuits.

References are part of the serialized instance data. Use matches_topology() when the reference and parameters should not affect a comparison:

assert configured.matches_topology(copy)
assert not configured.matches_exactly(copy)

Models

A model records a name, base primitive, and parameter mapping:

from nimphel import Model

slow_nmos = Model(
    name="slow_nmos",
    base="nmos",
    params={"vth": 0.55},
)

Models are ordinary owned elements and retain their position among other circuit elements.

Next