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

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

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

revision 3891 by jfenwick, Wed Jan 18 02:30:48 2012 UTC revision 3892 by jfenwick, Tue Apr 10 08:57:23 2012 UTC
# Line 29  from esys.escript.util import EPSILON Line 29  from esys.escript.util import EPSILON
29  class DataAccessTestCase(unittest.TestCase):  class DataAccessTestCase(unittest.TestCase):
30      #This is a very basic test - it only contains one value.      #This is a very basic test - it only contains one value.
31      def testtoListOfTuplesScalarOnNullDomain(self):      def testtoListOfTuplesScalarOnNullDomain(self):
32      inp=42.0          inp=42.0
33      d=Data(inp)          d=Data(inp)
34      t=d.toListOfTuples(scalarastuple=True)[0]          t=d.toListOfTuples(scalarastuple=True)[0]
35      self.assertTrue(type(t)==type((1.0,)), "Did not return tuple for scalar data")          self.assertTrue(type(t)==type((1.0,)), "Did not return tuple for scalar data")
36      self.assertTrue(abs(inp-t[0])<EPSILON, "Did not return correct value")          self.assertTrue(abs(inp-t[0])<EPSILON, "Did not return correct value")
37      t=d.toListOfTuples(scalarastuple=False)[0]          t=d.toListOfTuples(scalarastuple=False)[0]
38      self.assertTrue(type(t)==float, "Did not return non-tuple when asked")          self.assertTrue(type(t)==float, "Did not return non-tuple when asked")
39      self.assertTrue(abs(inp-t)<EPSILON, "Did not return correct (non-tuple)")          self.assertTrue(abs(inp-t)<EPSILON, "Did not return correct (non-tuple)")
40    
41      #Test for one point per sample      #Test for one point per sample
42      def testtoListOfTuples_SinglePPS(self):      def testtoListOfTuples_SinglePPS(self):
43      fs=getTestDomainFunctionSpace(1,5)          fs=getTestDomainFunctionSpace(1,5)
44      inp=42.0          inp=42.0
45      d=Data(inp,fs,True)          d=Data(inp,fs,True)
46      for x in range(5):          for x in range(5):
47          d.setValueOfDataPoint(x,43+x)              d.setValueOfDataPoint(x,43+x)
48      t=d.toListOfTuples(scalarastuple=True)          t=d.toListOfTuples(scalarastuple=True)
49      self.assertTrue(len(t)==5,"Returned list has the wrong length")          self.assertTrue(len(t)==5,"Returned list has the wrong length")
50      total=0          total=0
51      for x in range(5):          for x in range(5):
52          total+=t[x][0]-43-x              total+=t[x][0]-43-x
53      self.assertTrue(abs(total)<EPSILON,"Returned list has wrong elements")          self.assertTrue(abs(total)<EPSILON,"Returned list has wrong elements")
54      inp=[[1,2],[3,4]]          inp=[[1,2],[3,4]]
55      d=Data(inp,fs,True)          d=Data(inp,fs,True)
56      for x in range(5):          for x in range(5):
57          d.setValueOfDataPoint(x,((x,x+1),(x+2,x+3)))              d.setValueOfDataPoint(x,((x,x+1),(x+2,x+3)))
58      t=d.toListOfTuples(scalarastuple=True)          t=d.toListOfTuples(scalarastuple=True)
59      ok=True          ok=True
60      for x in range(5):          for x in range(5):
61          if t[x]!=((x,x+1),(x+2,x+3)): ok=False              if t[x]!=((x,x+1),(x+2,x+3)): ok=False
62      self.assertTrue(ok, "Returned matrix does not match")          self.assertTrue(ok, "Returned matrix does not match")
63                
64      def testtoListOfTuples_MultiPPS(self):      def testtoListOfTuples_MultiPPS(self):
65      fs=getTestDomainFunctionSpace(3,5)          fs=getTestDomainFunctionSpace(3,5)
66      inp=0          inp=0
67      d=Data(inp,fs,True)          d=Data(inp,fs,True)
68      for x in range(15):          for x in range(15):
69          d.setValueOfDataPoint(x,x)              d.setValueOfDataPoint(x,x)
70      ok=True          ok=True
71      t=d.toListOfTuples(scalarastuple=True)          t=d.toListOfTuples(scalarastuple=True)
72      for x in range(15):          for x in range(15):
73          if t[x]!=(x,): ok=False              if t[x]!=(x,): ok=False
74      self.assertTrue(ok,"Returned scalar does not match")          self.assertTrue(ok,"Returned scalar does not match")
75      inp=(0,0)          inp=(0,0)
76      d=Data(inp,fs,True)          d=Data(inp,fs,True)
77      for x in range(15):          for x in range(15):
78          d.setValueOfDataPoint(x,(2*(x/2),(2*(x/2)+1)))              d.setValueOfDataPoint(x,(2*(x/2),(2*(x/2)+1)))
79      t=d.toListOfTuples(scalarastuple=True)          t=d.toListOfTuples(scalarastuple=True)
80      ok=True          ok=True
81      for x in range(15):          for x in range(15):
82          if t[x]!=(2*(x/2),2*(x/2)+1): ok=False              if t[x]!=(2*(x/2),2*(x/2)+1): ok=False
83      self.assertTrue(ok,"Returned vector does not match")          self.assertTrue(ok,"Returned vector does not match")
84      # Now we try Matricies          # Now we try Matricies
85      inp=((0,0),(0,0))          inp=((0,0),(0,0))
86      d=Data(inp,fs,True)          d=Data(inp,fs,True)
87      for x in range(15):          for x in range(15):
88          d.setValueOfDataPoint(x,((x,x+1),(x+2,x+3)))              d.setValueOfDataPoint(x,((x,x+1),(x+2,x+3)))
89      t=d.toListOfTuples(scalarastuple=True)          t=d.toListOfTuples(scalarastuple=True)
90      ok=True          ok=True
91      for x in range(15):          for x in range(15):
92          if t[x]!=((x,x+1),(x+2,x+3)): ok=False              if t[x]!=((x,x+1),(x+2,x+3)): ok=False
93      self.assertTrue(ok,"Returned matrix does not match")          self.assertTrue(ok,"Returned matrix does not match")
94      #Now 3-Tensors          #Now 3-Tensors
95      inp=(((0,0),(0,0)),((0,0),(0,0)))          inp=(((0,0),(0,0)),((0,0),(0,0)))
96      d=Data(inp,fs,True)          d=Data(inp,fs,True)
97      for x in range(15):          for x in range(15):
98          d.setValueOfDataPoint(x,(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))))              d.setValueOfDataPoint(x,(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))))
99      t=d.toListOfTuples(scalarastuple=True)          t=d.toListOfTuples(scalarastuple=True)
100      ok=True          ok=True
101      for x in range(15):          for x in range(15):
102          if t[x]!=(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))): ok=False              if t[x]!=(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))): ok=False
103      self.assertTrue(ok,"Returned 3-Tensor does not match")          self.assertTrue(ok,"Returned 3-Tensor does not match")
104      #Now 4-Tensors          #Now 4-Tensors
105      inp=((((0,0),(0,0)),((0,0),(0,0))),(((0,0),(0,0)),((0,0),(0,0))))          inp=((((0,0),(0,0)),((0,0),(0,0))),(((0,0),(0,0)),((0,0),(0,0))))
106      d=Data(inp,fs,True)          d=Data(inp,fs,True)
107      for x in range(15):          for x in range(15):
108          d.setValueOfDataPoint(x,((((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))),(((9+x,9+x+1),(9+x+2,9+x+3)),((9+x+4,9+x+5),(9+x+6,9+x+7)))))              d.setValueOfDataPoint(x,((((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))),(((9+x,9+x+1),(9+x+2,9+x+3)),((9+x+4,9+x+5),(9+x+6,9+x+7)))))
109      t=d.toListOfTuples()          t=d.toListOfTuples()
110      ok=True          ok=True
111      for x in range(15):          for x in range(15):
112          if t[x]!=((((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))),(((9+x,9+x+1),(9+x+2,9+x+3)),((9+x+4,9+x+5),(9+x+6,9+x+7)))): ok=False              if t[x]!=((((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))),(((9+x,9+x+1),(9+x+2,9+x+3)),((9+x+4,9+x+5),(9+x+6,9+x+7)))): ok=False
113      self.assertTrue(ok,"Returned 4-Tensor does not match")            self.assertTrue(ok,"Returned 4-Tensor does not match")  
114                
115      # This test sets values then gets them.      # This test sets values then gets them.
116      # Strictly speaking, it is not a complete test because it cannot tell if functions are broken      # Strictly speaking, it is not a complete test because it cannot tell if functions are broken
117      # in a symmetric manner      # in a symmetric manner    
118      def testToFromTupleTogether(self):      def testToFromTupleTogether(self):
119      fs=getTestDomainFunctionSpace(3,5)          fs=getTestDomainFunctionSpace(3,5)
120      inp=0          inp=0
121      d=Data(inp,fs,True)          d=Data(inp,fs,True)
122      for x in range(15):          for x in range(15):
123          d.setValueOfDataPoint(x,x)              d.setValueOfDataPoint(x,x)
124      ok=True          ok=True
125      for x in range(15):          for x in range(15):
126          if d.getTupleForDataPoint(x)!=(x,): ok=False              if d.getTupleForDataPoint(x)!=(x,): ok=False
127      self.assertTrue(ok,"Returned scalar does not match")          self.assertTrue(ok,"Returned scalar does not match")
128      inp=(0,0)          inp=(0,0)
129      d=Data(inp,fs,True)          d=Data(inp,fs,True)
130      for x in range(15):          for x in range(15):
131          d.setValueOfDataPoint(x,(2*(x/2),(2*(x/2)+1)))              d.setValueOfDataPoint(x,(2*(x/2),(2*(x/2)+1)))
132      ok=True          ok=True
133      for x in range(15):          for x in range(15):
134          if d.getTupleForDataPoint(x)!=(2*(x/2),2*(x/2)+1): ok=False              if d.getTupleForDataPoint(x)!=(2*(x/2),2*(x/2)+1): ok=False
135      self.assertTrue(ok,"Returned vector does not match")          self.assertTrue(ok,"Returned vector does not match")
136      # Now we try Matricies          # Now we try Matricies
137      inp=((0,0),(0,0))          inp=((0,0),(0,0))
138      d=Data(inp,fs,True)          d=Data(inp,fs,True)
139      for x in range(15):          for x in range(15):
140          d.setValueOfDataPoint(x,((x,x+1),(x+2,x+3)))              d.setValueOfDataPoint(x,((x,x+1),(x+2,x+3)))
141      ok=True          ok=True
142      for x in range(15):          for x in range(15):
143          if d.getTupleForDataPoint(x)!=((x,x+1),(x+2,x+3)): ok=False              if d.getTupleForDataPoint(x)!=((x,x+1),(x+2,x+3)): ok=False
144      self.assertTrue(ok,"Returned matrix does not match")          self.assertTrue(ok,"Returned matrix does not match")
145      #Now 3-Tensors          #Now 3-Tensors
146      inp=(((0,0),(0,0)),((0,0),(0,0)))          inp=(((0,0),(0,0)),((0,0),(0,0)))
147      d=Data(inp,fs,True)          d=Data(inp,fs,True)
148      for x in range(15):          for x in range(15):
149          d.setValueOfDataPoint(x,(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))))              d.setValueOfDataPoint(x,(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))))
150      ok=True          ok=True
151      for x in range(15):          for x in range(15):
152          if d.getTupleForDataPoint(x)!=(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))): ok=False              if d.getTupleForDataPoint(x)!=(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))): ok=False
153      self.assertTrue(ok,"Returned 3-Tensor does not match")          self.assertTrue(ok,"Returned 3-Tensor does not match")
154      #Now 4-Tensors          #Now 4-Tensors
155      inp=((((0,0),(0,0)),((0,0),(0,0))),(((0,0),(0,0)),((0,0),(0,0))))          inp=((((0,0),(0,0)),((0,0),(0,0))),(((0,0),(0,0)),((0,0),(0,0))))
156      d=Data(inp,fs,True)          d=Data(inp,fs,True)
157      for x in range(15):          for x in range(15):
158          d.setValueOfDataPoint(x,((((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))),(((9+x,9+x+1),(9+x+2,9+x+3)),((9+x+4,9+x+5),(9+x+6,9+x+7)))))              d.setValueOfDataPoint(x,((((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))),(((9+x,9+x+1),(9+x+2,9+x+3)),((9+x+4,9+x+5),(9+x+6,9+x+7)))))
159      ok=True          ok=True
160      for x in range(15):          for x in range(15):
161          if d.getTupleForDataPoint(x)!=((((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))),(((9+x,9+x+1),(9+x+2,9+x+3)),((9+x+4,9+x+5),(9+x+6,9+x+7)))): ok=False              if d.getTupleForDataPoint(x)!=((((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))),(((9+x,9+x+1),(9+x+2,9+x+3)),((9+x+4,9+x+5),(9+x+6,9+x+7)))): ok=False
162      self.assertTrue(ok,"Returned 4-Tensor does not match")                self.assertTrue(ok,"Returned 4-Tensor does not match")          
163                
164                
165  if __name__ == "__main__":  if __name__ == "__main__":
166      if getMPISizeWorld() == 1:      if getMPISizeWorld() == 1:
167         suite = unittest.TestSuite()         suite = unittest.TestSuite()

Legend:
Removed from v.3891  
changed lines
  Added in v.3892

  ViewVC Help
Powered by ViewVC 1.1.26