[371] | 1 | """Class mesh2domain - 2D triangular domains for finite-volume computations of |
---|
| 2 | the shallow water wave equation |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | Copyright 2004 |
---|
| 6 | Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou |
---|
| 7 | Geoscience Australia |
---|
| 8 | """ |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | def pmesh_to_domain(fileName, tags = None, setting_function = None): |
---|
| 12 | """ |
---|
| 13 | """ |
---|
| 14 | #FIXME: The current design doesn't seem to accomadate tags and |
---|
| 15 | # setting_functions being passed into the domain at this point. |
---|
| 16 | |
---|
| 17 | #FIXME: Plus the names of the functions are no longer appropriate, |
---|
| 18 | #since domain objects aren't being returned. |
---|
| 19 | |
---|
| 20 | import sys |
---|
| 21 | from config import pmesh_filename |
---|
| 22 | sys.path.append(pmesh_filename) |
---|
| 23 | from load_mesh.loadASCII import import_trianglulation |
---|
| 24 | |
---|
| 25 | try: |
---|
| 26 | meshdic = import_trianglulation(fileName) |
---|
| 27 | except IOError, e: |
---|
| 28 | msg = 'Could not open file %s ' %fileName |
---|
| 29 | raise IOError, msg |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | return pmesh_dictionary_to_domain(meshdic, |
---|
| 33 | setting_function = setting_function) |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | def pmesh_dictionary_to_domain(meshdic, setting_function = None): |
---|
| 38 | """ |
---|
| 39 | convert a pmesh dictionary to a list of Volumes. |
---|
| 40 | Also, return a list of triangles which have boundary tags |
---|
| 41 | meshdic structure; |
---|
| 42 | generated point list: [(x1,y1),(x2,y2),...] (Tuples of doubles) |
---|
| 43 | generated point attribute list:[(P1att1,P1attt2, ...),(P2att1,P2attt2,...),...] |
---|
| 44 | generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers) |
---|
| 45 | generated segment marker list: [S1Marker, S2Marker, ...] (list of ints) |
---|
| 46 | triangle list: [(point1,point2, point3),(p5,p4, p1),...] (Tuples of integers) |
---|
| 47 | triangle neighbor list: [(triangle1,triangle2, triangle3),(t5,t4, t1),...] (Tuples of integers) -1 means there's no triangle neighbor |
---|
| 48 | triangle attribute list: [T1att, T2att, ...] (list of floats) |
---|
| 49 | """ |
---|
| 50 | |
---|
| 51 | vertex_coordinates = meshdic['generatedpointlist'] |
---|
| 52 | volumes = meshdic['generatedtrianglelist'] |
---|
| 53 | |
---|
| 54 | #if setting_function: |
---|
| 55 | # if not type(setting_function) is ListType: |
---|
| 56 | # setting_function = [setting_function] |
---|
| 57 | # for funct in setting_function: |
---|
| 58 | # meshdic = funct(meshdic, vertices = mesh_vertices, |
---|
| 59 | # volumes = volumes) |
---|
| 60 | |
---|
| 61 | marker_dict = pmesh_dict_to_marker_dict(volumes, meshdic, |
---|
| 62 | vertex_coordinates) |
---|
| 63 | |
---|
| 64 | return vertex_coordinates, volumes, marker_dict |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | #FIXME: The issue is whether this format should be stored in the tsh file |
---|
| 70 | #instead of having to be created here? |
---|
| 71 | |
---|
| 72 | #FIXME: Another issue is that the tsh file stores consecutive |
---|
| 73 | #indices explicitly. This is really redundant. |
---|
| 74 | #Suggest looking at obj and our own sww format and also consider |
---|
| 75 | #using netCDF. |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | def pmesh_dict_to_marker_dict(triangles,meshdic,Vertices): |
---|
| 79 | """ Convert the pmesh dictionary (meshdic) description of boundary tags |
---|
| 80 | to a dictionary of markers, indexed with volume id and face number. |
---|
| 81 | """ |
---|
| 82 | sides = calc_sides(triangles) |
---|
| 83 | marker_dict = {} |
---|
| 84 | for seg, marker in map(None,meshdic['generatedsegmentlist'], |
---|
| 85 | meshdic['generatedsegmentmarkerlist']): |
---|
| 86 | v1 = seg[0] |
---|
| 87 | v2 = seg[1] |
---|
| 88 | for key in [(v1,v2),(v2,v1)]: |
---|
| 89 | if sides.has_key(key): |
---|
| 90 | #this creates a dict of lists of faces, indexed by marker |
---|
| 91 | #tagged_edges.setdefault(marker,[]).append(sides[key]) |
---|
| 92 | marker_dict[sides[key]] = marker |
---|
| 93 | return marker_dict |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | def calc_sides(triangles): |
---|
| 97 | #Build dictionary mapping from sides (2-tuple of points) |
---|
| 98 | #to left hand side neighbouring triangle |
---|
| 99 | sides = {} |
---|
| 100 | for id, triangle in enumerate(triangles): |
---|
| 101 | a = triangle[0] |
---|
| 102 | b = triangle[1] |
---|
| 103 | c = triangle[2] |
---|
| 104 | sides[a,b] = (id, 2) #(id, face) |
---|
| 105 | sides[b,c] = (id, 0) #(id, face) |
---|
| 106 | sides[c,a] = (id, 1) #(id, face) |
---|
| 107 | return sides |
---|
| 108 | |
---|