def test_eval_fourier(typecode, dim): # Using sympy to compute an analytical solution x, y, z = symbols("x,y,z") sizes = (20, 19) funcs = { 2: cos(4 * x) + sin(6 * y), 3: sin(6 * x) + cos(4 * y) + sin(8 * z) } syms = {2: (x, y), 3: (x, y, z)} points = None if comm.Get_rank() == 0: points = np.random.random((dim, 3)) points = comm.bcast(points) t_0 = 0 t_1 = 0 t_2 = 0 for shape in product(*([sizes] * dim)): bases = [] for n in shape[:-1]: bases.append(Basis(n, 'F', dtype=typecode.upper())) bases.append(Basis(shape[-1], 'F', dtype=typecode)) fft = TensorProductSpace(comm, bases, dtype=typecode) X = fft.local_mesh(True) ue = funcs[dim] ul = lambdify(syms[dim], ue, 'numpy') uu = ul(*X).astype(typecode) uq = ul(*points).astype(typecode) u_hat = Function(fft) u_hat = fft.forward(uu, u_hat) t0 = time() result = fft.eval(points, u_hat, method=0) t_0 += time() - t0 assert allclose(uq, result), print(uq, result) t0 = time() result = fft.eval(points, u_hat, method=1) t_1 += time() - t0 assert allclose(uq, result) t0 = time() result = fft.eval(points, u_hat, method=2) t_2 += time() - t0 assert allclose(uq, result), print(uq, result) print('method=0', t_0) print('method=1', t_1) print('method=2', t_2)
# Create linear algebra solver H = SolverGeneric2NP(matrices) # Solve and transform to real space u_hat = Function(T) # Solution spectral space u_hat = H(f_hat, u_hat) # Solve uq = u_hat.backward() # Compare with analytical solution uj = ul(*X) print(abs(uj - uq).max()) assert np.allclose(uj, uq) points = np.array([[0.2, 0.3], [0.1, 0.5]]) p = T.eval(points, u_hat, method=2) assert np.allclose(p, ul(*points)) if 'pytest' not in os.environ: import matplotlib.pyplot as plt plt.figure() 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()
def test_eval_tensor(typecode, dim, ST, quad): # Using sympy to compute an analytical solution # Testing for Dirichlet and regular basis x, y, z = symbols("x,y,z") sizes = (22, 21) funcx = { '': (1 - x**2) * sin(np.pi * x), 'Dirichlet': (1 - x**2) * sin(np.pi * x), 'Neumann': (1 - x**2) * sin(np.pi * x), 'Biharmonic': (1 - x**2) * sin(2 * np.pi * x) } funcy = { '': (1 - y**2) * sin(np.pi * y), 'Dirichlet': (1 - y**2) * sin(np.pi * y), 'Neumann': (1 - y**2) * sin(np.pi * y), 'Biharmonic': (1 - y**2) * sin(2 * np.pi * y) } funcz = { '': (1 - z**2) * sin(np.pi * z), 'Dirichlet': (1 - z**2) * sin(np.pi * z), 'Neumann': (1 - z**2) * sin(np.pi * z), 'Biharmonic': (1 - z**2) * sin(2 * np.pi * z) } funcs = { (1, 0): cos(2 * y) * funcx[ST.boundary_condition()], (1, 1): cos(2 * x) * funcy[ST.boundary_condition()], (2, 0): sin(6 * z) * cos(4 * y) * funcx[ST.boundary_condition()], (2, 1): sin(2 * z) * cos(4 * x) * funcy[ST.boundary_condition()], (2, 2): sin(2 * x) * cos(4 * y) * funcz[ST.boundary_condition()] } syms = {1: (x, y), 2: (x, y, z)} points = None if comm.Get_rank() == 0: points = np.random.random((dim + 1, 4)) points = comm.bcast(points) t_0 = 0 t_1 = 0 t_2 = 0 for shape in product(*([sizes] * dim)): #for shape in ((64, 64),): bases = [] for n in shape[:-1]: bases.append(Basis(n, 'F', dtype=typecode.upper())) bases.append(Basis(shape[-1], 'F', dtype=typecode)) for axis in range(dim + 1): #for axis in (0,): ST0 = ST(shape[-1], quad=quad) bases.insert(axis, ST0) # Spectral space must be aligned in nonperiodic direction, hence axes fft = TensorProductSpace(comm, bases, dtype=typecode, axes=axes[dim][axis]) print('axes', axes[dim][axis]) print('bases', bases) #print(bases[0].axis, bases[1].axis) X = fft.local_mesh(True) ue = funcs[(dim, axis)] ul = lambdify(syms[dim], ue, 'numpy') uu = ul(*X).astype(typecode) uq = ul(*points).astype(typecode) u_hat = Function(fft) u_hat = fft.forward(uu, u_hat) t0 = time() result = fft.eval(points, u_hat, method=0) t_0 += time() - t0 assert np.allclose(uq, result, 0, 1e-6) t0 = time() result = fft.eval(points, u_hat, method=1) t_1 += time() - t0 assert np.allclose(uq, result, 0, 1e-6) t0 = time() result = fft.eval(points, u_hat, method=2) t_2 += time() - t0 print(uq) assert np.allclose(uq, result, 0, 1e-6), uq / result result = u_hat.eval(points) assert np.allclose(uq, result, 0, 1e-6) ua = u_hat.backward() assert np.allclose(uu, ua, 0, 1e-6) ua = Array(fft) ua = u_hat.backward(ua) assert np.allclose(uu, ua, 0, 1e-6) bases.pop(axis) fft.destroy() print('method=0', t_0) print('method=1', t_1) print('method=2', t_2)
uq = Array(T) uq = T.backward(u_hat, uq, fast_transform=True) uj = ul(*X) assert np.allclose(uj, uq) #from shenfun.tensorproductspace import Convolve #S0 = Basis(N[0], family='F', dtype='D', padding_factor=2.0) #S1 = Basis(N[1], family='F', dtype='d', padding_factor=2.0) #Tp = TensorProductSpace(comm, (S0, S1), axes=(0, 1)) #C0 = Convolve(Tp) #ff_hat = C0(f_hat, f_hat) # Test eval at point point = np.array([[0.1, 0.2], [0.2, 0.3], [0.3, 0.4], [0.1, 0.3]]) p = T.eval(point, u_hat) assert np.allclose(p, ul(*point.T)) p2 = u_hat.eval(point) assert np.allclose(p2, ul(*point.T)) if plt is not None and not 'pytest' in os.environ: plt.figure() 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)
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, fast_transform=True) uj = ul(*X) print(abs(uj - uq).max()) assert np.allclose(uj, uq) # Test eval at point point = np.array([[0.1, 0.2, 0.3], [0.2, 0.3, 0.3], [0.3, 0.4, 0.1]]) p = T.eval(point, f_hat) assert np.allclose(p, ul(*point.T)) p2 = f_hat.eval(point) assert np.allclose(p2, ul(*point.T)) if plt is not None and not 'pytest' in os.environ: plt.figure() plt.contourf(X[0][:, :, 0], X[1][:, :, 0], uq[:, :, 0]) plt.colorbar() plt.figure() plt.contourf(X[0][:, :, 0], X[1][:, :, 0], uj[:, :, 0]) plt.colorbar() plt.figure() plt.contourf(X[0][:, :, 0], X[1][:, :, 0], uq[:, :, 0] - uj[:, :, 0])
# Create linear algebra solver H = BiharmonicSolver(*matrices) # Solve and transform to real space u_hat = Function(T) # Solution spectral space u_hat = H(u_hat, f_hat) # Solve uq = u_hat.backward() # Compare with analytical solution uj = ul(*X) print(abs(uj - uq).max()) assert np.allclose(uj, uq) points = np.array([[0.2, 0.3], [0.1, 0.5]]) p = T.eval(points, u_hat) assert np.allclose(p, ul(*points)) if 'pytest' not in os.environ: import matplotlib.pyplot as plt plt.figure() 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()
def test_eval_tensor(typecode, dim, ST, quad): # Using sympy to compute an analytical solution # Testing for Dirichlet and regular basis x, y, z = symbols("x,y,z") sizes = (25, 24) funcx = (x**2 - 1) * cos(2 * np.pi * x) funcy = (y**2 - 1) * cos(2 * np.pi * y) funcz = (z**2 - 1) * cos(2 * np.pi * z) funcs = { (1, 0): cos(4 * y) * funcx, (1, 1): cos(4 * x) * funcy, (2, 0): (sin(6 * z) + cos(4 * y)) * funcx, (2, 1): (sin(2 * z) + cos(4 * x)) * funcy, (2, 2): (sin(2 * x) + cos(4 * y)) * funcz } syms = {1: (x, y), 2: (x, y, z)} points = np.array([[0.1] * (dim + 1), [0.01] * (dim + 1), [0.4] * (dim + 1), [0.5] * (dim + 1)]) for shape in product(*([sizes] * dim)): bases = [] for n in shape[:-1]: bases.append(Basis(n, 'F', dtype=typecode.upper())) bases.append(Basis(shape[-1], 'F', dtype=typecode)) if dim < 3: n = min(shape) if typecode in 'fdg': n //= 2 n += 1 if n < comm.size: continue for axis in range(dim + 1): ST0 = ST(shape[-1], quad=quad) bases.insert(axis, ST0) # Spectral space must be aligned in nonperiodic direction, hence axes fft = TensorProductSpace(comm, bases, dtype=typecode, axes=axes[dim][axis]) X = fft.local_mesh(True) ue = funcs[(dim, axis)] ul = lambdify(syms[dim], ue, 'numpy') uu = ul(*X).astype(typecode) uq = ul(*points.T).astype(typecode) u_hat = Function(fft) u_hat = fft.forward(uu, u_hat) result = fft.eval(points, u_hat, cython=True) assert np.allclose(uq, result, 0, 1e-6) result = fft.eval(points, u_hat, cython=False) assert np.allclose(uq, result, 0, 1e-6) result = u_hat.eval(points) assert np.allclose(uq, result, 0, 1e-6) ua = u_hat.backward() assert np.allclose(uu, ua, 0, 1e-6) ua = Array(fft) ua = u_hat.backward(ua) assert np.allclose(uu, ua, 0, 1e-6) bases.pop(axis) fft.destroy()