[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 | |
---|
[415] | 16 | vertex_coordinates, volumes, marker_dict, vertex_quantity_dict ,tagged_elements_dict = \ |
---|
[389] | 17 | pmesh_to_domain(fileName, |
---|
| 18 | setting_function=setting_function) |
---|
[371] | 19 | |
---|
[389] | 20 | assert issubclass(DomainClass, Domain), "DomainClass is not a subclass of Domain." |
---|
[636] | 21 | |
---|
| 22 | |
---|
| 23 | |
---|
[415] | 24 | domain = DomainClass(vertex_coordinates, volumes, marker_dict, |
---|
| 25 | tagged_elements = tagged_elements_dict ) |
---|
[371] | 26 | |
---|
[636] | 27 | |
---|
| 28 | |
---|
| 29 | #FIXME (Ole): Is this really the right place to apply the a default |
---|
| 30 | #value specific to the shallow water wave equation? |
---|
| 31 | #The 'assert' above indicates that any subclass of Domain is acceptable. |
---|
| 32 | #Suggestion - module shallow_water.py will eventually take care of this |
---|
| 33 | #(when I get around to it) so it should be removed from here. |
---|
| 34 | |
---|
[773] | 35 | # set the water stage to be the elevation |
---|
[663] | 36 | # This doesn't work on the domain instance. |
---|
[773] | 37 | if vertex_quantity_dict.has_key('elevation') and not vertex_quantity_dict.has_key('stage'): |
---|
| 38 | vertex_quantity_dict['stage'] = vertex_quantity_dict['elevation'] |
---|
[389] | 39 | domain.set_quantity_vertices_dict(vertex_quantity_dict) |
---|
| 40 | #print "vertex_quantity_dict",vertex_quantity_dict |
---|
| 41 | return domain |
---|
[371] | 42 | |
---|
| 43 | |
---|
[389] | 44 | |
---|
| 45 | def pmesh_to_domain(fileName, setting_function = None): |
---|
[371] | 46 | """ |
---|
| 47 | convert a pmesh dictionary to a list of Volumes. |
---|
| 48 | Also, return a list of triangles which have boundary tags |
---|
[389] | 49 | mesh_dict structure; |
---|
[371] | 50 | generated point list: [(x1,y1),(x2,y2),...] (Tuples of doubles) |
---|
| 51 | generated point attribute list:[(P1att1,P1attt2, ...),(P2att1,P2attt2,...),...] |
---|
| 52 | generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers) |
---|
| 53 | generated segment marker list: [S1Marker, S2Marker, ...] (list of ints) |
---|
| 54 | triangle list: [(point1,point2, point3),(p5,p4, p1),...] (Tuples of integers) |
---|
| 55 | triangle neighbor list: [(triangle1,triangle2, triangle3),(t5,t4, t1),...] (Tuples of integers) -1 means there's no triangle neighbor |
---|
[415] | 56 | triangle attribute list: [T1att, T2att, ...] (list of strings) |
---|
[371] | 57 | """ |
---|
[389] | 58 | |
---|
| 59 | from Numeric import transpose |
---|
| 60 | from load_mesh.loadASCII import mesh_file_to_mesh_dictionary |
---|
| 61 | |
---|
| 62 | mesh_dict = mesh_file_to_mesh_dictionary(fileName) |
---|
| 63 | #print "mesh_dict",mesh_dict |
---|
| 64 | vertex_coordinates = mesh_dict['generatedpointlist'] |
---|
| 65 | volumes = mesh_dict['generatedtrianglelist'] |
---|
[371] | 66 | |
---|
| 67 | #if setting_function: |
---|
| 68 | # if not type(setting_function) is ListType: |
---|
| 69 | # setting_function = [setting_function] |
---|
| 70 | # for funct in setting_function: |
---|
[389] | 71 | # mesh_dict = funct(mesh_dict, vertices = mesh_vertices, |
---|
[371] | 72 | # volumes = volumes) |
---|
[389] | 73 | |
---|
| 74 | |
---|
| 75 | vertex_quantity_dict = {} |
---|
| 76 | point_atts = transpose(mesh_dict['generatedpointattributelist']) |
---|
| 77 | point_titles = mesh_dict['generatedpointattributetitlelist'] |
---|
| 78 | #print "point_titles",point_titles |
---|
| 79 | for quantity, value_vector in map (None, point_titles, point_atts): |
---|
| 80 | vertex_quantity_dict[quantity] = value_vector |
---|
[415] | 81 | marker_dict = pmesh_dict_to_marker_dict(mesh_dict) |
---|
| 82 | tagged_elements_dict = build_tagged_elements_dictionary(mesh_dict) |
---|
| 83 | return vertex_coordinates, volumes, marker_dict, vertex_quantity_dict,tagged_elements_dict |
---|
[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 | """ |
---|
| 93 | tri_atts = mesh_dict['generatedtriangleattributelist'] |
---|
| 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 | |
---|
| 104 | #FIXME: Another issue is that the tsh file stores consecutive |
---|
| 105 | #indices explicitly. This is really redundant. |
---|
| 106 | #Suggest looking at obj and our own sww format and also consider |
---|
| 107 | #using netCDF. |
---|
| 108 | |
---|
[415] | 109 | def pmesh_dict_to_marker_dict(mesh_dict): |
---|
[389] | 110 | """ Convert the pmesh dictionary (mesh_dict) description of boundary tags |
---|
[371] | 111 | to a dictionary of markers, indexed with volume id and face number. |
---|
| 112 | """ |
---|
[415] | 113 | triangles = mesh_dict['generatedtrianglelist'] |
---|
[371] | 114 | sides = calc_sides(triangles) |
---|
| 115 | marker_dict = {} |
---|
[389] | 116 | for seg, marker in map(None,mesh_dict['generatedsegmentlist'], |
---|
| 117 | mesh_dict['generatedsegmentmarkerlist']): |
---|
[371] | 118 | v1 = seg[0] |
---|
| 119 | v2 = seg[1] |
---|
| 120 | for key in [(v1,v2),(v2,v1)]: |
---|
[686] | 121 | if sides.has_key(key) and marker <> "": |
---|
| 122 | #"" represents null. Don't put these into the dictionary |
---|
[371] | 123 | #this creates a dict of lists of faces, indexed by marker |
---|
| 124 | #tagged_edges.setdefault(marker,[]).append(sides[key]) |
---|
| 125 | marker_dict[sides[key]] = marker |
---|
[686] | 126 | |
---|
[371] | 127 | return marker_dict |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | def calc_sides(triangles): |
---|
| 131 | #Build dictionary mapping from sides (2-tuple of points) |
---|
| 132 | #to left hand side neighbouring triangle |
---|
| 133 | sides = {} |
---|
| 134 | for id, triangle in enumerate(triangles): |
---|
| 135 | a = triangle[0] |
---|
| 136 | b = triangle[1] |
---|
| 137 | c = triangle[2] |
---|
| 138 | sides[a,b] = (id, 2) #(id, face) |
---|
| 139 | sides[b,c] = (id, 0) #(id, face) |
---|
| 140 | sides[c,a] = (id, 1) #(id, face) |
---|
| 141 | return sides |
---|
| 142 | |
---|