Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,77 +1,51 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import plotly.graph_objs as go
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
go.
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
fig_wave.add_trace(
|
| 52 |
-
go.Scatter(x=np.linspace(0, 1, 1000), y=magnetic_field(np.linspace(0, 1, 1000), 0),
|
| 53 |
-
mode='lines', name='Magnetic Field')
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
# Set the title and axis labels for the wave figure
|
| 57 |
-
fig_wave.update_layout(
|
| 58 |
-
title='Electromagnetic Wave',
|
| 59 |
-
xaxis_title='Position',
|
| 60 |
-
yaxis_title='Field Strength'
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
# Add a slider for adjusting time
|
| 64 |
-
t_slider = fig_wave.add_slider(
|
| 65 |
-
dict(steps=[dict(method='animate', args=[None, {'frame': {'duration': 50, 'redraw': True}}])],
|
| 66 |
-
transition={'duration': 0},
|
| 67 |
-
x=0, y=0,
|
| 68 |
-
len=1.0,
|
| 69 |
-
currentvalue=dict(visible=True, xanchor='left'),
|
| 70 |
-
font=dict(size=10, color='#666')
|
| 71 |
-
),
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
# Define the animation frames
|
| 75 |
-
frames = [go.Frame(data=[go.Scatter(x=np.linspace(0, 1, 1000), y=electric_field(np.linspace(0, 1, 1000), t),
|
| 76 |
-
mode='lines', name='Electric Field'),
|
| 77 |
-
go.Scatter(x=np.linspace(0, 1, 1000), y=magnetic_field(np.linspace(0, 1,
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import plotly.graph_objs as go
|
| 3 |
+
from plotly.subplots import make_subplots
|
| 4 |
+
import streamlit as st
|
| 5 |
|
| 6 |
+
def calculate_wave(wavelength, amplitude, frequency, num_periods, time_step):
|
| 7 |
+
k = 2*np.pi/wavelength
|
| 8 |
+
omega = 2*np.pi*frequency
|
| 9 |
+
period = 1/frequency
|
| 10 |
+
time_array = np.arange(0, num_periods*period, time_step)
|
| 11 |
+
wave = amplitude * np.sin(k * np.arange(0, 1, wavelength/1000)[:, None] - omega * time_array)
|
| 12 |
+
return time_array, wave
|
| 13 |
+
|
| 14 |
+
def plot_3d_wave(wavelength, amplitude, frequency):
|
| 15 |
+
fig = make_subplots(rows=1, cols=2,
|
| 16 |
+
specs=[[{'type': 'surface'}, {'type': 'surface'}]],
|
| 17 |
+
subplot_titles=('Electric field', 'Magnetic field'),
|
| 18 |
+
)
|
| 19 |
+
time_step = 1/(100*frequency)
|
| 20 |
+
num_periods = 5
|
| 21 |
+
time_array, wave = calculate_wave(wavelength, amplitude, frequency, num_periods, time_step)
|
| 22 |
+
E_x = wave*np.cos(2*np.pi*time_array*frequency)
|
| 23 |
+
E_y = np.zeros_like(E_x)
|
| 24 |
+
E_z = np.zeros_like(E_x)
|
| 25 |
+
B_x = np.zeros_like(E_x)
|
| 26 |
+
B_y = -wave*np.sin(2*np.pi*time_array*frequency)
|
| 27 |
+
B_z = np.zeros_like(E_x)
|
| 28 |
+
|
| 29 |
+
fig.add_trace(go.Surface(x=wave, y=E_x, z=E_y, colorscale='Blues'), row=1, col=1)
|
| 30 |
+
fig.add_trace(go.Surface(x=wave, y=B_x, z=B_y, colorscale='Reds'), row=1, col=2)
|
| 31 |
+
fig.update_layout(scene_aspectratio=dict(x=1, y=1, z=1), width=800, height=400,
|
| 32 |
+
scene=dict(xaxis_title='Wave', yaxis_title='E_x', zaxis_title='E_y'),
|
| 33 |
+
scene2=dict(xaxis_title='Wave', yaxis_title='B_x', zaxis_title='B_y'),
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
fig.update_xaxes(range=[0, wavelength], row=1, col=1)
|
| 37 |
+
fig.update_xaxes(range=[0, wavelength], row=1, col=2)
|
| 38 |
+
fig.update_yaxes(range=[-amplitude, amplitude], row=1, col=1)
|
| 39 |
+
fig.update_yaxes(range=[-amplitude, amplitude], row=1, col=2)
|
| 40 |
+
fig.update_zaxes(range=[-amplitude, amplitude], row=1, col=1)
|
| 41 |
+
fig.update_zaxes(range=[-amplitude, amplitude], row=1, col=2)
|
| 42 |
+
|
| 43 |
+
return fig
|
| 44 |
+
|
| 45 |
+
wavelength = st.slider('Wavelength', 0.1, 10.0, 1.0)
|
| 46 |
+
amplitude = st.slider('Amplitude', 0.1, 1.0, 0.5)
|
| 47 |
+
frequency = st.slider('Frequency', 0.1, 10.0, 1.0)
|
| 48 |
+
|
| 49 |
+
fig = plot_3d_wave(wavelength, amplitude, frequency)
|
| 50 |
+
|
| 51 |
+
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|