def test_backward_uniform(family): T = FunctionSpace(N, family) uT = Function(T, buffer=f) ub = uT.backward(kind='uniform') xj = T.mesh(uniform=True) fj = sp.lambdify(x, f)(xj) assert np.linalg.norm(fj - ub) < 1e-8
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()
# Compute right hand side f_hat = Function(ST) f_hat = inner(v, fj, output_array=f_hat) # Solve Poisson equation A = inner(grad(v), grad(u)) u_hat = Function(ST) u_hat = A.solve(-f_hat, u_hat) uq = ST.backward(u_hat) u_hat = ST.forward(uq, u_hat, fast_transform=False) uq = ST.backward(u_hat, uq, fast_transform=False) assert np.allclose(uj, uq) point = np.array([0.1, 0.2]) p = ST.eval(point, u_hat) assert np.allclose(p, lambdify(x, ue)(point)) if 'pytest' not in os.environ: import matplotlib.pyplot as plt plt.figure() X = ST.mesh() plt.plot(X, uj.real) plt.title("U") plt.figure() plt.plot(X, (uq - uj).real) plt.title("Error") plt.show()
# Get left hand side of Poisson equation A = inner(v, div(grad(u))) B = extract_bc_matrices([A])[0] A = A[0] u_hat = Function(SD).set_boundary_dofs() f_hat -= B.matvec(u_hat, Function(SD)) u_hat = A.solve(f_hat, u_hat) # Solve and transform to real space u = np.zeros(N) # Solution real space u = SD.backward(u_hat, u) # Compare with analytical solution uj = Array(SD, buffer=ue) print(abs(uj - u).max()) assert np.allclose(uj, u) if 'pytest' not in os.environ: import matplotlib.pyplot as plt plt.figure() X = SD.mesh() plt.plot(X, u) plt.figure() plt.plot(X, uj) plt.figure() plt.plot(X, u - uj) plt.title('Error') #plt.show()