/[escript]/branches/escript3047_with_pastix2995/escript/test/DataAlgorithmAdapterTestCase.cpp
ViewVC logotype

Annotation of /branches/escript3047_with_pastix2995/escript/test/DataAlgorithmAdapterTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2271 - (hide annotations)
Mon Feb 16 05:08:29 2009 UTC (10 years, 8 months ago) by jfenwick
Original Path: trunk/escript/test/DataAlgorithmAdapterTestCase.cpp
File size: 8468 byte(s)
Merging version 2269 to trunk

1 ksteube 1312
2     /*******************************************************
3 ksteube 1811 *
4     * Copyright (c) 2003-2008 by University of Queensland
5     * 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 ksteube 1312
14 ksteube 1811
15 phornby 1026 #if (defined _WIN32) && (defined __INTEL_COMPILER)
16 woo409 757 #include <mathimf.h>
17     #else
18     #include <math.h>
19     #endif
20 jgs 113
21 robwdcock 670 #include "escript/DataExpanded.h"
22 jfenwick 1796 // #include "escript/DataArrayView.h"
23 robwdcock 670 #include "escript/DataAlgorithm.h"
24 jgs 82 #include "DataAlgorithmAdapterTestCase.h"
25 jfenwick 1796 #include "escript/DataTypes.h"
26 jgs 82
27     #include <iostream>
28     #include <algorithm>
29     #include <limits>
30    
31     using namespace CppUnitTest;
32     using namespace std;
33     using namespace escript;
34 jfenwick 1796 using namespace escript::DataTypes;
35 jgs 82
36     void DataAlgorithmAdapterTestCase::setUp() {
37     //
38     // This is called before each test is run
39    
40     }
41    
42     void DataAlgorithmAdapterTestCase::tearDown() {
43     //
44     // This is called after each test has been run
45    
46     }
47    
48 jfenwick 1796
49     namespace
50     {
51    
52 jfenwick 2271 ValueType::const_reference
53     getSRefRO(DataReady& data,int sample, int point)
54 jfenwick 1796 {
55 jfenwick 2271 return data.getVectorRO()[data.getPointOffset(sample,point)];
56 jfenwick 1796 }
57    
58    
59     }
60    
61 jgs 82 void DataAlgorithmAdapterTestCase::testAll() {
62 jgs 102
63 jgs 82 cout << endl;
64 jgs 102
65 jgs 108 cout << "\tTesting FMax." << endl;
66 jgs 106
67 jgs 108 FMax fmax;
68 gross 713 assert(std::abs(fmax(5,6)-6)<=REL_TOL*6);
69     assert(std::abs(fmax(5,-6)-5)<=REL_TOL*5);
70     assert(std::abs(fmax(0,0)-0)<=REL_TOL*0);
71     assert(std::abs(fmax(15,-96)-15)<=REL_TOL*15);
72 jgs 108
73     DataAlgorithmAdapter<FMax> sup(numeric_limits<double>::max()*-1);
74 jgs 106 sup.resetResult();
75 jgs 108 sup(-1);
76     sup(-2);
77     sup(-14);
78 jgs 106 sup(3);
79 gross 713 assert(std::abs(sup.getResult()-3)<=REL_TOL*3);
80 jgs 102
81     cout << "\tTesting AbsMax." << endl;
82 jgs 108
83     AbsMax absmax;
84 gross 713 assert(std::abs(absmax(5,6)-6)<=REL_TOL*6);
85     assert(std::abs(absmax(5,-6)-6)<=REL_TOL*6);
86     assert(std::abs(absmax(0,0)-0)<=REL_TOL*6);
87     assert(std::abs(absmax(15,-96)-96)<=REL_TOL*6);
88 jgs 108
89 jgs 102 DataAlgorithmAdapter<AbsMax> Lsup(0);
90 jgs 106 Lsup.resetResult();
91 jgs 82 Lsup(-2);
92     Lsup(2);
93     Lsup(5);
94 jgs 102 Lsup(-10);
95 gross 713 assert(std::abs(Lsup.getResult()-10)<=REL_TOL*10);
96 jgs 102
97     cout << "\tTesting FMin." << endl;
98 jgs 108
99     FMin fmin;
100 gross 713 assert(std::abs(fmin(5,6)-5)<=REL_TOL*5);
101     assert(std::abs(fmin(5,-6)-(-6))<=REL_TOL*6);
102     assert(std::abs(fmin(0,0)-0)<=REL_TOL*0);
103     assert(std::abs(fmin(15,-96)-(-96))<=REL_TOL*96);
104 jgs 108
105 jgs 82 DataAlgorithmAdapter<FMin> inf(numeric_limits<double>::max());
106 jgs 106 inf.resetResult();
107 jgs 82 inf(1);
108 jgs 102 inf(12);
109 jgs 82 inf(2);
110 jgs 102 inf(99);
111 gross 713 assert(std::abs(inf.getResult()-1)<=REL_TOL*1);
112 jgs 102
113 jgs 106 cout << "\tTesting Length." << endl;
114 jgs 108
115     Length lngth;
116 gross 713 assert(std::abs(lngth(5,6)-std::sqrt(61.0))<=REL_TOL*std::sqrt(61.0));
117     assert(std::abs(lngth(5,-6)-std::sqrt(61.0))<=REL_TOL*std::sqrt(61.0));
118     assert(std::abs(lngth(0,0)-std::sqrt(0.0))<=REL_TOL*std::sqrt(61.0));
119     assert(std::abs(lngth(15,-96)-std::sqrt(9441.0))<=REL_TOL*std::sqrt(61.0));
120 jgs 108
121 jgs 106 DataAlgorithmAdapter<Length> length(0);
122     length.resetResult();
123     length(2);
124     length(4);
125     length(6);
126     length(8);
127 gross 713 assert(std::abs(length.getResult()-std::sqrt(120.0))<=REL_TOL*std::sqrt(120.0));
128 jgs 106 length.resetResult();
129     length(1.5);
130     length(2.5);
131     length(3.5);
132     length(4.5);
133 gross 713 assert(std::abs(length.getResult()-std::sqrt(41.0))<=REL_TOL*std::sqrt(41.0));
134 jgs 102
135 jgs 106 cout << "\tTesting Trace." << endl;
136 jgs 108
137     Trace trce;
138 gross 713 assert(std::abs(trce(5,6)-11)<=REL_TOL*11);
139     assert(std::abs(trce(5,-6)-(-1))<=REL_TOL*1);
140     assert(std::abs(trce(0,0)-0)<=REL_TOL*0);
141     assert(std::abs(trce(15,-96)-(-81))<=REL_TOL*81);
142 jgs 108
143 jgs 106 DataAlgorithmAdapter<Trace> trace(0);
144     trace.resetResult();
145     trace(1);
146     trace(2);
147     trace(3);
148     trace(4);
149     trace(5);
150 gross 713 assert(std::abs(trace.getResult()-15)<=REL_TOL*15);
151 jgs 106 trace.resetResult();
152     trace(1.5);
153     trace(2.5);
154     trace(3.5);
155     trace(4.5);
156     trace(5.5);
157 gross 713 assert(std::abs(trace.getResult()-17.5)<=REL_TOL*17.5);
158 jgs 106
159 jgs 82 }
160    
161 jgs 113 void DataAlgorithmAdapterTestCase::testAlgorithm() {
162    
163 jgs 561 cout << endl;
164    
165 jgs 113 {
166    
167 jgs 561 cout << "\tTest algorithm on Data objects with a single rank 2 data-point." << endl;
168    
169 jgs 113 // define the shape for the DataArrayView
170 jfenwick 1796 DataTypes::ShapeType shape;
171 jgs 113 shape.push_back(2);
172     shape.push_back(3);
173    
174     // allocate the data for the DataArrayView
175 jfenwick 1796 DataTypes::ValueType dataArray(DataTypes::noValues(shape),0);
176 jgs 113
177     // construct DataArrayView
178 jfenwick 1796 // DataArrayView dataView(dataArray,shape);
179 jgs 113
180     // assign values to the data point
181     for (int i=0;i<shape[0];i++) {
182     for (int j=0;j<shape[1];j++) {
183 jfenwick 1796 dataArray[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
184 jgs 113 }
185     }
186    
187     // create a few Data objects from the created DataArrayView
188 jfenwick 1796 DataExpanded dataExp(FunctionSpace(),shape,dataArray);
189     DataConstant dataCon(FunctionSpace(),shape,dataArray);
190 jgs 113 DataTagged dataTag(dataCon);
191    
192     // test algorithm on DataExpanded
193 jgs 147 FMin fmin_func;
194 gross 713 assert(std::abs(escript::algorithm(dataExp,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
195 jgs 147 FMax fmax_func;
196 gross 713 assert(std::abs(escript::algorithm(dataExp,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
197 jgs 113
198     // test algorithm on DataTagged
199 gross 713 assert(std::abs(escript::algorithm(dataTag,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
200     assert(std::abs(escript::algorithm(dataTag,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
201 jgs 113
202     // test algorithm on DataConstant
203 gross 713 assert(std::abs(escript::algorithm(dataCon,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
204     assert(std::abs(escript::algorithm(dataCon,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
205 jgs 113
206     }
207    
208     }
209    
210     void DataAlgorithmAdapterTestCase::testDpAlgorithm() {
211    
212 jgs 561 cout << endl;
213    
214 jgs 113 {
215    
216 jgs 561 cout << "\tTest dp_algorithm on Data objects with a single rank 2 data-point." << endl;
217    
218 jgs 113 // define the shapes for the DataArrayViews
219 jfenwick 1796 DataTypes::ShapeType shape;
220 jgs 113 shape.push_back(2);
221     shape.push_back(3);
222 jfenwick 1796 DataTypes::ShapeType shape2;
223 jgs 113
224     // allocate the data for the DataArrayViews
225 jfenwick 1796 DataTypes::ValueType dataArray(DataTypes::noValues(shape),0);
226     DataTypes::ValueType dataArray2(DataTypes::noValues(shape2),0);
227 jgs 113
228     // construct DataArrayViews
229 jfenwick 1796 // DataArrayView dataView(dataArray,shape);
230     // DataArrayView dataView2(dataArray2,shape2);
231 jgs 113
232     // assign values to the data point
233     for (int i=0;i<shape[0];i++) {
234     for (int j=0;j<shape[1];j++) {
235 jfenwick 1796 dataArray[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
236 jgs 113 }
237     }
238    
239     // create a few Data objects from the created DataArrayViews
240 jfenwick 1796 DataExpanded dataExp(FunctionSpace(),shape,dataArray);
241     DataConstant dataCon(FunctionSpace(),shape,dataArray);
242 jgs 113 DataTagged dataTag(dataCon);
243    
244     // and create Data objects to receive the results of the dp_algorithm calls
245 jfenwick 1796 DataExpanded dataExp2(FunctionSpace(),shape2,dataArray2);
246     DataConstant dataCon2(FunctionSpace(),shape2,dataArray2);
247 jgs 113 DataTagged dataTag2(dataCon2);
248    
249     // test dp_algorithm on DataExpanded
250 jgs 147 FMin fmin_func;
251     escript::dp_algorithm(dataExp,dataExp2,fmin_func,numeric_limits<double>::max());
252 jfenwick 2271 assert(std::abs(getSRefRO(dataExp2,0,0)-0)<=REL_TOL*0);
253 jgs 147 FMax fmax_func;
254     escript::dp_algorithm(dataExp,dataExp2,fmax_func,numeric_limits<double>::max()*-1);
255 jfenwick 2271 assert(std::abs(getSRefRO(dataExp2,0,0)-5)<=REL_TOL*5);
256 jgs 113
257     // test dp_algorithm on DataTagged
258 jgs 147 escript::dp_algorithm(dataTag,dataTag2,fmin_func,numeric_limits<double>::max());
259 jfenwick 2271 assert(std::abs(getSRefRO(dataTag2,0,0)-0)<=REL_TOL*0);
260 jgs 147 escript::dp_algorithm(dataTag,dataTag2,fmax_func,numeric_limits<double>::max()*-1);
261 jfenwick 2271 assert(std::abs(getSRefRO(dataTag2,0,0)-5)<=REL_TOL*5);
262 jgs 113
263     // test dp_algorithm on DataConstant
264 jgs 147 escript::dp_algorithm(dataCon,dataCon2,fmin_func,numeric_limits<double>::max());
265 jfenwick 2271 assert(std::abs(getSRefRO(dataCon2,0,0)-0)<=REL_TOL*0);
266 jgs 147 escript::dp_algorithm(dataCon,dataCon2,fmax_func,numeric_limits<double>::max()*-1);
267 jfenwick 2271 assert(std::abs(getSRefRO(dataCon2,0,0)-5)<=REL_TOL*5);
268 jgs 113
269     }
270    
271     }
272    
273 jgs 82 TestSuite* DataAlgorithmAdapterTestCase::suite ()
274     {
275     //
276     // create the suite of tests to perform.
277     TestSuite *testSuite = new TestSuite ("DataAlgorithmAdapterTestCase");
278    
279     testSuite->addTest (new TestCaller< DataAlgorithmAdapterTestCase>("testAll",&DataAlgorithmAdapterTestCase::testAll));
280 jgs 113 testSuite->addTest (new TestCaller<DataAlgorithmAdapterTestCase>("testAlgorithm",&DataAlgorithmAdapterTestCase::testAlgorithm));
281     testSuite->addTest (new TestCaller<DataAlgorithmAdapterTestCase>("testDpAlgorithm",&DataAlgorithmAdapterTestCase::testDpAlgorithm));
282 jgs 82 return testSuite;
283     }

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26