Voltage-limited current source with smooth saturation#337
Open
esaruoho wants to merge 2 commits intopfalstad:devfrom
Open
Voltage-limited current source with smooth saturation#337esaruoho wants to merge 2 commits intopfalstad:devfrom
esaruoho wants to merge 2 commits intopfalstad:devfrom
Conversation
4 tasks
Owner
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>
be78635 to
f7156f2
Compare
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.
Author
|
Pushed Three changes:
Please re-test with the original circuit (all switches open, load tree). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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 = 0means unlimited (ideal current source — identical to today's behavior). Any positive value enables compliance. The dialog field readsMax Voltage (V, 0=unlimited).Convergence with switches open. Your test circuit on #241 was the canary — switches open + high-resistance loads make
vdgrow until it crossesmaxVoltage, 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:with
vt = 5% of maxVoltage(small enough to look hard-saturated to the eye, smooth enough for Newton). ThedoStep()Norton companion stamps the analytic Jacobian so Newton-Raphson sees a real derivative:A small gmin (
1e-9) is added to|g|to avoid singular matrix whensech²is near zero (well inside or well outside compliance), then the element stamps asstampResistor(1/|g|) + stampCurrentSource(i - g·vd).The source now rolls off smoothly to zero current as
|vd|approachesmaxVoltageinstead of slamming.Test plan
i=0.01 A,mv=5): converges, no failure, vd settles at compliance.mvattribute round-trips for compliance circuits; old circuits withoutmvload with maxVoltage = 0.🤖 Generated with Claude Code