[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 | |
---|
[3850] | 7 | from Numeric import allclose |
---|
| 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 | |
---|
[3861] | 23 | #plotting = False |
---|
[2229] | 24 | |
---|
[3850] | 25 | #------------------------- |
---|
| 26 | # Basic data |
---|
| 27 | #------------------------- |
---|
[3845] | 28 | |
---|
[3850] | 29 | finaltime = 22.5 |
---|
| 30 | timestep = 0.05 |
---|
[2229] | 31 | |
---|
[3850] | 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 |
---|
[2229] | 34 | gauge_names = ['Boundary', 'ch5', 'ch7', 'ch9'] |
---|
| 35 | |
---|
[3850] | 36 | validation_data = {} |
---|
| 37 | for 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 | |
---|
[3895] | 69 | expected_covariances = {'Boundary': 5.299487474489660856e-05, |
---|
| 70 | 'ch5': 1.169650160375980370e-04, |
---|
| 71 | 'ch7': 1.123947836450141360e-04, |
---|
| 72 | 'ch9': 1.248329330436513066e-04} |
---|
[3878] | 73 | |
---|
[3895] | 74 | expected_differences = {'Boundary': 8.269932106586290379e-04, |
---|
| 75 | 'ch5': 3.411769502897898498e-03, |
---|
| 76 | 'ch7': 2.777024544282339583e-03, |
---|
| 77 | 'ch9': 3.183530359567784788e-03} |
---|
[3878] | 78 | |
---|
[3883] | 79 | |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | |
---|
[3850] | 83 | #------------------------- |
---|
| 84 | # Read validation dataa |
---|
| 85 | #------------------------- |
---|
[2229] | 86 | |
---|
[3850] | 87 | print 'Reading', project.boundary_filename |
---|
| 88 | fid = NetCDFFile(project.boundary_filename, 'r') |
---|
[2229] | 89 | input_time = fid.variables['time'][:] |
---|
[3850] | 90 | validation_data['Boundary'] = fid.variables['stage'][:] |
---|
[2229] | 91 | |
---|
| 92 | reference_time = [] |
---|
[3850] | 93 | fid = open(project.validation_filename) |
---|
[2229] | 94 | lines = fid.readlines() |
---|
| 95 | fid.close() |
---|
[3850] | 96 | |
---|
[2229] | 97 | for 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] | 109 | assert reference_time[0] == 0.0 |
---|
| 110 | assert reference_time[-1] == finaltime |
---|
| 111 | assert allclose(reference_time, input_time) |
---|
| 112 | |
---|
[3850] | 113 | for 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 | |
---|
| 121 | import sys |
---|
| 122 | if len(sys.argv) > 1: |
---|
| 123 | sww_filename = sys.argv[1] |
---|
| 124 | else: |
---|
| 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] | 129 | f = 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 | |
---|
| 140 | eps = get_machine_precision() |
---|
[2229] | 141 | for 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 | |
---|