[1627] | 1 | """Example of shallow water wave equation. |
---|
| 2 | |
---|
| 3 | This script sets up a 2D version of the 1D LWRU1 benchmark with initial condition stated in the file benchmark_1.txt. |
---|
| 4 | |
---|
| 5 | See also |
---|
| 6 | |
---|
| 7 | http://www.cee.cornell.edu/longwave/index.cfm?page=benchmark&problem=1 |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | """ |
---|
| 11 | |
---|
| 12 | ###################### |
---|
| 13 | # Module imports |
---|
| 14 | |
---|
| 15 | from pyvolution.shallow_water import Domain, Reflective_boundary,\ |
---|
| 16 | Dirichlet_boundary,Transmissive_boundary, Constant_height, Constant_stage |
---|
| 17 | |
---|
| 18 | from pyvolution.mesh_factory import rectangular |
---|
| 19 | from Numeric import array, zeros, Float, allclose |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | ####################### |
---|
| 23 | # Domain |
---|
| 24 | # |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | print 'Creating domain' |
---|
| 28 | #Create basic mesh |
---|
| 29 | # |
---|
| 30 | #The initial condition extends 50km off shore |
---|
| 31 | #and 5,000m is allowed on shore for wetting |
---|
| 32 | #(only about 200m is expected, though) |
---|
| 33 | |
---|
| 34 | #points, vertices, boundary = rectangular(400, 40, |
---|
| 35 | # len1=55000, len2=5000, |
---|
| 36 | # origin = (-5000, 0.0)) |
---|
| 37 | |
---|
| 38 | points, vertices, boundary = rectangular(100, 10, |
---|
| 39 | len1=55000, len2=5000, |
---|
| 40 | origin = (-5000, 0.0)) |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | #Create shallow water domain |
---|
| 44 | domain = Domain(points, vertices, boundary) |
---|
| 45 | |
---|
| 46 | domain.check_integrity() |
---|
| 47 | domain.default_order = 2 |
---|
| 48 | |
---|
| 49 | #Output params |
---|
| 50 | domain.smooth = True |
---|
| 51 | domain.reduction = min #Looks a lot better on top of steep slopes |
---|
| 52 | print "Number of triangles = ", len(domain) |
---|
| 53 | |
---|
| 54 | domain.visualise = False |
---|
| 55 | domain.store = True #Store for visualisation purposes |
---|
| 56 | domain.format = 'sww' #Native netcdf visualisation format |
---|
| 57 | |
---|
| 58 | import sys, os |
---|
| 59 | base = os.path.basename(sys.argv[0]) |
---|
| 60 | domain.filename, _ = os.path.splitext(base) |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | #Set initial values |
---|
| 64 | def slope(x, y): |
---|
| 65 | return -x/10 |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | class IC_x: |
---|
| 69 | """ |
---|
| 70 | Read 1D initial condition and provide values at any x, y |
---|
| 71 | |
---|
| 72 | File is assumed to list x values in the first column and |
---|
| 73 | stage in the second. |
---|
| 74 | """ |
---|
| 75 | |
---|
| 76 | def __init__(self, filename): |
---|
| 77 | |
---|
| 78 | self.x = [] |
---|
| 79 | self.w = [] |
---|
| 80 | fid = open(filename) |
---|
| 81 | for line in fid.readlines(): |
---|
| 82 | fields = line.split() |
---|
| 83 | assert len(fields) == 2, '%s' %fields |
---|
| 84 | self.x.append( float(fields[0]) ) |
---|
| 85 | self.w.append( float(fields[1]) ) |
---|
| 86 | |
---|
| 87 | #print 'X', self.x, len(self.x) |
---|
| 88 | #print 'W', self.w, len(self.w) |
---|
| 89 | #from pylab import plot, show |
---|
| 90 | #plot(self.x, self.w) |
---|
| 91 | #show() |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | def __call__(self, x, y): |
---|
| 95 | |
---|
| 96 | w = zeros( len(x), Float ) |
---|
| 97 | for i in range(len(x)): |
---|
| 98 | xi = x[i] |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | #Find slot |
---|
| 102 | |
---|
| 103 | if xi < self.x[0]: w[i] = self.w[0] |
---|
| 104 | elif xi > self.x[-1]: w[i] = self.w[-1] |
---|
| 105 | else: |
---|
| 106 | index = 0 |
---|
| 107 | while xi > self.x[index]: index += 1 |
---|
| 108 | while xi < self.x[index]: index -= 1 |
---|
| 109 | |
---|
| 110 | #print xi, index, self.x[index], self.w[index] |
---|
| 111 | |
---|
| 112 | if xi == self.x[index]: |
---|
| 113 | #if allclose(xi, self.x[index]): |
---|
| 114 | #Protect against case where x is the last value |
---|
| 115 | # - also works in general when x == self.x[i] |
---|
| 116 | ratio = 0 |
---|
| 117 | else: |
---|
| 118 | #x is now between index and index+1 |
---|
| 119 | ratio = (xi - self.x[index])/\ |
---|
| 120 | (self.x[index+1] - self.x[index]) |
---|
| 121 | |
---|
| 122 | #print xi, index, self.x[index], ratio |
---|
| 123 | |
---|
| 124 | #Compute interpolated value |
---|
| 125 | if ratio > 0: |
---|
| 126 | w[i] = self.w[index] +\ |
---|
| 127 | ratio*(self.w[index+1] - self.w[index]) |
---|
| 128 | else: |
---|
| 129 | w[i] = self.w[index] |
---|
| 130 | |
---|
| 131 | #print x, w |
---|
| 132 | return w |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | print 'Field values' |
---|
| 137 | domain.set_quantity('elevation', slope) |
---|
| 138 | domain.set_quantity('friction', 0.0) |
---|
| 139 | domain.set_quantity('stage', IC_x('lwru1_IC.txt')) |
---|
| 140 | |
---|
| 141 | #import sys; sys.exit() |
---|
| 142 | |
---|
| 143 | #print domain.quantities['stage'].centroid_values |
---|
| 144 | |
---|
| 145 | ###################### |
---|
| 146 | # Boundary conditions |
---|
| 147 | # |
---|
| 148 | print 'Boundaries' |
---|
| 149 | Br = Reflective_boundary(domain) |
---|
| 150 | Bt = Transmissive_boundary(domain) |
---|
| 151 | |
---|
| 152 | #Constant inflow |
---|
| 153 | Bd = Dirichlet_boundary([0.0, 0.0, 0.0]) |
---|
| 154 | |
---|
| 155 | #Set boundary conditions |
---|
| 156 | domain.set_boundary({'left': Bd, 'right': Bd, 'bottom': Bd, 'top': Bd}) |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | #Evolve |
---|
| 160 | import time |
---|
| 161 | t0 = time.time() |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | pt = [] |
---|
| 167 | xes = [] |
---|
| 168 | y = 2500 |
---|
| 169 | x0 = -5000 |
---|
| 170 | step = 50 |
---|
| 171 | for i in range(1000): |
---|
| 172 | x = x0+i*step |
---|
| 173 | xes.append(x) |
---|
| 174 | pt.append( [x,y] ) |
---|
| 175 | |
---|
| 176 | from pylab import * |
---|
| 177 | from pyvolution.least_squares import Interpolation |
---|
| 178 | I = Interpolation(domain.coordinates, |
---|
| 179 | domain.triangles, |
---|
| 180 | point_coordinates = pt, |
---|
| 181 | verbose = True) |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | print 'xxxxx' |
---|
| 185 | |
---|
| 186 | hold(False) |
---|
| 187 | for t in domain.evolve(yieldstep = 1, finaltime = 220.0): |
---|
| 188 | |
---|
| 189 | f = domain.quantities['stage'].get_vertex_values(smooth = True) |
---|
| 190 | y = I.interpolate( f ) |
---|
| 191 | |
---|
| 192 | ion() |
---|
| 193 | plot(xes, y) |
---|
| 194 | domain.write_time() |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | print 'That took %.2f seconds' %(time.time()-t0) |
---|
| 198 | show() |
---|