-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
158 lines (127 loc) · 4.59 KB
/
Main.py
1
2
3
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# import libraries
import numpy as np
import time
import matplotlib.pyplot as plt
import datetime
import os
# Ask angle
angle = input('Enter the angle in degrees: ')
try:
angle = int(angle)
except:
print("For now Angle must be int. Enter to exit.")
input()
exit()
# Convert angle to radians
theta = np.radians(angle)
# Ask initial velocity
v0 = input('Enter the initial velocity in m/s: ')
v0 = float(v0)
# Calculate Inicial x and y velocities
v0x = v0 * np.cos(theta)
v0y = v0 * np.sin(theta)
# Mass of the projectile
m = input('Enter the mass in kg: ')
m = float(m)
# Gravitational acceleration
g = input('Enter the gravitational acceleration constant in m/s: ')
g = float(g)
# Density of the fluid
rho = input('Enter the density of the fluid in kg/m³: ')
rho = float(rho)
# Radius of the projectile
radius = input('Enter the radius of the projectile in meters: ')
radius = float(radius)
# Cross sectional area of the projectile
A = np.pi * (radius**2)
# Calculate k
k = (1/2) * 0.47 * rho * A
# Ask Timestep
print("Enter the time step in seconds. The quality of the calculation will be better as the time step decreases.")
print("However, if you enter a value that is too small, such as 0.0000001, the program may take a long time to finish depending on the speed of your cpu:")
delta_t = input()
delta_t = float(delta_t)
# Ask initial positions
x0 = int(input('Enter the initial x position: '))
y0 = int(input('Enter the initial y position: '))
# Ask for name
name = input('Enter the name of the calculation. This will be the name of the log file. Character limit is 20: ')
# Set time to 0 seconds
t=0
print('Calculating...')
time.sleep(1)
list_of_positions = []
# Equations
u1 = x0
u2 = v0x
u3 = y0
u4 = v0y
# Important for finding approximated time to highest point
min_u4 = abs(u4)
# Start log
print("Starting the log. This is for viewing what is happening and making sure the program don't freeze.")
time.sleep(5)
print("==========< LOG START >==========")
# Log
def log(type, log):
# Get current time
current_time = str(datetime.datetime.now())
# Make the log
new_log = "»»" + current_time + ": " + type + ": " + log
# Make the files directory
folder_path = os.path.expandvars("%appdata%") + "\\Projectile Trajectory"
if os.path.exists(folder_path):
# Path exists. Create log file
log_path = os.path.join(folder_path, name + "_log" + ".txt")
# Wite the log to the log file
with open(log_path, "a") as file:
file.write(str(new_log))
file.write("")
else:
os.makedirs(folder_path, exist_ok=True)
log_path = os.path.join(folder_path, name + "_log" + ".txt")
with open(log_path, "a") as file:
file.write(str(new_log))
file.write("")
print(new_log)
while True:
u1dot = u2
u2dot = - (k/m) * np.sqrt(u2**2 + u4**2)*u2 * np.cos(theta)
u3dot = u4
u4dot = - (k/m) * np.sqrt(u2**2 + u4**2)*u4 * np.sin(theta) - g
list_of_positions += [(u1, u3),]
log("Info", "At time t = " + str(t) + ": x = " + str(u1) + ", y = " + str(u3) + ", x velocity = " + str(u1dot) + ", y velocity = " + str(u3dot) + ", x acceleration = " + str(u2dot) + ", y acceleration = " + str(u4dot))
if (u3 < 0 or u3 == 0) and t != 0:
folder_path = os.path.expandvars("%appdata%") + "\\Projectile Trajectory"
file_path = os.path.join(folder_path, name + ".txt")
with open(file_path, "a") as file:
file.write(str(list_of_positions))
log("Success", "Projectile hit the ground. All coordinates have been saved to: %appdata%\\Projectile Trajectory\\")
break
if abs(u4) < min_u4: # Update min_u4 if a smaller value is found
min_u4 = abs(u4)
if u4 == 0:
hp = t
log("Success", "Found the exact time when the projectile reaches the highest point.")
u1 = u1 + u1dot * delta_t
u2 = u2 + u2dot * delta_t
u3 = u3 + u3dot * delta_t
u4 = u4 + u4dot * delta_t
t += delta_t
print("Time to hit the ground: ", t)
if 'hp' in locals():
print("Time to highest point: ", hp)
else:
log("Warning", "Could not find the exact time when the projectile reaches the highest point. Going to print the most approximated result.")
print("Time to highest point: ", min_u4)
# Extract x and y coordinates from the list of positions
x = [position[0] for position in list_of_positions]
y = [position[1] for position in list_of_positions]
# Create a line plot
plt.plot(x, y, marker='.')
# Add labels and title
plt.xlabel('x in meters')
plt.ylabel('y in meters')
plt.title('Trajectory of a projectile')
# Show the plot
plt.show()