def test_2d_incomplete(n=32, tol=1E-10): '''[|]''' mesh = UnitSquareMesh(n, n) cell_f = MeshFunction('size_t', mesh, 2, 2) CompiledSubDomain('x[0] < 0.5+DOLFIN_EPS').mark(cell_f, 1) left = EmbeddedMesh(cell_f, 1) right = EmbeddedMesh(cell_f, 2) facet_f = MeshFunction('size_t', left, 1, 0) CompiledSubDomain('near(x[0], 0.0)').mark(facet_f, 1) CompiledSubDomain('near(x[0], 0.5)').mark(facet_f, 2) CompiledSubDomain('near(x[1], 0.0)').mark(facet_f, 3) CompiledSubDomain('near(x[1], 1.0)').mark(facet_f, 4) # More complicated iface iface = EmbeddedMesh(facet_f, (1, 2, 3, 4)) # Right will interact with it in a simpler way facet_f = MeshFunction('size_t', right, 1, 0) CompiledSubDomain('near(x[0], 0.5)').mark(facet_f, 2) # We want to embed mappings = iface.compute_embedding(facet_f, 2) # Is it correct? xi, x = iface.coordinates(), right.coordinates() assert min(np.linalg.norm(xi[list(mappings[0].keys())]-x[list(mappings[0].values())], 2, 1)) < tol tdim = right.topology().dim()-1 right.init(tdim, 0) f2v = right.topology()(tdim, 0) icells = iface.cells() vertex_match = lambda xs, ys: all(min(np.linalg.norm(ys - x, 2, 1)) < tol for x in xs) assert all([vertex_match(xi[icells[key]], x[f2v(val)]) for key, val in list(mappings[tdim].items())]) try: iface.compute_embedding(facet_f, 2) except ValueError: pass V = FunctionSpace(right, 'CG', 2) u = interpolate(Expression('x[1]*x[1]', degree=1), V) # FIXME: this passes but looks weird dx_ = Measure('dx', domain=iface, subdomain_data=iface.marking_function) assert abs(ii_assemble(Trace(u, iface, tag=2)*dx_(2)) - 1./3) < tol
def test_2d(n=32, tol=1E-10): '''[|]''' mesh = UnitSquareMesh(n, n) cell_f = MeshFunction('size_t', mesh, 2, 2) CompiledSubDomain('x[0] < 0.5+DOLFIN_EPS').mark(cell_f, 1) left = EmbeddedMesh(cell_f, 1) right = EmbeddedMesh(cell_f, 2) facet_f = MeshFunction('size_t', left, 1, 0) CompiledSubDomain('near(x[0], 0.5)').mark(facet_f, 1) iface = EmbeddedMesh(facet_f, 1) # Now suppose facet_f = MeshFunction('size_t', right, 1, 0) CompiledSubDomain('near(x[0], 0.5)').mark(facet_f, 2) # We want to embed mappings = iface.compute_embedding(facet_f, 2) # Is it correct? xi, x = iface.coordinates(), right.coordinates() assert min(np.linalg.norm(xi[list(mappings[0].keys())]-x[list(mappings[0].values())], 2, 1)) < tol tdim = right.topology().dim()-1 right.init(tdim, 0) f2v = right.topology()(tdim, 0) icells = iface.cells() vertex_match = lambda xs, ys: all(min(np.linalg.norm(ys - x, 2, 1)) < tol for x in xs) assert all([vertex_match(xi[icells[key]], x[f2v(val)]) for key, val in list(mappings[tdim].items())]) try: iface.compute_embedding(facet_f, 2) except ValueError: pass V = FunctionSpace(right, 'CG', 1) u = interpolate(Expression('x[1]', degree=1), V) dx_ = Measure('dx', domain=iface) assert abs(ii_assemble(Trace(u, iface)*dx_) - 0.5) < tol
def test_3d(n=4, tol=1E-10): '''[|]''' mesh = UnitCubeMesh(n, n, n) cell_f = MeshFunction('size_t', mesh, 3, 2) CompiledSubDomain('x[0] < 0.5+DOLFIN_EPS').mark(cell_f, 1) left = EmbeddedMesh(cell_f, 1) facet_f = MeshFunction('size_t', left, 2, 0) CompiledSubDomain('near(x[0], 0.5)').mark(facet_f, 1) iface = EmbeddedMesh(facet_f, 1) # We want to embed mappings = iface.parent_entity_map[left.id()] # Is it correct? xi, x = iface.coordinates(), left.coordinates() assert min( np.linalg.norm( xi[list(mappings[0].keys())] - x[list(mappings[0].values())], 2, 1)) < tol tdim = left.topology().dim() - 1 left.init(tdim, 0) f2v = left.topology()(tdim, 0) icells = iface.cells() vertex_match = lambda xs, ys: all( min(np.linalg.norm(ys - x, 2, 1)) < tol for x in xs) assert all([ vertex_match(xi[icells[key]], x[f2v(val)]) for key, val in list(mappings[tdim].items()) ]) V = FunctionSpace(left, 'CG', 1) u = interpolate(Expression('x[0] + x[1]', degree=1), V) dx_ = Measure('dx', domain=iface) assert abs(ii_assemble(Trace(u, iface) * dx_) - 1.0) < tol
def test_2d_enclosed(n=32, tol=1E-10): ''' |----| | [] | |----| ''' mesh = UnitSquareMesh(n, n) # Lets get the outer part cell_f = MeshFunction('size_t', mesh, 2, 1) inside = '(x[0] > 0.25-DOLFIN_EPS) && (x[0] < 0.75+DOLFIN_EPS) && (x[1] > 0.25-DOLFIN_EPS) && (x[1] < 0.75+DOLFIN_EPS)' CompiledSubDomain(inside).mark(cell_f, 2) # Stokes --- mesh1 = EmbeddedMesh(cell_f, 1) bdries1 = MeshFunction('size_t', mesh1, mesh1.topology().dim()-1, 0) CompiledSubDomain('near(x[0], 0)').mark(bdries1, 10) CompiledSubDomain('near(x[0], 1)').mark(bdries1, 20) CompiledSubDomain('near(x[1], 0)').mark(bdries1, 30) CompiledSubDomain('near(x[1], 1)').mark(bdries1, 40) CompiledSubDomain('near(x[0], 0.25) && ((x[1] > 0.25-DOLFIN_EPS) && (x[1] < 0.75+DOLFIN_EPS))').mark(bdries1, 1) CompiledSubDomain('near(x[0], 0.75) && ((x[1] > 0.25-DOLFIN_EPS) && (x[1] < 0.75+DOLFIN_EPS))').mark(bdries1, 2) CompiledSubDomain('near(x[1], 0.25) && ((x[0] > 0.25-DOLFIN_EPS) && (x[0] < 0.75+DOLFIN_EPS))').mark(bdries1, 3) CompiledSubDomain('near(x[1], 0.75) && ((x[0] > 0.25-DOLFIN_EPS) && (x[0] < 0.75+DOLFIN_EPS))').mark(bdries1, 4) # Darcy --- mesh2 = EmbeddedMesh(cell_f, 2) bdries2 = MeshFunction('size_t', mesh2, mesh2.topology().dim()-1, 0) CompiledSubDomain('near(x[0], 0.25) && ((x[1] > 0.25-DOLFIN_EPS) && (x[1] < 0.75+DOLFIN_EPS))').mark(bdries2, 1) CompiledSubDomain('near(x[0], 0.75) && ((x[1] > 0.25-DOLFIN_EPS) && (x[1] < 0.75+DOLFIN_EPS))').mark(bdries2, 2) CompiledSubDomain('near(x[1], 0.25) && ((x[0] > 0.25-DOLFIN_EPS) && (x[0] < 0.75+DOLFIN_EPS))').mark(bdries2, 3) CompiledSubDomain('near(x[1], 0.75) && ((x[0] > 0.25-DOLFIN_EPS) && (x[0] < 0.75+DOLFIN_EPS))').mark(bdries2, 4) # ----------------- # And interface bmesh = EmbeddedMesh(bdries2, (1, 2, 3, 4)) # Embedded it viewwed from stokes mappings = bmesh.compute_embedding(bdries1, (1, 2, 3, 4)) xi, x = bmesh.coordinates(), mesh1.coordinates() assert min(np.linalg.norm(xi[list(mappings[0].keys())]-x[list(mappings[0].values())], 2, 1)) < tol tdim = mesh1.topology().dim()-1 mesh1.init(tdim, 0) f2v = mesh1.topology()(tdim, 0) icells = bmesh.cells() vertex_match = lambda xs, ys: all(min(np.linalg.norm(ys - x, 2, 1)) < tol for x in xs) assert all([vertex_match(xi[icells[key]], x[f2v(val)]) for key, val in list(mappings[tdim].items())]) try: bmesh.compute_embedding(bdries1, 2) except ValueError: pass V = VectorFunctionSpace(mesh1, 'CG', 1) u = interpolate(Expression(('x[1]', 'x[0]'), degree=1), V) dx_ = Measure('dx', domain=bmesh) n_ = OuterNormal(bmesh, [0.5, 0.5]) # Because it is divergence free assert abs(ii_assemble(dot(n_, Trace(u, bmesh))*dx_)) < tol
facet_f = MeshFunction('size_t', aux_mesh, 1, 0) DomainBoundary().mark(facet_f, 1) left.mark(facet_f, 2) right.mark(facet_f, 2) # Extending from gamma_mesh = StraightLineMesh(np.array([0.5, 0]), np.array([0.5, 1]), 3*ny) V1 = FunctionSpace(gamma_mesh, 'CG', 1) f = interpolate(Constant(1), V1) # The extension problem would be V2 = FunctionSpace(aux_mesh, 'CG', 1) u2, v2 = TrialFunction(V2), TestFunction(V2) a = inner(grad(u2), grad(v2))*dx + inner(u2, v2)*dx L = inner(f, Trace(v2, gamma_mesh))*dx(domain=gamma_mesh) A, b = list(map(ii_assemble, (a, L))) # We have boundary conditions to apply # bc = DirichletBC(V2, Constant(0), facet_f, 1) # A, b = apply_bc(A, b, bc) u2h = Function(V2) solve(A, u2h.vector(), b) # We get to extend domain by p, q = TrialFunction(Q), TestFunction(Q) f = Trace(u2h, ext_mesh) a = inner(p, q)*dx
# Let's check som functional => match the hand computed value while # the value of the functional is also > 0 def test(a, b): assert a < 1E-14 and b > 0, (a, b) return True mesh = UnitSquareMesh(10, 10) bmesh = BoundaryMesh(mesh, 'exterior') V = FunctionSpace(mesh, 'CG', 1) Q = FunctionSpace(bmesh, 'CG', 1) W = [V, Q] u, p = interpolate(Constant(2), V), interpolate(Constant(1), Q) v, q = list(map(TestFunction, W)) Tu, Tv = (Trace(x, bmesh) for x in (u, v)) # NOTE in the tests below `is_linear` assigns to u making it nonzero # so forms where based on u = Function(V) you expect zero are nz. # Also, since the assignment is rand this looks differently every time dxGamma = Measure('dx', domain=bmesh) # These should be linear L = inner(Tu, q) * dxGamma assert is_linear(L, Function(V)) == None test(*is_linear(L, u)) # --------------------------------------------------------------- L = inner(Tu + Tu, q) * dxGamma test(*is_linear(L, u))
f = MeshFunction('size_t', Emesh, 2, 0) CompiledSubDomain('!(near(x[2], 0) || near(x[2], 1))').mark(f, 1) # Mesh to extend to Emesh = SubMesh(Emesh, f, 1) # We look first at integrals (Ev1d, p)*dxLM so coupling to the multiplier V3d = FunctionSpace(mesh, 'CG', 1) V1d = FunctionSpace(Vmesh, 'CG', 1) Q = FunctionSpace(Emesh, 'CG', 1) W = [V3d, V1d, Q] u3d, u1d, p = list(map(TrialFunction, W)) v3d, v1d, q = list(map(TestFunction, W)) Tu3d, Tv3d = (Trace(f, Emesh) for f in (u3d, v3d)) Eu1d, Ev1d = (Extension(f, Emesh, type='uniform') for f in (u1d, v1d)) # Cell integral of Qspace dxLM = Measure('dx', domain=Emesh) a = inner(Ev1d, p) * dxLM A = ii_assemble(a) # Let's use the matrix to perform the following integral p_expr = Expression('x[0]-2*x[1]+3*x[2]', degree=1) # Something which can be extended exactly v_expr = Expression('2*x[2]', degree=1) true = assemble(inner(p_expr, v_expr) * dxLM) p_func = interpolate(p_expr, Q)
off_middle.mark(facet_f, 2) # Mesh to extend from, (so the d1) mesh_1d = StraightLineMesh(np.array([0.5, 0]), np.array([0.5, 1]), n) # WORKS: EmbeddedMesh(facet_f, 1) # And thhe multiplier one mesh_lm = EmbeddedMesh(facet_f, 2) V_2d = FunctionSpace(mesh, 'CG', 1) V_1d = FunctionSpace(mesh_1d, 'CG', 1) Q = FunctionSpace(mesh_lm, 'CG', 1) # We are after (Trace(V_2d) - Ext(V_1d), Q) q = TestFunction(Q) u2d, u1d = TrialFunction(V_2d), TrialFunction(V_1d) Tu2d = Trace(u2d, mesh_lm) Eu1d = Extension(u1d, mesh_lm, type='uniform') dxLM = Measure('dx', domain=mesh_lm) T = ii_assemble(inner(Tu2d, q) * dxLM) E = ii_assemble(inner(q, Eu1d) * dxLM) f2d = Expression('1+x[0]+2*x[1]', degree=1) # The extended function will practically be shifted so I want invariance # in that case f1d = Expression('2-x[1]', degree=1) # LM is free fLM = Expression('4-2*x[1]+x[0]', degree=1) # What we are after