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

Last change on this file since 1422 was 1422, checked in by duncan, 19 years ago

format fix

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