source: inundation/pyvolution/pmesh2domain.py @ 2597

Last change on this file since 2597 was 2597, checked in by howard, 18 years ago

Added FIXME comment

File size: 7.4 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
9def pmesh_instance_to_domain_instance(mesh,
10                                      DomainClass):
11    """
12    """
13    # FIXME (Howard): Needs a docstring to explain relationship to
14    # pmesh_to_domain
15    import sys
16    from domain import Domain
17
18    vertex_coordinates, vertices, tag_dict, vertex_quantity_dict \
19                        ,tagged_elements_dict, geo_reference = \
20                        _pmesh_to_domain(mesh_instance=mesh)
21
22
23    assert issubclass(DomainClass, Domain),"DomainClass is not a subclass of Domain."
24
25
26    domain = DomainClass(coordinates = vertex_coordinates,
27                         vertices = vertices,
28                         boundary = tag_dict,
29                         tagged_elements = tagged_elements_dict,
30                         geo_reference = geo_reference )
31
32    # set the water stage to be the elevation
33    if vertex_quantity_dict.has_key('elevation') and not vertex_quantity_dict.has_key('stage'):
34        vertex_quantity_dict['stage'] = vertex_quantity_dict['elevation']
35
36    domain.set_quantity_vertices_dict(vertex_quantity_dict)
37    #print "vertex_quantity_dict",vertex_quantity_dict
38    return domain
39
40
41
42def pmesh_to_domain_instance(file_name, DomainClass, use_cache = False, verbose = False):
43    """
44    Converts a mesh file(.tsh or .msh), to a Domain instance.
45
46    file_name is the name of the mesh file to convert, including the extension
47
48    DomainClass is the Class that will be returned.
49    It must be a subclass of Domain, with the same interface as domain.
50
51    use_cache: True means that caching is attempted for the computed domain.   
52    """
53
54    if use_cache is True:
55        from caching import cache
56        result = cache(_pmesh_to_domain_instance, (file_name, DomainClass),
57                       dependencies = [file_name],                     
58                       verbose = verbose)
59
60    else:
61        result = apply(_pmesh_to_domain_instance, (file_name, DomainClass))       
62       
63    return result
64
65
66
67
68def _pmesh_to_domain_instance(file_name, DomainClass):
69    """
70    Converts a mesh file(.tsh or .msh), to a Domain instance.
71
72    Internal function. See public interface pmesh_to_domain_instance for details
73    """
74   
75    import sys
76    from domain import Domain
77
78    vertex_coordinates, vertices, tag_dict, vertex_quantity_dict \
79                        ,tagged_elements_dict, geo_reference = \
80                        _pmesh_to_domain(file_name=file_name)
81
82
83    assert issubclass(DomainClass, Domain),"DomainClass is not a subclass of Domain."
84
85
86    domain = DomainClass(coordinates = vertex_coordinates,
87                         vertices = vertices,
88                         boundary = tag_dict,
89                         tagged_elements = tagged_elements_dict,
90                         geo_reference = geo_reference )
91
92
93
94    #FIXME (Ole): Is this really the right place to apply the a default
95    #value specific to the shallow water wave equation?
96    #The 'assert' above indicates that any subclass of Domain is acceptable.
97    #Suggestion - module shallow_water.py will eventually take care of this
98    #(when I get around to it) so it should be removed from here.
99
100    # This doesn't work on the domain instance.
101    # This is still needed so -ve elevations don't cuase 'lakes'
102    # The fixme we discussed was to only create a quantity when its values
103    #are set.
104    # I think that's the way to go still
105
106    # set the water stage to be the elevation
107    if vertex_quantity_dict.has_key('elevation') and not vertex_quantity_dict.has_key('stage'):
108        vertex_quantity_dict['stage'] = vertex_quantity_dict['elevation']
109
110    domain.set_quantity_vertices_dict(vertex_quantity_dict)
111    #print "vertex_quantity_dict",vertex_quantity_dict
112    return domain
113
114
115
116def _pmesh_to_domain(file_name=None,
117                    mesh_instance=None):
118    """
119    convert a pmesh dictionary to a list of Volumes.
120    Also, return a list of triangles which have boundary tags
121    mesh_dict structure;
122    generated point list: [(x1,y1),(x2,y2),...] (Tuples of doubles)
123    generated point attribute list:[(P1att1,P1attt2, ...),(P2att1,P2attt2,...),...]
124    generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers)
125    generated segment tag list: [S1Tag, S2Tag, ...] (list of ints)
126    triangle list:  [(point1, point2, point3),(p5,p4, p1),...] (Tuples of integers)
127    triangle neighbor list: [(triangle1,triangle2, triangle3),(t5,t4, t1),...] (Tuples of integers) -1 means there's no triangle neighbor
128    triangle attribute list: [T1att, T2att, ...] (list of strings)
129    """
130
131    from Numeric import transpose
132    from load_mesh.loadASCII import import_mesh_file
133
134    if file_name is None:
135        mesh_dict = mesh_instance.Mesh2IODict()
136    else:
137        mesh_dict = import_mesh_file(file_name)
138    #print "mesh_dict",mesh_dict
139    vertex_coordinates = mesh_dict['vertices']
140    volumes = mesh_dict['triangles']
141    vertex_quantity_dict = {}
142    point_atts = transpose(mesh_dict['vertex_attributes'])
143    point_titles  = mesh_dict['vertex_attribute_titles']
144    geo_reference  = mesh_dict['geo_reference']
145    for quantity, value_vector in map (None, point_titles, point_atts):
146        vertex_quantity_dict[quantity] = value_vector
147    tag_dict = pmesh_dict_to_tag_dict(mesh_dict)
148    tagged_elements_dict = build_tagged_elements_dictionary(mesh_dict)
149    return vertex_coordinates, volumes, tag_dict, vertex_quantity_dict, tagged_elements_dict, geo_reference
150
151
152
153def build_tagged_elements_dictionary(mesh_dict):
154    """Build the dictionary of element tags.
155    tagged_elements is a dictionary of element arrays,
156    keyed by tag:
157    { (tag): [e1, e2, e3..] }
158    """
159    tri_atts = mesh_dict['triangle_tags']
160    #print "tri_atts", tri_atts
161    tagged_elements = {}
162    for tri_att_index in range(len(tri_atts)):
163        tagged_elements.setdefault(tri_atts[tri_att_index],[]).append(tri_att_index)
164    #print "DSG pm2do tagged_elements", tagged_elements
165    return tagged_elements
166
167def pmesh_dict_to_tag_dict(mesh_dict):
168    """ Convert the pmesh dictionary (mesh_dict) description of boundary tags
169    to a dictionary of tags, indexed with volume id and face number.
170    """
171    triangles = mesh_dict['triangles']
172    sides = calc_sides(triangles)
173    tag_dict = {}
174    for seg, tag in map(None, mesh_dict['segments'],
175                        mesh_dict['segment_tags']):
176        v1 = seg[0]
177        v2 = seg[1]
178        for key in [(v1,v2),(v2,v1)]:
179            if sides.has_key(key) and tag <> "":
180                #"" represents null.  Don't put these into the dictionary
181                #this creates a dict of lists of faces, indexed by tag
182                #tagged_edges.setdefault(tag,[]).append(sides[key])
183                tag_dict[sides[key]] = tag
184
185    return tag_dict
186
187
188def calc_sides(triangles):
189    #Build dictionary mapping from sides (2-tuple of points)
190    #to left hand side neighbouring triangle
191    sides = {}
192    for id, triangle in enumerate(triangles):
193        a = triangle[0]
194        b = triangle[1]
195        c = triangle[2]
196        sides[a,b] = (id, 2) #(id, face)
197        sides[b,c] = (id, 0) #(id, face)
198        sides[c,a] = (id, 1) #(id, face)
199    return sides
Note: See TracBrowser for help on using the repository browser.