source: anuga_validation/okushiri_2005/extract_timeseries.py @ 3883

Last change on this file since 3883 was 3883, checked in by ole, 17 years ago

More cleanup of okushiri prior to release

File size: 6.1 KB
RevLine 
[3845]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.
[2229]5"""
6
[3850]7from Numeric import allclose
8from Scientific.IO.NetCDF import NetCDFFile
[3845]9
[3850]10from anuga.abstract_2d_finite_volumes.util import file_function
11from anuga.utilities.numerical_tools import\
12     ensure_numeric, cov, get_machine_precision
[3845]13
[3850]14import project
[2229]15
[3850]16try:
17    from pylab import ion, hold, plot, title, legend, xlabel, ylabel, savefig
18except:
19    plotting = False
20else:
21    plotting = True
[2229]22
[3861]23#plotting = False
[2229]24
[3850]25#-------------------------
26# Basic data
27#-------------------------
[3845]28
[3850]29finaltime = 22.5
30timestep = 0.05
[2229]31
[3850]32gauge_locations = [[0.000, 1.696]] # Boundary gauge
33gauge_locations += [[4.521, 1.196],  [4.521, 1.696],  [4.521, 2.196]] #Ch 5-7-9
[2229]34gauge_names = ['Boundary', 'ch5', 'ch7', 'ch9']
35
[3850]36validation_data = {}
37for key in gauge_names:
38    validation_data[key] = []
[2229]39
[3850]40   
[3861]41#expected_covariances = {'Boundary': 5.288392008865989e-05,
42#                        'ch5': 1.166748190444681e-04,
43#                        'ch7': 1.121816242516758e-04,
44#                        'ch9': 1.249543278366778e-04}
[2229]45
[3854]46# old limiters
[3878]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}
[2229]56
[3854]57
[3883]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}
[3861]67
68
[3883]69expected_covariances = {'Boundary': 5.278365381306500051e-05, 
70                        'ch5': 1.168250251701857399e-04,
71                        'ch7': 1.125146958894896555e-04,
72                        'ch9': 1.248634476898675049e-04}
[3878]73
[3883]74expected_differences = {'Boundary': 8.502104901511024380e-04, 
75                        'ch5': 3.442302747303998579e-03,
76                        'ch7': 2.802987591027187534e-03,
77                        'ch9': 3.191368834008508938e-03}
[3878]78
[3883]79
80
81
82
[3850]83#-------------------------
84# Read validation dataa
85#-------------------------
[2229]86
[3850]87print 'Reading', project.boundary_filename
88fid = NetCDFFile(project.boundary_filename, 'r')
[2229]89input_time = fid.variables['time'][:]
[3850]90validation_data['Boundary'] = fid.variables['stage'][:]
[2229]91
92reference_time = []
[3850]93fid = open(project.validation_filename)
[2229]94lines = fid.readlines()
95fid.close()
[3850]96
[2229]97for i, line in enumerate(lines[1:]):
98    if i == len(input_time): break
[3850]99   
[2229]100    fields = line.split()
101
[3850]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
[2229]106
107
[3850]108# Checks
[2229]109assert reference_time[0] == 0.0
110assert reference_time[-1] == finaltime
111assert allclose(reference_time, input_time)
112
[3850]113for key in gauge_names:
114    validation_data[key] = ensure_numeric(validation_data[key])
[2229]115
116
[3850]117#--------------------------------------------------
118# Read and interpolate model output
119#--------------------------------------------------
[3878]120
121import sys
122if len(sys.argv) > 1:
123    sww_filename = sys.argv[1]
124else:   
125    sww_filename = project.output_filename
126   
[3854]127#f = file_function('okushiri_new_limiters.sww',   #The best so far
[3850]128#f = file_function('okushiri_as2005_with_mxspd=0.1.sww',
[3878]129f = file_function(sww_filename,
[3850]130                  quantities='stage',
131                  interpolation_points=gauge_locations,
132                  use_cache=True,
133                  verbose=True)
[2229]134
135
[3850]136#--------------------------------------------------
137# Compare model output to validation data
138#--------------------------------------------------
139
140eps = get_machine_precision()
[2229]141for k, name in enumerate(gauge_names):
142    sqsum = 0
143    denom = 0
144    model = []
[3850]145    print 
[2229]146    print 'Validating ' + name
[3850]147    observed_timeseries = validation_data[name]
[2229]148    for i, t in enumerate(reference_time):
[3850]149        model.append(f(t, point_id=k)[0])
[2229]150
[3861]151    # Covariance measures   
[3850]152    res = cov(observed_timeseries, model)     
153    print 'Covariance = %.18e' %res
[2229]154
[3850]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:
[3854]160        print 'FAIL: Result is worse than expected by: %.18e'\
161                                %(res-expected_covariances[name])
162        print 'Expect = %.18e' %expected_covariances[name]
[3850]163    else:
164        pass
[2229]165
[3861]166    # Difference measures   
167    res = sum(abs(observed_timeseries-model))/len(model)     
168    print 'Accumulated difference = %.18e' %res
[2229]169
[3861]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
[3850]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)       
[2229]195
[3850]196        raw_input('Next')
[2229]197
198
[3850]199
Note: See TracBrowser for help on using the repository browser.