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

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

comments

File size: 6.1 KB
Line 
1"""Class pmesh2domain - Converting .tsh files to doamains
2
3
4   Copyright 2004
5   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
6   Geoscience Australia
7"""
8
9
10def pmesh_to_domain_instance(fileName, DomainClass, setting_function = None):
11    """
12    """
13    import sys
14    from domain import Domain
15
16    vertex_coordinates, volumes, marker_dict, vertex_quantity_dict ,tagged_elements_dict = \
17                        pmesh_to_domain(fileName,
18                                                   setting_function=setting_function)
19       
20    assert issubclass(DomainClass, Domain), "DomainClass is not a subclass of Domain."
21
22
23   
24    domain = DomainClass(vertex_coordinates, volumes, marker_dict,
25                         tagged_elements = tagged_elements_dict )
26
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   
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   
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']
43       
44    domain.set_quantity_vertices_dict(vertex_quantity_dict)
45    #print "vertex_quantity_dict",vertex_quantity_dict
46    return domain
47
48
49
50def pmesh_to_domain(fileName, setting_function = None):
51    """
52    convert a pmesh dictionary to a list of Volumes.
53    Also, return a list of triangles which have boundary tags
54    mesh_dict structure;
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)
58    generated segment marker list: [S1Marker, S2Marker, ...] (list of ints)
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
61    triangle attribute list: [T1att, T2att, ...] (list of strings)
62    """
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
69    vertex_coordinates = mesh_dict['generatedpointlist']
70    volumes = mesh_dict['generatedtrianglelist']
71
72    #if setting_function:
73    #    if not type(setting_function) is ListType:
74    #        setting_function = [setting_function]
75    #    for funct in setting_function:
76    #        mesh_dict = funct(mesh_dict, vertices = mesh_vertices,
77    #                        volumes = volumes)
78
79
80    vertex_quantity_dict = {}
81    point_atts = transpose(mesh_dict['generatedpointattributelist'])
82    point_titles  = mesh_dict['generatedpointattributetitlelist']
83    #print "point_titles",point_titles
84    for quantity, value_vector in map (None, point_titles, point_atts):
85        vertex_quantity_dict[quantity] = value_vector
86    marker_dict = pmesh_dict_to_marker_dict(mesh_dict)
87    tagged_elements_dict = build_tagged_elements_dictionary(mesh_dict)
88    return vertex_coordinates, volumes, marker_dict, vertex_quantity_dict,tagged_elements_dict
89
90
91
92def build_tagged_elements_dictionary(mesh_dict):
93    """Build the dictionary of element tags.
94    tagged_elements is a dictionary of element arrays,
95    keyed by tag:
96    { (tag): [e1, e2, e3..] }
97    """
98    tri_atts = mesh_dict['generatedtriangleattributelist']
99    #print "tri_atts", tri_atts
100    tagged_elements = {}
101    for tri_att_index in range(len(tri_atts)):
102        tagged_elements.setdefault(tri_atts[tri_att_index][0],[]).append(tri_att_index)
103    #print "DSG pm2do tagged_elements", tagged_elements
104    return tagged_elements
105
106#FIXME: The issue is whether this format should be stored in the tsh file
107#instead of having to be created here?
108
109#This information is pyvolution focused, not mesh generation focused.
110#This is an appropriate place for the info to be created. -DSG
111
112#FIXME: Another issue is that the tsh file stores consecutive
113#indices explicitly. This is really redundant.
114#Suggest looking at obj and our own sww format and also consider
115#using netCDF.
116
117# It is redundant.  It was the format originally decided on.
118# I'm happy for it to change. -DSG
119
120def pmesh_dict_to_marker_dict(mesh_dict):
121    """ Convert the pmesh dictionary (mesh_dict) description of boundary tags
122    to a dictionary of markers, indexed with volume id and face number.
123    """
124    triangles = mesh_dict['generatedtrianglelist']
125    sides = calc_sides(triangles)
126    marker_dict = {}
127    for seg, marker in map(None,mesh_dict['generatedsegmentlist'],
128                           mesh_dict['generatedsegmentmarkerlist']):
129        v1 = seg[0]
130        v2 = seg[1]
131        for key in [(v1,v2),(v2,v1)]:
132            if sides.has_key(key) and marker <> "":
133                #"" represents null.  Don't put these into the dictionary
134                #this creates a dict of lists of faces, indexed by marker
135                #tagged_edges.setdefault(marker,[]).append(sides[key])
136                marker_dict[sides[key]] = marker
137
138    return marker_dict
139
140   
141def calc_sides(triangles):
142    #Build dictionary mapping from sides (2-tuple of points)
143    #to left hand side neighbouring triangle       
144    sides = {}
145    for id, triangle in enumerate(triangles):
146        a = triangle[0]
147        b = triangle[1]
148        c = triangle[2]       
149        sides[a,b] = (id, 2) #(id, face)
150        sides[b,c] = (id, 0) #(id, face)
151        sides[c,a] = (id, 1) #(id, face)
152    return sides       
153
Note: See TracBrowser for help on using the repository browser.