def test_inner(): v = shenfun.TestFunction(TT) u = shenfun.TrialFunction(TT) p = shenfun.TrialFunction(T) q = shenfun.TestFunction(T) inner(div(u), div(v)) inner(grad(u), grad(v)) inner(q, div(u)) inner(curl(v), curl(u)) inner(grad(q), grad(p)) inner(v, grad(div(u))) inner(q, u[0]) inner(q, u[1]) inner(q, grad(p)[0]) inner(q, grad(p)[1]) inner(v, grad(u)[0]) inner(v, grad(u)[1]) wq = shenfun.TrialFunction(VT) w, q = wq hf = shenfun.TestFunction(VT) h, f = hf inner(h, div(grad(w))) inner(f, div(div(grad(w)))) inner(h, curl(w)) inner(curl(h), curl(w)) inner(h, grad(div(w))) qw = shenfun.TrialFunction(WT) q, w = qw fh = shenfun.TestFunction(WT) f, h = fh inner(h, div(grad(w))) inner(f, div(div(grad(w)))) inner(h, curl(w)) inner(curl(h), curl(w)) inner(h, grad(div(w)))
def test_biharmonic2D(family, axis): la = lla if family == 'chebyshev': la = cla N = (16, 16) SD = FunctionSpace(N[axis], family=family, bc='Biharmonic') K1 = FunctionSpace(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 = TensorProductSpace(subcomms, bases, axes=allaxes2D[axis]) u = shenfun.TrialFunction(T) v = shenfun.TestFunction(T) if family == 'chebyshev': mat = inner(v, div(grad(div(grad(u))))) else: mat = inner(div(grad(v)), div(grad(u))) H = la.Biharmonic(*mat) u = Function(T) u[:] = np.random.random(u.shape) + 1j * np.random.random(u.shape) f = Function(T) f = H.matvec(u, f) g0 = Function(T) g1 = Function(T) g2 = Function(T) M = {d.get_key(): d for d in mat} g0 = M['SBBmat'].matvec(u, g0) g1 = M['ABBmat'].matvec(u, g1) g2 = M['BBBmat'].matvec(u, g2) assert np.linalg.norm(f - (g0 + g1 + g2)) < 1e-8
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_helmholtz2D(family, axis): la = lla if family == 'chebyshev': la = cla N = (8, 9) SD = FunctionSpace(N[axis], family=family, bc=(0, 0)) K1 = FunctionSpace(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 = TensorProductSpace(subcomms, bases, axes=allaxes2D[axis], modify_spaces_inplace=True) u = shenfun.TrialFunction(T) v = shenfun.TestFunction(T) if family == 'chebyshev': mat = inner(v, div(grad(u))) else: mat = inner(grad(v), grad(u)) H = la.Helmholtz(*mat) u = Function(T) s = SD.sl[SD.slice()] u[s] = np.random.random(u[s].shape) + 1j*np.random.random(u[s].shape) f = Function(T) f = H.matvec(u, f) g0 = Function(T) g1 = 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)) uc = Function(T) uc = H(uc, f) assert np.linalg.norm(uc-u) < 1e-12
def test_curl(typecode): K0 = FunctionSpace(N[0], 'F', dtype=typecode.upper()) K1 = FunctionSpace(N[1], 'F', dtype=typecode.upper()) K2 = FunctionSpace(N[2], 'F', dtype=typecode) T = TensorProductSpace(comm, (K0, K1, K2), dtype=typecode) X = T.local_mesh(True) K = T.local_wavenumbers() Tk = VectorSpace(T) u = shenfun.TrialFunction(Tk) v = shenfun.TestFunction(Tk) U = Array(Tk) U_hat = Function(Tk) curl_hat = Function(Tk) curl_ = Array(Tk) # Initialize a Taylor Green vortex U[0] = np.sin(X[0]) * np.cos(X[1]) * np.cos(X[2]) U[1] = -np.cos(X[0]) * np.sin(X[1]) * np.cos(X[2]) U[2] = 0 U_hat = Tk.forward(U, U_hat) Uc = U_hat.copy() U = Tk.backward(U_hat, U) U_hat = Tk.forward(U, U_hat) assert allclose(U_hat, Uc) divu_hat = project(div(U_hat), T) divu = Array(T) divu = T.backward(divu_hat, divu) assert allclose(divu, 0) curl_hat[0] = 1j * (K[1] * U_hat[2] - K[2] * U_hat[1]) curl_hat[1] = 1j * (K[2] * U_hat[0] - K[0] * U_hat[2]) curl_hat[2] = 1j * (K[0] * U_hat[1] - K[1] * U_hat[0]) curl_ = Tk.backward(curl_hat, curl_) w_hat = Function(Tk) w_hat = inner(v, curl(U_hat), output_array=w_hat) A = inner(v, u) for i in range(3): w_hat[i] = A[i].solve(w_hat[i]) w = Array(Tk) w = Tk.backward(w_hat, w) assert allclose(w, curl_) u_hat = Function(Tk) u_hat = inner(v, U, output_array=u_hat) for i in range(3): u_hat[i] = A[i].solve(u_hat[i]) uu = Array(Tk) uu = Tk.backward(u_hat, uu) assert allclose(u_hat, U_hat)
def test_div2(basis, quad): B = basis(8, quad=quad) u = shenfun.TrialFunction(B) v = shenfun.TestFunction(B) m = inner(u, v) z = Function(B, val=1) c = m / z m2 = m.diags('csr') c2 = spsolve(m2, z[B.slice()]) assert np.allclose(c2, c[B.slice()])
def test_inner(): v = shenfun.TestFunction(TT) u = shenfun.TrialFunction(TT) p = shenfun.TrialFunction(T) q = shenfun.TestFunction(T) A = inner(div(u), div(v)) B = inner(grad(u), grad(v)) C = inner(q, div(u)) D = inner(curl(v), curl(u)) E = inner(grad(q), grad(p)) F = inner(v, grad(div(u))) wq = shenfun.TrialFunction(VT) w, q = wq hf = shenfun.TestFunction(VT) h, f = hf G = inner(h, div(grad(w))) H = inner(f, div(div(grad(w)))) I = inner(h, curl(w)) J = inner(curl(h), curl(w)) K = inner(h, grad(div(w)))
def test_mul2(): mat = SparseMatrix({0: 1}, (3, 3)) v = np.ones(3) c = mat * v assert np.allclose(c, 1) mat = SparseMatrix({-2: 1, -1: 1, 0: 1, 1: 1, 2: 1}, (3, 3)) c = mat * v assert np.allclose(c, 3) SD = FunctionSpace(8, "L", bc=(0, 0), scaled=True) u = shenfun.TrialFunction(SD) v = shenfun.TestFunction(SD) mat = inner(grad(u), grad(v)) z = Function(SD, val=1) c = mat * z assert np.allclose(c[:6], 1)
def solve(self): if self._dim == 2: map_boundary_to_component_index = { 'right': 0, 'top': 1, 'left': 0, 'bottom': 1 } map_boundary_to_start_end_index = { 'right': 1, 'top': 1, 'left': -1, 'bottom': -1 } if self._traction_bcs: boundary_traction_term = Function(self._V.get_orthogonal()) # b: boundary, c: component for b, c, value in self._traction_bcs: if self._dim == 2: side_index = map_boundary_to_component_index[b] bdry_basis_index = 1 if side_index == 0 else 0 boundary_basis = self._function_spaces[c][bdry_basis_index] start_or_end_index = map_boundary_to_start_end_index[b] # test function for boundary integral v_boundary = sf.TestFunction(boundary_basis) if isinstance(value, (float, int)): trac = Array(boundary_basis, val=value) else: trac = Array(boundary_basis, buffer=value) evaluate_on_boundary = self._function_spaces[c][ side_index].evaluate_basis_all(start_or_end_index) project_traction = inner(trac, v_boundary) if side_index == 0: boundary_traction_term[c] += np.outer( evaluate_on_boundary, project_traction) elif side_index == 1: boundary_traction_term[c] += np.outer( project_traction, evaluate_on_boundary) else: raise ValueError() M = sf.BlockMatrix(self._dw_int) if self._traction_bcs: self._dw_ext += boundary_traction_term u_hat = M.solve(self._dw_ext) return u_hat
def _setup_variational_problem(self): self._setup_function_space() u = sf.TrialFunction(self._V) v = sf.TestFunction(self._V) self._elastic_law.set_material_parameters(self._material_parameters) self._dw_int = self._elastic_law.dw_int(u, v) self._dw_ext = inner( v, sf.Array(self._V.get_orthogonal(), buffer=(0, ) * self._dim)) if self._body_forces is not None: V_body_forces = self._V.get_orthogonal() body_forces_quad = sf.Array(V_body_forces, buffer=self._body_forces) self._dw_ext = inner(v, body_forces_quad)
def test_project_1D(basis): ue = sin(2*np.pi*x)*(1-x**2) T = basis(12) u = shenfun.TrialFunction(T) v = shenfun.TestFunction(T) u_tilde = shenfun.Function(T) X = T.mesh() ua = shenfun.Array(T, buffer=ue) u_tilde = shenfun.inner(v, ua, output_array=u_tilde) M = shenfun.inner(u, v) u_p = shenfun.Function(T) u_p = M.solve(u_tilde, u=u_p) u_0 = shenfun.Function(T) u_0 = shenfun.project(ua, T) assert np.allclose(u_0, u_p) u_1 = shenfun.project(ue, T) assert np.allclose(u_1, u_p)
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_helmholtz3D(family, axis): la = lla if family == 'chebyshev': la = cla N = (8, 9, 10) SD = FunctionSpace(N[allaxes3D[axis][0]], family=family, bc=(0, 0)) K1 = FunctionSpace(N[allaxes3D[axis][1]], family='F', dtype='D') K2 = FunctionSpace(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 = TensorProductSpace(subcomms, bases, axes=allaxes3D[axis]) u = shenfun.TrialFunction(T) v = shenfun.TestFunction(T) if family == 'chebyshev': mat = inner(v, div(grad(u))) else: mat = inner(grad(v), grad(u)) H = la.Helmholtz(*mat) u = Function(T) s = SD.sl[SD.slice()] u[s] = np.random.random(u[s].shape) + 1j * np.random.random(u[s].shape) f = Function(T) f = H.matvec(u, f) g0 = Function(T) g1 = 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)) uc = Function(T) uc = H(uc, f) assert np.linalg.norm(uc - u) < 1e-12
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 # test coordinates assert coors.is_cartesian is False assert coors.is_orthogonal is True print('position vector\n', coors.rv) print('covariante basis\n', coors.b) print('normed covariant basis\n', coors.e) print('contravariant basis\n', coors.get_contravariant_basis())