-
Notifications
You must be signed in to change notification settings - Fork 2
/
frame.py
460 lines (409 loc) · 19.9 KB
/
frame.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import random
import numpy as np
from node import *
import generateMatrices
from solver import *
from generateMatrices import *
from objectiveFunction import *
from plotter import *
from loadCases import *
from math import *
class Frame:
def __init__(self):
self.tubes = []
self.nodes = []
self.geometryOptNodes = []
self.torStiffness = None
self.weight = None
self.internalForces = None
self.displacements = None
self.reactions = None
# Should have a list of the required nodes, which should be the only nodes
# accessed by loadCases/objectiveFunction b/c it wont be possible to remove them
def computeTorStiffness(self):
frontUpperLeftAArmNode = 4
frontLowerLeftAArmNode = 6
nodeToMeasureStiffnessAt = 4
# Z height of center of rotation, located along car's centerline
zCenter = 10
# How close the two radii should be for while-loop end condition
finalTolerance = 0.001
y = self.nodes[nodeToMeasureStiffnessAt].y
z = self.nodes[nodeToMeasureStiffnessAt].z
radius = sqrt(pow(y, 2) + pow(z - zCenter, 2))
displacedY = y + self.displacements[1][nodeToMeasureStiffnessAt]
displacedZ = z + self.displacements[2][nodeToMeasureStiffnessAt]
displacedRadius = sqrt(pow(displacedY, 2) + pow(displacedZ - zCenter, 2))
if abs(y) > abs(displacedY) and abs(z) > abs(displacedZ):
while abs(displacedRadius - radius) > finalTolerance:
if displacedRadius > radius:
zCenter -= finalTolerance/2
else:
zCenter += finalTolerance/2
radius = sqrt(pow(y, 2) + pow(z - zCenter, 2))
displacedRadius = sqrt(pow(displacedY, 2) + pow(displacedZ - zCenter, 2))
else:
while abs(displacedRadius - radius) > finalTolerance:
if displacedRadius < radius:
zCenter -= finalTolerance/2
else:
zCenter += finalTolerance/2
radius = sqrt(pow(y, 2) + pow(z - zCenter, 2))
displacedRadius = sqrt(pow(displacedY, 2) + pow(displacedZ - zCenter, 2))
print("Center at z = ", zCenter, "inches")
distBetweenNodeBeforeAndAfterDisp = sqrt(pow(y - displacedY, 2) + pow(z - displacedZ, 2))
angle = 2*degrees(asin((distBetweenNodeBeforeAndAfterDisp/2)/radius))
# Assumes couple moment
torque = 2*LoadCases.twist.forceUpUpper[2] * self.nodes[frontUpperLeftAArmNode].y
torque += 2*LoadCases.twist.forceUpLower[2] * self.nodes[frontLowerLeftAArmNode].y
torStiffness = (torque * 0.112984)
torStiffness /= angle
self.torStiffness = abs(torStiffness)
def getDisplacements(self):
numTubes, numNodes, coord, con, fixtures, loads, dist, E, G, areas, I_y, I_z, J, St, be = generateMatrices(self, False)
_, displacements, _ = Solver(numTubes, numNodes, coord, con, fixtures, loads, dist, E, G, areas, I_y, I_z, J, St, be)
return displacements
def solveAllLoadCases(self, weightMultiplier):
scorePerWeight = 0
avgDisps = []
allTargetNodeDisps = []
for loadCase in LoadCases.listLoadCases:
self.setLoadCase(loadCase)
scorePerWeightToAdd, dispList, avgDisplacement = self.solve(weightMultiplier)
if loadCase is LoadCases.twist:
self.computeTorStiffness()
scorePerWeight += scorePerWeightToAdd
avgDisps.append(avgDisplacement)
for disp in dispList:
allTargetNodeDisps.append(disp)
avgDisp = sum(avgDisps)/len(avgDisps)
return scorePerWeight, allTargetNodeDisps, avgDisp
def solve(self, weightMultiplier):
numTubes, numNodes, coord, con, fixtures, loads, dist, E, G, areas, I_y, I_z, J, St, be = generateMatrices(self, False)
internalForces, displacements, reactions = Solver(numTubes, numNodes, coord, con, fixtures, loads, dist, E, G, areas, I_y, I_z, J, St, be)
self.internalForces = internalForces
self.displacements = displacements
self.reactions = reactions
scorePerWeight, dispList, maxDisp = ObjectiveFunction(self, self.loadCase, weightMultiplier)
return scorePerWeight, dispList, maxDisp
def setLoadCase(self, loadCase):
for node in self.nodes:
node.setFixtures(0, 0, 0, 0, 0, 0)
node.setForcesApplied(0, 0, 0, 0, 0, 0)
for i in range(loadCase.nodeForceCases.__len__()):
forceCase = loadCase.nodeForceCases.__getitem__(i)
forces = forceCase.__getitem__(forceCase.__len__() - 1)
x = forces.__getitem__(0)
y = forces.__getitem__(1)
z = forces.__getitem__(2)
xMom = forces.__getitem__(3)
yMom = forces.__getitem__(4)
zMom = forces.__getitem__(5)
for j in range(forceCase.__len__() - 1):
index = forceCase.__getitem__(j)
self.nodes.__getitem__(index).setForcesApplied(x, y, z, xMom, yMom, zMom)
for index in loadCase.fixedNodes:
self.nodes.__getitem__(index).setFixtures(1, 1, 1, 1, 1, 1)
self.loadCase = loadCase
def setFixtures(self, nodeIndex, x, y, z, xMom, yMom, zMom):
fixNode = self.nodes.__getitem__(nodeIndex)
fixNode.setFixtures(x, y, z, xMom, yMom, zMom)
def getSymmetricTube(self, tube):
if tube.nodeFrom.name.endswith("#m") and tube.nodeTo.name.endswith("#m"):
symNodeFrom = tube.nodeFrom.name.split("#m")[0]
symNodeTo = tube.nodeTo.name.split("#m")[0]
else:
symNodeFrom = tube.nodeFrom.name + "#m"
symNodeTo = tube.nodeTo.name + "#m"
for searchTube in self.tubes:
if searchTube.nodeFrom.name == symNodeFrom and searchTube.nodeTo.name == symNodeTo:
return searchTube
return None
def getSymmetricNode(self, node):
if node.name.endswith("#m"):
symName = node.name.split("#m")[0]
else:
symName = node.name + "#m"
for searchNode in self.nodes:
if searchNode.name == symName:
return searchNode
return None
def changeTubeThickness(self, index, size):
tube = self.tubes.__getitem__(index)
tube.changeThickness(size)
if tube.isSymmetric:
symTube = self.getSymmetricTube(tube)
symTube.changeThickness(size)
if tube.group is not None:
for testTube in self.tubes:
if testTube.group is tube.group:
testTube.changeThickness(size)
self.getWeight()
def randomizeThicknessOfRandomTube(self):
randTube = random.choice(self.tubes)
index = self.tubes.index(randTube)
if randTube.isRound and randTube.size is not RD_1xSLD:
sizeIndex = allRoundSizes.index(randTube.minSize)
availableSizes = allRoundSizes[sizeIndex:len(allRoundSizes)]
thickness = random.choice(availableSizes)
self.changeTubeThickness(index, thickness)
# will not allow changes to square tubes
def randomizeThickness(self, index):
tube = self.tubes.__getitem__(index)
if tube.isRound and tube.size is not RD_1xSLD:
sizeIndex = allRoundSizes.index(tube.minSize)
availableSizes = allRoundSizes[sizeIndex:len(allRoundSizes)]
thickness = random.choice(availableSizes)
self.changeTubeThickness(index, thickness)
# optimization: should only randomize one tube in each symmetric pair -- also maybe implement lists of the symmetric
# pair to prevent repeat look-ups
def randomizeAllThicknesses(self):
for i in range(len(self.tubes)):
self.randomizeThickness(i)
def changeNodeLocation(self, index, x, y, z):
node = self.nodes.__getitem__(index)
deltaX = x - node.x
node.changeLocation(x, y, z)
if node.isSymmetric:
symNode = self.getSymmetricNode(node)
symNode.changeLocation(x, -y, z)
if node.hasXGroup:
for xGroupNode in self.geometryOptNodes:
if xGroupNode.hasXGroup:
if xGroupNode.xGroup == node.xGroup:
xGroupNode.x += deltaX
for tube in self.tubes:
tube.length = tube.getLength(tube.nodeFrom, tube.nodeFrom)
tube.weight = tube.getWeight(tube.length)
self.getWeight()
def randomizeLocationOfRandomNode(self):
yOnCenterline = False
node = random.choice(self.geometryOptNodes)
index = self.nodes.index(node)
xPos = node.xOrig + node.maxXPosDev
xNeg = node.xOrig - node.maxXNegDev
if node.yOrig is 0:
yOnCenterline = True
else:
yPos = node.yOrig + node.maxYPosDev
yNeg = node.yOrig - node.maxYNegDev
zPos = node.zOrig + node.maxZPosDev
zNeg = node.zOrig - node.maxZNegDev
newX = random.uniform(xNeg, xPos)
if yOnCenterline:
newY = 0
else:
newY = random.uniform(yNeg, yPos)
newZ = random.uniform(zPos, zNeg)
self.changeNodeLocation(index, newX, newY, newZ)
def randomizeAllNodeLocations(self):
for node in self.geometryOptNodes:
yOnCenterline = False
index = self.nodes.index(node)
xPos = node.xOrig + node.maxXPosDev
xNeg = node.xOrig - node.maxXNegDev
if node.yOrig is 0:
yOnCenterline = True
else:
yPos = node.yOrig + node.maxYPosDev
yNeg = node.yOrig - node.maxYNegDev
zPos = node.zOrig + node.maxZPosDev
zNeg = node.zOrig - node.maxZNegDev
newX = random.uniform(xNeg, xPos)
if yOnCenterline:
newY = 0
else:
newY = random.uniform(yNeg, yPos)
newZ = random.uniform(zPos, zNeg)
self.changeNodeLocation(index, newX, newY, newZ)
def splitTubeRandomly(self):
# Should split a tube at a random point along its length and add a node there,
# then it should reconstruct that tube, but with two elements
# Should potentially allow for more variation in geometry optimization
return "TODO"
def removeTubeRandomly(self):
# Note, you need to check the randint() docs to make sure the range is right
numTubes = len(self.tubes)
randIndex = random.randint(0, numTubes-1)
self.removeTube(randIndex)
def removeNodeRandomly(self):
# Should remove a random node and its tubes
return "TODO"
def addANodeAndTubesRandomly(self):
return "TODO"
def addTubeRandomly(self):
nodeFrom = random.choice(self.nodes)
nodeTo = random.choice(self.nodes)
alreadyExists = False
for tube in self.tubes:
if tube.nodeFrom is nodeFrom or tube.nodeTo is nodeFrom:
if tube.nodeFrom is nodeTo or tube.nodeTo is nodeTo:
alreadyExists = True
while nodeFrom.isSymmetric is False or nodeTo.isSymmetric is False or nodeFrom is nodeTo or alreadyExists:
nodeFrom = random.choice(self.nodes)
nodeTo = random.choice(self.nodes)
alreadyExists = False
for tube in self.tubes:
if tube.nodeFrom is nodeFrom or tube.nodeTo is nodeFrom:
if tube.nodeFrom is nodeTo or tube.nodeTo is nodeTo:
alreadyExists = True
thickness = random.choice(allRoundSizes)
self.addTube(thickness, RD_5x35, nodeFrom.name, nodeTo.name, True, False)
def addNode(self, name, x, y, z, isSymmetric, isRequired, maxXPosDev=None, maxXNegDev=None, maxYPosDev=None, maxYNegDev=None, maxZPosDev=None, maxZNegDev=None,xGroup=None):
node = Node(self, name, x, y, z, isSymmetric, isRequired, maxXPosDev, maxXNegDev, maxYPosDev, maxYNegDev, maxZPosDev, maxZNegDev, xGroup)
self.nodes.append(node)
if isSymmetric:
symName = name + "#m"
symNode = Node(self, symName, x, -y, z, isSymmetric, isRequired, maxXPosDev, maxXNegDev, maxYNegDev, maxYPosDev, maxZPosDev, maxZNegDev, xGroup)
self.nodes.append(symNode)
if symNode.geometryOptPossible:
self.geometryOptNodes.append(symNode)
if node.geometryOptPossible:
self.geometryOptNodes.append(node)
def removeNode(self, index):
node = self.nodes.__getitem__(index)
if node.isSymmetric:
symNode = self.getSymmetricNode(node)
for tube in symNode.tubes:
self.tubes.remove(tube)
self.nodes.remove(symNode)
if symNode.geometryOptPossible:
self.geometryOptNodes.remove(symNode)
if node.geometryOptPossible:
self.geometryOptNodes.remove(node)
for tube in node.tubes:
self.tubes.remove(tube)
self.nodes.remove(node)
for node in self.nodes:
node.updateConnectingTubes()
def addTube(self, size, minSize, nodeFrom, nodeTo, isSymmetric, isRequired, group=None):
tube = Tube(self, size, minSize, nodeFrom, nodeTo, isSymmetric, isRequired, group)
self.tubes.append(tube)
tube.nodeFrom.tubes.append(tube)
tube.nodeTo.tubes.append(tube)
if isSymmetric:
symNodeFrom = nodeFrom + "#m"
symNodeTo = nodeTo + "#m"
symTube = Tube(self, size, minSize, symNodeFrom, symNodeTo, isSymmetric, isRequired, group)
self.tubes.append(symTube)
symTube.nodeFrom.tubes.append(symTube)
symTube.nodeTo.tubes.append(symTube)
self.getWeight()
def removeTube(self, index):
tube = self.tubes.__getitem__(index)
if tube.isRequired:
if tube.isSymmetric:
symTube = self.getSymmetricTube(tube)
self.tubes.remove(symTube)
self.tubes.remove(tube)
for node in self.nodes:
node.updateConnectingTubes()
self.getWeight()
def getWeight(self):
weight = 0
for tube in self.tubes:
weight += tube.weight
self.weight = weight
return weight
def toString(self, printType=None, long=None):
if printType == "all":
self._printTubes(long)
self._printNodes(long)
if printType == "nodes":
self._printNodes(long)
if printType == "tubes":
self._printTubes(long)
print("Total Weight:", '%.3f' % self.weight, "lbs")
def _printTubes(self, long):
print("\nTUBES:", self.tubes.__len__(), "total")
print("----------\n")
index = 0
if long is 'long':
for tube in self.tubes:
print("\n#", index, "\n", tube.toString(), "going from", tube.nodeFrom.name, "to",
tube.nodeTo.name)
print(" From:", tube.nodeFrom.coordsToString(), "to", tube.nodeTo.coordsToString())
print(" Weight:", '%.3f' % tube.weight, "lbs\t\tLength:", '%.3f' % tube.length, "inches")
if tube.isRequired and tube.isSymmetric:
print(" Required and Symmetric\n")
elif tube.isRequired:
print(" Required\n")
elif tube.isSymmetric:
print(" Symmetric\n")
index += 1
else:
for tube in self.tubes:
print("#", index, "-", tube.toString(), "going from", tube.nodeFrom.name, "to",
tube.nodeTo.name)
index += 1
def _printNodes(self, long):
print("\nNODES:", self.nodes.__len__(), "total")
print("----------\n")
index = 0
if long is 'long':
for node in self.nodes:
print("#", index, "Name:", node.name, "\tCoordinates:", node.coordsToString())
print("\tHas forces:\t\t", node.forcesToString())
print("\tWith fixtures:\t", node.fixturesToString())
if node.isRequired and node.isSymmetric:
print("\tRequired and Symmetric")
elif node.isRequired:
print("\tRequired")
elif node.isSymmetric:
print("\tSymmetric")
print("\tConnects tubes:")
for tube in node.tubes:
print("\t ", "Tube No.", self.tubes.index(tube), "-->", tube.toString())
print("\n")
index += 1
else:
for node in self.nodes:
print("#", index, "Name:", node.name, "with coordinates:", node.coordsToString())
index += 1
def plot(self, displacedScaling, figPath=None):
plotFrame(self, displacedScaling, figPath)
def plotAni(self, axes, title):
plotFrameAni(self, axes, title)
def toTextFile(self, path):
createFramePath = "%s/createMaxFrame.txt" % path
createFrameFile = open(createFramePath, "w")
for node in self.nodes:
if not node.name.endswith("#m"):
if node.geometryOptPossible:
if node.hasXGroup:
createFrameFile.write("\tframe.addNode('%s',%f, %f, %f, %r, %r, %f, %f, %f, %f, %f, %f, '%s')\n" %
(node.name, node.x, node.y, node.z, node.isSymmetric, node.isRequired,
node.maxXPosDev, node.maxXNegDev, node.maxYPosDev, node.maxYNegDev,
node.maxZPosDev, node.maxZNegDev, node.xGroup))
else:
createFrameFile.write("\tframe.addNode('%s',%f, %f, %f, %r, %r, %f, %f, %f, %f, %f, %f)\n" %
(node.name, node.x, node.y, node.z, node.isSymmetric, node.isRequired,
node.maxXPosDev, node.maxXNegDev, node.maxYPosDev, node.maxYNegDev,
node.maxZPosDev, node.maxZNegDev))
else:
createFrameFile.write("\tframe.addNode('%s',%f, %f, %f, %r, %r)\n" %
(node.name, node.x, node.y, node.z, node.isSymmetric, node.isRequired))
alreadyFoundSymmetricPair = False
for tube in self.tubes:
if tube.isSymmetric and alreadyFoundSymmetricPair is False:
if tube.group is not None:
createFrameFile.write("\tframe.addTube(%s, %s, '%s', '%s', %r, %r, '%s')\n" %
(tube.size.string, tube.minSize.string, tube.nodeFrom.name, tube.nodeTo.name,
tube.isSymmetric, tube.isRequired, tube.group))
else:
createFrameFile.write("\tframe.addTube(%s, %s, '%s', '%s', %r, %r)\n" %
(tube.size.string, tube.minSize.string, tube.nodeFrom.name, tube.nodeTo.name,
tube.isSymmetric, tube.isRequired))
alreadyFoundSymmetricPair = True
elif not tube.isSymmetric:
if tube.group is not None:
createFrameFile.write("\tframe.addTube(%s, %s, '%s', '%s', %r, %r, '%s')\n" %
(tube.size.string, tube.minSize.string, tube.nodeFrom.name, tube.nodeTo.name,
tube.isSymmetric, tube.isRequired, tube.group))
else:
createFrameFile.write("\tframe.addTube(%s, %s, '%s', '%s', %r, %r)\n" %
(tube.size.string, tube.minSize.string, tube.nodeFrom.name, tube.nodeTo.name,
tube.isSymmetric, tube.isRequired))
else:
alreadyFoundSymmetricPair = False
createFrameFile.close()