def main(): """ main routine testing the performance """ print("Reports calls-per-second (larger is better)\n") # Cartesian grid with different shapes and boundary conditions for size in [32, 512]: grid = UnitGrid((size, size), periodic=False) print(grid) field = ScalarField.random_normal(grid) bc_value = np.ones(size) result = field.laplace(bc={"value": 1}).data for bc in ["scalar", "array", "linked"]: if bc == "scalar": bcs = {"value": 1} elif bc == "array": bcs = {"value": bc_value} elif bc == "linked": bcs = Boundaries.from_data(grid, {"value": bc_value}, rank=0) for ax, upper in grid._iter_boundaries(): bcs[ax][upper].link_value(bc_value) # result = field.laplace(bc=bcs).data laplace = grid.get_operator("laplace", bc=bcs) # call once to pre-compile and test result np.testing.assert_allclose(laplace(field.data), result) speed = estimate_computation_speed(laplace, field.data) print(f"{bc:>6s}:{int(speed):>9d}") print()
def test_unit_grid_3d(): """test 3D grids""" grid = UnitGrid([4, 4, 4]) assert grid.dim == 3 assert grid.numba_type == "f8[:, :, :]" assert grid.volume == 64 np.testing.assert_array_equal(grid.discretization, np.ones(3)) assert grid.get_image_data(np.zeros(grid.shape))["extent"] == [0, 4, 0, 4] assert grid.polar_coordinates_real((1, 1, 3)).shape == (4, 4, 4) periodic = random.choices([True, False], k=3) grid = UnitGrid([4, 6, 8], periodic=periodic) assert grid.dim == 3 assert grid.volume == 192 assert grid.polar_coordinates_real((1, 1, 2)).shape == (4, 6, 8) grid = UnitGrid([4, 4, 4], periodic=True) assert grid.dim == 3 assert grid.volume == 64 for _ in range(10): p = np.random.randn(3) not_too_large = grid.polar_coordinates_real(p) < np.sqrt(12) assert np.all(not_too_large) large_enough = grid.polar_coordinates_real((0, 0, 0)) > np.sqrt(6) assert np.any(large_enough) # test boundary points for bndry in grid._iter_boundaries(): assert grid._boundary_coordinates(*bndry).shape == (4, 4, 3)
def main(): """main routine testing the performance""" print("Reports calls-per-second (larger is better)\n") # Cartesian grid with different shapes and boundary conditions for size in [32, 512]: grid = UnitGrid([size, size], periodic=False) print(grid) field = ScalarField.random_normal(grid) bc_value = np.ones(size) result = field.laplace(bc={"value": 1}).data for bc in ["scalar", "array", "function", "time-dependent", "linked"]: if bc == "scalar": bcs = {"value": 1} elif bc == "array": bcs = {"value": bc_value} elif bc == "function": bcs = grid.get_boundary_conditions( {"virtual_point": "2 - value"}) elif bc == "time-dependent": bcs = grid.get_boundary_conditions({"value_expression": "t"}) elif bc == "linked": bcs = grid.get_boundary_conditions({"value": bc_value}) for ax, upper in grid._iter_boundaries(): bcs[ax][upper].link_value(bc_value) else: raise RuntimeError # create the operator with these conditions laplace = grid.make_operator("laplace", bc=bcs) if bc == "time-dependent": args = numba_dict({"t": 1}) # call once to pre-compile and test result np.testing.assert_allclose(laplace(field.data, args=args), result) # estimate the speed speed = estimate_computation_speed(laplace, field.data, args=args) else: # call once to pre-compile and test result np.testing.assert_allclose(laplace(field.data), result) # estimate the speed speed = estimate_computation_speed(laplace, field.data) print(f"{bc:>14s}:{int(speed):>9d}") print()