source: inundation/pyvolution/pmesh2domain.py @ 2378

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

comments

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