Esempio n. 1
0
    def test_reciprocal_calculation(self, axis, spacing, offsets):
        """
        A test to check that reciprocal eta calculated are consistent.
        """
        grid_offset = offsets[0]
        eval_offset = offsets[1]
        xyz = ('x', 'y', 'z')
        left = np.full((10, 10, 10), -2 * spacing, dtype=float)
        right = np.full((10, 10, 10), -2 * spacing, dtype=float)

        left_index = [None, None, None]
        left_index[axis] = 4
        right_index = [None, None, None]
        right_index[axis] = 5

        left[left_index[0], left_index[1], left_index[2]] = 0.3 * spacing

        right[right_index[0], right_index[1], right_index[2]] = -0.7 * spacing

        # Should produce the same results
        data_l = get_data_inc_reciprocals(left, spacing, xyz[axis],
                                          grid_offset, eval_offset)
        data_r = get_data_inc_reciprocals(right, spacing, xyz[axis],
                                          grid_offset, eval_offset)

        assert (np.all(np.isclose(data_l, data_r, equal_nan=True)))
Esempio n. 2
0
    def test_type_splitting(self, axis):
        """
        A test to check that splitting of points into various categories
        functions as intended.
        """
        xyz = ('x', 'y', 'z')
        distances = np.full((10, 10, 10), -2, dtype=float)
        ind = [slice(None), slice(None), slice(None)]
        ind[axis] = np.array([1, 2, 5])
        distances[ind[0], ind[1], ind[2]] = 0.6

        # TODO: Ideally want to vary grid offset too
        data = get_data_inc_reciprocals(distances, 1, xyz[axis], 0, 0)
        add_distance_column(data)

        first, last, double, paired_left, paired_right = split_types(
            data, xyz[axis], 10)

        assert (np.all(
            first.index.get_level_values(xyz[axis]).to_numpy() == 1))
        assert (np.all(last.index.get_level_values(xyz[axis]).to_numpy() == 6))
        assert (np.all(
            double.index.get_level_values(xyz[axis]).to_numpy() == 2))
        assert (np.all(
            paired_left.index.get_level_values(xyz[axis]).to_numpy() == 3))
        assert (np.all(
            paired_right.index.get_level_values(xyz[axis]).to_numpy() == 5))
Esempio n. 3
0
    def test_zero_handling(self, order, spec):
        """
        Check that stencils with distances of zero evaluate correctly.
        """
        # Unpack the spec
        bc_type = spec['bcs']
        deriv = spec['deriv']
        goffset = spec['goffset']
        eoffset = spec['eoffset']
        if bc_type == 'even':
            bcs = BoundaryConditions({2 * i: 0
                                      for i in range(1 + order // 2)}, order)
        else:
            bcs = BoundaryConditions(
                {2 * i + 1: 0
                 for i in range(1 + order // 2)}, order)

        cache = os.path.dirname(
            __file__) + '/../devitoboundary/extrapolation_cache.dat'

        stencils = StencilSet(deriv, eoffset, bcs, cache=cache)
        lambdas = stencils.lambdaify
        max_ext_points = stencils.max_ext_points

        distances = np.full((10, 1, 1), -2 * order, dtype=float)
        if goffset == 0.5:
            distances[4, :, :] = 0.5
        else:
            distances[4, :, :] = 0

        data = get_data_inc_reciprocals(distances, 1, 'x', goffset, eoffset)
        add_distance_column(data)
        data = data.iloc[1:-1]
        data = get_n_pts(data, 'double', order, eoffset)

        grid = Grid(shape=(10, 1, 11), extent=(9, 0, 0))
        s_dim = Dimension(name='s')
        ncoeffs = 2 * max_ext_points + 1

        w_shape = grid.shape + (ncoeffs, )
        w_dims = grid.dimensions + (s_dim, )

        w = Function(name='w', dimensions=w_dims, shape=w_shape)

        fill_stencils(data, 'double', max_ext_points, lambdas, w, 10, 'x')

        # Derivative stencils should be zero if evaluation offset is zero
        assert (np.all(np.abs(w.data) < np.finfo(np.float).eps))
Esempio n. 4
0
    def test_zero_handling_staggered(self, side, order, spec):
        """
        Check that stencils with distances of zero evaluate correctly for staggered
        systems.
        """
        # Unpack the spec
        bc_type = spec['bcs']
        deriv = spec['deriv']
        goffset = spec['goffset']
        eoffset = spec['eoffset']
        if bc_type == 'even':
            bcs = BoundaryConditions({2 * i: 0
                                      for i in range(1 + order // 2)}, order)
        else:
            bcs = BoundaryConditions(
                {2 * i + 1: 0
                 for i in range(1 + order // 2)}, order)

        cache = os.path.dirname(
            __file__) + '/../devitoboundary/extrapolation_cache.dat'

        # stencils_lambda = get_stencils_lambda(deriv, eoffset, bcs, cache=cache)
        stencils = StencilSet(deriv, eoffset, bcs, cache=cache)
        lambdas = stencils.lambdaify
        max_ext_points = stencils.max_ext_points

        distances = np.full((10, 1, 1), -2 * order, dtype=float)
        if goffset == 0.5:
            distances[4, :, :] = 0.5
        else:
            distances[4, :, :] = 0

        data = get_data_inc_reciprocals(distances, 1, 'x', goffset, eoffset)
        add_distance_column(data)

        right_dist = pd.notna(data.eta_l)
        left_dist = pd.notna(data.eta_r)
        data.loc[right_dist, 'dist'] = order
        data.loc[left_dist, 'dist'] = -order

        first = data.loc[left_dist]
        last = data.loc[right_dist]
        first = shift_grid_endpoint(first, 'x', goffset, eoffset)
        last = shift_grid_endpoint(last, 'x', goffset, eoffset)
        first = get_n_pts(first, 'first', order, eoffset)
        last = get_n_pts(last, 'last', order, eoffset)

        grid = Grid(shape=(10, 1, 1), extent=(9, 0, 0))
        s_dim = Dimension(name='s')
        ncoeffs = 2 * max_ext_points + 1

        w_shape = grid.shape + (ncoeffs, )
        w_dims = grid.dimensions + (s_dim, )

        w = Function(name='w', dimensions=w_dims, shape=w_shape)

        # Fill the weights using the standard stencil (for interior)
        if max_ext_points > order // 2:
            # Need to zero pad the standard stencil
            zero_pad = max_ext_points - order // 2
            w.data[:, :, :,
                   zero_pad:-zero_pad] = standard_stencil(deriv,
                                                          order,
                                                          offset=eoffset)
        else:
            w.data[:] = standard_stencil(deriv, order, offset=eoffset)

        if side == 'first':
            w.data[4:] = 0
            fill_stencils(first, 'first', max_ext_points, lambdas, w, 10, 'x')
        if side == 'last':
            w.data[:5] = 0
            fill_stencils(last, 'last', max_ext_points, lambdas, w, 10, 'x')

        # Check against a saved correct version
        # Generate filename
        filename = side + '_' + str(order) + '_' + bc_type + '_' + str(
            deriv) + '_' + str(goffset) + '_' + str(eoffset)

        # Load reference data
        reference = np.load(
            os.path.dirname(__file__) + '/zero_handling_stencils/' + filename +
            '.npy')

        assert np.all(np.absolute(w.data - reference) < np.finfo(float).eps)
Esempio n. 5
0
    def test_fill_stencils_offset(self, offset, point_type, order, spacing):
        """
        Check that offsetting the grid and boundary by the same amount results
        in identical stencils for both cases. This is checked on both sides of
        the boundary.
        """

        spec = {2 * i: 0 for i in range(1 + order // 2)}
        bcs = BoundaryConditions(spec, order)
        cache = os.path.dirname(
            __file__) + '/../devitoboundary/extrapolation_cache.dat'

        stencils = StencilSet(2, 0, bcs, cache=cache)
        lambdas = stencils.lambdaify
        max_ext_points = stencils.max_ext_points

        distances = np.full((10, 1, 10), -2 * order * spacing, dtype=float)
        distances[4, :, :] = np.linspace(0, 0.9 * spacing, 10)

        offset_distances = np.full((10, 1, 10),
                                   -2 * order * spacing,
                                   dtype=float)
        if offset == 0.5:
            # +ve stagger
            offset_distances[4, :, :5] = np.linspace(0.5 * spacing,
                                                     0.9 * spacing, 5)
            offset_distances[5, :, 5:] = np.linspace(0, 0.4 * spacing, 5)
        else:
            # -ve stagger
            offset_distances[4, :, :] = np.linspace(-0.5 * spacing,
                                                    0.4 * spacing, 10)

        data = get_data_inc_reciprocals(distances, spacing, 'x', 0, 0)
        offset_data = get_data_inc_reciprocals(offset_distances, spacing, 'x',
                                               offset, 0)
        dmask = np.full(21, True, dtype=bool)
        dmask[1] = False
        data = data[dmask]
        offset_data = offset_data[dmask]
        add_distance_column(data)
        add_distance_column(offset_data)
        if point_type == 'first':
            data = data[::2]
            data.dist = -order // 2
            offset_data = offset_data[::2]
            offset_data.dist = -order // 2
            # No need to drop points or shift grid endpoint, as that is done here

        else:
            data = data[1::2]
            data.dist = order // 2
            offset_data = offset_data[1::2]
            offset_data.dist = order // 2
            # No need to drop points or shift grid endpoint, as that is done here

        # Set n_pts
        data['n_pts'] = order // 2
        offset_data['n_pts'] = order // 2

        grid = Grid(shape=(10, 1, 10), extent=(9 * spacing, 0, 9 * spacing))
        s_dim = Dimension(name='s')
        ncoeffs = order + 1

        w_shape = grid.shape + (ncoeffs, )
        w_dims = grid.dimensions + (s_dim, )

        w_normal = Function(name='w_n', dimensions=w_dims, shape=w_shape)
        w_offset = Function(name='w_o', dimensions=w_dims, shape=w_shape)

        fill_stencils(data, point_type, max_ext_points, lambdas, w_normal, 10,
                      'x')
        fill_stencils(offset_data, point_type, max_ext_points, lambdas,
                      w_offset, 10, 'x')

        if point_type == 'first':
            assert np.all(np.isclose(w_normal.data[2:5], w_offset.data[2:5]))
        else:
            assert np.all(np.isclose(w_normal.data[5:7], w_offset.data[5:7]))