<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">def gmsh2DGF(points, cells, bndDomain = None, dim = None):
    """
    Parameter:
       points     array(list) of points of length dim
       cells      dict containing element vertex numbers
       bndDomain  dict id -&gt; str containing possible boundary domains
       dim        dimension of grid

    Returns
        String containing the mesh description in DGF format.
    """

    if dim is None:
        if "quad" in cells or "triangle" in cells:
            dim = 2
        else:
            dim = len(points[0])

    dgf="DGF\nVertex\n"
    for p in points:
        for i in range(dim):
            dgf += str(p[i]) + " "
        dgf += "\n"
    dgf += "#\n\n"

    if "triangle" in cells:
        dgf += "Simplex\n"
        for t in cells["triangle"]:
            for v in t:
                dgf += str(v) + " "
            dgf += "\n"
        dgf += "#\n\n"

    if "quad" in cells:
        dgf += "Cube\n"
        # gmsh has a different reference quadrilateral
        vxmap = [0,1,3,2] # flip vertex 2 and 3
        for t in cells["quad"]:
            for i in range(4):
                dgf += str(t[vxmap[i]]) + " "
            dgf += "\n"
        dgf += "#\n\n"

    if bndDomain is not None:
        assert isinstance(bndDomain, dict), "Expecting a dictionary for boundary domain"
        dgf += "BoundaryDomain\n"
        for bndid,bnd in bndDomain.items():
            if bnd == 'default':
                dgf += bnd + " " + str(bndid) +"\n"
            else:
                dgf += str(bndid) + " " + bnd +"\n"

        dgf += "#\n\n"

    return dgf
</pre></body></html>