/[escript]/trunk/escript/test/DataMathsTestCase.cpp
ViewVC logotype

Contents of /trunk/escript/test/DataMathsTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1811 - (show annotations)
Thu Sep 25 23:11:13 2008 UTC (11 years, 2 months ago) by ksteube
File size: 18383 byte(s)
Copyright updated in all files

1
2 /*******************************************************
3 *
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
14
15 #include "escript/DataAlgorithm.h"
16 #include "escript/DataVector.h"
17 #include "esysUtils/EsysException.h"
18
19 #include "DataMathsTestCase.h"
20 #include "escript/DataTypes.h"
21
22 #include <iostream>
23
24 using namespace CppUnitTest;
25 using namespace esysUtils;
26 using namespace escript;
27 using namespace std;
28 using namespace escript::DataTypes;
29 using namespace escript::DataMaths;
30
31 void DataMathsTestCase::setUp() {
32 //
33 // This is called before each test is run
34
35 }
36
37 void DataMathsTestCase::tearDown() {
38 //
39 // This is called after each test has been run
40
41 }
42
43
44 void DataMathsTestCase::testMatMult()
45 {
46
47 {
48 cout << endl;
49 cout << "\tTest result shape." << endl;
50
51 DataTypes::ShapeType leftShape;
52 leftShape.push_back(1);
53 leftShape.push_back(3);
54 DataTypes::ValueType leftData(DataTypes::noValues(leftShape),0);
55 // DataArrayView leftDataView(leftData,leftShape);
56
57 DataTypes::ShapeType rightShape;
58 rightShape.push_back(3);
59 rightShape.push_back(2);
60 DataTypes::ValueType rightData(DataTypes::noValues(rightShape),0);
61 // DataArrayView rightDataView(rightData,rightShape);
62
63 DataTypes::ShapeType resultShape=DataMaths::determineResultShape(leftShape,rightShape);
64
65 assert(resultShape.size()==2);
66 assert(resultShape[0]==1);
67 assert(resultShape[1]==2);
68
69 DataTypes::ValueType resultData(DataTypes::noValues(resultShape),0);
70
71 cout << "\tTest matrix multiplication.";
72 double aValue=0.0;
73 for (int i=0;i<leftShape[0];i++) {
74 for (int j=0;j<leftShape[1];j++) {
75 leftData[getRelIndex(leftShape,i,j)]=++aValue;
76 }
77 }
78 aValue=0.0;
79 for (int i=0;i<rightShape[0];i++) {
80 for (int j=0;j<rightShape[1];j++) {
81 rightData[getRelIndex(rightShape,i,j)]=++aValue;
82 }
83 }
84
85 DataMaths::matMult(leftData,leftShape,0,rightData,rightShape,0,resultData, resultShape);
86 assert((resultData[0]==22) && (resultData[1]==28));
87 }
88
89 cout << endl;
90
91 }
92
93 void DataMathsTestCase::testUnaryOp()
94 {
95
96 // This typedef allows function names to be cast to pointers
97 // to unary functions of the appropriate type.
98 typedef double (*UnaryDFunPtr)(double);
99
100 {
101 cout << endl;
102 cout << "\tTest unaryOp on scalar DataArrayView.";
103
104 // define the shape for the DataArrayView
105 DataTypes::ShapeType shape;
106
107 // allocate the data for the DataArrayView
108 int npoints=4;
109 DataTypes::ValueType data(DataTypes::noValues(shape)*npoints,0);
110
111 // constructor
112 // DataArrayView dataView(data,shape);
113
114 double tmp;
115 int offset=0;
116 // step the view along each data point in the underlying data
117 for (int p=0;p<npoints;p++) {
118
119 // assign values to the data point
120 data[offset]=p;
121
122 // apply a unary operation to this data point
123 unaryOp(data,shape,offset,(UnaryDFunPtr)std::sin);
124
125 // check the results
126 tmp = std::sin((double)p);
127 assert(std::abs(data[offset]-tmp)<=REL_TOL*std::abs(tmp));
128
129 if (p<npoints-1) {
130 offset++;
131 }
132
133 }
134
135 }
136
137 {
138 cout << endl;
139 cout << "\tTest unaryOp on shape (2,3) DataArrayView.";
140
141 // define the shape for the DataArrayView
142 DataTypes::ShapeType shape;
143 shape.push_back(2);
144 shape.push_back(3);
145
146 // allocate the data for the DataArrayView
147 int npoints=4;
148 DataTypes::ValueType data(DataTypes::noValues(shape)*npoints,0);
149
150 // constructor
151 //DataArrayView dataView(data,shape);
152
153
154 int offset=0;
155 // step the view along each data point in the underlying data
156 for (int p=0;p<npoints;p++) {
157
158 // assign values to the data point
159 for (int i=0;i<shape[0];i++) {
160 for (int j=0;j<shape[1];j++) {
161 data[offset+getRelIndex(shape,i,j)]=offset+getRelIndex(shape,i,j);
162 }
163 }
164
165 // apply a unary operation to this data point
166 // dataView.unaryOp((UnaryDFunPtr)std::sqrt);
167 unaryOp(data,shape,offset,(UnaryDFunPtr)std::sqrt);
168
169 // check the results
170 for (int i=0;i<shape[0];i++) {
171 for (int j=0;j<shape[1];j++) {
172 // assert(std::abs(dataView(i,j)-std::sqrt((double)dataView.index(i,j)))<=REL_TOL*std::sqrt((double)dataView.index(i,j)));
173 assert(std::abs(data[offset+getRelIndex(shape,i,j)]-std::sqrt((double)offset+getRelIndex(shape,i,j)))<=REL_TOL*std::sqrt((double)offset+getRelIndex(shape,i,j)));
174 }
175 }
176
177 if (p<npoints-1) {
178 offset++;
179 }
180
181 }
182
183 }
184
185 {
186 cout << endl;
187 cout << "\tTest unaryOp on shape (9,8,5,11) DataArrayView.";
188
189 // define the shape for the DataArrayView
190 DataTypes::ShapeType shape;
191 shape.push_back(9);
192 shape.push_back(8);
193 shape.push_back(5);
194 shape.push_back(11);
195
196 // allocate the data for the DataArrayView
197 int npoints=4;
198 DataTypes::ValueType data(DataTypes::noValues(shape)*npoints,0);
199
200 // constructor
201 //DataArrayView dataView(data,shape);
202
203 int offset=0;
204 // step the view along each data point in the underlying data
205 for (int p=0;p<npoints;p++) {
206
207 // assign values to the data point
208 for (int i=0;i<shape[0];i++) {
209 for (int j=0;j<shape[1];j++) {
210 for (int k=0;k<shape[2];k++) {
211 for (int l=0;l<shape[3];l++) {
212 data[offset+getRelIndex(shape,i,j,k,l)]=data[offset+getRelIndex(shape,i,j,k,l)]+1;
213 }
214 }
215 }
216 }
217
218 // apply a unary operation to this data point
219 // dataView.unaryOp((UnaryDFunPtr)std::log);
220 unaryOp(data,shape,offset,(UnaryDFunPtr)std::log);
221
222 // check the results
223 for (int i=0;i<shape[0];i++) {
224 for (int j=0;j<shape[1];j++) {
225 for (int k=0;k<shape[2];k++) {
226 for (int l=0;l<shape[3];l++) {
227 assert(std::abs(data[offset+getRelIndex(shape,i,j,k,l)]-std::log(1+(double)data[offset+getRelIndex(shape,i,j,k,l)]))<=REL_TOL*std::abs(std::log(1+(double)data[offset+getRelIndex(shape,i,j,k,l)])));
228 }
229 }
230 }
231 }
232
233 if (p<npoints-1) {
234 offset++;
235 }
236
237 }
238
239 }
240
241 cout << endl;
242
243 }
244
245 void DataMathsTestCase::testBinaryOp()
246 {
247
248 // This typedef allows function names to be cast to pointers
249 // to binary functions of the appropriate type.
250 typedef double (*BinaryDFunPtr)(double,double);
251
252 {
253 cout << endl;
254 cout << "\tTest binaryOp on scalar DataArrayViews.";
255
256 // define the shape for the DataArrayViews
257 DataTypes::ShapeType shape;
258
259 // allocate the data for the DataArrayViews
260 int npoints=4;
261 DataTypes::ValueType data1(DataTypes::noValues(shape)*npoints,0);
262 DataTypes::ValueType data2(DataTypes::noValues(shape)*npoints,0);
263
264 // constructor
265 //DataArrayView dataView1(data1,shape);
266 //DataArrayView dataView2(data2,shape);
267
268 int offset=0;
269 // step the views along each data point in the underlying data
270 for (int p=0;p<npoints;p++) {
271
272 // assign values to the data points
273 data1[offset]=p;
274 data2[offset]=p;
275
276 // apply a binary operation to these data points
277 // dataView1.binaryOp(dataView2,plus<double>());
278 binaryOp(data1,scalarShape,offset,data2,scalarShape,offset, plus<double>());
279
280 // check the results
281 assert(data1[offset]==p+p);
282
283 if (p<npoints-1) {
284 ++offset;
285 }
286
287 }
288
289 }
290
291 {
292 cout << endl;
293 cout << "\tTest binaryOp on shape (2,3) DataArrayViews.";
294
295 // define the shape for the DataArrayViews
296 DataTypes::ShapeType shape;
297 shape.push_back(2);
298 shape.push_back(3);
299
300 // allocate the data for the DataArrayViews
301 int npoints=4;
302 DataTypes::ValueType data1(DataTypes::noValues(shape)*npoints,0);
303 DataTypes::ValueType data2(DataTypes::noValues(shape)*npoints,0);
304
305 // constructor
306 //DataArrayView dataView1(data1,shape);
307 //DataArrayView dataView2(data2,shape);
308
309 int offset=0;
310 // step the views along each data point in the underlying data
311 for (int p=0;p<npoints;p++) {
312
313 // assign values to the data points
314 for (int i=0;i<shape[0];i++) {
315 for (int j=0;j<shape[1];j++) {
316 data1[offset+getRelIndex(shape,i,j)]=offset+getRelIndex(shape,i,j);
317 data2[offset+getRelIndex(shape,i,j)]=offset+getRelIndex(shape,i,j);
318 }
319 }
320
321 // apply a binary operation to these data points
322 /* dataView1.binaryOp(dataView2,multiplies<double>());*/
323 binaryOp(data1,shape,offset,data2,shape,offset,multiplies<double>());
324
325 // check the results
326 for (int i=0;i<shape[0];i++) {
327 for (int j=0;j<shape[1];j++) {
328 assert(data1[offset+getRelIndex(shape,i,j)]==(offset+getRelIndex(shape,i,j))*(offset+getRelIndex(shape,i,j)));
329 }
330 }
331
332 if (p<npoints-1) {
333 offset+=noValues(shape);
334 }
335
336 }
337
338 }
339
340 {
341 cout << endl;
342 cout << "\tTest binaryOp on shape (9,8,5,11) DataArrayViews.";
343
344 // define the shape for the DataArrayViews
345 DataTypes::ShapeType shape;
346 shape.push_back(9);
347 shape.push_back(8);
348 shape.push_back(5);
349 shape.push_back(11);
350
351 // allocate the data for the DataArrayViews
352 int npoints=4;
353 DataTypes::ValueType data1(DataTypes::noValues(shape)*npoints,0);
354 DataTypes::ValueType data2(DataTypes::noValues(shape)*npoints,0);
355
356 // constructor
357 //DataArrayView dataView1(data1,shape);
358 //DataArrayView dataView2(data2,shape);
359
360 int offset=0;
361 // step the views along each data point in the underlying data
362 for (int p=0;p<npoints;p++) {
363
364 // assign values to the data points
365 for (int i=0;i<shape[0];i++) {
366 for (int j=0;j<shape[1];j++) {
367 for (int k=0;k<shape[2];k++) {
368 for (int l=0;l<shape[3];l++) {
369 data1[offset+getRelIndex(shape,i,j,k,l)]=offset+getRelIndex(shape,i,j,k,l);
370 data2[offset+getRelIndex(shape,i,j,k,l)]=offset+getRelIndex(shape,i,j,k,l);
371 }
372 }
373 }
374 }
375
376 // apply a binary operation to these data points
377 // dataView1.binaryOp(dataView2,multiplies<double>());
378 binaryOp(data1,shape,offset,data2,shape,offset,multiplies<double>());
379
380 // check the results
381 for (int i=0;i<shape[0];i++) {
382 for (int j=0;j<shape[1];j++) {
383 for (int k=0;k<shape[2];k++) {
384 for (int l=0;l<shape[3];l++) {
385 assert(data1[offset+getRelIndex(shape,i,j,k,l)]==(offset+getRelIndex(shape,i,j,k,l))*(offset+getRelIndex(shape,i,j,k,l)));
386 }
387 }
388 }
389 }
390
391 if (p<npoints-1) {
392 offset+=noValues(shape);
393 }
394
395 }
396
397 }
398
399 {
400 cout << endl;
401 cout << "\tTest binaryOp on scalar DataArrayView and single value.";
402
403 // define the shape for the DataArrayView
404 DataTypes::ShapeType shape;
405
406 // allocate the data for the DataArrayView
407 int npoints=4;
408 DataTypes::ValueType data(DataTypes::noValues(shape)*npoints,0);
409
410 // constructor
411 // DataArrayView dataView(data,shape);
412
413 int offset=0;
414 // step the view along each data point in the underlying data
415 for (int p=0;p<npoints;p++) {
416
417 // assign values to the data point
418 data[offset]=p;
419
420 // apply a binary operation to this data point
421 // dataView.binaryOp(4.9,plus<double>());
422 binaryOp(data,shape,offset,4.9,plus<double>());
423
424 // check the results
425 assert(data[offset]==4.9+p);
426
427 if (p<npoints-1) {
428 ++offset;
429 }
430
431 }
432
433 }
434
435 {
436 cout << endl;
437 cout << "\tTest binaryOp on shape (2,3) DataArrayView and single value.";
438
439 // define the shape for the DataArrayView
440 DataTypes::ShapeType shape;
441 shape.push_back(2);
442 shape.push_back(3);
443
444 // allocate the data for the DataArrayView
445 int npoints=4;
446 DataTypes::ValueType data(DataTypes::noValues(shape)*npoints,0);
447
448 // constructor
449 // DataArrayView dataView(data,shape);
450
451
452 int offset=0;
453 // step the view along each data point in the underlying data
454 for (int p=0;p<npoints;p++) {
455
456 // assign values to the data point
457 for (int i=0;i<shape[0];i++) {
458 for (int j=0;j<shape[1];j++) {
459 data[offset+getRelIndex(shape,i,j)]=offset+getRelIndex(shape,i,j);
460 }
461 }
462
463 // apply a binary operation to the data point
464 // dataView.binaryOp(5.8,multiplies<double>());
465 binaryOp(data,shape,offset,5.8,multiplies<double>());
466
467 double tmp;
468 // check the results
469 for (int i=0;i<shape[0];i++) {
470 for (int j=0;j<shape[1];j++) {
471 tmp=5.8*(offset+getRelIndex(shape,i,j));
472 assert(std::abs(data[offset+getRelIndex(shape,i,j)]-tmp)<=REL_TOL*std::abs(tmp));
473 }
474 }
475
476 if (p<npoints-1) {
477 offset+=noValues(shape);
478 }
479
480 }
481
482 }
483
484 {
485 cout << endl;
486 cout << "\tTest binaryOp on shape (9,8,5,11) DataArrayView and single value.";
487
488 // define the shape for the DataArrayView
489 DataTypes::ShapeType shape;
490 shape.push_back(9);
491 shape.push_back(8);
492 shape.push_back(5);
493 shape.push_back(11);
494
495 // allocate the data for the DataArrayView
496 int npoints=4;
497 DataTypes::ValueType data(DataTypes::noValues(shape)*npoints,0);
498
499 // constructor
500 // DataArrayView dataView(data,shape);
501
502 int offset=0;
503 // step the view along each data point in the underlying data
504 for (int p=0;p<npoints;p++) {
505
506 // assign values to the data point
507 for (int i=0;i<shape[0];i++) {
508 for (int j=0;j<shape[1];j++) {
509 for (int k=0;k<shape[2];k++) {
510 for (int l=0;l<shape[3];l++) {
511 data[offset+getRelIndex(shape,i,j,k,l)]=offset+getRelIndex(shape,i,j,k,l);
512 }
513 }
514 }
515 }
516
517 // apply a binary operation to the data point
518 // dataView.binaryOp(5.4,multiplies<double>());
519 binaryOp(data,shape,offset,5.4,multiplies<double>());
520
521 double tmp;
522 // check the results
523 for (int i=0;i<shape[0];i++) {
524 for (int j=0;j<shape[1];j++) {
525 for (int k=0;k<shape[2];k++) {
526 for (int l=0;l<shape[3];l++) {
527 tmp=5.4*(offset+getRelIndex(shape,i,j,k,l));
528 assert(std::abs(data[offset+getRelIndex(shape,i,j,k,l)]-tmp)<=REL_TOL*std::abs(tmp));
529 }
530 }
531 }
532 }
533
534 if (p<npoints-1) {
535 offset+=noValues(shape);
536 }
537
538 }
539
540 }
541
542 cout << endl;
543
544 }
545
546 void DataMathsTestCase::testReductionOp()
547 {
548
549 {
550 cout << endl;
551 cout << "\tTest reductionOp on scalar DataArrayView.";
552
553 // define the shape for the DataArrayView
554 DataTypes::ShapeType shape;
555
556 // allocate the data for the DataArrayView
557 int npoints=4;
558 DataTypes::ValueType data(DataTypes::noValues(shape)*npoints,0);
559
560 // constructor
561 // DataArrayView dataView(data,shape);
562
563 int offset=0;
564 // step the view along each data point in the underlying data
565 for (int p=0;p<npoints;p++) {
566
567 // assign values to the data point
568 data[offset]=p;
569
570 // apply a reduction operation to this data point and check the results
571 FMax fmax_func;
572 // assert(std::abs(dataView.reductionOp(fmax_func,numeric_limits<double>::max()*-1)-p)<=REL_TOL*p);
573 assert(std::abs(reductionOp(data,shape,offset,fmax_func,numeric_limits<double>::max()*-1)-p)<=REL_TOL*p);
574
575
576
577 if (p<npoints-1) {
578 ++offset;
579 }
580
581 }
582
583 }
584
585 {
586 cout << endl;
587 cout << "\tTest reductionOp on shape (2,3) DataArrayView.";
588
589 // define the shape for the DataArrayView
590 DataTypes::ShapeType shape;
591 shape.push_back(2);
592 shape.push_back(3);
593
594 // allocate the data for the DataArrayView
595 int npoints=4;
596 DataTypes::ValueType data(DataTypes::noValues(shape)*npoints,0);
597
598 // constructor
599 // DataArrayView dataView(data,shape);
600
601 int offset=0;
602 // step the view along each data point in the underlying data
603 for (int p=0;p<npoints;p++) {
604
605 // assign values to the data point
606 for (int i=0;i<shape[0];i++) {
607 for (int j=0;j<shape[1];j++) {
608 data[offset+getRelIndex(shape,i,j)]=offset+getRelIndex(shape,i,j);
609 }
610 }
611
612 // apply a reduction operation to this data point and check the results
613 FMin fmin_func;
614 assert(std::abs(reductionOp(data,shape,offset,fmin_func,numeric_limits<double>::max())-offset)<=REL_TOL*std::abs(offset));
615
616 if (p<npoints-1) {
617 offset+=noValues(shape);
618 }
619
620 }
621
622 }
623
624 {
625 cout << endl;
626 cout << "\tTest reductionOp on shape (9,8,5,11) DataArrayView.";
627
628 // define the shape for the DataArrayView
629 DataTypes::ShapeType shape;
630 shape.push_back(9);
631 shape.push_back(8);
632 shape.push_back(5);
633 shape.push_back(11);
634
635 // allocate the data for the DataArrayView
636 int npoints=4;
637 DataTypes::ValueType data(DataTypes::noValues(shape)*npoints,0);
638
639 // constructor
640 // DataArrayView dataView(data,shape);
641
642 int offset=0;
643 // step the view along each data point in the underlying data
644 for (int p=0;p<npoints;p++) {
645
646 // assign values to the data point
647 for (int i=0;i<shape[0];i++) {
648 for (int j=0;j<shape[1];j++) {
649 for (int k=0;k<shape[2];k++) {
650 for (int l=0;l<shape[3];l++) {
651 data[offset+getRelIndex(shape,i,j,k,l)]=offset+getRelIndex(shape,i,j,k,l);
652 }
653 }
654 }
655 }
656
657 // apply a reduction operation to this data point and check the results
658 AbsMax absmax_func;
659 assert(reductionOp(data,shape,offset,absmax_func,0)==offset+getRelIndex(shape,8,7,4,10));
660
661 if (p<npoints-1) {
662 offset+=noValues(shape);
663 }
664
665 }
666
667 }
668
669 cout << endl;
670
671 }
672
673 TestSuite* DataMathsTestCase::suite ()
674 {
675 //
676 // create the suite of tests to perform.
677 TestSuite *testSuite = new TestSuite ("DataMathsTestCase");
678
679 testSuite->addTest (new TestCaller< DataMathsTestCase>("testUnaryOp",&DataMathsTestCase::testUnaryOp));
680 testSuite->addTest (new TestCaller< DataMathsTestCase>("testBinaryOp",&DataMathsTestCase::testBinaryOp));
681 testSuite->addTest (new TestCaller< DataMathsTestCase>("testReductionOp",&DataMathsTestCase::testReductionOp));
682 testSuite->addTest (new TestCaller< DataMathsTestCase>("testMatMult",&DataMathsTestCase::testMatMult));
683 return testSuite;
684 }

  ViewVC Help
Powered by ViewVC 1.1.26