source: branches/inundation-numpy-branch/pyvolution/pmesh2domain.py @ 7248

Last change on this file since 7248 was 2407, checked in by ole, 19 years ago

Pushed caching into conversion functions and beautified the Sydney scenario

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