[3530] | 1 | """Simple water flow example using ANUGA |
---|
| 2 | |
---|
| 3 | Water driven up a linear slope and time varying boundary, |
---|
| 4 | similar to a beach environment. |
---|
| 5 | |
---|
| 6 | The study area is discretised as a regular triangular grid 100m x 100m |
---|
| 7 | """ |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | #------------------------------------------------------------------------------ |
---|
| 11 | # Import necessary modules |
---|
| 12 | #------------------------------------------------------------------------------ |
---|
| 13 | |
---|
| 14 | import sys |
---|
| 15 | |
---|
[3535] | 16 | from anuga.pmesh.mesh_interface import create_mesh_from_regions |
---|
[3560] | 17 | from abstract_2d_finite_volumes.mesh_factory import rectangular_cross |
---|
| 18 | from abstract_2d_finite_volumes.shallow_water import Domain |
---|
| 19 | from abstract_2d_finite_volumes.shallow_water import Reflective_boundary |
---|
| 20 | from abstract_2d_finite_volumes.shallow_water import Dirichlet_boundary |
---|
| 21 | from abstract_2d_finite_volumes.shallow_water import Time_boundary |
---|
| 22 | from abstract_2d_finite_volumes.shallow_water import Transmissive_Momentum_Set_Stage_boundary |
---|
| 23 | from abstract_2d_finite_volumes.util import file_function |
---|
[3530] | 24 | from pylab import plot, xlabel, ylabel, title, ion, close, savefig, figure, axis, legend, grid, hold |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | #------------------------------------------------------------------------------ |
---|
| 29 | # Model constants |
---|
| 30 | |
---|
| 31 | slope = -0.05 # 1:20 Slope |
---|
| 32 | highest_point = 6 # Highest elevation (m) |
---|
| 33 | sea_level = 0 # Mean sea level |
---|
| 34 | min_elevation = -20 # Lowest elevation (elevation of offshore flat part) |
---|
| 35 | offshore_depth=sea_level-min_elevation # offshore water depth |
---|
| 36 | amplitude = 2 # Solitary wave height H |
---|
| 37 | normalized_amplitude = amplitude/offshore_depth |
---|
| 38 | simulation_name = 'runup_convergence' |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | # Basin dimensions (m) |
---|
| 42 | west = 0 # left boundary |
---|
| 43 | east = 500 # right boundary |
---|
| 44 | south = 0 # lower boundary |
---|
| 45 | north = 100 # upper bourdary |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | #------------------------------------------------------------------------------ |
---|
| 49 | # Setup computational domain all units in meters |
---|
| 50 | #------------------------------------------------------------------------------ |
---|
| 51 | |
---|
| 52 | # Structured mesh |
---|
| 53 | dx = 10 # Resolution: Lenght of subdivisions on x axis (length) |
---|
| 54 | dy = 10 # Resolution: Lenght of subdivisions on y axis (width) |
---|
| 55 | |
---|
| 56 | length = east-west |
---|
| 57 | width = north-south |
---|
| 58 | points, vertices, boundary = rectangular_cross(length/dx, width/dy, len1=length, len2=width, |
---|
| 59 | origin = (west, south)) |
---|
| 60 | |
---|
| 61 | domain = Domain(points, vertices, boundary) # Create domain |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | # Unstructured mesh |
---|
| 66 | polygon = [[east,north],[west,north],[west,south],[east,south]] |
---|
| 67 | interior_polygon = [[200,north-10],[west+10,north-10],[west+10,south+10],[200,south+10]] |
---|
| 68 | meshname = simulation_name + '.msh' |
---|
| 69 | create_mesh_from_regions(polygon, |
---|
| 70 | boundary_tags={'top': [0], 'left': [1], 'bottom': [2], 'right': [3]}, |
---|
| 71 | maximum_triangle_area=dx*dy/4, # Triangle area commensurate with structured mesh |
---|
| 72 | filename=meshname, |
---|
| 73 | interior_regions=[[interior_polygon,dx*dy/32]]) |
---|
| 74 | domain = Domain(meshname, use_cache=True, verbose = True) |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | domain.set_name(simulation_name) |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | #------------------------------------------------------------------------------ |
---|
| 82 | # Setup initial conditions |
---|
| 83 | #------------------------------------------------------------------------------ |
---|
| 84 | |
---|
| 85 | #def topography(x,y): |
---|
| 86 | # return slope*x+highest_point # Return linear bed slope bathymetry as vector |
---|
| 87 | |
---|
| 88 | def topography(x,y): |
---|
| 89 | """Two part topography - slope and flat part |
---|
| 90 | """ |
---|
| 91 | |
---|
| 92 | from Numeric import zeros, Float |
---|
| 93 | |
---|
| 94 | z = zeros(len(x), Float) # Allocate space for return vector |
---|
| 95 | for i in range(len(x)): |
---|
| 96 | |
---|
| 97 | z[i] = slope*x[i]+highest_point # Linear bed slope bathymetry |
---|
| 98 | |
---|
| 99 | if z[i] < min_elevation: # Limit depth |
---|
| 100 | z[i] = min_elevation |
---|
| 101 | |
---|
| 102 | return z |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | domain.set_quantity('elevation', topography) # Use function for elevation |
---|
| 108 | domain.set_quantity('friction', 0.0 ) # Constant friction |
---|
| 109 | domain.set_quantity('stage', sea_level) # Constant initial stage |
---|
| 110 | |
---|
| 111 | |
---|
| 112 | #------------------------------------------------------------------------------ |
---|
| 113 | # Setup boundary conditions |
---|
| 114 | #------------------------------------------------------------------------------ |
---|
| 115 | |
---|
| 116 | from math import sin, pi, cosh, sqrt |
---|
| 117 | Br = Reflective_boundary(domain) # Solid reflective wall |
---|
| 118 | Bd = Dirichlet_boundary([0.,0.,0.]) # Constant boundary values |
---|
| 119 | |
---|
| 120 | def waveform(t): |
---|
| 121 | return sea_level + normalized_amplitude/cosh(t-25)**2 |
---|
| 122 | |
---|
| 123 | Bw = Time_boundary(domain=domain, # Time dependent boundary |
---|
| 124 | f=lambda t: [waveform(t), 0.0, 0.0]) |
---|
| 125 | |
---|
| 126 | # Time dependent boundary for stage, where momentum is set automatically |
---|
| 127 | Bts = Transmissive_Momentum_Set_Stage_boundary(domain, waveform) |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | # Associate boundary tags with boundary objects |
---|
| 131 | domain.set_boundary({'left': Br, 'right': Bts, 'top': Br, 'bottom': Br}) |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | #------------------------------------------------------------------------------ |
---|
| 135 | # Evolve system through time |
---|
| 136 | #------------------------------------------------------------------------------ |
---|
| 137 | |
---|
| 138 | stagestep = [] |
---|
| 139 | for t in domain.evolve(yieldstep = 1, finaltime = 100): |
---|
| 140 | domain.write_time() |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | #----------------------------------------------------------------------------- |
---|
| 144 | # Interrogate solution |
---|
| 145 | #----------------------------------------------------------------------------- |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | # Define line of gauges through center of domain |
---|
| 149 | def gauge_line(west,east,north,south): |
---|
| 150 | from Numeric import arange |
---|
| 151 | x_vector = arange(west, (east-west)/2, 10) # Gauges every 10 meter from west to midpt |
---|
| 152 | y = (north+south)/2. |
---|
| 153 | |
---|
| 154 | gauges = [] |
---|
| 155 | for x in x_vector: |
---|
| 156 | gauges.append([x,y]) |
---|
| 157 | |
---|
| 158 | return gauges, x_vector |
---|
| 159 | |
---|
| 160 | gauges, x_vector = gauge_line(west,east,north,south) |
---|
| 161 | |
---|
| 162 | # Obtain interpolated timeseries at gauges |
---|
| 163 | f = file_function(domain.get_name()+'.sww', |
---|
| 164 | quantities = ['stage', 'elevation', 'xmomentum', 'ymomentum'], |
---|
| 165 | interpolation_points = gauges, |
---|
| 166 | verbose = True, |
---|
| 167 | use_cache = True) |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | # Find runup distance from western boundary through a linear search |
---|
| 171 | max_stage = [] |
---|
| 172 | min_stage = [] |
---|
| 173 | runup_point = west |
---|
| 174 | coastline = east |
---|
| 175 | for k, g in enumerate(gauges): |
---|
| 176 | z = f(0, point_id=k)[1] # Elevation |
---|
| 177 | |
---|
| 178 | min_w = sys.maxint |
---|
| 179 | max_w = -min_w |
---|
| 180 | for i, t in enumerate(f.get_time()): |
---|
| 181 | w = f(t, point_id = k)[0] |
---|
| 182 | if w > max_w: max_w = w |
---|
| 183 | if w < min_w: min_w = w |
---|
| 184 | |
---|
| 185 | if max_w-z <= 0.01: # Find first gauge where max depth > eps (runup) |
---|
| 186 | runup_point = g[0] |
---|
| 187 | |
---|
| 188 | if min_w-z <= 0.01: # Find first gauge where min depth > eps (coastline) |
---|
| 189 | coastline = g[0] |
---|
| 190 | |
---|
| 191 | max_stage.append(max_w) |
---|
| 192 | min_stage.append(min_w) |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | # Print |
---|
| 196 | print 'wave height [m]: ', amplitude |
---|
| 197 | runup_height = topography([runup_point], [(north+south)/2.])[0] |
---|
| 198 | print 'run up height [m]: ', runup_height |
---|
| 199 | |
---|
| 200 | runup_distance = runup_point-coastline |
---|
| 201 | print 'run up distance from coastline [m]: ', runup_distance |
---|
| 202 | |
---|
| 203 | print 'Coastline (meters form west): ', coastline |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | # Take snapshots and plot |
---|
| 208 | ion() |
---|
| 209 | figure(1) |
---|
| 210 | plot(x_vector, topography(x_vector,(north+south)/2.), 'r-') |
---|
| 211 | xlabel('x') |
---|
| 212 | ylabel('Elevation') |
---|
| 213 | #legend(('Max stage', 'Min stage', 'Elevation'), shadow=True, loc='upper right') |
---|
| 214 | title('Stage snapshots (t=0, 10, ...) for gauge line') |
---|
| 215 | grid() |
---|
| 216 | hold(True) |
---|
| 217 | |
---|
| 218 | for i, t in enumerate(f.get_time()): |
---|
| 219 | if i % 10 == 0: |
---|
| 220 | # Take only some timesteps to avoid clutter |
---|
| 221 | stages = [] |
---|
| 222 | for k, g in enumerate(gauges): |
---|
| 223 | w = f(t, point_id = k)[0] |
---|
| 224 | stages.append(w) |
---|
| 225 | |
---|
| 226 | plot(x_vector, stages, 'b-') |
---|
| 227 | |
---|
| 228 | savefig('snapshots') |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | # Store |
---|
| 233 | filename = 'maxrunup'+str(amplitude)+'.csv' |
---|
| 234 | fid = open(filename,'w') |
---|
| 235 | s = 'Waveheight,Runup distance,Runup height\n' |
---|
| 236 | fid.write(s) |
---|
| 237 | |
---|
| 238 | s = '%.2f,%.2f,%.2f\n' %(amplitude, runup_distance, runup_height) |
---|
| 239 | fid.write(s) |
---|
| 240 | |
---|
| 241 | fid.close() |
---|
| 242 | |
---|
| 243 | # Plot max runup etc |
---|
| 244 | ion() |
---|
| 245 | figure(1) |
---|
| 246 | plot(x_vector, max_stage, 'g+', |
---|
| 247 | x_vector, min_stage, 'b+', |
---|
| 248 | x_vector, topography(x_vector,(north+south)/2.), 'r-') |
---|
| 249 | xlabel('x') |
---|
| 250 | ylabel('stage') |
---|
| 251 | legend(('Max stage', 'Min stage', 'Elevation'), shadow=True, loc='upper right') |
---|
| 252 | title('Maximum stage for gauge line') |
---|
| 253 | grid() |
---|
| 254 | #axis([33000, 47000, -1000, 3000]) |
---|
| 255 | savefig('max_stage') |
---|
| 256 | |
---|
| 257 | close('all') |
---|
| 258 | |
---|
| 259 | |
---|
| 260 | |
---|