Circuit parameter optimization library for XSchem analog design workflows using ngspice.
# Option 1: nixshell
nix-shell
# Option 2: download xschem and ngspicematurin develop # Generate the python library
pytest test/ -v # Run Tests
python examples/optimizer.py # Run exampleParameters must follow the format: COMPONENT_PARAMETER
M1_W→ alters width of transistorM1M1_L→ alters length of transistorM1R1→ alters value of resistorR1
The library automatically maps parameter names to ngspice component parameters:
- Suffix
_W→ transistor width (uses@component[w]syntax) - Suffix
_L→ transistor length (uses@component[l]syntax) - Suffix
_M→ multiplier (uses@component[m]syntax) - No suffix → component value (for R, C, etc.)
The library automatically handles .control and .endc blocks:
# Target definition
Target(metric="GBW", ...)
spice_code="""
.ac dec 50 1 1G
.control
run
meas ac gbw_val WHEN vdb(vout)=0 CROSS=1
.endc
"""template/
├── OpAmp.sch # Schematic
├── OpAmp.sym # Symbol
└── OpAmp_tb.sch # Testbench (required)
test_flow.py
The optimizer:
- Generates netlist from
OpAmp_tb.schusing xschem - Loads circuit into ngspice
- Alters parameters for each iteration
- Runs tests and extracts metrics
- Computes cost and optimizes
from uwasic_optimizer import ParameterConstraint, RelationshipType
constraints = [
ParameterConstraint(
target_param=M1_W,
source_params=[M2_W],
expression="XM2_W", # M1_W = M2_W
relationship=RelationshipType.Equals,
description="Match differential pair widths",
),
]Every parameter on the right hand side must be defined in source_params, you can use any valid mathetmatical expression in expression. The python side will tell you if you are missing any parameters or wrote an incorrect expression.
Supported relationships:
Equals: target = expressionGreaterThan: target > expressionGreaterThanOrEqual: target ≥ expressionLessThan: target < expressionLessThanOrEqual: target ≤ expression
git tag v0.0.* && git push origin v0.0.*