-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
226 lines (174 loc) · 8.29 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import numpy as np
from matplotlib import pyplot as plt
from vis2 import MeshVisual
class Rigid:
def __init__(self, position):
self.pos = np.asarray(position, dtype=float)
self.initial_pos = self.pos
self.forces = np.zeros(3, dtype=float)
self.connections = []
def attach(self, spring):
self.connections.append(spring)
class PointMass(Rigid):
def __init__(self, position, mass):
super().__init__(position)
self.vel = np.zeros(3, dtype=float)
self.acc = np.zeros(3, dtype=float)
self.mass = mass
def react(self, t):
self.acc = self.forces / self.mass
self.vel = self.vel + self.acc * t
self.pos = self.pos + self.vel * t
def get_kinetic(self):
return 0.5 * self.mass * ((np.linalg.norm(self.vel))**2)
class Anchor(Rigid):
def __init__(self, position):
super().__init__(position)
def react(self, t):
pass
def get_kinetic(self):
return 0
class Spring:
def __init__(self, rigid_n, rigid_p, natural_length, k):
self.k = k
self.natural_length = natural_length
self.length = natural_length
self.rigids = [rigid_n, rigid_p]
def find_forces(self):
length_vector = self.rigids[1].pos - self.rigids[0].pos
self.length = np.linalg.norm(length_vector)
return (-1 * (self.length - self.natural_length) * self.k) * length_vector / self.length
def get_potential(self):
return 0.5 * self.k * ((self.length - self.natural_length) ** 2)
class Mesh:
def __init__(self, dim):
self.m = np.empty(dim, dtype=object)
self.spring_list = []
def create_pointmass(self, slot, position, mass):
self.m[tuple(slot)] = PointMass(position, mass)
def create_anchor(self, slot, position):
self.m[tuple(slot)] = Anchor(position)
def create_rest_spring(self, slot_n, slot_p, k):
rigid_n, rigid_p = self.m[tuple(slot_n)], self.m[tuple(slot_p)]
self.spring_list.append(Spring(rigid_n, rigid_p, np.linalg.norm(rigid_p.pos - rigid_n.pos), k))
rigid_n.attach(self.spring_list[-1])
rigid_p.attach(self.spring_list[-1])
class Instance:
def __init__(self, mesh, tick_size, length_of_time):
self.mesh = mesh
self.t = tick_size
self.extent = length_of_time
self.time_axis = np.arange(0, self.extent, self.t)
self.tracked_objects = None
self.tracked_axis = None
self.motion_tracker = None
self.energy_tracker = None
self.force_d = dict()
self.visual = MeshVisual(mesh, 1)
def initialize_tracking(self, tracked_object_locs, tracked_axis):
self.energy_tracker = np.zeros([int(self.extent / self.t), 3])
for spring in self.mesh.spring_list:
self.energy_tracker[0, 0] = self.energy_tracker[0, 0] + spring.get_potential()
for rigid in np.nditer(self.mesh.m, flags=["refs_ok"]):
rigid = rigid.item()
if rigid is not None:
self.energy_tracker[0, 1] = self.energy_tracker[0, 1] + rigid.get_kinetic()
self.energy_tracker[0, 2] = self.energy_tracker[0, 0] + self.energy_tracker[0, 1]
self.motion_tracker = np.zeros([int(self.extent / self.t), len(tracked_object_locs)])
self.tracked_objects = [self.mesh.m[tuple(loc)] for loc in tracked_object_locs]
self.tracked_axis = tracked_axis
for i, axis in enumerate(self.tracked_axis):
self.motion_tracker[0, i] = self.tracked_objects[i].pos[axis-1]
def initialize_displacement(self, starting_objects_locs, displacements):
for loc, disp in zip(starting_objects_locs, displacements):
self.mesh.m[tuple(loc)].pos = self.mesh.m[tuple(loc)].pos + disp
def initialize_static_load(self, loaded_objects_locs, forces):
for loc, force in zip(loaded_objects_locs, forces):
self.force_d[self.mesh.m[tuple(loc)]] = force
def simulate(self):
for i, tick in enumerate(np.arange(self.t, self.extent, self.t)):
for rigid, force in self.force_d.items():
rigid.forces = rigid.forces + force
for spring in self.mesh.spring_list:
force_applicator = spring.find_forces()
spring.rigids[1].forces = spring.rigids[1].forces + force_applicator
spring.rigids[0].forces = spring.rigids[0].forces + -1 * force_applicator
self.energy_tracker[i+1, 0] = self.energy_tracker[i+1, 0] + spring.get_potential()
for rigid in np.nditer(self.mesh.m, flags=["refs_ok"]):
rigid = rigid.item()
if rigid is not None:
self.energy_tracker[i+1, 1] = self.energy_tracker[i+1, 1] + rigid.get_kinetic()
rigid.react(self.t)
rigid.forces = np.zeros(3, dtype=float)
for j, (rigid, axis) in enumerate(zip(self.tracked_objects, self.tracked_axis)):
self.motion_tracker[i+1, j] = self.tracked_objects[j].pos[axis - 1]
self.energy_tracker[i + 1, 2] = self.energy_tracker[i + 1, 0] + self.energy_tracker[i + 1, 1]
self.visual.update()
def graph_motion(self):
self.sin_set = np.sin(self.time_axis)
motion_plot = plt.figure()
ax = motion_plot.add_subplot()
for j, rigid in enumerate(self.tracked_objects):
ax.plot(self.time_axis, self.motion_tracker[:, j], label=f"Rigid at {rigid.initial_pos}")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Position (m)")
ax.set_title("Displacements in a Hookean System")
ax.legend()
motion_plot.show()
def graph_energy(self):
energy_plot = plt.figure()
ax = energy_plot.add_subplot()
ax.plot(self.time_axis, self.energy_tracker[:, 0], label="Kinetic Energy")
ax.plot(self.time_axis, self.energy_tracker[:, 1], label="Potential Energy")
ax.plot(self.time_axis, self.energy_tracker[:, 2], label="Total Energy")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Energy (kJ)")
ax.set_title("Energy of a Hookean System")
ax.legend()
energy_plot.show()
def simple_fourier(self):
frequency_plot = plt.figure()
ax = frequency_plot.add_subplot()
ax.set_xlim([0, 1])
ax.set_title("Fourier Analysis of Displacement, Node 3")
ax.set_xlabel("Frequency [Hz]")
ax.set_ylabel("Amplitude Spectra")
for j, rigid in enumerate(self.tracked_objects):
fourier = np.fft.fft(self.motion_tracker[:, j] - np.mean(self.motion_tracker[:, j]))
fourier = np.fft.fftshift(fourier)
freq = np.fft.fftfreq(self.time_axis.shape[-1], d=self.t)
freq = np.fft.fftshift(freq)
ax.plot(freq, np.absolute(fourier))
frequency_plot.show()
def deviation_from_ideal(self, analytical):
ideal = analytical(self.time_axis)
error = [ideal[t] - self.motion_tracker[t, 0] for t, comp in enumerate(self.time_axis)]
error_plot = plt.figure()
ax = error_plot.add_subplot()
ax.plot(self.time_axis, error)
error_plot.show()
TrialMesh = Mesh([1, 6, 6])
TrialMesh.create_anchor([0, 0, 0], [0, 0, 0])
for x in range(0, 5):
for y in range(0, 5):
TrialMesh.create_pointmass([0, x, y], [0, x, y], 1)
for y in range(0, 5):
for x in range(0, 5):
if y == 4 and x == 4:
pass
elif y == 4 and x != 4:
TrialMesh.create_rest_spring([0, x, y], [0, x + 1, y], 3)
elif x == 4 and y != 4:
TrialMesh.create_rest_spring([0, x, y], [0, x, y + 1], 3)
else:
TrialMesh.create_rest_spring([0, x, y], [0, x, y + 1], 3)
TrialMesh.create_rest_spring([0, x, y], [0, x + 1, y], 3)
Instance1 = Instance(TrialMesh, 0.005, 200)
Instance1.initialize_displacement([[0, 2, 2]], [[0, 0.1, 0.1]])
#Instance1.initialize_displacement([[0, 1, 0], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 0, 0]], [[0, 0, 0.3], [0, 0, 0.3], [0, 0, 0.3], [0, 0, 0.3], [0, 0, 0.3]])
Instance1.initialize_tracking([[0, 1, 1], [0, 1, 1]], [2, 3])
Instance1.simulate()
Instance1.graph_motion()
#Instance1.graph_energy()
Instance1.simple_fourier()
#Instance1.deviation_from_ideal(lambda t: (-2/(20*np.sqrt(5)))*np.cos(t*np.sqrt((3+np.sqrt(5))/2)) + 2/(20*np.sqrt(5))*np.cos(t*np.sqrt((3-np.sqrt(5))/2))+1)