Beispiel #1
0
 def map(self, x, y):
     if df.near(x[0], 1) and df.near(x[1], 1):
         y[0] = x[0] - 1.
         y[1] = x[1] - 1.
     elif df.near(x[0], 1):
         y[0] = x[0] - 1.
         y[1] = x[1]
     else:   # near(x[1], 1)
         y[0] = x[0]
         y[1] = x[1] - 1.
def test_pointsource_matrix_second_constructor(mesh, point):
    """Tests point source when given different constructor PointSource(V1,
    V2, point, mag) with a matrix and when placed at a node for 1D, 2D
    and 3D. Global points given to constructor from rank 0
    processor. Currently only implemented if V1=V2.

    """

    V1 = FunctionSpace(mesh, "CG", 1)
    V2 = FunctionSpace(mesh, "CG", 1)

    rank = MPI.rank(mesh.mpi_comm())
    u, v = TrialFunction(V1), TestFunction(V2)
    w = Function(V1)
    A = assemble(Constant(0.0)*u*v*dx)
    if rank == 0:
        ps = PointSource(V1, V2, point, 10.0)
    else:
        ps = PointSource(V1, V2, [])
    ps.apply(A)

    # Checks array sums to correct value
    a_sum = MPI.sum(mesh.mpi_comm(), np.sum(A.array()))
    assert round(a_sum - 10.0) == 0

    # Checks point source is added to correct part of the array
    A.get_diagonal(w.vector())
    v2d = vertex_to_dof_map(V1)
    for v in vertices(mesh):
        if near(v.midpoint().distance(point), 0.0):
            ind = v2d[v.index()]
            if ind < len(A.array()):
                assert np.round(w.vector()[ind] - 10.0) == 0
def test_pointsource_vector_fs(mesh, point):
    """Tests point source when given constructor PointSource(V, point,
    mag) with a vector for a vector function space that isn't placed
    at a node for 1D, 2D and 3D. Global points given to constructor
    from rank 0 processor.

    """

    rank = MPI.rank(mesh.mpi_comm())
    V = VectorFunctionSpace(mesh, "CG", 1)
    v = TestFunction(V)
    b = assemble(dot(Constant([0.0]*mesh.geometry().dim()), v)*dx)
    if rank == 0:
        ps = PointSource(V, point, 10.0)
    else:
        ps = PointSource(V, [])
    ps.apply(b)

    # Checks array sums to correct value
    b_sum = b.sum()
    assert round(b_sum - 10.0*V.num_sub_spaces()) == 0

    # Checks point source is added to correct part of the array
    v2d = vertex_to_dof_map(V)
    for v in vertices(mesh):
        if near(v.midpoint().distance(point), 0.0):
            for spc_idx in range(V.num_sub_spaces()):
                ind = v2d[v.index()*V.num_sub_spaces() + spc_idx]
                if ind < len(b.get_local()):
                    assert np.round(b.get_local()[ind] - 10.0) == 0
Beispiel #4
0
# If we want a scalar function with twice
# the values of f1 (THIS WORKS)
vec = df.assemble(df.dot(2 * f1, df.TestFunction(space_S1)) * df.dP)
f2.vector().axpy(1, vec)

# Now we want to modify the vector function space
# to get the vector (2, 1, 1) in every vertex
vec = df.assemble(df.dot(df.as_vector((2 * f1, f1, f1)), df.TestFunction(space_S3)) * df.dP)
f3.vector().axpy(1, vec)

# Scalar space assign
g2 = df.Function(space_S1)
g2.assign(df.FunctionAXPY(f1, 2.))

g2.vector().axpy(-1, f2.vector())
assert df.near(g2.vector().norm('l2'), 0)

# Assigner for components of the vector space
S3_assigners = [df.FunctionAssigner(space_S3.sub(i), space_S1) for i in range(space_S3.num_sub_spaces())]

g3 = df.Function(space_S3)
# Assign to components
comps = [f2, f1, f1]
[S3_assigners[i].assign(g3.sub(i), comp) for i, comp in enumerate(comps)]

g3.vector().axpy(-1, f3.vector())
assert df.near(g3.vector().norm('l2'), 0)


df.plot(f2)
df.plot(f3)
Beispiel #5
0
        n0, dt0 = n, dt
        
    # Check creation
    mesh = df.UnitCubeMesh(10, 10, 10)

    f = df.MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0)
    chi = df.CompiledSubDomain('near(x[i], 0.5)', i=0) 
    for i in range(3):
        chi.i=i
        chi.mark(f, i+1)

    mesh = EmbeddedMesh(f, [1, 2, 3])

    volume = lambda c: df.Cell(mesh, c.index()).volume()
    
    assert df.near(sum(volume(c) for c in df.SubsetIterator(mesh.marking_function, 1)), 1, 1E-10)
    assert df.near(sum(volume(c) for c in df.SubsetIterator(mesh.marking_function, 2)), 1, 1E-10)
    assert df.near(sum(volume(c) for c in df.SubsetIterator(mesh.marking_function, 3)), 1, 1E-10)

    # Check normla computation
    mesh = df.UnitCubeMesh(10, 10, 10)
    bmesh = df.BoundaryMesh(mesh, 'exterior')

    n = OuterNormal(bmesh, [0.5, 0.5, 0.5])

    for cell in df.cells(bmesh):
        x = cell.midpoint().array()
        if df.near(x[0], 0):
            assert np.linalg.norm(n(x) - np.array([-1, 0, 0])) < 1E-10
        elif df.near(x[0], 1.):
            assert np.linalg.norm(n(x) - np.array([1, 0, 0])) < 1E-10
 def inside(self, x, on_boundary):
     return on_boundary and (dolfin.near(x[1], y0) or dolfin.near(x[1], y1))
Beispiel #7
0
def mortar_meshes(subdomains, markers, ifacet_iter=None, strict=True, tol=1E-14):
    '''
    Let subdomains a cell function. We assume that domains (cells) marked 
    with the given markers are adjecent and an interface can be defined 
    between these domains which is a continuous curve. Then for each 
    domain we create a (sub)mesh and a single interface mesh which holds 
    a connectivity map of its cells to facets of the submeshes. The submeshes 
    are returned as a list. The connectivity map is 
    of the form submesh.id -> facets. The marking function f of the EmbeddedMesh
    that is the interface is colored such that f[color] is the interface 
    of meshes (submeshes[m] for m color_map[color]).
    '''
    assert len(markers) > 1
    # Need a cell function
    mesh = subdomains.mesh()
    tdim = mesh.topology().dim()
    assert subdomains.dim() == tdim

    markers = list(markers)
    # For each facet we want to know which 2 cells share it
    tagged_iface = defaultdict(dict)

    if ifacet_iter is None:
        mesh.init(tdim-1)
        ifacet_iter = df.facets(mesh)
    
    mesh.init(tdim-1, tdim)
    for facet in ifacet_iter:
        cells = map(int, facet.entities(tdim))

        if len(cells) > 1:
            c0, c1 = cells
            tag0, tag1 = subdomains[c0], subdomains[c1]
            if tag0 != tag1 and tag0 in markers and tag1 in markers:
                # A key of sorted tags
                if tag0 < tag1:
                    key = (tag0, tag1)
                    # The cells connected to facet order to match to tags
                    value = (c0, c1)
                else:
                    key = (tag1, tag0)
                    value = (c1, c0)
                # A facet to 2 cells map for the facets of tagged pair
                tagged_iface[key][facet.index()] = value

    # order -> tagged keys
    color_to_tag_map = tagged_iface.keys()
    # Set to color which won't be encounred
    interface = df.MeshFunction('size_t', mesh, tdim-1, len(color_to_tag_map))
    values = interface.array()

    # Mark facets corresponding to tagged pair by a color
    for color, tags in enumerate(color_to_tag_map):
        values[tagged_iface[tags].keys()] = color

    # Finally create an interface mesh for all the colors
    interface_mesh = EmbeddedMesh(interface, range(len(color_to_tag_map)))

    # Try to recogninze the meshes which violates assumptions by counting
    assert not strict or is_continuous(interface_mesh)
    
    # And subdomain mesh for each marker
    subdomain_meshes = {tag: EmbeddedMesh(subdomains, tag) for tag in markers}

    # Alloc the entity maps for the embedded mesh
    interface_map = {subdomain_meshes[tag].id(): [None]*interface_mesh.num_cells()
                     for tag in markers}
    
    # THe maps are filled by the following idea. Using marking function
    # of interface mesh one cat get cells of that color and useing entity
    # map for (original) mesh map the cells to mesh facet. A color also
    # corresponds to a pair of tags which identifies the two meshes which
    # share the facet - facet connected to 2 cells one for each mesh. The
    # final step is to lean to map submesh cells to mesh cells

    # local submesh <- global of parent mesh
    sub_mesh_map = lambda tag: dict(
        (mesh_c, submesh_c)
        for submesh_c, mesh_c in
        enumerate(subdomain_meshes[tag].parent_entity_map[mesh.id()][tdim])
    )

    # Thec cell-cell connectivity of each submesh
    c2c = {tag: sub_mesh_map(tag) for tag in markers}
    # A connectivity of interface mesh cells to facets of global mesh
    c2f = interface_mesh.parent_entity_map[mesh.id()][tdim-1]

    for color, tags in enumerate(color_to_tag_map):
        # Precompute for the 2 tags
        submeshes = [subdomain_meshes[tag] for tag in tags]
        
        for cell in df.SubsetIterator(interface_mesh.marking_function, color):
            cell_index = cell.index()
            # The corresponding global cell facet
            facet = c2f[cell_index]
            # The two cells in global mesh numbering
            global_cells = tagged_iface[tags][facet]
            # Let's find the facet in submesh
            for tag, gc, submesh in zip(tags, global_cells, submeshes):
                # The map uses local cell
                local_cell = c2c[tag][gc]
                mesh_id = submesh.id()
                
                found = False
                for submesh_facet in df.facets(df.Cell(submesh, local_cell)):
                    found = df.near(cell.midpoint().distance(submesh_facet.midpoint()), 0, tol)
                    if found:
                        interface_map[mesh_id][cell_index] = submesh_facet.index()
                        break

    # Collapse to list; I want list indexing
    subdomain_meshes = np.array([subdomain_meshes[m] for m in markers]) 
    color_map = [map(markers.index, tags) for tags in color_to_tag_map]

    # Parent in the sense that the colored piece of interface
    # could have been created from mesh
    interface_mesh.parent_entity_map.update(
        dict((k, {tdim-1: v}) for k, v in interface_map.items())
    )

    return subdomain_meshes, interface_mesh, color_map
Beispiel #8
0
 def inside(self, x, on_boundary):
     return on_boundary and near(x[0], x_min)
Beispiel #9
0
def left(x, on_boundary):
    return on_boundary and near(x[0], 0.0)
def left(x, on_boundary): return on_boundary and x[1] < LAB(x) and near(x[0], 0.0)

def right(x, on_boundary): return on_boundary and x[1] < LAB(x) and near(x[0], MeshWidth)
Beispiel #11
0
def top(x, on_boundary):
    return on_boundary and near(x[2], mesh_height)
def top(x, on_boundary): return on_boundary and near(x[1], MeshHeight)

def bottom(x, on_boundary): return on_boundary and near(x[1], 0.0)
def bottom(x, on_boundary): return on_boundary and near(x[1], 0.0)

def left(x, on_boundary): return on_boundary and x[1] < LAB(x) and near(x[0], 0.0)
Beispiel #14
0
def front(x, on_boundary):
    return on_boundary and near(x[1], 0.0)
Beispiel #15
0
def back(x, on_boundary):
    return on_boundary and near(x[1], mesh_width)
Beispiel #16
0
def right(x, on_boundary):
    return on_boundary and near(x[0], mesh_width)
Beispiel #17
0
 def inside(s, x, on_boundary):
   return on_boundary and d.near(x[0], self.L)
def right(x, on_boundary): return on_boundary and x[1] < LAB(x) and near(x[0], MeshWidth)

def RunJob(Tb, mu_value, path):
Beispiel #19
0
 def inside(self, x, on_boundary):
     # return True if on left or bottom boundary AND NOT on one of the two corners (0, 1) and (1, 0)
     return bool((df.near(x[0], 0) or df.near(x[1], 0)) and
             (not ((df.near(x[0], 0) and df.near(x[1], 1)) or
                     (df.near(x[0], 1) and df.near(x[1], 0)))) and on_boundary)
 def inside(self, x, on_boundary):
     return on_boundary and dolfin.near(x[0], 0.0)
Beispiel #21
0
H = 0.41
L = 2.2
D = 0.1
center = 0.2
cases = {
      1: {'Um': 0.3,
          'Re': 20.0},
      
      2: {'Um': 1.5,
          'Re': 100.0}
      }

# Specify boundary conditions
Inlet = AutoSubDomain(lambda x, on_bnd: on_bnd and x[0] < 1e-8)
Wall = AutoSubDomain(lambda x, on_bnd: on_bnd and near(x[1]*(H-x[1]), 0))
Cyl = AutoSubDomain(lambda x, on_bnd: on_bnd and x[0]>1e-6 and x[0]<1 and x[1] < 3*H/4 and x[1] > H/4)
Outlet = AutoSubDomain(lambda x, on_bnd: on_bnd and x[0] > L-1e-8)

# Overload post_import_problem to choose between the two cases
def post_import_problem(NS_parameters, commandline_kwargs, **NS_namespace):
    """ Choose case - case could be defined through command line."""
    NS_parameters.update(commandline_kwargs)
    case = NS_parameters['case'] if 'case' in NS_parameters else 1
    Um = cases[case]["Um"]
    Re = cases[case]["Re"]
    Umean = 2./3.* Um
    nu = Umean*D/Re
    NS_parameters.update(nu=nu, Re=Re, Um=Um, Umean=Umean)

    return NS_parameters
Beispiel #22
0
 def inside(self, x, on_boundary):
     return dolfin.near(x[0], 1.0)
Beispiel #23
0
 def inside(self, x, on_boundary):
     dx = x[0] - c_x
     dy = x[1] - c_y
     dr = sqrt(dx**2 + dy**2)
     return on_boundary and (near(x[1]*(y_max - x[1]), 0) or dr < r + 1E-3)
Beispiel #24
0
def bottom(x, on_boundary):
    return on_boundary and near(x[2], 0.0)
Beispiel #25
0
def bzo_boundary(r_vec, on_boundary):

    # NB: input r_vec is a position vector
    r,theta,z = cart_to_cyl(x)  # NB: check the function cart_to_cyl can take non-tuple collecs.
    return d.near(r,bzo_radius)
Beispiel #26
0
 def inside(self, x, on_boundary):
     return not bool((dolfin.near(x[0], self.b)) and on_boundary)
Beispiel #27
0
    return emesh


# -------------------------------------------------------------------


if __name__ == '__main__':
    mesh = df.UnitSquareMesh(4, 4)
    subdomains = df.MeshFunction('size_t', mesh, mesh.topology().dim(), 3)
    df.CompiledSubDomain('x[0] < 0.25+DOLFIN_EPS').mark(subdomains, 1)
    df.CompiledSubDomain('x[0] > 0.75-DOLFIN_EPS').mark(subdomains, 2)

    mesh1 = SubDomainMesh(subdomains, (1, 3))
    mesh2 = SubDomainMesh(subdomains, (2, 3))
    mesh12 = OverlapMesh(mesh1, mesh2)

    # FIXME: split the file!
    map1 = mesh12.parent_entity_map[mesh1.id()][2]
    map2 = mesh12.parent_entity_map[mesh2.id()][2]
    # Cell check out
    for c, c1, c2 in zip(df.cells(mesh12), map1, map2):
        assert df.near(c.midpoint().distance(df.Cell(mesh1, c1).midpoint()), 0, 1E-14)
        assert df.near(c.midpoint().distance(df.Cell(mesh2, c2).midpoint()), 0, 1E-14)
        
    # Vertices are not that important but anyways
    x1 = mesh1.coordinates(); map1 = mesh12.parent_entity_map[mesh1.id()][0]
    x2 = mesh2.coordinates(); map2 = mesh12.parent_entity_map[mesh2.id()][0]
    for x, i1, i2 in zip(mesh12.coordinates(), map1, map2):
        assert np.linalg.norm(x - x1[i1]) < 1E-13
        assert np.linalg.norm(x - x2[i2]) < 1E-13
Beispiel #28
0
 def map(self, x, y):
     if dolfin.near(x[0], self.b):
         y[0] = self.a + (x[0] - self.b)
Beispiel #29
0
 def inside(self, x, on_boundary):
     return on_boundary and dolfin.near(x[0], MIN_X_DIM)
Beispiel #30
0
 def inside(self, x, on_boundary):
     return near(x[1], 1.0)