-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oldroyd-B1.py
executable file
·438 lines (365 loc) · 13.1 KB
/
Oldroyd-B1.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
#----------------------------------------------------------------------------#
# Fully Spectral Newton Raphson Solver
# Oldroyd B Model
# Last modified: Tue 4 Mar 10:56:51 2014
#----------------------------------------------------------------------------#
"""Solves system of equations using a fully spectral method. Equations given
by: V.dU(y,z)/dy + W.dU/dz = 1/Re .del^2."""
# MODULES
import sys
from scipy import *
from scipy import linalg
from scipy import optimize
from scipy import special
import cPickle as pickle
import ConfigParser
import argparse
# SETTINGS -------------------------------------------------------------------
config = ConfigParser.RawConfigParser()
fp = open('OB-settings.cfg')
config.readfp(fp)
cfgN = config.getint('settings', 'N')
cfgM = config.getint('settings', 'M')
cfgRe = config.getfloat('settings', 'Re')
cfgbeta = config.getfloat('settings','beta')
cfgWeiss = config.getfloat('settings','Weiss')
cfgAmp = config.getfloat('settings', 'Amp')
fp.close()
argparser = argparse.ArgumentParser()
argparser.add_argument("-N", type=int, default=cfgN,
help='Override Number of Fourier modes given in the config file')
argparser.add_argument("-M", type=int, default=cfgM,
help='Override Number of Chebyshev modes in the config file')
argparser.add_argument("-Re", type=float, default=cfgRe,
help="Override Reynold's number in the config file")
argparser.add_argument("-b", type=float, default=cfgbeta,
help='Override beta of the config file')
argparser.add_argument("-Wi", type=float, default=cfgWeiss,
help='Override Weissenberg number of the config file')
argparser.add_argument("-amp", type=float, default=cfgAmp,
help='Override amplitude of the streamwise vortices from the config file')
args = argparser.parse_args()
N = args.N
M = args.M
Re = args.Re
beta = args.b
Weiss = args.Wi
Amp = args.amp
filename = 'pf-N{N}-M{M}-Re{Re}-b{beta}-Wi{Weiss}-amp{Amp}.pickle'.format(\
N=N,M=M,Re=Re,beta=beta,Weiss=Weiss,Amp=Amp)
# -----------------------------------------------------------------------------
# FUNCTIONS
def mk_single_diffy():
"""Makes a matrix to differentiate a single vector of Chebyshev's,
for use in constructing large differentiation matrix for whole system"""
# make matrix:
mat = zeros((M, M), dtype='d')
for m in range(M):
for p in range(m+1, M, 2):
mat[m,p] = 2*p*oneOverC[m]
return mat
def mk_diff_y():
"""Make the matrix to differentiate a velocity vector wrt y."""
D = mk_single_diffy()
MDY = zeros( (vecLen, vecLen) )
for cheb in range(0,vecLen,M):
MDY[cheb:cheb+M, cheb:cheb+M] = D
del cheb
return MDY
def mk_diff_z():
"""Make matrix to do fourier differentiation wrt z."""
MDZ = zeros( (vecLen, vecLen), dtype='complex')
n = -N
for i in range(0, vecLen, M):
MDZ[i:i+M, i:i+M] = eye(M, M, dtype='complex')*n*gamma*1.j
n += 1
del n, i
return MDZ
def cheb_prod_mat(velA):
"""Function to return a matrix for left-multiplying two matrices
of velocities."""
D = zeros((M, M), dtype='complex')
#failcount = 0
for n in range(M):
for m in range(-M+1,M): # Bottom of range is inclusive
itr = abs(n-m)
if (itr < M):
D[n, abs(m)] += 0.5*oneOverC[n]*CFunc[itr]*CFunc[abs(m)]*velA[itr]
del m, n, itr
return D
def prod_mat(velA):
"""Function to return a matrix ready for the left dot product with another
velocity vector"""
MM = zeros((vecLen, vecLen), dtype='complex')
#First make the middle row
midMat = zeros((M, vecLen), dtype='complex')
for n in range(2*N+1): # Fourier Matrix is 2*N+1 cheb matricies
yprodmat = cheb_prod_mat(velA[n*M:(n+1)*M])
endind = 2*N+1-n
midMat[:, (endind-1)*M:endind*M] = yprodmat
del n
#copy matrix into MM, according to the matrix for spectral space
# top part first
for i in range(0, N):
MM[i*M:(i+1)*M, :] = column_stack((midMat[:, (N-i)*M:], zeros((M, (N-i)*M))) )
del i
# middle
MM[N*M:(N+1)*M, :] = midMat
# bottom
for i in range(0, N):
MM[(i+N+1)*M:(i+2+N)*M, :] = column_stack((zeros((M, (i+1)*M)), midMat[:, :(2*N-i)*M] ))
del i
return MM
def mk_V():
V = zeros((M, 2*N+1), dtype = 'complex')
for m in range(0,M,2):
V[m,N-1] = 2*oneOverC[m]*( ((-1)**(m/2))*(special.jv(m,p)/cos(p)) -
special.iv(m,gamma)/cosh(gamma) )
V[m,N+1] = 2*oneOverC[m]*( ((-1)**(m/2))*(special.jv(m,p)/cos(p)) -
special.iv(m,gamma)/cosh(gamma) )
del m
V = 0.5*V #For the cosine amplitude.
Normal = ( cos(p)*cosh(gamma) ) / ( cosh(gamma) - cos(p) )
V = Amp * Normal * V
return V.T.flatten() #return 1D array
def mk_W():
W = zeros((M, 2*N+1), dtype = 'complex')
for m in range(0,M,2):
W[m,N-1] = 2*oneOverC[m]*( ((-1)**(m/2))*(special.jv(m,p)/cos(p)) -
special.iv(m,gamma)/cosh(gamma) )
W[m,N+1] = 2*oneOverC[m]*( ((-1)**(m/2))*(special.jv(m,p)/cos(p)) -
special.iv(m,gamma)/cosh(gamma) )
del m
chebdY = mk_single_diffy()
W[:,N-1] = -dot(chebdY, W[:,N-1])
W[:,N+1] = -dot(chebdY, W[:,N+1])
W[:,N-1] = W[:,N-1]*0.5j
W[:,N+1] = W[:,N+1]*-0.5j
Normal = ( cos(p)*cosh(gamma) ) / ( cosh(gamma) - cos(p) )
W = Amp * Normal * W / gamma
return W.T.flatten() #return 1D array
def solve_eq1():
"""Oldroyd-B Equation independent of U
Linearly solves system of equations for vector containing Cyy, Czz, Cyz"""
#RHS of equation
RHS = zeros(3*vecLen,dtype='D')
RHS[N*M] = oneOverWeiss
RHS[vecLen + N*M] = oneOverWeiss
# Make the Jacobian:
jacobian = zeros((3*vecLen, 3*vecLen), dtype='complex')
# (GRAD - 2dVdy + I/Wi)*dCyy
jacobian[0:vecLen, 0:vecLen] = \
GRAD - 2*MMDYV + oneOverWeiss*eye(vecLen, vecLen)
# 0*dCzz
# (-2dVdz)*dCyz
jacobian[0:vecLen, 2*vecLen:3*vecLen] = -2*MMDZV
# 2nd block of rows
# 0*dCyy
# (GRAD - 2*MMDZW + I/Wi)*dCzz
jacobian[vecLen:2*vecLen, vecLen:2*vecLen] = \
GRAD - 2*MMDZW + oneOverWeiss*eye(vecLen, vecLen)
# (-2*dWdy)*dCyz
jacobian[vecLen:2*vecLen, 2*vecLen:3*vecLen] = -2*MMDYW
# 3rd block of rows
# (-dWdy)*dCyy
jacobian[2*vecLen:3*vecLen, 0:vecLen] = -MMDYW
# (-dVdz)*dCzz
jacobian[2*vecLen:3*vecLen, vecLen:2*vecLen] = -MMDZV
# (GRAD + I/Wi)*dCyz
jacobian[2*vecLen:3*vecLen, 2*vecLen:3*vecLen] = \
GRAD + oneOverWeiss*eye(vecLen, vecLen)
return linalg.solve(jacobian, RHS)
def solve_eq2(x):
"""use functions to find the residuals vector for the equation containing U"""
#cut x into U, and the Conformation tensor arrays
U = (x[0:vecLen])
conxx = (x[vecLen: 2*vecLen])
conxy = (x[2*vecLen: 3*vecLen])
conxz = (x[3*vecLen: 4*vecLen])
#calculate the stress tensor components, which are used to solve
#the equations
tauxx = oneOverWeiss*conxx
tauxx[N*M] -= oneOverWeiss
tauxy = oneOverWeiss*conxy
tauxz = oneOverWeiss*conxz
#some useful matrices to have:
MMDYU = prod_mat(dot(MDY, U))
MMDZU = prod_mat(dot(MDZ, U))
#calculate the equations
#xx 0 = v.grad(Cxx) - 2(Cxy.dy.U + CxzdzU) + tauxx
resxx = (dot(GRAD, conxx)
- 2*dot(MMDYU, conxy)
- 2*dot(MMDZU, conxz)
+ tauxx)
# xy 0 = (v.grad)Cxy - CyydyU - CxydyV - CxzdzV - CyzdzU + tauxy"""
resxy = ( dot(GRAD, conxy)
- dot(MMDYU, conyy)
- dot(MMDZU, conyz)
- dot(MMDYV, conxy)
- dot(MMDZV, conxz)
+ tauxy )
# xz 0 = (v.grad)Cxz - CxydyW - CxzDzW - CyzdyU - CzzdzU + tauxz"""
resxz = ( dot(GRAD, conxz)
- dot(MMDYW, conxy)
- dot(MMDZW, conxz)
- dot(MMDYU, conyz)
- dot(MMDZU, conzz)
+ tauxz )
# Navier-Stokes 0 = -Re*(v.grad)U + beta.laplacian + (1-beta).div.tau"""
resNS = ( -Re*dot(GRAD, U) + beta*dot(LAPLACIAN, U)
+ (1-beta)*(dot(MDY, tauxy) + dot(MDZ,tauxz)))
# Impose Boundary condition equations
Cheb0 = zeros(M, dtype='complex')
Cheb0[0] = 1
resNS[N*M + M-2] = dot(BTOP, (U[N*M:(N+1)*M] - Cheb0))
resNS[N*M + M-1] = dot(BBOT, (U[N*M:(N+1)*M] + Cheb0))
for k in range (N):
resNS[k*M + M-2] = dot(BTOP, U[k*M:k*M + M])
resNS[k*M + M-1] = dot(BBOT, U[k*M:k*M + M])
del k
for k in range(N+1, 2*N+1):
resNS[k*M + M-2] = dot(BTOP, U[k*M:k*M + M])
resNS[k*M + M-1] = dot(BBOT, U[k*M:k*M + M])
del k
# make residuals vector in 1D again
residuals = zeros(4*vecLen, dtype='complex')
residuals[0:vecLen] = resNS
residuals[vecLen:2*vecLen] = resxx
residuals[2*vecLen:3*vecLen] = resxy
residuals[3*vecLen:4*vecLen] = resxz
#make the Jacobian:
jacobian = zeros((4*vecLen,4*vecLen), dtype='complex')
#First row of blocks, for U equation:
#-Re*(Grad)dU
jacobian[0:vecLen, 0:vecLen] = -Re*GRAD + beta*LAPLACIAN
#0*dCxx
#((1-beta)1/Wi*d/dy)dCxy
jacobian[0:vecLen, 2*vecLen:3*vecLen] = \
(1-beta)*oneOverWeiss*MDY
#((1-beta)1/wi*d/dz)dCxz
jacobian[0:vecLen, 3*vecLen:4*vecLen] = \
(1-beta)*oneOverWeiss*MDZ
#2nd Row of blocks, for conxx equation:
#(-2conxy*d/dy -2conxz*d/dz)dU
jacobian[vecLen:2*vecLen, 0:vecLen] = \
-2*( dot(prod_mat(conxy), MDY) + dot(prod_mat(conxz), MDZ) )
#(GRAD + I/Wi)dCxx
jacobian[vecLen:2*vecLen, vecLen:2*vecLen] = \
GRAD + oneOverWeiss*eye(vecLen,vecLen, dtype='complex')
#(-2dUdy)dCxy
jacobian[vecLen:2*vecLen, 2*vecLen:3*vecLen] = \
-2*( MMDYU )
#(-2dUdz)dCxz
jacobian[vecLen:2*vecLen, 3*vecLen:4*vecLen] = \
-2*( MMDZU )
#Third row of blocks, for conxy equation:
#(-Cyy*d/dy -Cyz*d/dz)dU
jacobian[2*vecLen:3*vecLen, 0:vecLen] = \
- dot(prod_mat(conyy), MDY) - dot(prod_mat(conyz), MDZ)
#0*dCxx
#(GRAD - dVdy)*dCxy
jacobian[2*vecLen:3*vecLen, 2*vecLen:3*vecLen] = \
( GRAD - MMDYV + oneOverWeiss*eye(vecLen,vecLen, dtype='complex') )
#(-dV/dz)*dCxz
jacobian[2*vecLen:3*vecLen, 3*vecLen:4*vecLen] = \
-MMDZV
#4th row of blocks, conxz equation:
#(-Czz*d/dz -Cyz*d/dy)*dU
jacobian[3*vecLen:4*vecLen, 0:vecLen] = \
-dot(prod_mat(conzz), MDZ) - dot(prod_mat(conyz), MDY)
#(0)*dCxx
#(-dW/dy)*dCxy
jacobian[3*vecLen:4*vecLen, 2*vecLen:3*vecLen] = \
-MMDYW
#(Grad - dW/dz)*dCxz
jacobian[3*vecLen:4*vecLen, 3*vecLen:4*vecLen] = \
( GRAD - MMDZW + oneOverWeiss*eye(vecLen,vecLen, dtype='complex') )
#Apply BC's in the Jacobian
for n in range(2*N+1):
jacobian[n*M + M-2, 0 : 4*vecLen ] = \
concatenate( (zeros(n*M), BTOP, zeros((2*N-n)*M+3*vecLen)) )
jacobian[n*M + M-1, 0 : 4*vecLen ] = \
concatenate( (zeros(n*M), BBOT, zeros((2*N-n)*M+3*vecLen)) )
del n
return (jacobian, residuals)
def NR_solve(the_eq, xguess):
"""use Newton-Raphson to solve """
while True:
(J_x0, f_x0) = the_eq(xguess)
dx = linalg.solve(J_x0, -f_x0)
xguess = xguess + dx
print linalg.norm(f_x0, 2)
if (linalg.norm(f_x0,2) < NRdelta): break
return xguess
def save_pickle(array, name):
f = open(name, 'w')
pickle.dump(array, f)
f.close()
#
# MAIN
#
print """
----------------------------------------
N = {0}
M = {1}
Re = {2}
beta = {3}
Weiss = {4}
amp = {5}
----------------------------------------
""". format(N, M, Re, beta, Weiss, Amp)
gamma = pi / 2.
p = optimize.fsolve(lambda p: p*tan(p) + gamma*tanh(gamma), 2)
zLength = 2.*pi/gamma
vecLen = M*(2*N+1)
NRdelta = 0.00001
# Set the oneOverC function: 1/2 for m=0, 1 elsewhere:
oneOverC = ones(M)
oneOverC[0] = 1. / 2.
#set up the CFunc function: 2 for m=0, 1 elsewhere:
CFunc = ones(M)
CFunc[0] = 2.
#make the differentiation matrices:
MDY = mk_diff_y()
MDYY = dot(MDY,MDY)
MDZ = mk_diff_z()
MDZZ = dot(MDZ,MDZ)
V = mk_V()
W = mk_W()
GRAD = dot(prod_mat(V),MDY) + dot(prod_mat(W),MDZ)
LAPLACIAN = dot(MDY,MDY) + dot(MDZ,MDZ)
MMDYV = prod_mat(dot(MDY, V))
MMDZV = prod_mat(dot(MDZ, V))
MMDYW = prod_mat(dot(MDY, W))
MMDZW = prod_mat(dot(MDZ, W))
#Boundary arrays:
BTOP = ones(M)
BBOT = ones(M)
BBOT[1:M:2] = -1
oneOverWeiss = 1. / Weiss
#Guess conformation tensor:
conxx = zeros(vecLen, dtype='complex')
conyy = zeros(vecLen, dtype='complex')
conzz = zeros(vecLen, dtype='complex')
conxy = zeros(vecLen, dtype='complex')
conxz = zeros(vecLen, dtype='complex')
conyz = zeros(vecLen, dtype='complex')
#solve first equation:
x1 = solve_eq1()
conyy = x1[0:vecLen]
conzz = x1[vecLen:2*vecLen]
conyz = x1[2*vecLen:3*vecLen]
#solve second equation:
x2 = zeros(4*vecLen, dtype='complex')
#solve equation 2:
while True:
(J_x0, f_x0) = solve_eq2(x2)
dx = linalg.solve(J_x0, -f_x0)
x2 = x2 + dx
print linalg.norm(f_x0, 2)
if (linalg.norm(f_x0,2) < NRdelta): break
U = x2[0:vecLen]
conxx=x2[1*vecLen:2*vecLen]
conxy=x2[2*vecLen:3*vecLen]
conxz=x2[3*vecLen:4*vecLen]
save_pickle((U,V,W,conxx,conyy,conzz,conxy,conxz,conyz), filename)