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

Contents of /trunk/escriptcore/test/python/run_data_access.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4938 - (show annotations)
Wed May 14 01:13:23 2014 UTC (5 years, 3 months ago) by jfenwick
File MIME type: text/x-python
File size: 7279 byte(s)
Modify unit tests to read their classes from
esys.escriptcore.utestselect

Change the line in that file to switch between unittest and unittest2


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

  ViewVC Help
Powered by ViewVC 1.1.26