spicex API#

Circuit#

class spicex.Circuit(n_nodes: int)#

JAX-based Modified Nodal Analysis (MNA) circuit solver.

Node 0 is ground.

add_capacitor(node_a: int, node_b: int, capacitance: float) None#

Add an ideal capacitor between node_a and node_b.

In DC analysis the capacitor is an open circuit (zero current). The returned i_capacitor is always zero in DC.

add_current_source(node_neg: int, node_pos: int, current: float) None#

Add an independent current source.

node_neg is the - terminal, node_pos is the + terminal. Conventional current flows from node_neg to node_pos inside the source (i.e., current is injected into node_pos and extracted from node_neg).

add_inductor(node_a: int, node_b: int, inductance: float) None#

Add an ideal inductor between node_a and node_b.

node_a is the + terminal, node_b is the - terminal. In DC analysis the inductor is a short circuit (zero voltage drop). The returned i_inductor is positive when current flows from node_a through the inductor to node_b.

add_resistor(node_a: int, node_b: int, resistance: float) None#

Add a resistor between node_a and node_b.

add_voltage_source(node_neg: int, node_pos: int, voltage: float) None#

Add an independent voltage source.

node_neg is the - terminal, node_pos is the + terminal. V(node_pos) - V(node_neg) = voltage.

solve() tuple#

Solve the circuit with MNA.

Returns:

jnp array shape (n_nodes,), node voltages; v_nodes[0] = 0.

i_vsrc: jnp array shape (n_vsrc,), current through each voltage

source (positive = current flowing from + terminal through the external circuit).

i_inductor: jnp array shape (n_inductor,), current through each

inductor (positive = current flowing from node_a through the inductor to node_b).

i_capacitor: jnp array shape (n_capacitor,), current through each

capacitor; always zero in DC analysis.

Return type:

v_nodes

solve_transient(t_end: float, dt: float, v0=None, i_L0=None) tuple#

Solve the circuit in the time domain using backward Euler integration.

Capacitors and inductors are replaced with backward Euler companion models:
  • Capacitor: conductance G_eq = C/dt plus history current I_eq = G_eq*V_prev.

  • Inductor: conductance G_eq = dt/L plus history current I_eq = i_L_prev.

Inductors are NOT in the B matrix here; they contribute to G only.

The time loop uses jax.lax.scan, keeping the solver JAX-differentiable.

Parameters:
  • t_end – Simulation end time (s).

  • dt – Time step (s).

  • v0 – Initial node voltages shape (n_nodes,). Defaults to zeros. v0[0] must be 0 (ground).

  • i_L0 – Initial inductor currents shape (n_inductor,). Defaults to zeros.

Returns:

shape (n_steps,), time points dt, 2*dt, …

v_nodes: shape (n_steps, n_nodes), node voltages; column 0 = 0.

i_vsrc: shape (n_steps, n_vsrc), voltage-source currents.

i_inductor: shape (n_steps, n_inductor), inductor currents

(positive = node_a to node_b).

i_capacitor: shape (n_steps, n_capacitor), capacitor currents

(positive = node_a to node_b, i.e. C*dV/dt).

Return type:

t