-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathganesh.py
542 lines (416 loc) · 14.1 KB
/
ganesh.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#!/usr/bin/env python
# coding: utf8
from time import time
import numpy as np
import scipy.linalg as la
import scipy.sparse as sp
import scipy.sparse.linalg as spla
import bempp.api as bem
from domains import *
meshname = "./geo/sphere-disjoint.msh"
kRef = 0.3 * np.pi
dd = [
{ 'name': '0',
'phys': 1,
'union': [-1, -2, -3],
},
{ 'name': 'A',
'phys': 2,
'union': 1,
},
{ 'name': 'B',
'phys': 3,
'union': 2,
},
{ 'name': 'C',
'phys': 4,
'union': 3,
}
]
domains = Domains(dd)
grid = bem.import_grid(meshname)
opA = bem.BlockedOperator(len(domains), len(domains))
opX = bem.BlockedOperator(len(domains), len(domains))
opI = bem.BlockedOperator(len(domains), len(domains))
kind_d = ("P", 1)
kind_n = ("P", 1)
funK = bem.operators.boundary.helmholtz.double_layer
funV = bem.operators.boundary.helmholtz.single_layer
funQ = bem.operators.boundary.helmholtz.adjoint_double_layer
funW = bem.operators.boundary.helmholtz.hypersingular
funI = bem.operators.boundary.sparse.identity
tinit = time()
print('\n=Collecting all the blocks')
for dom in domains:
me = dom['name']
ii = domains.getIndexDom(me)
print('==Domain: {0}'.format(me))
print('===Diag: Block #({0}, {0})'.format(ii))
eps = dom['phys']
k = kRef * np.sqrt(eps)
opAA = bem.BlockedOperator(2, 2)
opII = bem.BlockedOperator(2, 2)
bem_space = bem.function_space
Nints = len(dom['interfaces'])
opK = bem.BlockedOperator(Nints, Nints)
opV = bem.BlockedOperator(Nints, Nints)
opW = bem.BlockedOperator(Nints, Nints)
opQ = bem.BlockedOperator(Nints, Nints)
opId = bem.BlockedOperator(Nints, Nints)
opIn = bem.BlockedOperator(Nints, Nints)
for facei, signi in zip(dom['interfaces'], dom['signs']):
i = domains.getIndexInt(me, facei)
space_test_d = bem_space(grid, kind_d[0], kind_d[1], domains=[facei])
space_test_n = bem_space(grid, kind_n[0], kind_n[1], domains=[facei])
space_range_d = bem_space(grid, kind_d[0], kind_d[1], domains=[facei])
space_range_n = bem_space(grid, kind_n[0], kind_d[1], domains=[facei])
for facej, signj in zip(dom['interfaces'], dom['signs']):
j = domains.getIndexInt(me, facej)
print('====Interface: ({0}, {1}) #({2}, {3})'.format(facei, facej,
i, j))
space_trial_d = bem_space(grid, kind_d[0], kind_d[1], domains=[facej])
space_trial_n = bem_space(grid, kind_n[0], kind_n[1], domains=[facej])
opKK = funK(space_trial_d, space_range_d, space_test_d, k)
opVV = funV(space_trial_n, space_range_d, space_test_d, k)
opWW = funW(space_trial_d, space_range_n, space_test_n, k)
opQQ = funQ(space_trial_n, space_range_n, space_test_n, k)
opK[i, j] = signj * opKK
opV[i, j] = opVV
opW[i, j] = opWW
opQ[i, j] = signi * opQQ
opIId = funI(space_trial_d, space_range_d, space_test_d)
opIIn = funI(space_trial_n, space_range_n, space_test_n)
opId[i, j] = opIId
opIn[i, j] = opIIn
opAA[0, 0] = - opK
opAA[0, 1] = opV
opAA[1, 0] = opW
opAA[1, 1] = opQ
opII[0, 0] = opId
opII[1, 1] = opIn
opA[ii, ii] = opAA
opI[ii, ii] = opII
for d in domains.getNeighborOf(dom['name']):
other = d['name']
jj = domains.getIndexDom(other)
print('===Coupling {0} with {1}: Block #({2}, {3})'.format(other,
domains.getName(jj),
ii, jj))
NintsMe, NintsOther = len(dom['interfaces']), len(d['interfaces'])
opXXd = bem.BlockedOperator(NintsMe, NintsOther)
opXXn = bem.BlockedOperator(NintsMe, NintsOther)
for facei, signi in zip(dom['interfaces'], dom['signs']):
i = domains.getIndexInt(me, facei)
space_test_d = bem_space(grid, kind_d[0], kind_d[1], domains=[facei])
space_test_n = bem_space(grid, kind_n[0], kind_n[1], domains=[facei])
space_range_d = bem_space(grid, kind_d[0], kind_d[1], domains=[facei])
space_range_n = bem_space(grid, kind_n[0], kind_d[1], domains=[facei])
for facej, signj in zip(d['interfaces'], d['signs']):
j = domains.getIndexInt(other, facej)
print('====Interface: ({0}, {1}) #({2}, {3})'.format(facei, facej,
i, j))
space_trial_d = bem_space(grid, kind_d[0], kind_d[1], domains=[facej])
space_trial_n = bem_space(grid, kind_n[0], kind_n[1], domains=[facej])
opXd = funI(space_trial_d, space_range_d, space_test_d)
opXn = funI(space_trial_n, space_range_n, space_test_n)
opXXd[i, j] = opXd
opXXn[i, j] = -opXn
opXX = bem.BlockedOperator(2, 2)
opXX[0, 0] = opXXd
opXX[1, 1] = opXXn
opX[ii, jj] = opXX
#
##done
print('\n=RHS')
def evalIncDirichletTrace(x, normal, dom_ind, result):
result[0] = -np.exp( 1j * kRef * x[1])
def evalIncNeumannTrace(x, normal, dom_ind, result):
result[0] = -1j * normal[1] * kRef * np.exp( 1j * kRef * x[1])
def evalZeros(point, normal, dom_ind, result):
result[0] = 0. + 1j * 0.
inf = '0'
rhss = [] * len(domains)
rhs = [] * len(domains)
neighbors = domains.getNeighborOf(inf)
for ii in range(len(domains)):
name = domains.getName(ii)
dom = domains.getEntry(name)
jj = domains.getIndexDom(dom['name'])
print('==Domain: {0} #{1}'.format(dom['name'], ii))
space_d = bem.function_space(grid, kind_d[0], kind_d[1], domains=dom['interfaces'])
space_n = bem.function_space(grid, kind_n[0], kind_n[1], domains=dom['interfaces'])
if dom['name'] == inf:
IncDirichletTrace = bem.GridFunction(space_d, fun=evalIncDirichletTrace)
IncNeumannTrace = bem.GridFunction(space_n, fun=evalIncNeumannTrace)
idir, ineu = IncDirichletTrace, - IncNeumannTrace
elif dom in neighbors:
IncDirichletTrace = bem.GridFunction(space_d, fun=evalIncDirichletTrace)
IncNeumannTrace = bem.GridFunction(space_n, fun=evalIncNeumannTrace)
idir, ineu = - IncDirichletTrace, - IncNeumannTrace
else:
IncDirichletTrace = bem.GridFunction(space_d, fun=evalZeros)
IncNeumannTrace = bem.GridFunction(space_n, fun=evalZeros)
idir, ineu = IncDirichletTrace, IncNeumannTrace
rhs.append(idir)
rhs.append(ineu)
tt = time()
print('==Assembling RHS (projections)')
b = np.array([], dtype=complex)
for r in rhs:
b = np.concatenate((b, r.projections()))
trhs = time() - tt
print('#time Assembling RHS: {}'.format(trhs))
##
tAssemb = time()
print('\n=Assembling all the matrices')
print('==BlockDiag assembling: A (be patient)')
for ii in range(len(domains)):
tt = time()
print('===Block: #({0}, {0})'.format(ii), end=' ')
opp = opA[ii, ii]
for i, j, who in zip([0, 0, 1, 1], [0, 1, 0, 1], ['K', 'V', 'W', 'Q']):
print(who, end=' ', flush=True)
op = opp[i, j]
a = op.weak_form()
print(' time: {}'.format(time() - tt))
# if something is missing... to be sure !
Aw = opA.weak_form()
print('==Coupling assembling: X')
Xw = opX.weak_form()
print('==MTF assembling: M ')
#########################
opMTF = opA - 0.5 * opX
########################
MTFw = opMTF.weak_form()
bmtf = 0.5 * b
print('==Identity assembling: J')
Jw = opI.weak_form()
tAssemb = time() - tAssemb
#
print('')
print('#total time Assembling: {}'.format(tAssemb))
print('')
#
tt = time()
Jcsc = sp.lil_matrix(Jw.shape, dtype=complex)
row_start, col_start = 0, 0
row_end, col_end = 0, 0
for ii in range(len(domains)):
Jb = Jw[ii, ii]
Jd, Jn = Jb[0, 0], Jb[1, 1]
mat = bem.as_matrix(Jd)
mat = sp.lil_matrix(mat)
r, c = mat.shape
row_end += r
col_end += c
Jcsc[row_start:row_end, col_start:col_end] = mat
row_start, col_start = row_end, col_end
mat = bem.as_matrix(Jn)
mat = sp.lil_matrix(mat)
r, c = mat.shape
row_end += r
col_end += c
Jcsc[row_start:row_end, col_start:col_end] = mat
row_start, col_start = row_end, col_end
Jcsc = Jcsc.tocsc()
print('##time convert Identity to CSC: {}'.format(time() - tt))
tt = time()
iJlu = spla.splu(Jcsc)
print('##time sparse J=LU: {}'.format(time() - tt))
tt = time()
Ecsc = sp.lil_matrix(Xw.shape, dtype=complex)
row_start, row_end = 0, 0
for r in range(len(domains)):
row, col = 0, 0
col_start, col_end = 0, 0
first = True
for c in range(len(domains)):
if first:
op = opI[r, r]
row, _ = op.weak_form().shape
row_end += row
first = False
op = opX[r, c]
if not op is None:
mat = bem.as_matrix(op.weak_form())
mat = sp.lil_matrix(mat)
_ , col = mat.shape
else:
opp = opI[c, c]
_ , col = opp.weak_form().shape
col_end += col
if c > r and (op is not None):
Ecsc[row_start:row_end, col_start:col_end] = mat
col_start = col_end
row_start = row_end
Ecsc = Ecsc.tocsc()
print('##time to build E: {}'.format(time() - tt))
tpro = time() - tinit
#
print('')
print('#total time all processing: {}'.format(tpro))
print('')
#
print('=Size: {}'.format(MTFw.shape))
#
print('')
print(' ## All assembling done ! ##')
print(" == LET'S GO !! ==")
print('')
#
#################################################
#################################################
#################################################
norm = np.linalg.norm
shape = MTFw.shape
MTF = spla.LinearOperator(shape, matvec=MTFw.matvec, dtype=complex)
J = spla.LinearOperator(shape, matvec=Jw.matvec, dtype=complex)
iJ = spla.LinearOperator(shape, matvec=iJlu.solve, dtype=complex)
At = spla.LinearOperator(shape, matvec=Aw.matvec, dtype=complex)
X = spla.LinearOperator(shape, matvec=Xw.matvec, dtype=complex)
E = spla.LinearOperator(shape, matvec=Ecsc.dot, dtype=complex)
#####################################
#####################################
A = 2.0 * At
M = A - X
#####################################
#####################################
#################################################
#################################################
#################################################
from krylov import gmres
#################################################
#################################################
#################################################
iA = iJ * A * iJ
Pjac = iA
Pgs = iA + iA * E * iA
Msigma = lambda sigma: (A - J) + sigma * (J - X)
#################################################
#################################################
#################################################
tol = 1e-6
res = []
restart = 20
if restart is None: scale = 1
else: scale = restart
maxiter = int((M.shape[0] / scale) * 0.1)
if maxiter < 50: maxiter = 50
norm_b = la.norm(b)
#################################################
#################################################
#################################################
def rescaleRes(res, P, b):
scale = 1.0 / la.norm(P(b))
new_res = scale * res
return new_res
#################################################
print('\nWO restart={0} maxiter={1}'.format(restart, maxiter))
del res
res = []
tt = time()
xx, info = gmres(M, b,
orthog='mgs',
tol=tol,
residuals=res,
restrt=restart,
maxiter=maxiter)
tt = time() - tt
print(info, len(res))
oResWO = np.array(res)
ResWO = rescaleRes(oResWO, lambda x: x, b)
print('#time: {}'.format(tt))
#sol = xx
print('=Error-Calderon WO')
y = A(xx)
z = J(xx)
e = la.norm(y - z)
ee = la.norm(xx)
print(e, ee, norm_b)
print('\nJac restart={0} maxiter={1}'.format(restart, maxiter))
del res
res = []
tt = time()
xx, info = gmres(M, b,
M = Pjac,
orthog='mgs',
tol=tol,
residuals=res,
restrt=restart,
maxiter=maxiter)
tt = time() - tt
print(info, len(res))
oResJac = np.array(res)
ResJac = rescaleRes(oResJac, Pjac, b)
print('#time: {}'.format(tt))
print('=Error-Calderon Jacobi')
y = A(xx)
z = J(xx)
e = la.norm(y - z)
ee = la.norm(xx)
print(e, ee, norm_b)
print('\nGS restart={0} maxiter={1}'.format(restart, maxiter))
del res
res = []
tt = time()
xx, info = gmres(M, b,
M = Pgs,
orthog='mgs',
tol=tol,
residuals=res,
restrt=restart,
maxiter=maxiter)
tt = time() - tt
print(info, len(res))
oResGS = np.array(res)
ResGS = rescaleRes(oResGS, Pgs, b)
print('#time: {}'.format(tt))
sol = xx
print('=Error-Calderon GS')
y = A(xx)
z = J(xx)
e = la.norm(y - z)
ee = la.norm(xx)
print(e, ee, norm_b)
sigma = -0.5
print('\nSigmaWO restart={0} maxiter={1}'.format(restart, maxiter))
del res
res = []
Ms, bs = Msigma(sigma), sigma * b
tt = time()
xx, info = gmres(Ms, bs,
orthog='mgs',
tol=tol,
residuals=res,
restrt=restart,
maxiter=maxiter)
tt = time() - tt
print(info, len(res))
oResSigWO = np.array(res)
ResSigJWO = rescaleRes(oResSigWO, lambda x: x, bs)
print('#time: {}'.format(tt))
print('=Error-Calderon Sigma WO')
y = A(xx)
z = J(xx)
e = la.norm(y - z)
ee = la.norm(xx)
print(e, ee, norm_b)
####################################
####################################
import matplotlib.pyplot as plt
def Res2Tuple(res):
return np.arange(len(res)), res
its, res = Res2Tuple(ResWO)
plt.semilogy(its, res, 'k-', linewidth=3, label='wo')
its, res = Res2Tuple(ResJac)
plt.semilogy(its, res, 'b-', linewidth=3, label='Jacobi')
its, res = Res2Tuple(ResGS)
plt.semilogy(its, res, 'r-', linewidth=3, label='approx. Gauss-Siedel')
its, res = Res2Tuple(ResSigJWO)
plt.semilogy(its, res, 'g-', linewidth=3, label='Sigma')
plt.title('Convergence History', fontsize=20)
plt.xlabel('#iterations', fontsize=14)
plt.ylabel('normalized residual', fontsize=30)
plt.legend()
plt.grid(True)
plt.show()