-
Notifications
You must be signed in to change notification settings - Fork 0
/
squeezer.py
364 lines (304 loc) · 10.5 KB
/
squeezer.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
from scipy import *
import scipy.optimize as so
import scipy.linalg as sl
import numpy
import math
from assimulo.problem import Implicit_Problem
from assimulo.solvers import IDA
import matplotlib.pyplot as plt
from assimulo.solvers.runge_kutta import RungeKutta4
from assimulo.problem import Explicit_Problem
from assimulo.solvers.runge_kutta import RungeKutta34
def init_squeezer():
y_1 = array([-0.0617138900142764496358948458001, # beta
0., # theta
0.455279819163070380255912382449, # gamma
0.222668390165885884674473185609, # phi
0.487364979543842550225598953530, # delta
-0.222668390165885884674473185609, # Omega
1.23054744454982119249735015568]) #epsilon
lamb = array([
98.5668703962410896057654982170, # lambda[0]
-6.12268834425566265503114393122]) # lambda[1]
y=hstack((y_1,zeros((7,)),lamb,zeros((4,))))
yp=hstack((zeros(7,),array([
14222.4439199541138705911625887, # betadotdot
-10666.8329399655854029433719415, # Thetadotdot
0.,0.,0.,0.,0.]),zeros((6,))))
return y,yp
def squeezer3 (t, y, yp):
y, lamb, g, gp, gqq, ff, m = defaultSqueezer(t, y)
res_1 = yp[0:7] - y[7:14]
res_2 = dot(m,yp[7:14])- ff[0:7]+dot(gp.T,lamb)
res_3 = g
r = hstack((res_1,res_2,res_3))
return r
def squeezer2 (t, y, yp):
y, lamb, g, gp, gqq, ff, m = defaultSqueezer(t, y)
res_1 = yp[0:7] - y[7:14]
res_2 = dot(m,yp[7:14])- ff[0:7]+dot(gp.T,lamb)
v = y[7:14]
res_3 = dot(gp,v)
r = hstack((res_1,res_2,res_3))
return r
def squeezer1 (t, y):
if (t == 0):
y, yd0 = init_squeezer()
return yd0
y, lamb, g, gp, gqq, ff, m = defaultSqueezer(t, y)
yp = zeros(14)
yp[0:7] = y[7:14]
Minv = sl.inv(m)
A = dot(dot(gp,Minv), gp.T)
b = gqq + dot(gp,dot(Minv,ff))
lambdad = sl.solve(A, b)
x = dot(gp.T,lambdad)
x2 = ff-x
w = dot(Minv,x2)
#print("x2 ========== ", x2)
yp[7:14] = w
#yp[14:20] = zeros(6)
#print("yp:===== ",yp)
return yp
def defaultSqueezer(t, y):
"""
Residual function of the 7-bar mechanism in
Hairer, Vol. II, p. 533 ff, see also formula (7.11)
written in residual form
y,yp vector of dim 20, t scalar
"""
#print("y: ", y)
# Inertia data
m1,m2,m3,m4,m5,m6,m7=.04325,.00365,.02373,.00706,.07050,.00706,.05498
i1,i2,i3,i4,i5,i6,i7=2.194e-6,4.410e-7,5.255e-6,5.667e-7,1.169e-5,5.667e-7,1.912e-5
# Geometry
xa,ya=-.06934,-.00227
xb,yb=-0.03635,.03273
xc,yc=.014,.072
d,da,e,ea=28.e-3,115.e-4,2.e-2,1421.e-5
rr,ra=7.e-3,92.e-5
ss,sa,sb,sc,sd=35.e-3,1874.e-5,1043.e-5,18.e-3,2.e-2
ta,tb=2308.e-5,916.e-5
u,ua,ub=4.e-2,1228.e-5,449.e-5
zf,zt=2.e-2,4.e-2
fa=1421.e-5
# Driving torque
mom=0.033
# Spring data
c0=4530.
lo=0.07785
# Initial computations and assignments
beta,theta,gamma,phi,delta,omega,epsilon=y[0:7]
bep,thp,gap,php,dep,omp,epp=y[7:14]
lamb=y[14:20]
sibe,sith,siga,siph,side,siom,siep=sin(y[0:7])
cobe,coth,coga,coph,code,coom,coep=cos(y[0:7])
sibeth = sin(beta+theta);cobeth = cos(beta+theta)
siphde = sin(phi+delta);cophde = cos(phi+delta)
siomep = sin(omega+epsilon);coomep = cos(omega+epsilon)
# Mass matrix
m=zeros((7,7))
m[0,0] = m1*ra**2 + m2*(rr**2-2*da*rr*coth+da**2) + i1 + i2
m[1,0] = m[0,1] = m2*(da**2-da*rr*coth) + i2
m[1,1] = m2*da**2 + i2
m[2,2] = m3*(sa**2+sb**2) + i3
m[3,3] = m4*(e-ea)**2 + i4
m[4,3] = m[3,4] = m4*((e-ea)**2+zt*(e-ea)*siph) + i4
m[4,4] = m4*(zt**2+2*zt*(e-ea)*siph+(e-ea)**2) + m5*(ta**2+tb**2)+ i4 + i5
m[5,5] = m6*(zf-fa)**2 + i6
m[6,5] = m[5,6] = m6*((zf-fa)**2-u*(zf-fa)*siom) + i6
m[6,6] = m6*((zf-fa)**2-2*u*(zf-fa)*siom+u**2) + m7*(ua**2+ub**2)+ i6 + i7
# Applied forces
xd = sd*coga + sc*siga + xb
yd = sd*siga - sc*coga + yb
lang = sqrt ((xd-xc)**2 + (yd-yc)**2)
force = - c0 * (lang - lo)/lang
fx = force * (xd-xc)
fy = force * (yd-yc)
ff=array([
mom - m2*da*rr*thp*(thp+2*bep)*sith,
m2*da*rr*bep**2*sith,
fx*(sc*coga - sd*siga) + fy*(sd*coga + sc*siga),
m4*zt*(e-ea)*dep**2*coph,
- m4*zt*(e-ea)*php*(php+2*dep)*coph,
- m6*u*(zf-fa)*epp**2*coom,
m6*u*(zf-fa)*omp*(omp+2*epp)*coom])
# constraint matrix G
gp=zeros((6,7))
gp[0,0] = - rr*sibe + d*sibeth
gp[0,1] = d*sibeth
gp[0,2] = - ss*coga
gp[1,0] = rr*cobe - d*cobeth
gp[1,1] = - d*cobeth
gp[1,2] = - ss*siga
gp[2,0] = - rr*sibe + d*sibeth
gp[2,1] = d*sibeth
gp[2,3] = - e*cophde
gp[2,4] = - e*cophde + zt*side
gp[3,0] = rr*cobe - d*cobeth
gp[3,1] = - d*cobeth
gp[3,3] = - e*siphde
gp[3,4] = - e*siphde - zt*code
gp[4,0] = - rr*sibe + d*sibeth
gp[4,1] = d*sibeth
gp[4,5] = zf*siomep
gp[4,6] = zf*siomep - u*coep
gp[5,0] = rr*cobe - d*cobeth
gp[5,1] = - d*cobeth
gp[5,5] = - zf*coomep
gp[5,6] = - zf*coomep - u*siep
# Index-3 constraint
g=zeros((6,))
g[0] = rr*cobe - d*cobeth - ss*siga - xb
g[1] = rr*sibe - d*sibeth + ss*coga - yb
g[2] = rr*cobe - d*cobeth - e*siphde - zt*code - xa
g[3] = rr*sibe - d*sibeth + e*cophde - zt*side - ya
g[4] = rr*cobe - d*cobeth - zf*coomep - u*siep - xa
g[5] = rr*sibe - d*sibeth - zf*siomep + u*coep - ya
# Index-1 constraint
gqq=zeros((6,))
v = y[7:14]
#print("v in defaultSq: ", v)
gqq[0]=-rr*cobe*v[0]**2 + d*cobeth*(v[0]+v[1])**2 + ss*siga*v[2]**2
gqq[1]=-rr*sibe*v[0]**2 + d*sibeth*(v[0]+v[1])**2 - ss*coga*v[2]**2
gqq[2]=-rr*cobe*v[0]**2 + d*cobeth*(v[0]+v[1])**2 + e*siphde*(v[3]+v[4])**2 + zt*code*v[4]**2
gqq[3]=-rr*sibe*v[0]**2 + d*sibeth*(v[0]+v[1])**2 - e*cophde*(v[3]+v[4])**2 + zt*side*v[4]**2
gqq[4]=-rr*cobe*v[0]**2 + d*cobeth*(v[0]+v[1])**2 + zf*coomep*(v[5]+v[6])**2 + u*siep*v[6]**2
gqq[5]=-rr*sibe*v[0]**2 + d*sibeth*(v[0]+v[1])**2 + zf*siomep*(v[5]+v[6])**2 - u*coep*v[6]**2
#print("gqq in defaultSq: ", gqq)
# Construction of the residual
return y, lamb, g, gp, gqq, ff, m
def jacobian(y):
#here we set our constant parameter
theta = 0
y = numpy.insert(y, 1, theta)
# Geometry
d,da,e,ea=28.e-3,115.e-4,2.e-2,1421.e-5
rr,ra=7.e-3,92.e-5
ss,sa,sb,sc,sd=35.e-3,1874.e-5,1043.e-5,18.e-3,2.e-2
ta,tb=2308.e-5,916.e-5
u,ua,ub=4.e-2,1228.e-5,449.e-5
zf,zt=2.e-2,4.e-2
# Driving torque
# Spring data
# Initial computations and assignments
beta,theta,gamma,phi,delta,omega,epsilon=y[0:7]
sibe,sith,siga,siph,side,siom,siep=sin(y[0:7])
cobe,coth,coga,coph,code,coom,coep=cos(y[0:7])
sibeth = sin(beta+theta);cobeth = cos(beta+theta)
siphde = sin(phi+delta);cophde = cos(phi+delta)
siomep = sin(omega+epsilon);coomep = cos(omega+epsilon)
#The Jacbian
gp=zeros((6,7))
gp[0,0] = - rr*sibe + d*sibeth
gp[0,1] = d*sibeth
gp[0,2] = - ss*coga
gp[1,0] = rr*cobe - d*cobeth
gp[1,1] = - d*cobeth
gp[1,2] = - ss*siga
gp[2,0] = - rr*sibe + d*sibeth
gp[2,1] = d*sibeth
gp[2,3] = - e*cophde
gp[2,4] = - e*cophde + zt*side
gp[3,0] = rr*cobe - d*cobeth
gp[3,1] = - d*cobeth
gp[3,3] = - e*siphde
gp[3,4] = - e*siphde - zt*code
gp[4,0] = - rr*sibe + d*sibeth
gp[4,1] = d*sibeth
gp[4,5] = zf*siomep
gp[4,6] = zf*siomep - u*coep
gp[5,0] = rr*cobe - d*cobeth
gp[5,1] = - d*cobeth
gp[5,5] = - zf*coomep
gp[5,6] = - zf*coomep - u*siep
gp = numpy.delete(gp, 1, 1)
return gp
def residual(y):
#here we set our constant parameter
theta = 0
y = numpy.insert(y, 1, theta)
# Geometry
xa,ya=-.06934,-.00227
xb,yb=-0.03635,.03273
xc,yc=.014,.072
d,da,e,ea=28.e-3,115.e-4,2.e-2,1421.e-5
rr,ra=7.e-3,92.e-5
ss,sa,sb,sc,sd=35.e-3,1874.e-5,1043.e-5,18.e-3,2.e-2
u,ua,ub=4.e-2,1228.e-5,449.e-5
zf,zt=2.e-2,4.e-2
# Initial computations and assignments
beta,theta,gamma,phi,delta,omega,epsilon=y[0:7]
sibe,sith,siga,siph,side,siom,siep=sin(y[0:7])
cobe,coth,coga,coph,code,coom,coep=cos(y[0:7])
sibeth = sin(beta+theta);cobeth = cos(beta+theta)
siphde = sin(phi+delta);cophde = cos(phi+delta)
siomep = sin(omega+epsilon);coomep = cos(omega+epsilon)
g=zeros((6,))
g[0] = rr*cobe - d*cobeth - ss*siga - xb
g[1] = rr*sibe - d*sibeth + ss*coga - yb
g[2] = rr*cobe - d*cobeth - e*siphde - zt*code - xa
g[3] = rr*sibe - d*sibeth + e*cophde - zt*side - ya
g[4] = rr*cobe - d*cobeth - zf*coomep - u*siep - xa
g[5] = rr*sibe - d*sibeth - zf*siomep + u*coep - ya
return g
def findInitialValues(x0):
x0 = numpy.delete(x0, 1)
x0 = x0*0
y0 = so.fsolve(residual, x0, fprime=jacobian)
theta = 0
y0 = numpy.insert(y0, 1, theta)
return y0
y0, yd0 = init_squeezer()
y0own = findInitialValues(y0[:7])
print("=====ydiff:==== ", y0[:7]-y0own)
t0 = 0.0
posIndex = list(range(0,7))
velocityIndex = list(range(7,14))
lambdaIndex = list(range(14,20))
algvar = numpy.ones(numpy.size(y0))
indexNumber = 1
#1 - expl runge - index1 solution
#2 - index2
#3 - index3
if indexNumber == 3:
problem = Implicit_Problem(squeezer3, y0, yd0, t0)
algvar[lambdaIndex] = 0
algvar[velocityIndex] = 0
sim = IDA(problem)
sim.atol = numpy.ones(numpy.size(y0))*1e-7
sim.atol[lambdaIndex] = 1e-1
sim.atol[velocityIndex] = 1e-5
elif indexNumber == 2:
problem = Implicit_Problem(squeezer2, y0, yd0, t0)
algvar[lambdaIndex] = 0
algvar[velocityIndex] = 1
sim = IDA(problem)
sim.atol = numpy.ones(numpy.size(y0))*1e-7
sim.atol[lambdaIndex] = 1e-5
sim.atol[velocityIndex] = 1e-5
elif indexNumber == 1:
problem = Explicit_Problem(squeezer1, y0[0:14], t0)
sim = RungeKutta34(problem)
sim.atol = numpy.ones(14)*1e-7
problem.name = 'Squeezer'
sim.rtol = 1e-8
tfinal = 0.03
ncp = 5000
if indexNumber == 2 or indexNumber == 3:
sim.algvar = algvar
sim.suppress_alg = True
t, y, yd = sim.simulate(tfinal, ncp)
elif indexNumber == 1:
t, y, = sim.simulate(tfinal, ncp)
print(numpy.shape(y))
#sim.plot()
pltVector = (y[:,:7]+1*numpy.pi)%(2*numpy.pi)-1*numpy.pi
#pltVector = y[:,lambdaIndex]
#pltVector = (y[:,:7])
beta_plt = plt.plot(t, pltVector,'.' )
plt.ylabel('Vinkel (rad)')
plt.xlabel('Tid (s)')
plt.legend(["beta", "theta", "gamma", "phi", "delta", "omega", "epsilon"], loc = 'lower left')
#plt.label('beta', 'gamma', 'phi', 'delta', 'omega', 'epsilon')
plt.show()