def main(N, family, bci, bcj, plotting=False): global fe, ue BX = FunctionSpace(N, family=family, bc=bcx[bci], domain=xdomain) BY = FunctionSpace(N, family=family, bc=bcy[bcj], domain=ydomain) T = TensorProductSpace(comm, (BX, BY)) u = TrialFunction(T) v = TestFunction(T) # Get f on quad points fj = Array(T, buffer=fe) # Compare with analytical solution ua = Array(T, buffer=ue) if T.use_fixed_gauge: mean = dx(ua, weighted=True) / inner(1, Array(T, val=1)) # Compute right hand side of Poisson equation f_hat = Function(T) f_hat = inner(v, fj, output_array=f_hat) # Get left hand side of Poisson equation A = inner(v, -div(grad(u))) u_hat = Function(T) sol = la.Solver2D(A, fixed_gauge=mean if T.use_fixed_gauge else None) u_hat = sol(f_hat, u_hat) uj = u_hat.backward() assert np.allclose(uj, ua), np.linalg.norm(uj - ua) print("Error=%2.16e" % (np.sqrt(dx((uj - ua)**2)))) if 'pytest' not in os.environ and plotting is True: import matplotlib.pyplot as plt X, Y = T.local_mesh(True) plt.contourf(X, Y, uj, 100) plt.colorbar() plt.figure() plt.contourf(X, Y, ua, 100) plt.colorbar() plt.figure() plt.contourf(X, Y, ua - uj, 100) plt.colorbar()
def main(N, family, bci): bc = bcs[bci] if bci == 0: SD = FunctionSpace(N, family=family, bc=bc, domain=domain, mean=mean[family.lower()]) else: SD = FunctionSpace(N, family=family, bc=bc, domain=domain) u = TrialFunction(SD) v = TestFunction(SD) # Get f on quad points fj = Array(SD, buffer=fe) # Compute right hand side of Poisson equation f_hat = Function(SD) f_hat = inner(v, fj, output_array=f_hat) # Get left hand side of Poisson equation A = inner(v, div(grad(u))) u_hat = Function(SD).set_boundary_dofs() if isinstance(A, list): bc_mat = extract_bc_matrices([A]) A = A[0] f_hat -= bc_mat[0].matvec(u_hat, Function(SD)) u_hat = A.solve(f_hat, u_hat) uj = u_hat.backward() uh = uj.forward() # Compare with analytical solution ua = Array(SD, buffer=ue) assert np.allclose(uj, ua), np.linalg.norm(uj - ua) if 'pytest' not in os.environ: print("Error=%2.16e" % (np.sqrt(dx((uj - ua)**2)))) import matplotlib.pyplot as plt plt.plot(SD.mesh(), uj, 'b', SD.mesh(), ua, 'r')
# Size of discretization N = int(sys.argv[-2]) SD = FunctionSpace(N, family=family, bc='NeumannDirichlet', domain=domain) u = TrialFunction(SD) v = TestFunction(SD) # Get f on quad points fj = Array(SD, buffer=fe) # Compute right hand side of Poisson equation f_hat = Function(SD) f_hat = inner(v, fj, output_array=f_hat) # Get left hand side of Poisson equation A = inner(v, div(grad(u))) u_hat = Function(SD) u_hat = A.solve(f_hat, u_hat) uj = u_hat.backward() uh = uj.forward() # Compare with analytical solution ua = Array(SD, buffer=ue) print("Error=%2.16e" % (np.sqrt(dx((uj - ua)**2)))) assert np.allclose(uj, ua) if 'pytest' not in os.environ: import matplotlib.pyplot as plt plt.plot(SD.mesh(), uj, 'b', SD.mesh(), ua, 'r') plt.show()
# Get f on quad points fj = Array(T, buffer=fe) # Compute right hand side f_hat = Function(T) f_hat = inner(v, fj, output_array=f_hat) # Solve Poisson equation A = inner(v, div(grad(u))) f_hat = A.solve(f_hat) uq = T.backward(f_hat) uj = Array(T, buffer=ue) print(np.sqrt(dx((uj - uq)**2))) assert np.allclose(uj, uq) print(f_hat.commsizes, fj.commsizes) if 'pytest' not in os.environ and comm.Get_size() == 1: import matplotlib.pyplot as plt plt.figure() plt.contourf(X[0][:, :, 0, 0], X[1][:, :, 0, 0], uq[:, :, 0, 0]) plt.colorbar() plt.figure() plt.contourf(X[0][:, :, 0, 0], X[1][:, :, 0, 0], uj[:, :, 0, 0]) plt.colorbar() plt.figure()
# Compute right hand side of Poisson equation f_hat = Function(T) f_hat = inner(v, -fj, output_array=f_hat) # Get left hand side of Poisson equation matrices = inner(grad(v), grad(u)) # Solve and transform to real space u_hat = Function(T) # Solution spectral space sol = la.SolverGeneric1ND(matrices) u_hat = sol(f_hat, u_hat) uq = u_hat.backward() # Compare with analytical solution uj = Array(T, buffer=ue) assert np.sqrt(dx((uj - uq)**2)) < 1e-5 if 'pytest' not in os.environ: import matplotlib.pyplot as plt plt.figure() X = T.local_mesh(True) plt.contourf(X[0], X[1], uq) plt.colorbar() plt.figure() plt.contourf(X[0], X[1], uj) plt.colorbar() plt.figure() plt.contourf(X[0], X[1], uq - uj) plt.colorbar() plt.title('Error')
# Get left hand side of Poisson equation matrices = inner(v, div(grad(u))) # Create Helmholtz linear algebra solver H = Solver(*matrices) # Solve and transform to real space u_hat = Function(T) # Solution spectral space t0 = time.time() u_hat = H(u_hat, f_hat) # Solve uq = u_hat.backward() # Compare with analytical solution uj = Array(T, buffer=ue) error = comm.reduce(dx((uj - uq)**2)) if comm.Get_rank() == 0 and regtest is True: print("Error=%2.16e" % (np.sqrt(error))) assert np.allclose(uj, uq) if 'pytest' not in os.environ: import matplotlib.pyplot as plt plt.figure() X = T.local_mesh() plt.contourf(X[2][0, 0, :], X[0][:, 0, 0], uq[:, 2, :]) plt.colorbar() plt.figure() plt.contourf(X[2][0, 0, :], X[0][:, 0, 0], uj[:, 2, :]) plt.colorbar()
# Size of discretization N = int(sys.argv[-2]) SD = FunctionSpace(N, family=family, bc=(0, 0)) u = TrialFunction(SD) v = TestFunction(SD) # Get f on quad points fj = Array(SD, buffer=fe) # Compute right hand side of Poisson equation f_hat = Array(SD) f_hat = inner(v, fj, output_array=f_hat) # Get left hand side of Poisson equation A = inner(v, -div(grad(u))) B = inner(v, alfa * u) H = Solver(A, B) u_hat = Function(SD) # Solution spectral space u_hat = H(u_hat, f_hat) uj = SD.backward(u_hat) # Compare with analytical solution ua = Array(SD, buffer=ue) error = dx((uj - ua)**2) print('Error=%2.6e' % (np.sqrt(error))) assert np.allclose(uj, ua)