def test_assemble_subblock_dense_complex_operator(self): from bempp.api import as_matrix, assemble_dense_block import numpy as np import bempp bempp.api.global_parameters.assembly.boundary_operator_assembly_type = 'dense' operator = self._complex_operator actual = as_matrix(assemble_dense_block(self._complex_operator, self._rows, self._cols, operator.domain, operator.dual_to_range)) expected = as_matrix(operator.weak_form())[self._rows[0]:self._rows[1], self._cols[0]:self._cols[1]] self.assertAlmostEqual(np.linalg.norm(actual - expected), 0)
def lu(A, b, lu_factor=None): """Simple direct solver interface. This function takes an operator and a grid function, converts the operator into a dense matrix and solves the system via LU decomposition. The result is again returned as a grid function. Parameters ---------- A : bempp.api.BoundaryOperator The left-hand side boundary operator b : bempp.api.GridFunction The right-hand side grid function lu_decomp : tuple Optionally pass the tuple (lu, piv) obtained by the scipy method scipy.linalg.lu_factor """ from bempp.api import GridFunction, as_matrix from scipy.linalg import solve, lu_solve if lu_factor is not None: vec = b.projections(A.dual_to_range) sol = lu_solve(lu_factor, vec) else: mat = as_matrix(A.weak_form()) vec = b.projections(A.dual_to_range) sol = solve(mat, vec) return GridFunction(A.domain, coefficients=sol)
def test_assemble_complete_dense_real_operator(self): from bempp.api import as_matrix, assemble_dense_block import numpy as np import bempp bempp.api.global_parameters.assembly.boundary_operator_assembly_type = 'dense' operator = self._real_operator actual = as_matrix(assemble_dense_block(self._real_operator, bempp.api.ALL, bempp.api.ALL, operator.domain, operator.dual_to_range)) expected = as_matrix(operator.weak_form()) self.assertEqual(258,actual.shape[0]) self.assertEqual(258,actual.shape[1]) self.assertAlmostEqual(np.linalg.norm(actual - expected), 0)
def test_assemble_complete_dense_complex_operator(self): """Assemble a complex dense operator.""" from bempp.api import as_matrix, assemble_dense_block import numpy as np import bempp.api bempp.api.global_parameters.assembly.boundary_operator_assembly_type = \ 'dense' operator = self._complex_operator actual = as_matrix( assemble_dense_block(self._complex_operator, bempp.api.ALL, bempp.api.ALL)) expected = as_matrix(operator.weak_form()) self.assertAlmostEqual(np.linalg.norm(actual - expected), 0)
def test_assemble_complete_dense_real_operator_non_square(self): "assemble an opperator with a non square matrix" from bempp.api import as_matrix, assemble_dense_block import numpy as np import bempp bempp.api.global_parameters.assembly.boundary_operator_assembly_type = 'dense' operator = self._real_operator_2 actual = as_matrix(assemble_dense_block(operator, bempp.api.ALL, bempp.api.ALL, operator.domain, operator.dual_to_range)) expected = as_matrix(operator.weak_form()) self.assertEqual(512, actual.shape[0]) self.assertEqual(258, actual.shape[1]) np.testing.assert_allclose(actual, expected)
def test_assemble_subblock_dense_real_operator(self): """Assemble a subblock of a dense real operator.""" from bempp.api import as_matrix, assemble_dense_block import numpy as np import bempp bempp.api.global_parameters.assembly.boundary_operator_assembly_type = \ 'dense' operator = self._real_operator actual = as_matrix( assemble_dense_block(self._real_operator, self._rows, self._cols)) expected = as_matrix(operator.weak_form())[self._rows[0]:self._rows[1], self._cols[0]:self._cols[1]] self.assertAlmostEqual(np.linalg.norm(actual - expected), 0)
def test_assemble_complete_dense_real_operator_non_square(self): "Assemble an opperator with a non square matrix" from bempp.api import as_matrix, assemble_dense_block import numpy as np import bempp bempp.api.global_parameters.assembly.boundary_operator_assembly_type = \ 'dense' operator = self._real_operator_2 actual = as_matrix( assemble_dense_block(operator, bempp.api.ALL, bempp.api.ALL)) expected = as_matrix(operator.weak_form()) self.assertEqual(512, actual.shape[0]) self.assertEqual(258, actual.shape[1]) np.testing.assert_allclose(actual, expected)
def compute_lu_factors(A): """ Precompute the LU factors of a dense operator A. This function returns a tuple of LU factors of A. This tuple can be used in the `lu_factor` attribute of the lu function so that the LU decomposition is not recomputed at each call to lu. """ from bempp.api import as_matrix from scipy.linalg import lu_factor return lu_factor(as_matrix(A.weak_form()))
def lu(A, b): """Simple direct solver interface. This function takes an operator and a grid function, converts the operator into a dense matrix and solves the system via LU decomposition. The result is again returned as a grid function. """ from numpy.linalg import solve from bempp.api import GridFunction, as_matrix mat = as_matrix(A.weak_form()) vec = b.projections(A.dual_to_range) sol = solve(mat, vec) return GridFunction(A.domain, coefficients=sol)
E = [] for kk in k: xtf = xTF(kk, n) Met = MET(xtf) met = Met.get() xtf.update(1j * kk) cMet = MET(xtf) cmet = cMet.get() print(' ') print('kk=', kk) print('shape(MET)=', met.shape) print(' ') print('converting...', end=' ', flush=True) A = bem.as_matrix(met) print('x', end=' ', flush=True) B = bem.as_matrix(cmet) print('eig...', end=' ', flush=True) w = la.eig(A, B, right=False) print('done.') E.append(1. / np.min(np.abs(w))) E = np.array(E) plt.figure(1) plt.plot(k, E) plt.grid() plt.show(block=False)
V = bem.operators.boundary.helmholtz.single_layer(space, space, space, k0) V_sing_wf = assemble_singular_part(V, distance=4) A = InverseSparseDiscreteBoundaryOperator(V_sing_wf, mu=0.01) df = bem.GridFunction(space, fun=dirichlet_fun) phi, info, res = bem.linalg.gmres(V, df, tol=tolerance, return_residuals=True, use_strong_form=False, maxiter=1000, restart=1000) Vm = bem.as_matrix(V.weak_form()) rhsp = np.array([-phi.projections()]) rhs2 = np.kron(rhsp, np.conj(rhsp.T)) * corr Vm1 = np.linalg.pinv(Vm) sigma = np.dot(np.dot(Vm1, rhs2), np.conj(Vm1).T) from scipy.sparse.linalg import LinearOperator class DenseTensorLinearOperator(LinearOperator): def __init__(self, A0, A1): self.A0 = A0 self.A1 = A1 self.n0 = self.A0.shape[0]
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
def Y(x, n, d, r): phi = Get_phi(x) theta = Get_theta(x) r[0] = sph_harm(p, p, phi, theta) #r[0] = sph_harm(p,p,theta,phi) Uinc = bem.GridFunction(sp, fun=Y) #Uinc.plot() V = bem.operators.boundary.helmholtz.single_layer(sp, sp, sp, kappa) K = bem.operators.boundary.helmholtz.double_layer(sp, sp, sp, kappa) W = bem.operators.boundary.helmholtz.hypersingular(sp, sp, sp, kappa) M = bem.operators.boundary.sparse.identity(sp, sp, sp) Vmat = bem.as_matrix(V.weak_form()) Kmat = bem.as_matrix(K.weak_form()) Wmat = bem.as_matrix(W.weak_form()) Mmat = bem.as_matrix(M.weak_form()) # Orthogonality Utemp = Uinc.projections() Err = np.vdot(Utemp, Uinc.coefficients) print abs(np.real(Err) - 1), 'Err Orthogonality' # V j_p, dj_p = sph_jn(p, kappa) y_p, dy_p = sph_yn(p, kappa)
count, tt = 1, time() for tj in t[0:-1]: print('#', count, '/', N) count += 1 print('updating...', end='\n -> ', flush=True) tup = time() xtf.update(psi(tj)) tup = time() - tup print('updated. time:', tup, end='\n', flush=True) Met = MET(xtf) met = Met.get() if bem.global_parameters.assembly.boundary_operator_assembly_type == 'dense': print('converting matrix...', end=' ', flush=True) tconv = time() mat = bem.as_matrix(met) tconv = time() - tconv print('done. time:', tconv, flush=True) print('solving...', end=' ', flush=True) tsolve = time() WWhat = la.solve(mat, Vhat) tsolve = time() - tsolve print('done. time:', tsolve, flush=True) An += phi(tj) * WWhat Bn += phi(tj) * psi(tj) * WWhat if its == True: print('its solving...', end=' ', flush=True) What = np.zeros((m, 1), dtype=np.complex) for ii in range(l): v = Vhat[:, ii]
count, tt = 1, time() for tj in t[0:-1]: print('#', count, '/', N) count += 1 print('updating...', end='\n -> ', flush=True) tup = time() xtf.update(psi(tj)) tup = time() - tup print('updated. time:', tup, end='\n', flush=True) Met = MET(xtf) met = Met.get() if bem.global_parameters.assembly.boundary_operator_assembly_type == 'dense': print('converting matrix...', end=' ', flush=True) tconv = time() mat = bem.as_matrix(met) tconv = time() - tconv print('done. time:', tconv, flush=True) print('solving...', end=' ', flush=True) tsolve = time() WWhat = la.solve(mat, Vhat) tsolve = time() - tsolve print('done. time:', tsolve, flush=True) An += phi(tj) * WWhat Bn += phi(tj) * psi(tj) * WWhat elif its == True: print('solving...', end=' ', flush=True) What = np.zeros((m, 1), dtype=np.complex) for ii in range(l): v = Vhat[:, ii]
E = [] for kk in k: xtf = xTF(kk, n) Met = MET(xtf) met = Met.get() xtf.update(1j * kk) cMet = MET(xtf) cmet = cMet.get() print(' ') print('kk=', kk) print('shape(MET)=', met.shape) print(' ') print('converting...', end=' ', flush=True) A = bem.as_matrix(met) print('x', end=' ', flush=True) B = bem.as_matrix(cmet) print('eig...', end=' ', flush=True) w = la.eig(A, B, right=False) print('done.') E.append(1./np.min(np.abs(w))) E = np.array(E) plt.figure(1) plt.plot(k, E) plt.grid() plt.show(block=False)