[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 | """ |
---|
| 9 | |
---|
| 10 | # Module imports |
---|
| 11 | from anuga.shallow_water import Domain |
---|
[6232] | 12 | from anuga.caching import cache |
---|
| 13 | from anuga.fit_interpolate.fit import _fit_to_mesh |
---|
[5857] | 14 | import project |
---|
[7276] | 15 | import numpy as num |
---|
[6237] | 16 | import time |
---|
[5857] | 17 | |
---|
[6548] | 18 | internal_verbose = True # Verbosity used within this function |
---|
[5857] | 19 | |
---|
[6232] | 20 | filename=project.bathymetry_filename |
---|
| 21 | alpha=0.02 |
---|
| 22 | from anuga.config import points_file_block_line_size as max_read_lines |
---|
| 23 | |
---|
[5857] | 24 | #------------------------- |
---|
| 25 | # Create Domain from mesh |
---|
| 26 | #------------------------- |
---|
[6548] | 27 | domain = cache(Domain, |
---|
| 28 | (project.mesh_filename, |
---|
| 29 | {'verbose': True}), |
---|
| 30 | 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, |
---|
| 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 | |
---|
[5857] | 64 | #------------------------- |
---|
| 65 | # Initial Conditions |
---|
| 66 | #------------------------- |
---|
[6237] | 67 | t0 = time.time() |
---|
[5857] | 68 | domain.set_quantity('elevation', |
---|
[6232] | 69 | filename=filename, |
---|
[5857] | 70 | alpha=0.02, |
---|
[6232] | 71 | verbose=internal_verbose, |
---|
[5857] | 72 | use_cache=True) |
---|
[6237] | 73 | compute_time = time.time()-t0 |
---|
[6232] | 74 | |
---|
| 75 | ref = domain.get_quantity('elevation').get_values() |
---|
[5857] | 76 | |
---|
[6232] | 77 | # Check that cache is now present (and correct) |
---|
| 78 | flag = cache(_fit_to_mesh, |
---|
| 79 | args, |
---|
| 80 | kwargs, |
---|
| 81 | verbose=False, |
---|
| 82 | dependencies=[filename], |
---|
| 83 | test=True) |
---|
[6548] | 84 | |
---|
[6232] | 85 | assert flag is not None |
---|
[6548] | 86 | res = domain.get_quantity('elevation').get_values() |
---|
[6232] | 87 | assert num.allclose(res, ref) |
---|
| 88 | |
---|
| 89 | # Now check this using the high level call |
---|
[6237] | 90 | print 'Try to read in via cache' |
---|
| 91 | t0 = time.time() |
---|
[5857] | 92 | domain.set_quantity('elevation', |
---|
[6232] | 93 | filename=filename, |
---|
[5857] | 94 | alpha=0.02, |
---|
[6232] | 95 | verbose=internal_verbose, |
---|
[5857] | 96 | use_cache=True) |
---|
[6237] | 97 | cache_time = time.time()-t0 |
---|
[6232] | 98 | |
---|
[6548] | 99 | res = domain.get_quantity('elevation').get_values() |
---|
[6232] | 100 | assert num.allclose(res, ref) |
---|
[6237] | 101 | |
---|
| 102 | print 'cache_time', cache_time |
---|
| 103 | print 'compute_time', compute_time |
---|
[6548] | 104 | |
---|
| 105 | msg = 'Caching did not speed things up as expected' |
---|
[6708] | 106 | msg += 'Compute time = %.f, Cache time = %.f' % (compute_time, |
---|
| 107 | cache_time) |
---|
[6548] | 108 | assert cache_time < compute_time/10, msg |
---|