Skip to content

Improve op amp convergence for differentiator circuits#254

Open
esaruoho wants to merge 1 commit intopfalstad:v3-devfrom
esaruoho:fix-opamp-differentiator
Open

Improve op amp convergence for differentiator circuits#254
esaruoho wants to merge 1 commit intopfalstad:v3-devfrom
esaruoho:fix-opamp-differentiator

Conversation

@esaruoho
Copy link
Copy Markdown

@esaruoho esaruoho commented Mar 2, 2026

Summary

  • Fixes ideal op amp "freaking out" in differentiator circuits with fast edges (op amp freaking out in differentiator #82)
  • Two changes to OpAmpElm.doStep():
    1. Voltage limiting: clamps differential input voltage change between iterations to prevent Newton-Raphson overshoot. With gain=100000, the linear region is only ~0.3mV wide — without limiting, the iteration bounces wildly between saturation states.
    2. Deterministic convergence escape: replaces the random getrand(4)==1 check with a deterministic subIterations > 20 condition. The random 25% chance of switching saturation direction introduced nondeterministic oscillation that destabilized converging iterations in high-gain circuits.

Test plan

  • Build op amp differentiator with 1k resistor + capacitor + logic input pulse → verify stable output
  • Verify the built-in differentiator example (amp-dfdx.txt) still works correctly
  • Test Schmitt trigger circuit (amp-schmitt.txt) → verify hysteresis still works
  • Test standard op amp circuits (inverting, non-inverting, integrator) for regression
  • Verify lower-gain op amps are unaffected

Fixes #82

🤖 Generated with Claude Code

Two changes to OpAmpElm.doStep():

1. Add voltage limiting: clamp the differential input voltage change
   between iterations to prevent Newton-Raphson overshoot. With
   gain=100000, the linear region is only ~0.3mV wide, so large
   voltage steps cause the iteration to bounce between saturation
   states indefinitely.

2. Replace random getrand(4) escape mechanism with a deterministic
   subIterations-based check. The random 25% chance of switching
   saturation direction introduced nondeterministic behavior that
   destabilized otherwise-converging iterations in high-gain circuits
   like differentiators with fast transient inputs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant