def test_convert_constant_tensor(Nphi, Ntheta, Nr, k, dealias, basis, dtype): c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, k, dealias, dtype) f = field.Field(dist=d, dtype=dtype, tensorsig=(c, c)) f['g'][0, 0] = f['g'][1, 1] = f['g'][2, 2] = 1 g = operators.Convert(f, b).evaluate() assert np.allclose(f['g'], g['g'])
def test_convert_constant_scalar(Nphi, Ntheta, Nr, k, dealias, basis, dtype): c, d, b, phi, theta, r, x, y, z = basis(Nphi, Ntheta, Nr, k, dealias, dtype) f = field.Field(dist=d, dtype=dtype) f['g'] = 1 g = operators.Convert(f, b).evaluate() assert np.allclose(f['g'], g['g'])
def test_convert_constant_scalar_explicit(Nphi, Ntheta, dealias, dtype): c, d, b, phi, theta = build_sphere(Nphi, Ntheta, dealias, dtype) f = d.Field() if np.iscomplexobj(dtype()): fref = 1j else: fref = 1 f['g'] = fref g = operators.Convert(f, b).evaluate() assert np.allclose(g['g'], fref)
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'])