/[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/esys2/escript/test/DataAlgorithmAdapter/DataAlgorithmAdapterTestCase.cpp revision 102 by jgs, Wed Dec 15 07:08:39 2004 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  #include "escript/Data/DataAlgorithm.h"  #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"  #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 36  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;
64    
65    cout << "\tTesting FMax." << endl;    cout << "\tTesting FMax." << endl;
66    DataAlgorithmAdapter<FMax> myMax(numeric_limits<double>::min());  
67    myMax(1);    FMax fmax;
68    myMax(2);    assert(std::abs(fmax(5,6)-6)<=REL_TOL*6);
69    myMax(14);    assert(std::abs(fmax(5,-6)-5)<=REL_TOL*5);
70    myMax(3);    assert(std::abs(fmax(0,0)-0)<=REL_TOL*0);
71    assert(myMax.getResult()==14);    assert(std::abs(fmax(15,-96)-15)<=REL_TOL*15);
72    
73      DataAlgorithmAdapter<FMax> sup(numeric_limits<double>::max()*-1);
74      sup.resetResult();
75      sup(-1);
76      sup(-2);
77      sup(-14);
78      sup(3);
79      assert(std::abs(sup.getResult()-3)<=REL_TOL*3);
80    
81    cout << "\tTesting AbsMax." << endl;    cout << "\tTesting AbsMax." << endl;
82    
83      AbsMax absmax;
84      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    
89    DataAlgorithmAdapter<AbsMax> Lsup(0);    DataAlgorithmAdapter<AbsMax> Lsup(0);
90      Lsup.resetResult();
91    Lsup(-2);    Lsup(-2);
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;
100      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    
105    DataAlgorithmAdapter<FMin> inf(numeric_limits<double>::max());    DataAlgorithmAdapter<FMin> inf(numeric_limits<double>::max());
106      inf.resetResult();
107    inf(1);    inf(1);
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;
114    
115    cout << "\tSize: " << sizeof(DataAlgorithmAdapter<FMin>) << endl;    Length lngth;
116      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    
121      DataAlgorithmAdapter<Length> length(0);
122      length.resetResult();
123      length(2);
124      length(4);
125      length(6);
126      length(8);
127      assert(std::abs(length.getResult()-std::sqrt(120.0))<=REL_TOL*std::sqrt(120.0));
128      length.resetResult();
129      length(1.5);
130      length(2.5);
131      length(3.5);
132      length(4.5);
133      assert(std::abs(length.getResult()-std::sqrt(41.0))<=REL_TOL*std::sqrt(41.0));
134    
135      cout << "\tTesting Trace." << endl;
136    
137      Trace trce;
138      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    
143      DataAlgorithmAdapter<Trace> trace(0);
144      trace.resetResult();
145      trace(1);
146      trace(2);
147      trace(3);
148      trace(4);
149      trace(5);
150      assert(std::abs(trace.getResult()-15)<=REL_TOL*15);
151      trace.resetResult();
152      trace(1.5);
153      trace(2.5);
154      trace(3.5);
155      trace(4.5);
156      trace(5.5);
157      assert(std::abs(trace.getResult()-17.5)<=REL_TOL*17.5);
158    
159    }
160    
161    void DataAlgorithmAdapterTestCase::testAlgorithm() {
162    
163      cout << endl;
164    
165      {
166    
167        cout << "\tTest algorithm on Data objects with a single rank 2 data-point." << endl;
168    
169        // define the shape for the DataArrayView
170        DataTypes::ShapeType shape;
171        shape.push_back(2);
172        shape.push_back(3);
173    
174        // allocate the data for the DataArrayView
175        DataTypes::ValueType dataArray(DataTypes::noValues(shape),0);
176    
177        // construct DataArrayView
178    //     DataArrayView dataView(dataArray,shape);
179    
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            dataArray[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
184          }
185        }
186    
187        // create a few Data objects from the created DataArrayView
188        DataExpanded dataExp(FunctionSpace(),shape,dataArray);
189        DataConstant dataCon(FunctionSpace(),shape,dataArray);
190        DataTagged   dataTag(dataCon);
191    
192        // test algorithm on DataExpanded
193        FMin fmin_func;
194        assert(std::abs(escript::algorithm(dataExp,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
195        FMax fmax_func;
196        assert(std::abs(escript::algorithm(dataExp,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
197    
198        // test algorithm on DataTagged
199        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    
202        // test algorithm on DataConstant
203        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    
206      }
207    
208    }
209    
210    void DataAlgorithmAdapterTestCase::testDpAlgorithm() {
211    
212      cout << endl;
213    
214      {
215    
216        cout << "\tTest dp_algorithm on Data objects with a single rank 2 data-point." << endl;
217    
218        // define the shapes for the DataArrayViews
219        DataTypes::ShapeType shape;
220        shape.push_back(2);
221        shape.push_back(3);
222        DataTypes::ShapeType shape2;
223    
224        // allocate the data for the DataArrayViews
225        DataTypes::ValueType dataArray(DataTypes::noValues(shape),0);
226        DataTypes::ValueType dataArray2(DataTypes::noValues(shape2),0);
227    
228        // construct DataArrayViews
229    //     DataArrayView dataView(dataArray,shape);
230    //     DataArrayView dataView2(dataArray2,shape2);
231    
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            dataArray[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
236          }
237        }
238    
239        // create a few Data objects from the created DataArrayViews
240        DataExpanded dataExp(FunctionSpace(),shape,dataArray);
241        DataConstant dataCon(FunctionSpace(),shape,dataArray);
242        DataTagged   dataTag(dataCon);
243    
244        // and create Data objects to receive the results of the dp_algorithm calls
245        DataExpanded dataExp2(FunctionSpace(),shape2,dataArray2);
246        DataConstant dataCon2(FunctionSpace(),shape2,dataArray2);
247        DataTagged   dataTag2(dataCon2);
248    
249        // test dp_algorithm on DataExpanded
250        FMin fmin_func;
251        escript::dp_algorithm(dataExp,dataExp2,fmin_func,numeric_limits<double>::max());
252        assert(std::abs(getSRef(dataExp2,0,0)-0)<=REL_TOL*0);
253        FMax fmax_func;
254        escript::dp_algorithm(dataExp,dataExp2,fmax_func,numeric_limits<double>::max()*-1);
255        assert(std::abs(getSRef(dataExp2,0,0)-5)<=REL_TOL*5);
256    
257        // test dp_algorithm on DataTagged
258        escript::dp_algorithm(dataTag,dataTag2,fmin_func,numeric_limits<double>::max());
259        assert(std::abs(getSRef(dataTag2,0,0)-0)<=REL_TOL*0);
260        escript::dp_algorithm(dataTag,dataTag2,fmax_func,numeric_limits<double>::max()*-1);
261        assert(std::abs(getSRef(dataTag2,0,0)-5)<=REL_TOL*5);
262    
263        // test dp_algorithm on DataConstant
264        escript::dp_algorithm(dataCon,dataCon2,fmin_func,numeric_limits<double>::max());
265        assert(std::abs(getSRef(dataCon2,0,0)-0)<=REL_TOL*0);
266        escript::dp_algorithm(dataCon,dataCon2,fmax_func,numeric_limits<double>::max()*-1);
267        assert(std::abs(getSRef(dataCon2,0,0)-5)<=REL_TOL*5);
268    
269      }
270    
271  }  }
272    
# Line 75  TestSuite* DataAlgorithmAdapterTestCase: Line 277  TestSuite* DataAlgorithmAdapterTestCase:
277    TestSuite *testSuite = new TestSuite ("DataAlgorithmAdapterTestCase");    TestSuite *testSuite = new TestSuite ("DataAlgorithmAdapterTestCase");
278    
279    testSuite->addTest (new TestCaller< DataAlgorithmAdapterTestCase>("testAll",&DataAlgorithmAdapterTestCase::testAll));    testSuite->addTest (new TestCaller< DataAlgorithmAdapterTestCase>("testAll",&DataAlgorithmAdapterTestCase::testAll));
280      testSuite->addTest (new TestCaller<DataAlgorithmAdapterTestCase>("testAlgorithm",&DataAlgorithmAdapterTestCase::testAlgorithm));
281      testSuite->addTest (new TestCaller<DataAlgorithmAdapterTestCase>("testDpAlgorithm",&DataAlgorithmAdapterTestCase::testDpAlgorithm));
282    return testSuite;    return testSuite;
283  }  }
   

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

  ViewVC Help
Powered by ViewVC 1.1.26