source: inundation/ga/storm_surge/pyvolution/pmesh2domain.py @ 1108

Last change on this file since 1108 was 969, checked in by duncan, 20 years ago

for pmesh/loadmesh, changing markers to tags

File size: 5.9 KB
RevLine 
[374]1"""Class pmesh2domain - Converting .tsh files to doamains
[371]2
3
4   Copyright 2004
5   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
6   Geoscience Australia
7"""
8
9
[415]10def pmesh_to_domain_instance(fileName, DomainClass, setting_function = None):
[371]11    """
12    """
13    import sys
[389]14    from domain import Domain
[371]15
[969]16    vertex_coordinates, volumes, tag_dict, vertex_quantity_dict ,tagged_elements_dict = \
[389]17                        pmesh_to_domain(fileName,
18                                                   setting_function=setting_function)
[371]19       
[389]20    assert issubclass(DomainClass, Domain), "DomainClass is not a subclass of Domain."
[636]21
22
23   
[969]24    domain = DomainClass(vertex_coordinates, volumes, tag_dict,
[415]25                         tagged_elements = tagged_elements_dict )
[371]26
[636]27
28   
29    #FIXME (Ole): Is this really the right place to apply the a default
30    #value specific to the shallow water wave equation?
31    #The 'assert' above indicates that any subclass of Domain is acceptable.
32    #Suggestion - module shallow_water.py will eventually take care of this
33    #(when I get around to it) so it should be removed from here. 
34   
[905]35    # This doesn't work on the domain instance.
36    # This is still needed so -ve elevations don't cuase 'lakes'
37    # The fixme we discussed was to only create a quantity when its values are set.
38    # I think that's the way to go still
39   
[773]40    # set the water stage to be the elevation
41    if vertex_quantity_dict.has_key('elevation') and not vertex_quantity_dict.has_key('stage'):
42        vertex_quantity_dict['stage'] = vertex_quantity_dict['elevation']
[905]43       
[389]44    domain.set_quantity_vertices_dict(vertex_quantity_dict)
45    #print "vertex_quantity_dict",vertex_quantity_dict
46    return domain
[371]47
48
[389]49
50def pmesh_to_domain(fileName, setting_function = None):
[371]51    """
52    convert a pmesh dictionary to a list of Volumes.
53    Also, return a list of triangles which have boundary tags
[389]54    mesh_dict structure;
[371]55    generated point list: [(x1,y1),(x2,y2),...] (Tuples of doubles) 
56    generated point attribute list:[(P1att1,P1attt2, ...),(P2att1,P2attt2,...),...]
57    generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers)
[969]58    generated segment tag list: [S1Tag, S2Tag, ...] (list of ints)
[371]59    triangle list:  [(point1,point2, point3),(p5,p4, p1),...] (Tuples of integers)
60    triangle neighbor list: [(triangle1,triangle2, triangle3),(t5,t4, t1),...] (Tuples of integers) -1 means there's no triangle neighbor
[415]61    triangle attribute list: [T1att, T2att, ...] (list of strings)
[371]62    """
[389]63   
64    from Numeric import transpose
65    from load_mesh.loadASCII import mesh_file_to_mesh_dictionary
66   
67    mesh_dict = mesh_file_to_mesh_dictionary(fileName)
68    #print "mesh_dict",mesh_dict
[968]69    vertex_coordinates = mesh_dict['vertices']
70    volumes = mesh_dict['triangles']
[371]71
72    #if setting_function:
73    #    if not type(setting_function) is ListType:
74    #        setting_function = [setting_function]
75    #    for funct in setting_function:
[389]76    #        mesh_dict = funct(mesh_dict, vertices = mesh_vertices,
[371]77    #                        volumes = volumes)
[389]78
79
80    vertex_quantity_dict = {}
[968]81    point_atts = transpose(mesh_dict['vertex_attributes'])
82    point_titles  = mesh_dict['vertex_attribute_titles']
[389]83    for quantity, value_vector in map (None, point_titles, point_atts):
84        vertex_quantity_dict[quantity] = value_vector
[969]85    tag_dict = pmesh_dict_to_tag_dict(mesh_dict)
[415]86    tagged_elements_dict = build_tagged_elements_dictionary(mesh_dict)
[969]87    return vertex_coordinates, volumes, tag_dict, vertex_quantity_dict,tagged_elements_dict
[371]88
89
90
[415]91def build_tagged_elements_dictionary(mesh_dict):
92    """Build the dictionary of element tags.
93    tagged_elements is a dictionary of element arrays,
94    keyed by tag:
95    { (tag): [e1, e2, e3..] }
96    """
[969]97    tri_atts = mesh_dict['triangle_tags']
[415]98    #print "tri_atts", tri_atts
99    tagged_elements = {}
100    for tri_att_index in range(len(tri_atts)):
101        tagged_elements.setdefault(tri_atts[tri_att_index][0],[]).append(tri_att_index)
102    #print "DSG pm2do tagged_elements", tagged_elements
103    return tagged_elements
[371]104
105#FIXME: The issue is whether this format should be stored in the tsh file
106#instead of having to be created here?
107
[905]108#This information is pyvolution focused, not mesh generation focused.
109#This is an appropriate place for the info to be created. -DSG
110
[371]111#FIXME: Another issue is that the tsh file stores consecutive
112#indices explicitly. This is really redundant.
113#Suggest looking at obj and our own sww format and also consider
114#using netCDF.
115
[905]116# It is redundant.  It was the format originally decided on.
117# I'm happy for it to change. -DSG
118
[969]119def pmesh_dict_to_tag_dict(mesh_dict):
[389]120    """ Convert the pmesh dictionary (mesh_dict) description of boundary tags
[969]121    to a dictionary of tags, indexed with volume id and face number.
[371]122    """
[968]123    triangles = mesh_dict['triangles']
[371]124    sides = calc_sides(triangles)
[969]125    tag_dict = {}
126    for seg, tag in map(None,mesh_dict['segments'],
127                           mesh_dict['segment_tags']):
[371]128        v1 = seg[0]
129        v2 = seg[1]
130        for key in [(v1,v2),(v2,v1)]:
[969]131            if sides.has_key(key) and tag <> "":
[686]132                #"" represents null.  Don't put these into the dictionary
[969]133                #this creates a dict of lists of faces, indexed by tag
134                #tagged_edges.setdefault(tag,[]).append(sides[key])
135                tag_dict[sides[key]] = tag
[686]136
[969]137    return tag_dict
[371]138
139   
140def calc_sides(triangles):
141    #Build dictionary mapping from sides (2-tuple of points)
142    #to left hand side neighbouring triangle       
143    sides = {}
144    for id, triangle in enumerate(triangles):
145        a = triangle[0]
146        b = triangle[1]
147        c = triangle[2]       
148        sides[a,b] = (id, 2) #(id, face)
149        sides[b,c] = (id, 0) #(id, face)
150        sides[c,a] = (id, 1) #(id, face)
151    return sides       
152
Note: See TracBrowser for help on using the repository browser.