-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.py
170 lines (159 loc) · 7.48 KB
/
nodes.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
import numpy as np
class Grid:
def __init__(self, grid_size, cell_size):
self.grid_size = grid_size
self.cell_size = cell_size
self.node_count = (int(grid_size[0] / cell_size), int(grid_size[1] / cell_size))
self.total_nodes = self.node_count[0] * self.node_count[1]
# Initialize body_id with the correct size
self.body_id = np.full((2 * self.total_nodes,), -1, dtype=int)
self.physical_size = (grid_size[0], grid_size[1])
self.position = np.zeros((self.node_count[0], self.node_count[1], 2))
self.mass = np.zeros((self.node_count[0], self.node_count[1]))
self.mass_body1 = np.zeros((self.node_count[0], self.node_count[1]))
self.mass_body2 = np.zeros((self.node_count[0], self.node_count[1]))
self.velocity = np.zeros((self.node_count[0], self.node_count[1], 2))
self.velocity_body1 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.velocity_body2 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.momentum = np.zeros((self.node_count[0], self.node_count[1], 2))
self.momentum_body1 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.momentum_body2 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.force = np.zeros((self.node_count[0], self.node_count[1], 2))
self.force_body1 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.force_body2 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.acceleration_body1 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.acceleration_body2 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.acceleration = np.zeros((self.node_count[0], self.node_count[1], 2))
self.normals_body1 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.normals_body2 = np.zeros((self.node_count[0], self.node_count[1], 2))
self.initialize_grid()
def initialize_grid(self):
for i in range(self.node_count[0]):
for j in range(self.node_count[1]):
x = i * self.cell_size
y = j * self.cell_size
self.position[i, j] = [x, y]
def reset_nodes(self):
self.mass.fill(0)
self.mass_body1.fill(0)
self.mass_body2.fill(0)
self.velocity.fill(0)
self.velocity_body1.fill(0)
self.velocity_body2.fill(0)
self.body_id.fill(-1) # Reset to -1 instead of 0
self.momentum.fill(0)
self.momentum_body1.fill(0)
self.momentum_body2.fill(0)
self.force.fill(0)
self.force_body1.fill(0)
self.force_body2.fill(0)
self.acceleration.fill(0)
self.acceleration_body1.fill(0)
self.acceleration_body2.fill(0)
self.normals_body1.fill(0)
self.normals_body2.fill(0)
def get_node(self, i, j):
cell_ID = i + j * self.node_count[0]
return {
'position': self.position[i, j],
'mass': self.mass[i, j],
'mass_body1': self.mass_body1[i, j],
'mass_body2': self.mass_body2[i, j],
'velocity': self.velocity[i, j],
'velocity_body1': self.velocity_body1[i, j],
'velocity_body2': self.velocity_body2[i, j],
'momentum': self.momentum[i, j],
'momentum_body1': self.momentum_body1[i, j],
'momentum_body2': self.momentum_body2[i, j],
'force': self.force[i, j],
'acceleration': self.acceleration[i, j],
'acceleration_body1': self.acceleration_body1[i, j],
'acceleration_body2': self.acceleration_body2[i, j],
'normals_body1': self.normals_body1[i, j],
'normals_body2': self.normals_body2[i, j],
'body_id': self.body_id[2*cell_ID:2*cell_ID+2] # Access the correct slice of the 1D array
}
def set_node(self, i, j, node):
cell_ID = i + j * self.node_count[0]
self.body_id[2 * cell_ID] = node['body_id'][0]
self.body_id[2 * cell_ID + 1] = node['body_id'][1]
self.mass[i, j] = node['mass']
self.mass_body1[i, j] = node['mass_body1']
self.mass_body2[i, j] = node['mass_body2']
self.velocity[i, j] = node['velocity']
self.velocity_body1[i, j] = node['velocity_body1']
self.velocity_body2[i, j] = node['velocity_body2']
self.momentum[i, j] = node['momentum']
self.momentum_body1[i, j] = node['momentum_body1']
self.momentum_body2[i, j] = node['momentum_body2']
self.force[i, j] = node['force']
self.force_body1[i, j] = node['force_body1']
self.force_body2[i, j] = node['force_body2']
self.acceleration[i, j] = node['acceleration']
self.acceleration_body1[i, j] = node['acceleration_body1']
self.acceleration_body2[i, j] = node['acceleration_body2']
self.normals_body1[i, j] = node['normals_body1']
self.normals_body2[i, j] = node['normals_body2']
def get_nearby_nodes(self, particle_position):
i = int(particle_position[0] / self.cell_size)
j = int(particle_position[1] / self.cell_size)
nearby_nodes = []
for di in range(-2, 3):
for dj in range(-2, 3):
if 0 <= i + di < self.node_count[0] and 0 <= j + dj < self.node_count[1]:
nearby_nodes.append(self.get_node(i + di, j + dj))
return nearby_nodes
def get_surrounding_nodes(self, particle_position, psep):
index_x = int((particle_position[0] - (psep / 2) - self.position[0, 0, 0]) / self.cell_size)
index_y = int((particle_position[1] - (psep / 2) - self.position[0, 0, 1]) / self.cell_size)
surrounding_nodes = []
for j in range(3):
for i in range(3):
ni = index_x + i
nj = index_y + j
if 0 <= ni < self.node_count[0] and 0 <= nj < self.node_count[1]:
surrounding_nodes.append(self.get_node(ni, nj))
else:
surrounding_nodes.append(self.get_node(0, 0)) # Default to (0, 0) if out of bounds
return surrounding_nodes
class Node:
def __init__(self, position):
self.position = position
self.mass = 0.0
self.mass_body1 = 0.0
self.mass_body2 = 0.0
self.velocity = np.zeros(2)
self.velocity_body1 = np.zeros(2)
self.velocity_body2 = np.zeros(2)
self.momentum = np.zeros(2)
self.momentum_body1 = np.zeros(2)
self.momentum_body2 = np.zeros(2)
self.force = np.zeros(2)
self.force_body1 = np.zeros(2)
self.force_body2 = np.zeros(2)
self.acceleration = np.zeros(2)
self.acceleration_body1 = np.zeros(2)
self.acceleration_body2 = np.zeros(2)
self.normals_body1 = np.zeros(2)
self.normals_body2 = np.zeros(2)
self.body_id = np.full((2,), -1, dtype=int)
def reset(self):
self.mass = 0.0
self.mass_body1 = 0.0
self.mass_body2 = 0.0
self.velocity.fill(0)
self.velocity_body1.fill(0)
self.velocity_body2.fill(0)
self.momentum.fill(0)
self.momentum_body1.fill(0)
self.momentum_body2.fill(0)
self.force.fill(0)
self.force_body1.fill(0)
self.force_body2.fill(0)
self.acceleration.fill(0)
self.acceleration_body1.fill(0)
self.acceleration_body2.fill(0)
self.normals_body1.fill(0)
self.normals_body2.fill(0)
self.body_id.fill(-1)
# Reset other properties as needed