source: inundation/fit_interpolate/benchmark_least_squares.py @ 2695

Last change on this file since 2695 was 2573, checked in by duncan, 18 years ago

bug fixing, adding infinite variable to use with Numeric

File size: 5.9 KB
Line 
1"""Least squares smooting and interpolation.
2
3   measure the speed of least squares.
4
5   ________________________
6   General comments
7   
8   The max_points_per_cell does effect the time spent solving a
9   problem.  The best value to use is probably dependent on the number
10   of triangles.  Maybe develop a simple imperical algorithm, based on
11   test results.
12   
13   Duncan Gray
14   Geoscience Australia, 2004.
15"""
16
17
18import os
19import sys
20import time
21from random import seed, random
22
23from pyvolution.least_squares import Interpolation
24from fit_interpolate.interpolate import Interpolate
25from pmesh.mesh import Mesh
26
27def mem_usage():
28    '''
29    returns the rss.
30
31  RSS  The total amount of physical memory used by  the  task,  in  kilo-
32            bytes,  is  shown  here.  For ELF processes used library pages are
33            counted here, for a.out processes not.
34           
35    Only works on nix systems.
36    '''
37    import string
38    p=os.popen('ps uwp %s'%os.getpid()) 
39    lines=p.readlines()
40    #print "lines", lines
41    status=p.close() 
42    if status or len(lines)!=2 or sys.platform == 'win32': 
43        return None 
44    return int(string.split(lines[1])[4]) 
45
46
47
48
49class BenchmarkLeastSquares:
50
51
52    def __init__(self):
53        pass
54
55    def trial(self,
56              num_of_points=20000,
57              maxArea=1000,
58              max_points_per_cell=4,
59              is_fit=True,
60              use_least_squares=True,
61              save=False):
62        '''
63        num_of_points
64        '''
65       
66        #print "num_of_points",num_of_points
67        #print "maxArea",maxArea
68        #print "max_points_per_cell", max_points_per_cell
69
70        mesh_dict = self._build_regular_mesh_dict(maxArea=maxArea,
71                                                  save=save)
72        points_dict = self._build_points_dict(num_of_points=num_of_points)
73           
74        #Initial time and memory
75        t0 = time.time()
76        #m0 = None on windows
77        m0 = mem_usage()
78
79        if use_least_squares is True:
80            interp = Interpolation(mesh_dict['vertices'],
81                                   mesh_dict['triangles'],
82                                   points_dict['points'],
83                                   expand_search=True,
84                                   verbose = False,
85                                   max_points_per_cell = max_points_per_cell) 
86            if is_fit is True:
87                calc = interp.fit_points(points_dict['point_attributes'])
88               
89            else:
90                # run an interploate problem.
91                print "Interpolate!"
92                calc = interp.interpolate(mesh_dict['vertex_attributes'])
93        else:
94            # need to change to fit_interpolate code
95            interp = Interpolate(mesh_dict['vertices'],
96                                 mesh_dict['triangles'], 
97                                 max_vertices_per_cell = max_points_per_cell) 
98            if is_fit is True:
99                #calc = interp.fit_points(point_atts)
100                pass
101            else:
102                # run an interploate problem.
103                print "Interpolate!"
104                calc = interp.interpolate(mesh_dict['vertex_attributes']
105                                          ,points_dict['points']
106                                          ,start_blocking_len = 500000)
107           
108        time_taken_sec = (time.time()-t0)
109        m1 = mem_usage()
110        if m0 is None or m1 is None:
111            memory_used = None
112        else:
113            memory_used = (m1 - m0)
114        #print 'That took %.2f seconds' %time_taken_sec
115        return time_taken_sec, memory_used, len(mesh_dict['triangles'])
116
117    def _build_regular_mesh_dict(self,
118                                 maxArea=1000,
119                                 save=False):
120      # make a mesh
121        # pretty regular size, with some segments thrown in.
122        m = Mesh()
123        m.addUserVertex(0,0)
124        m.addUserVertex(1.0,0)
125        m.addUserVertex(0,1.0)
126        m.addUserVertex(1.0,1.0)
127       
128        m.autoSegment(alpha = 100 )
129       
130        dict = {}
131        dict['points'] = [[.10,.10],[.90,.20]]
132        dict['segments'] = [[0,1]] 
133        dict['segment_tags'] = ['wall1']   
134        m.addVertsSegs(dict)
135   
136        dict = {}
137        dict['points'] = [[.10,.90],[.40,.20]]
138        dict['segments'] = [[0,1]] 
139        dict['segment_tags'] = ['wall2']   
140        m.addVertsSegs(dict)
141       
142        dict = {}
143        dict['points'] = [[.20,.90],[.60,.60]]
144        dict['segments'] = [[0,1]] 
145        dict['segment_tags'] = ['wall3'] 
146        m.addVertsSegs(dict)
147       
148        dict = {}
149        dict['points'] = [[.60,.20],[.90,.90]]
150        dict['segments'] = [[0,1]] 
151        dict['segment_tags'] = ['wall4']   
152        m.addVertsSegs(dict)
153
154        m.generateMesh(mode = "Q", maxArea = maxArea)       
155        if save is True:
156            m.export_mesh_file("aaaa.tsh")
157        mesh_dict =  m.Mesh2IOTriangulationDict()
158
159        #Add vert attribute info to the mesh
160        mesh_dict['vertex_attributes'] = []
161        # There has to be a better way of doing this..
162        for vertex in mesh_dict['vertices']:
163            mesh_dict['vertex_attributes'].append([10.0])
164
165        return mesh_dict
166
167    def _build_points_dict(self, num_of_points=20000):
168       
169        points_dict = {}
170        points = []
171        point_atts = []
172
173        for point in range(num_of_points):
174            points.append([random(), random()])
175            point_atts.append(10.0)
176
177        points_dict['points'] = points
178        points_dict['point_attributes'] = point_atts
179        return points_dict
180
181
182#-------------------------------------------------------------
183if __name__ == "__main__":
184        b = BenchmarkLeastSquares()
185        b._build_regular_mesh_dict()
186        b._build_points_dict()
Note: See TracBrowser for help on using the repository browser.