source: anuga_validation/automated_validation_tests/okushiri_tank_validation/compare_timeseries_with_measures.py @ 4844

Last change on this file since 4844 was 4844, checked in by ole, 16 years ago

Comments

File size: 7.1 KB
Line 
1"""Verify that simulation produced by ANUGA compares to published
2validation timeseries ch5, ch7 and ch9 as well as the boundary timeseries.
3
4RMS norm is printed and plots are produced as png files.
5No plots are shown on screen.
6"""
7
8from Numeric import allclose, argmin, argmax
9from Scientific.IO.NetCDF import NetCDFFile
10
11from anuga.abstract_2d_finite_volumes.util import file_function
12from anuga.utilities.numerical_tools import\
13     ensure_numeric, cov, get_machine_precision
14
15import project
16
17try:
18    from pylab import ion, hold, plot, title, legend, xlabel, ylabel, savefig
19except:
20    print 'Could not import pylab'
21    plotting = False
22else:
23    # Create plots as png files
24    plotting = True
25   
26
27#-------------------------
28# Basic data
29#-------------------------
30
31finaltime = 22.5
32timestep = 0.05
33
34gauge_locations = [[0.000, 1.696]] # Boundary gauge
35gauge_locations += [[4.521, 1.196],  [4.521, 1.696],  [4.521, 2.196]] #Ch 5-7-9
36gauge_names = ['Boundary', 'ch5', 'ch7', 'ch9']
37
38validation_data = {}
39for key in gauge_names:
40    validation_data[key] = []
41
42
43# Expected values
44expected_covariance = {'Boundary': 5.269569575007607815e-05, 
45                       'ch5': 1.166277999581819919e-04,
46                       'ch7': 1.127136457890861503e-04,
47                       'ch9': 1.250659477418482129e-04}
48
49expected_difference = {'Boundary': 8.350712673810733924e-04,
50                       'ch5': 3.405426180525532483e-03,
51                       'ch7': 2.852870417368218517e-03,
52                       'ch9': 3.248778982037564891e-03}
53
54expected_maximum = {'Boundary': 1.611749508386188523e-02, 
55                    'ch5': 3.551308418158714147e-02,
56                    'ch7': 3.858418457126511908e-02,
57                    'ch9': 4.317962986578308127e-02}
58
59expected_minimum = {'Boundary': -1.164547474575844919e-02, 
60                    'ch5': -8.664439185502026408e-03,
61                    'ch7': -2.726335488279797541e-03,
62                    'ch9': -5.977581218447349659e-03}
63
64expected_argmax = {'Boundary': 1.255000000000000071e+01, 
65                   'ch5': 1.839999999999999858e+01,
66                   'ch7': 1.700000000000000000e+01,
67                   'ch9': 1.685000000000000142e+01}
68
69expected_argmin = {'Boundary': 2.064999999999999858e+01, 
70                   'ch5': 1.459999999999999964e+01,
71                   'ch7': 1.230000000000000071e+01,
72                   'ch9': 1.315000000000000036e+01}
73
74#-------------------------
75# Read validation dataa
76#-------------------------
77
78print 'Reading', project.boundary_filename
79fid = NetCDFFile(project.boundary_filename, 'r')
80input_time = fid.variables['time'][:]
81validation_data['Boundary'] = fid.variables['stage'][:]
82
83reference_time = []
84fid = open(project.validation_filename)
85lines = fid.readlines()
86fid.close()
87
88for i, line in enumerate(lines[1:]):
89    if i == len(input_time): break
90   
91    fields = line.split()
92
93    reference_time.append(float(fields[0]))    # Record reference time
94    for j, key in enumerate(gauge_names[1:]):  # Omit boundary gauge
95        value = float(fields[1:][j])           # Omit time
96        validation_data[key].append(value/100) # Convert cm2m
97
98
99# Checks
100assert reference_time[0] == 0.0
101assert reference_time[-1] == finaltime
102assert allclose(reference_time, input_time)
103
104for key in gauge_names:
105    validation_data[key] = ensure_numeric(validation_data[key])
106
107#--------------------------------------------------
108# Read and interpolate model output
109#--------------------------------------------------
110
111import sys
112if len(sys.argv) > 1:
113    sww_filename = sys.argv[1]
114else:   
115    sww_filename = project.output_filename
116   
117f = file_function(sww_filename,
118                  quantities='stage',
119                  interpolation_points=gauge_locations,
120                  use_cache=True,
121                  verbose=True)
122
123
124def report_difference(name, computed_value, reference_value, rtol, atol):
125
126    if abs(reference_value) > 0:
127        msg = '%s (expected, computed):\n  (%.18e, %.18e):\n  Relative error=%.18e'\
128              %(name, reference_value, computed_value,
129                abs(reference_value-computed_value)/reference_value)
130        print msg
131       
132
133    msg = '  Absolute error=%.18e'\
134          %(abs(reference_value-computed_value))       
135    print msg
136
137   
138    #print 'Allclose:', allclose(reference_value, computed_value,
139    #                            rtol=rtol, atol=atol)
140    if plotting is False:
141        assert allclose(reference_value, computed_value,
142                        rtol=rtol, atol=atol), msg
143   
144
145
146#--------------------------------------------------
147# Compare model output to validation data
148#--------------------------------------------------
149
150
151#eps = get_machine_precision()
152
153# Windows tolerances
154rtol = 1.0e-2
155atol = 1.0e-3
156print 'Precisions used: rtol=%e, atol=%e' %(rtol, atol)
157
158for k, name in enumerate(gauge_names):
159
160    sqsum = 0
161    denom = 0
162    model = []
163    print 
164    print 'Validating ' + name
165    observed_timeseries = validation_data[name]
166    for i, t in enumerate(reference_time):
167        model.append(f(t, point_id=k)[0])
168
169    # Covariance measure   
170    res = cov(observed_timeseries, model)
171    report_difference('Covariance', res, expected_covariance[name], rtol, atol)
172     
173    # Difference measures   
174    res = sum(abs(observed_timeseries-model))/len(model)
175    report_difference('Accumulated difference', res,
176                      expected_difference[name], rtol, atol)   
177
178    # Extrema
179    res = max(model)
180    report_difference('Maximum', res, expected_maximum[name], rtol, atol)
181   
182    res = min(model)
183    report_difference('Minimum', res, expected_minimum[name], rtol, atol)   
184
185    # Locations of extrema
186    #i0 = argmax(observed_timeseries)
187    i1 = argmax(model)
188    res = reference_time[i1]
189    report_difference('Location of maximum', res, expected_argmax[name], rtol, atol)   
190   
191
192    if not name in ['ch7', 'ch9']:
193        # Minima of ch7 and ch9 are very flat and hard to pinpoint
194        i1 = argmin(model)
195        res = reference_time[i1]
196        report_difference('Location of minimum', res, expected_argmin[name],
197                          rtol, atol)       
198
199
200    if plotting is True:
201        #ion() # No plotting on screen
202        hold(False)
203   
204        plot(reference_time, validation_data[name], 'r-',
205             reference_time, model, 'k-')
206        title('Gauge %s' %name)
207        xlabel('time(s)')
208        ylabel('stage (m)')   
209        legend(('Observed', 'Modelled'), shadow=True, loc='upper left')
210        savefig(name, dpi = 300)       
211
212
213
214
215# Check max runup
216
217from anuga.shallow_water.data_manager import get_maximum_inundation_elevation
218from anuga.shallow_water.data_manager import get_maximum_inundation_location
219from anuga.utilities.polygon import is_inside_polygon
220
221q = get_maximum_inundation_elevation(sww_filename)
222loc = get_maximum_inundation_location(sww_filename)
223
224
225print 'Max runup elevation: ', q
226print 'Max runup elevation (scaled by 400): ', q*400
227print 'Max runup location:  ', loc
228
229
230from create_okushiri import gulleys
231assert is_inside_polygon(loc, gulleys)
232
233# FIXME more asserts here
234
235
236
237#msg = 'We got %f, should have been %f' %(q, q_max)
238#assert allclose(q, q_max, rtol=1.0/N), msg
239##print 'loc', loc, q
240#assert allclose(-loc[0]/2, q) # From topography formula
241
242print 'OK'
Note: See TracBrowser for help on using the repository browser.