[5343] | 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, argmin, argmax |
---|
| 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 | |
---|
[5345] | 14 | import project_truescale |
---|
[5343] | 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 | print 'plotting', plotting |
---|
| 24 | #plotting = False |
---|
| 25 | |
---|
| 26 | #------------------------- |
---|
| 27 | # Basic data |
---|
| 28 | #------------------------- |
---|
| 29 | |
---|
[5344] | 30 | finaltime = 450 |
---|
| 31 | timestep = 1 |
---|
[5343] | 32 | |
---|
[5344] | 33 | gauge_locations = [[0.000, 678.4]] # Boundary gauge |
---|
| 34 | gauge_locations += [[1808.4, 478.4], [1808.4, 678.4], [1808.4, 878.4]] #Ch 5-7-9 |
---|
[5343] | 35 | gauge_names = ['Boundary', 'ch5', 'ch7', 'ch9'] |
---|
| 36 | |
---|
| 37 | validation_data = {} |
---|
| 38 | for key in gauge_names: |
---|
| 39 | validation_data[key] = [] |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | #------------------------- |
---|
| 43 | # Read validation dataa |
---|
| 44 | #------------------------- |
---|
| 45 | |
---|
[5344] | 46 | print 'Reading', project_truescale.boundary_filename |
---|
| 47 | fid = NetCDFFile(project_truescale.boundary_filename, 'r') |
---|
[5343] | 48 | input_time = fid.variables['time'][:] |
---|
| 49 | validation_data['Boundary'] = fid.variables['stage'][:] |
---|
| 50 | |
---|
| 51 | reference_time = [] |
---|
[5344] | 52 | fid = open(project_truescale.validation_filename) |
---|
[5343] | 53 | lines = fid.readlines() |
---|
| 54 | fid.close() |
---|
| 55 | |
---|
| 56 | for i, line in enumerate(lines[1:]): |
---|
| 57 | if i == len(input_time): break |
---|
| 58 | |
---|
| 59 | fields = line.split() |
---|
| 60 | |
---|
| 61 | reference_time.append(float(fields[0])) # Record reference time |
---|
| 62 | for j, key in enumerate(gauge_names[1:]): # Omit boundary gauge |
---|
| 63 | value = float(fields[1:][j]) # Omit time |
---|
[5344] | 64 | validation_data[key].append(value) |
---|
[5343] | 65 | |
---|
| 66 | |
---|
| 67 | # Checks |
---|
| 68 | assert reference_time[0] == 0.0 |
---|
| 69 | assert reference_time[-1] == finaltime |
---|
| 70 | assert allclose(reference_time, input_time) |
---|
| 71 | |
---|
| 72 | for key in gauge_names: |
---|
| 73 | validation_data[key] = ensure_numeric(validation_data[key]) |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | #-------------------------------------------------- |
---|
| 77 | # Read and interpolate model output |
---|
| 78 | #-------------------------------------------------- |
---|
| 79 | |
---|
| 80 | import sys |
---|
| 81 | if len(sys.argv) > 1: |
---|
| 82 | sww_filename = sys.argv[1] |
---|
| 83 | else: |
---|
[5344] | 84 | sww_filename = project_truescale.output_filename |
---|
[5343] | 85 | |
---|
| 86 | f = file_function(sww_filename, |
---|
| 87 | quantities='stage', |
---|
| 88 | interpolation_points=gauge_locations, |
---|
| 89 | use_cache=True, |
---|
| 90 | verbose=True) |
---|
| 91 | |
---|
| 92 | #-------------------------------------------------- |
---|
| 93 | # Check max runup |
---|
| 94 | #-------------------------------------------------- |
---|
| 95 | |
---|
| 96 | from anuga.shallow_water.data_manager import get_maximum_inundation_elevation |
---|
| 97 | from anuga.shallow_water.data_manager import get_maximum_inundation_location |
---|
| 98 | from anuga.utilities.polygon import is_inside_polygon |
---|
| 99 | |
---|
[5397] | 100 | q = get_maximum_inundation_elevation(sww_filename, tolerance=.4) |
---|
| 101 | loc = get_maximum_inundation_location(sww_filename, tolerance=.4) |
---|
[5343] | 102 | |
---|
| 103 | print 'Max runup elevation: ', q |
---|
| 104 | print 'Max runup location: ', loc |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | #-------------------------------------------------- |
---|
| 109 | # Compare model output to validation data |
---|
| 110 | #-------------------------------------------------- |
---|
| 111 | |
---|
| 112 | eps = get_machine_precision() |
---|
| 113 | for k, name in enumerate(gauge_names): |
---|
| 114 | sqsum = 0 |
---|
| 115 | denom = 0 |
---|
| 116 | model = [] |
---|
| 117 | print |
---|
| 118 | print 'Validating ' + name |
---|
| 119 | observed_timeseries = validation_data[name] |
---|
| 120 | for i, t in enumerate(reference_time): |
---|
| 121 | model.append(f(t, point_id=k)[0]) |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | # Covariance measures |
---|
| 125 | res = cov(observed_timeseries, model) |
---|
| 126 | print 'Covariance = %.18e' %res |
---|
| 127 | |
---|
| 128 | # Difference measures |
---|
| 129 | res = sum(abs(observed_timeseries-model))/len(model) |
---|
| 130 | print 'Accumulated difference = %.18e' %res |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | # Extrema |
---|
| 134 | res = abs(max(observed_timeseries)-max(model)) |
---|
| 135 | print 'Difference in maxima = %.18e' %res |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | res = abs(min(observed_timeseries)-min(model)) |
---|
| 139 | print 'Difference in minima = %.18e' %res |
---|
| 140 | |
---|
| 141 | # Locations of extrema |
---|
| 142 | i0 = argmax(observed_timeseries) |
---|
| 143 | i1 = argmax(model) |
---|
| 144 | res = abs(reference_time[i1] - reference_time[i0]) |
---|
| 145 | print 'Timelag between maxima = %.18e' %res |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | i0 = argmin(observed_timeseries) |
---|
| 149 | i1 = argmin(model) |
---|
| 150 | res = abs(reference_time[i1] - reference_time[i0]) |
---|
| 151 | print 'Timelag between minima = %.18e' %res |
---|
| 152 | |
---|
| 153 | |
---|
[5397] | 154 | # Write timeseries at gauge locations to file |
---|
| 155 | filename=name+'_truescale_model.txt' |
---|
| 156 | fid=open(filename,'w') |
---|
| 157 | fid.write('Time (s), Stage (m)\n') |
---|
| 158 | for i, t in enumerate(reference_time): |
---|
| 159 | v=model[i] |
---|
| 160 | fid.write('%.2f, %.2f\n' %(t, v)) |
---|
[5343] | 161 | |
---|
[5397] | 162 | fid.close() |
---|
| 163 | |
---|
[5343] | 164 | |
---|
| 165 | if plotting is True: |
---|
| 166 | ion() |
---|
| 167 | hold(False) |
---|
| 168 | |
---|
| 169 | plot(reference_time, validation_data[name], 'r-', |
---|
| 170 | reference_time, model, 'k-') |
---|
| 171 | title('Gauge %s' %name) |
---|
| 172 | xlabel('time(s)') |
---|
| 173 | ylabel('stage (m)') |
---|
| 174 | legend(('Observed', 'Modelled'), shadow=True, loc='upper left') |
---|
| 175 | savefig(name, dpi = 300) |
---|
| 176 | |
---|
| 177 | raw_input('Next') |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | |
---|