-
Notifications
You must be signed in to change notification settings - Fork 0
/
bases_solver.py
393 lines (352 loc) · 14.3 KB
/
bases_solver.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
from __future__ import division
import numpy as np
import scipy.sparse as spa
from .tpfa import tpfa
def _solve1D(G,DG,K):
# Solve the lower dimensional problem on the edges
K = np.reshape(K,(G['nz']*G['ny']*G['nx'],3))
N_edge = len(DG['edge_cells'])
bases1D = [list() for _ in range(N_edge)]
for edge in range(N_edge):
cells=DG['edge_cells'][edge]
nodes=DG['connectivity']['edge_nodes'][edge]
centers=[DG['nodes'][node] for node in nodes]
Grid = dict()
Grid['nx']=np.size(cells); Grid['hx']=1
Grid['ny']=1; Grid['hy']=1
Grid['nz']=1; Grid['hz']=1
N=Grid['nx']*Grid['ny']*Grid['nz']
Grid['K']=np.zeros((Grid['nz'],Grid['ny'],Grid['nx'],3))
Grid['K'][0,0,:,:]=K[cells,:]
idxs = [i for i,j in enumerate(cells) if j in centers]
for idx in idxs:
vals = np.zeros(len(idxs))
vals[idxs.index(idx)] = 1.0
dirichlet = np.column_stack([idxs, vals])
q=np.zeros(N)
P = tpfa(Grid,Grid['K'],q,dirichlet)
bases1D[edge].append((nodes[idxs.index(idx)],P.ravel()))
return bases1D
def _solve2D(G,CG,DG,K,bases1D,timer=None):
# Solve for the bases for each dual cell
K = np.reshape(K,(G['nz']*G['ny']*G['nx'],3))
N_dg = len(DG['cells'])
bases2D = [list() for _ in range(N_dg)]
L=G['nx']; l=CG['nx']
lp=int(L/l)
Grid=dict()
Grid['nx']=lp+1; Grid['hx']=1
Grid['ny']=lp+1; Grid['hy']=1
Grid['nz']=1; Grid['hz']=1
N=Grid['nx']*Grid['ny']*Grid['nz']
Grid['K']=np.zeros((Grid['nz'],Grid['ny'],Grid['nx'],3))
for j in range(1,l):
for i in range(1,l):
cell=j*(l+1)+i
cells=DG['cells'][cell]
nodes=DG['connectivity']['cell_nodes'][cell]
edges=DG['connectivity']['cell_edges'][cell]
Grid['K'][0,:,:,:]=np.reshape(K[cells,:],(Grid['ny'],Grid['nx'],3))
for node in nodes:
bottom = range(0,Grid['nx'],1)
top = range(N-Grid['nx'],N,1)
left = range(0,N-Grid['nx']+1,Grid['nx'])
right = range(Grid['nx']-1,N,Grid['nx'])
idxs = list(set(bottom+top+left+right))
vals = np.zeros(len(idxs))
basis_idxs = list()
basis_vals = list()
for edge in edges:
bases = bases1D[edge]
for basis in bases:
if node == basis[0]:
basis_idxs.extend([ii for ii,jj in
enumerate(cells) if
jj in DG['edge_cells'][edge]])
basis_vals.extend(basis[1])
# Dirichlet conditions
for idx in basis_idxs:
if idx in idxs:
val_idx = idxs.index(idx)
vals[val_idx] = basis_vals[basis_idxs.index(idx)]
dirichlet = np.column_stack([idxs,vals])
q=np.zeros(N)
P = tpfa(Grid, Grid['K'], q, dirichlet,timer=timer)
bases2D[cell].append((node,P.ravel()))
# Bottom half cells
Grid=dict()
Grid['nx']=lp+1; Grid['hx']=1
Grid['ny']=int((lp+1)/2); Grid['hy']=1
Grid['nz']=1; Grid['hz']=1
N=Grid['nx']*Grid['ny']*Grid['nz']
Grid['K']=np.zeros((Grid['nz'],Grid['ny'],Grid['nx'],3))
j = 0
for i in range(1,l):
cell=j*(l+1)+i
cells=DG['cells'][cell]
nodes=DG['connectivity']['cell_nodes'][cell]
edges=DG['connectivity']['cell_edges'][cell]
Grid['K'][0,:,:,:]=np.reshape(K[cells,:],(Grid['ny'],Grid['nx'],3))
for node in nodes:
# bottom = range(0,Grid['nx'],1)
top = range(N-Grid['nx'],N,1)
left = range(0,N-Grid['nx']+1,Grid['nx'])
right = range(Grid['nx']-1,N,Grid['nx'])
idxs = list(set(top+left+right))
vals = np.zeros(len(idxs))
basis_idxs = list()
basis_vals = list()
for edge in edges:
bases = bases1D[edge]
for basis in bases:
if node == basis[0]:
basis_idxs.extend([ii for ii,jj in
enumerate(cells) if
jj in DG['edge_cells'][edge]])
basis_vals.extend(basis[1])
for idx in basis_idxs:
if idx in idxs:
val_idx = idxs.index(idx)
vals[val_idx] = basis_vals[basis_idxs.index(idx)]
dirichlet = np.column_stack([idxs,vals])
q=np.zeros(N)
P = tpfa(Grid, Grid['K'], q, dirichlet)
bases2D[cell].append((node,P.ravel()))
# Top half cells
Grid=dict()
Grid['nx']=int(lp+1); Grid['hx']=1
Grid['ny']=int((lp+1)/2); Grid['hy']=1
Grid['nz']=1; Grid['hz']=1
N=Grid['nx']*Grid['ny']*Grid['nz']
Grid['K']=np.zeros((Grid['nz'],Grid['ny'],Grid['nx'],3))
j = l
for i in range(1,l):
cell=j*(l+1)+i
cells=DG['cells'][cell]
nodes=DG['connectivity']['cell_nodes'][cell]
edges=DG['connectivity']['cell_edges'][cell]
Grid['K'][0,:,:,:]=np.reshape(K[cells,:],(Grid['ny'],Grid['nx'],3))
for node in nodes:
bottom = range(0,Grid['nx'],1)
# top = range(N-Grid['nx'],N,1)
left = range(0,N-Grid['nx']+1,Grid['nx'])
right = range(Grid['nx']-1,N,Grid['nx'])
idxs = list(set(bottom+left+right))
vals = np.zeros(len(idxs))
basis_idxs = list()
basis_vals = list()
for edge in edges:
bases = bases1D[edge]
for basis in bases:
if node == basis[0]:
basis_idxs.extend([ii for ii,jj in
enumerate(cells) if
jj in DG['edge_cells'][edge]])
basis_vals.extend(basis[1])
for idx in basis_idxs:
if idx in idxs:
val_idx = idxs.index(idx)
vals[val_idx] = basis_vals[basis_idxs.index(idx)]
dirichlet = np.column_stack([idxs,vals])
q=np.zeros(N)
P = tpfa(Grid, Grid['K'], q, dirichlet)
bases2D[cell].append((node,P.ravel()))
# Left half cells
Grid=dict()
Grid['nx']=int((lp+1)/2); Grid['hx']=1
Grid['ny']=int(lp+1); Grid['hy']=1
Grid['nz']=1; Grid['hz']=1
N=Grid['nx']*Grid['ny']*Grid['nz']
Grid['K']=np.zeros((Grid['nz'],Grid['ny'],Grid['nx'],3))
i = 0
for j in range(1,l):
cell=j*(l+1)+i
cells=DG['cells'][cell]
nodes=DG['connectivity']['cell_nodes'][cell]
edges=DG['connectivity']['cell_edges'][cell]
Grid['K'][0,:,:,:]=np.reshape(K[cells,:],(Grid['ny'],Grid['nx'],3))
for node in nodes:
bottom = range(0,Grid['nx'],1)
top = range(N-Grid['nx'],N,1)
# left = range(0,N-Grid['nx']+1,Grid['nx'])
right = range(Grid['nx']-1,N,Grid['nx'])
idxs = list(set(bottom+top+right))
vals = np.zeros(len(idxs))
basis_idxs = list()
basis_vals = list()
for edge in edges:
bases = bases1D[edge]
for basis in bases:
if node == basis[0]:
basis_idxs.extend([ii for ii,jj in
enumerate(cells) if
jj in DG['edge_cells'][edge]])
basis_vals.extend(basis[1])
for idx in basis_idxs:
if idx in idxs:
val_idx = idxs.index(idx)
vals[val_idx] = basis_vals[basis_idxs.index(idx)]
dirichlet = np.column_stack([idxs,vals])
q=np.zeros(N)
P = tpfa(Grid, Grid['K'], q, dirichlet)
bases2D[cell].append((node,P.ravel()))
# Right half cells
Grid=dict()
Grid['nx']=int((lp+1)/2); Grid['hx']=1
Grid['ny']=int(lp+1); Grid['hy']=1
Grid['nz']=1; Grid['hz']=1
N=Grid['nx']*Grid['ny']*Grid['nz']
Grid['K']=np.zeros((Grid['nz'],Grid['ny'],Grid['nx'],3))
i = l
for j in range(1,l):
cell=j*(l+1)+i
cells=DG['cells'][cell]
nodes=DG['connectivity']['cell_nodes'][cell]
edges=DG['connectivity']['cell_edges'][cell]
Grid['K'][0,:,:,:]=np.reshape(K[cells,:],(Grid['ny'],Grid['nx'],3))
for node in nodes:
bottom = range(0,Grid['nx'],1)
top = range(N-Grid['nx'],N,1)
left = range(0,N-Grid['nx']+1,Grid['nx'])
# right = range(Grid['nx']-1,N,Grid['nx'])
idxs = list(set(bottom+top+left))
vals = np.zeros(len(idxs))
basis_idxs = list()
basis_vals = list()
for edge in edges:
bases = bases1D[edge]
for basis in bases:
if node == basis[0]:
basis_idxs.extend([ii for ii,jj in
enumerate(cells) if
jj in DG['edge_cells'][edge]])
basis_vals.extend(basis[1])
for idx in basis_idxs:
if idx in idxs:
val_idx = idxs.index(idx)
vals[val_idx] = basis_vals[basis_idxs.index(idx)]
dirichlet = np.column_stack([idxs,vals])
q=np.zeros(N)
P = tpfa(Grid, Grid['K'], q, dirichlet)
bases2D[cell].append((node,P.ravel()))
# Corner quarter cells
Grid=dict()
Grid['nx']=int((lp+1)/2); Grid['hx']=1
Grid['ny']=int((lp+1)/2); Grid['hy']=1
Grid['nz']=1; Grid['hz']=1/Grid['nz']
N=Grid['nx']*Grid['ny']*Grid['nz']
Grid['K']=np.zeros((Grid['nz'],Grid['ny'],Grid['nx'],3))
i=0; j=0
cell=j*(l+1)+i
cells=DG['cells'][cell]
nodes=DG['connectivity']['cell_nodes'][cell]
edges=DG['connectivity']['cell_edges'][cell]
Grid['K'][0,:,:,:]=np.reshape(K[cells,:],(Grid['ny'],Grid['nx'],3))
for node in nodes:
basis_idxs = list()
basis_vals = list()
for edge in edges:
bases = bases1D[edge]
for basis in bases:
if node == basis[0]:
basis_idxs.extend([ii for ii,jj in
enumerate(cells) if
jj in DG['edge_cells'][edge]])
basis_vals.extend(basis[1])
dirichlet = np.column_stack([basis_idxs,basis_vals])
q=np.zeros(N)
P = tpfa(Grid, Grid['K'], q, dirichlet)
bases2D[cell].append((node,P.ravel()))
i=l; j=0
cell=j*(l+1)+i
cells=DG['cells'][cell]
nodes=DG['connectivity']['cell_nodes'][cell]
edges=DG['connectivity']['cell_edges'][cell]
Grid['K'][0,:,:,:]=np.reshape(K[cells,:],(Grid['ny'],Grid['nx'],3))
for node in nodes:
basis_idxs = list()
basis_vals = list()
for edge in edges:
bases = bases1D[edge]
for basis in bases:
if node == basis[0]:
basis_idxs.extend([ii for ii,jj in
enumerate(cells) if
jj in DG['edge_cells'][edge]])
basis_vals.extend(basis[1])
q=np.zeros(N)
dirichlet = np.column_stack([basis_idxs,basis_vals])
P = tpfa(Grid, Grid['K'], q, dirichlet)
bases2D[cell].append((node,P.ravel()))
i=0; j=l
cell=j*(l+1)+i
cells=DG['cells'][cell]
nodes=DG['connectivity']['cell_nodes'][cell]
edges=DG['connectivity']['cell_edges'][cell]
Grid['K'][0,:,:,:]=np.reshape(K[cells,:],(Grid['ny'],Grid['nx'],3))
for node in nodes:
basis_idxs = list()
basis_vals = list()
for edge in edges:
bases = bases1D[edge]
for basis in bases:
if node == basis[0]:
basis_idxs.extend([ii for ii,jj in
enumerate(cells) if
jj in DG['edge_cells'][edge]])
basis_vals.extend(basis[1])
dirichlet = np.column_stack([basis_idxs,basis_vals])
q=np.zeros(N)
P = tpfa(Grid, Grid['K'], q, dirichlet)
bases2D[cell].append((node,P.ravel()))
i=l; j=l
cell=j*(l+1)+i
cells=DG['cells'][cell]
nodes=DG['connectivity']['cell_nodes'][cell]
edges=DG['connectivity']['cell_edges'][cell]
Grid['K'][0,:,:,:]=np.reshape(K[cells,:],(Grid['ny'],Grid['nx'],3))
for node in nodes:
basis_idxs = list()
basis_vals = list()
for edge in edges:
bases = bases1D[edge]
for basis in bases:
if node == basis[0]:
basis_idxs.extend([ii for ii,jj in
enumerate(cells) if
jj in DG['edge_cells'][edge]])
basis_vals.extend(basis[1])
dirichlet = np.column_stack([basis_idxs,basis_vals])
q=np.zeros(N)
P = tpfa(Grid, Grid['K'], q, dirichlet)
bases2D[cell].append((node,P.ravel()))
return bases2D
def _patch_bases(G,CG,DG,bases2D):
N = G['nz']*G['ny']*G['nx']
N_cg = CG['nz']*CG['ny']*CG['nx']
N_dg = len(DG['cells'])
bases = [[] for k in range(N_cg)]
basis_in_pieces = [[] for k in range(N_cg)]
cells_list = [[] for k in range(N_cg)]
# gather dual cells
for i in range(N_dg):
for k,b in bases2D[i]:
basis_in_pieces[k].append(b)
cells_list[k].append(DG['cells'][i])
# patch them
for k in range(N_cg):
cells = np.concatenate(cells_list[k])
basis = np.concatenate(basis_in_pieces[k])
# eliminate boundary duplication
u = np.unique(cells, return_index=True)[1]
ij = (cells[u],np.zeros_like(cells[u]))
bases[k] = spa.csr_matrix((basis[u],ij), shape=(N,1))
return bases
def compute_bases(G,CG,DG,K,verbose=False,timer=None):
# if verbose:
# print '-Computing basis functions...'
bases1D = _solve1D(G,DG,K)
bases2D = _solve2D(G,CG,DG,K,bases1D,timer=timer)
# if verbose:
# print '-Patching basis functions...'
return _patch_bases(G,CG,DG,bases2D)