Changeset 540


Ignore:
Timestamp:
Nov 15, 2004, 1:31:53 PM (20 years ago)
Author:
ole
Message:

Got Smapson to work

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  
    2323sys.path.append('..'+sep+'pyvolution')
    2424
    25 from shallow_water import Transmissive_boundary, Reflective_boundary, \
    26      Dirichlet_boundary
    27 from domain import Domain
    28 from Numeric import array
     25from shallow_water import Domain, Dirichlet_boundary
    2926from 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
     27from mesh_factory import strang_mesh
    3828
    3929
     
    4333# two or three entries. Two entries are for points and three for triangles.
    4434
    45 domain = Strang_domain('yoon_circle.pt')
     35
     36points, elements = strang_mesh('yoon_circle.pt')
     37domain = Domain(points, elements)
    4638
    4739domain.default_order = 2
    4840domain.smooth = True
    4941
     42# Provide file name for storing output
     43domain.store = True
     44domain.format = 'sww'
     45domain.filename = 'sampson_strang_second_order'
    5046
    51 # Provide file name for storing output
    52 domain.filename="yoon_strang_second_order"
     47print 'Number of triangles = ', len(domain)
    5348
    54 print "Number of triangles = ", len(Volume.instances)
    55 
    56 domain.visualise = False
    57 domain.checkpoint = False #
    58 domain.store = True    #Store for visualisation purposes
    59 domain.format = 'sww'   #Native netcdf visualisation format
    6049
    6150#Reduction operation for get_vertex_values             
    62 from pytools.stats import mean
     51from util import mean
    6352domain.reduction = mean
    6453#domain.reduction = min  #Looks better near steep slopes
     
    9180    return z
    9281
    93 domain.set_field_values(x_slope, None)
     82domain.set_quantity('elevation', x_slope)
    9483
    95 
    96 #Set the water depth
    97 def height(x,y):
     84#Set the water level (w = z+h)
     85def level(x,y):
    9886    z = x_slope(x,y)
    9987    n = x.shape[0]
     
    10795    return h   
    10896
    109 domain.set_conserved_quantities(height, None, None)
     97domain.set_quantity('level', level)
    11098
     99
     100############
     101#Boundary
     102domain.set_boundary({'exterior': Dirichlet_boundary([0.0, 0.0, 0.0])})
    111103   
    112 ######################
    113 #Visualisation
    114 if domain.visualise:
    115     visualise.create_surface(domain)
    116  
    117104
    118105######################
    119106#Evolution
    120 
    121107import time
    122108t0 = time.time()
    123 for t in domain.evolve(max_timestep = 1.0, yieldstep = 10.0, finaltime = 300):
     109for t in domain.evolve(yieldstep = 10.0, finaltime = 300):
    124110    domain.write_time()
    125111
    126 # Remove the following if Visual Python not required       
    127     if domain.visualise: 
    128         visualise.update(domain, index=0, smooth=True)
    129    
    130112print 'That took %.2f seconds' %(time.time()-t0)
    131113
  • inundation/ga/storm_surge/analytical solutions/Analytical_solution_Yoon_import_mesh.py

    r536 r540  
    2020from shallow_water import Domain, Dirichlet_boundary
    2121from math import sqrt, cos, sin, pi
    22 
    23 
    24 
    25 ######################
    26 # Boundary conditions
    27 # Not required for this problem
    28 
    2922from mesh_factory import strang_mesh
    3023
     
    4538domain.store = True
    4639domain.format = 'sww'
    47 domain.filename='yoon_strang_second_order'
     40domain.filename = 'yoon_strang_second_order'
    4841
    4942print 'Number of triangles = ', len(domain)
  • inundation/ga/storm_surge/pyvolution/mesh_factory.py

    r534 r540  
    6262    return points, elements, boundary       
    6363
    64 
    65 
    66 def from_polyfile(name):
    67     """Read mesh from .poly file, an obj like file format
    68     listing first vertex coordinates and then connectivity
    69     """
    70 
    71     from util import anglediff
    72     from math import pi
    73     import os.path
    74     root, ext = os.path.splitext(name)
    75 
    76     if ext == 'poly':
    77         filename = name
    78     else:
    79         filename = name + '.poly'
    80 
    81    
    82     fid = open(filename)
    83 
    84     points = []    #x, y
    85     values = []    #z
    86     ##vertex_values = []    #Repeated z   
    87     triangles = [] #v0, v1, v2
    88    
    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', msg
    94 
    95     offending = 0
    96     i = 1
    97     while keyword == 'POINTS':
    98         line = lines[i].strip()
    99         i += 1
    100 
    101         if line == 'POLYS':
    102             keyword = line
    103             break
    104 
    105         fields = line.split(':')
    106         assert int(fields[0]) == i-1, 'Point indices not consecutive'
    107 
    108         #Split the three floats
    109         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 = i   
    120     while keyword == 'POLYS':
    121         line = lines[i].strip()
    122         i += 1
    123 
    124         if line == 'END':
    125             keyword = line
    126             break
    127        
    128 
    129         fields = line.split(':')
    130         assert int(fields[0]) == i-k, 'Poly indices not consecutive'
    131 
    132         #Split the three indices
    133         vvv = fields[1].split()
    134 
    135         i0 = int(vvv[0])-1
    136         i1 = int(vvv[1])-1
    137         i2 = int(vvv[2])-1
    138 
    139         #Check for and exclude degenerate areas
    140         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))/2
    148         if area > 0:
    149 
    150             #Ensure that points are arranged in counter clock-wise order
    151             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 well
    162                 j0 = i0
    163                 j1 = i1
    164                 j2 = i2
    165             else:
    166                 #Swap two vertices
    167                 j0 = i1
    168                 j1 = i0
    169                 j2 = i2
    170                
    171             triangles.append([j0, j1, j2])
    172             ##vertex_values.append([values[j0], values[j1], values[j2]])
    173         else:
    174             offending +=1
    175 
    176     print 'Removed %d offending triangles out of %d' %(offending, len(lines))
    177     return points, triangles, values
    178 
    179 
    180 
    181 def strang_mesh(filename):
    182     """Read Strang generated mesh.
    183     """
    184 
    185     from math import pi
    186     from util import anglediff
    187    
    188    
    189     fid = open(filename)
    190     points = []    # List of x, y coordinates
    191     triangles = [] # List of vertex ids as listed in the file
    192    
    193     for line in fid.readlines():
    194         fields = line.split()
    195         if len(fields) == 2:
    196             # we are reading vertex coordinates
    197             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 ' + filename
    205 
    206     elements = [] #Final list of elements
    207 
    208     for t in triangles:
    209         #Get vertex coordinates
    210         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 order
    222         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, elements                             
    236 
    237 
    238 
    239 #FIXME: These haven't been done yet
    240 # 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 triangles
    243 #     with m+1 radial segments by n+1 points around the circuference
    244 #     and radius. If radius is are omitted the mesh defaults to the unit circle
    245 #     radius.
    246  
    247 #     radius: radius of circle
    248 
    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 keywords
    252 #     rectangular_mesh(...,Triangle=Volume, Mesh=Domain)"""
    253    
    254 
    255 #     from Numeric import array
    256 #     from visual import rate
    257 #     import math
    258 
    259 #     delta = radius/float(m)
    260 
    261 #     #Dictionary of vertex objects
    262 #     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 element       
    270 #     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)) #Lower               
    279 #             elements.append(Triangle(v1,v3,v2)) #Upper   
    280 
    281 #     #Do the center
    282 #     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) #center
    288 #         elements.append(T)
    289 
    290 #     return Mesh(elements)
    29164
    29265# def oblique_mesh(m, n, lenx = 1.0, leny = 1.0, theta = 8.95, origin = (0.0, 0.0),
     
    388161   
    389162#     return M
     163
     164def 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
     279def 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
    390390
    391391# 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.