-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemb.py
735 lines (578 loc) · 23 KB
/
assemb.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# coding: utf8
from time import time
import warnings
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 Domains
class Bridge:
def __init__(self, shape, dot, dtype):
self.shape = shape
self.pot = dot
# hack because the type-system of bem++ is weird
self.dtype = np.dtype(dtype)
def dot(self, x):
return self.pot(x)
class MultiTrace:
def __init__(self, kRef, meshname, doms,
J_is='BlockedDiscrete',
X_is='BlockedDiscrete',
use_slp=True):
if isinstance(doms, list):
domains = Domains(doms)
elif isinstance(doms, Domains):
domains = doms
else:
raise TypeError('configuration needs to be a list or a Domains class')
self._collected = False
self._assembled = False
self._A_assembled = False
self._X_assembled = False
self._J_assembled = False
self._iJ_assembled = False
if not J_is in ['BlockedDiscrete', 'CSC', 'Blocked']:
warnings.warn('{0} is not supported. Default used {1}'.format(J_is,
'BlockedDiscrete'))
J_is = 'BlockedDiscrete'
self._J_is = J_is
if not X_is in ['BlockedDiscrete', 'Blocked']:
warnings.warn('{0} is not supported. Default used {1}'.format(J_is,
'BlockedDiscrete'))
X_is = 'BlockedDiscrete'
self._X_is = X_is
self.use_slp = use_slp
print('==J_is: {0} , X_is: {1} , use_slp={2}'.format(J_is, X_is,
use_slp))
self.domains = domains
self.N = len(domains)
print('==Importing Grid/Mesh {}'.format(meshname), end=' ', flush=True)
self.grid = grid = bem.import_grid(meshname)
print('done.', flush=True)
N = self.N
self.opA = bem.BlockedOperator(N, N)
self.opX = bem.BlockedOperator(N, N)
self.opI = bem.BlockedOperator(N, N)
if np.abs(kRef) == 0.0:
kernel = 'lapl'
self.kRef = 0.0
dtype = np.float
else:
kernel = 'helm'
self.kRef = kRef #float(kRef)
dtype = np.complex
self.kernel = kernel
if not dtype is None:
if kernel == 'helm':
if dtype != np.complex:
warnings.warn('Helmholtz is complex. dtype={}'.format(np.complex))
dtype = np.complex
else:
if dtype != np.float:
warnings.warn('Unsupported dtype. Converted to {}'.format(np.float))
dtype = np.float
self.dtype = dtype
if kernel == 'helm':
funK = lambda trial, ran, test, k: bem.operators.boundary.helmholtz.double_layer(trial, ran, test, k)
funV = lambda trial, ran, test, k: bem.operators.boundary.helmholtz.single_layer(trial, ran, test, k)
funQ = lambda trial, ran, test, k: bem.operators.boundary.helmholtz.adjoint_double_layer(trial, ran, test, k)
funW = lambda trial, ran, test, k: bem.operators.boundary.helmholtz.hypersingular(trial, ran, test, k, use_slp=use_slp)
else:
funK = lambda trial, ran, test, k: bem.operators.boundary.laplace.double_layer(trial, ran, test)
funV = lambda trial, ran, test, k: bem.operators.boundary.laplace.single_layer(trial, ran, test)
funQ = lambda trial, ran, test, k: bem.operators.boundary.laplace.adjoint_double_layer(trial, ran, test)
funW = lambda trial, ran, test, k: bem.operators.boundary.laplace.hypersingular(trial, ran, test, use_slp=use_slp)
self._funK, self._funV = funK, funV
self._funW, self._funQ = funW, funQ
self._funI = bem.operators.boundary.sparse.identity
self.spaces = [ (
('test_d', 'trial_d'),
('test_n', 'trial_n')
) for d in domains ]
def collecting(self):
if self._collected:
print('Already collected')
return self.tcollect
tinit = time()
space = bem.function_space
dtype = self.dtype
grid = self.grid
kRef = self.kRef
domains = self.domains
opA = self.opA
opX = self.opX
opI = self.opI
funK = self._funK
funV = self._funV
funW = self._funW
funQ = self._funQ
funI = self._funI
nrow, ncol = 0, 0
print('\n=Collecting all the blocks')
for dom in domains:
ii = domains.getIndexDom(dom['name'])
eps, alpha, beta = dom['phys']
k = kRef * np.sqrt(eps)
sig = dom['sign']
print('==Domain: {0}'.format(dom['name']))
print('====info {0}: #{1}, eps={2}, (alpha={3}, beta={4}), sig={5}'.format(
dom['name'], ii, eps, alpha, beta, sig))
print('===Diag: Block #({0}, {0})'.format(ii))
opAA = bem.BlockedOperator(2, 2)
opII = bem.BlockedOperator(2, 2)
space_trial_d = space(grid, "P", 1, domains=dom['interfaces'])
if self.use_slp:
space_trial_n = space_trial_d
space_range_d, space_range_n = space_trial_d, space_trial_n
space_test_d, space_test_n = space_trial_d, space_trial_n
else:
space_trial_n = space(grid, "P", 1, domains=dom['interfaces'])
space_range_d = space(grid, "P", 1, domains=dom['interfaces'])
space_range_n = space(grid, "P", 1, domains=dom['interfaces'])
space_test_d = space(grid, "P", 1, domains=dom['interfaces'])
space_test_n = space(grid, "P", 1, domains=dom['interfaces'])
space_d = (space_test_d, space_trial_d)
space_n = (space_test_n, space_trial_n)
spaces = (space_d, space_n)
self.spaces[ii] = spaces
ncol += space_test_d.global_dof_count + space_test_n.global_dof_count
nrow += space_trial_d.global_dof_count + space_trial_n.global_dof_count
# the kernel type is managed in __init__
opK = funK(space_trial_d, space_range_d, space_test_d, k)
opV = funV(space_trial_n, space_range_d, space_test_d, k)
opW = funW(space_trial_d, space_range_n, space_test_n, k)
opQ = funQ(space_trial_n, space_range_n, space_test_n, k)
opId = funI(space_trial_d, space_range_d, space_test_d)
opIn = funI(space_trial_n, space_range_n, space_test_n)
opAA[0, 0] = - sig * opK
opAA[0, 1] = opV
opAA[1, 0] = opW
opAA[1, 1] = sig * opQ
opII[0, 0] = opId
opII[1, 1] = opIn
opA[ii, ii] = opAA
opI[ii, ii] = opII
for d in domains.getNeighborOf(dom['name']):
jj = domains.getIndexDom(d['name'])
print('===Coupling {0} with {1}: Block #({2}, {3})'.format(dom['name'],
domains.getName(jj),
ii, jj))
_, alph, bet = d['phys']
opXX = bem.BlockedOperator(2, 2)
space_trial_d = space(grid, "P", 1, domains=d['interfaces'])
space_trial_n = space(grid, "P", 1, domains=d['interfaces'])
space_range_d = space(grid, "P", 1, domains=d['interfaces'])
space_range_n = space(grid, "P", 1, domains=d['interfaces'])
opXd = funI(space_trial_d, space_range_d, space_test_d)
opXn = funI(space_trial_n, space_range_n, space_test_n)
print('====coeffs jumps: alpha_j/i={0:e} beta_j/i={1:e}'.format(alph/alpha,
-bet/beta))
opXX[0, 0] = (alph/alpha) * opXd
opXX[1, 1] = - (bet/beta) * opXn
opX[ii, jj] = opXX
self.opA = opA
self.opX = opX
self.opI = opI
self.shape = (nrow, ncol)
self._collected = True
self.tcollect = time() - tinit
return self.tcollect
def _check_shape(self, shape):
if hasattr(self, 'shape'):
if shape != self.shape:
raise ValueError('Inconsistent shape')
else:
self.shape = shape
def _collect(self):
if not self._collected:
self.tcollect = self.collecting()
def A_weak_form(self):
self._collect()
if self._A_assembled:
return self.Aw
tinit = time()
opA = self.opA
N = self.N
print('==BlockDiag assembling: A (be patient)')
for ii in range(N):
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 !
self.Aw = opA.weak_form()
self._check_shape(self.Aw.shape)
self._A_assembled = True
self.tassembA = time() - tinit
return self.Aw
def X_weak_form(self, X_is='Blocked'):
self._collect()
if self._X_assembled:
return self.Xw
tinit = time()
domains = self.domains
dtype = self.dtype
print('==Coupling assembling: X', end=' ')
Xw = self.opX.weak_form()
self._check_shape(Xw.shape)
if X_is == 'Blocked':
self._X_is = 'Blocked'
self.Xw = Xw
self._X_assembled = True
self.tassembX = time() - tinit
print('Blocked time: {0}'.format(time() - tinit))
return self.Xw
N = len(domains)
for dom in domains:
ii = domains.getIndexDom(dom['name'])
for d in domains.getNeighborOf(dom['name']):
jj = domains.getIndexDom(d['name'])
Xb = Xw[ii, jj]
Xd, Xn = Xb[0, 0], Xb[1, 1]
xs = Xd.sparse_operator
xs = xs.astype(dtype)
Xd = Bridge(xs.shape, dot=xs.dot, dtype=dtype)
xs = Xn.sparse_operator
xs = xs.astype(dtype)
Xn = Bridge(xs.shape, dot=xs.dot, dtype=dtype)
Xloc = bem.BlockedDiscreteOperator(2, 2)
Xloc[0, 0], Xloc[1, 1] = Xd, Xn
Xw[ii, jj] = Xloc
self.Xw = Xw
self._X_assembled = True
self._X_is = 'BlockedDiscrete'
self.tassembX = time() - tinit
print('BlockedDiscrete time: {0}'.format(time() - tinit))
return self.Xw
def J_weak_form(self, J_is='CSC'):
self._collect()
if self._J_assembled:
return self.Jw
tinit = time()
domains = self.domains
dtype = self.dtype
print('==Identity assembling: J', end=' ', flush=True)
Jw = self.opI.weak_form()
self._check_shape(Jw.shape)
if J_is == 'Blocked':
self._J_is = 'Blocked'
self.Jw = Jw
self._J_assembled = True
self.tassembJ = time() - tinit
print('Blocked time: {0}'.format(time() - tinit))
return self.Jw
if J_is == 'BlockedDiscrete':
self._J_is = 'BlockedDiscrete'
N = len(domains)
for ii in range(N):
Jb = Jw[ii, ii]
Jd, Jn = Jb[0, 0], Jb[1, 1]
js = Jd.sparse_operator
js = js.astype(dtype)
Jd = Bridge(js.shape, dot=js.dot, dtype=dtype)
js = Jn.sparse_operator
js = js.astype(dtype)
Jn = Bridge(js.shape, dot=js.dot, dtype=dtype)
Jloc = bem.BlockedDiscreteOperator(2, 2)
Jloc[0, 0], Jloc[1, 1] = Jd, Jn
Jw[ii, ii] = Jloc
self.Jw = Jw
self._J_assembled = True
self.tassembJ = time() - tinit
print('BlockedDiscrete time: {0}'.format(time()-tinit))
return self.Jw
tt = time()
Jsp = sp.lil_matrix(Jw.shape, dtype=np.float)
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 = Jd.sparse_operator.toarray()
#mat = sp.lil_matrix(mat, dtype=dtype)
r, c = mat.shape
row_end += r
col_end += c
Jsp[row_start:row_end, col_start:col_end] = mat
row_start, col_start = row_end, col_end
#mat = bem.as_matrix(Jn)
mat = Jn.sparse_operator.toarray()
#mat = sp.lil_matrix(mat, dtype=dtype)
r, c = mat.shape
row_end += r
col_end += c
Jsp[row_start:row_end, col_start:col_end] = mat
row_start, col_start = row_end, col_end
Jsp = Jsp.astype(dtype)
print('CSC time: {0}'.format(time()-tinit))
tt = time()
self.Jw = Jsp.tocsc()
self._J_is = 'CSC'
self._J_assembled = True
self.tassembJ = time() - tinit
print('##time convert Identity to {0} CSC: {1}'.format(dtype,
time()-tt))
return self.Jw
def iJ_weak_form(self):
self._collect()
if self._iJ_assembled:
return self.iJlu
tinit = time()
print('==Factorization LU: J')
if self._J_is == 'CSC':
self.iJlu = spla.splu(self.Jw)
self._iJ_assembled = True
self.tassembiJ = time() - tinit
print('##time CSC J=LU: {}'.format(self.tassembiJ))
return self.iJlu
Jw = self.opI.weak_form()
N = len(self.domains)
dtype = self.dtype
iJlu = bem.BlockedDiscreteOperator(N, N)
for ii in range(N):
Jb = Jw[ii, ii]
Jd, Jn = Jb[0, 0], Jb[1, 1]
js = Jd.sparse_operator
js = js.astype(dtype)
js = spla.splu(js)
iJd = Bridge(js.shape, dot=js.solve, dtype=dtype)
js = Jn.sparse_operator
js = js.astype(dtype)
js = spla.splu(js)
iJn = Bridge(js.shape, dot=js.solve, dtype=dtype)
iJloc = bem.BlockedDiscreteOperator(2, 2)
iJloc[0, 0], iJloc[1, 1] = iJd, iJn
iJlu[ii, ii] = iJloc
self.iJlu = iJlu
self._iJ_assembled = True
self.tassembiJ = time() - tinit
print('##time blocked-discrete J=LU: {}'.format(self.tassembiJ))
return self.iJlu
def weak_form(self):
self._collect()
tinit = time()
print('\n=Assembling all the matrices')
self.A_weak_form()
self.X_weak_form(X_is=self._X_is)
self.J_weak_form(J_is=self._J_is)
self.iJ_weak_form()
tA, tX = self.tassembA, self.tassembX
tJ, tiJ = self.tassembJ, self.tassembiJ
tassemb = time() - tinit
if tassemb < tA + tX + tJ + tiJ:
tassemb = tA + tX + tJ + tiJ
self.tassemb = tassemb
#
print('')
print('#total time Assembling: {0}'.format(tassemb))
print('')
#
return tassemb
def J_tolinop(self):
self.J_weak_form(self._J_is)
J_is = self._J_is
if J_is == 'CSC':
mv = self.Jw.dot
else:
mv = self.Jw.matvec
J = spla.LinearOperator(self.shape,
matvec=mv,
dtype=self.dtype)
return J
def iJ_tolinop(self):
self.iJ_weak_form()
J_is = self._J_is
if J_is == 'CSC':
mv = self.iJlu.solve
else:
mv = self.iJlu.matvec
iJ = spla.LinearOperator(self.shape,
matvec=mv,
dtype=self.dtype)
return iJ
def X_tolinop(self):
self.X_weak_form(self._X_is)
X = spla.LinearOperator(self.shape,
matvec=self.Xw.matvec,
dtype=self.dtype)
return X
def A_tolinop(self):
self.A_weak_form()
A = spla.LinearOperator(self.shape,
matvec=self.Aw.matvec,
dtype=self.dtype)
return A
def tolinop(self):
self.weak_form()
A, X = self.A_tolinop(), self.X_tolinop()
J, iJ = self.J_tolinop(), self.iJ_tolinop()
return A, X, J, iJ
def upper(self):
print('==building Upper: E', flush=True)
domains = self.domains
dtype = self.dtype
N = len(domains)
if self._J_is == 'CSC':
Jw = self.opI.weak_form()
else:
Jw = self.J_weak_form(self._J_is)
# Xw = self.X_weak_form(self._X_is)
Xw = self.opX.weak_form()
## not nice, because the type conversion is done 2 times
## need to fix with the Bridge
tt = time()
E = bem.BlockedDiscreteOperator(N, N)
for dom in domains:
ii = domains.getIndexDom(dom['name'])
Jb = Jw[ii, ii]
es = Bridge(Jb.shape, dot=lambda x: 0.0 * x, dtype=dtype)
E[ii, ii] = es
for d in domains.getNeighborOf(dom['name']):
jj = domains.getIndexDom(d['name'])
if jj > ii:
Xij = Xw[ii, jj]
Xd, Xn = Xij[0, 0], Xij[1, 1]
ed = Xd.sparse_operator
ed = ed.astype(dtype)
en = Xn.sparse_operator
en = en.astype(dtype)
es = bem.BlockedDiscreteOperator(2, 2)
es[0, 0], es[1, 1] = ed, en
E[ii, jj] = es
print('##time to build E: {}'.format(time() - tt), flush=True)
E = spla.LinearOperator(self.shape,
matvec=E.matvec,
dtype=self.dtype)
return E
# tt = time()
# Esp = sp.lil_matrix(self.Xw.shape, dtype=np.float)
# row_start, row_end = 0, 0
# for r in range(N):
# row, col = 0, 0
# col_start, col_end = 0, 0
# first = True
# for c in range(N):
# 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)
# # mat = op.weak_form().sparse_operator.toarray()
# _ , col = mat.shape
# else:
# opp = opI[c, c]
# _ , col = opp.weak_form().shape
# col_end += col
# if c > r and (op is not None):
# Esp[row_start:row_end, col_start:col_end] = mat
# col_start = col_end
# row_start = row_end
# print('===converting Upper: LIL to CSC', flush=True)
# Esp = Esp.astype(dtype)
# Esp = Esp.tocsc()
# print('##time to build E: {}'.format(time() - tt), flush=True)
# E = spla.LinearOperator(self.shape,
# matvec=Esp.dot,
# dtype=self.dtype)
# return E
##################################
def rhs(self, fdir, fneu, inf='0'):
print('\n=RHS')
def fzero(point, normal, dom_ind, result):
result[0] = 0. + 1j * 0.
dtype = self.dtype
grid = self.grid
N = self.N
domains = self.domains
space = bem.function_space
grid_fun = bem.GridFunction
for dom in domains:
if dom['name'] == inf:
_, alpha, beta = dom['phys']
rhs = [] * N
neighbors = domains.getNeighborOf(inf)
for ii in range(N):
name = domains.getName(ii)
dom = domains.getEntry(name)
jj = domains.getIndexDom(dom['name'])
_, alph, bet = dom['phys']
print('==Domain: {0} #{1} \t (alpha={2}, beta={3})'.format(
dom['name'], ii, alph, bet))
space_d = space(grid, "P", 1, domains=dom['interfaces'])
space_n = space(grid, "P", 1, domains=dom['interfaces'])
if dom['name'] == inf:
diri = grid_fun(space_d, fun=fdir)
neum = grid_fun(space_n, fun=fneu)
idir, ineu = diri, - neum
elif dom in neighbors:
diri = grid_fun(space_d, fun=fdir)
neum = grid_fun(space_n, fun=fneu)
a, b = alpha / alph, beta / bet
idir, ineu = - a * diri, - b * neum
else:
diri = grid_fun(space_d, fun=fzero)
neum = grid_fun(space_n, fun=fzero)
idir, ineu = diri, neum
rhs.append(idir)
rhs.append(ineu)
tt = time()
print('==Assembling RHS (projections)')
b = np.array([], dtype=dtype)
for r in rhs:
b = np.concatenate((b, r.projections()))
trhs = time() - tt
print('#time Assembling RHS: {}'.format(trhs))
return b
def getSlices(self):
if self._J_is == 'CSC':
Jw = self.opI.weak_form()
else:
Jw = self.J_weak_form(self._J_is)
domains = self.domains
slices = {}
start, end = 0, 0
for ii in range(len(domains)):
name = domains.getName(ii)
s = Jw[ii, ii].shape
if s[0] != s[1]:
print('Warning: block #{0} = ({1}, {2}) rectangular'.format(ii, s[0], s[1]))
end = start + s[1]
slices[name] = (start, end)
start = end
self.slices = slices
return slices
###########################
def checker(string, A, B, x, b=None):
if not isinstance(A, spla.LinearOperator):
raise TypeError('A has to be a LinearOperator')
if not isinstance(B, spla.LinearOperator):
raise TypeError('B has to be a LinearOperator')
if A.shape != B.shape:
raise ValueError('Inconsistent shape')
print('==check ' + string)
t0 = time()
y = A(x)
t1 = time() - t0
t0 = time()
z = B(x)
t2 = time() - t0
if b is None:
e = la.norm(y - z)
else:
e = la.norm(y - z - b)
print(e)
print('#time: {0} [{1} {2} {3}]'.format(t1 + t2, t1, t2, t1 - t2))
return e
###########################