Exemplo n.º 1
0
def test_laplace_cyl():
    """ test the implementation of the laplace operator """
    for boundary_z in ["periodic", "derivative"]:
        grid = CylindricalGrid(4, (0, 5), (8, 16),
                               periodic_z=(boundary_z == "periodic"))
        a_2d = np.random.uniform(0, 1, grid.shape)

        bcs = grid.get_boundary_conditions(["derivative", boundary_z])
        lap_2d = ops.make_laplace(bcs)
        b_2d = lap_2d(a_2d)
        assert b_2d.shape == grid.shape
Exemplo n.º 2
0
def main():
    """ main routine testing the performance """
    print("Reports calls-per-second (larger is better)")
    print("  The `CUSTOM` method implemented by hand is the baseline case.")
    print(
        "  The `FLEXIBLE` method is a serial implementation using the "
        "boundary conditions from the package."
    )
    print("  The other methods use the functions supplied by the package.\n")

    # Cartesian grid with different shapes and boundary conditions
    for shape in [(32, 32), (512, 512)]:
        data = np.random.random(shape)
        for periodic in [True, False]:
            grid = UnitGrid(shape, periodic=periodic)
            bcs = Boundaries.from_data(grid, "natural")
            print(grid)
            result = cartesian.make_laplace(bcs, method="scipy")(data)

            for method in ["FLEXIBLE", "CUSTOM", "numba", "matrix", "scipy"]:
                if method == "FLEXIBLE":
                    laplace = flexible_laplace_2d(bcs)
                elif method == "CUSTOM":
                    laplace = custom_laplace_2d(shape, periodic=periodic)
                elif method in {"numba", "matrix", "scipy"}:
                    laplace = cartesian.make_laplace(bcs, method=method)
                else:
                    raise ValueError(f"Unknown method `{method}`")
                # call once to pre-compile and test result
                np.testing.assert_allclose(laplace(data), result)
                speed = estimate_computation_speed(laplace, data)
                print(f"{method:>8s}: {int(speed):>9d}")
            print()

    # Cylindrical grid with different shapes
    for shape in [(32, 64), (512, 512)]:
        data = np.random.random(shape)
        grid = CylindricalGrid(shape[0], [0, shape[1]], shape)
        print(f"Cylindrical grid, shape={shape}")
        bcs = Boundaries.from_data(grid, "no-flux")
        laplace_cyl = cylindrical.make_laplace(bcs)
        result = laplace_cyl(data)

        for method in ["CUSTOM", "numba"]:
            if method == "CUSTOM":
                laplace = custom_laplace_cyl_no_flux(shape)
            elif method == "numba":
                laplace = laplace_cyl
            else:
                raise ValueError(f"Unknown method `{method}`")
            # call once to pre-compile and test result
            np.testing.assert_allclose(laplace(data), result)
            speed = estimate_computation_speed(laplace, data)
            print(f"{method:>8s}: {int(speed):>9d}")
        print()

    # Spherical grid with different shapes
    for shape in [32, 512]:
        data = np.random.random(shape)
        grid = SphericalGrid(shape, shape)
        print(grid)
        bcs = Boundaries.from_data(grid, "no-flux")
        make_laplace = spherical.make_laplace

        for conservative in [True, False]:
            laplace = make_laplace(bcs, conservative=conservative)
            # call once to pre-compile
            laplace(data)
            speed = estimate_computation_speed(laplace, data)
            print(f" numba (conservative={str(conservative):<5}): {int(speed):>9d}")
        print()