def test_jacobi_ncc_solve(N, a0, b0, k_ncc, k_arg, dealias, dtype): """ This tests for aliasing errors when solving the equation f(x)*u(x) = f(x)*g(x). With 3/2 dealiasing, the RHS product will generally contain aliasing errors in the last 2*max(k_ncc, k_arg) modes. We can eliminate these by zeroing the corresponding number of modes of f(x) and/or g(x). """ c = coords.Coordinate('x') d = distributor.Distributor((c,)) b = basis.Jacobi(c, size=N, a=a0, b=b0, bounds=(0, 1), dealias=dealias) b_ncc = b.clone_with(a=a0+k_ncc, b=b0+k_ncc) b_arg = b.clone_with(a=a0+k_arg, b=b0+k_arg) f = field.Field(dist=d, bases=(b_ncc,), dtype=dtype) g = field.Field(dist=d, bases=(b_arg,), dtype=dtype) u = field.Field(dist=d, bases=(b_arg,), dtype=dtype) f['g'] = np.random.randn(*f['g'].shape) g['g'] = np.random.randn(*g['g'].shape) kmax = max(k_ncc, k_arg) if kmax > 0 and dealias < 2: f['c'][-kmax:] = 0 g['c'][-kmax:] = 0 problem = problems.LBVP([u]) problem.add_equation((f*u, f*g)) solver = solvers.LinearBoundaryValueSolver(problem) solver.solve() assert np.allclose(u['c'], g['c'])
def test_jacobi_differentiate(N, a, b, k, dtype): c = coords.Coordinate('x') d = distributor.Distributor((c,)) b = basis.Jacobi(c, size=N, a0=a, b0=b, a=a+k, b=b+k, bounds=(0, 1)) x = b.local_grid(1) f = field.Field(dist=d, bases=(b,), dtype=dtype) f['g'] = x**5 fx = operators.Differentiate(f, c).evaluate() assert np.allclose(fx['g'], 5*x**4)
def test_jacobi_convert_constant(N, a, b, k, dtype, layout): c = coords.Coordinate('x') d = distributor.Distributor((c,)) b = basis.Jacobi(c, size=N, a0=a, b0=b, a=a+k, b=b+k, bounds=(0, 1)) fc = field.Field(dist=d, dtype=dtype) fc['g'] = 1 fc[layout] f = operators.convert(fc, (b,)).evaluate() assert np.allclose(fc['g'], f['g'])
def test_jacobi_convert_explicit(N, a, b, k, dtype, layout): c = coords.Coordinate('x') d = distributor.Distributor((c,)) b = basis.Jacobi(c, size=N, a0=a, b0=b, a=a+k, b=b+k, bounds=(0, 1)) x = b.local_grid(1) f = field.Field(dist=d, bases=(b,), dtype=dtype) f['g'] = x**5 fx = operators.Differentiate(f, c) f[layout] g = operators.convert(f, fx.domain.bases).evaluate() assert np.allclose(g['g'], f['g'])
def test_jacobi_interpolate(N, a, b, k, dtype): c = coords.Coordinate('x') d = distributor.Distributor((c,)) b = basis.Jacobi(c, size=N, a0=a, b0=b, a=a+k, b=b+k, bounds=(0, 1)) x = b.local_grid(1) f = field.Field(dist=d, bases=(b,), dtype=dtype) f['g'] = x**5 results = [] for p in [0, 1, np.random.rand()]: fp = operators.Interpolate(f, c, p).evaluate() results.append(np.allclose(fp['g'], p**5)) assert all(results)
def test_jacobi_ufunc(N, a, b, dtype, dealias, func): c = coords.Coordinate('x') d = distributor.Distributor((c, )) b = basis.Jacobi(c, size=N, a=a, b=b, bounds=(0, 1), dealias=dealias) x = b.local_grid(1) f = field.Field(dist=d, bases=(b, ), dtype=dtype) if func is np.arccosh: f['g'] = 1 + x**2 else: f['g'] = x**2 g0 = func(f['g']) g = func(f).evaluate() assert np.allclose(g['g'], g0)
def test_J_scalar_roundtrip(a, b, N, dealias, dtype): if comm.size == 1: c = coords.Coordinate('x') d = distributor.Distributor([c]) xb = basis.Jacobi(c, a=a, b=b, size=N, bounds=(0, 1), dealias=dealias) x = xb.local_grid(dealias) # Scalar transforms u = field.Field(dist=d, bases=(xb, ), dtype=dtype) u.preset_scales(dealias) u['g'] = ug = 2 * x**2 - 1 u['c'] assert np.allclose(u['g'], ug) else: pytest.skip("Can only test 1D transform in serial")
def test_jacobi_convert_implicit(N, a, b, k, dtype): c = coords.Coordinate('x') d = distributor.Distributor((c,)) b = basis.Jacobi(c, size=N, a0=a, b0=b, a=a+k, b=b+k, bounds=(0, 1)) x = b.local_grid(1) f = field.Field(dist=d, bases=(b,), dtype=dtype) f['g'] = x**5 fx = operators.Differentiate(f, c).evaluate() g = field.Field(dist=d, bases=(b,), dtype=dtype) problem = problems.LBVP([g]) problem.add_equation((g, fx)) solver = solvers.LinearBoundaryValueSolver(problem) solver.solve() assert np.allclose(g['g'], fx['g'])
def build_CF_J(a, b, Nx, Ny, dealias_x, dealias_y): c = coords.CartesianCoordinates('x', 'y') d = distributor.Distributor((c, )) xb = basis.ComplexFourier(c.coords[0], size=Nx, bounds=(0, np.pi), dealias=dealias_x) yb = basis.Jacobi(c.coords[1], a=a, b=b, size=Ny, bounds=(0, 1), dealias=dealias_y) x = xb.local_grid(dealias_x) y = yb.local_grid(dealias_y) return c, d, xb, yb, x, y
def test_jacobi_ncc_eval(N, a0, b0, k_ncc, k_arg, dealias, dtype): """ This tests for aliasing errors when evaluating the product f(x) * g(x) as both an NCC operator and with the pseudospectral method. With 3/2 dealiasing, the product will generally contain aliasing errors in the last 2*max(k_ncc, k_arg) modes. We can eliminate these by zeroing the corresponding number of modes of f(x) and/or g(x). """ c = coords.Coordinate('x') d = distributor.Distributor((c,)) b = basis.Jacobi(c, size=N, a=a0, b=b0, bounds=(0, 1), dealias=dealias) b_ncc = b.clone_with(a=a0+k_ncc, b=b0+k_ncc) b_arg = b.clone_with(a=a0+k_arg, b=b0+k_arg) f = field.Field(dist=d, bases=(b_ncc,), dtype=dtype) g = field.Field(dist=d, bases=(b_arg,), dtype=dtype) f['g'] = np.random.randn(*f['g'].shape) g['g'] = np.random.randn(*g['g'].shape) x = b.local_grid(1) f['g'] = np.cos(6*x) g['g'] = np.cos(6*x) kmax = max(k_ncc, k_arg) if kmax > 0 and dealias < 2: f['c'][-kmax:] = 0 g['c'][-kmax:] = 0 vars = [g] w0 = f * g w1 = w0.reinitialize(ncc=True, ncc_vars=vars) problem = problems.LBVP(vars) problem.add_equation((w1, 0)) solver = solvers.LinearBoundaryValueSolver(problem) w1.store_ncc_matrices(vars, solver.subproblems) w0 = w0.evaluate() w2 = field.Field(dist=d, bases=(b,), dtype=dtype) w2.preset_scales(w2.domain.dealias) w2['g'] = w0['g'] w2['c'] w0['c'] print(np.max(np.abs(w2['g'] - w0['g']))) w2 = operators.Convert(w2, w0.domain.bases[0]).evaluate() w1 = w1.evaluate_as_ncc() assert np.allclose(w0['c'], w2['c'])