source: inundation/fit_interpolate/benchmark_least_squares.py @ 1982

Last change on this file since 1982 was 1974, checked in by duncan, 19 years ago

adding memory stuff

File size: 6.6 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   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
14   Geoscience Australia, 2004.
15"""
16
17
18import unittest
19from math import sqrt
20import time
21
22
23from pyvolution.least_squares import Interpolation
24from Numeric import allclose, array, transpose
25import os
26import sys
27
28def mem_usage():
29    '''
30    returns the rss.
31
32  RSS  The total amount of physical memory used by  the  task,  in  kilo-
33            bytes,  is  shown  here.  For ELF processes used library pages are
34            counted here, for a.out processes not.
35           
36    Only works on nix systems.
37    '''
38    import string
39    p=os.popen('ps uwp %s'%os.getpid()) 
40    lines=p.readlines()
41    #print "lines", lines
42    status=p.close() 
43    if status or len(lines)!=2 or sys.platform == 'win32': 
44        return None 
45    return int(string.split(lines[1])[4]) 
46
47
48
49
50class Test_Least_Squares(unittest.TestCase):
51
52    def setUp(self):
53        pass
54
55    def tearDown(self):
56        pass
57
58    def ztest_expand_search2(self):
59        from random import seed, random
60        from pmesh.mesh import Mesh
61       
62        seed(2)
63        m = Mesh()
64        verts_per_sector = 100 #100
65        num_of_points = 300
66        sec_side = 9.0
67
68        vert_atts = []
69        for vert in range(verts_per_sector):
70            m.addUserVertex(random()*sec_side, random()*sec_side)
71            vert_atts.append(random())
72        for vert in range(verts_per_sector):
73            m.addUserVertex(90.0+random()*sec_side, 90.0+random()*sec_side)
74            vert_atts.append(random())
75        for vert in range(verts_per_sector):
76            m.addUserVertex(random()*sec_side, 90.0+random()*sec_side)
77            vert_atts.append(random())
78        for vert in range(verts_per_sector):
79            m.addUserVertex(90.0+random()*sec_side, random()*sec_side)
80            vert_atts.append(random())
81        #for vert in range(verts_per_sector):
82        #    m.addUserVertex(40.0+random()*sec_side, 40.0+random()*sec_side)
83        #    vert_atts.append(random())
84           
85        m.autoSegment(alpha = 100 )
86        m.generateMesh(mode = "Q")
87        m.export_mesh_file("aaaa.tsh")
88        mesh_dict =  m.Mesh2IOTriangulationDict()
89
90        points = []
91        point_atts = []
92        for point in range(num_of_points):
93            points.append([random()*100, random()*100])
94            point_atts.append(10.0)
95        m.autoSegment(alpha = 100 )
96
97        #print 'points', points
98        t0 = time.time()
99
100        interp = Interpolation(mesh_dict['vertices'],
101                               mesh_dict['triangles'], points,
102                               alpha=0.2, expand_search=True,
103                               verbose = False,
104                               max_points_per_cell = 4)
105        calc = interp.fit_points(point_atts )
106        #print "interp.expanded_quad_searches", interp.expanded_quad_searches
107        #print "calc", calc
108        print 'That took %.2f seconds' %(time.time()-t0)
109
110       
111    def test_expand_search3(self):
112        from random import seed, random
113        from pmesh.mesh import Mesh
114
115        # takes 7.73 seconds
116        #num_of_points = 20000
117        #maxArea = 1000
118        #max_points_per_cell = 4
119
120        # took 9.84 secs
121        #num_of_points = 20000
122        #maxArea = 100
123        #max_points_per_cell = 4
124
125        # 16.61 secs
126        num_of_points = 20000
127        maxArea = 10 #836 basis functions
128        #max_points_per_cell = 4 #16.61 sec
129       
130        #max_points_per_cell = 30 #31.17 sec
131        #max_points_per_cell = 2 #16.125 sec
132        max_points_per_cell = 8 #17.66 sec
133
134       
135        # 16.61 secs
136        num_of_points = 20000
137        maxArea = 1 #7917 basis functions
138        max_points_per_cell = 4 # 76.047 sec
139       
140        #max_points_per_cell = 30 #
141        #max_points_per_cell = 2 #78.97 sec
142        #max_points_per_cell = 16 # 82.33 sec
143        #max_points_per_cell = 8 #77.984 sec
144       
145        # 8.44 secs
146        #num_of_points = 20000
147        #maxArea = 1
148       
149       
150        seed(2)
151        m = Mesh()
152        m.addUserVertex(0,0)
153        m.addUserVertex(100,0)
154        m.addUserVertex(0,100)
155        m.addUserVertex(100,100)
156       
157        m.autoSegment(alpha = 100 )
158       
159        dict = {}
160        dict['points'] = [[10,10],[90,20]]
161        dict['segments'] = [[0,1]] 
162        dict['segment_tags'] = ['wall1']   
163        m.addVertsSegs(dict)
164   
165        dict = {}
166        dict['points'] = [[10,90],[40,20]]
167        dict['segments'] = [[0,1]] 
168        dict['segment_tags'] = ['wall2']   
169        m.addVertsSegs(dict)
170       
171        dict = {}
172        dict['points'] = [[20,90],[60,60]]
173        dict['segments'] = [[0,1]] 
174        dict['segment_tags'] = ['wall3'] 
175        m.addVertsSegs(dict)
176       
177        dict = {}
178        dict['points'] = [[60,20],[90,90]]
179        dict['segments'] = [[0,1]] 
180        dict['segment_tags'] = ['wall4']   
181        m.addVertsSegs(dict)
182       
183        m.addVertsSegs(dict)
184        m.generateMesh(mode = "Q", maxArea = maxArea)
185        m.export_mesh_file("aaaa.tsh")
186        mesh_dict =  m.Mesh2IOTriangulationDict()
187
188        points = []
189        point_atts = []
190        for point in range(num_of_points):
191            points.append([random()*100, random()*100])
192            point_atts.append(10.0)
193        #m.autoSegment(alpha = 100 )
194
195        #print 'points', points
196        t0 = time.time()
197        interp = Interpolation(mesh_dict['vertices'],
198                               mesh_dict['triangles'], points,
199                               alpha=0.2, expand_search=True,
200                               verbose = True, #False,
201                               max_points_per_cell = max_points_per_cell)
202        calc = interp.fit_points(point_atts )
203        print "interp.expanded_quad_searches", interp.expanded_quad_searches
204        #print "calc", calc
205        print 'That took %.2f seconds' %(time.time()-t0)
206       
207#-------------------------------------------------------------
208if __name__ == "__main__":
209    import os
210    suite = unittest.makeSuite(Test_Least_Squares,'test')
211    runner = unittest.TextTestRunner(verbosity=1)
212    runner.run(suite)
213    val = mem_usage()
214    print 'RSS', val
Note: See TracBrowser for help on using the repository browser.