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