-
Notifications
You must be signed in to change notification settings - Fork 0
/
teste_mbs_trilho.py
360 lines (289 loc) · 9.34 KB
/
teste_mbs_trilho.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 23 07:01:46 2022
@author: leonardo
"""
import convert_stl as stl
import vpython as vp
from bodiesc import wheelset
from profiles import planarProfile
import MBS.MultibodySystem as mbs
import MBS.BodyConnections.Forces
import MBS.BodyConnections.BodyConnection
import MBS.BodyConnections.Contacts.WheelRailContact
import MBS.Bodies.flexibleTrack
import numpy as np
from assimulo.solvers import IDA
import matplotlib.pyplot as plt
# %% SYSTEM SETUP
'''
Initialize system
'''
system = mbs.RailwaySystem('Trilho com rodeiro')
system.gravity = np.array([0., 0., 9.81], dtype=np.float64)
initialVelocity = 4.0 # m/s
setVelocity = 4.0 # m/s
'''
Track
'''
trackWidth = 1.0
track = MBS.Bodies.flexibleTrack.flexibleTrack('Via',
system=system,
gauge=trackWidth,
sleeperDistance=0.58,
nel=5)
track.activeSleepers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
rail = track.leftRail
rail2 = track.rightRail
'''
Wheel profiles
'''
wLprofile = planarProfile('Design 2 profile - VALE', './design2.pro', 1)
wRprofile = planarProfile('Design 2 profile - VALE', './design2.pro', -1)
wheelRadius = 0.831 * 0.5
wheel = wheelset('Wheel',
wLprofile, wRprofile,
b2bDist=0.917,
gaugeRadius=wheelRadius)
wsmass = 2700. # 1568.
wsInertiaRadial = 656.
wsInertiaAxial = 168.
wsInertiaTensor = np.diag([wsInertiaRadial, wsInertiaAxial,
wsInertiaRadial])
wheel.setMass(wsmass)
wheel.setInertiaTensor(wsInertiaTensor)
wheel.setPositionInitialConditions(0, 0.25)
wheel.setPositionInitialConditions(2, -0.838262/2)
wheel.setVelocityInitialConditions(0, initialVelocity)
wheel.setVelocityInitialConditions(4, -initialVelocity / wheelRadius)
forceWheel = MBS.BodyConnections.Forces.force('Wheel pull force')
forceWheel.connect(wheel, system.ground)
def pullWheelset(t, p, v, m1, m2):
"""
Apply forces to wheelset.
Parameters
----------
t : TYPE
DESCRIPTION.
p : TYPE
DESCRIPTION.
v : TYPE
DESCRIPTION.
m1 : TYPE
DESCRIPTION.
m2 : TYPE
DESCRIPTION.
Returns
-------
f : TYPE
DESCRIPTION.
"""
w = m1.parent
f = np.zeros_like(p)
error, wvel = pullWstGap(t, p, v, m1, m2)
# wvel = v[w.globalDof]
if t > 0.01:
f[w.globalDof[0]] = - 10000 * error
f[w.globalDof[2]] = - wvel[2] * 1e4
f[w.globalDof[3]] = - wvel[3] * 1e8
return f
def pullWstGap(t, p, v, m1, m2):
"""
Determine gap for pull wheelset forces.
Parameters
----------
t : TYPE
DESCRIPTION.
p : TYPE
DESCRIPTION.
v : TYPE
DESCRIPTION.
m1 : TYPE
DESCRIPTION.
m2 : TYPE
DESCRIPTION.
Returns
-------
wpos : TYPE
DESCRIPTION.
wvel : TYPE
DESCRIPTION.
"""
wstBody = m1.parent
wpos = p[wstBody.globalDof]
wvel = v[wstBody.globalDof]
error = wvel[0] - setVelocity
return error, wvel
forceWheel.setForceFunction(pullWheelset)
forceWheel.setGapFunction(pullWstGap)
'''
Constraint on edges
'''
node0fixLeft = MBS.BodyConnections.BodyConnection.nodeEncastreToRigidBody(
'Fix first node on left rail',
track.leftRail,
system.ground,
track.leftRail.elementList[0].nodes[0].marker
)
node0fixRight = MBS.BodyConnections.BodyConnection.nodeEncastreToRigidBody(
'Fix first node on right rail',
track.rightRail,
system.ground,
track.rightRail.elementList[0].nodes[0].marker
)
'''
CONTACT
'''
wheelRailContact = MBS.BodyConnections.Contacts.WheelRailContact.wrContact(
rail, rail2, wheel, 'Wheel-track contact')
'''
Multibody system setup
'''
system.addTrack(track)
system.addBody([wheel])
system.addForce(wheelRailContact)
system.addForce(forceWheel)
# system.addConstraint([node0fixLeft, node0fixRight])
system.setupSystem()
# %% SOLUTION
'''
Solution
'''
problem = system.generate_problem('ind3')
wheelRailContact.evaluateForceFunction(0, system.pos0, system.vel0)
DAE = IDA(problem)
DAE.report_continuously = True
DAE.inith = 1e-4
DAE.maxh = 5e-3
DAE.atol = 1e-6
# DAE.maxord = 3
DAE.num_threads = 12
DAE.suppress_alg = True
outFreq = 5e2 # Hz
finalTime = (track.length - 0.25) / setVelocity
# p0 = DAE.make_consistent('IDA_YA_YDP_INIT')
t, p, v = DAE.simulate(finalTime, finalTime * outFreq)
q = p[:, :system.n_p]
v = p[:, system.n_p:2*system.n_p]
lam = p[:, 2*system.n_p:]
np.savez('results/railOut', t=t, p=p, v=v, lam=lam)
'''
Post-processing
'''
# %% POST PROCESSING
oFiles = np.load('./results/railOut.npz')
t = oFiles['t']
p = oFiles['p']
v = oFiles['v']
lam = oFiles['lam']
system.postProcess(t, p, v)
def plotRails(nplots=4):
from helper_funcs import unitaryVector as uv
plt.figure()
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(8)
b = rail2.plotPositions(8)
k += 1
plt.plot(a[:, 0], a[:, 2], label='{:.2f} s'.format(
t[i]), color='red', alpha=(k/(nplots+1)))
plt.plot(b[:, 0], b[:, 2], 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(system.name)
return a, b
'''
Output wheelset configuration
'''
''' VPYTHON visuals '''
def run_animation(vprate=100):
scene = vp.canvas(width=1600, height=700, background=vp.color.gray(0.7),
forward=vp.vec(1, 0, 0),
up=vp.vec(0, 0, -1), range=0.5)
# w1 = vp.cylinder(axis = vp.vec(0,0,0.5*trackWidth), radius = 0.15)
# w2 = w1.clone(axis = vp.vec(0,0,-0.5*trackWidth))
# wheelRep = vp.compound([w1,w2])
wheelRep = stl.stl_to_triangles('Rodeiro_carga.stl')
wheelRep.pos = vp.vec(*wheel.simQ[0, :3])
# wheelRep.rotate(angle=np.pi/2, axis=vp.vec(1, 0, 0))
wheelRep.visible = True
wheelRep.color = vp.color.cyan
# rail profile
rail_points = track.leftRail.profiles[0].points.tolist()
rail_points.append(rail_points[0])
rail_points = vp.shapes.points(pos=rail_points, rotate=vp.pi/2)
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)
c2 = vp.extrusion(path=path, shape=rail_points, color=vp.color.blue)
# c1 = vp.curve(path, color=vp.color.green, radius=0.01)
c1 = vp.extrusion(path=path, shape=rail_points, color=vp.color.blue)
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)
outFreq = 100
vp.rate(vprate)
for i in range(0, len(t), 20):
scene.title = 't = {} s'.format(t[i])
for n, r in enumerate([rail, rail2]):
r.updateDisplacements(r.simQ[i])
path = [vp.vec(p[0], p[1], p[2])
for p in r.plotPositions().tolist()]
crails[n] = vp.extrusion(
path=path, shape=rail_points, color=vp.color.blue)
wheelRep.pos.x = wheel.simQ[i, 0]
wheelRep.pos.y = wheel.simQ[i, 1]
wheelRep.pos.z = wheel.simQ[i, 2]
wheelRep.rotate(angle=wheel.simU[i, 3]/outFreq, axis=vp.vec(1, 0, 0))
wheelRep.rotate(angle=wheel.simU[i, 4]/outFreq, axis=vp.vec(0, 1, 0))
wheelRep.rotate(angle=wheel.simU[i, 5]/outFreq, axis=vp.vec(0, 0, 1))
# run_animation()
# '''
# Output to Paraview
# '''
# def load_stl(file_path):
# reader = vtk.vtkSTLReader()
# reader.SetFileName(file_path)
# reader.Update()
# return reader.GetOutput()
# def write_vtk(filename, dataset):
# writer = vtk.vtkXMLPolyDataWriter()
# writer.SetFileName(filename)
# writer.SetInputData(dataset)
# writer.Write()
# def output_paraview():
# wheelCenter = np.zeros(3)
# # Load the STL file
# geometry = load_stl('Rodeiro_carga_2.stl')
# for i in range(len(t)):
# wheelCenter[0] = wheel.simQ[i, 0] # Simulate falling motion
# wheelCenter[1] = wheel.simQ[i, 2]
# wheelCenter[2] = wheel.simQ[i, 1]
# # Translate the geometry to the new position
# transform = vtk.vtkTransform()
# transform.Translate(wheelCenter)
# transform_filter = vtk.vtkTransformFilter()
# transform_filter.SetTransform(transform)
# transform_filter.SetInputData(geometry)
# transform_filter.Update()
# polydata = transform_filter.GetOutput()
# # Add wheel.simQ[i,:] values to the point data
# simQ_values = vtk.vtkDoubleArray()
# simQ_values.SetName("SimQ")
# simQ_values.SetNumberOfComponents(3)
# simQ_values.InsertNextTuple(wheel.simQ[i, 0:3])
# polydata.GetPointData().AddArray(simQ_values)
# write_vtk("./results/wheelset_ani_test/wheelset_anim_%04d.vtp" %
# i, polydata)