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_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
A, B = (nx-1)/2./nx, (nx+1)/2./nx mesh = UnitSquareMesh(nx, ny) cell_f = MeshFunction('size_t', mesh, 2, 0) CompiledSubDomain('x[0] > A - tol && x[0] < B + tol', A=A, B=B, tol=DOLFIN_EPS).mark(cell_f, 1) left = CompiledSubDomain('A-tol < x[0] && x[0] < A+tol', A=A, tol=DOLFIN_EPS) right = CompiledSubDomain('A-tol < x[0] && x[0] < A+tol', A=B, tol=DOLFIN_EPS) # We would be extending to facet_f = MeshFunction('size_t', mesh, 1, 0) left.mark(facet_f, 1) right.mark(facet_f, 1) ext_mesh = EmbeddedMesh(facet_f, 1) Q = FunctionSpace(ext_mesh, 'CG', 1) # The auxiliary problem would be speced at aux_mesh = SubMesh(mesh, cell_f, 1) 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
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
# On to next cell return PETScMatrix(mat) # -------------------------------------------------------------------- if __name__ == '__main__': from dolfin import * from xii import EmbeddedMesh mesh = UnitCubeMesh(10, 10, 10) f = EdgeFunction('size_t', mesh, 0) CompiledSubDomain('near(x[0], 0.5) && near(x[1], 0.5)').mark(f, 1) bmesh = EmbeddedMesh(f, 1) # Trace V = FunctionSpace(mesh, 'CG', 2) TV = FunctionSpace(bmesh, 'DG', 1) f = interpolate(Expression('x[0]+x[1]+x[2]', degree=1), V) Tf0 = interpolate(f, TV) Trace = avg_mat(V, TV, bmesh, {'radius': None, 'surface': 'cylinder'}) Tf = Function(TV) Trace.mult(f.vector(), Tf.vector()) Tf0.vector().axpy(-1, Tf.vector()) print '??', Tf0.vector().norm('linf') V = VectorFunctionSpace(mesh, 'CG', 2)
from dolfin import * from xii import OuterNormal, EmbeddedMesh import numpy as np mesh = UnitSquareMesh(10, 10) f = MeshFunction('size_t', mesh, 1, 0) CompiledSubDomain('near(x[1], 0)').mark(f, 1) CompiledSubDomain('near(x[1], 1)').mark(f, 1) bmesh = EmbeddedMesh(f, 1) n = OuterNormal(bmesh, [0.5, 0.5]) # Top? for x in bmesh.coordinates(): if near(x[1], 1.0): assert np.linalg.norm(n(x) - np.array([0, 1.])) < 1E-13 # Bottom for x in bmesh.coordinates(): if near(x[1], 0.0): assert np.linalg.norm(n(x) - np.array([0, -1.])) < 1E-13
# # Attache the mapiing of cut_mesh cells to facets ... # cut_mesh.facet_map = sc2bf[cut_mesh.data().array('parent_cell_indices', fdim)] # return cut_mesh # -------------------------------------------------------------------- if __name__ == '__main__': from dolfin import * from xii import EmbeddedMesh bckg = UnitSquareMesh(32, 20) # Make the cut as x = 0.5 mesh = RectangleMesh(Point(0.5, 0), Point(1, 1), 5, 100) facet_f = MeshFunction('size_t', mesh, 1, 0) CompiledSubDomain('near(x[0], 0.5)').mark(facet_f, 1) cut = EmbeddedMesh(facet_f, 1) #print point_cloud(cut, bckg) f = CutSurfaceMesh(bckg, cut) #print f.num_cells() # File('foo.pvd') << f # mapping = f.facet_map # # Sanity of the map # #assert all(cell.midpoint().distance(Facet(bckg, mapping[i]).midpoint()) < 1E-13 # # for i, cell in enumerate(cells(f)))
def immersed_geometry(i, which): ''' We discretize the geometrical setup where inner domain [0.25, 0.75]^2 is enclosed by the outer domain such that their union fills a unit square. Return mesh and (coupling relevant marking function) ''' # The surfaces are tagged as follows # Outer Inner # ^ # 8 # v ^ # 4 4 # <5 1> <2 6> <1 2> # 3 3 # ^ v # 7 # v n = 4*2**i mesh = df.UnitSquareMesh(n, n) inner = df.CompiledSubDomain(' && '.join(['(x[0] < 0.75+DOLFIN_EPS)', '(0.25-DOLFIN_EPS < x[0])', '(x[1] < 0.75+DOLFIN_EPS)', '(0.25-DOLFIN_EPS < x[1])'])) cell_f = df.MeshFunction('size_t', mesh, 2, 1) inner.mark(cell_f, 2) outer_domain = EmbeddedMesh(cell_f, 1) outer_ff = df.MeshFunction('size_t', outer_domain, 1, 0) outer_boundaries = { 1: df.CompiledSubDomain('near(x[0], 0.25) && ((0.25-DOLFIN_EPS < x[1]) && (x[1] < 0.75+DOLFIN_EPS))'), 2: df.CompiledSubDomain('near(x[0], 0.75) && ((0.25-DOLFIN_EPS < x[1]) && (x[1] < 0.75+DOLFIN_EPS))'), 3: df.CompiledSubDomain('near(x[1], 0.25) && ((0.25-DOLFIN_EPS < x[0]) && (x[0] < 0.75+DOLFIN_EPS))'), 4: df.CompiledSubDomain('near(x[1], 0.75) && ((0.25-DOLFIN_EPS < x[0]) && (x[0] < 0.75+DOLFIN_EPS))'), 5: df.CompiledSubDomain('near(x[0], 0.0)'), 6: df.CompiledSubDomain('near(x[0], 1.0)'), 7: df.CompiledSubDomain('near(x[1], 0.0)'), 8: df.CompiledSubDomain('near(x[1], 1.0)')} [bdry.mark(outer_ff, tag) for tag, bdry in outer_boundaries.items()] inner_domain = EmbeddedMesh(cell_f, 2) inner_ff = df.MeshFunction('size_t', inner_domain, 1, 0) inner_boundaries = { 1: df.CompiledSubDomain('near(x[0], 0.25) && ((0.25-DOLFIN_EPS < x[1]) && (x[1] < 0.75+DOLFIN_EPS))'), 2: df.CompiledSubDomain('near(x[0], 0.75) && ((0.25-DOLFIN_EPS < x[1]) && (x[1] < 0.75+DOLFIN_EPS))'), 3: df.CompiledSubDomain('near(x[1], 0.25) && ((0.25-DOLFIN_EPS < x[0]) && (x[0] < 0.75+DOLFIN_EPS))'), 4: df.CompiledSubDomain('near(x[1], 0.75) && ((0.25-DOLFIN_EPS < x[0]) && (x[0] < 0.75+DOLFIN_EPS))') } [bdry.mark(inner_ff, tag) for tag, bdry in inner_boundaries.items()] if which == 'outer': return (outer_domain, outer_ff) if which == 'inner': return (inner_domain, inner_ff) assert which == 'interface' imesh = EmbeddedMesh(inner_ff, (1, 2, 3, 4)) return (imesh, imesh.marking_function)
# ------------------------------------------------------------------- if __name__ == '__main__': from dolfin import (CompiledSubDomain, DomainBoundary, SubsetIterator, UnitSquareMesh, Facet) from xii import EmbeddedMesh mesh = UnitSquareMesh(4, 4) surfaces = MeshFunction('size_t', mesh, 2, 0) CompiledSubDomain('x[0] > 0.5-DOLFIN_EPS').mark(surfaces, 1) # What should be trasfered f = MeshFunction('size_t', mesh, 1, 0) DomainBoundary().mark(f, 1) CompiledSubDomain('near(x[0], 0.5)').mark(f, 1) # Assign funky colors for i, e in enumerate(SubsetIterator(f, 1), 1): f[e] = i ch_mesh = EmbeddedMesh(surfaces, 1) ch_f = transfer_markers(ch_mesh, f) # Every color in child is found in parent and we get the midpoint right p_values, ch_values = list(f.array()), list(ch_f.array()) for ch_value in set(ch_f.array()) - set((0, )): assert ch_value in p_values x = Facet(mesh, p_values.index(ch_value)).midpoint() y = Facet(ch_mesh, ch_values.index(ch_value)).midpoint() assert x.distance(y) < 1E-13
from dolfin import * from xii.assembler.trace_matrix import trace_mat from xii import EmbeddedMesh mesh = UnitSquareMesh(10, 10) bdry = FacetFunction('size_t', mesh, 0) DomainBoundary().mark(bdry, 1) bmesh = EmbeddedMesh(bdry, 1) V = FunctionSpace(mesh, 'CG', 2) TV = FunctionSpace(bmesh, 'CG', 2) Trace = trace_mat(V, TV, bmesh, {'restriction': '', 'normal': None}) f = Expression('sin(pi*(x[0]+x[1]))', degree=3) v = interpolate(f, V) Tv0 = interpolate(f, TV) Tv = Function(TV) Trace.mult(v.vector(), Tv.vector()) Tv0.vector().axpy(-1, Tv.vector()) print Tv0.vector().norm('linf') V = VectorFunctionSpace(mesh, 'CG', 2) TV = VectorFunctionSpace(bmesh, 'CG', 2) Trace = trace_mat(V, TV, bmesh, {'restriction': '', 'normal': None})
positions = [0, 60, 120, 180, 240, 300] cylinder_noslip_tag = 4 first_jet = cylinder_noslip_tag + 1 njets = len(positions) width = 10 radius = 10 tagged_positions = zip(range(first_jet, first_jet + njets), positions) for tag, theta0 in tagged_positions: tag, theta0 = tagged_positions[0] cylinder = EmbeddedMesh(surfaces, markers=[tag]) x, y = SpatialCoordinate(cylinder) # cylinder_surfaces = cylinder.marking_function V = VectorFunctionSpace(cylinder, 'CG', 2) v = JetBCValue(radius, width, theta0, Q=tag, degree=5) v.Q = 2 * tag f = interpolate(v, V) # Outer normal of the cylinder n = as_vector((x, y)) / Constant(radius) print theta0, abs(assemble(dot(v, n) * dx) - 2 * tag), assemble(
# We will have the 1d guy live in the middle and the multiplier slighly # off middle = CompiledSubDomain('near(x[0], 0.5)') off_middle = CompiledSubDomain('near(x[0], A) or near(x[0], B)', A=(n / 2 + 1.) / n, B=(n / 2 - 1.) / n) facet_f = MeshFunction('size_t', mesh, 1, 0) middle.mark(facet_f, 1) 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)
from dolfin import * from xii import EmbeddedMesh from xii.assembler.extension_matrix import uniform_extension_matrix from scipy.linalg import svdvals import numpy as np n = 8 mesh = UnitCubeMesh(n, n, n) f = MeshFunction('size_t', mesh, 1, 0) CompiledSubDomain('near(x[0], 0.5) && near(x[1], 0.5)').mark(f, 1) # Mesh to extend from Vmesh = EmbeddedMesh(f, 1) # Get the tube as submesh of the full cube boundary Emesh = BoundaryMesh(mesh, 'exterior') f = MeshFunction('size_t', Emesh, 2, 1) CompiledSubDomain('near(x[2], 0) || near(x[2], 1)').mark(f, 2) # Mesh to extend to Emesh = SubMesh(Emesh, f, 1) # Check scalar V = FunctionSpace(Vmesh, 'CG', 1) EV = FunctionSpace(Emesh, 'CG', 1) E = uniform_extension_matrix(V, EV) f = Expression('x[2]', degree=1)
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_color(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) # And interface bmesh = EmbeddedMesh(bdries1, (1, 2, 3, 4)) # Embedded it viewwed from stokes mappings = bmesh.parent_entity_map[mesh1.id()] 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()) ]) # Now color specific cf = bmesh.marking_function assert set(cf.array()) == set((1, 2, 3, 4)) assert all(bdries1[v] == cf[k] for k, v in list(mappings[tdim].items()))