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