1 | """Verify that simulation produced by ANUGA compares to published |
---|
2 | validation timeseries ch5, ch7 and ch9 as well as the boundary timeseries. |
---|
3 | |
---|
4 | RMS norm is printed and plots are produced. |
---|
5 | """ |
---|
6 | |
---|
7 | from Numeric import allclose |
---|
8 | from Scientific.IO.NetCDF import NetCDFFile |
---|
9 | |
---|
10 | from anuga.abstract_2d_finite_volumes.util import file_function |
---|
11 | from anuga.utilities.numerical_tools import\ |
---|
12 | ensure_numeric, cov, get_machine_precision |
---|
13 | |
---|
14 | import project |
---|
15 | |
---|
16 | try: |
---|
17 | from pylab import ion, hold, plot, title, legend, xlabel, ylabel, savefig |
---|
18 | except: |
---|
19 | plotting = False |
---|
20 | else: |
---|
21 | plotting = True |
---|
22 | |
---|
23 | #plotting = False |
---|
24 | |
---|
25 | #------------------------- |
---|
26 | # Basic data |
---|
27 | #------------------------- |
---|
28 | |
---|
29 | finaltime = 22.5 |
---|
30 | timestep = 0.05 |
---|
31 | |
---|
32 | gauge_locations = [[0.000, 1.696]] # Boundary gauge |
---|
33 | gauge_locations += [[4.521, 1.196], [4.521, 1.696], [4.521, 2.196]] #Ch 5-7-9 |
---|
34 | gauge_names = ['Boundary', 'ch5', 'ch7', 'ch9'] |
---|
35 | |
---|
36 | validation_data = {} |
---|
37 | for key in gauge_names: |
---|
38 | validation_data[key] = [] |
---|
39 | |
---|
40 | |
---|
41 | #expected_covariances = {'Boundary': 5.288392008865989e-05, |
---|
42 | # 'ch5': 1.166748190444681e-04, |
---|
43 | # 'ch7': 1.121816242516758e-04, |
---|
44 | # 'ch9': 1.249543278366778e-04} |
---|
45 | |
---|
46 | # old limiters |
---|
47 | expected_covariances = {'Boundary': 5.288601162783020386e-05, |
---|
48 | 'ch5': 1.167001054284431472e-04, |
---|
49 | 'ch7': 1.121474766904651861e-04, |
---|
50 | 'ch9': 1.249244820847215335e-04} |
---|
51 | |
---|
52 | expected_differences = {'Boundary': 8.361144081847830638e-04, |
---|
53 | 'ch5': 3.423673831653336816e-03, |
---|
54 | 'ch7': 2.799962153549145211e-03, |
---|
55 | 'ch9': 3.198560464876740433e-03} |
---|
56 | |
---|
57 | |
---|
58 | |
---|
59 | #------------------------- |
---|
60 | # Read validation dataa |
---|
61 | #------------------------- |
---|
62 | |
---|
63 | print 'Reading', project.boundary_filename |
---|
64 | fid = NetCDFFile(project.boundary_filename, 'r') |
---|
65 | input_time = fid.variables['time'][:] |
---|
66 | validation_data['Boundary'] = fid.variables['stage'][:] |
---|
67 | |
---|
68 | reference_time = [] |
---|
69 | fid = open(project.validation_filename) |
---|
70 | lines = fid.readlines() |
---|
71 | fid.close() |
---|
72 | |
---|
73 | for i, line in enumerate(lines[1:]): |
---|
74 | if i == len(input_time): break |
---|
75 | |
---|
76 | fields = line.split() |
---|
77 | |
---|
78 | reference_time.append(float(fields[0])) # Record reference time |
---|
79 | for j, key in enumerate(gauge_names[1:]): # Omit boundary gauge |
---|
80 | value = float(fields[1:][j]) # Omit time |
---|
81 | validation_data[key].append(value/100) # Convert cm2m |
---|
82 | |
---|
83 | |
---|
84 | # Checks |
---|
85 | assert reference_time[0] == 0.0 |
---|
86 | assert reference_time[-1] == finaltime |
---|
87 | assert allclose(reference_time, input_time) |
---|
88 | |
---|
89 | for key in gauge_names: |
---|
90 | validation_data[key] = ensure_numeric(validation_data[key]) |
---|
91 | |
---|
92 | |
---|
93 | #-------------------------------------------------- |
---|
94 | # Read and interpolate model output |
---|
95 | #-------------------------------------------------- |
---|
96 | #f = file_function('okushiri_new_limiters.sww', #The best so far |
---|
97 | #f = file_function('okushiri_as2005_with_mxspd=0.1.sww', |
---|
98 | f = file_function(project.output_filename, |
---|
99 | quantities='stage', |
---|
100 | interpolation_points=gauge_locations, |
---|
101 | use_cache=True, |
---|
102 | verbose=True) |
---|
103 | |
---|
104 | |
---|
105 | #-------------------------------------------------- |
---|
106 | # Compare model output to validation data |
---|
107 | #-------------------------------------------------- |
---|
108 | |
---|
109 | eps = get_machine_precision() |
---|
110 | for k, name in enumerate(gauge_names): |
---|
111 | sqsum = 0 |
---|
112 | denom = 0 |
---|
113 | model = [] |
---|
114 | print |
---|
115 | print 'Validating ' + name |
---|
116 | observed_timeseries = validation_data[name] |
---|
117 | for i, t in enumerate(reference_time): |
---|
118 | model.append(f(t, point_id=k)[0]) |
---|
119 | |
---|
120 | # Covariance measures |
---|
121 | res = cov(observed_timeseries, model) |
---|
122 | print 'Covariance = %.18e' %res |
---|
123 | |
---|
124 | if res < expected_covariances[name]-eps: |
---|
125 | print 'Result is better than expected by: %.18e'\ |
---|
126 | %(res-expected_covariances[name]) |
---|
127 | print 'Expect = %.18e' %expected_covariances[name] |
---|
128 | elif res > expected_covariances[name]+eps: |
---|
129 | print 'FAIL: Result is worse than expected by: %.18e'\ |
---|
130 | %(res-expected_covariances[name]) |
---|
131 | print 'Expect = %.18e' %expected_covariances[name] |
---|
132 | else: |
---|
133 | pass |
---|
134 | |
---|
135 | # Difference measures |
---|
136 | res = sum(abs(observed_timeseries-model))/len(model) |
---|
137 | print 'Accumulated difference = %.18e' %res |
---|
138 | |
---|
139 | if res < expected_differences[name]-eps: |
---|
140 | print 'Result is better than expected by: %.18e'\ |
---|
141 | %(res-expected_differences[name]) |
---|
142 | print 'Expect = %.18e' %expected_differences[name] |
---|
143 | elif res > expected_differences[name]+eps: |
---|
144 | print 'FAIL: Result is worse than expected by: %.18e'\ |
---|
145 | %(res-expected_differences[name]) |
---|
146 | print 'Expect = %.18e' %expected_differences[name] |
---|
147 | else: |
---|
148 | pass |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | if plotting is True: |
---|
154 | ion() |
---|
155 | hold(False) |
---|
156 | |
---|
157 | plot(reference_time, validation_data[name], 'r.-', |
---|
158 | reference_time, model, 'k-') |
---|
159 | title('Gauge %s' %name) |
---|
160 | xlabel('time(s)') |
---|
161 | ylabel('stage (m)') |
---|
162 | legend(('Observed', 'Modelled'), shadow=True, loc='upper left') |
---|
163 | savefig(name, dpi = 300) |
---|
164 | |
---|
165 | raw_input('Next') |
---|
166 | |
---|
167 | |
---|
168 | |
---|