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