alpha = 1. a = -1. b = 1. x = symbols("x") ue = sin(4*np.pi*x)*(x+domain[0])*(x+domain[1]) + a*(x-domain[0])/2. + b*(domain[1] - x)/2. fe = ue.diff(x, 2) + alpha*ue.diff(x, 1) # Lambdify for faster evaluation ul = lambdify(x, ue, 'numpy') fl = lambdify(x, fe, 'numpy') # Size of discretization N = int(sys.argv[-2]) SD = Basis(N, family=family, bc=(a, b), domain=domain) X = SD.mesh() u = TrialFunction(SD) v = TestFunction(SD) # Get f on quad points fj = Array(SD, buffer=fl(X)) # Compute right hand side of Poisson equation f_hat = Function(SD) 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))
# Use sympy to compute a rhs, given an analytical solution x = Symbol("x") ue = cos(4 * x) + 1j * sin(6 * x) #ue = cos(4*x) fe = ue.diff(x, 2) # Size of discretization N = 40 dtype = {True: np.complex, False: np.float}[ue.has(1j)] ST = Basis(N, dtype=dtype, domain=(-2 * np.pi, 2 * np.pi)) u = TrialFunction(ST) v = TestFunction(ST) X = ST.mesh() # Get f on quad points and exact solution fj = Array(ST, buffer=fe) 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)
#ue = cos(4*x) fe = ue.diff(x, 2) # Lambdify for faster evaluation ul = lambdify(x, ue, 'numpy') fl = lambdify(x, fe, 'numpy') # Size of discretization N = 40 dtype = {True: np.complex, False: np.float}[ue.has(1j)] ST = Basis(N, dtype=dtype, plan=True, domain=(-2 * np.pi, 2 * np.pi)) u = TrialFunction(ST) v = TestFunction(ST) X = ST.mesh(N) # Get f on quad points and exact solution fj = Array(ST, buffer=fl(X)) uj = Array(ST, buffer=ul(X)) # 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)
shen = importlib.import_module('.'.join(('shenfun', family))) # Use sympy to compute a rhs, given an analytical solution x = symbols("x") ue = sin(np.pi * x) * (1 - x**2) fe = ue.diff(x, 2) # Lambdify for faster evaluation ul = lambdify(x, ue, 'numpy') fl = lambdify(x, fe, 'numpy') # Size of discretization N = 32 SD = Basis(N, family=family, bc='Neumann', plan=True) X = SD.mesh(N) u = TrialFunction(SD) v = TestFunction(SD) # Get f on quad points fj = Array(SD, buffer=fl(X)) # Compute right hand side of Poisson equation f_hat = Function(SD, buffer=inner(v, fj)) if family == 'legendre': f_hat *= -1. # Get left hand side of Poisson equation if family == 'chebyshev': A = inner(v, div(grad(u)))
family = 'legendre' # Use sympy to compute a rhs, given an analytical solution domain = (-1., 1.) x = symbols("x", real=True) d = 2. / (domain[1] - domain[0]) x_map = -1 + (x - domain[0]) * d ue = 1 + cos(5 * pi * (x_map + 1) / 2) fe = ue.diff(x, 2) # Size of discretization N = int(sys.argv[-1]) SD = Basis(N, family=family, bc='NeumannDirichlet', domain=domain) X = SD.mesh() 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)