/[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 4984 - (show annotations)
Mon Jun 2 02:50:34 2014 UTC (4 years, 10 months ago) by sshaw
File MIME type: text/x-python
File size: 7096 byte(s)
revamping testrunners, now uses automated discovery and allows running specific tests without modifying files (see escriptcore/py_src/testing.py for more info/examples)

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

  ViewVC Help
Powered by ViewVC 1.1.26