def __init__(self, doflocs, t, **kwargs): warnings.warn("MeshQuad2 is an experimental feature and " "not governed by the semantic versioning. " "Several features of MeshQuad are still " "missing.") if t.shape[0] == 9: dofs, ix = np.unique(t[:4], return_inverse=True) super(MeshQuad2, self).__init__( doflocs[:, dofs], np.arange(len(dofs), dtype=np.int)[ix].reshape(t[:4].shape), **kwargs) else: # fallback for refinterp super(MeshQuad2, self).__init__(doflocs, t, **kwargs) from skfem.element import ElementQuad1, ElementQuad2 from skfem.assembly import InteriorBasis from skfem.mapping import MappingIsoparametric self._elem = ElementQuad2() self._basis = InteriorBasis(self, self._elem, MappingIsoparametric(self, ElementQuad1())) self._mesh = MeshQuad.from_basis(self._basis) if t.shape[0] == 9: self._mesh.p = doflocs self._mesh.t = t
def __init__(self, doflocs, t, **kwargs): warnings.warn("MeshTri2 is an experimental feature and " "not governed by the semantic versioning. " "Several features of MeshTri are still " "missing.") if t.shape[0] == 6: dofs, ix = np.unique(t[:3], return_inverse=True) super(MeshTri2, self).__init__( doflocs[:, dofs], np.arange(len(dofs), dtype=np.int)[ix].reshape(t[:3].shape), sort_t=False, **kwargs) else: # fallback for refinterp super(MeshTri2, self).__init__(doflocs, t, **kwargs) from skfem.element import ElementTriP2 from skfem.assembly import InteriorBasis from skfem.mapping import MappingAffine self._elem = ElementTriP2() self._basis = InteriorBasis(self, self._elem, MappingAffine(self)) self._mesh = MeshTri.from_basis(self._basis) if t.shape[0] == 6: self._mesh.p = doflocs self._mesh.t = t
def runTest(self): m = self.mesh().refined(4) basis = InteriorBasis(m, self.elem) boundary_basis = FacetBasis(m, self.elem) boundary_dofs = boundary_basis.get_dofs().flatten() def dirichlet(x): """return a harmonic function""" return ((x[0] + 1.j * x[1])**2).real u = basis.zeros() A = laplace.assemble(basis) u[boundary_dofs] = projection(dirichlet, boundary_basis, I=boundary_dofs) u = solve(*enforce(A, x=u, D=boundary_dofs)) @Functional def gradu(w): gradu = w['sol'].grad return dot(gradu, gradu) self.assertAlmostEqual( gradu.assemble(basis, sol=basis.interpolate(u)), 8 / 3, delta=1e-10, )
def test_dg_element(m, e, edg): edg = edg(e) @Functional def square(w): return w['random']**2 basis = InteriorBasis(m, e) basisdg = InteriorBasis(m, edg) assert_allclose( square.assemble(basis, random=basis.interpolate(basis.zeros() + 1)), square.assemble(basisdg, random=basisdg.interpolate(basisdg.zeros() + 1)), )
def runTest(self): m = MeshTri() basis = InteriorBasis(m, ElementTriP2()) dofs = basis.get_dofs() self.assertEqual(len(dofs.nodal['u']), 4) self.assertEqual(len(dofs.facet['u']), 4)
def runTest(self): m = MeshQuad().refined(2) e = ElementQuad1() basis = InteriorBasis(m, e) M, X = basis.refinterp(m.p[0], 3) self.assertEqual(M.p.shape[1], len(X))
def runTest(self): m = self.init_mesh() basis = InteriorBasis(m, self.element_type()) A = laplace.assemble(basis) b = unit_load.assemble(basis) x = solve(*condense(A, b, D=basis.get_dofs())) self.assertAlmostEqual(np.max(x), self.maxval, places=3)
def runTest(self): m = MeshTri() basis = InteriorBasis(m, ElementTriP2()) D1 = basis.get_dofs(lambda x: x[0] == 0) D2 = basis.get_dofs(lambda x: x[0] == 1) D3 = basis.get_dofs(lambda x: x[1] == 1) D4 = basis.get_dofs(lambda x: x[1] == 0) assert_allclose(D1 | D2 | D3 | D4, basis.get_dofs()) assert_allclose(D1 + D2 + D3 + D4, basis.get_dofs())
def runTest(self): path = Path(__file__).parents[1] / 'docs' / 'examples' / 'meshes' m = self.mesh_type.load(path / self.filename) basis = InteriorBasis(m, self.element_type()) A = laplace.assemble(basis) b = unit_load.assemble(basis) x = solve(*condense(A, b, D=basis.get_dofs())) self.assertAlmostEqual(np.max(x), 0.06261690318912218, places=3)
def runTest(self): mtype, etype = self.case m = mtype().refined(3) e = etype() ib = InteriorBasis(m, e) x = self.initOnes(ib) f = ib.interpolator(x) X = np.array([np.sin(m.p[0, :]), np.sin(3. * m.p[1, :])]) self.assertTrue(np.sum(f(X) - 1.0) < 1.0e-10)
def runTest(self): mesh = MeshTri().refined(5) basis = InteriorBasis(mesh, ElementTriP1()) def fun(x, y): return x**2 + y**2 x = L2_projection(fun, basis) y = fun(*mesh.p) normest = np.linalg.norm(x - y) self.assertTrue(normest < 0.011, msg="|x-y| = {}".format(normest))
def runTest(self): m = MeshQuad().refined(3) e = ElementQuad1() basis = InteriorBasis(m, e) @Functional def x_squared(w): return w.x[0]**2 y = asm(x_squared, basis) self.assertAlmostEqual(y, 1. / 3.) self.assertEqual(len(x_squared.elemental(basis)), m.t.shape[1])
def runTest(self): m = MeshHex() # check that these assemble to the same matrix ec = ElementHex1() * ElementHex1() * ElementHex1() ev = ElementVectorH1(ElementHex1()) basisc = InteriorBasis(m, ec) basisv = InteriorBasis(m, ev) @BilinearForm def bilinf_ev(u, v, w): from skfem.helpers import dot return dot(u, v) @BilinearForm def bilinf_ec(ux, uy, uz, vx, vy, vz, w): return ux * vx + uy * vy + uz * vz Kv = asm(bilinf_ev, basisv) Kc = asm(bilinf_ec, basisc) self.assertAlmostEqual(np.sum(np.sum((Kv - Kc).todense())), 0.)
def runTest(self): m = self.mesh_type().refined(self.nrefs) basis = InteriorBasis(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 runTest(self): m = self.mesh_type().refined(2) basis = InteriorBasis(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 test_evaluate_functional(mtype, e, mtype2): m = mtype().refined(3) if mtype2 is not None: m = mtype2.from_mesh(m) basis = InteriorBasis(m, e) @Functional def x_squared(w): return w.x[0] ** 2 y = asm(x_squared, basis) assert_almost_equal(y, 1. / 3.) assert_equal(len(x_squared.elemental(basis)), m.t.shape[1])
def runTest(self): """Solve Stokes problem, try splitting and other small things.""" m = MeshTri() m.refine() m.define_boundary('centreline', lambda x: x[0] == .5, boundaries_only=False) m.refine(3) e = ElementVectorH1(ElementTriP2()) * ElementTriP1() m.define_boundary('up', lambda x: x[1] == 1.) m.define_boundary('rest', lambda x: x[1] != 1.) basis = InteriorBasis(m, e) self.assertEqual( basis.get_dofs(m.boundaries['centreline']).all().size, (2 + 1) * (2**(1 + 3) + 1) + 2 * 2**(1 + 3)) self.assertEqual(basis.find_dofs()['centreline'].all().size, (2 + 1) * (2**(1 + 3) + 1) + 2 * 2**(1 + 3)) @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, basis.zeros(), 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 runTest(self): m = MeshLine(np.linspace(0., 1.)).refined(2) ib = InteriorBasis(m, self.e) fb = FacetBasis(m, self.e) @LinearForm def boundary_flux(v, w): return v * (w.x[0] == 1.) L = asm(laplace, ib) b = asm(boundary_flux, fb) D = m.nodes_satisfying(lambda x: x == 0.0) I = ib.complement_dofs(D) # noqa E741 u = solve(*condense(L, b, I=I)) # noqa E741 np.testing.assert_array_almost_equal(u[ib.nodal_dofs[0]], m.p[0], -10)
def runTest(self): m = self.mesh().refined(3) ib = InteriorBasis(m, self.elem()) A = asm(laplace, ib) D = ib.get_dofs().all() I = ib.complement_dofs(D) for X in self.funs: x = self.set_bc(X, ib) Xh = x.copy() x = solve(*condense(A, x=x, I=I)) self.assertLessEqual(np.sum(x - Xh), 1e-10)
def runTest(self): m = MeshLine(np.linspace(0., 1.)).refined(2) ib = InteriorBasis(m, self.e) fb = FacetBasis(m, self.e) @LinearForm def boundary_flux(v, w): return v * (w.x[0] == 1) - v * (w.x[0] == 0) L = asm(laplace, ib) M = asm(mass, ib) b = asm(boundary_flux, fb) u = solve(L + 1e-6 * M, b) np.testing.assert_array_almost_equal(u[ib.nodal_dofs[0]], m.p[0] - .5, -4)
def runTest(self): m = self.mesh e = ElementTriP1() basis = InteriorBasis(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 runTest(self): m = MeshTri() e = ElementTriP1() basis = InteriorBasis(m, e) @Functional def feqx(w): from skfem.helpers import grad f = w['func'] # f(x) = x return grad(f)[0] # f'(x) = 1 func = basis.interpolate(m.p[0]) # integrate f'(x) = 1 over [0, 1]^2 self.assertAlmostEqual(feqx.assemble(basis, func=func), 1.)
def runTest(self): m = MeshLine(np.linspace(0., 1.)) m.refine(2) e = ElementLineP1() ib = InteriorBasis(m, e) fb = FacetBasis(m, e) @linear_form def boundary_flux(v, dv, w): return v * (w.x[0] == 1) - v * (w.x[0] == 0) L = asm(laplace, ib) M = asm(mass, ib) b = asm(boundary_flux, fb) u = solve(L + 1e-6 * M, b) self.assertTrue(np.sum(np.abs(u - m.p[0, :] + 0.5)) < 1e-4)
def runTest(self): m = MeshTri() e = ElementTriP1() basis = InteriorBasis(m, e) @Functional def feqx(w): from skfem.helpers import grad f = w['func'] # f(x, y) = x g = w['gunc'] # g(x, y) = y return grad(f)[0] + grad(g)[1] func = basis.interpolate(m.p[0]) gunc = basis.interpolate(m.p[1]) self.assertAlmostEqual(feqx.assemble(basis, func=func, gunc=gunc), 2.)
def runTest(self): m = MeshTri() e = ElementTriArgyris() basis = InteriorBasis(m, e) all_dofs = basis.get_dofs() self.assertEqual(len(all_dofs.keep('u').nodal), 1) self.assertTrue('u' in all_dofs.keep('u').nodal) self.assertEqual(len(all_dofs.keep('u_n').facet), 1) self.assertEqual(len(all_dofs.drop('u').facet), 1) all_dofs = basis.dofs.get_facet_dofs(m.facets_satisfying(lambda x: 1)) self.assertEqual(len(all_dofs.keep('u_n').facet), 1) self.assertEqual(len(all_dofs.drop('u').facet), 1)
def runTest(self): m = self.case[0]() m.refine(self.prerefs) hs = [] L2s = [] for itr in range(3): e = self.case[1]() ib = InteriorBasis(m, e) @BilinearForm def bilinf(u, v, w): return ddot(dd(u), dd(v)) @LinearForm def linf(v, w): return 1. * v K = asm(bilinf, ib) f = asm(linf, ib) x = solve(*condense(K, f, D=ib.get_dofs().all())) X = ib.interpolate(x) def exact(x): return (x ** 2 - 2. * x ** 3 + x ** 4) / 24. @Functional def error(w): return (w.w - exact(w.x)) ** 2 L2 = np.sqrt(error.assemble(ib, w=X)) L2s.append(L2) hs.append(m.param()) m.refine() hs = np.array(hs) L2s = np.array(L2s) pfit = np.polyfit(np.log10(hs), np.log10(L2s), 1) self.assertGreater(pfit[0], self.limits[0]) self.assertLess(pfit[0], self.limits[1]) self.assertLess(L2s[-1], self.abs_limit)
def runTest(self): m = MeshLine(np.linspace(0., 1.)).refined(2) ib = InteriorBasis(m, self.e) m.define_boundary('left', lambda x: x[0] == 0.0) m.define_boundary('right', lambda x: x[0] == 1.0) fb = FacetBasis(m, self.e, facets=m.boundaries['right']) @LinearForm def boundary_flux(v, w): return -w.x[0] * v L = asm(laplace, ib) b = asm(boundary_flux, fb) D = ib.find_dofs()['left'].all() I = ib.complement_dofs(D) # noqa E741 u = solve(*condense(L, b, I=I)) # noqa E741 np.testing.assert_array_almost_equal(u[ib.nodal_dofs[0]], -m.p[0], -10)
def test_trace(mtype, e1, e2): m = mtype().refined(3) # use the boundary where last coordinate is zero basis = FacetBasis( m, e1, facets=m.facets_satisfying(lambda x: x[x.shape[0] - 1] == 0.0)) xfun = projection(lambda x: x[0], InteriorBasis(m, e1)) nbasis, y = basis.trace(xfun, lambda p: p[0:(p.shape[0] - 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 = MeshTri() e = ElementTriArgyris() basis = InteriorBasis(m, e) all_dofs = basis.get_dofs() assert_allclose( all_dofs.keep(['u', 'u_n']).keep('u'), all_dofs.keep('u')) assert_allclose( all_dofs.drop(['u_x', 'u_y', 'u_xx', 'u_xy', 'u_yy', 'u_n']), all_dofs.keep('u')) assert_allclose(all_dofs, all_dofs.drop('does_not_exist')) assert_allclose(np.empty((0, ), dtype=np.int64), all_dofs.keep('does_not_exist'))
def runTest(self): m = MeshLine(np.linspace(0., 1.)) m.refine(2) e = ElementLineP1() ib = InteriorBasis(m, e) fb = FacetBasis(m, e) @linear_form def boundary_flux(v, dv, w): return -v * (w.x[0] == 1.) L = asm(laplace, ib) b = asm(boundary_flux, fb) D = m.nodes_satisfying(lambda x: x == 0.0) I = ib.complement_dofs(D) # noqa E741 u = solve(*condense(L, b, I=I)) # noqa E741 self.assertTrue(np.sum(np.abs(u + m.p[0, :])) < 1e-10)