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

Last change on this file since 7317 was 7317, checked in by rwilson, 15 years ago

Replaced 'print' statements with log.critical() calls.

File size: 6.5 KB
Line 
1#!/usr/bin/env python
2
3import sys
4
5from types import ListType, TupleType
6
7import exceptions
8
9class NoTrianglesError(exceptions.Exception): pass
10import anuga.mesh_engine.mesh_engine_c_layer as triang
11#import anuga.mesh_engine.list_dic as triang
12
13import numpy as num
14
15from anuga.utilities.numerical_tools import ensure_numeric
16from anuga.utilities.anuga_exceptions import ANUGAError
17   
18def generate_mesh(points=None,
19                  segments=None,holes=None,regions=None,
20                  pointatts=None,segatts=None,
21                  mode=None, dummy_test=None):
22    """
23    pointatts can be a list of lists.
24
25    generatedtriangleattributelist is used to represent tagged regions.
26    #FIXME (DSG-DSG): add comments
27    """
28    #FIXME (DSG-DSG): Catch parameters that are lists,
29    #instead of lists of lists
30    # check shape[1] is 2 etc
31
32    if points is None:
33        points = []
34
35    if segments is None:
36        segments = []
37
38    if holes is None:
39        holes = []
40       
41    if regions is None:
42        regions = []
43
44    if dummy_test is None:
45        dummy_test  = []
46       
47    try:
48        points =  ensure_numeric(points, num.float)
49    except ValueError:
50        msg = 'ERROR: Inconsistent points array.'
51        raise ANUGAError, msg
52    if points.shape[1] <>2:
53        msg = 'ERROR: Bad shape points array.'
54        raise ANUGAError, msg
55
56    # This is after points is numeric
57    if pointatts is None or pointatts == []:
58        pointatts = [[] for x in range(points.shape[0])]
59       
60    try:
61        # If num.int is used, instead of num.int32, it fails in Linux
62        segments = ensure_numeric(segments, num.int32)
63       
64    except ValueError:
65        msg = 'ERROR: Inconsistent segments array.'
66        raise ANUGAError, msg
67   
68    # This is after segments is numeric
69    if segatts is None or segatts == []:
70        segatts = [0 for x in range(segments.shape[0])]
71       
72    try:
73        holes = ensure_numeric(holes, num.float)
74    except ValueError:
75        msg = 'ERROR: Inconsistent holess array.'
76        raise ANUGAError, msg
77
78   
79    regions = add_area_tag(regions)
80    try:
81        regions = ensure_numeric(regions, num.float)
82    except  (ValueError, TypeError):
83        msg = 'ERROR: Inconsistent regions array.'
84        raise ANUGAError, msg
85       
86    if not regions.shape[0] == 0 and regions.shape[1] <= 2:
87        msg = 'ERROR: Bad shape points array.'
88        raise ANUGAError, msg
89   
90    try:
91        pointatts = ensure_numeric(pointatts, num.float)
92    except (ValueError, TypeError):
93        msg = 'ERROR: Inconsistent point attributes array.'
94        raise ANUGAError, msg
95
96    if pointatts.shape[0] <> points.shape[0]:
97        msg = """ERROR: Point attributes array not the same shape as
98        point array."""
99        raise ANUGAError, msg
100    if len(pointatts.shape) == 1:
101        pointatts = num.reshape(pointatts,(pointatts.shape[0],1))
102   
103    try:
104        segatts = ensure_numeric(segatts, num.int32)
105    except ValueError:
106        msg = 'ERROR: Inconsistent point attributes array.'
107        raise ANUGAError, msg
108    if segatts.shape[0] <> segments.shape[0]:
109        msg = """ERROR: Segment attributes array not the same shape as
110        segment array."""
111        raise ANUGAError, msg
112   
113    if mode.find('n'):
114        #pass
115        mode = 'j' + mode
116        # j- Jettisons vertices that are not part of the final
117        #    triangulation from the output .node file (including duplicate
118        #    input vertices and vertices ``eaten'' by holes).  - output a
119        #    list of neighboring triangles
120        # EG handles lone verts!
121           
122    trianglelist, pointlist, pointmarkerlist, pointattributelist, triangleattributelist, segmentlist, segmentmarkerlist, neighborlist = triang.genMesh(points,segments,holes,regions,
123                          pointatts,segatts, mode)
124    mesh_dict = {}
125    # the values as arrays
126    mesh_dict['generatedtrianglelist'] = trianglelist
127    mesh_dict['generatedpointlist'] = pointlist
128    # WARNING - generatedpointmarkerlist IS UNTESTED
129    mesh_dict['generatedpointmarkerlist'] = pointmarkerlist
130    mesh_dict['generatedpointattributelist'] = pointattributelist
131    mesh_dict['generatedsegmentlist'] = segmentlist
132    mesh_dict['generatedsegmentmarkerlist'] =  segmentmarkerlist
133    mesh_dict['generatedtriangleneighborlist'] = neighborlist
134    mesh_dict['qaz'] = 1 #debugging
135
136    #mesh_dict['triangleattributelist'] = triangleattributelist
137    if True: 
138        mesh_dict['generatedtriangleattributelist'] = triangleattributelist
139
140        if mesh_dict['generatedtriangleattributelist'].shape[1] == 0:
141            mesh_dict['generatedtriangleattributelist'] = None
142           
143        if mesh_dict['generatedpointattributelist'].shape[1] == 0:
144            mesh_dict['generatedpointattributelist'] = None
145           
146        if mesh_dict['generatedtriangleneighborlist'].shape[1] == 0:
147            mesh_dict['generatedtriangleneighborlist'] = None
148           
149        if trianglelist.shape[0] == 0:
150            # There are no triangles.
151            # this is used by urs_ungridded2sww
152            raise NoTrianglesError
153    a = mesh_dict['generatedtriangleattributelist']
154    # the structure of generatedtriangleattributelist is an list of
155    # list of integers.  It is transformed into a list of list of
156    # strings later on.  This is then inputted into an triangle
157    # object.  The triangle object outputs a list of strings.  Note
158    # the subtle change!  How should I handle this?  For my code, when
159    # the list of list of integers is transformed, transform it into a
160    # list of strings, not a list of list of strings.
161   
162    return mesh_dict
163
164def add_area_tag(regions):
165    """
166    So, what is the format?
167    A list with
168    [x,y,region_tag,area] OR [x,y,region_tag]
169    if it's [x,y,region_tag], add a 4th element, value of 0.0.
170    """
171    if isinstance(regions, ListType):
172        for i, region in enumerate(regions):
173            if len(region) == 3:
174                if isinstance(region, TupleType):
175                    #FIXME: How do you convert a tuple to a list?
176                    # I can do it a stupid way..
177                    tuple = region[:]
178                    regions[i] = []
179                    for j in tuple:
180                        regions[i].append(j)
181                    regions[i].append(0.0)
182                else:
183                    regions[i].append(0.0)
184                   
185                # let ensure numeric catch this..   
186                #len(region) <= 2:
187                #msg = 'ERROR: Inconsistent regions array.'
188                #raise msg
189            #elif
190    return regions
191
192if __name__ == "__main__":
193    pass 
Note: See TracBrowser for help on using the repository browser.