Esempio n. 1
0
 def get_mesh(self, k):
     n = 2**(k + 1)
     vertices, cells = meshzoo.cube(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, n + 1,
                                    n + 1, n + 1)
     return meshplex.MeshTetra(vertices, cells)
Esempio n. 2
0
def test_regular_tet0(a):
    points = (a * np.array([
        [1.0, 0, 0],
        [-0.5, +np.sqrt(3.0) / 2.0, 0],
        [-0.5, -np.sqrt(3.0) / 2.0, 0],
        [0.0, 0.0, np.sqrt(2.0)],
    ]) / np.sqrt(3.0))
    cells = np.array([[0, 1, 2, 3]])
    mesh = meshplex.MeshTetra(points, cells.copy())
    # test __repr__
    print(mesh)

    assert np.all(mesh.cells("points") == cells)

    mesh.show()
    mesh.show_edge(0)

    tol = 1.0e-14

    z = a / np.sqrt(24.0)
    assert is_near_equal(mesh.cell_circumcenters, [0.0, 0.0, z], tol)

    assert is_near_equal(mesh.circumcenter_facet_distances, [z, z, z, z], tol)

    # covolume/edge length ratios
    # alpha = a / 12.0 / np.sqrt(2)
    alpha = a / 2 / np.sqrt(24) / np.sqrt(12)
    vals = mesh.ce_ratios
    assert is_near_equal(
        vals,
        [[
            [alpha, alpha, alpha],
            [alpha, alpha, alpha],
            [alpha, alpha, alpha],
            [alpha, alpha, alpha],
        ]],
        tol,
    )

    # cell volumes
    vol = a**3 / 6.0 / np.sqrt(2)
    assert is_near_equal(mesh.cell_volumes, [vol], tol)

    # control volumes
    val = vol / 4.0
    assert is_near_equal(mesh.control_volumes, [val, val, val, val], tol)

    # inradius
    # side_area = np.sqrt(3) / 4 * a ** 2
    # vol = a ** 3 / 6.0 / np.sqrt(2)
    # inradius = 3 * vol / (4 * side_area)
    inradius = a * np.sqrt(6) / 12
    assert is_near_equal(mesh.cell_inradius, [inradius], tol)

    # circumradius
    circumradius = a * np.sqrt(6) / 4
    assert is_near_equal(mesh.cell_circumradius, [circumradius], tol)

    # cell quality
    assert is_near_equal(mesh.q_radius_ratio, [1.0], tol)

    assert is_near_equal(mesh.cell_barycenters, mesh.cell_centroids, tol)
    assert is_near_equal(mesh.cell_barycenters,
                         [[0.0, 0.0, a * np.sqrt(6) / 12]], tol)

    assert is_near_equal(mesh.cell_incenters,
                         [[0.0, 0.0, a * np.sqrt(6) / 12]], tol)

    assert is_near_equal(mesh.q_min_sin_dihedral_angles, [1.0], tol)

    assert is_near_equal(mesh.q_vol_rms_edgelength3, [1.0], tol)
Esempio n. 3
0
def test_regular_tet0(a):
    points = (a * numpy.array([
        [1.0, 0, 0],
        [-0.5, +numpy.sqrt(3.0) / 2.0, 0],
        [-0.5, -numpy.sqrt(3.0) / 2.0, 0],
        [0.0, 0.0, numpy.sqrt(2.0)],
    ]) / numpy.sqrt(3.0))
    cells = numpy.array([[0, 1, 2, 3]])
    mesh = meshplex.MeshTetra(points, cells.copy())

    assert all((mesh.cells["nodes"] == cells).flat)

    mesh.show()
    mesh.show_edge(0)
    # from matplotlib import pyplot as plt
    # plt.show()

    ref_local_idx = [
        [[2, 3], [3, 1], [1, 2]],
        [[3, 0], [0, 2], [2, 3]],
        [[0, 1], [1, 3], [3, 0]],
        [[1, 2], [2, 0], [0, 1]],
    ]
    assert (mesh.local_idx.T == ref_local_idx).all()

    ref_local_idx_inv = [
        [(0, 0, 2), (0, 1, 1), (0, 2, 3), (1, 0, 1), (1, 1, 3), (1, 2, 2)],
        [(0, 0, 3), (0, 1, 2), (0, 2, 0), (1, 0, 2), (1, 1, 0), (1, 2, 3)],
        [(0, 0, 0), (0, 1, 3), (0, 2, 1), (1, 0, 3), (1, 1, 1), (1, 2, 0)],
        [(0, 0, 1), (0, 1, 0), (0, 2, 2), (1, 0, 0), (1, 1, 2), (1, 2, 1)],
    ]
    assert mesh.local_idx_inv == ref_local_idx_inv

    tol = 1.0e-14

    z = a / numpy.sqrt(24.0)
    assert near_equal(mesh.cell_circumcenters, [0.0, 0.0, z], tol)

    # pylint: disable=protected-access
    mesh._compute_ce_ratios_geometric()
    assert near_equal(mesh.circumcenter_face_distances, [z, z, z, z], tol)

    # covolume/edge length ratios
    # alpha = a / 12.0 / numpy.sqrt(2)
    alpha = a / 2 / numpy.sqrt(24) / numpy.sqrt(12)
    vals = mesh.ce_ratios
    assert near_equal(
        vals,
        [[
            [alpha, alpha, alpha],
            [alpha, alpha, alpha],
            [alpha, alpha, alpha],
            [alpha, alpha, alpha],
        ]],
        tol,
    )

    # cell volumes
    vol = a**3 / 6.0 / numpy.sqrt(2)
    assert near_equal(mesh.cell_volumes, [vol], tol)

    # control volumes
    val = vol / 4.0
    assert near_equal(mesh.control_volumes, [val, val, val, val], tol)

    # inradius
    # side_area = numpy.sqrt(3) / 4 * a ** 2
    # vol = a ** 3 / 6.0 / numpy.sqrt(2)
    # inradius = 3 * vol / (4 * side_area)
    inradius = a * numpy.sqrt(6) / 12
    assert near_equal(mesh.cell_inradius, [inradius], tol)

    # circumradius
    circumradius = a * numpy.sqrt(6) / 4
    assert near_equal(mesh.cell_circumradius, [circumradius], tol)

    # cell quality
    assert near_equal(mesh.cell_quality, [1.0], tol)

    mesh.mark_boundary()

    assert near_equal(mesh.cell_barycenters, mesh.cell_centroids, tol)
    assert near_equal(mesh.cell_barycenters,
                      [[0.0, 0.0, a * numpy.sqrt(6) / 12]], tol)

    assert near_equal(mesh.cell_incenters,
                      [[0.0, 0.0, a * numpy.sqrt(6) / 12]], tol)

    return
Esempio n. 4
0
def create_plots(prefix, functions, H, time_limit=60):
    times = []
    quality_min = []
    quality_avg = []
    num_poisson_steps = []
    num_points = []

    poisson_tol = 1.0e-10
    with Progress() as progress:
        task1 = progress.add_task("Overall", total=len(H))
        task2 = progress.add_task("Functions", total=len(functions))
        for h in H:
            times.append([])
            quality_min.append([])
            quality_avg.append([])
            num_poisson_steps.append([])
            num_points.append([])
            progress.update(task2, completed=0)
            for fun in functions:
                try:
                    with time_limiter(time_limit):
                        tic = time.time()
                        points, cells = fun(h)
                        toc = time.time()
                except TimeoutException:
                    times[-1].append(numpy.nan)
                    quality_min[-1].append(numpy.nan)
                    quality_avg[-1].append(numpy.nan)
                    num_points[-1].append(numpy.nan)
                    num_poisson_steps[-1].append(numpy.nan)
                else:
                    if cells.shape[1] == 3:
                        mesh = meshplex.MeshTri(points, cells)
                    else:
                        assert cells.shape[1] == 4
                        mesh = meshplex.MeshTetra(points, cells)

                    times[-1].append(toc - tic)
                    quality_min[-1].append(numpy.min(mesh.q_radius_ratio))
                    quality_avg[-1].append(numpy.average(mesh.q_radius_ratio))
                    num_points[-1].append(mesh.node_coords.shape[0])

                    if numpy.min(mesh.q_radius_ratio) < 1.0e-5:
                        num_poisson_steps[-1].append(numpy.nan)
                    else:
                        num_steps = get_poisson_steps(points, cells, poisson_tol)
                        num_poisson_steps[-1].append(num_steps)

                progress.update(task2, advance=1)
            progress.update(task1, advance=1)

    times = numpy.array(times)
    quality_min = numpy.array(quality_min)
    quality_avg = numpy.array(quality_avg)
    num_poisson_steps = numpy.array(num_poisson_steps)
    num_points = numpy.array(num_points)

    names = [inspect.getmodule(fun).desc for fun in functions]
    colors = [inspect.getmodule(fun).colors for fun in functions]

    # plot the data
    plt.style.use(dufte.style)
    for name, num_pts, t, cols in zip(names, num_points.T, times.T, colors):
        plt.loglog(num_pts, t, color=cols[0], label=name)
    dufte.legend()
    plt.xlabel("num points")
    plt.title("mesh creation times [s]")
    plt.savefig(f"{prefix}-times.svg", transparent=True, bbox_inches="tight")
    # plt.show()
    plt.close()

    for name, num_pts, qa, qm, cols in zip(
        names, num_points.T, quality_avg.T, quality_min.T, colors
    ):
        plt.semilogx(num_pts, qa, color=cols[0], linestyle="-", label=f"{name}")
        plt.semilogx(num_pts, qm, color=cols[1], linestyle="--", label="")
        plt.ylim(0.0, 1.0)
    dufte.legend()
    plt.xlabel("num points")
    plt.title("cell quality, avg  and min (dashed)")
    plt.savefig(f"{prefix}-quality.svg", transparent=True, bbox_inches="tight")
    # plt.show()
    plt.close()

    for name, num_pts, np, cols in zip(
        names, num_points.T, num_poisson_steps.T, colors
    ):
        plt.semilogx(num_pts, np, color=cols[0], label=f"{name}")
    dufte.legend()
    plt.xlabel("num points")
    plt.title(f"number of CG steps for the Poisson problem (tol={poisson_tol:.1e})")
    plt.savefig(f"{prefix}-poisson.svg", transparent=True, bbox_inches="tight")
    # plt.show()
    plt.close()