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

Last change on this file since 643 was 636, checked in by ole, 20 years ago

Comments

File size: 5.5 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    # set the water level to be the elevation
36    if vertex_quantity_dict.has_key('elevation') and not vertex_quantity_dict.has_key('level'):
37        vertex_quantity_dict['level'] = vertex_quantity_dict['elevation']
38    domain.set_quantity_vertices_dict(vertex_quantity_dict)
39    #print "vertex_quantity_dict",vertex_quantity_dict
40    return domain
41
42
43
44def pmesh_to_domain(fileName, setting_function = None):
45    """
46    convert a pmesh dictionary to a list of Volumes.
47    Also, return a list of triangles which have boundary tags
48    mesh_dict structure;
49    generated point list: [(x1,y1),(x2,y2),...] (Tuples of doubles) 
50    generated point attribute list:[(P1att1,P1attt2, ...),(P2att1,P2attt2,...),...]
51    generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers)
52    generated segment marker list: [S1Marker, S2Marker, ...] (list of ints)
53    triangle list:  [(point1,point2, point3),(p5,p4, p1),...] (Tuples of integers)
54    triangle neighbor list: [(triangle1,triangle2, triangle3),(t5,t4, t1),...] (Tuples of integers) -1 means there's no triangle neighbor
55    triangle attribute list: [T1att, T2att, ...] (list of strings)
56    """
57   
58    from Numeric import transpose
59    from load_mesh.loadASCII import mesh_file_to_mesh_dictionary
60   
61    mesh_dict = mesh_file_to_mesh_dictionary(fileName)
62    #print "mesh_dict",mesh_dict
63    vertex_coordinates = mesh_dict['generatedpointlist']
64    volumes = mesh_dict['generatedtrianglelist']
65
66    #if setting_function:
67    #    if not type(setting_function) is ListType:
68    #        setting_function = [setting_function]
69    #    for funct in setting_function:
70    #        mesh_dict = funct(mesh_dict, vertices = mesh_vertices,
71    #                        volumes = volumes)
72
73
74    vertex_quantity_dict = {}
75    point_atts = transpose(mesh_dict['generatedpointattributelist'])
76    point_titles  = mesh_dict['generatedpointattributetitlelist']
77    #print "point_titles",point_titles
78    for quantity, value_vector in map (None, point_titles, point_atts):
79        vertex_quantity_dict[quantity] = value_vector
80    marker_dict = pmesh_dict_to_marker_dict(mesh_dict)
81    tagged_elements_dict = build_tagged_elements_dictionary(mesh_dict)
82    return vertex_coordinates, volumes, marker_dict, vertex_quantity_dict,tagged_elements_dict
83
84
85
86def build_tagged_elements_dictionary(mesh_dict):
87    """Build the dictionary of element tags.
88    tagged_elements is a dictionary of element arrays,
89    keyed by tag:
90    { (tag): [e1, e2, e3..] }
91    """
92    tri_atts = mesh_dict['generatedtriangleattributelist']
93    #print "tri_atts", tri_atts
94    tagged_elements = {}
95    for tri_att_index in range(len(tri_atts)):
96        tagged_elements.setdefault(tri_atts[tri_att_index][0],[]).append(tri_att_index)
97    #print "DSG pm2do tagged_elements", tagged_elements
98    return tagged_elements
99
100#FIXME: The issue is whether this format should be stored in the tsh file
101#instead of having to be created here?
102
103#FIXME: Another issue is that the tsh file stores consecutive
104#indices explicitly. This is really redundant.
105#Suggest looking at obj and our own sww format and also consider
106#using netCDF.
107
108def pmesh_dict_to_marker_dict(mesh_dict):
109    """ Convert the pmesh dictionary (mesh_dict) description of boundary tags
110    to a dictionary of markers, indexed with volume id and face number.
111    """
112    triangles = mesh_dict['generatedtrianglelist']
113    sides = calc_sides(triangles)
114    marker_dict = {}
115    for seg, marker in map(None,mesh_dict['generatedsegmentlist'],
116                           mesh_dict['generatedsegmentmarkerlist']):
117        v1 = seg[0]
118        v2 = seg[1]
119        for key in [(v1,v2),(v2,v1)]:
120            if sides.has_key(key):
121                #this creates a dict of lists of faces, indexed by marker
122                #tagged_edges.setdefault(marker,[]).append(sides[key])
123                marker_dict[sides[key]] = marker
124    return marker_dict
125
126   
127def calc_sides(triangles):
128    #Build dictionary mapping from sides (2-tuple of points)
129    #to left hand side neighbouring triangle       
130    sides = {}
131    for id, triangle in enumerate(triangles):
132        a = triangle[0]
133        b = triangle[1]
134        c = triangle[2]       
135        sides[a,b] = (id, 2) #(id, face)
136        sides[b,c] = (id, 0) #(id, face)
137        sides[c,a] = (id, 1) #(id, face)
138    return sides       
139
Note: See TracBrowser for help on using the repository browser.