jpoptum commited on
Commit
861a920
1 Parent(s): 259f356

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -74
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
- st.title('Electromagnetic Wave Simulation with Plotly')
6
-
7
- # Set the default wavelength and amplitude values
8
- wavelength_default = 1
9
- amplitude_default = 1
10
-
11
- # Create sliders for adjusting wavelength and amplitude
12
- wavelength = st.slider('Wavelength', min_value=0.1, max_value=10.0, value=wavelength_default, step=0.1)
13
- amplitude = st.slider('Amplitude', min_value=0.1, max_value=5.0, value=amplitude_default, step=0.1)
14
-
15
- # Define the wave function for the electric field
16
- def electric_field(x, t):
17
- return amplitude * np.sin(2*np.pi*x/wavelength - 2*np.pi*t)
18
-
19
- # Define the wave function for the magnetic field
20
- def magnetic_field(x, t):
21
- return amplitude * np.sin(2*np.pi*x/wavelength - 2*np.pi*t + np.pi/2)
22
-
23
- # Create a plotly figure for the electric field
24
- fig_electric = go.Figure()
25
-
26
- # Add a line for the electric field
27
- fig_electric.add_trace(
28
- go.Scatter(x=np.linspace(0, 1, 1000), y=electric_field(np.linspace(0, 1, 1000), 0),
29
- mode='lines', name='Electric Field')
30
- )
31
-
32
- # Create a plotly figure for the magnetic field
33
- fig_magnetic = go.Figure()
34
-
35
- # Add a line for the magnetic field
36
- fig_magnetic.add_trace(
37
- go.Scatter(x=np.linspace(0, 1, 1000), y=magnetic_field(np.linspace(0, 1, 1000), 0),
38
- mode='lines', name='Magnetic Field')
39
- )
40
-
41
- # Create a plotly figure for the wave
42
- fig_wave = go.Figure()
43
-
44
- # Add a line for the electric field
45
- fig_wave.add_trace(
46
- go.Scatter(x=np.linspace(0, 1, 1000), y=electric_field(np.linspace(0, 1, 1000), 0),
47
- mode='lines', name='Electric Field')
48
- )
49
-
50
- # Add a line for the magnetic field
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)