|
| 1 | +Here’s a complete outline and code template for a **Tesla coil simulation (Python, circuit/field simulation)** with these features: |
| 2 | + |
| 3 | +- **Parameter input from the command line** |
| 4 | +- **Field visualization (magnetic field)** |
| 5 | +- **Comprehensive documentation** |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 1. Directory Structure |
| 10 | + |
| 11 | +``` |
| 12 | +tesla-coil-simulation/ |
| 13 | +├── main.py |
| 14 | +├── requirements.txt |
| 15 | +└── README.md |
| 16 | +``` |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## 2. main.py |
| 21 | + |
| 22 | +```python |
| 23 | +import numpy as np |
| 24 | +from scipy.integrate import solve_ivp |
| 25 | +import matplotlib.pyplot as plt |
| 26 | +import argparse |
| 27 | +
|
| 28 | +def parse_args(): |
| 29 | + parser = argparse.ArgumentParser(description='Tesla Coil RLC Simulation with Field Visualization') |
| 30 | + parser.add_argument('--L1', type=float, default=20e-6, help='Primary inductance (H)') |
| 31 | + parser.add_argument('--L2', type=float, default=80e-3, help='Secondary inductance (H)') |
| 32 | + parser.add_argument('--C1', type=float, default=10e-9, help='Primary capacitance (F)') |
| 33 | + parser.add_argument('--C2', type=float, default=50e-12, help='Secondary capacitance (F)') |
| 34 | + parser.add_argument('--R1', type=float, default=0.2, help='Primary resistance (Ohm)') |
| 35 | + parser.add_argument('--R2', type=float, default=1000, help='Secondary resistance (Ohm)') |
| 36 | + parser.add_argument('--M', type=float, default=2e-4, help='Mutual inductance (H)') |
| 37 | + parser.add_argument('--V0', type=float, default=5000, help='Initial voltage on C1 (V)') |
| 38 | + parser.add_argument('--tmax', type=float, default=0.0005, help='Simulation time (s)') |
| 39 | + parser.add_argument('--field', action='store_true', help='Plot magnetic field visualization') |
| 40 | + return parser.parse_args() |
| 41 | +
|
| 42 | +def circuit(t, y, args): |
| 43 | + I1, Vc1, I2, Vc2 = y |
| 44 | + dI2_dt_val = (Vc2 - args.R2 * I2) / args.L2 |
| 45 | + dI1_dt = (Vc1 - args.R1 * I1 - args.M * dI2_dt_val) / args.L1 |
| 46 | + dVc1_dt = -I1 / args.C1 |
| 47 | + dI2_dt = (Vc2 - args.R2 * I2 - args.M * dI1_dt) / args.L2 |
| 48 | + dVc2_dt = -I2 / args.C2 |
| 49 | + return [dI1_dt, dVc1_dt, dI2_dt, dVc2_dt] |
| 50 | +
|
| 51 | +def plot_currents(t_eval, I1, I2): |
| 52 | + plt.figure(figsize=(10, 6)) |
| 53 | + plt.plot(t_eval * 1e3, I1, label='Primary Current (I1)') |
| 54 | + plt.plot(t_eval * 1e3, I2, label='Secondary Current (I2)') |
| 55 | + plt.xlabel('Time (ms)') |
| 56 | + plt.ylabel('Current (A)') |
| 57 | + plt.title('Coupled RLC Circuit Simulation (Tesla Coil)') |
| 58 | + plt.legend() |
| 59 | + plt.grid() |
| 60 | + plt.tight_layout() |
| 61 | + plt.show() |
| 62 | +
|
| 63 | +def plot_magnetic_field(I2_peak): |
| 64 | + # Visualize magnetic field in the plane around the coil |
| 65 | + mu0 = 4 * np.pi * 1e-7 |
| 66 | + N = 100 # coil turns |
| 67 | + R = 0.1 # coil radius (m) |
| 68 | + x = np.linspace(-0.2, 0.2, 100) |
| 69 | + y = np.linspace(-0.2, 0.2, 100) |
| 70 | + X, Y = np.meshgrid(x, y) |
| 71 | + Z = 0 # field in XY plane at center |
| 72 | + r = np.sqrt(X**2 + Y**2 + Z**2) |
| 73 | + Bz = mu0 * N * I2_peak * R**2 / (2 * (r**3 + 1e-12)) # add epsilon to avoid division by zero |
| 74 | + plt.figure(figsize=(6, 5)) |
| 75 | + plt.contourf(X, Y, Bz, 50, cmap='viridis') |
| 76 | + plt.colorbar(label='Magnetic Field Bz (T)') |
| 77 | + plt.title('Magnetic Field around Secondary Coil (Peak)') |
| 78 | + plt.xlabel('x (m)') |
| 79 | + plt.ylabel('y (m)') |
| 80 | + plt.axis('equal') |
| 81 | + plt.show() |
| 82 | +
|
| 83 | +def main(): |
| 84 | + args = parse_args() |
| 85 | + y0 = [0, args.V0, 0, 0] |
| 86 | + t_span = (0, args.tmax) |
| 87 | + t_eval = np.linspace(*t_span, 2000) |
| 88 | + sol = solve_ivp(lambda t, y: circuit(t, y, args), t_span, y0, t_eval=t_eval, method='RK45') |
| 89 | + I1, Vc1, I2, Vc2 = sol.y |
| 90 | + plot_currents(t_eval, I1, I2) |
| 91 | + if args.field: |
| 92 | + I2_peak = np.max(np.abs(I2)) |
| 93 | + plot_magnetic_field(I2_peak) |
| 94 | +
|
| 95 | +if __name__ == "__main__": |
| 96 | + main() |
| 97 | +``` |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## 3. requirements.txt |
| 102 | + |
| 103 | +``` |
| 104 | +numpy |
| 105 | +scipy |
| 106 | +matplotlib |
| 107 | +``` |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## 4. README.md |
| 112 | + |
| 113 | +```markdown |
| 114 | +# Tesla Coil RLC Simulation |
| 115 | +
|
| 116 | +This Python project simulates a Tesla coil as a coupled RLC circuit and optionally visualizes the magnetic field around the secondary coil. |
| 117 | +
|
| 118 | +## Features |
| 119 | +
|
| 120 | +- Command-line parameter input for circuit values (inductance, capacitance, resistance, etc.) |
| 121 | +- Visualizes primary and secondary coil currents |
| 122 | +- Optional magnetic field visualization around the secondary coil |
| 123 | +
|
| 124 | +## Usage |
| 125 | +
|
| 126 | +1. Install dependencies: |
| 127 | +
|
| 128 | + ``` |
| 129 | + pip install -r requirements.txt |
| 130 | + ``` |
| 131 | +
|
| 132 | +2. Run with default parameters: |
| 133 | +
|
| 134 | + ``` |
| 135 | + python main.py |
| 136 | + ``` |
| 137 | +
|
| 138 | +3. Customize parameters via command line (example): |
| 139 | +
|
| 140 | + ``` |
| 141 | + python main.py --L1 25e-6 --L2 90e-3 --C1 12e-9 --R2 1500 --V0 6000 --tmax 0.001 |
| 142 | + ``` |
| 143 | +
|
| 144 | +4. Add `--field` to visualize the magnetic field: |
| 145 | + |
| 146 | + ``` |
| 147 | + python main.py --field |
| 148 | + ``` |
| 149 | + |
| 150 | +## Parameters |
| 151 | + |
| 152 | +- `--L1`: Primary inductance (H) |
| 153 | +- `--L2`: Secondary inductance (H) |
| 154 | +- `--C1`: Primary capacitance (F) |
| 155 | +- `--C2`: Secondary capacitance (F) |
| 156 | +- `--R1`: Primary resistance (Ohm) |
| 157 | +- `--R2`: Secondary resistance (Ohm) |
| 158 | +- `--M`: Mutual inductance (H) |
| 159 | +- `--V0`: Initial voltage on C1 (V) |
| 160 | +- `--tmax`: Simulation time (s) |
| 161 | +- `--field`: Plot magnetic field visualization |
| 162 | + |
| 163 | +## License |
| 164 | + |
| 165 | +MIT License |
| 166 | +``` |
| 167 | +
|
| 168 | +--- |
| 169 | +
|
| 170 | +**This template is ready to use as a GitHub repository.** |
| 171 | +If you’d like more advanced field visualization (e.g., vector fields or 3D), or integration with Jupyter notebooks, let me know! |
0 commit comments