PyAMS
Python library for analog and mixed-signal simulation (PyAMS Library)
PyAMS Library
What is pyams-lib?
pyams_lib is a Python package designed to simplify the modeling of analog and digital elements and the simulation of electronic circuits. It provides:
- The ability to create custom models of electrical components.
- Simulation of circuits in different modes of operation (DC, DC sweep, transient, AC).
- Digital and mixed-signal models (gates, flip-flops, counters, buses with
dsignal, DAC / ADC) solved together with the analog circuit. - Hierarchical models: a model made of other models with internal nodes (
subcircuit()). - Visualization of simulation results using
matplotlib, including a logic waveform viewer (plot_wave). - Compatibility with Python 3+, working across Linux, Windows, and macOS.
- PyAMS library (pyams_lib) documentation https://pyams-lib.readthedocs.io/.
- For circuit design using the CAD system, visit the software section at https://pyams.sf.net.
Installation
pip install pyams_lib
To upgrade to the latest version:
pip install --upgrade pyams_lib
Requires Python 3 with numpy and matplotlib.
Contents
1. Quick start
A half-wave rectifier: a 10 V / 2 Hz sine source, a diode and a 1 kΩ load.
from pyams.lib import circuit
from pyams.models import Resistor, SinVoltage, Diode
# 1. create the elements; the arguments are node names ("0" is ground)
V1 = SinVoltage("In", "0")
D1 = Diode("In", "Out")
R1 = Resistor("Out", "0")
# 2. set their parameters
V1.setParams("Va=10V Fr=2Hz")
R1.setParams("R=1KΩ")
# 3. build the circuit and choose what to record
cir = circuit()
cir.addElements({"V1": V1, "D1": D1, "R1": R1})
cir.setOutPuts("In", "Out")
# 4. choose an analysis, run it, plot
cir.analysis(mode="tran", start=0, stop=2, step=0.001)
cir.run()
cir.plot()
The five steps — elements → parameters → circuit → analysis → results — are the same for every circuit.
2. Modeling
2.1 Building a circuit
An element is created with the names of the nodes its terminals are connected to.
Two elements that use the same node name are connected there. The names "0",
"gnd" and "ground" all mean the reference node.
R1 = Resistor("In", "N1") # between node In and node N1
C1 = Capacitor("N1", "0") # between node N1 and ground
Elements are then added to a circuit as a dictionary {name: element}.
The element name is used later to select parameters and signals
(for example "R1.R" or "D1.I"):
cir = circuit()
cir.addElements({"V1": V1, "R1": R1, "C1": C1})
cir.setOutPuts(...) selects what is recorded and plotted. It accepts
| Output name | Meaning |
|---|---|
"Out" |
voltage of node Out |
"R1.I" |
a signal of an element: Element.signal (current) |
"R1.V" |
voltage across the element |
If setOutPuts is not called, all node voltages are recorded.
2.2 Setting parameters
Parameters are set with a string of name=value pairs. Values accept engineering
prefixes (T G Meg k m u n p f) and optional units:
R1.setParams("R=4.7KΩ")
V1.setParams("Va=5V Fr=50Hz")
C1.setParams("C=100nF")
L1.setParams(L=10e-3) # keyword arguments also work
D1.setParams({"Iss": 1e-12, "n": 1.5}) # ... and so does a dict
element.getParams() returns every parameter with its unit and description;
an unknown parameter name raises an error that lists the valid ones.
2.3 Writing your own model
A model is a class derived from model.
__init__receives the terminals (node names), declares the signals and the parameters.analog()holds the equations.
from math import exp
from pyams.lib import model, signal, param, voltage, current
class MyDiode(model):
def __init__(self, p, n):
self.V = signal('in', voltage, p, n) # read: V = v(p) - v(n)
self.I = signal('out', current, p, n) # write: current p -> n
self.Is = param(1e-14, 'A', 'Saturation current')
self.Vt = param(0.02585, 'V', 'Thermal voltage')
def analog(self):
self.I += self.Is * (exp(self.V / self.Vt) - 1)
Use it exactly like a built-in element:
V1 = DCVoltage("In", "0"); V1.setParams("Vdc=5V")
R1 = Resistor("In", "A"); R1.setParams("R=1KΩ")
D1 = MyDiode("A", "0")
cir = circuit()
cir.addElements({"V1": V1, "R1": R1, "D1": D1})
cir.setOutPuts("A", "D1.I")
cir.analysis(mode="dc")
print(cir.run()) # {'time': [0.], 'A': [0.692...], 'D1.I': [0.0043...]}
Signals — signal(direction, kind, p, n)
| Declaration | Meaning |
|---|---|
signal('in', voltage, p, n) |
the value of v(p) - v(n); read it inside analog() |
signal('out', current, p, n) |
sig += x adds a current x flowing from p to n through the element |
signal('out', voltage, p, n) |
sig += x imposes v(p) - v(n) = x (an ideal voltage source) |
signal('in', current, p, n) |
the current of the ('out', voltage) signal on the same two nodes (current-controlled elements) |
Time derivatives. Inside analog(), ddt(x) is the time derivative of x
(it is 0 in DC), and idt(x) its integral. Signals, parameters and time behave
like ordinary floats, so math.exp, math.sin, … can be applied directly.
A capacitor, for instance, is just:
from pyams.lib import ddt
class MyCap(model):
def __init__(self, p, n):
self.V = signal('in', voltage, p, n)
self.I = signal('out', current, p, n)
self.C = param(1e-6, 'F', 'Capacitance')
def analog(self):
self.I += self.C * ddt(self.V)
You only write the equations — the solver builds and linearises the system
(modified nodal analysis with Newton–Raphson; the Jacobian is obtained by finite
differences of analog()), so no derivatives have to be coded by hand.
2.4 Hierarchical models: subcircuit()
A model can be built from other models. Define subcircuit() in an ordinary model
class: self.node(name) creates an internal node of the instance and
self.add(name, element, **params) adds a sub-model (which may itself contain
sub-models, to any depth).
from pyams.lib import model, param
from pyams.models import Resistor, Capacitor
class RCLadder(model):
def __init__(self, inp, out, gnd="0"):
self.inp, self.out, self.gnd = inp, out, gnd # terminals
self.R = param(1e3, 'Ω', 'Resistance of a section')
self.C = param(1e-6, 'F', 'Capacitance of a section')
def subcircuit(self):
mid = self.node("mid") # internal node "L1.mid"
self.add("R1", Resistor(self.inp, mid), R=self.R)
self.add("R2", Resistor(mid, self.out), R=self.R)
self.add("C1", Capacitor(self.out, self.gnd), C=self.C)
- Inner nodes and sub-models are recorded with dotted names:
setOutPuts("L1.mid", "L1.R1.I"). subcircuit()runs again before every analysis, so parameters changed bysweep()are always current.- A model may have both
subcircuit()andanalog()(its own equations are solved together with its sub-models). - The circuit solves the flattened network, so a hierarchical model costs the same as writing the elements one by one.
DiodeBridge(ac1, ac2, plus, minus) is a ready-made example (four Diode sub-models):
2.5 Digital models and buses: dsignal
Digital models are written with dsignal (Verilog-like). A model that has a digital() method is
called at every accepted time point; assignments use <<= and are driven on the node through a
Thevenin output stage (logic levels Voh, Vol, resistance Rout, delay Tpd, ...), so digital blocks
connect directly to analog circuits.
from pyams.lib import model, dsignal
class MyCounter(model):
def __init__(self, clk, q):
self.Clk = dsignal('in', clk)
self.Q = dsignal('out', q, width=3) # a 3-bit bus: nodes q[0], q[1], q[2] (bit 0 = LSB)
def digital(self):
if self.Clk.rising: # also .falling / .changed
self.Q <<= self.Q + 1 # wraps at 8; <<= masks to the width
A bus supports integer operators, slices ds[lo:hi], .value, and delayed assignment ds.set(v, delay).
The digital models of section 5 are all written this way, and a model can have both digital() and analog().
| Counters | Sequential logic | ADC / DAC |
|---|---|---|
3. Analysis
The analysis is chosen with cir.analysis(mode=...) and executed with cir.run().
mode |
Use it for | Main arguments |
|---|---|---|
"dc" |
operating point | – |
"dc" + param= |
DC sweep (I–V curves, transfer curves) | param, start, stop, step |
"tran" |
behaviour in time | start, stop, step (seconds) |
"ac" |
small-signal frequency response | start, stop (Hz), step (points / decade) |
3.1 DC operating point
cir.analysis(mode="dc")
res = cir.run()
print(res["A"][0], res["D1.I"][0])
3.2 DC sweep
Sweep one parameter ("Element.Parameter") and solve the operating point at every value:
cir.analysis(mode="dc", param="V1.Vdc", start=0, stop=5, step=0.05)
res = cir.run()
# res["sweep"] -> swept values, res["D1.I"] -> diode current
cir.plot()
3.3 Transient analysis
cir.analysis(mode="tran", start=0, stop=0.06, step=1e-4)
res = cir.run() # {'time': ..., 'In': ..., 'Out': ...}
Options for demanding circuits (switching, stiff, oscillators):
| Option | Effect |
|---|---|
order=2 |
second-order (BDF2) integration; default 1 = backward Euler |
adaptive=True |
the solver chooses its own step (tiny at fast edges, large elsewhere); step then only sets the spacing of the output points |
lte, lte_abs |
relative / absolute local-error tolerance of the adaptive step |
max_step |
largest internal step |
reltol |
Newton convergence tolerance (default 1e-6) |
uic=True |
start from an all-zero state instead of the DC operating point |
progress=True |
show a progress bar on the console (or pass a function fn(fraction, t) for a GUI; raise in it to abort) |
cir.analysis(mode="tran", start=0, stop=0.006, step=1e-6, adaptive=True, order=2)
3.4 AC (small-signal) analysis
PyAMS solves the DC operating point, linearises every element around it, and solves
the complex system at each frequency. The stimulus comes from the sources that define
an AC value (ACVoltage, ACCurrent). With a 1 V stimulus, the node phasor is the transfer function.
import numpy as np
from pyams.lib import circuit
from pyams.models import ACVoltage, Resistor, Capacitor
V1 = ACVoltage("In", "0")
R1 = Resistor("In", "Out"); R1.setParams("R=1KΩ")
C1 = Capacitor("Out", "0"); C1.setParams("C=1uF")
cir = circuit()
cir.addElements({"V1": V1, "R1": R1, "C1": C1})
cir.setOutPuts("Out")
cir.analysis(mode="ac", start=1, stop=1e5, step=20) # 20 points per decade
res = cir.run()
f, H = res["freq"], res["Out"] # H is complex
gain_db = 20 * np.log10(np.abs(H))
phase_deg = np.angle(H, deg=True)
fc = f[np.argmin(np.abs(gain_db + 3.01))] # -3 dB corner: ~159 Hz
cir.plot() # Bode magnitude + phase
scale="dec" (default) / "oct" give step points per decade / octave;
scale="lin" gives step points in total.
3.5 Parametric sweep
cir.sweep(param, list) repeats the whole analysis (transient, DC or AC) for each value
of a parameter. run() then returns a list of result dictionaries (each with the extra
key "param"), and plot() overlays them.
cir.analysis(mode="ac", start=1, stop=1e5, step=20)
cir.sweep(param="R1.R", list=[100, 1000, 10000])
runs = cir.run() # one result per resistor value
cir.plot()
cir.sweep() # switch the parametric sweep off
4. Showing the results
Built-in plot
cir.plot() # show a window
cir.plot("result.png", show=False) # save to a file
plot() adapts to the analysis: a transient run gives one panel per unit
(voltages, currents) against time, a DC sweep is plotted against the swept parameter,
an AC run gives magnitude and phase against frequency, and a parametric sweep overlays all runs.
Transient (series RLC, rlc.py) |
AC (series RLC band-pass, ac_analysis.py) |
|---|---|
Your own plots and post-processing
run() returns plain NumPy arrays in a dictionary, so you can compute anything with them:
import matplotlib.pyplot as plt
res = cir.run()
t, vout = res["time"], res["Out"]
print("peak output: %.3f V" % vout.max())
print("average : %.3f V" % vout.mean())
fig, ax = plt.subplots()
ax.plot(t * 1e3, vout, label="Out")
ax.set_xlabel("Time (ms)")
ax.set_ylabel("Voltage (V)")
ax.legend()
fig.savefig("out.png", dpi=150)
| Analysis | Keys of the result dictionary |
|---|---|
tran |
"time", then one array per output |
dc (point) |
"time" (one value), then one value per output |
dc + param |
"sweep", "Element.Parameter", then one array per output |
ac |
"freq", then one complex array per output |
with sweep() |
a list of the dictionaries above, each with the extra key "param" |
Logic waveform viewer: plot_wave
pyams.wave.plot_wave draws a VHDL-style wave window: single bits as step traces, buses as
hexagons with their value (hex / bin / dec) and analog traces in the same window.
from pyams.wave import plot_wave
plot_wave(res, [
"clk", # a bit
("count", ["s0", "s1", "s2"]), # a bus, LSB first, hex
("count", ["s0", "s1", "s2"], "dec"), # 'hex' | 'bin' | 'dec'
("Vin, Vdac", ["vin", "vdac"], "analog"), # one or more analog traces
], vth=2.5, tscale=1e6, tunit="µs", tlim=(60, 100), filename="wave.png", show=True)
vth is the logic threshold (or a (vl, vh) pair: a bus with a bit in between shows X),
tlim zooms on a time window, show=True opens a window and filename saves a png.
Bits, buses and analog (wave_example.py) |
Tracking ADC: comparator + up/down counter + DAC (tracking_adc.py) |
|---|---|
5. Built-in models
from pyams.models import Resistor, Capacitor, Diode, BJT # ... see the list below
| Group | Models |
|---|---|
| Passive | Resistor, Conductor, Capacitor, CapacitorIc, Inductor, InductorIc, NonlinearResistance, Transformer, TransmissionLine |
| Switches | Switch, SwitchV, IdealSwitch, Relay |
| Laplace blocks | LaplaceND, LaplaceZP (and the II, IV, VI versions with voltage / current ports) |
| Independent sources | DCVoltage, SinVoltage, ACVoltage, ExpVoltage, PWLVoltage, TrapezoidVoltage, DCCurrent, SinCurrent, ACCurrent, ExpCurrent, PWLCurrent, TrapezoidCurrent |
| Controlled sources | VCVS, VCCS, CCVS, CCCS |
| Diodes / switching | Diode, DiodeBridge, ZenerDiode, TunnelDiode, Diac, Thyristor, UJT |
| Transistors | BJT, MOSFET, MOSFET4, JFET, IGBT |
| Analog ICs | OpAmp, Comparator, Timer555 |
| Digital: gates and clock | DigitalClock, NOT, AND, OR, NAND, NOR, XOR, XNOR |
| Digital: sequential | SRLatch, DLatch, DFF, JKFF, TFF, Register, ShiftRegister, Counter, LFSR |
| Digital: combinational | Multiplexer, Demultiplexer, Decoder, PriorityEncoder, MagnitudeComparator, ParityGenerator, BinaryToGray, GrayToBinary, SevenSegmentDecoder, Adder |
| Mixed-signal | DAC, ADC |
Each class documents its terminals, signals and parameters in its docstring:
help(BJT)
print(BJT("c", "b", "e").getParams())
Switches and relay
from pyams.models import Switch, SwitchV, Relay
sw = Switch("vs", "a", "vc", "0"); sw.setParams("Vt=0.5 Vw=1m Ron=1m Roff=1G") # closed when V(vc) > Vt (smooth Ron/Roff)
sv = SwitchV("vs", "a", "vc", "0"); sv.setParams("Vt=0.5 Vw=1m") # a 0 V source when closed, removed when open
K = Relay("cp", "0", "vs", "no", "nc") # coil, common, normally open / closed contacts
K.setParams("Rc=70 Lc=0.1 Ion=0.05 Ioff=0.01 Ton=5m Toff=3m")
Relay has a coil model V = Rc·I + Lc·dI/dt, hysteresis (Ion / Ioff) and contact delays (Ton / Toff).
| Switch charging / discharging a capacitor | Switch |
SwitchV |
Relay |
|---|---|---|---|
Laplace blocks
Transfer functions in the s domain, usable in AC, DC and transient analysis:
H = LaplaceND("in", "0", "out", "0", num=[1, 0, 0], den=[1, 2*3.14159e3, (2*3.14159e3)**2]) # s² / (...)
Z = LaplaceZP("in", "0", "out", "0", zeros=[...], poles=[...], gain=1.0) # zeros / poles form
Laplace blocks (laplace_examples.py) |
Elliptic filter (elliptic_filter.py) |
|---|---|
Digital blocks
Combinational blocks, arithmetic, registers and an LFSR, all with dsignal buses
(digital_combinational.py):
MX = Multiplexer(["t0", "t1", "t2", "t3"], ["t4", "t5"], "mx") # 4:1, data inputs, select bits, output
AD = Adder(["a0", "a1", "a2", "a3"], ["b0", "b1", "b2", "b3"], ["s0", "s1", "s2", "s3"], cout="co")
RG = Register(["d0", "d1", "d2", "d3"], "clk", ["q0", "q1", "q2", "q3"])
6. More examples
The examples/ folder contains ready-to-run scripts, each saving a figure:
| Script | What it shows |
|---|---|
bridge_rectifier.py, zener_regulator.py |
rectification and regulation |
rlc.py, charge_discharge.py |
transient response of RC / RLC circuits |
ac_analysis.py, elliptic_filter.py |
AC analysis: RLC band-pass, common-emitter amplifier, op-amp, filters |
jfet_curves.py, thyristor_iv.py, ujt_oscillator.py |
DC sweeps and device characteristics |
bjt_astable.py, timer555_astable.py, ring_oscillator.py |
oscillators and multivibrators |
schmitt_opamp.py, comparator_demo.py |
comparators and Schmitt triggers |
chua_opamp.py, van_der_pol.py |
non-linear oscillators / chaos |
switch_example.py, switch_v_example.py, switch_charge_discharge.py, relay_example.py |
switches and relay |
laplace_examples.py, laplace_pulse_int_diff.py |
Laplace transfer-function blocks |
digital_counter.py, digital_sequential.py, digital_adc_dac.py, digital_combinational.py |
digital models with dsignal buses and DAC / ADC |
subcircuit_example.py, diode_bridge_example.py |
hierarchical models (subcircuit()) |
wave_example.py, tracking_adc.py |
logic waveform viewer and a closed analog / digital loop (tracking ADC) |
transmission_line.py, thyristor_lamp_dimmer.py, igbt_chopper.py |
power and transmission-line circuits |
| 555 astable | JFET output curves | Chua double scroll |
|---|---|---|
License
pyams_lib is free to use and distributed under the MIT license.
Metadata
Release files for pyams-lib 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyams_lib-0.5.0.tar.gz | 98.6 kB | Without provenance | Details for pyams_lib-0.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyams_lib-0.5.0-py3-none-any.whl | Python 3 | none | any | Details for pyams_lib-0.5.0-py3-none-any.whl |
Total release size: 217.1 kB
Release files / pyams_lib-0.5.0.tar.gz
| Download URL | pyams_lib-0.5.0.tar.gz |
|---|---|
| Size | 98.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
352858360fefa651b08985a4bb403bef0b80956a1499cfa7dbb475af4bceee95
|
|
BLAKE2b-256 checksum How to use checksums |
48c2b7c24d365d423c8eb468e9fe766c03c5385297a266244def3d25fb81f651
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.2
|
Release files / pyams_lib-0.5.0-py3-none-any.whl
| Download URL | pyams_lib-0.5.0-py3-none-any.whl |
|---|---|
| Size | 118.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2ccf8d237ae0bfd4be4fd5df1206ad5200490c65b11e48c3c5638274eb7ce11a
|
|
BLAKE2b-256 checksum How to use checksums |
07ed4f52dc253c118ffa4b6a77dc71cd6ae5c72c4d4190e14b29615794160173
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.2
|
Security for pyams-lib 0.5.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.