with pytest.raises(ValueError): intp(np.array([100, -100])) res = f.make_interpolator("numba", fill=45)(np.array([100, -100])) np.testing.assert_almost_equal(res, np.full(f.data_shape, 45)) @pytest.mark.slow @pytest.mark.parametrize( "grid", [ UnitGrid((6, )), PolarGrid(6, 4), SphericalGrid(7, 4), CylindricalGrid(6, (0, 8), (7, 8)), ], ) def test_interpolation_to_cartesian(grid): """ test whether data is interpolated correctly to Cartesian grid """ dim = grid.dim vf = VectorField(grid, 2) sf = vf[0] # test extraction of fields fc = FieldCollection([sf, vf]) # subset grid_cart = UnitGrid([4] * dim) for f in [sf, fc]: res = f.interpolate_to_grid(grid_cart) np.testing.assert_allclose(res.data, 2)
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()
with pytest.raises(ValueError): intp(np.array([100, -100])) res = f.make_interpolator(backend="numba", fill=45)(np.array([100, -100])) np.testing.assert_almost_equal(res, np.full(f.data_shape, 45)) @pytest.mark.slow @pytest.mark.parametrize( "grid", [ UnitGrid((6,)), PolarGrid(6, 4), SphericalGrid(7, 4), CylindricalGrid(6, (0, 8), (7, 8)), ], ) def test_interpolation_to_cartesian(grid): """ test whether data is interpolated correctly to Cartesian grid """ dim = grid.dim vf = VectorField(grid, 2) sf = vf[0] # test extraction of fields fc = FieldCollection([sf, vf]) # subset grid_cart = UnitGrid([4] * dim) for f in [sf, fc]: res = f.interpolate_to_grid(grid_cart) np.testing.assert_allclose(res.data, 2)