[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." |
---|
[415] | 21 | domain = DomainClass(vertex_coordinates, volumes, marker_dict, |
---|
| 22 | tagged_elements = tagged_elements_dict ) |
---|
[371] | 23 | |
---|
[389] | 24 | # set the water level to be the elevation |
---|
| 25 | if vertex_quantity_dict.has_key('elevation') and not vertex_quantity_dict.has_key('level'): |
---|
| 26 | vertex_quantity_dict['level'] = vertex_quantity_dict['elevation'] |
---|
| 27 | domain.set_quantity_vertices_dict(vertex_quantity_dict) |
---|
| 28 | #print "vertex_quantity_dict",vertex_quantity_dict |
---|
| 29 | return domain |
---|
[371] | 30 | |
---|
| 31 | |
---|
[389] | 32 | |
---|
| 33 | def pmesh_to_domain(fileName, setting_function = None): |
---|
[371] | 34 | """ |
---|
| 35 | convert a pmesh dictionary to a list of Volumes. |
---|
| 36 | Also, return a list of triangles which have boundary tags |
---|
[389] | 37 | mesh_dict structure; |
---|
[371] | 38 | generated point list: [(x1,y1),(x2,y2),...] (Tuples of doubles) |
---|
| 39 | generated point attribute list:[(P1att1,P1attt2, ...),(P2att1,P2attt2,...),...] |
---|
| 40 | generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers) |
---|
| 41 | generated segment marker list: [S1Marker, S2Marker, ...] (list of ints) |
---|
| 42 | triangle list: [(point1,point2, point3),(p5,p4, p1),...] (Tuples of integers) |
---|
| 43 | triangle neighbor list: [(triangle1,triangle2, triangle3),(t5,t4, t1),...] (Tuples of integers) -1 means there's no triangle neighbor |
---|
[415] | 44 | triangle attribute list: [T1att, T2att, ...] (list of strings) |
---|
[371] | 45 | """ |
---|
[389] | 46 | |
---|
| 47 | from Numeric import transpose |
---|
| 48 | from load_mesh.loadASCII import mesh_file_to_mesh_dictionary |
---|
| 49 | |
---|
| 50 | mesh_dict = mesh_file_to_mesh_dictionary(fileName) |
---|
| 51 | #print "mesh_dict",mesh_dict |
---|
| 52 | vertex_coordinates = mesh_dict['generatedpointlist'] |
---|
| 53 | volumes = mesh_dict['generatedtrianglelist'] |
---|
[371] | 54 | |
---|
| 55 | #if setting_function: |
---|
| 56 | # if not type(setting_function) is ListType: |
---|
| 57 | # setting_function = [setting_function] |
---|
| 58 | # for funct in setting_function: |
---|
[389] | 59 | # mesh_dict = funct(mesh_dict, vertices = mesh_vertices, |
---|
[371] | 60 | # volumes = volumes) |
---|
[389] | 61 | |
---|
| 62 | |
---|
| 63 | vertex_quantity_dict = {} |
---|
| 64 | point_atts = transpose(mesh_dict['generatedpointattributelist']) |
---|
| 65 | point_titles = mesh_dict['generatedpointattributetitlelist'] |
---|
| 66 | #print "point_titles",point_titles |
---|
| 67 | for quantity, value_vector in map (None, point_titles, point_atts): |
---|
| 68 | vertex_quantity_dict[quantity] = value_vector |
---|
[415] | 69 | marker_dict = pmesh_dict_to_marker_dict(mesh_dict) |
---|
| 70 | tagged_elements_dict = build_tagged_elements_dictionary(mesh_dict) |
---|
| 71 | return vertex_coordinates, volumes, marker_dict, vertex_quantity_dict,tagged_elements_dict |
---|
[371] | 72 | |
---|
| 73 | |
---|
| 74 | |
---|
[415] | 75 | def build_tagged_elements_dictionary(mesh_dict): |
---|
| 76 | """Build the dictionary of element tags. |
---|
| 77 | tagged_elements is a dictionary of element arrays, |
---|
| 78 | keyed by tag: |
---|
| 79 | { (tag): [e1, e2, e3..] } |
---|
| 80 | """ |
---|
| 81 | tri_atts = mesh_dict['generatedtriangleattributelist'] |
---|
| 82 | #print "tri_atts", tri_atts |
---|
| 83 | tagged_elements = {} |
---|
| 84 | for tri_att_index in range(len(tri_atts)): |
---|
| 85 | tagged_elements.setdefault(tri_atts[tri_att_index][0],[]).append(tri_att_index) |
---|
| 86 | #print "DSG pm2do tagged_elements", tagged_elements |
---|
| 87 | return tagged_elements |
---|
[371] | 88 | |
---|
| 89 | #FIXME: The issue is whether this format should be stored in the tsh file |
---|
| 90 | #instead of having to be created here? |
---|
| 91 | |
---|
| 92 | #FIXME: Another issue is that the tsh file stores consecutive |
---|
| 93 | #indices explicitly. This is really redundant. |
---|
| 94 | #Suggest looking at obj and our own sww format and also consider |
---|
| 95 | #using netCDF. |
---|
| 96 | |
---|
[415] | 97 | def pmesh_dict_to_marker_dict(mesh_dict): |
---|
[389] | 98 | """ Convert the pmesh dictionary (mesh_dict) description of boundary tags |
---|
[371] | 99 | to a dictionary of markers, indexed with volume id and face number. |
---|
| 100 | """ |
---|
[415] | 101 | triangles = mesh_dict['generatedtrianglelist'] |
---|
[371] | 102 | sides = calc_sides(triangles) |
---|
| 103 | marker_dict = {} |
---|
[389] | 104 | for seg, marker in map(None,mesh_dict['generatedsegmentlist'], |
---|
| 105 | mesh_dict['generatedsegmentmarkerlist']): |
---|
[371] | 106 | v1 = seg[0] |
---|
| 107 | v2 = seg[1] |
---|
| 108 | for key in [(v1,v2),(v2,v1)]: |
---|
| 109 | if sides.has_key(key): |
---|
| 110 | #this creates a dict of lists of faces, indexed by marker |
---|
| 111 | #tagged_edges.setdefault(marker,[]).append(sides[key]) |
---|
| 112 | marker_dict[sides[key]] = marker |
---|
| 113 | return marker_dict |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | def calc_sides(triangles): |
---|
| 117 | #Build dictionary mapping from sides (2-tuple of points) |
---|
| 118 | #to left hand side neighbouring triangle |
---|
| 119 | sides = {} |
---|
| 120 | for id, triangle in enumerate(triangles): |
---|
| 121 | a = triangle[0] |
---|
| 122 | b = triangle[1] |
---|
| 123 | c = triangle[2] |
---|
| 124 | sides[a,b] = (id, 2) #(id, face) |
---|
| 125 | sides[b,c] = (id, 0) #(id, face) |
---|
| 126 | sides[c,a] = (id, 1) #(id, face) |
---|
| 127 | return sides |
---|
| 128 | |
---|