[6237] | 1 | """This script tests that caching works for set_quantity using point data from a file. |
---|
| 2 | First cache is cleared, then set_quantity is run twice checking that |
---|
| 3 | fitting is evaluated only first time and that the result from cache on the |
---|
| 4 | second round is correct. |
---|
[5857] | 5 | |
---|
[6237] | 6 | This script depends on Benchmark_2.msh and Benchmark_2_Bathymetry.pts |
---|
[5857] | 7 | """ |
---|
[6237] | 8 | # FIXME(Ole): This needs to become a unit test - in automated validation tests under Okushiri. |
---|
[5857] | 9 | |
---|
[6237] | 10 | # FIXME(Ole): This won't work until ticket:314 has been fixed. |
---|
[6196] | 11 | |
---|
[5857] | 12 | # Module imports |
---|
| 13 | from anuga.shallow_water import Domain |
---|
[6232] | 14 | from anuga.caching import cache |
---|
| 15 | from anuga.fit_interpolate.fit import _fit_to_mesh |
---|
[5857] | 16 | import project |
---|
[6232] | 17 | import Numeric as num |
---|
[6237] | 18 | import time |
---|
[5857] | 19 | |
---|
[6237] | 20 | internal_verbose = True # Verbose used in set_quantity and passed into fit_to_mesh |
---|
[5857] | 21 | |
---|
[6232] | 22 | filename=project.bathymetry_filename |
---|
| 23 | alpha=0.02 |
---|
| 24 | from anuga.config import points_file_block_line_size as max_read_lines |
---|
| 25 | |
---|
[5857] | 26 | #------------------------- |
---|
| 27 | # Create Domain from mesh |
---|
| 28 | #------------------------- |
---|
[6232] | 29 | domain = cache(Domain, (project.mesh_filename, {'verbose': True}), verbose=False) |
---|
[5857] | 30 | |
---|
[6232] | 31 | # Clear caching of underlying function |
---|
| 32 | args = (filename, ) |
---|
| 33 | kwargs = {'vertex_coordinates': None, |
---|
| 34 | 'triangles': None, |
---|
| 35 | 'mesh': domain.mesh, |
---|
| 36 | 'point_attributes': None, |
---|
| 37 | 'alpha': alpha, |
---|
| 38 | 'verbose': internal_verbose, |
---|
[6237] | 39 | 'acceptable_overshoot': 1.01, # This is the default value in _fit_to_mesh - actually, not implemented. Remove! |
---|
[6232] | 40 | 'mesh_origin': None, |
---|
| 41 | 'data_origin': None, |
---|
| 42 | 'max_read_lines': max_read_lines, |
---|
| 43 | 'attribute_name': None |
---|
| 44 | } |
---|
[5857] | 45 | |
---|
[6232] | 46 | |
---|
| 47 | cache(_fit_to_mesh, |
---|
| 48 | args, |
---|
| 49 | kwargs, |
---|
| 50 | verbose=False, |
---|
| 51 | dependencies=[filename], |
---|
| 52 | clear=True) |
---|
| 53 | |
---|
| 54 | # Check that cache is empty |
---|
| 55 | flag = cache(_fit_to_mesh, |
---|
| 56 | args, |
---|
| 57 | kwargs, |
---|
| 58 | verbose=False, |
---|
| 59 | dependencies=[filename], |
---|
| 60 | test=True) |
---|
| 61 | assert flag is None |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | |
---|
[5857] | 65 | #------------------------- |
---|
| 66 | # Initial Conditions |
---|
| 67 | #------------------------- |
---|
[6232] | 68 | #print 'Set elevation and cache' |
---|
[6237] | 69 | t0 = time.time() |
---|
[5857] | 70 | domain.set_quantity('elevation', |
---|
[6232] | 71 | filename=filename, |
---|
[5857] | 72 | alpha=0.02, |
---|
[6232] | 73 | verbose=internal_verbose, |
---|
[5857] | 74 | use_cache=True) |
---|
[6237] | 75 | compute_time = time.time()-t0 |
---|
[6232] | 76 | |
---|
| 77 | ref = domain.get_quantity('elevation').get_values() |
---|
[5857] | 78 | |
---|
[6232] | 79 | # Check that cache is now present (and correct) |
---|
| 80 | flag = cache(_fit_to_mesh, |
---|
| 81 | args, |
---|
| 82 | kwargs, |
---|
| 83 | verbose=False, |
---|
| 84 | dependencies=[filename], |
---|
| 85 | test=True) |
---|
| 86 | assert flag is not None |
---|
| 87 | res = domain.get_quantity('elevation').get_values() |
---|
| 88 | assert num.allclose(res, ref) |
---|
| 89 | |
---|
| 90 | # Now check this using the high level call |
---|
[6237] | 91 | |
---|
| 92 | print 'Try to read in via cache' |
---|
| 93 | t0 = time.time() |
---|
[5857] | 94 | domain.set_quantity('elevation', |
---|
[6232] | 95 | filename=filename, |
---|
[5857] | 96 | alpha=0.02, |
---|
[6232] | 97 | verbose=internal_verbose, |
---|
[5857] | 98 | use_cache=True) |
---|
[6237] | 99 | cache_time = time.time()-t0 |
---|
[6232] | 100 | |
---|
| 101 | res = domain.get_quantity('elevation').get_values() |
---|
| 102 | assert num.allclose(res, ref) |
---|
[6237] | 103 | |
---|
| 104 | print 'cache_time', cache_time |
---|
| 105 | print 'compute_time', compute_time |
---|
| 106 | assert cache_time < compute_time/10 |
---|