f_hat = inner(v, fj, output_array=f_hat) if family == 'legendre': f_hat *= -1. # Get left hand side of Poisson equation if family == 'chebyshev': A = inner(v, div(grad(u))) + alpha*inner(v, grad(u)) else: A = inner(grad(v), grad(u)) - alpha*inner(v, grad(u)) if family == 'chebyshev': f_hat[0] -= 0.5*np.pi*alpha*a f_hat[0] += 0.5*np.pi*alpha*b elif family == 'legendre': f_hat[0] += alpha*a f_hat[0] -= alpha*b f_hat[:-2] = A.solve(f_hat[:-2]) f_hat[-2] = a f_hat[-1] = b uj = SD.backward(f_hat) # Compare with analytical solution ua = ul(X) print("Error=%2.16e" %(np.linalg.norm(uj-ua))) assert np.allclose(uj, ua) point = np.array([0.1, 0.2]) p = SD.eval(point, f_hat) assert np.allclose(p, ul(point))
uj = Array(ST, buffer=ue) # 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() plt.plot(X, uj.real) plt.title("U") plt.figure() plt.plot(X, (uq - uj).real) plt.title("Error") plt.show()