source: anuga_core/source/anuga/mesh_engine/mesh_engine.py @ 4894

Last change on this file since 4894 was 4894, checked in by duncan, 16 years ago

Good fix for the memory leak found in ticket#189

File size: 4.9 KB
Line 
1#!/usr/bin/env python
2
3import sys
4
5from types import ListType, TupleType
6
7import anuga.mesh_engine.mesh_engine_c_layer as triang
8#import anuga.mesh_engine.list_dic as triang
9
10from Numeric import array, Float, Int32
11
12from anuga.utilities.numerical_tools import ensure_numeric
13from anuga.utilities.anuga_exceptions import ANUGAError
14   
15def generate_mesh(points=None,
16                  segments=None,holes=None,regions=None,
17                  pointatts=None,segatts=None,
18                  mode=None, dummy_test=None):
19    """
20   
21    """
22    #FIXME (DSG-DSG): Catch parameters that are lists,
23    #instead of lists of lists
24    # check shape[1] is 2 etc
25
26    if points is None:
27        points = []
28
29    if segments is None:
30        segments = []
31
32    if holes is None:
33        holes = []
34       
35    if regions is None:
36        regions = []
37
38    if dummy_test is None:
39        dummy_test  = []
40       
41    try:
42        points =  ensure_numeric(points, Float)
43    except ValueError:
44        msg = 'ERROR: Inconsistent points array.'
45        raise ANUGAError, msg
46    if points.shape[1] <>2:
47        msg = 'ERROR: Bad shape points array.'
48        raise ANUGAError, msg
49
50    #print "pointatts",pointatts
51    # This is after points is numeric
52    if pointatts is None or pointatts == []:
53        pointatts = [[] for x in range(points.shape[0])]
54       
55    try:
56        # If Int is used, instead of Int32, it fails in Linux
57        segments = ensure_numeric(segments, Int32)
58    except ValueError:
59        msg = 'ERROR: Inconsistent segments array.'
60        raise ANUGAError, msg
61   
62    # This is after segments is numeric
63    if segatts is None or segatts == []:
64        segatts = [0 for x in range(segments.shape[0])]
65       
66    try:
67        holes = ensure_numeric(holes, Float)
68    except ValueError:
69        msg = 'ERROR: Inconsistent holess array.'
70        raise ANUGAError, msg
71
72   
73    regions = add_area_tag(regions)
74    try:
75        regions = ensure_numeric(regions, Float)
76    except  (ValueError, TypeError):
77        msg = 'ERROR: Inconsistent regions array.'
78        raise ANUGAError, msg
79       
80    if not regions.shape[0] == 0 and regions.shape[1] <= 2:
81        msg = 'ERROR: Bad shape points array.'
82        raise ANUGAError, msg
83   
84    try:
85        #print "pointatts",pointatts
86        pointatts = ensure_numeric(pointatts, Float)
87    except (ValueError, TypeError):
88        msg = 'ERROR: Inconsistent point attributes array.'
89        raise ANUGAError, msg
90
91    if pointatts.shape[0] <> points.shape[0]:
92        msg = """ERROR: Point attributes array not the same shape as
93        point array."""
94        raise ANUGAError, msg
95   
96    try:
97        segatts = ensure_numeric(segatts, Int32)
98    except ValueError:
99        msg = 'ERROR: Inconsistent point attributes array.'
100        raise ANUGAError, msg
101    if segatts.shape[0] <> segments.shape[0]:
102        msg = """ERROR: Segment attributes array not the same shape as
103        segment array."""
104        raise ANUGAError, msg
105
106   
107    #print "mode", mode
108    if mode.find('n'):
109        #pass
110        mode = 'j' + mode
111        # j- Jettisons vertices that are not part of the final
112        #    triangulation from the output .node file (including duplicate
113        #    input vertices and vertices ``eaten'' by holes).  - output a
114        #    list of neighboring triangles
115           
116    #print "points",points
117    #print "segments", segments
118    #print "segments.shape", segments.shape
119    #print "holes", holes
120    #print "regions", regions
121    #print "pointatts", pointatts
122    #print "segatts", segatts
123    #print "mode", mode
124    #print "yeah"
125    mesh_dict, r_test = triang.genMesh(points,segments,holes,regions,
126                          pointatts,segatts, mode, segments.flat)
127    mesh_dict['qaz'] = 1 #debugging
128    ##print "r_test", r_test
129    return mesh_dict
130
131def add_area_tag(regions):
132    """
133    So, what is the format?
134    A list with
135    [x,y,region_tag,area] OR [x,y,region_tag]
136    if it's [x,y,region_tag], add a 4th element, value of 0.0.
137    """
138    if isinstance(regions, ListType):
139        for i, region in enumerate(regions):
140            if len(region) == 3:
141                if isinstance(region, TupleType):
142                    #FIXME: How do you convert a tuple to a list?
143                    # I can do it a stupid way..
144                    tuple = region[:]
145                    regions[i] = []
146                    for j in tuple:
147                        regions[i].append(j)
148                    regions[i].append(0.0)
149                else:
150                    regions[i].append(0.0)
151                   
152                # let ensure numeric catch this..   
153                #len(region) <= 2:
154                #msg = 'ERROR: Inconsistent regions array.'
155                #raise msg
156            #elif
157    return regions
158
159if __name__ == "__main__":
160    pass 
Note: See TracBrowser for help on using the repository browser.