def test_helmholtz2D(family, axis): la = lla if family == 'chebyshev': la = cla N = (8, 9) SD = shenfun.Basis(N[axis], family=family, bc=(0, 0)) K1 = shenfun.Basis(N[(axis + 1) % 2], family='F', dtype='d') subcomms = mpi4py_fft.pencil.Subcomm(MPI.COMM_WORLD, allaxes2D[axis]) bases = [K1] bases.insert(axis, SD) T = shenfun.TensorProductSpace(subcomms, bases, axes=allaxes2D[axis]) u = shenfun.TrialFunction(T) v = shenfun.TestFunction(T) if family == 'chebyshev': mat = shenfun.inner(v, shenfun.div(shenfun.grad(u))) else: mat = shenfun.inner(shenfun.grad(v), shenfun.grad(u)) H = la.Helmholtz(*mat) H = la.Helmholtz(*mat) u = shenfun.Function(T) u[:] = np.random.random(u.shape) + 1j * np.random.random(u.shape) f = shenfun.Function(T) f = H.matvec(u, f) f = H.matvec(u, f) g0 = shenfun.Function(T) g1 = shenfun.Function(T) M = {d.get_key(): d for d in mat} g0 = M['ADDmat'].matvec(u, g0) g1 = M['BDDmat'].matvec(u, g1) assert np.linalg.norm(f - (g0 + g1)) < 1e-12, np.linalg.norm(f - (g0 + g1))
def test_biharmonic3D(family, axis): la = lla if family == 'chebyshev': la = cla N = (16, 16, 16) SD = shenfun.Basis(N[allaxes3D[axis][0]], family=family, bc='Biharmonic') K1 = shenfun.Basis(N[allaxes3D[axis][1]], family='F', dtype='D') K2 = shenfun.Basis(N[allaxes3D[axis][2]], family='F', dtype='d') subcomms = mpi4py_fft.pencil.Subcomm(MPI.COMM_WORLD, [0, 1, 1]) bases = [0] * 3 bases[allaxes3D[axis][0]] = SD bases[allaxes3D[axis][1]] = K1 bases[allaxes3D[axis][2]] = K2 T = shenfun.TensorProductSpace(subcomms, bases, axes=allaxes3D[axis]) u = shenfun.TrialFunction(T) v = shenfun.TestFunction(T) if family == 'chebyshev': mat = shenfun.inner( v, shenfun.div(shenfun.grad(shenfun.div(shenfun.grad(u))))) else: mat = shenfun.inner(shenfun.div(shenfun.grad(v)), shenfun.div(shenfun.grad(u))) H = la.Biharmonic(*mat) H = la.Biharmonic(*mat) u = shenfun.Function(T) u[:] = np.random.random(u.shape) + 1j * np.random.random(u.shape) f = shenfun.Function(T) f = H.matvec(u, f) f = H.matvec(u, f) g0 = shenfun.Function(T) g1 = shenfun.Function(T) g2 = shenfun.Function(T) M = {d.get_key(): d for d in mat} amat = 'ABBmat' if family == 'chebyshev' else 'PBBmat' g0 = M['SBBmat'].matvec(u, g0) g1 = M[amat].matvec(u, g1) g2 = M['BBBmat'].matvec(u, g2) assert np.linalg.norm(f - (g0 + g1 + g2)) < 1e-8, np.linalg.norm(f - (g0 + g1 + g2))
def test_mul2(): mat = shenfun.SparseMatrix({0: 1}, (3, 3)) v = np.ones(3) c = mat * v assert np.allclose(c, 1) mat = shenfun.SparseMatrix({-2:1, -1:1, 0: 1, 1:1, 2:1}, (3, 3)) c = mat * v assert np.allclose(c, 3) SD = shenfun.Basis(8, "L", bc=(0, 0), plan=True, scaled=True) u = shenfun.TrialFunction(SD) v = shenfun.TestFunction(SD) mat = shenfun.inner(shenfun.grad(u), shenfun.grad(v)) z = shenfun.Function(SD, val=1) c = mat * z assert np.allclose(c[:6], 1)
def test_to_ortho(basis, quad): N = 10 if basis.family() == 'legendre': B1 = lBasis[0](N, quad) B3 = lBasis[0](N, quad) elif basis.family() == 'chebyshev': B1 = cBasis[0](N, quad) B3 = cBasis[0](N, quad) elif basis.family() == 'laguerre': B1 = laBasis[0](N, quad) B3 = laBasis[0](N, quad) B0 = basis(N, quad=quad) a = shenfun.Array(B0) a_hat = shenfun.Function(B0) b0_hat = shenfun.Function(B1) b1_hat = shenfun.Function(B1) a[:] = np.random.random(a.shape) a_hat = a.forward(a_hat) b0_hat = shenfun.project(a_hat, B1, output_array=b0_hat, fill=False, use_to_ortho=True) b1_hat = shenfun.project(a_hat, B1, output_array=b1_hat, fill=False, use_to_ortho=False) assert np.linalg.norm(b0_hat-b1_hat) < 1e-10 B2 = basis(N, quad=quad) TD = shenfun.TensorProductSpace(shenfun.comm, (B0, B2)) TC = shenfun.TensorProductSpace(shenfun.comm, (B1, B3)) a = shenfun.Array(TD) a_hat = shenfun.Function(TD) b0_hat = shenfun.Function(TC) b1_hat = shenfun.Function(TC) a[:] = np.random.random(a.shape) a_hat = a.forward(a_hat) b0_hat = shenfun.project(a_hat, TC, output_array=b0_hat, fill=False, use_to_ortho=True) b1_hat = shenfun.project(a_hat, TC, output_array=b1_hat, fill=False, use_to_ortho=False) assert np.linalg.norm(b0_hat-b1_hat) < 1e-10 F0 = shenfun.Basis(N, 'F') TD = shenfun.TensorProductSpace(shenfun.comm, (B0, F0)) TC = shenfun.TensorProductSpace(shenfun.comm, (B1, F0)) a = shenfun.Array(TD) a_hat = shenfun.Function(TD) b0_hat = shenfun.Function(TC) b1_hat = shenfun.Function(TC) a[:] = np.random.random(a.shape) a_hat = a.forward(a_hat) b0_hat = shenfun.project(a_hat, TC, output_array=b0_hat, fill=False, use_to_ortho=True) b1_hat = shenfun.project(a_hat, TC, output_array=b1_hat, fill=False, use_to_ortho=False) assert np.linalg.norm(b0_hat-b1_hat) < 1e-10
import pytest import numpy as np from mpi4py import MPI import shenfun N = 8 comm = MPI.COMM_WORLD V = shenfun.Basis(N, 'C') u0 = shenfun.TrialFunction(V) T = shenfun.TensorProductSpace(comm, (V, V)) u1 = shenfun.TrialFunction(V) TT = shenfun.VectorTensorProductSpace(T) u2 = shenfun.TrialFunction(TT) @pytest.mark.parametrize('basis', (u0, u1, u2)) def test_mul(basis): e = shenfun.Expr(basis) e2 = 2 * e assert np.allclose(e2.scales(), 2.) e2 = e * 2 assert np.allclose(e2.scales(), 2.) if e.expr_rank() == 1: a = tuple(range(e.dimensions)) e2 = a * e assert np.allclose(e2.scales()[:, 0], (0, 1))