1 | """This script tests that caching works for set_quantity using point |
---|
2 | data from a file. |
---|
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. |
---|
6 | |
---|
7 | This script depends on Benchmark_2.msh and Benchmark_2_Bathymetry.pts |
---|
8 | """ |
---|
9 | # FIXME(Ole): This needs to become a unit test - in automated validation tests under Okushiri. |
---|
10 | |
---|
11 | # FIXME(Ole): This won't work until ticket:314 has been fixed. |
---|
12 | |
---|
13 | # Module imports |
---|
14 | from anuga.shallow_water import Domain |
---|
15 | from anuga.caching import cache |
---|
16 | from anuga.fit_interpolate.fit import _fit_to_mesh |
---|
17 | import project |
---|
18 | import Numeric as num |
---|
19 | import time |
---|
20 | |
---|
21 | internal_verbose = True # Verbose used in set_quantity and passed into fit_to_mesh |
---|
22 | |
---|
23 | filename=project.bathymetry_filename |
---|
24 | alpha=0.02 |
---|
25 | from anuga.config import points_file_block_line_size as max_read_lines |
---|
26 | |
---|
27 | #------------------------- |
---|
28 | # Create Domain from mesh |
---|
29 | #------------------------- |
---|
30 | domain = cache(Domain, (project.mesh_filename, {'verbose': True}), verbose=False) |
---|
31 | |
---|
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 | 'acceptable_overshoot': 1.01, # This is the default value in _fit_to_mesh - actually, not implemented. Remove! |
---|
41 | 'mesh_origin': None, |
---|
42 | 'data_origin': None, |
---|
43 | 'max_read_lines': max_read_lines, |
---|
44 | 'attribute_name': None |
---|
45 | } |
---|
46 | |
---|
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 | |
---|
66 | #------------------------- |
---|
67 | # Initial Conditions |
---|
68 | #------------------------- |
---|
69 | #print 'Set elevation and cache' |
---|
70 | t0 = time.time() |
---|
71 | domain.set_quantity('elevation', |
---|
72 | filename=filename, |
---|
73 | alpha=0.02, |
---|
74 | verbose=internal_verbose, |
---|
75 | use_cache=True) |
---|
76 | compute_time = time.time()-t0 |
---|
77 | |
---|
78 | ref = domain.get_quantity('elevation').get_values() |
---|
79 | |
---|
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 |
---|
92 | |
---|
93 | print 'Try to read in via cache' |
---|
94 | t0 = time.time() |
---|
95 | domain.set_quantity('elevation', |
---|
96 | filename=filename, |
---|
97 | alpha=0.02, |
---|
98 | verbose=internal_verbose, |
---|
99 | use_cache=True) |
---|
100 | cache_time = time.time()-t0 |
---|
101 | |
---|
102 | res = domain.get_quantity('elevation').get_values() |
---|
103 | assert num.allclose(res, ref) |
---|
104 | |
---|
105 | print 'cache_time', cache_time |
---|
106 | print 'compute_time', compute_time |
---|
107 | assert cache_time < compute_time/10 |
---|