Skip to content

Commit

Permalink
tests: Add test for injection into Function on SubDomain
Browse files Browse the repository at this point in the history
  • Loading branch information
EdCaunt committed Apr 4, 2024
1 parent 4cd0321 commit 4268c54
Showing 1 changed file with 64 additions and 16 deletions.
80 changes: 64 additions & 16 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,22 @@ def test_interpolation_radius():
assert True


class SD0(SubDomain):
name = 'sd0'

def define(self, dimensions):
x, y = dimensions
return {x: ('left', 6), y: y}


class SD1(SubDomain):
name = 'sd1'

def define(self, dimensions):
x, y = dimensions
return {x: ('middle', 2, 1), y: ('right', 6)}


class TestSubDomainInterpolation:
"""
Tests for interpolation onto and off of Functions defined on
Expand All @@ -773,23 +789,9 @@ class TestSubDomainInterpolation:

def test_interpolate_subdomain(self):
"""
Test interpolation off of a function defined on a SubDomain.
Test interpolation off of a Function defined on a SubDomain.
"""

class SD0(SubDomain):
name = 'sd0'

def define(self, dimensions):
x, y = dimensions
return {x: ('left', 6), y: y}

class SD1(SubDomain):
name = 'sd1'

def define(self, dimensions):
x, y = dimensions
return {x: ('middle', 2, 1), y: ('right', 6)}

grid = Grid(shape=(11, 11), extent=(10., 10.))
sd0 = SD0(grid=grid)
sd1 = SD1(grid=grid)
Expand Down Expand Up @@ -832,4 +834,50 @@ def define(self, dimensions):

assert np.all(np.isclose(sr0.data, check0))
assert np.all(np.isclose(sr1.data, check1))
assert np.all(np.isclose(sr2.data, check2))
assert np.all(np.isclose(sr2.data, check2))

def test_inject_subdomain(self):
"""
Test injection into a Function defined on a SubDomain.
"""
grid = Grid(shape=(11, 11), extent=(10., 10.))
sd0 = SD0(grid=grid)
sd1 = SD1(grid=grid)

f0 = Function(name='f0', grid=sd0)
f1 = Function(name='f1', grid=sd1)

sr0 = SparseFunction(name='sr0', grid=grid, npoint=8)
sr1 = SparseFunction(name='sr1', grid=grid, npoint=8)

coords = np.array([[2.5, 1.5], [4.5, 2.], [8.5, 4.],
[0.5, 6.], [7.5, 4.], [5.5, 5.5],
[1.5, 4.5], [7.5, 8.5]])

sr0.coordinates.data[:] = coords
sr1.coordinates.data[:] = coords

src0 = sr0.inject(f0, Float(1.))
src1 = sr1.inject(f1, Float(1.))

op = Operator([src0, src1])

op.apply()

check0 = np.array([[0., 0., 0., 0., 0., 0., 0.5, 0., 0., 0., 0.],
[0., 0., 0., 0., 0.25, 0.25, 0.5, 0., 0., 0., 0.],
[0., 0.25, 0.25, 0., 0.25, 0.25, 0., 0., 0., 0., 0.],
[0., 0.25, 0.25, 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0.5, 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0.5, 0., 0., 0.25, 0.25, 0., 0., 0., 0.]])
check1 = np.array([[0.25, 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.],
[0.25, 0.25, 0., 0., 0., 0.],
[0.25, 0.25, 0., 0., 0., 0.],
[0., 0., 0., 0.25, 0.25, 0.],
[0., 0., 0., 0.25, 0.25, 0.],
[0., 0., 0., 0., 0., 0.]])

assert np.all(np.isclose(f0.data, check0))
assert np.all(np.isclose(f1.data, check1))

0 comments on commit 4268c54

Please sign in to comment.