[3845] | 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. |
---|
[2229] | 5 | """ |
---|
| 6 | |
---|
[7184] | 7 | import numpy as num |
---|
[3850] | 8 | from Scientific.IO.NetCDF import NetCDFFile |
---|
[3845] | 9 | |
---|
[3850] | 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 |
---|
[3845] | 13 | |
---|
[3850] | 14 | import project |
---|
[2229] | 15 | |
---|
[3850] | 16 | try: |
---|
| 17 | from pylab import ion, hold, plot, title, legend, xlabel, ylabel, savefig |
---|
| 18 | except: |
---|
| 19 | plotting = False |
---|
| 20 | else: |
---|
| 21 | plotting = True |
---|
[2229] | 22 | |
---|
[4227] | 23 | print 'plotting', plotting |
---|
[3861] | 24 | #plotting = False |
---|
[2229] | 25 | |
---|
[3850] | 26 | #------------------------- |
---|
| 27 | # Basic data |
---|
| 28 | #------------------------- |
---|
[3845] | 29 | |
---|
[3850] | 30 | finaltime = 22.5 |
---|
| 31 | timestep = 0.05 |
---|
[2229] | 32 | |
---|
[3850] | 33 | gauge_locations = [[0.000, 1.696]] # Boundary gauge |
---|
| 34 | gauge_locations += [[4.521, 1.196], [4.521, 1.696], [4.521, 2.196]] #Ch 5-7-9 |
---|
[2229] | 35 | gauge_names = ['Boundary', 'ch5', 'ch7', 'ch9'] |
---|
| 36 | |
---|
[3850] | 37 | validation_data = {} |
---|
| 38 | for key in gauge_names: |
---|
| 39 | validation_data[key] = [] |
---|
[2229] | 40 | |
---|
| 41 | |
---|
[3850] | 42 | #------------------------- |
---|
| 43 | # Read validation dataa |
---|
| 44 | #------------------------- |
---|
[2229] | 45 | |
---|
[3850] | 46 | print 'Reading', project.boundary_filename |
---|
| 47 | fid = NetCDFFile(project.boundary_filename, 'r') |
---|
[2229] | 48 | input_time = fid.variables['time'][:] |
---|
[3850] | 49 | validation_data['Boundary'] = fid.variables['stage'][:] |
---|
[2229] | 50 | |
---|
| 51 | reference_time = [] |
---|
[3850] | 52 | fid = open(project.validation_filename) |
---|
[2229] | 53 | lines = fid.readlines() |
---|
| 54 | fid.close() |
---|
[3850] | 55 | |
---|
[2229] | 56 | for i, line in enumerate(lines[1:]): |
---|
[7199] | 57 | if i == len(input_time): break |
---|
[3850] | 58 | |
---|
[2229] | 59 | fields = line.split() |
---|
| 60 | |
---|
[3850] | 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 |
---|
| 64 | validation_data[key].append(value/100) # Convert cm2m |
---|
[2229] | 65 | |
---|
| 66 | |
---|
[3850] | 67 | # Checks |
---|
[2229] | 68 | assert reference_time[0] == 0.0 |
---|
| 69 | assert reference_time[-1] == finaltime |
---|
[7184] | 70 | assert num.allclose(reference_time, input_time) |
---|
[2229] | 71 | |
---|
[3850] | 72 | for key in gauge_names: |
---|
| 73 | validation_data[key] = ensure_numeric(validation_data[key]) |
---|
[2229] | 74 | |
---|
| 75 | |
---|
[3850] | 76 | #-------------------------------------------------- |
---|
| 77 | # Read and interpolate model output |
---|
| 78 | #-------------------------------------------------- |
---|
[3878] | 79 | |
---|
| 80 | import sys |
---|
| 81 | if len(sys.argv) > 1: |
---|
| 82 | sww_filename = sys.argv[1] |
---|
| 83 | else: |
---|
| 84 | sww_filename = project.output_filename |
---|
| 85 | |
---|
| 86 | f = file_function(sww_filename, |
---|
[3850] | 87 | quantities='stage', |
---|
| 88 | interpolation_points=gauge_locations, |
---|
| 89 | use_cache=True, |
---|
| 90 | verbose=True) |
---|
[2229] | 91 | |
---|
[4557] | 92 | #-------------------------------------------------- |
---|
| 93 | # Check max runup |
---|
| 94 | #-------------------------------------------------- |
---|
[2229] | 95 | |
---|
[4557] | 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 | |
---|
| 100 | q = get_maximum_inundation_elevation(sww_filename) |
---|
| 101 | loc = get_maximum_inundation_location(sww_filename) |
---|
| 102 | |
---|
| 103 | print 'Max runup elevation: ', q |
---|
| 104 | print 'Max runup elevation (scaled by 400): ', q*400 |
---|
| 105 | print 'Max runup location: ', loc |
---|
| 106 | |
---|
| 107 | #from create_okushiri import gulleys |
---|
| 108 | #assert is_inside_polygon(loc, gulleys) |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | |
---|
[3850] | 112 | #-------------------------------------------------- |
---|
| 113 | # Compare model output to validation data |
---|
| 114 | #-------------------------------------------------- |
---|
| 115 | |
---|
| 116 | eps = get_machine_precision() |
---|
[2229] | 117 | for k, name in enumerate(gauge_names): |
---|
| 118 | sqsum = 0 |
---|
| 119 | denom = 0 |
---|
| 120 | model = [] |
---|
[3850] | 121 | print |
---|
[2229] | 122 | print 'Validating ' + name |
---|
[3850] | 123 | observed_timeseries = validation_data[name] |
---|
[2229] | 124 | for i, t in enumerate(reference_time): |
---|
[3850] | 125 | model.append(f(t, point_id=k)[0]) |
---|
[2229] | 126 | |
---|
[3913] | 127 | |
---|
[3861] | 128 | # Covariance measures |
---|
[3850] | 129 | res = cov(observed_timeseries, model) |
---|
| 130 | print 'Covariance = %.18e' %res |
---|
[2229] | 131 | |
---|
[3861] | 132 | # Difference measures |
---|
| 133 | res = sum(abs(observed_timeseries-model))/len(model) |
---|
| 134 | print 'Accumulated difference = %.18e' %res |
---|
[2229] | 135 | |
---|
[3861] | 136 | |
---|
[3913] | 137 | # Extrema |
---|
| 138 | res = abs(max(observed_timeseries)-max(model)) |
---|
| 139 | print 'Difference in maxima = %.18e' %res |
---|
[3861] | 140 | |
---|
[3913] | 141 | |
---|
| 142 | res = abs(min(observed_timeseries)-min(model)) |
---|
| 143 | print 'Difference in minima = %.18e' %res |
---|
| 144 | |
---|
| 145 | # Locations of extrema |
---|
[7184] | 146 | i0 = num.argmax(observed_timeseries) |
---|
| 147 | i1 = num.argmax(model) |
---|
[3913] | 148 | res = abs(reference_time[i1] - reference_time[i0]) |
---|
| 149 | print 'Timelag between maxima = %.18e' %res |
---|
| 150 | |
---|
| 151 | |
---|
[7184] | 152 | i0 = num.argmin(observed_timeseries) |
---|
| 153 | i1 = num.argmin(model) |
---|
[3913] | 154 | res = abs(reference_time[i1] - reference_time[i0]) |
---|
| 155 | print 'Timelag between minima = %.18e' %res |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | |
---|
[3850] | 160 | if plotting is True: |
---|
| 161 | ion() |
---|
| 162 | hold(False) |
---|
| 163 | |
---|
[3913] | 164 | plot(reference_time, validation_data[name], 'r-', |
---|
[3850] | 165 | reference_time, model, 'k-') |
---|
| 166 | title('Gauge %s' %name) |
---|
| 167 | xlabel('time(s)') |
---|
| 168 | ylabel('stage (m)') |
---|
| 169 | legend(('Observed', 'Modelled'), shadow=True, loc='upper left') |
---|
| 170 | savefig(name, dpi = 300) |
---|
[2229] | 171 | |
---|
[3850] | 172 | raw_input('Next') |
---|
[2229] | 173 | |
---|
| 174 | |
---|
[3850] | 175 | |
---|