source: inundation/ga/storm_surge/pyvolution/pmesh2domain.py @ 303

Last change on this file since 303 was 298, checked in by ole, 21 years ago

Files for pmesh_to_domain

File size: 3.5 KB
Line 
1def pmesh_to_domain(fileName, tags = None, setting_function = None):
2    import sys
3    from config import pmesh_filename
4    sys.path.append(pmesh_filename)
5    from load_mesh.loadASCII import import_trianglulation
6
7    try:
8        meshdic = import_trianglulation(fileName)
9    except IOError, e:       
10        msg = 'Could not open file %s ' %fileName
11        raise IOError, msg
12
13       
14    domain  = pmesh_dictionary_to_domain(meshdic,
15                                         setting_function = setting_function)
16    if tags:
17        domain.set_boundary(tags)
18       
19    return domain
20
21
22
23def pmesh_dictionary_to_domain(meshdic, setting_function = None):
24    """
25    convert a pmesh dictionary to a list of Volumes.
26    Also, return a list of triangles which have boundary tags
27    meshdic structure;
28    generated point list: [(x1,y1),(x2,y2),...] (Tuples of doubles) 
29    generated point attribute list:[(P1att1,P1attt2, ...),(P2att1,P2attt2,...),...]
30    generated segment list: [(point1,point2),(p3,p4),...] (Tuples of integers)
31    generated segment marker list: [S1Marker, S2Marker, ...] (list of ints)
32    triangle list:  [(point1,point2, point3),(p5,p4, p1),...] (Tuples of integers)
33    triangle neighbor list: [(triangle1,triangle2, triangle3),(t5,t4, t1),...] (Tuples of integers) -1 means there's no triangle neighbor
34    triangle attribute list: [T1att, T2att, ...] (list of floats)
35    """
36
37    vertex_coordinates = meshdic['generatedpointlist']
38    volumes = meshdic['generatedtrianglelist']
39
40    #if setting_function:
41    #    if not type(setting_function) is ListType:
42    #        setting_function = [setting_function]
43    #    for funct in setting_function:
44    #        meshdic = funct(meshdic, vertices = mesh_vertices,
45    #                        volumes = volumes)
46       
47    marker_dict = pmesh_dict_to_marker_dict(volumes, meshdic,
48                                            vertex_coordinates)
49
50    return vertex_coordinates, volumes, marker_dict
51
52
53
54
55#FIXME: The issue is whether this format should be stored in the tsh file
56#instead of having to be created here?
57
58#FIXME: Another issue is that the tsh file stores consecutive
59#indices explicitly. This is really redundant.
60#Suggest looking at obj and our own sww format and also consider
61#using netCDF.
62
63   
64def pmesh_dict_to_marker_dict(triangles,meshdic,Vertices):
65    """ Convert the pmesh dictionary (meshdic) description of boundary tags
66    to a dictionary of markers, indexed with volume id and face number.
67    """
68    sides = calc_sides(triangles)
69    marker_dict = {}
70    for seg, marker in map(None,meshdic['generatedsegmentlist'],
71                           meshdic['generatedsegmentmarkerlist']):
72        v1 = seg[0]
73        v2 = seg[1]
74        for key in [(v1,v2),(v2,v1)]:
75            if sides.has_key(key):
76                #this creates a dict of lists of faces, indexed by marker
77                #tagged_edges.setdefault(marker,[]).append(sides[key])
78                marker_dict[sides[key]] = marker
79    return marker_dict
80
81   
82def calc_sides(triangles):
83    #Build dictionary mapping from sides (2-tuple of points)
84    #to left hand side neighbouring triangle       
85    sides = {}
86    for id, triangle in enumerate(triangles):
87        a = triangle[0]
88        b = triangle[1]
89        c = triangle[2]       
90        sides[a,b] = (id, 2) #(id, face)
91        sides[b,c] = (id, 0) #(id, face)
92        sides[c,a] = (id, 1) #(id, face)
93    return sides       
94
Note: See TracBrowser for help on using the repository browser.