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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1796 - (show annotations)
Wed Sep 17 01:45:46 2008 UTC (11 years, 1 month ago) by jfenwick
File size: 18477 byte(s)
Merged noarrayview branch onto trunk.


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

  ViewVC Help
Powered by ViewVC 1.1.26