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

Last change on this file since 4218 was 4158, checked in by duncan, 18 years ago

committing files that should've been committed in r4144

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