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 | #expected_covariances = {'Boundary': 5.288392008865989237e-05, |
---|
59 | # 'ch5': 1.166748190444680592e-04, |
---|
60 | # 'ch7': 1.121816242516757850e-04, |
---|
61 | # 'ch9': 1.249543278366777640e-04} |
---|
62 | # |
---|
63 | #expected_differences = {'Boundary': 8.373150808730501615e-04, |
---|
64 | # 'ch5': 3.425914311580337875e-03, |
---|
65 | # 'ch7': 2.802327594773105189e-03, |
---|
66 | # 'ch9': 3.198733498646373370e-03} |
---|
67 | |
---|
68 | |
---|
69 | expected_covariances = {'Boundary': 5.299487474489660856e-05, |
---|
70 | 'ch5': 1.169650160375980370e-04, |
---|
71 | 'ch7': 1.123947836450141360e-04, |
---|
72 | 'ch9': 1.248329330436513066e-04} |
---|
73 | |
---|
74 | expected_differences = {'Boundary': 8.269932106586290379e-04, |
---|
75 | 'ch5': 3.411769502897898498e-03, |
---|
76 | 'ch7': 2.777024544282339583e-03, |
---|
77 | 'ch9': 3.183530359567784788e-03} |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | #------------------------- |
---|
84 | # Read validation dataa |
---|
85 | #------------------------- |
---|
86 | |
---|
87 | print 'Reading', project.boundary_filename |
---|
88 | fid = NetCDFFile(project.boundary_filename, 'r') |
---|
89 | input_time = fid.variables['time'][:] |
---|
90 | validation_data['Boundary'] = fid.variables['stage'][:] |
---|
91 | |
---|
92 | reference_time = [] |
---|
93 | fid = open(project.validation_filename) |
---|
94 | lines = fid.readlines() |
---|
95 | fid.close() |
---|
96 | |
---|
97 | for i, line in enumerate(lines[1:]): |
---|
98 | if i == len(input_time): break |
---|
99 | |
---|
100 | fields = line.split() |
---|
101 | |
---|
102 | reference_time.append(float(fields[0])) # Record reference time |
---|
103 | for j, key in enumerate(gauge_names[1:]): # Omit boundary gauge |
---|
104 | value = float(fields[1:][j]) # Omit time |
---|
105 | validation_data[key].append(value/100) # Convert cm2m |
---|
106 | |
---|
107 | |
---|
108 | # Checks |
---|
109 | assert reference_time[0] == 0.0 |
---|
110 | assert reference_time[-1] == finaltime |
---|
111 | assert allclose(reference_time, input_time) |
---|
112 | |
---|
113 | for key in gauge_names: |
---|
114 | validation_data[key] = ensure_numeric(validation_data[key]) |
---|
115 | |
---|
116 | |
---|
117 | #-------------------------------------------------- |
---|
118 | # Read and interpolate model output |
---|
119 | #-------------------------------------------------- |
---|
120 | |
---|
121 | import sys |
---|
122 | if len(sys.argv) > 1: |
---|
123 | sww_filename = sys.argv[1] |
---|
124 | else: |
---|
125 | sww_filename = project.output_filename |
---|
126 | |
---|
127 | #f = file_function('okushiri_new_limiters.sww', #The best so far |
---|
128 | #f = file_function('okushiri_as2005_with_mxspd=0.1.sww', |
---|
129 | f = file_function(sww_filename, |
---|
130 | quantities='stage', |
---|
131 | interpolation_points=gauge_locations, |
---|
132 | use_cache=True, |
---|
133 | verbose=True) |
---|
134 | |
---|
135 | |
---|
136 | #-------------------------------------------------- |
---|
137 | # Compare model output to validation data |
---|
138 | #-------------------------------------------------- |
---|
139 | |
---|
140 | eps = get_machine_precision() |
---|
141 | for k, name in enumerate(gauge_names): |
---|
142 | sqsum = 0 |
---|
143 | denom = 0 |
---|
144 | model = [] |
---|
145 | print |
---|
146 | print 'Validating ' + name |
---|
147 | observed_timeseries = validation_data[name] |
---|
148 | for i, t in enumerate(reference_time): |
---|
149 | model.append(f(t, point_id=k)[0]) |
---|
150 | |
---|
151 | # Covariance measures |
---|
152 | res = cov(observed_timeseries, model) |
---|
153 | print 'Covariance = %.18e' %res |
---|
154 | |
---|
155 | if res < expected_covariances[name]-eps: |
---|
156 | print 'Result is better than expected by: %.18e'\ |
---|
157 | %(res-expected_covariances[name]) |
---|
158 | print 'Expect = %.18e' %expected_covariances[name] |
---|
159 | elif res > expected_covariances[name]+eps: |
---|
160 | print 'FAIL: Result is worse than expected by: %.18e'\ |
---|
161 | %(res-expected_covariances[name]) |
---|
162 | print 'Expect = %.18e' %expected_covariances[name] |
---|
163 | else: |
---|
164 | pass |
---|
165 | |
---|
166 | # Difference measures |
---|
167 | res = sum(abs(observed_timeseries-model))/len(model) |
---|
168 | print 'Accumulated difference = %.18e' %res |
---|
169 | |
---|
170 | if res < expected_differences[name]-eps: |
---|
171 | print 'Result is better than expected by: %.18e'\ |
---|
172 | %(res-expected_differences[name]) |
---|
173 | print 'Expect = %.18e' %expected_differences[name] |
---|
174 | elif res > expected_differences[name]+eps: |
---|
175 | print 'FAIL: Result is worse than expected by: %.18e'\ |
---|
176 | %(res-expected_differences[name]) |
---|
177 | print 'Expect = %.18e' %expected_differences[name] |
---|
178 | else: |
---|
179 | pass |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | if plotting is True: |
---|
185 | ion() |
---|
186 | hold(False) |
---|
187 | |
---|
188 | plot(reference_time, validation_data[name], 'r.-', |
---|
189 | reference_time, model, 'k-') |
---|
190 | title('Gauge %s' %name) |
---|
191 | xlabel('time(s)') |
---|
192 | ylabel('stage (m)') |
---|
193 | legend(('Observed', 'Modelled'), shadow=True, loc='upper left') |
---|
194 | savefig(name, dpi = 300) |
---|
195 | |
---|
196 | raw_input('Next') |
---|
197 | |
---|
198 | |
---|
199 | |
---|