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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3892 - (hide annotations)
Tue Apr 10 08:57:23 2012 UTC (7 years ago) by jfenwick
File MIME type: text/x-python
File size: 7117 byte(s)
Merged changes across from the attempt2 branch.
This version builds and passes python2 tests.
It also passes most python3 tests.



1 jfenwick 2485
2     ########################################################
3     #
4 jfenwick 2881 # Copyright (c) 2003-2010 by University of Queensland
5 jfenwick 2485 # Earth Systems Science Computational Center (ESSCC)
6     # http://www.uq.edu.au/esscc
7     #
8     # Primary Business: Queensland, Australia
9     # Licensed under the Open Software License version 3.0
10     # http://www.opensource.org/licenses/osl-3.0.php
11     #
12     ########################################################
13    
14 jfenwick 2881 __copyright__="""Copyright (c) 2003-2010 by University of Queensland
15 jfenwick 2485 Earth Systems Science Computational Center (ESSCC)
16     http://www.uq.edu.au/esscc
17     Primary Business: Queensland, Australia"""
18     __license__="""Licensed under the Open Software License version 3.0
19     http://www.opensource.org/licenses/osl-3.0.php"""
20     __url__="https://launchpad.net/escript-finley"
21    
22     import unittest
23     import sys
24     from esys.escript import *
25     from esys.escript.util import EPSILON
26    
27     # This test assumes that samples are in order in the object, ie the last point of sample x is
28     # immediately before the first point of sample x+1
29     class DataAccessTestCase(unittest.TestCase):
30     #This is a very basic test - it only contains one value.
31     def testtoListOfTuplesScalarOnNullDomain(self):
32 jfenwick 3892 inp=42.0
33     d=Data(inp)
34     t=d.toListOfTuples(scalarastuple=True)[0]
35     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")
37     t=d.toListOfTuples(scalarastuple=False)[0]
38     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)")
40 jfenwick 2485
41     #Test for one point per sample
42     def testtoListOfTuples_SinglePPS(self):
43 jfenwick 3892 fs=getTestDomainFunctionSpace(1,5)
44     inp=42.0
45     d=Data(inp,fs,True)
46     for x in range(5):
47     d.setValueOfDataPoint(x,43+x)
48     t=d.toListOfTuples(scalarastuple=True)
49     self.assertTrue(len(t)==5,"Returned list has the wrong length")
50     total=0
51     for x in range(5):
52     total+=t[x][0]-43-x
53     self.assertTrue(abs(total)<EPSILON,"Returned list has wrong elements")
54     inp=[[1,2],[3,4]]
55     d=Data(inp,fs,True)
56     for x in range(5):
57     d.setValueOfDataPoint(x,((x,x+1),(x+2,x+3)))
58     t=d.toListOfTuples(scalarastuple=True)
59     ok=True
60     for x in range(5):
61     if t[x]!=((x,x+1),(x+2,x+3)): ok=False
62     self.assertTrue(ok, "Returned matrix does not match")
63    
64 jfenwick 2485 def testtoListOfTuples_MultiPPS(self):
65 jfenwick 3892 fs=getTestDomainFunctionSpace(3,5)
66     inp=0
67     d=Data(inp,fs,True)
68     for x in range(15):
69     d.setValueOfDataPoint(x,x)
70     ok=True
71     t=d.toListOfTuples(scalarastuple=True)
72     for x in range(15):
73     if t[x]!=(x,): ok=False
74     self.assertTrue(ok,"Returned scalar does not match")
75     inp=(0,0)
76     d=Data(inp,fs,True)
77     for x in range(15):
78     d.setValueOfDataPoint(x,(2*(x/2),(2*(x/2)+1)))
79     t=d.toListOfTuples(scalarastuple=True)
80     ok=True
81     for x in range(15):
82     if t[x]!=(2*(x/2),2*(x/2)+1): ok=False
83     self.assertTrue(ok,"Returned vector does not match")
84     # Now we try Matricies
85     inp=((0,0),(0,0))
86     d=Data(inp,fs,True)
87     for x in range(15):
88     d.setValueOfDataPoint(x,((x,x+1),(x+2,x+3)))
89     t=d.toListOfTuples(scalarastuple=True)
90     ok=True
91     for x in range(15):
92     if t[x]!=((x,x+1),(x+2,x+3)): ok=False
93     self.assertTrue(ok,"Returned matrix does not match")
94     #Now 3-Tensors
95     inp=(((0,0),(0,0)),((0,0),(0,0)))
96     d=Data(inp,fs,True)
97     for x in range(15):
98     d.setValueOfDataPoint(x,(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))))
99     t=d.toListOfTuples(scalarastuple=True)
100     ok=True
101     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
103     self.assertTrue(ok,"Returned 3-Tensor does not match")
104     #Now 4-Tensors
105     inp=((((0,0),(0,0)),((0,0),(0,0))),(((0,0),(0,0)),((0,0),(0,0))))
106     d=Data(inp,fs,True)
107     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)))))
109     t=d.toListOfTuples()
110     ok=True
111     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
113     self.assertTrue(ok,"Returned 4-Tensor does not match")
114    
115 jfenwick 2485 # This test sets values then gets them.
116     # Strictly speaking, it is not a complete test because it cannot tell if functions are broken
117 jfenwick 3892 # in a symmetric manner
118 jfenwick 2485 def testToFromTupleTogether(self):
119 jfenwick 3892 fs=getTestDomainFunctionSpace(3,5)
120     inp=0
121     d=Data(inp,fs,True)
122     for x in range(15):
123     d.setValueOfDataPoint(x,x)
124     ok=True
125     for x in range(15):
126     if d.getTupleForDataPoint(x)!=(x,): ok=False
127     self.assertTrue(ok,"Returned scalar does not match")
128     inp=(0,0)
129     d=Data(inp,fs,True)
130     for x in range(15):
131     d.setValueOfDataPoint(x,(2*(x/2),(2*(x/2)+1)))
132     ok=True
133     for x in range(15):
134     if d.getTupleForDataPoint(x)!=(2*(x/2),2*(x/2)+1): ok=False
135     self.assertTrue(ok,"Returned vector does not match")
136     # Now we try Matricies
137     inp=((0,0),(0,0))
138     d=Data(inp,fs,True)
139     for x in range(15):
140     d.setValueOfDataPoint(x,((x,x+1),(x+2,x+3)))
141     ok=True
142     for x in range(15):
143     if d.getTupleForDataPoint(x)!=((x,x+1),(x+2,x+3)): ok=False
144     self.assertTrue(ok,"Returned matrix does not match")
145     #Now 3-Tensors
146     inp=(((0,0),(0,0)),((0,0),(0,0)))
147     d=Data(inp,fs,True)
148     for x in range(15):
149     d.setValueOfDataPoint(x,(((x,x+1),(x+2,x+3)),((x+4,x+5),(x+6,x+7))))
150     ok=True
151     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
153     self.assertTrue(ok,"Returned 3-Tensor does not match")
154     #Now 4-Tensors
155     inp=((((0,0),(0,0)),((0,0),(0,0))),(((0,0),(0,0)),((0,0),(0,0))))
156     d=Data(inp,fs,True)
157     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)))))
159     ok=True
160     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
162     self.assertTrue(ok,"Returned 4-Tensor does not match")
163    
164    
165 jfenwick 2485 if __name__ == "__main__":
166     if getMPISizeWorld() == 1:
167     suite = unittest.TestSuite()
168     suite.addTest(unittest.makeSuite(DataAccessTestCase))
169     s=unittest.TextTestRunner(verbosity=2).run(suite)
170     if not s.wasSuccessful(): sys.exit(1)
171     else:
172 jfenwick 3771 print("run_data_access is not executed as more than one processor is used.")

  ViewVC Help
Powered by ViewVC 1.1.26