1 | """Automated validation of ANUGA. |
---|
2 | |
---|
3 | This script runs the benchmark on a regular grid and verifies that |
---|
4 | the predicted timeseries at selected gauges are close enought to the observed |
---|
5 | timeseries provided from the experiment. |
---|
6 | |
---|
7 | See anuga_validation/okushiri_2005 for scripts that produce simulations at |
---|
8 | greater resolution. |
---|
9 | |
---|
10 | """ |
---|
11 | |
---|
12 | # Module imports |
---|
13 | from Numeric import array, zeros, Float, allclose |
---|
14 | |
---|
15 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
16 | from anuga.shallow_water import Domain |
---|
17 | from anuga.shallow_water import Reflective_boundary |
---|
18 | from anuga.shallow_water import Transmissive_Momentum_Set_Stage_boundary |
---|
19 | from anuga.abstract_2d_finite_volumes.mesh_factory import rectangular_cross |
---|
20 | from anuga.abstract_2d_finite_volumes.util import file_function |
---|
21 | |
---|
22 | import project |
---|
23 | |
---|
24 | try: |
---|
25 | import pylab |
---|
26 | except: |
---|
27 | plotting = False |
---|
28 | else: |
---|
29 | plotting = True |
---|
30 | |
---|
31 | |
---|
32 | finaltime = 22.5 |
---|
33 | timestep = 0.05 |
---|
34 | |
---|
35 | #------------------------- |
---|
36 | # Create Domain |
---|
37 | #------------------------- |
---|
38 | N = 50 |
---|
39 | #N = 10 |
---|
40 | points, vertices, boundary = rectangular_cross(N, N/5*3, |
---|
41 | len1=5.448, len2=3.402) |
---|
42 | domain = Domain(points, vertices, boundary) |
---|
43 | |
---|
44 | domain.set_name('okushiri_automated_validation') |
---|
45 | domain.set_default_order(2) |
---|
46 | domain.set_minimum_storable_height(0.001) |
---|
47 | domain.check_integrity() |
---|
48 | |
---|
49 | |
---|
50 | #------------------------- |
---|
51 | # Initial Conditions |
---|
52 | #------------------------- |
---|
53 | domain.set_quantity('friction', 0.0) |
---|
54 | domain.set_quantity('stage', 0.0) |
---|
55 | domain.set_quantity('elevation', |
---|
56 | filename=project.bathymetry_filename[:-4] + '.pts', |
---|
57 | alpha=0.02, |
---|
58 | verbose=True, |
---|
59 | use_cache=True) |
---|
60 | |
---|
61 | #------------------------- |
---|
62 | # Boundary Conditions |
---|
63 | #------------------------- |
---|
64 | Br = Reflective_boundary(domain) |
---|
65 | |
---|
66 | function = file_function(project.boundary_filename[:-4] + '.tms', |
---|
67 | domain, |
---|
68 | verbose=True) |
---|
69 | Bts = Transmissive_Momentum_Set_Stage_boundary(domain, function) |
---|
70 | domain.set_boundary({'left': Bts, 'right': Br, 'bottom': Br, 'top': Br}) |
---|
71 | |
---|
72 | |
---|
73 | #------------------------- |
---|
74 | # Set up validation data |
---|
75 | #------------------------- |
---|
76 | gauge_locations = [[0.000, 1.696]] #Boundary |
---|
77 | gauge_locations += [[4.521, 1.196], [4.521, 1.696], [4.521, 2.196]] #Ch 5-7-9 |
---|
78 | |
---|
79 | #Boundary input wave |
---|
80 | filename = project.boundary_filename[:-4] + '.tms' |
---|
81 | print 'Reading', filename |
---|
82 | from Scientific.IO.NetCDF import NetCDFFile |
---|
83 | from Numeric import allclose |
---|
84 | fid = NetCDFFile(filename, 'r')# |
---|
85 | |
---|
86 | input_time = fid.variables['time'][:] |
---|
87 | input_stage = fid.variables['stage'][:] |
---|
88 | |
---|
89 | |
---|
90 | # reference gauge timeseries |
---|
91 | reference_time = [] |
---|
92 | ch5 = [] |
---|
93 | ch7 = [] |
---|
94 | ch9 = [] |
---|
95 | fid = open('output_ch5-7-9.txt') |
---|
96 | lines = fid.readlines() |
---|
97 | fid.close() |
---|
98 | #for i, line in enumerate(lines[1:]): |
---|
99 | for line in lines[1:]: |
---|
100 | fields = line.split() |
---|
101 | |
---|
102 | reference_time.append(float(fields[0])) |
---|
103 | ch5.append(float(fields[1])/100) #cm2m |
---|
104 | ch7.append(float(fields[2])/100) #cm2m |
---|
105 | ch9.append(float(fields[3])/100) #cm2m |
---|
106 | |
---|
107 | if fields[0] == str(finaltime): break |
---|
108 | |
---|
109 | |
---|
110 | assert reference_time[0] == 0.0 |
---|
111 | assert reference_time[-1] == finaltime |
---|
112 | |
---|
113 | reference_gauge_values = [ensure_numeric(input_stage), |
---|
114 | ensure_numeric(ch5), |
---|
115 | ensure_numeric(ch7), |
---|
116 | ensure_numeric(ch9)] |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | predicted_gauge_values = [[], [], [], []] |
---|
121 | |
---|
122 | |
---|
123 | #------------------------- |
---|
124 | # Evolve through time |
---|
125 | #------------------------- |
---|
126 | import time |
---|
127 | t0 = time.time() |
---|
128 | |
---|
129 | for j, t in enumerate(domain.evolve(yieldstep = timestep, |
---|
130 | finaltime = finaltime)): |
---|
131 | domain.write_time() |
---|
132 | |
---|
133 | # Record gauge values |
---|
134 | stage = domain.get_quantity('stage') |
---|
135 | for i, (x, y) in enumerate(gauge_locations): |
---|
136 | w = stage.get_values(interpolation_points=[[x,y]])[0] |
---|
137 | predicted_gauge_values[i].append(w) |
---|
138 | |
---|
139 | #print 'difference', x, y,\ |
---|
140 | # predicted_gauge_values[i][j] - reference_gauge_values[i][j] |
---|
141 | |
---|
142 | |
---|
143 | #------------------------- |
---|
144 | # Validate |
---|
145 | #------------------------- |
---|
146 | for i, (x, y) in enumerate(gauge_locations): |
---|
147 | predicted_gauge_values[i] = ensure_numeric(predicted_gauge_values[i]) |
---|
148 | |
---|
149 | print predicted_gauge_values[i].shape |
---|
150 | print reference_gauge_values[i].shape |
---|
151 | #print predicted_gauge_values.shape |
---|
152 | #print reference_gauge_values.shape |
---|
153 | |
---|
154 | sqsum = sum((predicted_gauge_values[i].flat - \ |
---|
155 | reference_gauge_values[i].flat)**2) |
---|
156 | denom = sum(reference_gauge_values[i].flat**2) |
---|
157 | |
---|
158 | print i, sqsum, sqsum/denom |
---|
159 | |
---|
160 | msg = 'Rms error is %f' %(sqsum/denom) |
---|
161 | print msg |
---|
162 | |
---|
163 | assert sqsum/denom < 0.01, msg |
---|
164 | |
---|
165 | |
---|
166 | if plotting is True: |
---|
167 | |
---|
168 | from pylab import * |
---|
169 | |
---|
170 | ion() |
---|
171 | hold(False) |
---|
172 | plot(reference_time, reference_gauge_values[i], 'r.-', |
---|
173 | reference_time, predicted_gauge_values[i], 'k-') |
---|
174 | title('Gauge %s' %name) |
---|
175 | xlabel('time(s)') |
---|
176 | ylabel('stage (m)') |
---|
177 | legend(('Observed', 'Modelled'), shadow=True, loc='upper left') |
---|
178 | savefig(name, dpi = 300) |
---|
179 | |
---|
180 | raw_input('Next') |
---|
181 | |
---|
182 | |
---|
183 | if plotting is True: |
---|
184 | show() |
---|
185 | |
---|
186 | |
---|
187 | print 'That took %.2f seconds' %(time.time()-t0) |
---|