Skip to content

Voltage-limited current source with smooth saturation#337

Open
esaruoho wants to merge 2 commits intopfalstad:devfrom
esaruoho:voltage-limited-current-source
Open

Voltage-limited current source with smooth saturation#337
esaruoho wants to merge 2 commits intopfalstad:devfrom
esaruoho:voltage-limited-current-source

Conversation

@esaruoho
Copy link
Copy Markdown

Summary

Split out from #241 with both of @pfalstad's redesign requests applied. Adds optional voltage compliance to CurrentElm.

Both pieces of feedback addressed

No checkbox. Default maxVoltage = 0 means unlimited (ideal current source — identical to today's behavior). Any positive value enables compliance. The dialog field reads Max Voltage (V, 0=unlimited).

Convergence with switches open. Your test circuit on #241 was the canary — switches open + high-resistance loads make vd grow until it crosses maxVoltage, at which point the original code hard-stepped between "stamp ideal source" and "stamp open circuit". Newton-Raphson had no fixed point and ping-ponged between the two stamps each iteration. Fixed with a smooth tanh-shaped saturation:

i(vd) = currentValue * 0.5 * (1 - tanh((|vd| - maxVoltage) / vt))

with vt = 5% of maxVoltage (small enough to look hard-saturated to the eye, smooth enough for Newton). The doStep() Norton companion stamps the analytic Jacobian so Newton-Raphson sees a real derivative:

di/dvd = -currentValue * 0.5 * sech²(arg) / vt * sign(vd)

A small gmin (1e-9) is added to |g| to avoid singular matrix when sech² is near zero (well inside or well outside compliance), then the element stamps as stampResistor(1/|g|) + stampCurrentSource(i - g·vd).

The source now rolls off smoothly to zero current as |vd| approaches maxVoltage instead of slamming.

Test plan

  • Default behavior: place a current source, leave Max Voltage = 0 → identical to current behavior (ideal current source, no convergence overhead).
  • Set Max Voltage = 5, load with a low-resistance path → source pushes the configured current, voltage stays well below 5 V.
  • Increase load impedance until vd would exceed 5 V → current rolls off smoothly to zero, vd plateaus near 5 V.
  • @pfalstad's Optocoupler CTR, SRAM reset restore, voltage-limited current source, macOS paste #241 test circuit (multiple switches all open, current source i=0.01 A, mv=5): converges, no failure, vd settles at compliance.
  • Open all switches in the test circuit: simulator does not log convergence failures.
  • Save/load: mv attribute round-trips for compliance circuits; old circuits without mv load with maxVoltage = 0.

🤖 Generated with Claude Code

@pfalstad
Copy link
Copy Markdown
Owner

The same test circuit is still giving me convergence failures.

Screenshot 2026-04-14 at 11 35 07 PM

@esaruoho esaruoho changed the base branch from v3-dev to dev April 15, 2026 09:50
Adds an optional compliance voltage to CurrentElm. The redesign
addresses both pieces of @pfalstad feedback on PR pfalstad#241:

- No checkbox. Default maxVoltage = 0 means unlimited (ideal current
  source); a positive value enables compliance. The dialog field is
  labelled "Max Voltage (V, 0=unlimited)".
- Convergence with switches open. The original implementation
  hard-stepped between "stamp ideal source" and "stamp open" once
  |vd| crossed maxVoltage. Newton-Raphson then ping-ponged between
  the two stamps with no fixed point, especially with high-impedance
  loads (open switches, large resistors). Replaced with a smooth
  tanh-shaped saturation:

      i(vd) = currentValue * 0.5 * (1 - tanh((|vd| - maxVoltage)/vt))

  with vt = 5% of maxVoltage. The Norton companion model stamps the
  exact analytic Jacobian (di/dvd = -currentValue * 0.5 * sech^2(arg)
  / vt * sign(vd)) plus a small gmin so the matrix never goes
  singular when sech^2 is near zero.

Result: the source rolls off smoothly to zero current as |vd|
approaches maxVoltage rather than slamming between full-on and
full-off, which is what was killing convergence in the test circuit
@pfalstad shared on pfalstad#241.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@esaruoho esaruoho force-pushed the voltage-limited-current-source branch from be78635 to f7156f2 Compare April 15, 2026 09:51
@pfalstad
Copy link
Copy Markdown
Owner

ok I guess you just moved all your PR's to the dev branch which is good. I tested this one again just in case and still get convergence failures.

Paul's pfalstad#241 test circuit with multiple open switches was still failing.
Root cause: vt was 5% of maxVoltage (0.25V for a 5V limit), giving a
near-discontinuous Jacobian. g jumps by ~1e7 across a few tens of mV
near the compliance threshold, so Newton pings between the linear and
saturated regimes.

Changes:
- vt = max(0.2 * maxVoltage, 0.1V) spreads the knee so sech^2 varies
  smoothly across ~1V instead of ~0.25V.
- Gmin floor raised from 1e-9 to 1e-6 to keep the Norton resistor well
  below the matrix stiffness scale in open-circuit topologies.
- Removed the explicit converged=false trigger; the solver's own
  convergence criteria are sufficient once the Jacobian is well-shaped.
@esaruoho
Copy link
Copy Markdown
Author

Pushed 24fecc4f. Root cause of the persistent failure: vt = 0.05 * maxVoltage gave a near-discontinuous Jacobian — g jumps by ~1e7 across ~50 mV near the knee, so Newton ping-pongs between linear and saturated iterations.

Three changes:

  • vt = max(0.2 * maxVoltage, 0.1V) — knee spread over ~1V so sech² varies smoothly.
  • Gmin floor 1e-6 (was 1e-9) — keeps the Norton resistance under the matrix stiffness scale in open-circuit topologies.
  • Dropped the explicit sim.converged = false trigger — the solver's native convergence check is sufficient once the Jacobian is well-shaped. The old trigger was forcing a rerun on any 0.01 V movement, which fought the solver's own damping.

Please re-test with the original circuit (all switches open, load tree).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants