Skip to content

Commit

Permalink
Adaptively increase resolution for numerical integrals in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avirshup committed Aug 24, 2017
1 parent 49c458a commit d8da718
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
4 changes: 2 additions & 2 deletions moldesign/_tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def assert_almost_equal(actual, desired, **kwargs):
""" Decorator to disable tests that need internet if we can't connect to the network """


def generate_grid(g, g2=None):
def generate_grid(g, g2=None, npoints=64):
from moldesign import mathutils

if g2 is None: g2 = g
Expand All @@ -243,6 +243,6 @@ def generate_grid(g, g2=None):
ranges = np.ones((3, 2))*5.0*width
ranges[:, 0] *= -1
ranges += ((g.center + g2.center)/2.0)[:, None]
grid = mathutils.VolumetricGrid(*ranges, npoints=64)
grid = mathutils.VolumetricGrid(*ranges, npoints=npoints)
allpoints = grid.allpoints()
return allpoints, grid
31 changes: 26 additions & 5 deletions moldesign/_tests/test_gaussian_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,32 @@ def _assert_numerical_analytical_overlaps_match(g1, g2):
else:
helpers.assert_almost_equal(prod.integral, olap)

allpoints, grid = helpers.generate_grid(g1, g2)
with np.errstate(under='ignore'):
prodvals = g1(allpoints) * g2(allpoints)
numsum = prodvals.sum() * grid.dx * grid.dy * grid.dz
helpers.assert_almost_equal(numsum, olap, decimal=4)
def assert_with_resolution(npoints):
allpoints, grid = helpers.generate_grid(g1, g2, npoints)
with np.errstate(under='ignore'):
prodvals = g1(allpoints) * g2(allpoints)
numsum = prodvals.sum() * grid.dx * grid.dy * grid.dz
helpers.assert_almost_equal(numsum, olap, decimal=4)

# If numerical isn't equal to analytical, try again with higher resolution
# to make sure the failure isn't due to a sparse grid:
try:
assert_with_resolution(64)
except AssertionError:
pass
else:
return

try:
assert_with_resolution(128)
except AssertionError:
pass
else:
return

assert_with_resolution(256)




@pytest.mark.parametrize('withunits', [False, True])
Expand Down

0 comments on commit d8da718

Please sign in to comment.