-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate_truck.py
379 lines (295 loc) · 10.5 KB
/
simulate_truck.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 3 06:45:00 2022
@author: leonardo
"""
from nachbagauer3Dc import node, railANCF3Dquadratic, beamANCF3Dquadratic
from materialsc import linearElasticMaterial
from bodiesc import flexibleBody3D, rigidBody
from profiles import planarProfile
import MultibodySystem as MBS
import numpy as np
from assimulo.solvers import IDA, ODASSL
import matplotlib.pyplot as plt
import helper_funcs as hf
'''
Initialize system
'''
mbs = MBS.MultibodySystem('Trilho com rodeiro')
mbs.gravity = np.array([0,-9.81,0],dtype=np.float64)
'''
Material
'''
steel = linearElasticMaterial('Steel',E = 207e9,
nu = 0.3,
rho = 7.85e3)
'''
Mesh
'''
nq = []
nq2 = []
nel = 8
totalLength = 2 * nel * 0.58
trackWidth = 1.0
for i in range(nel+1):
nq.append(node([totalLength * i/nel,0.0,-0.5*trackWidth,
0.0,0.99968765,0.02499219, #0.0,1.0,0.0,
0.0,-0.02499219,0.9968765]))
nq2.append(node([totalLength * i/nel,0.0,0.5*trackWidth,
0.0,0.99968765,-0.02499219,
0.0,0.02499219,0.9968765]))
eq = []
eq2 = []
for j in range(nel):
eq.append(
railANCF3Dquadratic(nq[j],nq[j+1],
0.18575,
6*0.0254,
0.0805,
0.022147,
0.032165,
135.605e-3,
23.815e-3,
78.339e-3,
8652.0e-6)
)
eq2.append(
railANCF3Dquadratic(nq2[j],nq2[j+1],
0.18575,
6*0.0254,
0.0805,
0.022147,
0.032165,
135.605e-3,
23.815e-3,
78.339e-3,
8652.0e-6)
)
'''
Bodies
'''
rail = flexibleBody3D('Rail L',steel)
rail.addElement(eq)
rail.nonLinear = 'L'
rail.assembleTangentStiffnessMatrix()
rail2 = flexibleBody3D('Rail R',steel)
rail2.addElement(eq2)
rail2.nonLinear = 'L'
rail2.assembleTangentStiffnessMatrix()
class wheelSet(rigidBody):
def __init__(self,name):
super().__init__(name)
wsmass = 132.
wsInertiaRadial = 1/12*wsmass*(3*0.15**2+trackWidth**2)
I = np.diag([1/12*wsmass*trackWidth**2,1/12*wsmass*trackWidth**2,1/2*wsmass*0.15*0.15])
self.setMass(wsmass)
self.setInertiaTensor(I)
self.setPositionInitialConditions(1,0.092902 + 0.41)
wheelset1 = wheelSet('Wheelset 1')
wheelset1.setPositionInitialConditions(0,0.75+1.6)
wheelset2 = wheelSet('Wheelset 2')
wheelset2.setPositionInitialConditions(0,0.75)
'''
Sleepers
'''
sleeper1 = MBS.force('Sleepers')
sleeper1.connect(rail,rail2)
def slpForce(t,p,v,m1,m2):
leftRail = m1.parent
rightRail = m2.parent
## Vertical stiffness ##
# states
leftDist = p[leftRail.globalDof[1::9]]
leftVelo = v[leftRail.globalDof[1::9]]
rightDist = p[rightRail.globalDof[1::9]]
rightVelo = v[rightRail.globalDof[1::9]]
f = np.zeros_like(p)
f[leftRail.globalDof[1::9]] = 3e6 * leftDist + 3e4 * leftVelo
f[rightRail.globalDof[1::9]] = 3e6 * rightDist + 3e4 * rightVelo
## Lateral stiffness
stiffness = 35e9 * 0.17 * 0.24 / trackWidth
# states
leftDist = p[leftRail.globalDof[2::9]]
leftVelo = v[leftRail.globalDof[2::9]]
rightDist = p[rightRail.globalDof[2::9]]
rightVelo = v[rightRail.globalDof[2::9]]
ds = leftDist - rightDist
dv = leftVelo - rightVelo
f[leftRail.globalDof[2::9]] = stiffness * (ds + 0.02*dv) + 1e6 * leftDist
f[rightRail.globalDof[2::9]] = stiffness * (- ds - 0.02*dv) + 1e6 * rightDist
## TODO: Rotation stiffness
leftY = p[leftRail.globalDof[4::9]]
leftZ = p[leftRail.globalDof[5::9]]
rightY = p[rightRail.globalDof[4::9]]
rightZ = p[rightRail.globalDof[5::9]]
return -f
sleeper1.setForceFunction(slpForce)
#sleeper2.setForceFunction(slpForce)
# Force to move the wheel
forceWheel = MBS.force('Wheel pull force')
forceWheel.connect(wheelset1,MBS.ground())
def pullWheelset(t,p,v,m1,m2):
w = m1.parent
f = np.zeros_like(p)
wpos = p[w.globalDof]
if t > 0.1:
f[w.globalDof[0]] = 100
f[w.globalDof[1:3]] = - wpos[1:3] * 1e3
return f
forceWheel.setForceFunction(pullWheelset)
# truck connection
truckConn = MBS.force('Truck')
truckConn.connect(wheelset1,wheelset2)
def truckForce(t,p,v,m1,m2):
f = np.zeros_like(p)
w1 = m1.parent
w2 = m2.parent
wpos1 = p[w1.globalDof]
wpos1[0] -= 1.6 # remove wheelbase
wpos2 = p[w2.globalDof]
delta = wpos1-wpos2
tforce = - 1e7 * delta
f[w1.globalDof] = tforce
f[w2.globalDof] = -tforce
return f
truckConn.setForceFunction(truckForce)
'''
Profiles
'''
rProf = planarProfile('rail', convPar=-1)
rProf.setProfilePointsFromFile('./tr68.pro')
rProf.centerProfile()
rail.addProfile(rProf)
'''
Contact
pt2 are the positions wheel profile reference frames
'''
contactL1 = MBS.force('Contact left wheel to rail')
contactL1.connect(rail,wheelset1,pt2=np.array([0.0,-0.41,-0.5*trackWidth]))
contactR1 = MBS.force('Contact right wheel to rail')
contactR1.connect(rail2,wheelset1,pt2=np.array([0.0,-0.41,0.5*trackWidth]))
contactL2 = MBS.force('Contact left wheel to rail')
contactL2.connect(rail,wheelset2,pt2=np.array([0.0,-0.41,-0.5*trackWidth]))
contactR2 = MBS.force('Contact right wheel to rail')
contactR2.connect(rail2,wheelset2,pt2=np.array([0.0,-0.41,0.5*trackWidth]))
def cForce(t,p,v,m1,m2):
railBody = m1.parent
wheelBody = m2.parent
cardans = p[wheelBody.globalDof[3:]]
cardans[2] = 0
Rwheel = hf.cardanRotationMatrix(cardans)
rhoM2 = Rwheel.dot(m2.position)
pWheel = p[wheelBody.globalDof[:3]] + rhoM2
railDof = np.array(railBody.globalDof)
isit = railBody.findElement(pWheel)
f = np.zeros_like(p)
if isit >= 0:
contactElement = railBody.elementList[isit]
localXi = contactElement.mapToLocalCoords(pWheel)
pRail = contactElement.interpolatePosition(localXi[0],1,localXi[2])
cNormal = contactElement.shapeFunctionDerivative(localXi[0],1,localXi[2])[3:6,:].dot(contactElement.qtotal)
gap = (pWheel-pRail).dot(hf.unitaryVector(cNormal)[0])
if gap < 0:
contactForce = cNormal * gap * 300e6
# print(t)
# print(railBody.name,gap)
f[railDof[contactElement.globalDof]] += np.dot(contactForce, contactElement.shapeFunctionMatrix(localXi[0],1,localXi[2]))
f[wheelBody.globalDof[:3]] -= contactForce
f[wheelBody.globalDof[3:]] -= hf.skew(rhoM2).dot(contactForce)
return f
contactL1.setForceFunction(cForce)
contactR1.setForceFunction(cForce)
contactL2.setForceFunction(cForce)
contactR2.setForceFunction(cForce)
'''
Multibody system
'''
mbs.addBody([rail,rail2,wheelset1,wheelset2])
mbs.addForce(sleeper1)
#mbs.addForce(sleeper2)
mbs.addForce(contactL1)
mbs.addForce(contactR1)
mbs.addForce(contactL2)
mbs.addForce(contactR2)
mbs.addForce(forceWheel)
mbs.addForce(truckConn)
mbs.setupSystem()
#%%
'''
Solution
'''
problem = mbs.generate_problem('ind3')
DAE = IDA(problem)
DAE.report_continuously = True
DAE.inith = 1e-6
DAE.num_threads = 8
DAE.suppress_alg = True
outFreq = 10e2 # Hz
finalTime = 0.10
#DAE.make_consistent('IDA_YA_YDP_INIT')
t,p,v=DAE.simulate(finalTime, finalTime * outFreq)
q = p[:,:mbs.n_p] + mbs.pos0
v = p[:,mbs.n_p:2*mbs.n_p] + mbs.vel0
lam = p[:,2*mbs.n_p:] + mbs.lam0
'''
Post-processing
'''
#%%
mbs.postProcess(t,p,v)
from helper_funcs import unitaryVector as uv
plt.figure()
nplots = 3
k = 0
for i in np.arange(0, p.shape[0],int(p.shape[0]/nplots)):
rail.updateDisplacements(rail.simQ[i])
rail2.updateDisplacements(rail2.simQ[i])
a = rail.plotPositions(5)
b = rail2.plotPositions(5)
k += 1
plt.plot(a[:,0],a[:,1], label='{:.2f} s'.format(t[i]), color='red', alpha = ( k/(nplots+1)))
plt.plot(b[:,0],b[:,1], label='{:.2f} s'.format(t[i]), color='blue', alpha = ( k/(nplots+1)))
plt.legend()
plt.xlabel('Comprimento ao longo do trilho / m')
plt.ylabel('Deslocamento vertical / m')
plt.title(mbs.name)
''' VPYTHON visuals '''
import vpython as vp
import convert_stl as stl
def run_animation():
scene = vp.canvas(width=1600,height=700,background=vp.color.gray(0.7),fov=0.001,
forward = vp.vec(1,0,0))
wheels = [wheelset1,wheelset2]
wheelReps = [stl.stl_to_triangles('Rodeiro montado.stl'),
stl.stl_to_triangles('Rodeiro montado.stl')]
for i in range(2):
wheelReps[i].pos = vp.vec(*wheels[i].simQ[0,:3])
wheelReps[i].rotate(angle=np.pi/2,axis=vp.vec(1,0,0))
wheelReps[i].visible = True
wheelReps[i].color = vp.color.cyan
rail.updateDisplacements(rail.simQ[0])
rail2.updateDisplacements(rail2.simQ[0])
path = []
for p in rail2.plotPositions():
path.append(vp.vec(*p))
c2 = vp.curve(path, color=vp.color.blue, radius = 0.01)
c1 = vp.curve(path, color=vp.color.green, radius = 0.01)
crails = [c1,c2]
# axisX = vp.arrow(pos=vp.vec(0,0,0),axis=vp.vec(0.5,0,0), shaftwidth=0.01, color=vp.color.red)
# axisY = vp.arrow(pos=vp.vec(0,0,0),axis=vp.vec(0.0,0.5,0), shaftwidth=0.01, color=vp.color.green)
# axisz = vp.arrow(pos=vp.vec(0,0,0),axis=vp.vec(0.0,0,0.5), shaftwidth=0.01, color=vp.color.blue)
vp.rate(500)
for i in range(len(t)):
scene.title = 't = {} s'.format(t[i])
for n,r in enumerate([rail,rail2]):
r.updateDisplacements(r.simQ[i])
for j,p in enumerate(r.plotPositions(eta=1)):
crails[n].modify(j,vp.vec(*p))
for i in range(2):
wheelReps[i].pos.x = wheels[i].simQ[i,0]
wheelReps[i].pos.y = wheels[i].simQ[i,1]
wheelReps[i].pos.z = wheels[i].simQ[i,2]
wheelReps[i].rotate(angle=wheels[i].simU[i,3]/outFreq, axis=vp.vec(1,0,0))
wheelReps[i].rotate(angle=wheels[i].simU[i,4]/outFreq, axis=vp.vec(0,1,0))
wheelReps[i].rotate(angle=wheels[i].simU[i,5]/outFreq, axis=vp.vec(0,0,1))
run_animation()