def test_adjoint_inject_interpolate(self, shape, coords, npoints=19): """ Verify that p.inject is the adjoint of p.interpolate for a devito SparseFunction p """ a = unit_box(shape=shape) a.data[:] = 0. c = unit_box(shape=shape, name='c', grid=a.grid) c.data[:] = 27. assert a.grid == c.grid # Inject receiver p = points(a.grid, ranges=coords, npoints=npoints) p.data[:] = 1.2 expr = p.inject(field=a, expr=p) # Read receiver p2 = points(a.grid, name='points2', ranges=coords, npoints=npoints) expr2 = p2.interpolate(expr=c) Operator(expr + expr2)(a=a, c=c) # < P x, y > - < x, P^T y> # Px => p2 # y => p # x => c # P^T y => a term1 = np.dot(p2.data.reshape(-1), p.data.reshape(-1)) term2 = np.dot(c.data.reshape(-1), a.data.reshape(-1)) assert np.isclose((term1-term2) / term1, 0., atol=1.e-6)
def test_interpolate_array(shape, coords, npoints=20): """Test generic point interpolation testing the x-coordinate of an abitrary set of points going across the grid. """ a = unit_box(shape=shape) p = points(a.grid, coords, npoints=npoints) xcoords = p.coordinates.data[:, 0] expr = p.interpolate(a) Operator(expr)(a=a, points=p.data[:]) assert np.allclose(p.data[:], xcoords, rtol=1e-6)
def test_inject(shape, coords, result, npoints=19): """Test point injection with a set of points forming a line through the middle of the grid. """ a = unit_box(shape=shape) a.data[:] = 0. p = points(a.grid, ranges=coords, npoints=npoints) expr = p.inject(a, FLOAT(1.)) Operator(expr)(a=a) indices = [slice(4, 6, 1) for _ in coords] indices[0] = slice(1, -1, 1) assert np.allclose(a.data[indices], result, rtol=1.e-5)
def test_interpolate_custom(shape, coords, npoints=20): """Test generic point interpolation testing the x-coordinate of an abitrary set of points going across the grid. """ a = unit_box(shape=shape) p = custom_points(a.grid, coords, npoints=npoints) xcoords = p.coordinates.data[:, 0] p.data[:] = 1. expr = p.interpolate(a * p.indices[0]) Operator(expr)(a=a) assert np.allclose(p.data[0, :], 0.0 * xcoords, rtol=1e-6) assert np.allclose(p.data[1, :], 1.0 * xcoords, rtol=1e-6) assert np.allclose(p.data[2, :], 2.0 * xcoords, rtol=1e-6)
def test_inject_from_field(shape, coords, result, npoints=19): """Test point injection from a second field along a line through the middle of the grid. """ a = unit_box(shape=shape) a.data[:] = 0. b = Function(name='b', grid=a.grid) b.data[:] = 1. p = points(a.grid, ranges=coords, npoints=npoints) expr = p.inject(field=a, expr=b) Operator(expr)(a=a, b=b) indices = [slice(4, 6, 1) for _ in coords] indices[0] = slice(1, -1, 1) assert np.allclose(a.data[indices], result, rtol=1.e-5)
def test_interpolate_indexed(shape, coords, npoints=20): """Test generic point interpolation testing the x-coordinate of an abitrary set of points going across the grid. Unlike other tests, here we interpolate an expression built using the indexed notation. """ a = unit_box(shape=shape) p = custom_points(a.grid, coords, npoints=npoints) xcoords = p.coordinates.data[:, 0] p.data[:] = 1. expr = p.interpolate(a[a.grid.dimensions] * p.indices[0]) Operator(expr)(a=a) assert np.allclose(p.data[0, :], 0.0 * xcoords, rtol=1e-6) assert np.allclose(p.data[1, :], 1.0 * xcoords, rtol=1e-6) assert np.allclose(p.data[2, :], 2.0 * xcoords, rtol=1e-6)
def test_interpolation_dx(): """ Test interpolation of a SparseFunction from a Derivative of a Function. """ u = unit_box(shape=(11, 11)) sf1 = SparseFunction(name='s', grid=u.grid, npoint=1) sf1.coordinates.data[0, :] = (0.5, 0.5) op = Operator(sf1.interpolate(u.dx)) assert sf1.data.shape == (1, ) u.data[:] = 0.0 u.data[5, 5] = 4.0 u.data[4, 5] = 2.0 u.data[6, 5] = 2.0 op.apply() # Exactly in the middle of 4 points, only 1 nonzero is 4 assert sf1.data[0] == pytest.approx(-20.0)