[374] | 1 | """Class pmesh2domain - Converting .tsh files to doamains |
---|
[371] | 2 | |
---|
| 3 | |
---|
| 4 | Copyright 2004 |
---|
| 5 | Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou |
---|
| 6 | Geoscience Australia |
---|
| 7 | """ |
---|
| 8 | |
---|
[2056] | 9 | def pmesh_instance_to_domain_instance(mesh, |
---|
[2324] | 10 | DomainClass): |
---|
[2056] | 11 | """ |
---|
| 12 | """ |
---|
| 13 | import sys |
---|
| 14 | from domain import Domain |
---|
[371] | 15 | |
---|
[2056] | 16 | vertex_coordinates, vertices, tag_dict, vertex_quantity_dict \ |
---|
| 17 | ,tagged_elements_dict, geo_reference = \ |
---|
[2324] | 18 | _pmesh_to_domain(mesh_instance=mesh) |
---|
[2056] | 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 | |
---|
[2324] | 38 | def pmesh_to_domain_instance(file_name, DomainClass): |
---|
[371] | 39 | """ |
---|
[2324] | 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 | |
---|
[371] | 47 | """ |
---|
[2319] | 48 | |
---|
[371] | 49 | import sys |
---|
[389] | 50 | from domain import Domain |
---|
[371] | 51 | |
---|
[1556] | 52 | vertex_coordinates, vertices, tag_dict, vertex_quantity_dict \ |
---|
[1178] | 53 | ,tagged_elements_dict, geo_reference = \ |
---|
[2324] | 54 | _pmesh_to_domain(file_name=file_name) |
---|
[1556] | 55 | |
---|
| 56 | |
---|
[1178] | 57 | assert issubclass(DomainClass, Domain),"DomainClass is not a subclass of Domain." |
---|
[636] | 58 | |
---|
| 59 | |
---|
[1556] | 60 | domain = DomainClass(coordinates = vertex_coordinates, |
---|
| 61 | vertices = vertices, |
---|
| 62 | boundary = tag_dict, |
---|
[1178] | 63 | tagged_elements = tagged_elements_dict, |
---|
| 64 | geo_reference = geo_reference ) |
---|
[371] | 65 | |
---|
[636] | 66 | |
---|
[1556] | 67 | |
---|
[636] | 68 | #FIXME (Ole): Is this really the right place to apply the a default |
---|
| 69 | #value specific to the shallow water wave equation? |
---|
[1556] | 70 | #The 'assert' above indicates that any subclass of Domain is acceptable. |
---|
[636] | 71 | #Suggestion - module shallow_water.py will eventually take care of this |
---|
[1556] | 72 | #(when I get around to it) so it should be removed from here. |
---|
| 73 | |
---|
[905] | 74 | # This doesn't work on the domain instance. |
---|
| 75 | # This is still needed so -ve elevations don't cuase 'lakes' |
---|
[1178] | 76 | # The fixme we discussed was to only create a quantity when its values |
---|
| 77 | #are set. |
---|
[905] | 78 | # I think that's the way to go still |
---|
[1556] | 79 | |
---|
[773] | 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'] |
---|
[1556] | 83 | |
---|
[389] | 84 | domain.set_quantity_vertices_dict(vertex_quantity_dict) |
---|
| 85 | #print "vertex_quantity_dict",vertex_quantity_dict |
---|
| 86 | return domain |
---|
[371] | 87 | |
---|
| 88 | |
---|
[389] | 89 | |
---|
[2324] | 90 | def _pmesh_to_domain(file_name=None, |
---|
| 91 | mesh_instance=None): |
---|
[371] | 92 | """ |
---|
| 93 | convert a pmesh dictionary to a list of Volumes. |
---|
| 94 | Also, return a list of triangles which have boundary tags |
---|
[389] | 95 | mesh_dict structure; |
---|
[1556] | 96 | generated point list: [(x1,y1),(x2,y2),...] (Tuples of doubles) |
---|
[371] | 97 | generated point attribute list:[(P1att1,P1attt2, ...),(P2att1,P2attt2,...),...] |
---|
| 98 | generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers) |
---|
[969] | 99 | generated segment tag list: [S1Tag, S2Tag, ...] (list of ints) |
---|
[371] | 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 |
---|
[415] | 102 | triangle attribute list: [T1att, T2att, ...] (list of strings) |
---|
[371] | 103 | """ |
---|
[1556] | 104 | |
---|
[389] | 105 | from Numeric import transpose |
---|
[1423] | 106 | from load_mesh.loadASCII import import_mesh_file |
---|
[1556] | 107 | |
---|
[2324] | 108 | if file_name is None: |
---|
[2056] | 109 | mesh_dict = mesh_instance.Mesh2IODict() |
---|
| 110 | else: |
---|
[2324] | 111 | mesh_dict = import_mesh_file(file_name) |
---|
[389] | 112 | #print "mesh_dict",mesh_dict |
---|
[968] | 113 | vertex_coordinates = mesh_dict['vertices'] |
---|
| 114 | volumes = mesh_dict['triangles'] |
---|
[389] | 115 | vertex_quantity_dict = {} |
---|
[968] | 116 | point_atts = transpose(mesh_dict['vertex_attributes']) |
---|
| 117 | point_titles = mesh_dict['vertex_attribute_titles'] |
---|
[1178] | 118 | geo_reference = mesh_dict['geo_reference'] |
---|
[389] | 119 | for quantity, value_vector in map (None, point_titles, point_atts): |
---|
| 120 | vertex_quantity_dict[quantity] = value_vector |
---|
[969] | 121 | tag_dict = pmesh_dict_to_tag_dict(mesh_dict) |
---|
[415] | 122 | tagged_elements_dict = build_tagged_elements_dictionary(mesh_dict) |
---|
[1556] | 123 | return vertex_coordinates, volumes, tag_dict, vertex_quantity_dict, tagged_elements_dict, geo_reference |
---|
[371] | 124 | |
---|
| 125 | |
---|
| 126 | |
---|
[415] | 127 | def 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 | """ |
---|
[969] | 133 | tri_atts = mesh_dict['triangle_tags'] |
---|
[415] | 134 | #print "tri_atts", tri_atts |
---|
| 135 | tagged_elements = {} |
---|
| 136 | for tri_att_index in range(len(tri_atts)): |
---|
[1183] | 137 | tagged_elements.setdefault(tri_atts[tri_att_index],[]).append(tri_att_index) |
---|
[415] | 138 | #print "DSG pm2do tagged_elements", tagged_elements |
---|
| 139 | return tagged_elements |
---|
[371] | 140 | |
---|
[969] | 141 | def pmesh_dict_to_tag_dict(mesh_dict): |
---|
[389] | 142 | """ Convert the pmesh dictionary (mesh_dict) description of boundary tags |
---|
[969] | 143 | to a dictionary of tags, indexed with volume id and face number. |
---|
[371] | 144 | """ |
---|
[968] | 145 | triangles = mesh_dict['triangles'] |
---|
[371] | 146 | sides = calc_sides(triangles) |
---|
[969] | 147 | tag_dict = {} |
---|
| 148 | for seg, tag in map(None,mesh_dict['segments'], |
---|
| 149 | mesh_dict['segment_tags']): |
---|
[371] | 150 | v1 = seg[0] |
---|
| 151 | v2 = seg[1] |
---|
| 152 | for key in [(v1,v2),(v2,v1)]: |
---|
[969] | 153 | if sides.has_key(key) and tag <> "": |
---|
[686] | 154 | #"" represents null. Don't put these into the dictionary |
---|
[969] | 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 |
---|
[686] | 158 | |
---|
[969] | 159 | return tag_dict |
---|
[371] | 160 | |
---|
[1556] | 161 | |
---|
[371] | 162 | def calc_sides(triangles): |
---|
| 163 | #Build dictionary mapping from sides (2-tuple of points) |
---|
[1556] | 164 | #to left hand side neighbouring triangle |
---|
[371] | 165 | sides = {} |
---|
| 166 | for id, triangle in enumerate(triangles): |
---|
| 167 | a = triangle[0] |
---|
| 168 | b = triangle[1] |
---|
[1556] | 169 | c = triangle[2] |
---|
[371] | 170 | sides[a,b] = (id, 2) #(id, face) |
---|
| 171 | sides[b,c] = (id, 0) #(id, face) |
---|
| 172 | sides[c,a] = (id, 1) #(id, face) |
---|
[1556] | 173 | return sides |
---|