-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFCIFEM_streamline_upwind.py
212 lines (188 loc) · 7.37 KB
/
FCIFEM_streamline_upwind.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
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 8 13:47:07 2020
@author: samal
"""
from scipy.special import roots_legendre
import scipy.integrate
import scipy.sparse as sp
import scipy.sparse.linalg as sp_la
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def mapping(points, x=0., theta=False, deriv=False):
A = 0.2
phase = -np.pi/2
if deriv:
return A*np.cos(points[:,0] - phase)
elif theta:
return np.arctan(A*np.cos(points[:,0] - phase))
else:
offsets = points[:,1] - A*np.sin(points[:,0] - phase)
return (A*np.sin(x - phase) + offsets) % 1
def f(points):
return np.repeat(0., len(points))
velocity = np.array([1, 0.])
nSteps = 209
dt = 0.01
NX = 3 # number of planes
NY = 20 # number of grid divisions on plane
nNodes = NX*NY
Nquad = 20
ndim = 2
dx = 2*np.pi/NX
dy = 1/NY
arcLengths = np.array([scipy.integrate.quad(lambda x:
np.sqrt(1 + mapping(np.array([[x,0]]), deriv=True)**2),
i*dx, (i+1)*dx)[0] for i in range(NX)])
nodeX = np.array([i*dx for i in range(NX)])
nodeY = np.linspace(0, 1-dy, NY)
# nodeY = [np.linspace(0, 1, NY) for i in range(NX)]
# generate quadrature points
offsets, weights = roots_legendre(Nquad)
offsets = [offsets * np.pi / NX, offsets / (2*NY)]
weights = [weights * np.pi / NX, weights / (2*NY)]
quads = ( np.indices([1, NY], dtype='float').T.reshape(-1, ndim) + 0.5 ) \
* [2*np.pi/NX, 1/NY]
quadWeights = np.repeat(1., len(quads))
for i in range(ndim):
quads = np.concatenate( [quads +
offset*np.eye(ndim)[i] for offset in offsets[i]] )
quadWeights = np.concatenate(
[quadWeights * weight for weight in weights[i]] )
nQuads = len(quads)
# pre-allocate arrays for mass matrix triplets
nEntries = (2*ndim)**2
nMaxEntriesM = nEntries * nQuads * NX
Mdata = np.zeros(nMaxEntriesM)
row_ind = np.zeros(nMaxEntriesM, dtype='int')
col_ind = np.zeros(nMaxEntriesM, dtype='int')
# compute mass matrix
index = 0
phiX = quads[:,0] / dx
for iPlane in range(NX):
mapL = mapping(quads + [iPlane*dx, 0], iPlane * dx)
mapR = mapping(quads + [iPlane*dx, 0], (iPlane+1) * dx)
indL = (mapL // dy).astype('int')
indR = (mapR // dy).astype('int')
phiLY = (mapL - indL*dy) / dy
phiRY = (mapR - indR*dy) / dy
for iQ, quad in enumerate(quads):
phis = np.array([[(1-phiLY[iQ]), (1-phiX[iQ])],
[ phiLY[iQ] , (1-phiX[iQ])],
[(1-phiRY[iQ]), phiX[iQ] ],
[ phiRY[iQ] , phiX[iQ] ]])
phis = np.prod(phis, axis=1)
indices = np.array([indL[iQ] + NY*iPlane,
(indL[iQ]+1) % NY + NY*iPlane,
(indR[iQ] + NY*(iPlane+1)) % nNodes,
((indR[iQ]+1) % NY + NY*(iPlane+1)) % nNodes])
Mdata[index:index+nEntries] = quadWeights[iQ] * \
np.outer(phis, phis).ravel()
row_ind[index:index+nEntries] = np.repeat(indices, 2*ndim)
col_ind[index:index+nEntries] = np.tile(indices, 2*ndim)
index += nEntries
M = sp.csr_matrix( (Mdata, (row_ind, col_ind)), shape=(nNodes, nNodes) )
# pre-allocate arrays for convection matrix triplets
nEntries = ndim + 1
nMaxEntriesA = nEntries * nNodes
Adata = np.zeros(nMaxEntriesA)
row_ind = np.zeros(nMaxEntriesA, dtype='int')
col_ind = np.zeros(nMaxEntriesA, dtype='int')
# compute convection matrix
index = 0
magV = np.linalg.norm(velocity)
nodeWeight = dx*dy
nodeIndices = np.arange(-NY, 0)
for iPlane in range(NX):
nodes = np.vstack((np.repeat(nodeX[iPlane], NY), nodeY)).T
nodeIndices += NY
mapL = mapping(nodes, (iPlane - 1)*dx)
indL = (mapL // dy).astype('int')
phiLY = (mapL - indL*dy) / dy
grad = np.array([[-1/arcLengths[iPlane]], [-1/dy]])
for iN, nodes in enumerate(nodes):
phis = np.array([(1-phiLY[iN]), phiLY[iN], -1])
indices = np.array([NY*(iPlane - 1) + indL[iN],
NY*(iPlane - 1) + (indL[iN]+1) % NY,
nodeIndices[iN]])
Adata[index:index+nEntries] = phis * magV * nodeWeight * grad[0,0]
row_ind[index:index+nEntries] = np.repeat(nodeIndices[iN], 3)
col_ind[index:index+nEntries] = indices
index += nEntries
col_ind[col_ind<0] += nNodes
A = sp.csr_matrix( (Adata, (row_ind, col_ind)), shape=(nNodes, nNodes) )
# Backward-Euler
M /= dt
K = M - A
u = np.zeros(nNodes)
# u[15] = 1;
A = 1.0
r0 = 0.5
sigma = 0.1
for ix in range(NX):
for iy in range(NY):
u[ix*NY + iy] = A*np.exp( -0.5*( ((ix*dx-r0)/sigma)**2
+ ((iy*dy-r0)/sigma)**2 ) ) # Gaussian
# u[ix*NY + iy] = np.sin(ix*dx - np.pi/2) * np.sin(iy*dy*2*np.pi)
dudt = np.zeros(nNodes)
betas = np.array([0.25, 1/3, 0.5, 1])
for i in range(nSteps):
# uTemp = u
# for beta in betas:
# dudt, info = sp_la.cg(M, A @ uTemp, x0=dudt, tol=1e-10, atol=1e-10)
# # self.dudt = sp_la.spsolve(self.M, self.KA@uTemp)
# uTemp = u + beta * dt * dudt
# if (info != 0):
# print(f'solution failed with error code: {info}')
# u = uTemp
u, info = sp_la.lgmres(K, M @ u, u, tol=1e-10, atol=1e-10) # Backward-Euler
periodicIndices = np.concatenate([np.append(np.arange(NY),0) + i*NY
for i in range(NX+1)]) % nNodes
X = np.repeat(np.append(nodeX, 2*np.pi), NY+1)
Y = np.tile(np.append(nodeY, 1), NX+1)
U = u[periodicIndices]
uQuad = np.empty(nQuads * NX)
for iPlane in range(NX):
mapL = mapping(quads + [iPlane*dx, 0], iPlane * dx)
mapR = mapping(quads + [iPlane*dx, 0], (iPlane+1) * dx)
indL = (mapL // dy).astype('int')
indR = (mapR // dy).astype('int')
phiLY = (mapL - indL*dy) / dy
phiRY = (mapR - indR*dy) / dy
for iQ, quad in enumerate(quads):
phis = np.array([(1-phiLY[iQ]) * (1-phiX[iQ]), phiLY[iQ] * (1-phiX[iQ]),
(1-phiRY[iQ]) * phiX[iQ] , phiRY[iQ] * phiX[iQ]])
indices = np.array([indL[iQ] + NY*iPlane,
(indL[iQ]+1) % NY + NY*iPlane,
(indR[iQ] + NY*(iPlane+1)) % nNodes,
((indR[iQ]+1) % NY + NY*(iPlane+1)) % nNodes])
uQuad[iPlane*nQuads + iQ] = np.sum(phis * u[indices])
# For plotting only the quad points
# X = np.concatenate([quads[:,0] + i*dx for i in range(NX)])
# Y = np.tile(quads[:,1], NX)
# U = uQuad
# For concatenating the quad points to the node points
X = np.concatenate([X] + [quads[:,0] + i*dx for i in range(NX)])
Y = np.concatenate((Y, np.tile(quads[:,1], NX)))
U = np.concatenate((U, uQuad))
plt.tripcolor(X, Y, U, shading='gouraud'
,cmap='seismic'
,vmin=-np.max(np.abs(U))
,vmax=np.max(np.abs(U))
)
# tri = mpl.tri.Triangulation(X,Y)
# plt.triplot(tri, 'r-', lw=1)
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, [mapping(np.array([[0, 0.75]]), i) for i in x], 'k')
for xi in nodeX:
plt.plot([xi, xi], [0, 1], 'k:')
plt.plot(X[np.argmax(U)], Y[np.argmax(U)], 'g+', markersize=10)
plt.colorbar()
plt.xlabel(r'$x$')
plt.ylabel(r'$y$', rotation=0)
plt.xticks(np.linspace(0, 2*np.pi, 7),
['0',r'$\pi/3$',r'$2\pi/3$',r'$\pi$',r'$4\pi/3$',r'$5\pi/3$',r'$2\pi$'])
plt.margins(0,0)
plt.show()