示例#1
0
def import_mesh(
        prefix="mesh",
        subdomains=False,
        dim=2,
        directory=".",
):
    """Function importing a dolfin mesh.

    Arguments:
        prefix (str, optional): mesh files prefix (eg. my_mesh.msh,
            my_mesh_domain.xdmf, my_mesh_bondaries.xdmf). Defaults to "mesh".
        subdomains (bool, optional): True if there are subdomains. Defaults to
            False.
        dim (int, optional): dimension of the domain. Defaults to 2.
        directory (str, optional): directory of the mesh files. Defaults to ".".

    Output:
        - dolfin Mesh object containing the domain;
        - dolfin MeshFunction object containing the physical lines (dim=2) or
            surfaces (dim=3) defined in the msh file and the sub-domains;
        - association table
    """
    # Set the file name
    domain = "{}_domain.xdmf".format(prefix)
    boundaries = "{}_boundaries.xdmf".format(prefix)

    # create 2 xdmf files if not converted before
    if not os.path.exists("{}/{}".format(directory, domain)) or \
       not os.path.exists("{}/{}".format(directory, boundaries)):
        msh2xdmf("{}.msh".format(prefix), dim=dim, directory=directory)

    # Import the converted domain
    mesh = Mesh()
    with XDMFFile("{}/{}".format(directory, domain)) as infile:
        infile.read(mesh)
    # Import the boundaries
    boundaries_mvc = MeshValueCollection("size_t", mesh, dim=dim)
    with XDMFFile("{}/{}".format(directory, boundaries)) as infile:
        infile.read(boundaries_mvc, 'boundaries')
    boundaries_mf = MeshFunctionSizet(mesh, boundaries_mvc)
    # Import the subdomains
    if subdomains:
        subdomains_mvc = MeshValueCollection("size_t", mesh, dim=dim)
        with XDMFFile("{}/{}".format(directory, domain)) as infile:
            infile.read(subdomains_mvc, 'subdomains')
        subdomains_mf = MeshFunctionSizet(mesh, subdomains_mvc)
    # Import the association table
    association_table_name = "{}/{}_{}".format(
        directory, prefix, "association_table.ini")
    file_content = ConfigParser()
    file_content.read(association_table_name)
    association_table = dict(file_content["ASSOCIATION TABLE"])
    # Convert the value from string to int
    for key, value in association_table.items():
        association_table[key] = int(value)
    # Return the Mesh and the MeshFunction objects
    if not subdomains:
        return mesh, boundaries_mf, association_table
    else:
        return mesh, boundaries_mf, subdomains_mf, association_table
示例#2
0
def import_mesh_from_xdmf(
    prefix="mesh",
    subdomains=False,
    dim=2,
    directory=".",
):
    """
    Function importing a dolfin mesh.

    Arguments:
        - domain (str): name of the domain XDMF file;
        - boundaries (str): name of the boundaries XDMF file;
        - dim (int): dimension of the domain;
        - subdomains (bool): true if there are subdomains, else false
        - directory (str): (optional) directory of the mesh;

    Output:
        - dolfin Mesh object containing the domain;
        - dolfin MeshFunction object containing the physical lines (dim=2) or
            surfaces (dim=3) defined in the msh file and the sub-domains;
        - association table
    """
    # Set the file name
    domain = "{}_domain.xdmf".format(prefix)
    boundaries = "{}_boundaries.xdmf".format(prefix)
    # Import the converted domain
    mesh = Mesh()
    with XDMFFile("{}/{}".format(directory, domain)) as infile:
        infile.read(mesh)
    # Import the boundaries
    boundaries_mvc = MeshValueCollection("size_t", mesh, dim=dim)
    with XDMFFile("{}/{}".format(directory, boundaries)) as infile:
        infile.read(boundaries_mvc, 'boundaries')
    boundaries_mf = MeshFunctionSizet(mesh, boundaries_mvc)
    # Import the subdomains
    if subdomains:
        subdomains_mvc = MeshValueCollection("size_t", mesh, dim=dim)
        with XDMFFile("{}/{}".format(directory, domain)) as infile:
            infile.read(subdomains_mvc, 'subdomains')
        subdomains_mf = MeshFunctionSizet(mesh, subdomains_mvc)
    # Import the association table
    association_table_name = "{}/{}_{}".format(directory, prefix,
                                               "association_table.ini")
    file_content = ConfigParser()
    file_content.read(association_table_name)
    association_table = dict(file_content["ASSOCIATION TABLE"])
    # Convert the value from string to int
    for key, value in association_table.items():
        association_table[key] = int(value)
    # Return the Mesh and the MeshFunction objects
    if not subdomains:
        return mesh, boundaries_mf, association_table
    else:
        return mesh, boundaries_mf, subdomains_mf, association_table
示例#3
0
    def __init__(self, g: float, mesh_dir: str, materials: Dict[int,
                                                                Material]):
        self._g = g
        self._mesh_dir = mesh_dir
        self._materials = materials

        self._mesh = Mesh(os.path.join(mesh_dir, 'mesh.xml'))
        self._subdomains = MeshFunctionSizet(
            self.mesh, os.path.join(mesh_dir, 'mesh_physical_region.xml'))
        self._boundaries = MeshFunctionSizet(
            self.mesh, os.path.join(mesh_dir, 'mesh_facet_region.xml'))
示例#4
0
def DolfinReader(directory, file):
    msh = Mesh()
    with XDMFFile("{}/{}.xdmf".format(directory, file)) as infile:
        infile.read(msh)
        dim = msh.topology().dim()
    mvc = MeshValueCollection("size_t", msh, dim=dim - 1)
    with XDMFFile("{}/{}_facets.xdmf".format(directory, file)) as infile:
        infile.read(mvc, "boundaries")
    boundaries = MeshFunctionSizet(msh, mvc)
    mvc = MeshValueCollection("size_t", msh, dim=dim)
    with XDMFFile("{}/{}_physical_region.xdmf".format(directory,
                                                      file)) as infile:
        infile.read(mvc, "subdomains")
    subdomains = MeshFunctionSizet(msh, mvc)
    return msh, boundaries, subdomains
示例#5
0
def mark_boundaries(
    domains: MeshFunctionSizet,
    model_geometry: ModelGeometry,
    boundary_markers: Tuple[BOUNDARY_MARKER],
    external_boundary,
) -> Tuple[MeshFunctionSizet, MeshFunctionSizet]:
    """Top level function to mark boundaries.

    :param domains: Model domains
    :param model_geometry: Model geometry
    :param boundary_markers: List of BoundaryMarkers
    :param external_boundary: ID of the external boundary
    :return: Boundary and Outline MeshFunctions
    """
    mesh = domains.mesh()
    boundaries = create_mesh_function("boundary", mesh, 1)
    # todo copy boundaries if possible?
    outline = create_mesh_function("outline", mesh, 1)

    External().mark(boundaries, external_boundary)  # mark external boundaries

    # get each edge and its mating domains
    for edge, neighbouring_domains in boundary_iterator(domains):
        #  all edges at this stage are interesting and belong to an outline
        outline[edge] = 1
        boundaries[edge] = mark_boundary(model_geometry,
                                         list(neighbouring_domains),
                                         boundary_markers)

    return boundaries, outline
示例#6
0
def import_mesh_from_xdmf(domain="domain.xdmf",
                          boundaries="boundaries.xdmf",
                          labels="labels.yaml",
                          subdomains=False,
                          dim=2,
                          directory="."):
    """
    Function importing a msh mesh and converting it into a dolfin mesh.

    Arguments:
        - domain (str): name of the domain XDMF file;
        - boundaries (str): name of the boundaries XDMF file;
        - dim (int): dimension of the domain;
        - subdomains (bool): true if there are subdomains, else false
        - directory (str): (optional) directory of the mesh;

    Output:
        - dolfin Mesh object containing the domain;
        - dolfin MeshFunction object containing the physical lines (dim=2) or
        surfaces (dim=3) defined in the msh file and the sub-domains;
        - a dictionary with labels for boundaries and subdomains
    """
    # Import the converted domain
    mesh = Mesh()
    with XDMFFile("{}/{}".format(directory, domain)) as infile:
        infile.read(mesh)
    # Import the boundaries
    boundaries_mvc = MeshValueCollection("size_t", mesh, dim=dim)
    with XDMFFile("{}/{}".format(directory, boundaries)) as infile:
        infile.read(boundaries_mvc, 'boundaries')
    boundaries_mf = MeshFunctionSizet(mesh, boundaries_mvc)
    # Import the subdomains
    if subdomains:
        subdomains_mvc = MeshValueCollection("size_t", mesh, dim=dim)
        with XDMFFile("{}/{}".format(directory, domain)) as infile:
            infile.read(subdomains_mvc, 'subdomains')
        subdomains_mf = MeshFunctionSizet(mesh, subdomains_mvc)
    # Import labels
    with open("{}/{}".format(directory, labels), 'r') as infile:
        labels = yaml.load(infile, Loader=yaml.FullLoader)
    # Return the Mesh and the MeshFunction objects
    if not subdomains:
        return mesh, boundaries_mf, labels
    else:
        return mesh, boundaries_mf, subdomains_mf, labels
    print(labels)
示例#7
0
def boundary_iterator(
    domains: MeshFunctionSizet,
) -> Iterable[Tuple[df.Facet, Tuple[int, ...]]]:
    """Get every facet (edge) with at least 2 things touching it i.e. not an external edge.

    :param domains: The labeled domains
    :return: Iterable of tuples of an edge and its touching domains
    """
    for facet in df.facets(domains.mesh()):  # iterate over edges in the mesh
        # set: therefore ignore duplicates
        touching_domains = {domains[c] for c in df.cells(facet)}
        if len(touching_domains) > 1:
            # if at least 2 partners i.e. not an external edge
            yield facet, tuple(sorted(touching_domains))