amkj84 commited on
Commit
68f221e
·
verified ·
1 Parent(s): bb4c6ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -71
app.py CHANGED
@@ -5,85 +5,77 @@ import time
5
 
6
  # Page Configurations
7
  st.set_page_config(
8
- page_title="Gun Firing Simulation",
9
  layout="centered"
10
  )
11
 
12
  # Title and Description
13
- st.title("Gun Firing Simulation")
14
- st.write("This app simulates the firing of a bullet from a gun. Watch the bullet travel along its trajectory!")
15
 
16
  # Parameters Input
17
  st.sidebar.header("Simulation Parameters")
18
- initial_velocity = st.sidebar.number_input("Initial Velocity (m/s):", min_value=1, max_value=1000, value=500, step=10)
19
- angle = st.sidebar.slider("Firing Angle (degrees):", min_value=0, max_value=90, value=45)
20
- gravity = st.sidebar.number_input("Gravity (m/s²):", min_value=1.0, max_value=20.0, value=9.8, step=0.1)
 
21
 
22
- def simulate_bullet():
23
  # Constants
24
- angle_rad = np.radians(angle)
25
-
26
- # Time of flight
27
- time_of_flight = (2 * initial_velocity * np.sin(angle_rad)) / gravity
28
-
29
- # Maximum range
30
- max_range = (initial_velocity**2 * np.sin(2 * angle_rad)) / gravity
31
-
32
- # Maximum height
33
- max_height = (initial_velocity**2 * (np.sin(angle_rad)**2)) / (2 * gravity)
34
-
35
- # Time intervals
36
- t = np.linspace(0, time_of_flight, num=500)
37
-
38
- # Trajectory equations
39
- x = initial_velocity * np.cos(angle_rad) * t
40
- y = initial_velocity * np.sin(angle_rad) * t - 0.5 * gravity * t**2
41
-
42
- return x, y, max_range, max_height, time_of_flight
 
 
43
 
44
  # Simulate and Plot
45
- if st.button("Fire Bullet!"):
46
- st.write("## Bullet Trajectory")
47
- st.write("Simulating... Hold on!")
48
-
49
- x, y, max_range, max_height, time_of_flight = simulate_bullet()
50
-
51
- # Plot
52
- fig, ax = plt.subplots()
53
- ax.plot(x, y, label="Bullet Trajectory")
54
- ax.axhline(0, color='black', linewidth=0.5, linestyle='--')
55
- ax.set_xlabel("Distance (m)")
56
- ax.set_ylabel("Height (m)")
57
- ax.set_title("Bullet Trajectory Simulation")
58
- ax.legend()
59
-
60
- st.pyplot(fig)
61
-
62
- # Display parameters
63
- st.write(f"**Maximum Range:** {max_range:.2f} meters")
64
- st.write(f"**Maximum Height:** {max_height:.2f} meters")
65
- st.write(f"**Time of Flight:** {time_of_flight:.2f} seconds")
66
-
67
- # Animation Simulation
68
- st.write("### Animation of the Bullet")
69
- progress_bar = st.progress(0)
70
- bullet_fig, bullet_ax = plt.subplots()
71
-
72
- for i in range(len(x)):
73
- if y[i] < 0: # Stop animation if bullet hits the ground
74
- break
75
- bullet_ax.clear()
76
- bullet_ax.plot(x, y, label="Bullet Trajectory")
77
- bullet_ax.plot(x[i], y[i], 'ro', label="Bullet Position")
78
- bullet_ax.axhline(0, color='black', linewidth=0.5, linestyle='--')
79
- bullet_ax.set_xlim(0, max(x))
80
- bullet_ax.set_ylim(0, max(y) + 5)
81
- bullet_ax.set_xlabel("Distance (m)")
82
- bullet_ax.set_ylabel("Height (m)")
83
- bullet_ax.set_title("Bullet Animation")
84
- bullet_ax.legend()
85
- st.pyplot(bullet_fig)
86
- progress_bar.progress(int((i / len(x)) * 100))
87
- time.sleep(0.01)
88
-
89
- st.write("Simulation Complete!")
 
5
 
6
  # Page Configurations
7
  st.set_page_config(
8
+ page_title="CBT: Gun Firing Internal Mechanism",
9
  layout="centered"
10
  )
11
 
12
  # Title and Description
13
+ st.title("CBT: Gun Firing Internal Mechanism")
14
+ st.write("This app simulates the internal mechanism of a gun firing process, including the combustion and pressure dynamics inside the chamber.")
15
 
16
  # Parameters Input
17
  st.sidebar.header("Simulation Parameters")
18
+ chamber_pressure = st.sidebar.number_input("Chamber Pressure (MPa):", min_value=1, max_value=100, value=50, step=1)
19
+ barrel_length = st.sidebar.number_input("Barrel Length (cm):", min_value=10, max_value=200, value=50, step=5)
20
+ bullet_mass = st.sidebar.number_input("Bullet Mass (grams):", min_value=1, max_value=50, value=10, step=1)
21
+ gas_expansion_ratio = st.sidebar.slider("Gas Expansion Ratio:", min_value=1.0, max_value=5.0, value=1.5, step=0.1)
22
 
23
+ def simulate_internal_mechanism():
24
  # Constants
25
+ chamber_pressure_pa = chamber_pressure * 1e6 # Convert MPa to Pa
26
+ barrel_length_m = barrel_length / 100 # Convert cm to meters
27
+ bullet_mass_kg = bullet_mass / 1000 # Convert grams to kg
28
+
29
+ # Time array for simulation (arbitrary resolution)
30
+ time_steps = np.linspace(0, 0.01, num=500) # 10 ms simulation
31
+
32
+ # Pressure dynamics (simplified as linear decay for simulation purposes)
33
+ pressure_profile = chamber_pressure_pa * np.exp(-time_steps / (gas_expansion_ratio * 0.002))
34
+
35
+ # Force on bullet
36
+ force_profile = pressure_profile * (np.pi * (0.01)**2) # Assuming a barrel diameter of 2 cm
37
+
38
+ # Acceleration of bullet
39
+ acceleration_profile = force_profile / bullet_mass_kg
40
+
41
+ # Velocity and displacement
42
+ velocity = np.cumsum(acceleration_profile * np.diff(np.insert(time_steps, 0, 0)))
43
+ displacement = np.cumsum(velocity * np.diff(np.insert(time_steps, 0, 0)))
44
+
45
+ return time_steps, pressure_profile, velocity, displacement
46
 
47
  # Simulate and Plot
48
+ if st.button("Simulate Gun Firing!"):
49
+ st.write("## Internal Mechanism Simulation")
50
+ st.write("Simulating the internal dynamics of the gun firing process...")
51
+
52
+ time_steps, pressure_profile, velocity, displacement = simulate_internal_mechanism()
53
+
54
+ # Plot Chamber Pressure
55
+ fig1, ax1 = plt.subplots()
56
+ ax1.plot(time_steps * 1000, pressure_profile / 1e6, label="Chamber Pressure")
57
+ ax1.set_xlabel("Time (ms)")
58
+ ax1.set_ylabel("Pressure (MPa)")
59
+ ax1.set_title("Chamber Pressure Over Time")
60
+ ax1.legend()
61
+ st.pyplot(fig1)
62
+
63
+ # Plot Bullet Velocity
64
+ fig2, ax2 = plt.subplots()
65
+ ax2.plot(time_steps * 1000, velocity, label="Bullet Velocity")
66
+ ax2.set_xlabel("Time (ms)")
67
+ ax2.set_ylabel("Velocity (m/s)")
68
+ ax2.set_title("Bullet Velocity Over Time")
69
+ ax2.legend()
70
+ st.pyplot(fig2)
71
+
72
+ # Plot Bullet Displacement
73
+ fig3, ax3 = plt.subplots()
74
+ ax3.plot(time_steps * 1000, displacement, label="Bullet Displacement")
75
+ ax3.set_xlabel("Time (ms)")
76
+ ax3.set_ylabel("Displacement (m)")
77
+ ax3.set_title("Bullet Displacement Over Time")
78
+ ax3.legend()
79
+ st.pyplot(fig3)
80
+
81
+ st.write("Simulation Complete! Explore the dynamics of the gun's internal firing mechanism.")