def test_tensor2(): B0 = shenfun.FunctionSpace(8, 'C') T = shenfun.TensorProductSpace(comm, (B0, B0)) x, y = sp.symbols('x,y') ue = x**2 + y**2 ua = shenfun.Array(T, buffer=ue) uh = ua.forward() M = shenfun.VectorSpace(T) gradu = shenfun.project(grad(uh), M)
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': if basis.short_name() == 'DU': B1 = cBasisGC[0](N, quad) else: 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, B0)) TC = shenfun.TensorProductSpace(shenfun.comm, (B1, B1)) 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.FunctionSpace(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
def test_tensor2(): B0 = shenfun.FunctionSpace(8, 'C') T = shenfun.TensorProductSpace(comm, (B0, B0)) x, y = sp.symbols('x,y') ue = x**2 + y**2 ua = shenfun.Array(T, buffer=ue) uh = ua.forward() M = shenfun.VectorSpace(T) gradu = shenfun.project(grad(uh), M) V = shenfun.TensorSpace(T) gradgradu = shenfun.project(grad(grad(uh)), V) g = gradgradu.backward() assert np.allclose(g.v[0], 2) assert np.allclose(g.v[1], 0) assert np.allclose(g.v[2], 0) assert np.allclose(g.v[3], 2)
def _setup_function_space(self): self._nonhomogeneous_bcs = False self._function_spaces = [] vec_space = [] for i in range(self._dim): tens_space = [] for j in range(self._dim): basis = sf.FunctionSpace(self._N[j], family='legendre', bc=self._bcs[i][j], domain=tuple(self._domain[j])) if basis.has_nonhomogeneous_bcs: self._nonhomogeneous_bcs = True tens_space.append(basis) self._function_spaces.append(tens_space) vec_space.append(sf.TensorProductSpace(comm, tuple(tens_space))) self._V = sf.VectorSpace(vec_space)
def get_dimensional_solution(self): assert hasattr(self, "_solution") vec_space = list() for i in range(self._dim): tens_space = [] for j in range(self._dim): basis = sf.FunctionSpace(self._N[j], family='legendre', bc=self._bcs[i][j], domain=tuple(self._domain[j])) tens_space.append(basis) vec_space.append(sf.TensorProductSpace(sf.comm, tuple(tens_space))) V = sf.VectorSpace(vec_space) u_hat = sf.Function(V) for i in range(self._dim): # u has the same expansions coefficients as u_dimless u_hat[i] = self._u_ref * self._solution[i] return u_hat
def test_transforms(ST, quad): N = 10 kwargs = {} if not ST.family() == 'fourier': kwargs['quad'] = quad ST0 = ST(N, **kwargs) fj = shenfun.Array(ST0) fj[:] = np.random.random(N) fj = fj.forward().backward().copy() assert np.allclose(fj, fj.forward().backward()) u0 = shenfun.Function(ST0) u1 = shenfun.Array(ST0) u0 = ST0.forward(fj, u0, fast_transform=False) u1 = ST0.backward(u0, u1, fast_transform=False) assert np.allclose(fj, u1, rtol=1e-5, atol=1e-6) # Multidimensional version ST0 = ST(N, **kwargs) if ST0.short_name() in ('R2C', 'C2C'): F0 = shenfun.FunctionSpace(N, 'F', dtype='D') T0 = shenfun.TensorProductSpace(shenfun.comm, (F0, ST0)) else: F0 = shenfun.FunctionSpace(N, 'F', dtype='d') T0 = shenfun.TensorProductSpace(shenfun.comm, (F0, ST0)) fij = shenfun.Array(T0) fij[:] = np.random.random(T0.shape(False)) fij = fij.forward().backward().copy() assert np.allclose(fij, fij.forward().backward()) if ST0.short_name() in ('R2C', 'C2C'): F0 = shenfun.FunctionSpace(N, 'F', dtype='D') F1 = shenfun.FunctionSpace(N, 'F', dtype='D') T = shenfun.TensorProductSpace(shenfun.comm, (F0, F1, ST0), dtype=ST0.dtype.char) else: F0 = shenfun.FunctionSpace(N, 'F', dtype='d') F1 = shenfun.FunctionSpace(N, ST.family()) T = shenfun.TensorProductSpace(shenfun.comm, (F0, ST0, F1)) fij = shenfun.Array(T) fij[:] = np.random.random(T.shape(False)) fij = fij.forward().backward().copy() assert np.allclose(fij, fij.forward().backward())
import shenfun as sf import numpy as np import sympy as sp import matplotlib.pyplot as plt B = sf.FunctionSpace(20, family='legendre') r, phi = sp.symbols('x,y', real=True, positive=True) psi = (r, phi) # position vector rv = (r * sp.cos(phi), r * sp.sin(phi)) # some parameters r = 2 N, M = 30, 30 R = sf.FunctionSpace(N, family='legendre', domain=(0, r), dtype='D') PHI = sf.FunctionSpace(M, family='fourier', domain=(0, 2.0 * np.pi)) T = sf.TensorProductSpace(sf.comm, (R, PHI), coordinates=(psi, rv)) u = sf.TrialFunction(T) v = sf.TestFunction(T) X, Y = T.cartesian_mesh() plt.scatter(X, Y) coors = T.coors
import pytest import numpy as np import sympy as sp from mpi4py import MPI import shenfun from shenfun import inner, div, grad, curl N = 8 comm = MPI.COMM_WORLD V = shenfun.FunctionSpace(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(np.array(e2.scales()).astype(np.int), 2) e2 = e*2 assert np.allclose(np.array(e2.scales()).astype(np.int), 2.) if e.expr_rank() == 1: a = tuple(range(e.dimensions)) e2 = a*e assert np.allclose(np.array(e2.scales()).astype(np.int)[:, 0], (0, 1))
import shenfun as sf import numpy as np import sympy as sp import matplotlib.pyplot as plt N = 10 L = sf.FunctionSpace(N, family='legendre', bc=(0, 0)) print(L.__class__) # plot quadrature points quad_points = L.mpmath_points_and_weights()[0] plt.scatter(quad_points, np.zeros_like(quad_points)) # a projection x = sp.symbols('x', real=True) f = 1 - 1 / 2 * (3 * x**2 - 1) # the first Shen-Dirichlet basis function P = sf.Function(L) func = sf.project(f, L) func_quad = sf.Array(L, buffer=f) plt.scatter(quad_points, func_quad) # should be orthogonal to all others except 0th and 2nd scalprod = L.scalar_product(func_quad) assert np.allclose(scalprod[np.r_[1, 3:N - 1]], 0) assert np.logical_not(np.allclose(scalprod[np.r_[0, 2]], 0))