Esempio n. 1
0
def test_2d_cell(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)

    # We want to embed
    mappings = left.parent_entity_map[mesh.id()]
    # Is it correct?

    xi, x = left.coordinates(), mesh.coordinates()
    assert min(
        np.linalg.norm(
            xi[list(mappings[0].keys())] - x[list(mappings[0].values())], 2,
            1)) < tol

    tdim = mesh.topology().dim()
    c2v = mesh.topology()(tdim, 0)

    icells = left.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[c2v(val)])
        for key, val in list(mappings[tdim].items())
    ])
Esempio n. 2
0
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
Esempio n. 3
0
def test_3d_performance(n=4, tol=1E-10, strict=False):
    '''[|]'''
    mesh = UnitCubeMesh(n, n, n)
    bdries1 = MeshFunction('size_t', mesh, 2, 0)
    DomainBoundary().mark(bdries1, 1)
    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, 1)
    CompiledSubDomain(
        'near(x[1], 0.25) && ((x[0] > 0.25-DOLFIN_EPS) && (x[0] < 0.75+DOLFIN_EPS))'
    ).mark(bdries1, 1)
    CompiledSubDomain(
        'near(x[1], 0.75) && ((x[0] > 0.25-DOLFIN_EPS) && (x[0] < 0.75+DOLFIN_EPS))'
    ).mark(bdries1, 1)

    t = Timer('embed')
    iface = EmbeddedMesh(bdries1, 1)
    dt = t.stop()

    if strict:
        # We want to embed
        mappings = iface.parent_entity_map[mesh.id()]
        # Is it correct?

        xi, x = iface.coordinates(), mesh.coordinates()
        assert min(
            np.linalg.norm(
                xi[list(mappings[0].keys())] - x[list(mappings[0].values())],
                2, 1)) < tol

        tdim = mesh.topology().dim() - 1
        mesh.init(tdim, 0)
        f2v = mesh.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())
        ])

    return (dt, mesh.num_cells(), iface.num_cells())
Esempio n. 4
0
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
Esempio n. 5
0
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
Esempio n. 6
0
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
Esempio n. 7
0
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()))