def test_precomputed_interpolation():
    """ Test interpolation with PrecomputedSparseFunction which accepts
        precomputed values for interpolation coefficients
    """
    shape = (101, 101)
    points = [(.05, .9), (.01, .8), (0.07, 0.84)]
    origin = (0, 0)

    grid = Grid(shape=shape, origin=origin)
    r = 2  # Constant for linear interpolation
    #  because we interpolate across 2 neighbouring points in each dimension

    def init(data):
        for i in range(data.shape[0]):
            for j in range(data.shape[1]):
                data[i, j] = sin(grid.spacing[0]*i) + sin(grid.spacing[1]*j)
        return data

    m = Function(name='m', grid=grid, initializer=init, space_order=0)

    gridpoints, interpolation_coeffs = precompute_linear_interpolation(points,
                                                                       grid, origin)

    sf = PrecomputedSparseFunction(name='s', grid=grid, r=r, npoint=len(points),
                                   gridpoints=gridpoints,
                                   interpolation_coeffs=interpolation_coeffs)
    eqn = sf.interpolate(m)
    op = Operator(eqn)
    op()
    expected_values = [sin(point[0]) + sin(point[1]) for point in points]
    assert(all(np.isclose(sf.data, expected_values, rtol=1e-6)))
Ejemplo n.º 2
0
def test_precomputed_injection():
    """Test injection with PrecomputedSparseFunction which accepts
       precomputed values for interpolation coefficients
    """
    shape = (11, 11)
    coords = [(.05, .95), (.45, .45)]
    origin = (0, 0)
    result = 0.25

    # Constant for linear interpolation
    # because we interpolate across 2 neighbouring points in each dimension
    r = 2

    m = unit_box(shape=shape)
    m.data[:] = 0.

    gridpoints, interpolation_coeffs = precompute_linear_interpolation(
        coords, m.grid, origin)

    sf = PrecomputedSparseFunction(name='s',
                                   grid=m.grid,
                                   r=r,
                                   npoint=len(coords),
                                   gridpoints=gridpoints,
                                   interpolation_coeffs=interpolation_coeffs)

    expr = sf.inject(m, Float(1.))

    Operator(expr)()

    indices = [slice(0, 2, 1), slice(9, 11, 1)]
    assert np.allclose(m.data[indices], result, rtol=1.e-5)

    indices = [slice(4, 6, 1) for _ in coords]
    assert np.allclose(m.data[indices], result, rtol=1.e-5)
Ejemplo n.º 3
0
def test_precomputed_interpolation():
    """ Test interpolation with PrecomputedSparseFunction which accepts
        precomputed values for interpolation coefficients
    """
    shape = (101, 101)
    points = [(.05, .9), (.01, .8), (0.07, 0.84)]
    origin = (0, 0)

    grid = Grid(shape=shape, origin=origin)
    r = 2  # Constant for linear interpolation
    #  because we interpolate across 2 neighbouring points in each dimension

    def init(data):
        for i in range(data.shape[0]):
            for j in range(data.shape[1]):
                data[i, j] = sin(grid.spacing[0]*i) + sin(grid.spacing[1]*j)
        return data

    m = Function(name='m', grid=grid, initializer=init, space_order=0)

    gridpoints, interpolation_coeffs = precompute_linear_interpolation(points,
                                                                       grid, origin)

    sf = PrecomputedSparseFunction(name='s', grid=grid, r=r, npoint=len(points),
                                   gridpoints=gridpoints,
                                   interpolation_coeffs=interpolation_coeffs)
    eqn = sf.interpolate(m)
    op = Operator(eqn)
    op()
    expected_values = [sin(point[0]) + sin(point[1]) for point in points]
    assert(all(np.isclose(sf.data, expected_values, rtol=1e-6)))