def rhs(spaces, funs): spaced, spacen = spaces triad, testd = spaced trian, testn = spacen fd, fn = funs gfd = bem.GridFunction(testd, fun=fd) gfn = bem.GridFunction(testn, fun=fn) pd, pn = gfd.projections(), gfn.projections() b = pd for p in [pn, pd, pn]: b = np.concatenate((b, -p)) return b
def perturbate(grid, t, kappa_pert=None): P1 = bem.function_space(grid, 'P', 1) grid_funz = bem.GridFunction(P1, fun=Phiz) elements = grid.leaf_view.elements vertices = grid.leaf_view.vertices normals = P1.global_dof_normals x, y, z = vertices vertices[2, :] = z + t * grid_funz.coefficients return bem.grid_from_element_data(vertices, elements)
def rhs(m, l, space, rand=True): print('Computing rhs', m, l) if rand: VVhat = np.random.rand(m, l) + 1j * np.random.rand(m, l) else: VVhat = np.eye(m) VVhat = Vhat[:, 0:l] Vhat = np.zeros((m, 1), dtype=np.complex) for v in VVhat.T: if m % 2 == 0: half = int(m/2) else: raise ValueError('Odd shape: WEIRD !! is possible ?') v1, v2 = v[:half], v[half:] gfun1 = bem.GridFunction(space, coefficients=v1) gfun2 = bem.GridFunction(space, coefficients=v2) u = np.concatenate((gfun1.projections(), gfun2.projections()), axis=0) Vhat = np.concatenate((Vhat, u.reshape((m, 1))), axis=1) Vhat = Vhat[:, 1:] return Vhat
def gamma_1_U_inc(x, n, domain_index, result): result[0] = 1j * kappa * np.exp(1j * kappa * x[0]) * n[0] grid = bem.shapes.sphere(h=h) space = bem.function_space(grid, "P", 1) # Can be applied to any function space # Assembly of the hypersingular W = bem.operators.boundary.helmholtz.hypersingular(space, space, space, kappa) Wop = W.weak_form() # We work with the total field U neumann_data = bem.GridFunction(space, fun=gamma_1_U_inc) lhs = Wop rhs_0 = neumann_data.projections() dirichlet_coeff_U, info = scipy.sparse.linalg.gmres(lhs, rhs_0, tol=1E-5) # Convert into bempp grid dirichlet_U = bem.GridFunction(space, coefficients=dirichlet_coeff_U) dirichlet_U.plot() sol = Helmholtz_Exact_Solution(kappa) dirichlet_U_exact_fun = sol.uExactDirichletTrace dirichlet_U_exact = bem.GridFunction(space, fun=sol.uExactBoundaryDirichletTrace)
x_stf = solve(stf, rhs_stf) xd_stf, xn_stf = x_stf[0:shape], x_stf[shape:] x_mtf = solve(mtf, rhs_mtf) xd_mtf, xn_mtf = x_mtf[0:shape], x_mtf[shape:2 * shape] yd_mtf, yn_mtf = x_mtf[2 * shape:3 * shape], x_mtf[3 * shape:4 * shape] print('') print('l2 norm (relative)') print(la.norm(xd_mtf - xd_stf), la.norm(xn_mtf - xn_stf)) print( la.norm(xd_mtf - yd_mtf - fd) / la.norm(xd_mtf), la.norm(-xn_mtf - yn_mtf - fn) / la.norm(xn_mtf)) gd_mtf = bem.GridFunction(space, coefficients=xd_mtf) gn_mtf = bem.GridFunction(space, coefficients=xn_mtf) ggd_mtf = bem.GridFunction(space, coefficients=yd_mtf) ggn_mtf = bem.GridFunction(space, coefficients=yn_mtf) gd_stf = bem.GridFunction(space, coefficients=xd_stf) gn_stf = bem.GridFunction(space, coefficients=xn_stf) print('L2 norm') print((gd_mtf - gd_stf).l2_norm(), (gn_mtf - gn_stf).l2_norm()) print((gd_mtf - ggd_mtf - fdir).l2_norm(), (-gn_mtf - ggn_mtf - fneu).l2_norm()) # plt.figure(1) # plt.plot(Psi.real, Psi.imag, 'b.-', label='contour') # plt.xlabel('real') # plt.ylabel('imag')
###### corr = np.zeros((N, N), dtype=np.complex128) for ii in range(5): for jj in range(5): def fun(point, n, domain_index, result): x, y, z = point res = 0j if z == 0.5 and (x <= 0.5) and (y <= 0.5): res += function(x, y, ii, jj) result[0] = res Cx = np.array([bem.GridFunction(space, fun=fun).coefficients]) corr += 1. / 3 * np.kron(Cx, Cx.T) refvar = corr.diagonal() ####### from login import assemble_singular_part from ilu import InverseSparseDiscreteBoundaryOperator 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)
checker('Transmission Prec', J, X, xx, b) ################################################# slices = mtf.getSlices() s = slices['0'] sol = xx[s[0]:s[1]] d = mtf.domains.getIndexDom('0') (space, _), (_, _) = mtf.spaces[d] n, = sol.shape n = int(n / 2) sold, soln = sol[:n], sol[n:] gsold = bem.GridFunction(space, coefficients=sold) gsoln = bem.GridFunction(space, coefficients=soln) bem.export(grid_function=gsold, file_name="soldR.msh", transformation=np.real) bem.export(grid_function=gsold, file_name="soldI.msh", transformation=np.imag) print('solution saved.') # Nx = 200 # Ny = 200 # xmin, xmax, ymin, ymax = [-0.5, 4.5, -0.5, 1.5] # plot_grid = np.mgrid[xmin:xmax:Nx * 1j, ymin:ymax:Ny * 1j] # points = np.vstack((plot_grid[0].ravel(), # plot_grid[1].ravel(), # np.zeros(plot_grid[0].size))) # u = np.zeros(points.shape[1], dtype=np.complex)
result[0] = val def dnuinc(point, normal, dom_ind, result): result[0] = 1j * kRef * normal[1] * np.exp(1j * kRef * point[1]) # grid = bem.import_grid(meshname) # space = bem.function_space(grid, "P", 1) d = mtf.domains.getIndexDom('0') (space, _), (_, _) = mtf.spaces[d] n, = sol.shape n = int(n / 2) sold, soln = sol[:n], sol[n:] gsold = bem.GridFunction(space, coefficients=sold) gmie = bem.GridFunction(space, fun=mieD) miecoeffs = gmie.coefficients errd = sold - miecoeffs aerrd = np.abs(errd) gerrd = bem.GridFunction(space, coefficients=errd) gaerrd = bem.GridFunction(space, coefficients=aerrd) print(la.norm(errd), la.norm(aerrd), la.norm(errd) / la.norm(miecoeffs)) print(gerrd.l2_norm(), gaerrd.l2_norm(), gerrd.l2_norm() / gmie.l2_norm()) print(' ') fmie = bem.GridFunction(space, fun=mieN) dnmiecoeffs = fmie.coefficients
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)
G = Gw.sparse_operator iGlu = spla.splu(G) Gt = G.transpose() iGtlu = spla.splu(Gt) iG = spla.LinearOperator(iGlu.shape, matvec=iGlu.solve) iGt = spla.LinearOperator(iGtlu.shape, matvec=iGtlu.solve) print('Assembling W...') tt = time() W = opW.weak_form() tt_W = time() - tt Dof[i, 4], Tps[i, 4] = W.shape[0], tt_W Dof[i, 5], Tps[i, 5] = W.shape[0], tt_W gf0 = bem.GridFunction(P0, fun=fdata) f0 = gf0.projections() gf = bem.GridFunction(P0d, fun=fdata) f = gf.projections() print('Solving V00...') res_V0, ts_V0, x_V0 = solve(V00, f0) Its[i, 0], STps[i, 0] = len(res_V0), ts_V0 print('Solving iJV0...') res_iJV0, ts_iJV0, x_iJV0 = solve(V00, f0, Ml=lambda x: iJ00(x)) Its[i, 1], STps[i, 1] = len(res_iJV0), ts_iJV0
print(bem.global_parameters.assembly.boundary_operator_assembly_type) m = met.shape[0] # Vhat = np.random.rand(m, l) + 1j * np.random.rand(m, l) Vhat = np.eye(m) VVhat = Vhat[:, 0:l] Vhat = np.zeros((m, 1), dtype=np.complex) space = xtf.space for v in VVhat.T: if m % 2 == 0: half = int(m / 2) else: raise ValueError('Odd shape: WEIRD !! is possible ?') v1, v2 = v[:half], v[half:] gfun1 = bem.GridFunction(space, coefficients=v1) gfun2 = bem.GridFunction(space, coefficients=v2) u = np.concatenate((gfun1.projections(), gfun2.projections()), axis=0) Vhat = np.concatenate((Vhat, u.reshape((m, 1))), axis=1) Vhat = Vhat[:, 1:] AN = np.zeros((m, l), dtype=np.complex) BN = np.zeros((m, l), dtype=np.complex) An = np.zeros((m, l), dtype=np.complex) Bn = np.zeros((m, l), dtype=np.complex) count, tt = 1, time() for tj in t[0:-1]: print('#', count, '/', N) count += 1
print('') stf = STF(xtf) M_stf = stf.get() b_stf = stf.rhs() print(M_stf.shape) zz_stf, niter_stf, res_stf, tt_stf = my_gmres( "\nGmres restart={0} maxiter={1}", M_stf, b_stf, tol, restart, maxiter) n, = zz_stf.shape n = int(n / 2) sold_stf, soln_stf = zz_stf[:n], zz_stf[n:] gui = bem.GridFunction(stf.space, fun=uinc) uicoeffs = gui.coefficients fui = bem.GridFunction(stf.space, fun=dnuinc) dnuicoeffs = fui.coefficients # There is a sign somewhere which is opposite. # in the Mie series ? soldi = sold_stf + uicoeffs solni = -soln_stf + dnuicoeffs sol0 = np.concatenate((sold_stf, soln_stf)) sol1 = np.concatenate((soldi, solni)) xx_stf = np.concatenate((sol0, sol1)) ecald_stf = checker('Calderon ', A, J, xx_stf) etrans_stf = checker('Transmission ', J, X, xx_stf, b)
import scipy from scipy.special import sph_harm, sph_jn, sph_yn, lpn p = 1 kappa = 1 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()
def setRHS(self, dir_data, neu_data): self.fdir = bem.GridFunction(self.space, fun=dir_data) self.fneu = bem.GridFunction(self.space, fun=neu_data)
def fdat(point, normal, dom_ind, result): x, y, z = point val = x**2 result[0] = val def gfun(point): x, y, z = point val = y**2 return y def gdat(point, normal, dom_ind, result): val = gfun(point) result[0] = val tt = time.time() f = bem.GridFunction(space, fun=fdat) tf = time.time() - tt print('time gf:', tf) print('', flush=True) tt = time.time() g = bem.GridFunction(space, fun=gdat) tg = time.time() - tt print('time gf:', tg) print('', flush=True) if tf < tg: print('double call slower') else: print('weird?!') print('', flush=True)
grid = bem.shapes.sphere(h=h) space = bem.function_space(grid, "P", 2) # Can be applied to any function space # Assembly of operators V = bem.operators.boundary.helmholtz.single_layer(space, space, space, kappa) Vop = V.weak_form() ID = bem.operators.boundary.sparse.identity(space, space, space) K = bem.operators.boundary.helmholtz.double_layer(space, space, space, kappa) KP = bem.operators.boundary.helmholtz.adjoint_double_layer( space, space, space, kappa) dirichlet_data = bem.GridFunction(space, fun=U_inc) neumann_data = bem.GridFunction(space, fun=gamma_1_U_inc) # We work with the total field U # Direct formulation lhs = Vop rhs_0 = dirichlet_data.projections() neumann_coeff_U, info = scipy.sparse.linalg.gmres(lhs, rhs_0, tol=1E-5) # Combined formulation lhs_combined = .5 * ID + KP - 1j * kappa * V lhs_combined = lhs_combined.weak_form()