/[escript]/trunk/escript/test/python/test_objects.py
ViewVC logotype

Diff of /trunk/escript/test/python/test_objects.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3992 by jfenwick, Fri Sep 21 02:47:54 2012 UTC revision 3993 by caltinay, Wed Sep 26 07:00:55 2012 UTC
# Line 21  http://www.opensource.org/licenses/osl-3 Line 21  http://www.opensource.org/licenses/osl-3
21  __url__="https://launchpad.net/escript-finley"  __url__="https://launchpad.net/escript-finley"
22    
23  """  """
24  Test suite for data objects. at the moment for dump and load only.  Test suite for data objects.
25    
26  The tests must be linked with some function space class object in the setUp method:  The tests must be linked with some function space class object in the setUp
27  to run the use:  method to run:
28    
29     from esys.dudley import Brick     from esys.dudley import Brick
30     class Test_DumpOnDudley(Test_Dump):     class Test_DumpOnDudley(Test_Dump):
31         def setUp(self):         def setUp(self):
32            self.domain =Rectangle(NE,NE+1,2)            self.domain=Rectangle(NE,NE+1,2)
33            self.domain_with_different_number_of_samples =Rectangle(2*NE,NE+1,2)            self.domain_with_different_number_of_samples=Rectangle(2*NE,NE+1,2)
34            self.domain_with_different_number_of_data_points_per_sample =Rectangle(2*NE,NE+1,2,integrationOrder=2)            self.domain_with_different_number_of_data_points_per_sample=Rectangle(2*NE,NE+1,2,integrationOrder=2)
35            self.domain_with_different_sample_ordering =Rectangle(1,(NE+1)*NE,2)            self.domain_with_different_sample_ordering=Rectangle(1,(NE+1)*NE,2)
36            self.filename_base="."            self.filename_base="."
37    
38     suite = unittest.TestSuite()     suite = unittest.TestSuite()
# Line 60  except KeyError: Line 60  except KeyError:
60       ESCRIPT_WORKDIR='.'       ESCRIPT_WORKDIR='.'
61    
62  class Test_TableInterpolation(unittest.TestCase):  class Test_TableInterpolation(unittest.TestCase):
63      RES_TOL=1.e-7 # RES_TOLerance to compare results          RES_TOL=1.e-7 # RES_TOLerance to compare results
64            
           
65      def test_NullFunctionSpace(self):      def test_NullFunctionSpace(self):
66          arL=[[0, -1, -2, -3, -4], [1, 1, -2, -3, -4], [2, 2, 2, -3, -4], [3, 3, 3, 3, -4], [4, 4, 4, 4, 4]]          arL=[[0, -1, -2, -3, -4], [1, 1, -2, -3, -4], [2, 2, 2, -3, -4], [3, 3, 3, 3, -4], [4, 4, 4, 4, 4]]
67          arn=numpy.array(arL)          arn=numpy.array(arL)
# Line 90  class Test_TableInterpolation(unittest.T Line 89  class Test_TableInterpolation(unittest.T
89              d4.interpolateTable( arL,0, 0.5, d1, 0, 1, 100, check_boundaries=False  )              d4.interpolateTable( arL,0, 0.5, d1, 0, 1, 100, check_boundaries=False  )
90              dm05.interpolateTable( arL,0,1, d1 , 0,1, 100, check_boundaries=False  )              dm05.interpolateTable( arL,0,1, d1 , 0,1, 100, check_boundaries=False  )
91              d1.interpolateTable( arL,0,1, dm05 , 0,1, 100,check_boundaries=False  )              d1.interpolateTable( arL,0,1, dm05 , 0,1, 100,check_boundaries=False  )
92                  
93                 # interpolated value too large                 # interpolated value too large
94              self.assertRaises(RuntimeError, d2.interpolateTable, arL, 0, 1, d2, 0, 1, 1 )              self.assertRaises(RuntimeError, d2.interpolateTable, arL, 0, 1, d2, 0, 1, 1 )
95    
# Line 112  class Test_TableInterpolation(unittest.T Line 111  class Test_TableInterpolation(unittest.T
111                  zmin=inf(z)                  zmin=inf(z)
112                  xwidth=(xmax-xmin)/(self.xn-1)                  xwidth=(xmax-xmin)/(self.xn-1)
113                  ywidth=(ymax-ymin)/(self.yn-1)                  ywidth=(ymax-ymin)/(self.yn-1)
114                  zwidth=(zmax-zmin)/(self.zn-1)                            zwidth=(zmax-zmin)/(self.zn-1)
115                  table=[]                  table=[]
116                  for k in range(self.zn):                  for k in range(self.zn):
117                      face=[]                      face=[]
# Line 154  class Test_TableInterpolation(unittest.T Line 153  class Test_TableInterpolation(unittest.T
153                        for i in range(self.xn):                        for i in range(self.xn):
154                          row.append(v0+v1*xwidth*i+v2*ywidth*j+v3*i*j*xwidth*ywidth)                          row.append(v0+v1*xwidth*i+v2*ywidth*j+v3*i*j*xwidth*ywidth)
155                        table.append(row)                        table.append(row)
156                  ref=v0+v1*(x-xmin)+v2*(y-ymin)+v3*(x-xmin)*(y-ymin)                  ref=v0+v1*(x-xmin)+v2*(y-ymin)+v3*(x-xmin)*(y-ymin)
157                  lsupref=Lsup(ref)                  lsupref=Lsup(ref)
158                  if lsupref!=0 and xwidth>0:             #This will happen in cases where there are no samples on this rank                  if lsupref!=0 and xwidth>0:             #This will happen in cases where there are no samples on this rank
159                      res=x.interpolateTable(table,xmin,xwidth,y, ymin, ywidth,500)                      res=x.interpolateTable(table,xmin,xwidth,y, ymin, ywidth,500)
# Line 162  class Test_TableInterpolation(unittest.T Line 161  class Test_TableInterpolation(unittest.T
161                      #Now we try for the new interface                      #Now we try for the new interface
162                      res=interpolateTable(table,points[0:2], (xmin, ymin), (xwidth, ywidth),500)                      res=interpolateTable(table,points[0:2], (xmin, ymin), (xwidth, ywidth),500)
163                      self.assertTrue(Lsup(res-ref)/Lsup(ref)<self.RES_TOL,"Failed for %s under unified call."%str(fs))                      self.assertTrue(Lsup(res-ref)/Lsup(ref)<self.RES_TOL,"Failed for %s under unified call."%str(fs))
164                        
165                
166    
167      def test_FunctionSpace1D(self):      def test_FunctionSpace1D(self):
168          vs=[(1,3), (-1,1), (0.5, 17)]     #There is no particular significance to these numbers          vs=[(1,3), (-1,1), (0.5, 17)]     #There is no particular significance to these numbers
# Line 191  class Test_TableInterpolation(unittest.T Line 190  class Test_TableInterpolation(unittest.T
190    
191  class Test_saveCSV(unittest.TestCase):  class Test_saveCSV(unittest.TestCase):
192    
193     def test_save1(self):     def test_csv_header_separator_and_append(self):
194          X=self.domain.getX()          X=self.domain.getX()
195          X0=X[0]          X0=X[0]
196          fname=os.path.join(ESCRIPT_WORKDIR, "test_save1.csv")          fname=os.path.join(ESCRIPT_WORKDIR, "test_save1.csv")
197          saveDataCSV(fname,C=X, D=X0)          saveDataCSV(fname, C=X, D=X0)
198            self.assertTrue(os.path.exists(fname), "test file not created")
199          saveDataCSV(fname,append=True, J=X0, H=X)          saveDataCSV(fname,append=True, J=X0, H=X)
200          self.assertTrue(os.path.exists(fname), "test file not created")          self.assertTrue(os.path.exists(fname), "test file not created")
201          f=open(fname,'r')          line=open(fname, 'r').readline()
202          line=f.readline()          # Test both separator strings for vector, scalar
203          self.assertEquals(line,"C_0, C_1, D\n")    #This tests both separator strings          self.assertEquals(line, "C_0, C_1, D\n")
204          #Now we find out how many rows it has  
205          linecount=0          # Test Tensor header
         while line != '':  
                 linecount+=1  
                 line=f.readline()  
         self.assertEquals(linecount,self.linecount1*2)  
         f.close()  
         #Now to other output  
206          T2=Tensor(7,X.getFunctionSpace())          T2=Tensor(7,X.getFunctionSpace())
207          T3=Tensor3(8,X.getFunctionSpace())          T3=Tensor3(8,X.getFunctionSpace())
208          T4=Tensor4(9,X.getFunctionSpace())          T4=Tensor4(9,X.getFunctionSpace())
209          saveDataCSV(fname,A=T2,B=T3,C=T4)          saveDataCSV(fname,A=T2,B=T3,C=T4)
210          f=open(fname,'r')          line=open(fname,'r').readline()
         line=f.readline()  
211          self.assertEquals(line, 'A_0_0, A_1_0, A_0_1, A_1_1, B_0_0_0, B_0_0_1, B_1_0_0, B_1_0_1, B_0_1_0, B_0_1_1, B_1_1_0, B_1_1_1, C_0_0_0_0, C_0_0_0_1, C_0_0_1_0, C_0_0_1_1, C_1_0_0_0, C_1_0_0_1, C_1_0_1_0, C_1_0_1_1, C_0_1_0_0, C_0_1_0_1, C_0_1_1_0, C_0_1_1_1, C_1_1_0_0, C_1_1_0_1, C_1_1_1_0, C_1_1_1_1\n')          self.assertEquals(line, 'A_0_0, A_1_0, A_0_1, A_1_1, B_0_0_0, B_0_0_1, B_1_0_0, B_1_0_1, B_0_1_0, B_0_1_1, B_1_1_0, B_1_1_1, C_0_0_0_0, C_0_0_0_1, C_0_0_1_0, C_0_0_1_1, C_1_0_0_0, C_1_0_0_1, C_1_0_1_0, C_1_0_1_1, C_0_1_0_0, C_0_1_0_1, C_0_1_1_0, C_0_1_1_1, C_1_1_0_0, C_1_1_0_1, C_1_1_1_0, C_1_1_1_1\n')
212          line=f.readline()  
213          line_expected=[7.]*4+[8.]*8+[9.]*16          # test different separator
214          line_got=[float(elt) for elt in line.split(',')]          saveDataCSV(fname, sep="|",csep="/", U=X, V=X0)
215          self.assertEquals(line_got,line_expected)          line=open(fname,'r').readline()
216          linecount=1          self.assertEquals(line, 'U/0|U/1|V\n')
217          while line != '':  
218       def test_saveCSV_functionspaces(self):
219            for i in xrange(len(self.functionspaces)):
220                FS=self.functionspaces[i]
221                X=FS(self.domain).getX()
222                X0=X[0]
223                fname=os.path.join(ESCRIPT_WORKDIR, "test_save2.csv")
224                saveDataCSV(fname,C=X, D=X0)
225                f=open(fname,'r')
226                # test number of rows written
227                linecount=0
228                line=f.readline() # skip header
229                while line != '':
230                  linecount+=1                  linecount+=1
231                  line=f.readline()                  line=f.readline()
232          self.assertEquals(linecount, self.linecount1)              self.assertEquals(linecount, self.linecounts[i])
233          f.close()                    f.close()
234          #Now to test separators and mask  
235          saveDataCSV(fname, sep="|",csep="/", U=X, V=X0, mask=X0)              # Now check data content
236          f=open(fname,'r')              T2=Tensor(7, X.getFunctionSpace())
237          line=f.readline()              T3=Tensor3(8, X.getFunctionSpace())
238          self.assertEquals(line, 'U/0|U/1|V\n')              T4=Tensor4(9, X.getFunctionSpace())
239          line=f.readline()              expected=[7.]*4+[8.]*8+[9.]*16
240          line_got=[float(elt) for elt in line.split('|')]              saveDataCSV(fname, A=T2, B=T3, C=T4)
241          self.assertEquals(len(self.line_expected),len(line_got))              f=open(fname, 'r')
242          for i in range(len(self.line_expected)):              f.readline() # skip header
243                  self.assertAlmostEquals(self.line_expected[i],line_got[i])              line=f.readline()
244          linecount=1              linecount=1
245          while line!='':              while line != '':
246                    line_got=[float(elt) for elt in line.split(',')]
247                    self.assertEquals(line_got, expected)
248                    linecount+=1
249                    line=f.readline()
250                self.assertEquals(linecount, self.linecounts[i])
251                f.close()
252    
253                # As above but with mask
254                saveDataCSV(fname, U=X, V=X0, mask=X0)
255                f=open(fname, 'r')
256                f.readline() # skip header
257                line=f.readline()
258                line_got=[float(elt) for elt in line.split(',')]
259                self.assertEquals(len(self.firstline[i]),len(line_got))
260                for j in xrange(len(self.firstline[i])):
261                    self.assertAlmostEquals(self.firstline[i][j],line_got[j])
262                linecount=1
263                while line!='':
264                  linecount+=1                  linecount+=1
265                  line=f.readline()                  line=f.readline()
266          self.assertEquals(linecount,self.linecount2)              self.assertEquals(linecount, self.linecounts_masked[i])
267                        f.close()
268            
269    
270  class Test_Domain(unittest.TestCase):  class Test_Domain(unittest.TestCase):
271    
272     def test_getListOfTags(self): # requires self.boundary_tag_list     def test_getListOfTags(self): # requires self.boundary_tag_list
# Line 461  class Test_GlobalMinMax(unittest.TestCas Line 485  class Test_GlobalMinMax(unittest.TestCas
485          self.assertTrue(n==0,"Incorrect position for min")          self.assertTrue(n==0,"Incorrect position for min")
486          if d.getNumberOfDataPoints()>0:          if d.getNumberOfDataPoints()>0:
487                  d.setValueOfDataPoint(0,myrank+0.001)                  d.setValueOfDataPoint(0,myrank+0.001)
488          p,n=d.maxGlobalDataPoint()                p,n=d.maxGlobalDataPoint()
489          self.assertTrue(p==maxproc,"Incorrect process indentified as holding min")          self.assertTrue(p==maxproc,"Incorrect process indentified as holding min")
490          self.assertTrue(n==0,"Incorrect position for min")          self.assertTrue(n==0,"Incorrect position for min")
491    
492            
493    
494  class Test_SetDataPointValue(unittest.TestCase):  class Test_SetDataPointValue(unittest.TestCase):
495     arg0=9.81     arg0=9.81
# Line 1254  class Test_Dump(unittest.TestCase): Line 1278  class Test_Dump(unittest.TestCase):
1278     #===========================================================================     #===========================================================================
1279     ## This functionspace does not currently support tags.     ## This functionspace does not currently support tags.
1280     ## Instead, we test that the canTag() function throws in test_canTag_Failures.     ## Instead, we test that the canTag() function throws in test_canTag_Failures.
1281      
1282     #def test_DumpAndLoad_Tagged_Solution_Rank0(self):     #def test_DumpAndLoad_Tagged_Solution_Rank0(self):
1283         #if loadIsConfigured():         #if loadIsConfigured():
1284            #filemame=os.path.join(self.filename_base,"tagged_solution_rank0.nc")            #filemame=os.path.join(self.filename_base,"tagged_solution_rank0.nc")
# Line 1302  class Test_Dump(unittest.TestCase): Line 1326  class Test_Dump(unittest.TestCase):
1326     ##===========================================================================     ##===========================================================================
1327     ## This functionspace does not currently support tags.     ## This functionspace does not currently support tags.
1328     ## Instead, we test that the canTag() function throws in test_canTag_Failures.     ## Instead, we test that the canTag() function throws in test_canTag_Failures.
1329      
1330     #def test_DumpAndLoad_Tagged_ReducedSolution_Rank0(self):     #def test_DumpAndLoad_Tagged_ReducedSolution_Rank0(self):
1331         #if loadIsConfigured():         #if loadIsConfigured():
1332            #filemame=os.path.join(self.filename_base,"tagged_reduced_solution_rank0.nc")            #filemame=os.path.join(self.filename_base,"tagged_reduced_solution_rank0.nc")
# Line 1591  class Test_Lazy(unittest.TestCase): Line 1615  class Test_Lazy(unittest.TestCase):
1615          t=delay(t)          t=delay(t)
1616          t=t*2          t=t*2
1617          return r1,r2,r3,f,t          return r1,r2,r3,f,t
1618      
1619    def test_GroupRes(self):    def test_GroupRes(self):
1620          rr1,rr2,rr3,rf,rt=self.makeLazyObj()          rr1,rr2,rr3,rf,rt=self.makeLazyObj()
1621          rr1=resolve(rr1)          rr1=resolve(rr1)
# Line 1610  class Test_Lazy(unittest.TestCase): Line 1634  class Test_Lazy(unittest.TestCase):
1634          r1,r2,r3,f,t=self.makeLazyObj()          r1,r2,r3,f,t=self.makeLazyObj()
1635          err=Lsup(rr1-r1)+Lsup(rr2-r2)+Lsup(rr3-r3)+Lsup(rt-t)+Lsup(rf-f)          err=Lsup(rr1-r1)+Lsup(rr2-r2)+Lsup(rr3-r3)+Lsup(rt-t)+Lsup(rf-f)
1636          self.assertTrue(err<0.001, "Same functionspace group resolve with mixed functionspaces")          self.assertTrue(err<0.001, "Same functionspace group resolve with mixed functionspaces")
1637            
1638            

Legend:
Removed from v.3992  
changed lines
  Added in v.3993

  ViewVC Help
Powered by ViewVC 1.1.26