[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 | |
---|
[3913] | 7 | from Numeric import allclose, argmin, argmax |
---|
[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 | |
---|
[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 | |
---|
| 40 | |
---|
[3850] | 41 | #------------------------- |
---|
| 42 | # Read validation dataa |
---|
| 43 | #------------------------- |
---|
[2229] | 44 | |
---|
[3850] | 45 | print 'Reading', project.boundary_filename |
---|
| 46 | fid = NetCDFFile(project.boundary_filename, 'r') |
---|
[2229] | 47 | input_time = fid.variables['time'][:] |
---|
[3850] | 48 | validation_data['Boundary'] = fid.variables['stage'][:] |
---|
[2229] | 49 | |
---|
| 50 | reference_time = [] |
---|
[3850] | 51 | fid = open(project.validation_filename) |
---|
[2229] | 52 | lines = fid.readlines() |
---|
| 53 | fid.close() |
---|
[3850] | 54 | |
---|
[2229] | 55 | for i, line in enumerate(lines[1:]): |
---|
| 56 | if i == len(input_time): break |
---|
[3850] | 57 | |
---|
[2229] | 58 | fields = line.split() |
---|
| 59 | |
---|
[3850] | 60 | reference_time.append(float(fields[0])) # Record reference time |
---|
| 61 | for j, key in enumerate(gauge_names[1:]): # Omit boundary gauge |
---|
| 62 | value = float(fields[1:][j]) # Omit time |
---|
| 63 | validation_data[key].append(value/100) # Convert cm2m |
---|
[2229] | 64 | |
---|
| 65 | |
---|
[3850] | 66 | # Checks |
---|
[2229] | 67 | assert reference_time[0] == 0.0 |
---|
| 68 | assert reference_time[-1] == finaltime |
---|
| 69 | assert allclose(reference_time, input_time) |
---|
| 70 | |
---|
[3850] | 71 | for key in gauge_names: |
---|
| 72 | validation_data[key] = ensure_numeric(validation_data[key]) |
---|
[2229] | 73 | |
---|
| 74 | |
---|
[3850] | 75 | #-------------------------------------------------- |
---|
| 76 | # Read and interpolate model output |
---|
| 77 | #-------------------------------------------------- |
---|
[3878] | 78 | |
---|
| 79 | import sys |
---|
| 80 | if len(sys.argv) > 1: |
---|
| 81 | sww_filename = sys.argv[1] |
---|
| 82 | else: |
---|
| 83 | sww_filename = project.output_filename |
---|
| 84 | |
---|
| 85 | f = file_function(sww_filename, |
---|
[3850] | 86 | quantities='stage', |
---|
| 87 | interpolation_points=gauge_locations, |
---|
| 88 | use_cache=True, |
---|
| 89 | verbose=True) |
---|
[2229] | 90 | |
---|
| 91 | |
---|
[3850] | 92 | #-------------------------------------------------- |
---|
| 93 | # Compare model output to validation data |
---|
| 94 | #-------------------------------------------------- |
---|
| 95 | |
---|
| 96 | eps = get_machine_precision() |
---|
[2229] | 97 | for k, name in enumerate(gauge_names): |
---|
| 98 | sqsum = 0 |
---|
| 99 | denom = 0 |
---|
| 100 | model = [] |
---|
[3850] | 101 | print |
---|
[2229] | 102 | print 'Validating ' + name |
---|
[3850] | 103 | observed_timeseries = validation_data[name] |
---|
[2229] | 104 | for i, t in enumerate(reference_time): |
---|
[3850] | 105 | model.append(f(t, point_id=k)[0]) |
---|
[2229] | 106 | |
---|
[3913] | 107 | |
---|
[3861] | 108 | # Covariance measures |
---|
[3850] | 109 | res = cov(observed_timeseries, model) |
---|
| 110 | print 'Covariance = %.18e' %res |
---|
[2229] | 111 | |
---|
[3861] | 112 | # Difference measures |
---|
| 113 | res = sum(abs(observed_timeseries-model))/len(model) |
---|
| 114 | print 'Accumulated difference = %.18e' %res |
---|
[2229] | 115 | |
---|
[3861] | 116 | |
---|
[3913] | 117 | # Extrema |
---|
| 118 | res = abs(max(observed_timeseries)-max(model)) |
---|
| 119 | print 'Difference in maxima = %.18e' %res |
---|
[3861] | 120 | |
---|
[3913] | 121 | |
---|
| 122 | res = abs(min(observed_timeseries)-min(model)) |
---|
| 123 | print 'Difference in minima = %.18e' %res |
---|
| 124 | |
---|
| 125 | # Locations of extrema |
---|
| 126 | i0 = argmax(observed_timeseries) |
---|
| 127 | i1 = argmax(model) |
---|
| 128 | res = abs(reference_time[i1] - reference_time[i0]) |
---|
| 129 | print 'Timelag between maxima = %.18e' %res |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | i0 = argmin(observed_timeseries) |
---|
| 133 | i1 = argmin(model) |
---|
| 134 | res = abs(reference_time[i1] - reference_time[i0]) |
---|
| 135 | print 'Timelag between minima = %.18e' %res |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | |
---|
[3850] | 140 | if plotting is True: |
---|
| 141 | ion() |
---|
| 142 | hold(False) |
---|
| 143 | |
---|
[3913] | 144 | plot(reference_time, validation_data[name], 'r-', |
---|
[3850] | 145 | reference_time, model, 'k-') |
---|
| 146 | title('Gauge %s' %name) |
---|
| 147 | xlabel('time(s)') |
---|
| 148 | ylabel('stage (m)') |
---|
| 149 | legend(('Observed', 'Modelled'), shadow=True, loc='upper left') |
---|
| 150 | savefig(name, dpi = 300) |
---|
[2229] | 151 | |
---|
[3850] | 152 | raw_input('Next') |
---|
[2229] | 153 | |
---|
| 154 | |
---|
[3850] | 155 | |
---|