Real-time Transmission Line Electron Density Simulation with Adjustable Parameters
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button, RadioButtons, Slider
import matplotlib.animation as animation
# Transmission line parameters
line_length = 100 # Length of transmission line
simulation_time = 1000 # Total simulation time
c = 1.0 # Base wave propagation speed
Z0 = 50 # Characteristic impedance
# Initialize electron density arrays
upper_density = np.zeros(line_length)
lower_density = np.zeros(line_length)
# Initialize states and parameters
switch_state = False
load_type = "Open" # Options: "Open", "Short", "Matched"
is_lossless = True
simulation_speed = 1.0
physical_time = 0.0 # in nanoseconds
time_unit = "ns"
# Reflection coefficients
reflection_coefficients = {
"Open": 1.0,
"Short": -1.0,
"Matched": 0.0
}
# Create figure and axes
fig = plt.figure(figsize=(14, 8))
ax = plt.subplot2grid((5, 1), (0, 0), rowspan=3)
plt.subplots_adjust(left=0.1, bottom=0.3)
# Create line plots
upper_line, = ax.plot(np.arange(line_length), upper_density, 'b-', lw=2, label='Upper Density')
lower_line, = ax.plot(np.arange(line_length), lower_density, 'r-', lw=2, label='Lower Density')
# Time display text
time_text = ax.text(0.02, 0.95, f'Time: 0.0 {time_unit}', transform=ax.transAxes, fontsize=12)
# Set up the plot
ax.set_xlim(0, line_length)
ax.set_ylim(-1.5, 1.5)
ax.set_xlabel('Position on Transmission Line')
ax.set_ylabel('Electron Density')
ax.set_title('Transmission Line Electron Density Simulation')
ax.legend()
ax.grid(True)
# Add switch button
switch_ax = plt.axes([0.15, 0.15, 0.15, 0.05])
switch_button = Button(switch_ax, 'Switch: OFF')
# Add load type radio buttons
load_ax = plt.axes([0.7, 0.1, 0.15, 0.15])
load_radio = RadioButtons(load_ax, ('Open', 'Short', 'Matched'), active=0)
# Add lossless/lossy radio buttons
loss_ax = plt.axes([0.4, 0.1, 0.15, 0.15])
loss_radio = RadioButtons(loss_ax, ('Lossless', 'Lossy'), active=0)
# Add simulation speed slider
speed_ax = plt.axes([0.2, 0.05, 0.6, 0.03])
speed_slider = Slider(speed_ax, 'Simulation Speed', 0.1, 5.0, valinit=1.0, valstep=0.1)
# Add attenuation factor slider (for lossy line)
atten_ax = plt.axes([0.2, 0.01, 0.6, 0.03])
atten_slider = Slider(atten_ax, 'Attenuation Factor', 0.0, 0.05, valinit=0.01, valstep=0.001)
# Wave propagation history
wave_history = []
def update_switch(event):
global switch_state
switch_state = not switch_state
switch_button.label.set_text(f'Switch: {"ON" if switch_state else "OFF"}')
def update_load(label):
global load_type
load_type = label
def update_loss(label):
global is_lossless
is_lossless = (label == 'Lossless')
def update_speed(val):
global simulation_speed
simulation_speed = val
def update_attenuation(val):
# No need to store this as a global, we'll read it directly from the slider
switch_button.on_clicked(update_switch)
load_radio.on_clicked(update_load)
loss_radio.on_clicked(update_loss)
speed_slider.on_changed(update_speed)
atten_slider.on_changed(update_attenuation)
def animate(frame):
global upper_density, lower_density, wave_history, physical_time
# Update physical time (assuming each frame is 0.05 seconds in real time)
# Adjust based on simulation_speed
time_increment = 0.1 * simulation_speed # ns per frame
physical_time += time_increment
time_text.set_text(f'Time: {physical_time:.2f} {time_unit}')
# Shift existing waves
for i in range(len(wave_history)):
wave_history[i]['position'] += wave_history[i]['direction'] * c * simulation_speed
# Apply attenuation for lossy line
if not is_lossless:
attenuation = atten_slider.val
wave_history[i]['amplitude'] *= (1.0 - attenuation * simulation_speed)
# Generate new wave if switch is on
if switch_state and frame % 5 == 0:
wave_history.append({
'position': 0,
'direction': 1, # Moving right
'amplitude': 1.0,
'width': 5
})
# Remove waves that have left the transmission line or have negligible amplitude
wave_history = [wave for wave in wave_history if
(0 <= wave['position'] <= line_length) and
(abs(wave['amplitude']) > 0.01)]
# Handle reflections at the load end
for wave in wave_history:
if wave['position'] >= line_length and wave['direction'] > 0:
reflection_coef = reflection_coefficients[load_type]
if reflection_coef != 0:
wave['direction'] = -1 # Reflect back
wave['amplitude'] *= reflection_coef
# Reset densities
upper_density = np.zeros(line_length)
lower_density = np.zeros(line_length)
# Calculate electron density based on all waves
for wave in wave_history:
pos = int(wave['position'])
width = wave['width']
amp = wave['amplitude']
# Create a Gaussian-like pulse
for i in range(max(0, pos-width), min(line_length, pos+width)):
distance = abs(i - pos)
value = amp * np.exp(-0.5 * (distance/width)**2)
if wave['direction'] > 0: # Right-moving wave
upper_density[i] += value
lower_density[i] -= value
else: # Left-moving wave
upper_density[i] -= value
lower_density[i] += value
# Update the plot
upper_line.set_ydata(upper_density)
lower_line.set_ydata(lower_density)
return upper_line, lower_line, time_text
ani = animation.FuncAnimation(fig, animate, frames=simulation_time, interval=50, blit=True)
plt.show()