-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElementsForce.py
executable file
·226 lines (188 loc) · 11.7 KB
/
ElementsForce.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
223
224
225
226
from BreakBall import BreakBall
from EventBus import EventBus, destroyBall
from BallForce import *
from Elements import *
def isCrossForce(ball1: BallForce, ball2: BallForce):
# проверяем столкнулись ли шары и если да -- двигаются ли они навстречу друг другу
if distanceNow(ball1, ball2) < (ball1.radius + ball2.radius):
return True
return False
def findLocalVelocities(ball, gama):
# Угол между линией удара и горизонталью
# Углы направления шаров в локальной системе координат
alphaRadianLocal = ball.alphaRadian - gama
# Скорости шаров в локальной системе координат
velocityXLocal = ball.velocityAbsolute * cos(alphaRadianLocal)
velocityYLocal = ball.velocityAbsolute * sin(alphaRadianLocal)
return velocityXLocal, velocityYLocal
def findRelativeVelocityY(velocityYLocal, ball_1, ball_2):
return velocityYLocal - ((ball_1.velocityTheta * ball_1.radius) + (ball_2.velocityTheta * ball_2.radius))
def dampingLocalVelocity(velocity, velocityRelative, c):
dampening = velocityRelative * c
velocity = dampeningVelocity(dampening, velocity)
return velocity
def methodForce(ball_1, ball_2):
# Решение задачи о нецентральном упругом ударе двух дисков, путём приведения к задаче о
# столкновении шаров по оси Х(линия столкновения становится горизонтальной, происходит
# переход в локальную систему координат)
# Также учет диссипации при каждом столкновении шаров
gama = atan2((ball_1.y - ball_2.y), (ball_1.x - ball_2.x))
E_eff = ((1 - (ball_1.nu ** 2)) / ball_1.Emod + (1 - (ball_2.nu ** 2)) / ball_2.Emod) ** (-1)
G_eff = ((2 - ball_1.nu) / ball_1.Gmod + (2 - ball_2.nu) / ball_2.Gmod) ** (-1)
velocity1XLocal, velocity1YLocal = findLocalVelocities(ball_1, gama)
velocity2XLocal, velocity2YLocal = findLocalVelocities(ball_2, gama)
velocity1XRelative = velocity1XLocal - velocity2XLocal
velocity2XRelative = velocity2XLocal - velocity1XLocal
# Непосредственно решение задачи о нецентральном упругом ударе двух дисков
entryNormal = (ball_1.radius + ball_2.radius - sqrt((ball_1.x - ball_2.x) ** 2 + (ball_1.y - ball_2.y) ** 2))
radiusEffective = ((1 / ball_1.radius) + (1 / ball_2.radius)) ** (-1)
stiffness = getStiffness(radiusEffective, entryNormal, E_eff)
forceNormal1 = stiffness * entryNormal
forceNormal2 = -1 * stiffness * entryNormal
# forcePlot.append(forceNormal1)
# velocityPlot.append(velocity1XLocal * 10000)
# if len(stepCountForce) == 0:
# stepCountForce.append(0)
# else:
# stepCountForce.append(stepCountForce[len(stepCountForce) - 1] + 1)
accelerationNormal1 = forceNormal1 / ball_1.mass
accelerationNormal2 = forceNormal2 / ball_2.mass
velocity1YRelative = zeroToZeroBig(findRelativeVelocityY(velocity1YLocal - velocity2YLocal, ball_1, ball_2))
velocity2YRelative = -1 * velocity1YRelative
signVelocityRelativeTangent1 = customSign(velocity1YRelative)
signVelocityRelativeTangent2 = customSign(velocity2YRelative)
velocityThetaRelative = ball_1.velocityTheta + ball_2.velocityTheta
signVelocityRelativeAngular = customSign(velocityThetaRelative)
# print(numberOfI)
accelerationAngular1, accelerationTangent1 = ball_1.findAccelerationAngular(signVelocityRelativeTangent1,
abs(forceNormal1), 1, radiusEffective,
signVelocityRelativeAngular)
# print(numberOfJ)
accelerationAngular2, accelerationTangent2 = ball_2.findAccelerationAngular(signVelocityRelativeTangent2,
abs(forceNormal2), -1, radiusEffective,
signVelocityRelativeAngular)
jerkNormal1, jerkTangent1, jerkAngular1 = ball_1.getJerk(entryNormal, velocity1XLocal,
accelerationNormal1,
signVelocityRelativeTangent1, 1, radiusEffective,
signVelocityRelativeAngular,
accelerationAngular1,
accelerationTangent1, E_eff)
# jerkPlot.append(jerkNormal1 * 1e-3)
jerkNormal2, jerkTangent2, jerkAngular2 = ball_2.getJerk(entryNormal, velocity2XLocal,
accelerationNormal2,
signVelocityRelativeTangent2, -1, radiusEffective,
signVelocityRelativeAngular,
accelerationAngular2,
accelerationTangent2, E_eff)
# ----------------------------- Damping part -----------------------------
accelerationDampeningNormal1 = velocity1XRelative * getDampingNormal(radiusEffective, entryNormal, ball_1.mass,
ball_1.cn, E_eff) / ball_1.mass * (-1)
accelerationDampeningTangent1 = velocity1YRelative * getDampingTangent(radiusEffective, entryNormal, ball_1.mass,
ball_1.cs, G_eff) / ball_1.mass
accelerationNormal1 += accelerationDampeningNormal1
accelerationTangent1 += accelerationDampeningTangent1
accelerationDampeningNormal2 = velocity2XRelative * getDampingNormal(radiusEffective, entryNormal,
ball_2.mass, ball_2.cn,
E_eff) / ball_2.mass * (-1)
accelerationDampeningTangent2 = velocity2YRelative * getDampingTangent(radiusEffective, entryNormal,
ball_2.mass, ball_2.cs, G_eff) / ball_2.mass
accelerationNormal2 += accelerationDampeningNormal2
accelerationTangent2 += accelerationDampeningTangent2
# ----------------------------- End damping part -----------------------------
interactionCountFlag = ball_1.interactionCountFlag and ball_2.interactionCountFlag
ball_1.saveAccelerationLength(gama, accelerationNormal1, accelerationTangent1, jerkNormal1, jerkTangent1,
jerkAngular1, entryNormal, accelerationAngular1, isBall=True, number=ball_2,
stiffness=stiffness, interactionCountFlag=interactionCountFlag)
ball_2.saveAccelerationLength(gama, accelerationNormal2, accelerationTangent2, jerkNormal2, jerkTangent2,
jerkAngular2, entryNormal, accelerationAngular2, isBall=True, number=ball_1,
stiffness=stiffness, interactionCountFlag=interactionCountFlag)
def isCrossBefore(i, numberOfJ): # Возможно стоит удалить две неиспользуемых переменных
for interaction in i.interactionArray:
if interaction.isBall and interaction.number is numberOfJ:
return True
return False
def deleteInteraction(i, numberOfJ, recursion = True):
for interaction in i.interactionArray:
if interaction.isBall and interaction.number is numberOfJ:
# if len(ballInteraction) > 0:
# if ballInteraction[len(ballInteraction) - 1] != interaction.n:
# ballInteraction.append(interaction.n)
# else:
# ballInteraction.append(interaction.n)
if isinstance(i, BreakBall) and recursion:
i.reactForEndInteraction(interaction.maxEnergy)
if interaction in i.interactionArray:
i.interactionArray.remove(interaction)
break
class ElementsForce(Elements):
def __init__(self, balls, canvas, eventBus: EventBus):
Elements.__init__(self, balls, canvas)
self.newBalls = []
self.removingBalls = []
self.eventBus = eventBus
self.eventBus.on(destroyBall, self.destruct)
self.recalculateGrid = True
def calculation(self):
# Есть необходимость отключения не глобальных ускорений,
# а ускорений взаимодействия, что проверяется отдельно
if len(self.newBalls) > 0 or self.recalculateGrid:
for ball in self.removingBalls:
index = -1
for i, ball_ in enumerate(self.balls):
if ball_ == ball:
index = i
break
if index == -1:
print('elemForce -> destruct -> i try to remove ball that does not exist')
exit(1)
for pair in self.pairs:
i = pair.i.number
j = pair.j.number
if i != index and j != index:
continue
elif j == index:
i, j = j, i
deleteInteraction(self.balls[j], self.balls[index], False)
for ball in self.removingBalls:
if ball in self.balls:
self.balls.remove(ball)
self.balls.extend(self.newBalls)
self.newBalls.clear()
self.removingBalls.clear()
self.pairs = self.hashTable.getPairs(self.balls)
self.recalculateGrid = False
for pair in self.pairs:
i = pair.i.number
j = pair.j.number
ball_i = self.balls[i]
ball_j = self.balls[j]
if isCrossForce(ball_i, ball_j):
methodForce(ball_i, ball_j)
elif isCrossBefore(ball_i, ball_j):
deleteInteraction(ball_i, ball_j)
deleteInteraction(ball_j, ball_i)
def destruct(self, data):
ball = data['destroyingBall']
for ball_ in self.removingBalls:
if ball_ is ball:
return
newBalls = data['newBalls']
self.removingBalls.append(ball)
self.newBalls.extend(newBalls)
self.recalculateGrid = True
if len(newBalls) == 0:
self.deadBalls.append(ball.radius)
def makeNewBall(self):
print('It`s new ball')
lastBall = self.balls[-1]
radius = lastBall.radiusBegin if isinstance(lastBall, BreakBall) else lastBall.radius
wall = MoveWall.getInstance()
self.newBalls.append(BreakBall(x=3/2 * wall.centerX,# - radius * 1.5,#
y=1/2 * wall.centerX,#
radius=radius, radiusBegin=radius,
alpha=0, velocity=0, velocityTheta=0,
cn=lastBall.cn, cs=lastBall.cs, nu=lastBall.nu,
density=lastBall.density, Emod=lastBall.Emod,
color=lastBall.color, canvas=self.canvas,
eventBus=self.eventBus))
self.saveMass += 4 / 3 * pi * (radius ** 3)