def test_basis_interpolate_project(m, e): basis = CellBasis(m, e) x = basis.zeros() + 1 Y = basis.interpolate(x) X = basis.project(Y) # check that interpolate and project are inverses assert_almost_equal(x, X)
def test_point_source(etype): mesh = MeshLine1().refined() basis = CellBasis(mesh, etype()) source = np.array([0.7]) u = solve(*condense(asm(laplace, basis), basis.point_source(source), D=basis.get_dofs())) exact = np.stack([(1 - source) * mesh.p, (1 - mesh.p) * source]).min(0) assert_almost_equal(u[basis.nodal_dofs], exact)
def test_basis_project_grad(): m, e = (MeshTri(), ElementTriP1()) basis = CellBasis(m, e) Y = basis.interpolate(m.p[0]) y = basis.project(Y.grad[0]) @Functional def int_y(w): return w.y ** 2 assert_almost_equal(int_y.assemble(basis, y=y), 1.)
def test_basis_project(m, e, fun): basis = CellBasis(m, e) y = basis.project(fun) @Functional def int_y_1(w): return w.y**2 @Functional def int_y_2(w): return fun(w.x)**2 assert_almost_equal(int_y_1.assemble(basis, y=y), int_y_2.assemble(basis))
def runTest(self): m = self.prepare_mesh() basis = CellBasis(m, self.element_type()) x = projection(lambda x: x[0] ** 2, basis) fun = basis.interpolator(x) X = np.linspace(0, 1, 10) dim = m.dim() if dim == 3: y = fun(np.array([X, [0.31] * 10, [0.62] * 10])) elif dim == 2: y = fun(np.array([X, [0.31] * 10])) else: y = fun(np.array([X])) assert_allclose(y, X ** 2, atol=1e-10)
def test_interpolator_probes(mtype, e, nrefs, npoints): m = mtype() if nrefs > 0: m = m.refined(nrefs) np.random.seed(0) X = np.random.rand(m.p.shape[0], int(npoints)) basis = CellBasis(m, e) y = projection(lambda x: x[0] + x[1], basis) assert_allclose(basis.probes(X) @ y, basis.interpolator(y)(X)) assert_allclose(basis.probes(X) @ y, X[0] + X[1])
def runTest(self): m = self.mesh_type().refined(2) basis = CellBasis(m, self.elem_type()) for fun in [ lambda x: x[0] == 0, lambda x: x[0] == 1, lambda x: x[1] == 0, lambda x: x[1] == 1, lambda x: x[2] == 0, lambda x: x[2] == 1 ]: arr1 = basis.find_dofs({'kek': m.facets_satisfying(fun)})['kek'].edge['u'] arr2 = basis.edge_dofs[:, m.edges_satisfying(fun)] assert_allclose(arr1, arr2.flatten())
def runTest(self): m = self.mesh_type() basis = CellBasis(m, self.mesh_type.elem()) y = basis.project(lambda x: x[0]) plot(basis, y, nrefs=1, figsize=(4, 4), aspect=1.1, cmap=plt.get_cmap('viridis'), shading='gouraud', vmin=0, vmax=1, levels=3, colorbar=True)
def plot(self, *args, **kwargs): """Convenience wrapper for :func:`skfem.visuals.matplotlib.plot`.""" from skfem.visuals.matplotlib import plot, show from skfem.assembly import CellBasis ax = plot(CellBasis(self, self.elem()), *args, **kwargs) ax.show = show return ax
def test_pickling(): # some simple checks for pickle mesh = MeshQuad() elem = ElementQuad1() mapping = MappingIsoparametric(mesh, elem) basis = CellBasis(mesh, elem, mapping) pickled_mesh = pickle.dumps(mesh) pickled_elem = pickle.dumps(elem) pickled_mapping = pickle.dumps(mapping) pickled_basis = pickle.dumps(basis) mesh1 = pickle.loads(pickled_mesh) elem1 = pickle.loads(pickled_elem) mapping1 = pickle.loads(pickled_mapping) basis1 = pickle.loads(pickled_basis) assert_almost_equal( laplace.assemble(basis).toarray(), laplace.assemble(basis1).toarray(), ) assert_almost_equal( mesh.doflocs, mesh1.doflocs, ) assert_almost_equal( mapping.J(0, 0, np.array([[.3], [.3]])), mapping1.J(0, 0, np.array([[.3], [.3]])), ) assert_almost_equal( elem.doflocs, elem1.doflocs, )
def test_subdomain_facet_assembly(): def subdomain(x): return np.logical_and( np.logical_and(x[0] > .25, x[0] < .75), np.logical_and(x[1] > .25, x[1] < .75), ) m, e = MeshTri().refined(4), ElementTriP2() cbasis = CellBasis(m, e) cbasis_p0 = cbasis.with_element(ElementTriP0()) sfbasis = FacetBasis(m, e, facets=m.facets_around(subdomain, flip=True)) sfbasis_p0 = sfbasis.with_element(ElementTriP0()) sigma = cbasis_p0.zeros() + 1 @BilinearForm def laplace(u, v, w): return dot(w.sigma * grad(u), grad(v)) A = laplace.assemble(cbasis, sigma=cbasis_p0.interpolate(sigma)) u0 = cbasis.zeros() u0[cbasis.get_dofs(elements=subdomain)] = 1 u0_dofs = cbasis.get_dofs() + cbasis.get_dofs(elements=subdomain) A, b = enforce(A, D=u0_dofs, x=u0) u = solve(A, b) @Functional def measure_current(w): return dot(w.n, w.sigma * grad(w.u)) meas = measure_current.assemble(sfbasis, sigma=sfbasis_p0.interpolate(sigma), u=sfbasis.interpolate(u)) assert_almost_equal(meas, 9.751915526759191)
def test_oriented_gauss_integral(m, e): facets = m.facets_around('mid') fb = FacetBasis(m, e, facets=facets) cb = CellBasis(m, e, elements='mid') assert_almost_equal( Functional(lambda w: w.x[0] * w.n[0]).assemble(fb), Functional(lambda w: 1. + 0. * w.x[0]).assemble(cb), decimal=5, )
def runTest(self): """Solve Stokes problem, try splitting and other small things.""" m = MeshTri().refined() m = m.refined(3).with_boundaries({ 'up': lambda x: x[1] == 1., 'rest': lambda x: x[1] != 1., }) e = ElementVectorH1(ElementTriP2()) * ElementTriP1() basis = CellBasis(m, e) @BilinearForm def bilinf(u, p, v, q, w): from skfem.helpers import grad, ddot, div return (ddot(grad(u), grad(v)) - div(u) * q - div(v) * p - 1e-2 * p * q) S = asm(bilinf, basis) D = basis.find_dofs(skip=['u^2']) x = basis.zeros() x[D['up'].all('u^1^1')] = .1 x = solve(*condense(S, x=x, D=D)) (u, u_basis), (p, p_basis) = basis.split(x) self.assertEqual(len(u), m.p.shape[1] * 2 + m.facets.shape[1] * 2) self.assertEqual(len(p), m.p.shape[1]) self.assertTrue(np.sum(p - x[basis.nodal_dofs[2]]) < 1e-8) U, P = basis.interpolate(x) self.assertTrue(isinstance(U.value, np.ndarray)) self.assertTrue(isinstance(P.value, np.ndarray)) self.assertTrue(P.shape[0] == m.nelements) self.assertTrue((basis.doflocs[:, D['up'].all()][1] == 1.).all()) # test blocks splitting of forms while at it C1 = asm(bilinf.block(1, 1), CellBasis(m, ElementTriP1())) C2 = S[basis.nodal_dofs[-1]].T[basis.nodal_dofs[-1]].T self.assertTrue(abs((C1 - C2).min()) < 1e-10) self.assertTrue(abs((C1 - C2).max()) < 1e-10) # test splitting ElementVector (ux, uxbasis), (uy, uybasis) = u_basis.split(u) assert_allclose(ux[uxbasis.nodal_dofs[0]], u[u_basis.nodal_dofs[0]]) assert_allclose(ux[uxbasis.facet_dofs[0]], u[u_basis.facet_dofs[0]]) assert_allclose(uy[uybasis.nodal_dofs[0]], u[u_basis.nodal_dofs[1]]) assert_allclose(uy[uybasis.facet_dofs[0]], u[u_basis.facet_dofs[1]])
def runTest(self): mesh = MeshTri().refined(5) basis = CellBasis(mesh, ElementTriP1()) def fun(X): x, y = X return x**2 + y**2 x = projection(fun, basis) y = fun(mesh.p) normest = np.linalg.norm(x - y) self.assertTrue(normest < 0.011, msg="|x-y| = {}".format(normest))
def test_trace(mtype, e1, e2, flat): m = mtype().refined(3) # use the boundary where last coordinate is zero basis = FacetBasis(m, e1, facets=m.facets_satisfying(lambda x: x[-1] == 0.0)) xfun = projection(lambda x: x[0], CellBasis(m, e1)) nbasis, y = basis.trace(xfun, lambda p: p[0] if flat else p[:-1], target_elem=e2) @Functional def integ(w): return w.y # integrate f(x) = x_1 over trace mesh assert_almost_equal(integ.assemble(nbasis, y=nbasis.interpolate(y)), .5)
def runTest(self): m = self.mesh e = ElementTriP1() basis = CellBasis(m, e) A = laplace.assemble(basis) M = mass.assemble(basis) D = m.boundary_nodes() assert_almost_equal(enforce(A, D=D).toarray(), np.eye(A.shape[0])) assert_almost_equal( enforce(M, D=D, diag=0.).toarray(), np.zeros(M.shape)) enforce(A, D=D, overwrite=True) assert_almost_equal(A.toarray(), np.eye(A.shape[0]))
def test_simple_cg_solver(): m = MeshTri().refined(3) basis = CellBasis(m, ElementTriP1()) A0 = laplace.coo_data(basis) A1 = laplace.assemble(basis) f = unit_load.assemble(basis) D = m.boundary_nodes() x1 = solve(*condense(A1, f, D=D)) f[D] = 0 x0 = A0.solve(f, D=D) assert_almost_equal(x0, x1)
def runTest(self): """Solve Stokes problem, try splitting and other small things.""" m = MeshTri().refined() m = m.refined(3).with_boundaries({ 'up': lambda x: x[1] == 1., 'rest': lambda x: x[1] != 1., }) e = ElementVectorH1(ElementTriP2()) * ElementTriP1() basis = CellBasis(m, e) @BilinearForm def bilinf(u, p, v, q, w): from skfem.helpers import grad, ddot, div return (ddot(grad(u), grad(v)) - div(u) * q - div(v) * p - 1e-2 * p * q) S = asm(bilinf, basis) D = basis.find_dofs(skip=['u^2']) x = basis.zeros() x[D['up'].all('u^1^1')] = .1 x = solve(*condense(S, x=x, D=D)) (u, u_basis), (p, p_basis) = basis.split(x) self.assertEqual(len(u), m.p.shape[1] * 2 + m.facets.shape[1] * 2) self.assertEqual(len(p), m.p.shape[1]) self.assertTrue(np.sum(p - x[basis.nodal_dofs[2]]) < 1e-8) U, P = basis.interpolate(x) self.assertTrue(isinstance(U.value, np.ndarray)) self.assertTrue(isinstance(P.value, np.ndarray)) self.assertTrue((basis.doflocs[:, D['up'].all()][1] == 1.).all())
def test_call_svg_plot(mtype): m = mtype() svg = drawsvg(m, nrefs=2) basis = CellBasis(m, mtype.elem()) svg_plot = plotsvg(basis, m.p[0], nrefs=2)
def test_basis_project_composite(m, e, p1, p2): basis = CellBasis(m, e) y = basis.project(p1) assert_almost_equal(y, p2)
def runTest(self): with self.assertRaises(ValueError): m = MeshTri() e = ElementTetP2() basis = CellBasis(m, e)