jpoptum commited on
Commit
259f356
1 Parent(s): 04a0096

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -27
app.py CHANGED
@@ -2,33 +2,76 @@ import streamlit as st
2
  import numpy as np
3
  import plotly.graph_objs as go
4
 
5
- st.title("Electromagnetic Wave Simulation")
6
-
7
- # Set up initial values
8
- c = 3e8 # Speed of light (m/s)
9
- lambda_ = 1e-9 # Wavelength (m)
10
- omega = 2 * np.pi * c / lambda_ # Angular frequency
11
- k = omega / c # Wave number
12
- E0 = 1 # Amplitude of electric field
13
- B0 = E0 / c # Amplitude of magnetic field
14
- t = np.linspace(0, 1e-14, 1000) # Time (s)
15
- z = np.linspace(0, 1e-6, 100) # Position (m)
16
-
17
- # Calculate the electric and magnetic fields
18
- E = E0 * np.sin(k * z - omega * t[:, np.newaxis])
19
- B = B0 * np.sin(k * z - omega * t[:, np.newaxis])
20
-
21
- # Create a 3D surface plot
22
- fig = go.Figure(
23
- go.Surface(x=z, y=t, z=E.T, colorscale='Blues', showscale=False, name="Electric Field"),
24
- go.Surface(x=z, y=t, z=B.T, colorscale='Reds', showscale=False, name="Magnetic Field"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  )
26
- fig.update_layout(
27
- title="Electromagnetic Wave Simulation",
28
- scene=dict(xaxis_title="Position (m)", yaxis_title="Time (s)", zaxis_title="Field Strength"),
29
- margin=dict(l=0, r=0, b=0, t=40),
30
- height=700,
 
 
 
 
 
 
 
31
  )
32
 
33
- # Display the plot
34
- st.plotly_chart(fig, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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,