Changeset 540
- Timestamp:
- Nov 15, 2004, 1:31:53 PM (20 years ago)
- Location:
- inundation/ga/storm_surge
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/analytical solutions/Analytical_solution_Sampson_import_mesh.py
r536 r540 23 23 sys.path.append('..'+sep+'pyvolution') 24 24 25 from shallow_water import Transmissive_boundary, Reflective_boundary, \ 26 Dirichlet_boundary 27 from domain import Domain 28 from Numeric import array 25 from shallow_water import Domain, Dirichlet_boundary 29 26 from math import sqrt, cos, sin, pi 30 31 32 33 ###################### 34 # Boundary conditions 35 # Not required for this problem 36 37 from domain import Strang_domain 27 from mesh_factory import strang_mesh 38 28 39 29 … … 43 33 # two or three entries. Two entries are for points and three for triangles. 44 34 45 domain = Strang_domain('yoon_circle.pt') 35 36 points, elements = strang_mesh('yoon_circle.pt') 37 domain = Domain(points, elements) 46 38 47 39 domain.default_order = 2 48 40 domain.smooth = True 49 41 42 # Provide file name for storing output 43 domain.store = True 44 domain.format = 'sww' 45 domain.filename = 'sampson_strang_second_order' 50 46 51 # Provide file name for storing output 52 domain.filename="yoon_strang_second_order" 47 print 'Number of triangles = ', len(domain) 53 48 54 print "Number of triangles = ", len(Volume.instances)55 56 domain.visualise = False57 domain.checkpoint = False #58 domain.store = True #Store for visualisation purposes59 domain.format = 'sww' #Native netcdf visualisation format60 49 61 50 #Reduction operation for get_vertex_values 62 from pytools.statsimport mean51 from util import mean 63 52 domain.reduction = mean 64 53 #domain.reduction = min #Looks better near steep slopes … … 91 80 return z 92 81 93 domain.set_ field_values(x_slope, None)82 domain.set_quantity('elevation', x_slope) 94 83 95 96 #Set the water depth 97 def height(x,y): 84 #Set the water level (w = z+h) 85 def level(x,y): 98 86 z = x_slope(x,y) 99 87 n = x.shape[0] … … 107 95 return h 108 96 109 domain.set_ conserved_quantities(height, None, None)97 domain.set_quantity('level', level) 110 98 99 100 ############ 101 #Boundary 102 domain.set_boundary({'exterior': Dirichlet_boundary([0.0, 0.0, 0.0])}) 111 103 112 ######################113 #Visualisation114 if domain.visualise:115 visualise.create_surface(domain)116 117 104 118 105 ###################### 119 106 #Evolution 120 121 107 import time 122 108 t0 = time.time() 123 for t in domain.evolve( max_timestep = 1.0,yieldstep = 10.0, finaltime = 300):109 for t in domain.evolve(yieldstep = 10.0, finaltime = 300): 124 110 domain.write_time() 125 111 126 # Remove the following if Visual Python not required127 if domain.visualise:128 visualise.update(domain, index=0, smooth=True)129 130 112 print 'That took %.2f seconds' %(time.time()-t0) 131 113 -
inundation/ga/storm_surge/analytical solutions/Analytical_solution_Yoon_import_mesh.py
r536 r540 20 20 from shallow_water import Domain, Dirichlet_boundary 21 21 from math import sqrt, cos, sin, pi 22 23 24 25 ######################26 # Boundary conditions27 # Not required for this problem28 29 22 from mesh_factory import strang_mesh 30 23 … … 45 38 domain.store = True 46 39 domain.format = 'sww' 47 domain.filename ='yoon_strang_second_order'40 domain.filename = 'yoon_strang_second_order' 48 41 49 42 print 'Number of triangles = ', len(domain) -
inundation/ga/storm_surge/pyvolution/mesh_factory.py
r534 r540 62 62 return points, elements, boundary 63 63 64 65 66 def from_polyfile(name):67 """Read mesh from .poly file, an obj like file format68 listing first vertex coordinates and then connectivity69 """70 71 from util import anglediff72 from math import pi73 import os.path74 root, ext = os.path.splitext(name)75 76 if ext == 'poly':77 filename = name78 else:79 filename = name + '.poly'80 81 82 fid = open(filename)83 84 points = [] #x, y85 values = [] #z86 ##vertex_values = [] #Repeated z87 triangles = [] #v0, v1, v288 89 lines = fid.readlines()90 91 keyword = lines[0].strip()92 msg = 'First line in .poly file must contain the keyword: POINTS'93 assert keyword == 'POINTS', msg94 95 offending = 096 i = 197 while keyword == 'POINTS':98 line = lines[i].strip()99 i += 1100 101 if line == 'POLYS':102 keyword = line103 break104 105 fields = line.split(':')106 assert int(fields[0]) == i-1, 'Point indices not consecutive'107 108 #Split the three floats109 xyz = fields[1].split()110 111 x = float(xyz[0])112 y = float(xyz[1])113 z = float(xyz[2])114 115 points.append([x, y])116 values.append(z)117 118 119 k = i120 while keyword == 'POLYS':121 line = lines[i].strip()122 i += 1123 124 if line == 'END':125 keyword = line126 break127 128 129 fields = line.split(':')130 assert int(fields[0]) == i-k, 'Poly indices not consecutive'131 132 #Split the three indices133 vvv = fields[1].split()134 135 i0 = int(vvv[0])-1136 i1 = int(vvv[1])-1137 i2 = int(vvv[2])-1138 139 #Check for and exclude degenerate areas140 x0 = points[i0][0]141 y0 = points[i0][1]142 x1 = points[i1][0]143 y1 = points[i1][1]144 x2 = points[i2][0]145 y2 = points[i2][1]146 147 area = abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0))/2148 if area > 0:149 150 #Ensure that points are arranged in counter clock-wise order151 v0 = [x1-x0, y1-y0]152 v1 = [x2-x1, y2-y1]153 v2 = [x0-x2, y0-y2]154 155 a0 = anglediff(v1, v0)156 a1 = anglediff(v2, v1)157 a2 = anglediff(v0, v2)158 159 160 if a0 < pi and a1 < pi and a2 < pi:161 #all is well162 j0 = i0163 j1 = i1164 j2 = i2165 else:166 #Swap two vertices167 j0 = i1168 j1 = i0169 j2 = i2170 171 triangles.append([j0, j1, j2])172 ##vertex_values.append([values[j0], values[j1], values[j2]])173 else:174 offending +=1175 176 print 'Removed %d offending triangles out of %d' %(offending, len(lines))177 return points, triangles, values178 179 180 181 def strang_mesh(filename):182 """Read Strang generated mesh.183 """184 185 from math import pi186 from util import anglediff187 188 189 fid = open(filename)190 points = [] # List of x, y coordinates191 triangles = [] # List of vertex ids as listed in the file192 193 for line in fid.readlines():194 fields = line.split()195 if len(fields) == 2:196 # we are reading vertex coordinates197 points.append([float(fields[0]), float(fields[1])])198 elif len(fields) == 3:199 # we are reading triangle point id's (format ae+b)200 triangles.append([int(float(fields[0]))-1,201 int(float(fields[1]))-1,202 int(float(fields[2]))-1])203 else:204 raise 'wrong format in ' + filename205 206 elements = [] #Final list of elements207 208 for t in triangles:209 #Get vertex coordinates210 v0 = t[0]211 v1 = t[1]212 v2 = t[2]213 214 x0 = points[v0][0]215 y0 = points[v0][1]216 x1 = points[v1][0]217 y1 = points[v1][1]218 x2 = points[v2][0]219 y2 = points[v2][1]220 221 #Check that points are arranged in counter clock-wise order222 vec0 = [x1-x0, y1-y0]223 vec1 = [x2-x1, y2-y1]224 vec2 = [x0-x2, y0-y2]225 226 a0 = anglediff(vec1, vec0)227 a1 = anglediff(vec2, vec1)228 a2 = anglediff(vec0, vec2)229 230 if a0 < pi and a1 < pi and a2 < pi:231 elements.append([v0, v1, v2])232 else:233 elements.append([v0, v2, v1])234 235 return points, elements236 237 238 239 #FIXME: These haven't been done yet240 # def circular_mesh(m, n, radius=1.0, center = (0.0, 0.0), Triangle=Triangle,241 # Mesh=Mesh, Point=Point):242 # """Setup a circular grid of triangles243 # with m+1 radial segments by n+1 points around the circuference244 # and radius. If radius is are omitted the mesh defaults to the unit circle245 # radius.246 247 # radius: radius of circle248 249 # Triangle refers to the actual class or subclass to be instantiated:250 # e.g. if Volume is a subclass of Triangle,251 # this function can be invoked with the keywords252 # rectangular_mesh(...,Triangle=Volume, Mesh=Domain)"""253 254 255 # from Numeric import array256 # from visual import rate257 # import math258 259 # delta = radius/float(m)260 261 # #Dictionary of vertex objects262 # vertices = {}263 # for i in range(n+1):264 # theta = float(i)*(2*math.pi)/float(n)265 # for j in range(m+1):266 # delta = float(j)*radius/float(m)267 # vertices[i,j] = Point(delta*math.cos(theta),delta*math.sin(theta))268 269 # #Construct 2 triangles per element270 # elements = []271 # for i in range(n):272 # for j in range(1,m):273 # v1 = vertices[i,j+1]274 # v2 = vertices[i,j]275 # v3 = vertices[i+1,j+1]276 # v4 = vertices[i+1,j]277 278 # elements.append(Triangle(v4,v2,v3)) #Lower279 # elements.append(Triangle(v1,v3,v2)) #Upper280 281 # #Do the center282 # v1 = vertices[0,0]283 # for i in range(n):284 # v2 = vertices[i,1]285 # v3 = vertices[i+1,1]286 287 # T = Triangle(v1,v2,v3) #center288 # elements.append(T)289 290 # return Mesh(elements)291 64 292 65 # def oblique_mesh(m, n, lenx = 1.0, leny = 1.0, theta = 8.95, origin = (0.0, 0.0), … … 388 161 389 162 # return M 163 164 def from_polyfile(name): 165 """Read mesh from .poly file, an obj like file format 166 listing first vertex coordinates and then connectivity 167 """ 168 169 from util import anglediff 170 from math import pi 171 import os.path 172 root, ext = os.path.splitext(name) 173 174 if ext == 'poly': 175 filename = name 176 else: 177 filename = name + '.poly' 178 179 180 fid = open(filename) 181 182 points = [] #x, y 183 values = [] #z 184 ##vertex_values = [] #Repeated z 185 triangles = [] #v0, v1, v2 186 187 lines = fid.readlines() 188 189 keyword = lines[0].strip() 190 msg = 'First line in .poly file must contain the keyword: POINTS' 191 assert keyword == 'POINTS', msg 192 193 offending = 0 194 i = 1 195 while keyword == 'POINTS': 196 line = lines[i].strip() 197 i += 1 198 199 if line == 'POLYS': 200 keyword = line 201 break 202 203 fields = line.split(':') 204 assert int(fields[0]) == i-1, 'Point indices not consecutive' 205 206 #Split the three floats 207 xyz = fields[1].split() 208 209 x = float(xyz[0]) 210 y = float(xyz[1]) 211 z = float(xyz[2]) 212 213 points.append([x, y]) 214 values.append(z) 215 216 217 k = i 218 while keyword == 'POLYS': 219 line = lines[i].strip() 220 i += 1 221 222 if line == 'END': 223 keyword = line 224 break 225 226 227 fields = line.split(':') 228 assert int(fields[0]) == i-k, 'Poly indices not consecutive' 229 230 #Split the three indices 231 vvv = fields[1].split() 232 233 i0 = int(vvv[0])-1 234 i1 = int(vvv[1])-1 235 i2 = int(vvv[2])-1 236 237 #Check for and exclude degenerate areas 238 x0 = points[i0][0] 239 y0 = points[i0][1] 240 x1 = points[i1][0] 241 y1 = points[i1][1] 242 x2 = points[i2][0] 243 y2 = points[i2][1] 244 245 area = abs((x1*y0-x0*y1)+(x2*y1-x1*y2)+(x0*y2-x2*y0))/2 246 if area > 0: 247 248 #Ensure that points are arranged in counter clock-wise order 249 v0 = [x1-x0, y1-y0] 250 v1 = [x2-x1, y2-y1] 251 v2 = [x0-x2, y0-y2] 252 253 a0 = anglediff(v1, v0) 254 a1 = anglediff(v2, v1) 255 a2 = anglediff(v0, v2) 256 257 258 if a0 < pi and a1 < pi and a2 < pi: 259 #all is well 260 j0 = i0 261 j1 = i1 262 j2 = i2 263 else: 264 #Swap two vertices 265 j0 = i1 266 j1 = i0 267 j2 = i2 268 269 triangles.append([j0, j1, j2]) 270 ##vertex_values.append([values[j0], values[j1], values[j2]]) 271 else: 272 offending +=1 273 274 print 'Removed %d offending triangles out of %d' %(offending, len(lines)) 275 return points, triangles, values 276 277 278 279 def strang_mesh(filename): 280 """Read Strang generated mesh. 281 """ 282 283 from math import pi 284 from util import anglediff 285 286 287 fid = open(filename) 288 points = [] # List of x, y coordinates 289 triangles = [] # List of vertex ids as listed in the file 290 291 for line in fid.readlines(): 292 fields = line.split() 293 if len(fields) == 2: 294 # we are reading vertex coordinates 295 points.append([float(fields[0]), float(fields[1])]) 296 elif len(fields) == 3: 297 # we are reading triangle point id's (format ae+b) 298 triangles.append([int(float(fields[0]))-1, 299 int(float(fields[1]))-1, 300 int(float(fields[2]))-1]) 301 else: 302 raise 'wrong format in ' + filename 303 304 elements = [] #Final list of elements 305 306 for t in triangles: 307 #Get vertex coordinates 308 v0 = t[0] 309 v1 = t[1] 310 v2 = t[2] 311 312 x0 = points[v0][0] 313 y0 = points[v0][1] 314 x1 = points[v1][0] 315 y1 = points[v1][1] 316 x2 = points[v2][0] 317 y2 = points[v2][1] 318 319 #Check that points are arranged in counter clock-wise order 320 vec0 = [x1-x0, y1-y0] 321 vec1 = [x2-x1, y2-y1] 322 vec2 = [x0-x2, y0-y2] 323 324 a0 = anglediff(vec1, vec0) 325 a1 = anglediff(vec2, vec1) 326 a2 = anglediff(vec0, vec2) 327 328 if a0 < pi and a1 < pi and a2 < pi: 329 elements.append([v0, v1, v2]) 330 else: 331 elements.append([v0, v2, v1]) 332 333 return points, elements 334 335 336 337 #FIXME: These haven't been done yet 338 # def circular_mesh(m, n, radius=1.0, center = (0.0, 0.0), Triangle=Triangle, 339 # Mesh=Mesh, Point=Point): 340 # """Setup a circular grid of triangles 341 # with m+1 radial segments by n+1 points around the circuference 342 # and radius. If radius is are omitted the mesh defaults to the unit circle 343 # radius. 344 345 # radius: radius of circle 346 347 # Triangle refers to the actual class or subclass to be instantiated: 348 # e.g. if Volume is a subclass of Triangle, 349 # this function can be invoked with the keywords 350 # rectangular_mesh(...,Triangle=Volume, Mesh=Domain)""" 351 352 353 # from Numeric import array 354 # from visual import rate 355 # import math 356 357 # delta = radius/float(m) 358 359 # #Dictionary of vertex objects 360 # vertices = {} 361 # for i in range(n+1): 362 # theta = float(i)*(2*math.pi)/float(n) 363 # for j in range(m+1): 364 # delta = float(j)*radius/float(m) 365 # vertices[i,j] = Point(delta*math.cos(theta),delta*math.sin(theta)) 366 367 # #Construct 2 triangles per element 368 # elements = [] 369 # for i in range(n): 370 # for j in range(1,m): 371 # v1 = vertices[i,j+1] 372 # v2 = vertices[i,j] 373 # v3 = vertices[i+1,j+1] 374 # v4 = vertices[i+1,j] 375 376 # elements.append(Triangle(v4,v2,v3)) #Lower 377 # elements.append(Triangle(v1,v3,v2)) #Upper 378 379 # #Do the center 380 # v1 = vertices[0,0] 381 # for i in range(n): 382 # v2 = vertices[i,1] 383 # v3 = vertices[i+1,1] 384 385 # T = Triangle(v1,v2,v3) #center 386 # elements.append(T) 387 388 # return Mesh(elements) 389 390 390 391 391 # def contracting_channel_mesh(m, n, x1 = 0.0, x2 = 1./3., x3 = 2./3., x4 = 1.0,
Note: See TracChangeset
for help on using the changeset viewer.