[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 | |
---|
| 9 | |
---|
[415] | 10 | def pmesh_to_domain_instance(fileName, DomainClass, setting_function = None): |
---|
[371] | 11 | """ |
---|
| 12 | """ |
---|
| 13 | import sys |
---|
[389] | 14 | from domain import Domain |
---|
[371] | 15 | |
---|
[1178] | 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) |
---|
[371] | 20 | |
---|
[1178] | 21 | assert issubclass(DomainClass, Domain),"DomainClass is not a subclass of Domain." |
---|
[636] | 22 | |
---|
| 23 | |
---|
| 24 | |
---|
[969] | 25 | domain = DomainClass(vertex_coordinates, volumes, tag_dict, |
---|
[1178] | 26 | tagged_elements = tagged_elements_dict, |
---|
| 27 | geo_reference = geo_reference ) |
---|
[371] | 28 | |
---|
[636] | 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 | |
---|
[905] | 37 | # This doesn't work on the domain instance. |
---|
| 38 | # This is still needed so -ve elevations don't cuase 'lakes' |
---|
[1178] | 39 | # The fixme we discussed was to only create a quantity when its values |
---|
| 40 | #are set. |
---|
[905] | 41 | # I think that's the way to go still |
---|
| 42 | |
---|
[773] | 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'] |
---|
[905] | 46 | |
---|
[389] | 47 | domain.set_quantity_vertices_dict(vertex_quantity_dict) |
---|
| 48 | #print "vertex_quantity_dict",vertex_quantity_dict |
---|
| 49 | return domain |
---|
[371] | 50 | |
---|
| 51 | |
---|
[389] | 52 | |
---|
| 53 | def pmesh_to_domain(fileName, setting_function = None): |
---|
[371] | 54 | """ |
---|
| 55 | convert a pmesh dictionary to a list of Volumes. |
---|
| 56 | Also, return a list of triangles which have boundary tags |
---|
[389] | 57 | mesh_dict structure; |
---|
[371] | 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) |
---|
[969] | 61 | generated segment tag list: [S1Tag, S2Tag, ...] (list of ints) |
---|
[371] | 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 |
---|
[415] | 64 | triangle attribute list: [T1att, T2att, ...] (list of strings) |
---|
[371] | 65 | """ |
---|
[389] | 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 |
---|
[968] | 72 | vertex_coordinates = mesh_dict['vertices'] |
---|
| 73 | volumes = mesh_dict['triangles'] |
---|
[371] | 74 | |
---|
[389] | 75 | vertex_quantity_dict = {} |
---|
[968] | 76 | point_atts = transpose(mesh_dict['vertex_attributes']) |
---|
| 77 | point_titles = mesh_dict['vertex_attribute_titles'] |
---|
[1178] | 78 | geo_reference = mesh_dict['geo_reference'] |
---|
[389] | 79 | for quantity, value_vector in map (None, point_titles, point_atts): |
---|
| 80 | vertex_quantity_dict[quantity] = value_vector |
---|
[969] | 81 | tag_dict = pmesh_dict_to_tag_dict(mesh_dict) |
---|
[415] | 82 | tagged_elements_dict = build_tagged_elements_dictionary(mesh_dict) |
---|
[1178] | 83 | return vertex_coordinates, volumes, tag_dict, vertex_quantity_dict,tagged_elements_dict, geo_reference |
---|
[371] | 84 | |
---|
| 85 | |
---|
| 86 | |
---|
[415] | 87 | def build_tagged_elements_dictionary(mesh_dict): |
---|
| 88 | """Build the dictionary of element tags. |
---|
| 89 | tagged_elements is a dictionary of element arrays, |
---|
| 90 | keyed by tag: |
---|
| 91 | { (tag): [e1, e2, e3..] } |
---|
| 92 | """ |
---|
[969] | 93 | tri_atts = mesh_dict['triangle_tags'] |
---|
[415] | 94 | #print "tri_atts", tri_atts |
---|
| 95 | tagged_elements = {} |
---|
| 96 | for tri_att_index in range(len(tri_atts)): |
---|
| 97 | tagged_elements.setdefault(tri_atts[tri_att_index][0],[]).append(tri_att_index) |
---|
| 98 | #print "DSG pm2do tagged_elements", tagged_elements |
---|
| 99 | return tagged_elements |
---|
[371] | 100 | |
---|
| 101 | #FIXME: The issue is whether this format should be stored in the tsh file |
---|
| 102 | #instead of having to be created here? |
---|
| 103 | |
---|
[905] | 104 | #This information is pyvolution focused, not mesh generation focused. |
---|
| 105 | #This is an appropriate place for the info to be created. -DSG |
---|
| 106 | |
---|
[371] | 107 | #FIXME: Another issue is that the tsh file stores consecutive |
---|
| 108 | #indices explicitly. This is really redundant. |
---|
| 109 | #Suggest looking at obj and our own sww format and also consider |
---|
| 110 | #using netCDF. |
---|
| 111 | |
---|
[905] | 112 | # It is redundant. It was the format originally decided on. |
---|
| 113 | # I'm happy for it to change. -DSG |
---|
| 114 | |
---|
[969] | 115 | def pmesh_dict_to_tag_dict(mesh_dict): |
---|
[389] | 116 | """ Convert the pmesh dictionary (mesh_dict) description of boundary tags |
---|
[969] | 117 | to a dictionary of tags, indexed with volume id and face number. |
---|
[371] | 118 | """ |
---|
[968] | 119 | triangles = mesh_dict['triangles'] |
---|
[371] | 120 | sides = calc_sides(triangles) |
---|
[969] | 121 | tag_dict = {} |
---|
| 122 | for seg, tag in map(None,mesh_dict['segments'], |
---|
| 123 | mesh_dict['segment_tags']): |
---|
[371] | 124 | v1 = seg[0] |
---|
| 125 | v2 = seg[1] |
---|
| 126 | for key in [(v1,v2),(v2,v1)]: |
---|
[969] | 127 | if sides.has_key(key) and tag <> "": |
---|
[686] | 128 | #"" represents null. Don't put these into the dictionary |
---|
[969] | 129 | #this creates a dict of lists of faces, indexed by tag |
---|
| 130 | #tagged_edges.setdefault(tag,[]).append(sides[key]) |
---|
| 131 | tag_dict[sides[key]] = tag |
---|
[686] | 132 | |
---|
[969] | 133 | return tag_dict |
---|
[371] | 134 | |
---|
| 135 | |
---|
| 136 | def calc_sides(triangles): |
---|
| 137 | #Build dictionary mapping from sides (2-tuple of points) |
---|
| 138 | #to left hand side neighbouring triangle |
---|
| 139 | sides = {} |
---|
| 140 | for id, triangle in enumerate(triangles): |
---|
| 141 | a = triangle[0] |
---|
| 142 | b = triangle[1] |
---|
| 143 | c = triangle[2] |
---|
| 144 | sides[a,b] = (id, 2) #(id, face) |
---|
| 145 | sides[b,c] = (id, 0) #(id, face) |
---|
| 146 | sides[c,a] = (id, 1) #(id, face) |
---|
| 147 | return sides |
---|
| 148 | |
---|