This repository documents the evolution of a computational physics study on Driven LCR-Series Circuits. Originally developed as a dissertation in June 2023 using procedural Fortran 90, this project is currently being modernized into a high-performance Python/NumPy framework.
The primary goal is to solve the governing second-order differential equation for an AC-driven LCR circuit using the 4th-Order Runge-Kutta (RK4) numerical method and to benchmark the performance gains of modern vectorization.
The dynamic behavior of the circuit is modeled by Kirchhoff’s Voltage Law (KVL):
In this project, the 2nd-order ODE is decomposed into a system of two coupled 1st-order equations to allow for high-precision integration via the RK4 algorithm.
This method captures transient responses and steady-state resonance with a validated accuracy of
This project utilizes the 4th-Order Runge-Kutta (RK4) algorithm, which provides a local truncation error of
-
Underdamped: Characterized by oscillating transients before reaching steady-state resonance.
-
Overdamped: A non-oscillatory return to equilibrium.
-
Critically Damped: The fastest return to steady state without oscillation.
A critical part of this modernization is proving the mathematical exactness of the new Python architecture.
The custom Python RK4 engine (lcr_engine.py) was benchmarked against the industry-standard scipy.integrate.solve_ivp (RK45).
- Result: The maximum residual error across the entire time domain is
$9.97 \times 10^{-8}$ , proving the custom integrator is mathematically sound and research-grade.
The outputs of the V2 Python engine were overlaid with the raw data (.dat) from the original V1 Fortran dissertation to ensure physical continuity between the two frameworks.
Validation Note on Superposition: During the V1 to V2 migration, initial cross-validation revealed a transient phase shift. This was successfully traced back to the legacy Fortran architecture, which decoupled the Natural and Forced responses. To achieve the 100% trace overlay seen above, the Python V2 engine's initial boundary conditions were adjusted to mathematically reflect the superposition of the legacy states (
By leveraging NumPy arrays, the V2 engine processes the Underdamped (
A core objective of this project was measuring the computational overhead of migrating from a compiled language (Fortran) to an interpreted language (Python).
A stress-test benchmark was conducted evaluating the Underdamped LCR state over
| Engine | Language | N-Steps | Execution Time (s) | Relative Speed |
|---|---|---|---|---|
| V1 Legacy Engine | Fortran 90 | 0.29 s | 1.0x (Baseline) | |
| V2 Vectorized Engine | Python 3 | 78.77 s | ~269x slower |
Conclusion: Differential equation solvers (like RK4) are inherently sequential, meaning step for loops. The overhead of the Python interpreter results in the engine running roughly 269x slower than the pre-compiled Fortran executable.
Future Optimization: To bridge this gap in Python, Just-In-Time (JIT) compilers like Numba or Cython are required to bypass the interpreter loop overhead.
To visualize the energy dissipation and stability of the system, a Phase Space topological map was generated, plotting the state variable (Current,
Figure 4: Phase Space trajectories. The system begins at the initial state and spirals inward as transient energy dissipates, eventually locking into a stable, elliptical Limit Cycle Attractor driven by the AC source.
-
The Boundary Condition: All simulations trace back to the exact coordinate
$(0, 0.2)$ , proving the mathematical superposition of the initial natural and forced states. -
Transient Energy Dissipation: The wild, crossing spirals seen in the Underdamped (
$10 \Omega$ ) trace represent the "fight" between the circuit's natural resonant frequency and the driving AC frequency ($\omega$ ). -
The Limit Cycle Attractor: As the resistor burns off the transient energy, the AC source perfectly balances the loss. The system gets trapped in a stable, repeating elliptical orbit. (Plotting a steady-state sine wave against its cosine derivative mathematically generates this perfect ellipse).
-
Impact of Damping: The Overdamped (
$60 \Omega$ ) and Critically Damped ($40 \Omega$ ) states aggressively choke the energy flow, bypassing the chaotic transient spirals and dropping almost immediately into a much smaller, lower-energy limit cycle.
Contains the original research artifacts from the 2023 dissertation:
-
Source Code: Procedural Fortran 90 implementations of the RK4 solver.
-
Visualization: GnuPlot scripts used for transient and steady-state analysis.
-
Documentation: The original unpublished dissertation PDF.
A modular, high-performance refactor containing:
-
lcr_engine.py: The core vectorized RK4 mathematical solver. -
scipy_validation.py: Residual calculation script vs. SciPy. -
run_sweep.py: Multi-parameter simultaneous solver script. -
compare_legacy.py: Cross-validation auditor against Fortran data.
-
Numerical Validation: Cross-verifying custom RK4 results against SciPy.
-
Vectorized Solvers: Re-implementing the RK4 engine using NumPy for multi-parameter sweeps.
-
Performance Auditing: Measuring the runtime overhead of Python vs. Fortran for large-scale simulations (
$N > 10^6$ steps). -
Phase Space Analysis: Visualizing energy dissipation (
$q$ vs.$i$ ) and attractor stability in complex damping scenarios.
-
Legacy: View original Fortran logic and GnuPlot visualizations in the
v1_legacy_fortran/directory. -
Modern: Run the Python engine (requires NumPy and Matplotlib) found in
v2_modern_python/.

