예제 #1
0
    def test_make_upwind(self):
        # Arrange
        psi_data = np.array((0, 1, 0))
        flux_data = np.array((0, 0, 1, 0))

        options = Options()
        halo = options.n_halo
        traversals = Traversals(grid=psi_data.shape,
                                halo=halo,
                                jit_flags={},
                                n_threads=1)
        upwind = make_upwind(options=options,
                             non_unit_g_factor=False,
                             traversals=traversals)

        bc = [PeriodicBoundaryCondition()]
        psi = ScalarField(psi_data, halo, bc)
        psi_impl = psi.impl
        flux_impl = VectorField((flux_data, ), halo, bc).impl
        null_impl = ScalarField.make_null(len(psi_data.shape)).impl

        # Act
        upwind(psi_impl[0], *flux_impl, *null_impl)

        # Assert
        np.testing.assert_array_equal(psi.get(), np.roll(psi_data, 1))
예제 #2
0
    def test_apply_scalar(n_threads, halo, grid, loop):
        n_dims = len(grid)
        if n_dims == 1 and n_threads > 1:
            return

        # arrange
        traversals = Traversals(grid, halo, jit_flags, n_threads)
        sut = traversals.apply_scalar(loop=loop)

        scl_null_arg_impl = ScalarField.make_null(n_dims).impl
        vec_null_arg_impl = VectorField.make_null(n_dims).impl

        out = ScalarField(np.zeros(grid), halo,
                          [ConstantBoundaryCondition(np.nan)] * n_dims)

        # act
        sut(_cell_id_scalar, _cell_id_scalar, *out.impl[0],
            *vec_null_arg_impl[0], *vec_null_arg_impl[1],
            *scl_null_arg_impl[0], *scl_null_arg_impl[1],
            *scl_null_arg_impl[0], *scl_null_arg_impl[1],
            *scl_null_arg_impl[0], *scl_null_arg_impl[1])

        # assert
        data = out.get()
        assert data.shape == grid
        focus = (-halo, -halo)
        for i in range(halo, halo + grid[0]):
            for j in (-1, ) if n_dims == 1 else range(halo, halo + grid[1]):
                value = indexers[n_dims].at[0](focus, data, i, j)
                assert value == (n_dims if loop else 1) * cell_id(i, j)
        assert scl_null_arg_impl[0][0][meta_halo_valid]
        assert vec_null_arg_impl[0][0][meta_halo_valid]
        assert not out.impl[0][0][meta_halo_valid]
예제 #3
0
    def test_scalar_2d(self, halo, n_threads):
        # arrange
        data = np.array([[1, 6], [2, 7], [3, 8], [4, 9]])
        bc = (PeriodicBoundaryCondition(),
              PolarBoundaryCondition(data.shape, 0, 1))
        field = ScalarField(data, halo, bc)
        meta_and_data, fill_halos = field.impl
        traversals = Traversals(grid=data.shape,
                                halo=halo,
                                jit_flags={},
                                n_threads=n_threads)
        sut = traversals._fill_halos_scalar

        # act
        for thread_id in numba.prange(n_threads):
            sut(thread_id, *meta_and_data, *fill_halos)

        # assert
        np.testing.assert_array_equal(
            field.data[halo:-halo, :halo],
            np.roll(field.get()[:, :halo], data.shape[0] // 2, axis=0))
        np.testing.assert_array_equal(
            field.data[halo:-halo, -halo:],
            np.roll(field.get()[:, -halo:], data.shape[0] // 2, axis=0))
예제 #4
0
 def test_1d_contiguous():
     grid = (44, )
     data = np.empty(grid)
     bc = (PeriodicBoundaryCondition(), )
     sut = ScalarField(data, halo=1, boundary_conditions=bc)
     assert sut.get().data.contiguous
예제 #5
0
 def test_2d_second_dim_contiguous():
     grid = (44, 44)
     data = np.empty(grid)
     bc = (PeriodicBoundaryCondition(), PeriodicBoundaryCondition())
     sut = ScalarField(data, halo=1, boundary_conditions=bc)
     assert sut.get()[0, :].data.contiguous