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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/escript/test/DataAlgorithmAdapter/DataAlgorithmAdapterTestCase.cpp revision 155 by jgs, Wed Nov 9 02:02:19 2005 UTC trunk/escript/test/DataAlgorithmAdapterTestCase.cpp revision 2005 by jfenwick, Mon Nov 10 01:21:39 2008 UTC
# Line 1  Line 1 
1  // $Id$  
2  /*  /*******************************************************
3   *****************************************************************************  *
4   *                                                                           *  * Copyright (c) 2003-2008 by University of Queensland
5   *       COPYRIGHT  ACcESS  -  All Rights Reserved                           *  * Earth Systems Science Computational Center (ESSCC)
6   *                                                                           *  * http://www.uq.edu.au/esscc
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   *  * Primary Business: Queensland, Australia
9   * consent of ACcESS.  Copying, use or modification of this software         *  * Licensed under the Open Software License version 3.0
10   * by any unauthorised person is illegal unless that person has a software   *  * http://www.opensource.org/licenses/osl-3.0.php
11   * license agreement with ACcESS.                                            *  *
12   *                                                                           *  *******************************************************/
13   *****************************************************************************  
14  */  
15    #if (defined _WIN32) && (defined __INTEL_COMPILER)
16  #include "escript/Data/DataExpanded.h"  #include <mathimf.h>
17  #include "escript/Data/DataArrayView.h"  #else
18  #include "escript/Data/DataAlgorithm.h"  #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"  #include "DataAlgorithmAdapterTestCase.h"
25    #include "escript/DataTypes.h"
26    
27  #include <iostream>  #include <iostream>
28  #include <algorithm>  #include <algorithm>
 #include <math.h>  
29  #include <limits>  #include <limits>
30    
31  using namespace CppUnitTest;  using namespace CppUnitTest;
32  using namespace std;  using namespace std;
33  using namespace escript;  using namespace escript;
34    using namespace escript::DataTypes;
35    
36  void DataAlgorithmAdapterTestCase::setUp() {  void DataAlgorithmAdapterTestCase::setUp() {
37    //    //
# Line 39  void DataAlgorithmAdapterTestCase::tearD Line 45  void DataAlgorithmAdapterTestCase::tearD
45    
46  }  }
47    
48    
49    namespace
50    {
51    
52    ValueType::reference
53    getSRef(DataReady& data,int sample, int point)
54    {
55       return data.getVector()[data.getPointOffset(sample,point)];
56    }
57    
58    
59    }
60    
61  void DataAlgorithmAdapterTestCase::testAll() {  void DataAlgorithmAdapterTestCase::testAll() {
62    
63    cout << endl;    cout << endl;
# Line 46  void DataAlgorithmAdapterTestCase::testA Line 65  void DataAlgorithmAdapterTestCase::testA
65    cout << "\tTesting FMax." << endl;    cout << "\tTesting FMax." << endl;
66    
67    FMax fmax;    FMax fmax;
68    assert(fmax(5,6)==6);    assert(std::abs(fmax(5,6)-6)<=REL_TOL*6);
69    assert(fmax(5,-6)==5);    assert(std::abs(fmax(5,-6)-5)<=REL_TOL*5);
70    assert(fmax(0,0)==0);    assert(std::abs(fmax(0,0)-0)<=REL_TOL*0);
71    assert(fmax(15,-96)==15);    assert(std::abs(fmax(15,-96)-15)<=REL_TOL*15);
72    
73    DataAlgorithmAdapter<FMax> sup(numeric_limits<double>::max()*-1);    DataAlgorithmAdapter<FMax> sup(numeric_limits<double>::max()*-1);
74    sup.resetResult();    sup.resetResult();
# Line 57  void DataAlgorithmAdapterTestCase::testA Line 76  void DataAlgorithmAdapterTestCase::testA
76    sup(-2);    sup(-2);
77    sup(-14);    sup(-14);
78    sup(3);    sup(3);
79    assert(sup.getResult()==3);    assert(std::abs(sup.getResult()-3)<=REL_TOL*3);
80    
81    cout << "\tTesting AbsMax." << endl;    cout << "\tTesting AbsMax." << endl;
82    
83    AbsMax absmax;    AbsMax absmax;
84    assert(absmax(5,6)==6);    assert(std::abs(absmax(5,6)-6)<=REL_TOL*6);
85    assert(absmax(5,-6)==6);    assert(std::abs(absmax(5,-6)-6)<=REL_TOL*6);
86    assert(absmax(0,0)==0);    assert(std::abs(absmax(0,0)-0)<=REL_TOL*6);
87    assert(absmax(15,-96)==96);    assert(std::abs(absmax(15,-96)-96)<=REL_TOL*6);
88    
89    DataAlgorithmAdapter<AbsMax> Lsup(0);    DataAlgorithmAdapter<AbsMax> Lsup(0);
90    Lsup.resetResult();    Lsup.resetResult();
# Line 73  void DataAlgorithmAdapterTestCase::testA Line 92  void DataAlgorithmAdapterTestCase::testA
92    Lsup(2);    Lsup(2);
93    Lsup(5);    Lsup(5);
94    Lsup(-10);    Lsup(-10);
95    assert(Lsup.getResult()==10);    assert(std::abs(Lsup.getResult()-10)<=REL_TOL*10);
96    
97    cout << "\tTesting FMin." << endl;    cout << "\tTesting FMin." << endl;
98    
99    FMin fmin;    FMin fmin;
100    assert(fmin(5,6)==5);    assert(std::abs(fmin(5,6)-5)<=REL_TOL*5);
101    assert(fmin(5,-6)==-6);    assert(std::abs(fmin(5,-6)-(-6))<=REL_TOL*6);
102    assert(fmin(0,0)==0);    assert(std::abs(fmin(0,0)-0)<=REL_TOL*0);
103    assert(fmin(15,-96)==-96);    assert(std::abs(fmin(15,-96)-(-96))<=REL_TOL*96);
104    
105    DataAlgorithmAdapter<FMin> inf(numeric_limits<double>::max());    DataAlgorithmAdapter<FMin> inf(numeric_limits<double>::max());
106    inf.resetResult();    inf.resetResult();
# Line 89  void DataAlgorithmAdapterTestCase::testA Line 108  void DataAlgorithmAdapterTestCase::testA
108    inf(12);    inf(12);
109    inf(2);    inf(2);
110    inf(99);    inf(99);
111    assert(inf.getResult()==1);    assert(std::abs(inf.getResult()-1)<=REL_TOL*1);
112    
113    cout << "\tTesting Length." << endl;    cout << "\tTesting Length." << endl;
114    
115    Length lngth;    Length lngth;
116    assert(lngth(5,6)==std::sqrt(61.0));    assert(std::abs(lngth(5,6)-std::sqrt(61.0))<=REL_TOL*std::sqrt(61.0));
117    assert(lngth(5,-6)==std::sqrt(61.0));    assert(std::abs(lngth(5,-6)-std::sqrt(61.0))<=REL_TOL*std::sqrt(61.0));
118    assert(lngth(0,0)==std::sqrt(0.0));    assert(std::abs(lngth(0,0)-std::sqrt(0.0))<=REL_TOL*std::sqrt(61.0));
119    assert(lngth(15,-96)==std::sqrt(9441.0));    assert(std::abs(lngth(15,-96)-std::sqrt(9441.0))<=REL_TOL*std::sqrt(61.0));
120    
121    DataAlgorithmAdapter<Length> length(0);    DataAlgorithmAdapter<Length> length(0);
122    length.resetResult();    length.resetResult();
# Line 105  void DataAlgorithmAdapterTestCase::testA Line 124  void DataAlgorithmAdapterTestCase::testA
124    length(4);    length(4);
125    length(6);    length(6);
126    length(8);    length(8);
127    assert(length.getResult()==std::sqrt(120.0));    assert(std::abs(length.getResult()-std::sqrt(120.0))<=REL_TOL*std::sqrt(120.0));
128    length.resetResult();    length.resetResult();
129    length(1.5);    length(1.5);
130    length(2.5);    length(2.5);
131    length(3.5);    length(3.5);
132    length(4.5);    length(4.5);
133    assert(length.getResult()==std::sqrt(41.0));    assert(std::abs(length.getResult()-std::sqrt(41.0))<=REL_TOL*std::sqrt(41.0));
134    
135    cout << "\tTesting Trace." << endl;    cout << "\tTesting Trace." << endl;
136    
137    Trace trce;    Trace trce;
138    assert(trce(5,6)==11);    assert(std::abs(trce(5,6)-11)<=REL_TOL*11);
139    assert(trce(5,-6)==-1);    assert(std::abs(trce(5,-6)-(-1))<=REL_TOL*1);
140    assert(trce(0,0)==0);    assert(std::abs(trce(0,0)-0)<=REL_TOL*0);
141    assert(trce(15,-96)==-81);    assert(std::abs(trce(15,-96)-(-81))<=REL_TOL*81);
142    
143    DataAlgorithmAdapter<Trace> trace(0);    DataAlgorithmAdapter<Trace> trace(0);
144    trace.resetResult();    trace.resetResult();
# Line 128  void DataAlgorithmAdapterTestCase::testA Line 147  void DataAlgorithmAdapterTestCase::testA
147    trace(3);    trace(3);
148    trace(4);    trace(4);
149    trace(5);    trace(5);
150    assert(trace.getResult()==15);    assert(std::abs(trace.getResult()-15)<=REL_TOL*15);
151    trace.resetResult();    trace.resetResult();
152    trace(1.5);    trace(1.5);
153    trace(2.5);    trace(2.5);
154    trace(3.5);    trace(3.5);
155    trace(4.5);    trace(4.5);
156    trace(5.5);    trace(5.5);
157    assert(trace.getResult()==17.5);    assert(std::abs(trace.getResult()-17.5)<=REL_TOL*17.5);
158    
159  }  }
160    
161  void DataAlgorithmAdapterTestCase::testAlgorithm() {  void DataAlgorithmAdapterTestCase::testAlgorithm() {
162    
163      cout << endl;
164    
165    {    {
166      cout << endl;  
167      cout << "\tTest algorithm on Data objects with a single data-point.";      cout << "\tTest algorithm on Data objects with a single rank 2 data-point." << endl;
168    
169      // define the shape for the DataArrayView      // define the shape for the DataArrayView
170      DataArrayView::ShapeType shape;      DataTypes::ShapeType shape;
171      shape.push_back(2);      shape.push_back(2);
172      shape.push_back(3);      shape.push_back(3);
173    
174      // allocate the data for the DataArrayView      // allocate the data for the DataArrayView
175      DataArrayView::ValueType dataArray(DataArrayView::noValues(shape),0);      DataTypes::ValueType dataArray(DataTypes::noValues(shape),0);
176    
177      // construct DataArrayView      // construct DataArrayView
178      DataArrayView dataView(dataArray,shape);  //     DataArrayView dataView(dataArray,shape);
179    
180      // assign values to the data point      // assign values to the data point
181      for (int i=0;i<shape[0];i++) {      for (int i=0;i<shape[0];i++) {
182        for (int j=0;j<shape[1];j++) {        for (int j=0;j<shape[1];j++) {
183          dataView(i,j)=dataView.index(i,j);          dataArray[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
184        }        }
185      }      }
186    
187      // create a few Data objects from the created DataArrayView      // create a few Data objects from the created DataArrayView
188      DataExpanded dataExp(dataView,FunctionSpace());      DataExpanded dataExp(FunctionSpace(),shape,dataArray);
189      DataConstant dataCon(dataView,FunctionSpace());      DataConstant dataCon(FunctionSpace(),shape,dataArray);
190      DataTagged   dataTag(dataCon);      DataTagged   dataTag(dataCon);
191    
192      // test algorithm on DataExpanded      // test algorithm on DataExpanded
193      FMin fmin_func;      FMin fmin_func;
194      assert(escript::algorithm(dataExp,fmin_func,numeric_limits<double>::max())==0);      assert(std::abs(escript::algorithm(dataExp,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
195      FMax fmax_func;      FMax fmax_func;
196      assert(escript::algorithm(dataExp,fmax_func,numeric_limits<double>::max()*-1)==5);      assert(std::abs(escript::algorithm(dataExp,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
197    
198      // test algorithm on DataTagged      // test algorithm on DataTagged
199      assert(escript::algorithm(dataTag,fmin_func,numeric_limits<double>::max())==0);      assert(std::abs(escript::algorithm(dataTag,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
200      assert(escript::algorithm(dataTag,fmax_func,numeric_limits<double>::max()*-1)==5);      assert(std::abs(escript::algorithm(dataTag,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
201    
202      // test algorithm on DataConstant      // test algorithm on DataConstant
203      assert(escript::algorithm(dataCon,fmin_func,numeric_limits<double>::max())==0);      assert(std::abs(escript::algorithm(dataCon,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
204      assert(escript::algorithm(dataCon,fmax_func,numeric_limits<double>::max()*-1)==5);      assert(std::abs(escript::algorithm(dataCon,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
205    
206    }    }
207    
   cout << endl;  
   
208  }  }
209    
210  void DataAlgorithmAdapterTestCase::testDpAlgorithm() {  void DataAlgorithmAdapterTestCase::testDpAlgorithm() {
211    
212      cout << endl;
213    
214    {    {
215      cout << endl;  
216      cout << "\tTest dp_algorithm on Data objects with a single data-point.";      cout << "\tTest dp_algorithm on Data objects with a single rank 2 data-point." << endl;
217    
218      // define the shapes for the DataArrayViews      // define the shapes for the DataArrayViews
219      DataArrayView::ShapeType shape;      DataTypes::ShapeType shape;
220      shape.push_back(2);      shape.push_back(2);
221      shape.push_back(3);      shape.push_back(3);
222      DataArrayView::ShapeType shape2;      DataTypes::ShapeType shape2;
223    
224      // allocate the data for the DataArrayViews      // allocate the data for the DataArrayViews
225      DataArrayView::ValueType dataArray(DataArrayView::noValues(shape),0);      DataTypes::ValueType dataArray(DataTypes::noValues(shape),0);
226      DataArrayView::ValueType dataArray2(DataArrayView::noValues(shape2),0);      DataTypes::ValueType dataArray2(DataTypes::noValues(shape2),0);
227    
228      // construct DataArrayViews      // construct DataArrayViews
229      DataArrayView dataView(dataArray,shape);  //     DataArrayView dataView(dataArray,shape);
230      DataArrayView dataView2(dataArray2,shape2);  //     DataArrayView dataView2(dataArray2,shape2);
231    
232      // assign values to the data point      // assign values to the data point
233      for (int i=0;i<shape[0];i++) {      for (int i=0;i<shape[0];i++) {
234        for (int j=0;j<shape[1];j++) {        for (int j=0;j<shape[1];j++) {
235          dataView(i,j)=dataView.index(i,j);          dataArray[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
236        }        }
237      }      }
238    
239      // create a few Data objects from the created DataArrayViews      // create a few Data objects from the created DataArrayViews
240      DataExpanded dataExp(dataView,FunctionSpace());      DataExpanded dataExp(FunctionSpace(),shape,dataArray);
241      DataConstant dataCon(dataView,FunctionSpace());      DataConstant dataCon(FunctionSpace(),shape,dataArray);
242      DataTagged   dataTag(dataCon);      DataTagged   dataTag(dataCon);
243    
244      // and create Data objects to receive the results of the dp_algorithm calls      // and create Data objects to receive the results of the dp_algorithm calls
245      DataExpanded dataExp2(dataView2,FunctionSpace());      DataExpanded dataExp2(FunctionSpace(),shape2,dataArray2);
246      DataConstant dataCon2(dataView2,FunctionSpace());      DataConstant dataCon2(FunctionSpace(),shape2,dataArray2);
247      DataTagged   dataTag2(dataCon2);      DataTagged   dataTag2(dataCon2);
248    
249      // test dp_algorithm on DataExpanded      // test dp_algorithm on DataExpanded
250      FMin fmin_func;      FMin fmin_func;
251      escript::dp_algorithm(dataExp,dataExp2,fmin_func,numeric_limits<double>::max());      escript::dp_algorithm(dataExp,dataExp2,fmin_func,numeric_limits<double>::max());
252      assert(dataExp2.getDataPoint(0,0)()==0);      assert(std::abs(getSRef(dataExp2,0,0)-0)<=REL_TOL*0);
253      FMax fmax_func;      FMax fmax_func;
254      escript::dp_algorithm(dataExp,dataExp2,fmax_func,numeric_limits<double>::max()*-1);      escript::dp_algorithm(dataExp,dataExp2,fmax_func,numeric_limits<double>::max()*-1);
255      assert(dataExp2.getDataPoint(0,0)()==5);      assert(std::abs(getSRef(dataExp2,0,0)-5)<=REL_TOL*5);
256    
257      // test dp_algorithm on DataTagged      // test dp_algorithm on DataTagged
258      escript::dp_algorithm(dataTag,dataTag2,fmin_func,numeric_limits<double>::max());      escript::dp_algorithm(dataTag,dataTag2,fmin_func,numeric_limits<double>::max());
259      assert(dataTag2.getDataPoint(0,0)()==0);      assert(std::abs(getSRef(dataTag2,0,0)-0)<=REL_TOL*0);
260      escript::dp_algorithm(dataTag,dataTag2,fmax_func,numeric_limits<double>::max()*-1);      escript::dp_algorithm(dataTag,dataTag2,fmax_func,numeric_limits<double>::max()*-1);
261      assert(dataTag2.getDataPoint(0,0)()==5);      assert(std::abs(getSRef(dataTag2,0,0)-5)<=REL_TOL*5);
262    
263      // test dp_algorithm on DataConstant      // test dp_algorithm on DataConstant
264      escript::dp_algorithm(dataCon,dataCon2,fmin_func,numeric_limits<double>::max());      escript::dp_algorithm(dataCon,dataCon2,fmin_func,numeric_limits<double>::max());
265      assert(dataCon2.getDataPoint(0,0)()==0);      assert(std::abs(getSRef(dataCon2,0,0)-0)<=REL_TOL*0);
266      escript::dp_algorithm(dataCon,dataCon2,fmax_func,numeric_limits<double>::max()*-1);      escript::dp_algorithm(dataCon,dataCon2,fmax_func,numeric_limits<double>::max()*-1);
267      assert(dataCon2.getDataPoint(0,0)()==5);      assert(std::abs(getSRef(dataCon2,0,0)-5)<=REL_TOL*5);
268    
269    }    }
270    
   cout << endl;  
   
271  }  }
272    
273  TestSuite* DataAlgorithmAdapterTestCase::suite ()  TestSuite* DataAlgorithmAdapterTestCase::suite ()
# Line 262  TestSuite* DataAlgorithmAdapterTestCase: Line 281  TestSuite* DataAlgorithmAdapterTestCase:
281    testSuite->addTest (new TestCaller<DataAlgorithmAdapterTestCase>("testDpAlgorithm",&DataAlgorithmAdapterTestCase::testDpAlgorithm));    testSuite->addTest (new TestCaller<DataAlgorithmAdapterTestCase>("testDpAlgorithm",&DataAlgorithmAdapterTestCase::testDpAlgorithm));
282    return testSuite;    return testSuite;
283  }  }
   

Legend:
Removed from v.155  
changed lines
  Added in v.2005

  ViewVC Help
Powered by ViewVC 1.1.26