def test_solving_inhomogeneous_laplace(mesh_elem, impose): """Adapted from example 14.""" mesh, elem = mesh_elem m = mesh().refined(4) basis = Basis(m, elem) boundary_basis = FacetBasis(m, 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(*impose(A, x=u, D=boundary_dofs)) @Functional def gradu(w): gradu = w['sol'].grad return dot(gradu, gradu) np.testing.assert_almost_equal(gradu.assemble(basis, sol=basis.interpolate(u)), 8 / 3, decimal=9)
def test_adaptive_splitting_3d_3(): # adaptively refine one face of a cube, check that the mesh parameter h # is approximately linear w.r.t to distance from the face m = MeshTet.init_tensor(np.linspace(0, 1, 3), np.linspace(0, 1, 3), np.linspace(0, 1, 3)) for itr in range(15): m = m.refined(m.f2t[0, m.facets_satisfying(lambda x: x[0] == 0)]) @LinearForm def hproj(v, w): return w.h * v basis = Basis(m, ElementTetP1()) h = projection(hproj, basis) funh = basis.interpolator(h) xs = np.vstack(( np.linspace(0, .5, 20), np.zeros(20) + .5, np.zeros(20) + .5, )) hs = funh(xs) assert np.max(np.abs(hs - xs[0])) < 0.063
def runTest(self): m = MeshQuad().refined(2) e = ElementQuad1() basis = Basis(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 = Basis(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): mtype, etype = self.case m = mtype().refined(3) e = etype() ib = Basis(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 test_coodata_inverse(m, e, edg): E = edg(e) basis = Basis(m, E) basisdg = Basis(m, E) M1 = mass.assemble(basis) M2 = mass.coo_data(basisdg) assert_array_almost_equal( np.linalg.inv(M1.toarray()), M2.inverse().tocsr().toarray(), )
def test_matrix_element_projection(m, e): E1 = ElementVector(e) E2 = ElementVector(ElementVector(e)) basis0 = Basis(m, E1) basis1 = basis0.with_element(E2) C = linear_stress() x = basis0.interpolate(np.random.random(basis0.N)) @LinearForm def proj(v, _): return ddot(C(sym_grad(x)), v) y = projection(proj, basis1, basis0)
def test_periodic_loading(m, mdgtype, e): def _sort(ix): # sort index arrays so that ix[0] matches return ix[np.argsort(np.sum(m.p, axis=0)[ix])] mp = mdgtype.periodic(m, _sort(m.nodes_satisfying(lambda x: x[0] == 0)), _sort(m.nodes_satisfying(lambda x: x[0] == 1))) basis = Basis(mp, e) A = laplace.assemble(basis) M = mass.assemble(basis) @LinearForm def linf(v, w): return np.sin(2. * np.pi * w.x[0]) * v f = linf.assemble(basis) x = solve(A + 1e-6 * M, f) def uexact(x): return (1. / (2. * np.pi)**2) * np.sin(2. * np.pi * x[0]) @Functional def func(w): uexact = (1. / (2. * np.pi)**2) * np.sin(2. * np.pi * w.x[0]) return (uexact - w['u'])**2 assert_almost_equal(func.assemble(basis, u=x), 0, decimal=5)
def runTest(self): m = self.mesh().refined(3) ib = Basis(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 = MeshTri() e = ElementTriP1() basis = Basis(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.)).refined(2) ib = Basis(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 = MeshTri() e = ElementTriP1() basis = Basis(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 test_periodic_mesh_assembly(m, mdgtype, etype, check1, check2): def _sort(ix): # sort index arrays so that ix[0] matches return ix[np.argsort(np.sum(m.p, axis=0)[ix])] mp = mdgtype.periodic(m, _sort(m.nodes_satisfying(check1)), _sort(m.nodes_satisfying(check2))) basis = Basis(mp, etype()) A = laplace.assemble(basis) f = unit_load.assemble(basis) D = basis.get_dofs() x = solve(*condense(A, f, D=D)) if m.dim() == 2: assert_almost_equal(x.max(), 0.125) else: assert_almost_equal(x.max(), 0.07389930610869411, decimal=3) assert not np.isnan(x).any()
def runTest(self): m = MeshLine(np.linspace(0., 1.)).refined(2).with_boundaries({ 'left': lambda x: x[0] == 0.0, 'right': lambda x: x[0] == 1.0, }) ib = Basis(m, self.e) 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 runTest(self): m = MeshHex() # check that these assemble to the same matrix ec = ElementHex1() * ElementHex1() * ElementHex1() ev = ElementVectorH1(ElementHex1()) basisc = Basis(m, ec) basisv = Basis(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 test_evaluate_functional(mtype, e, mtype2): m = mtype().refined(3) if mtype2 is not None: m = mtype2.from_mesh(m) basis = Basis(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): m = MeshLine(np.linspace(0., 1.)).refined(2) ib = Basis(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 test_multimesh_2(): m = MeshTri() m1 = MeshTri.init_refdom() m2 = MeshTri.init_refdom().scaled((-1., -1.)).translated(( 1., 1., )) M = m1 @ m2 E = [ElementTriP1(), ElementTriP1()] basis1 = list(map(Basis, M, E)) basis2 = Basis(m, ElementTriP1()) M1 = asm(mass, basis1) M2 = asm(mass, basis2) assert_array_almost_equal(M1.toarray(), M2.toarray())
def runTest(self): m = MeshTri().refined() e = ElementTriP1() basis = Basis(m, e) @BilinearForm def nonsym(u, v, w): return u.grad[0] * v @BilinearForm(nthreads=2) def threaded_nonsym(u, v, w): return u.grad[0] * v assert_almost_equal( nonsym.assemble(basis).toarray(), threaded_nonsym.assemble(basis).toarray(), )
def runTest(self): m = MeshTri() e = ElementTriP1() basis = Basis(m, e) self.interior_area = 1 @BilinearForm(dtype=np.complex64) def complexmass(u, v, w): return 1j * u * v @LinearForm(dtype=np.complex64) def complexfun(v, w): return 1j * v M = asm(complexmass, basis) f = asm(complexfun, basis) ones = np.ones(M.shape[1]) self.assertAlmostEqual(np.dot(ones, M @ ones), 1j * self.interior_area) self.assertAlmostEqual(np.dot(ones, f), 1j * self.interior_area)
def test_trilinear_form(m, e): basis = Basis(m, e) out = (TrilinearForm( lambda u, v, w, p: dot(w * grad(u), grad(v))).assemble(basis)) kp = np.random.rand(100, basis.N) # transform to a dense 3-tensor (slow) A = out.toarray() # # initialize a sparse tensor instead # import sparse # arr = sparse.COO(*out.astuple(), has_duplicates=True) opt1 = np.einsum('ijk,li->ljk', A, kp) for i in range(kp.shape[0]): opt2 = (BilinearForm( lambda u, v, p: dot(p['kp'] * grad(u), grad(v))).assemble( basis, kp=kp[i])) assert abs((opt1[i] - opt2).min()) < 1e-10 assert abs((opt1[i] - opt2).max()) < 1e-10
def create_basis(self, m): e = ElementQuad2() return Basis(m, e)
def create_basis(self, m): e = ElementLineMini() return Basis(m, e)
def create_basis(self, m): e = ElementTriHermite() return Basis(m, e)
def create_basis(self, m): e = ElementTetMini() return Basis(m, e, intorder=3)
def create_basis(self, m): e = ElementHexS2() return Basis(m, e)
mesh = from_file(Path(__file__).parent / 'meshes' / 'ex35.json') element = ElementTriP1() # permeability of vacuum mu0 = 1.25663706212e-6 # permittivity of vacuum eps0 = 8.8541878128e-12 # relative permittivity of polytetrafluoroethylene eps_ptfe = 2.1 # relative permittivity of fluorinated ethylene propylene eps_fep = 2.1 global_basis = Basis(mesh, element) inner_conductor_basis = Basis( mesh, element, elements=mesh.subdomains['inner_conductor']) outer_conductor_basis = Basis( mesh, element, elements=mesh.subdomains['outer_conductor']) inner_insulator_basis = Basis( mesh, element, elements=mesh.subdomains['inner_insulator']) outer_insulator_basis = Basis( mesh, element, elements=mesh.subdomains['outer_insulator']) inner_conductor_outer_surface_basis = FacetBasis( mesh, element, facets=mesh.boundaries['inner_conductor_outer_surface']) outer_conductor_inner_surface_basis = FacetBasis( mesh, element, facets=mesh.boundaries['outer_conductor_inner_surface']) dofs = {
def create_basis(self, m): e = ElementWedge1() return Basis(m, e)
def create_basis(self, m): e = ElementTriCCR() return Basis(m, e)
def create_basis(self, m): e = ElementTetP2() return Basis(m, e)