source: trunk/anuga_validation/automated_validation_tests/okushiri_tank_validation/test_caching_of_set_quantity.py @ 7877

Last change on this file since 7877 was 7877, checked in by hudson, 14 years ago

Moved all development files into trunk.

File size: 3.1 KB
Line 
1"""This script tests that caching works for set_quantity using point
2data from a file.
3First cache is cleared, then set_quantity is run twice checking that
4fitting is evaluated only first time and that the result from cache on the
5second round is correct.
6
7This script depends on  Benchmark_2.msh and Benchmark_2_Bathymetry.pts
8"""
9
10# Module imports
11from anuga.shallow_water.shallow_water_domain import Domain
12from anuga.caching import cache
13from anuga.fit_interpolate.fit import _fit_to_mesh
14import project
15import numpy as num
16import time
17
18internal_verbose = True # Verbosity used within this function
19
20filename=project.bathymetry_filename
21alpha=0.02
22from anuga.config import points_file_block_line_size as max_read_lines
23
24#-------------------------
25# Create Domain from mesh
26#-------------------------
27domain = cache(Domain, 
28               (project.mesh_filename, 
29                {'verbose': True}), 
30               verbose=False)
31
32# Clear caching of underlying function                                           
33args = (filename, )
34kwargs = {'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          }
45
46
47cache(_fit_to_mesh,
48      args, 
49      kwargs,
50      verbose=False,
51      dependencies=[filename],
52      clear=True)
53
54# Check that cache is empty     
55flag = cache(_fit_to_mesh,
56             args, 
57             kwargs,
58             verbose=False,
59             dependencies=[filename],
60             test=True)
61assert flag is None
62
63
64#-------------------------
65# Initial Conditions
66#-------------------------
67t0 = time.time()
68domain.set_quantity('elevation',
69                    filename=filename,
70                    alpha=0.02,                   
71                    verbose=internal_verbose,
72                    use_cache=True)
73compute_time = time.time()-t0
74                   
75ref = domain.get_quantity('elevation').get_values()                   
76
77# Check that cache is now present (and correct)
78flag = cache(_fit_to_mesh,
79             args, 
80             kwargs,
81             verbose=False,
82             dependencies=[filename],
83             test=True)
84             
85assert flag is not None
86res = domain.get_quantity('elevation').get_values()
87assert num.allclose(res, ref)
88
89# Now check this using the high level call
90print 'Try to read in via cache'
91t0 = time.time()
92domain.set_quantity('elevation',
93                    filename=filename,
94                    alpha=0.02,                   
95                    verbose=internal_verbose,
96                    use_cache=True)
97cache_time = time.time()-t0                   
98                   
99res = domain.get_quantity('elevation').get_values() 
100assert num.allclose(res, ref)
101
102print 'cache_time', cache_time
103print 'compute_time', compute_time
104
105msg = 'Caching did not speed things up as expected'
106msg += 'Compute time = %.f, Cache time = %.f' % (compute_time,
107                                                 cache_time)
108assert cache_time < compute_time/10, msg
Note: See TracBrowser for help on using the repository browser.