示例#1
0
def download_nodal_dg_if_not_present(path="nodal-dg"):
    """Download the nodal-DG source code.

    :arg path: The destination path.
    """
    import os
    if os.path.exists(path):
        return

    import tempfile
    with tempfile.TemporaryDirectory() as tmp:
        filename = os.path.join(tmp, "master.zip")

        from pytools import download_from_web_if_not_present
        download_from_web_if_not_present(
            url="https://github.com/tcew/nodal-dg/archive/master.zip",
            local_name=filename)

        import zipfile
        with zipfile.ZipFile(filename, "r") as zp:
            zp.extractall(tmp)

        if not os.path.exists(path):
            import shutil
            shutil.move(os.path.join(tmp, "nodal-dg-master"), path)
def generate_festa_sommariva_quadrature_rules(outfile):
    filename = f"set_amr_square.m"
    download_from_web_if_not_present(
            url=f"{_URL}",
            local_name=filename)

    with open(filename, "r") as fd:
        mfile = fd.read()

    # rules are hardcoded as part of a MATLAB file
    # the are extracted by looking for the pattern
    #
    #   xw=[
    #       <x> <y> <weight>
    #       ...
    #   ];
    #
    # The degree of each quadrature rule is written in a line above the
    # nodes and weights in the pattern
    #
    #   DEGREE:  <degree>

    _re_degree = re.compile(r"DEGREE:\s+(\d+)")
    _re_xw = re.compile(r"xw\s?=\s?\[(.+?)\];", re.DOTALL)

    # NOTE: some degrees have multiple quadrature rules with a repeated
    # header, so we just take the unique ones here
    degrees = np.unique(
            np.fromiter(_re_degree.findall(mfile), dtype=np.int)
            )

    rules = {}
    for imatch, match in enumerate(_re_xw.findall(mfile)):
        d = degrees[imatch]
        assert d == imatch + 1, (d, imatch + 1)

        xw = np.fromstring(match.strip(), dtype=np.float64, sep=" ").reshape(-1, 3)
        rules[d] = {
                "quad_degree": d,
                "points": xw[:, :-1].T.tolist(),
                "weights": xw[:, -1].tolist(),
                }

        assert abs(np.sum(xw[:, -1]) - 4.0) < 1.0e-12
        print("degree {} points {} / {}".format(
            d, xw.shape[0], (d + 1) * (d + 2) // 2
            ))

    from pprint import pformat
    txt = (_PYTHON_TEMPLATE % pformat(rules, width=80)).replace('"', "")

    if outfile:
        with open(outfile, "w") as fd:
            fd.write(txt)
    else:
        print(txt)
示例#3
0
def generate_witherden_vincent_quadrature_rules(outfile):
    filename = "witherden_vincent.zip"
    download_from_web_if_not_present(
            url="https://ars.els-cdn.com/content/image/1-s2.0-S0898122115001224-mmc1.zip",  # noqa: E501
            local_name=filename)

    import zipfile
    with zipfile.ZipFile(filename, "r") as zp:
        zp.extractall(filename[:-4])

    # files are named <strength>-<number-of-nodes>.txt
    # contents of each row are <x> <y> [<z>] <weight>

    quad_rules = {}
    dirname = os.path.join(filename[:-4], "expanded", "quad")
    for f in os.listdir(dirname):
        degree = int(f.split("-")[0])
        txt = np.loadtxt(os.path.join(dirname, f)).reshape(-1, 3)

        quad_rules[degree] = {
                "quad_degree": degree,
                "points": txt[:, :-1].T.tolist(),
                "weights": txt[:, -1].tolist()
                }

        assert abs(np.sum(txt[:, -1]) - 4.0) < 1.0e-12

    hex_rules = {}
    dirname = os.path.join(filename[:-4], "expanded", "hex")
    for f in os.listdir(dirname):
        degree = int(f.split("-")[0])
        txt = np.loadtxt(os.path.join(dirname, f)).reshape(-1, 4)

        hex_rules[degree] = {
                "quad_degree": degree,
                "points": txt[:, :-1].T.tolist(),
                "weights": txt[:, -1].tolist()
                }

        assert abs(np.sum(txt[:, -1]) - 8.0) < 1.0e-12

    from pprint import pformat
    txt = (_PYTHON_TEMPLATE % (
        pformat(quad_rules), pformat(hex_rules)
        )).replace('"', "")

    if outfile:
        with open(outfile, "w") as fd:
            fd.write(txt)
    else:
        print(txt)
示例#4
0
    def get_mesh(self, resolution, target_order):
        from pytools import download_from_web_if_not_present

        download_from_web_if_not_present(
                "https://raw.githubusercontent.com/inducer/geometries/master/"
                "surface-3d/elliptiplane.brep")

        from meshmode.mesh.io import generate_gmsh, FileSource
        mesh = generate_gmsh(
                FileSource("elliptiplane.brep"), 2, order=2,
                other_options=[
                    "-string",
                    "Mesh.CharacteristicLengthMax = %g;" % resolution])

        # now centered at origin and extends to -1,1

        # Flip elements--gmsh generates inside-out geometry.
        from meshmode.mesh.processing import perform_flips
        return perform_flips(mesh, np.ones(mesh.nelements))
示例#5
0
    def get_mesh(self, resolution, target_order):
        from pytools import download_from_web_if_not_present

        download_from_web_if_not_present(
                "https://raw.githubusercontent.com/inducer/geometries/master/"
                "surface-3d/elliptiplane.brep")

        from meshmode.mesh.io import generate_gmsh, FileSource
        mesh = generate_gmsh(
                FileSource("elliptiplane.brep"), 2, order=2,
                other_options=[
                    "-string",
                    "Mesh.CharacteristicLengthMax = %g;" % resolution])

        # now centered at origin and extends to -1,1

        # Flip elements--gmsh generates inside-out geometry.
        from meshmode.mesh.processing import perform_flips
        return perform_flips(mesh, np.ones(mesh.nelements))
示例#6
0
    def get_mesh(self, resolution, target_order):
        from pytools import download_from_web_if_not_present

        download_from_web_if_not_present(
                "https://raw.githubusercontent.com/inducer/geometries/a869fc3/"
                "surface-3d/betterplane.brep")

        from meshmode.mesh.io import generate_gmsh, ScriptWithFilesSource
        mesh = generate_gmsh(
                ScriptWithFilesSource("""
                    Merge "betterplane.brep";

                    Mesh.CharacteristicLengthMax = %(lcmax)f;
                    Mesh.ElementOrder = 2;
                    Mesh.CharacteristicLengthExtendFromBoundary = 0;

                    // 2D mesh optimization
                    // Mesh.Lloyd = 1;

                    l_superfine() = Unique(Abs(Boundary{ Surface{
                        27, 25, 17, 13, 18  }; }));
                    l_fine() = Unique(Abs(Boundary{ Surface{ 2, 6, 7}; }));
                    l_coarse() = Unique(Abs(Boundary{ Surface{ 14, 16  }; }));

                    // p() = Unique(Abs(Boundary{ Line{l_fine()}; }));
                    // Characteristic Length{p()} = 0.05;

                    Field[1] = Attractor;
                    Field[1].NNodesByEdge = 100;
                    Field[1].EdgesList = {l_superfine()};

                    Field[2] = Threshold;
                    Field[2].IField = 1;
                    Field[2].LcMin = 0.075;
                    Field[2].LcMax = %(lcmax)f;
                    Field[2].DistMin = 0.1;
                    Field[2].DistMax = 0.4;

                    Field[3] = Attractor;
                    Field[3].NNodesByEdge = 100;
                    Field[3].EdgesList = {l_fine()};

                    Field[4] = Threshold;
                    Field[4].IField = 3;
                    Field[4].LcMin = 0.1;
                    Field[4].LcMax = %(lcmax)f;
                    Field[4].DistMin = 0.15;
                    Field[4].DistMax = 0.4;

                    Field[5] = Attractor;
                    Field[5].NNodesByEdge = 100;
                    Field[5].EdgesList = {l_coarse()};

                    Field[6] = Threshold;
                    Field[6].IField = 5;
                    Field[6].LcMin = 0.15;
                    Field[6].LcMax = %(lcmax)f;
                    Field[6].DistMin = 0.2;
                    Field[6].DistMax = 0.4;

                    Field[7] = Min;
                    Field[7].FieldsList = {2, 4, 6};

                    Background Field = 7;
                    """ % {
                        "lcmax": resolution,
                        }, ["betterplane.brep"]), 2)

        # Flip elements--gmsh generates inside-out geometry.
        from meshmode.mesh.processing import perform_flips
        return perform_flips(mesh, np.ones(mesh.nelements))
示例#7
0
    def get_mesh(self, resolution, target_order):
        from pytools import download_from_web_if_not_present

        download_from_web_if_not_present(
            "https://raw.githubusercontent.com/inducer/geometries/a869fc3/"
            "surface-3d/betterplane.brep")

        from meshmode.mesh.io import generate_gmsh, ScriptWithFilesSource
        mesh = generate_gmsh(
            ScriptWithFilesSource(
                """
                    Merge "betterplane.brep";

                    Mesh.CharacteristicLengthMax = %(lcmax)f;
                    Mesh.ElementOrder = 2;
                    Mesh.CharacteristicLengthExtendFromBoundary = 0;

                    // 2D mesh optimization
                    // Mesh.Lloyd = 1;

                    l_superfine() = Unique(Abs(Boundary{ Surface{
                        27, 25, 17, 13, 18  }; }));
                    l_fine() = Unique(Abs(Boundary{ Surface{ 2, 6, 7}; }));
                    l_coarse() = Unique(Abs(Boundary{ Surface{ 14, 16  }; }));

                    // p() = Unique(Abs(Boundary{ Line{l_fine()}; }));
                    // Characteristic Length{p()} = 0.05;

                    Field[1] = Attractor;
                    Field[1].NNodesByEdge = 100;
                    Field[1].EdgesList = {l_superfine()};

                    Field[2] = Threshold;
                    Field[2].IField = 1;
                    Field[2].LcMin = 0.075;
                    Field[2].LcMax = %(lcmax)f;
                    Field[2].DistMin = 0.1;
                    Field[2].DistMax = 0.4;

                    Field[3] = Attractor;
                    Field[3].NNodesByEdge = 100;
                    Field[3].EdgesList = {l_fine()};

                    Field[4] = Threshold;
                    Field[4].IField = 3;
                    Field[4].LcMin = 0.1;
                    Field[4].LcMax = %(lcmax)f;
                    Field[4].DistMin = 0.15;
                    Field[4].DistMax = 0.4;

                    Field[5] = Attractor;
                    Field[5].NNodesByEdge = 100;
                    Field[5].EdgesList = {l_coarse()};

                    Field[6] = Threshold;
                    Field[6].IField = 5;
                    Field[6].LcMin = 0.15;
                    Field[6].LcMax = %(lcmax)f;
                    Field[6].DistMin = 0.2;
                    Field[6].DistMax = 0.4;

                    Field[7] = Min;
                    Field[7].FieldsList = {2, 4, 6};

                    Background Field = 7;
                    """ % {
                    "lcmax": resolution,
                }, ["betterplane.brep"]), 2)

        # Flip elements--gmsh generates inside-out geometry.
        from meshmode.mesh.processing import perform_flips
        return perform_flips(mesh, np.ones(mesh.nelements))