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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 854 - (show annotations)
Thu Sep 21 05:29:42 2006 UTC (13 years, 4 months ago) by gross
File size: 18226 byte(s)
Some modifications to the binary operations +,-,*/, pow. 
The code is a bit simpler now and more efficient has there is
no reseising required now. the resizing method has been removed as
it is very, very inefficient. Even serial code should be faster now.
It is now forbidden to do an inplace update of scalar data object with an object 
of rank >0 as this is very slow (and does not make much sense). 


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 #include "escript/FunctionSpace.h"
16 #include "escript/DataExpanded.h"
17 #include "esysUtils/EsysException.h"
18 #include "DataExpandedTestCase.h"
19
20 #include <iostream>
21
22 using namespace CppUnitTest;
23 using namespace escript;
24 using namespace std;
25 using namespace esysUtils;
26
27 void DataExpandedTestCase::setUp() {
28 //
29 // This is called before each test is run
30
31 }
32
33 void DataExpandedTestCase::tearDown() {
34 //
35 // This is called after each test has been run
36
37 }
38
39 void DataExpandedTestCase::testAll() {
40
41 cout << endl;
42
43 //
44 // Create a rank 1 pointData
45 DataArrayView::ShapeType shape;
46 shape.push_back(3);
47 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
48 DataArrayView pointData(data,shape);
49
50 //
51 // Assign an arbitrary value
52 pointData(0)=0.0;
53 pointData(1)=1.0;
54 pointData(2)=2.0;
55
56 //
57 // Test constructor
58 cout << "\tTest DataExpanded constructor." << endl;
59 DataExpanded testData(pointData,FunctionSpace());
60
61 cout << "\tTest getLength." << endl;
62 assert(testData.getLength()==pointData.noValues());
63
64 cout << "\tTest getPointDataView." << endl;
65 for (int i=0;i<testData.getPointDataView().getShape()[0];i++) {
66 assert(testData.getPointDataView()(i)==pointData(i));
67 }
68
69 cout << "\tVerify data point attributes." << endl;
70 DataArrayView dataView=testData.getPointDataView();
71 assert(dataView.getRank()==shape.size());
72 assert(dataView.noValues()==shape[0]*1);
73 assert(dataView.getShape()[0]==shape[0]);
74 assert(testData.getNumDPPSample()==1);
75 assert(testData.getNumSamples()==1);
76 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
77 assert(testData.validSampleNo(testData.getNumSamples()-1));
78
79 //
80 // Test alternative constructor
81 cout << "\tTest DataExpanded alternative constructor." << endl;
82 data[0]=0.0;
83 data[1]=1.0;
84 data[2]=2.0;
85 FunctionSpace tmp_fns;
86 DataExpanded testData1(tmp_fns,shape,data);
87
88 cout << "\tTest getLength." << endl;
89 assert(testData1.getLength()==pointData.noValues());
90
91 cout << "\tTest getPointDataView." << endl;
92 for (int i=0;i<testData1.getPointDataView().getShape()[0];i++) {
93 assert(testData1.getPointDataView()(i)==pointData(i));
94 }
95
96 cout << "\tVerify data point attributes." << endl;
97 dataView=testData1.getPointDataView();
98 assert(dataView.getRank()==shape.size());
99 assert(dataView.noValues()==shape[0]*1);
100 assert(dataView.getShape()[0]==shape[0]);
101 assert(testData.getNumDPPSample()==1);
102 assert(testData.getNumSamples()==1);
103 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
104 assert(testData.validSampleNo(testData.getNumSamples()-1));
105
106 //
107 // Test copy constructor
108 cout << "\tTest DataExpanded copy constructor." << endl;
109 DataExpanded testData2(testData);
110
111 cout << "\tTest getLength." << endl;
112 assert(testData2.getLength()==pointData.noValues());
113
114 cout << "\tTest getPointDataView." << endl;
115 for (int i=0;i<testData2.getPointDataView().getShape()[0];i++) {
116 assert(testData2.getPointDataView()(i)==pointData(i));
117 }
118
119 cout << "\tVerify data point attributes." << endl;
120 dataView=testData2.getPointDataView();
121 assert(dataView.getRank()==shape.size());
122 assert(dataView.noValues()==shape[0]*1);
123 assert(dataView.getShape()[0]==shape[0]);
124 assert(testData2.getNumDPPSample()==1);
125 assert(testData2.getNumSamples()==1);
126 assert(testData2.validSamplePointNo(testData2.getNumDPPSample()-1));
127 assert(testData2.validSampleNo(testData2.getNumSamples()-1));
128
129 }
130
131
132 void DataExpandedTestCase::testSlicing() {
133
134 cout << endl;
135
136 //
137 // Create a rank 1 pointData
138 DataArrayView::ShapeType shape;
139 shape.push_back(3);
140 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
141 DataArrayView pointData(data,shape);
142
143 //
144 // Assign an arbitrary value
145 pointData(0)=0.0;
146 pointData(1)=1.0;
147 pointData(2)=2.0;
148
149 //
150 // Create object to test
151 cout << "\tCreate rank 1 DataExpanded object." << endl;
152 DataExpanded testData(pointData,FunctionSpace());
153
154 cout << "\tTest slicing (whole object)." << endl;
155 DataArrayView::RegionType region;
156 region.push_back(DataArrayView::RegionType::value_type(0,shape[0]));
157
158 DataAbstract* testData2=testData.getSlice(region);
159
160 //
161 // Verify data values
162 cout << "\tVerify data point values." << endl;
163 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
164 assert(testData2->getPointDataView()(i)==pointData(region[0].first+i));
165 }
166
167 cout << "\tVerify data point attributes." << endl;
168 DataArrayView dataView=testData2->getPointDataView();
169 assert(dataView.getRank()==shape.size());
170 assert(dataView.noValues()==shape[0]*1);
171 assert(dataView.getShape()[0]==shape[0]);
172
173 delete testData2;
174 }
175
176 void DataExpandedTestCase::testSlicing2() {
177
178 cout << endl;
179
180 //
181 // Create a rank 2 pointData
182 DataArrayView::ShapeType shape;
183 shape.push_back(3);
184 shape.push_back(3);
185 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
186 DataArrayView pointData(data,shape);
187
188 //
189 // Assign an arbitrary value
190 pointData(0,0)=0.0;
191 pointData(1,0)=1.0;
192 pointData(2,0)=2.0;
193 pointData(0,1)=3.0;
194 pointData(1,1)=4.0;
195 pointData(2,1)=5.0;
196 pointData(0,2)=6.0;
197 pointData(1,2)=7.0;
198 pointData(2,2)=8.0;
199
200 //
201 // Create object to test
202 cout << "\tCreate rank 2 DataExpanded object." << endl;
203 DataExpanded testData(pointData,FunctionSpace());
204
205 cout << "\tTest slicing (part object)." << endl;
206 DataArrayView::RegionType region;
207 region.push_back(DataArrayView::RegionType::value_type(0,2));
208 region.push_back(DataArrayView::RegionType::value_type(0,2));
209 DataAbstract* testData2=testData.getSlice(region);
210
211 //
212 // Verify data values
213 cout << "\tVerify data point values." << endl;
214 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
215 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
216 assert(testData2->getPointDataView()(i,j)==pointData(region[0].first+i,region[1].first+j));
217 }
218 }
219
220 cout << "\tVerify data point attributes." << endl;
221 DataArrayView dataView=testData2->getPointDataView();
222 assert(dataView.getRank()==region.size());
223 assert(dataView.noValues()==(region[0].second-region[0].first)*(region[1].second-region[1].first));
224 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
225 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
226
227 cout << "\tTest slicing (part object)." << endl;
228 DataArrayView::RegionType region2;
229 region2.push_back(DataArrayView::RegionType::value_type(1,3));
230 region2.push_back(DataArrayView::RegionType::value_type(1,3));
231 DataAbstract* testData3=testData.getSlice(region2);
232
233 //
234 // Verify data values
235 cout << "\tVerify data point values." << endl;
236 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
237 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
238 assert(testData3->getPointDataView()(i,j)==pointData(region2[0].first+i,region2[1].first+j));
239 }
240 }
241
242 cout << "\tVerify data point attributes." << endl;
243 dataView=testData3->getPointDataView();
244 assert(dataView.getRank()==region2.size());
245 assert(dataView.noValues()==(region2[0].second-region2[0].first)*(region2[1].second-region2[1].first));
246 assert(dataView.getShape()[0]==(region2[0].second-region2[0].first));
247 assert(dataView.getShape()[1]==(region2[1].second-region2[1].first));
248
249 delete testData2;
250 delete testData3;
251
252 }
253
254 void DataExpandedTestCase::testSlicing3() {
255
256 cout << endl;
257
258 //
259 // Create a rank 3 pointData
260 DataArrayView::ShapeType shape;
261 shape.push_back(3);
262 shape.push_back(3);
263 shape.push_back(3);
264 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
265 DataArrayView pointData(data,shape);
266
267 //
268 // Assign an arbitrary value
269 pointData(0,0,0)=0.0;
270 pointData(1,0,0)=1.0;
271 pointData(2,0,0)=2.0;
272 pointData(0,1,0)=3.0;
273 pointData(1,1,0)=4.0;
274 pointData(2,1,0)=5.0;
275 pointData(0,2,0)=6.0;
276 pointData(1,2,0)=7.0;
277 pointData(2,2,0)=8.0;
278
279 pointData(0,0,1)=9.0;
280 pointData(1,0,1)=10.0;
281 pointData(2,0,1)=11.0;
282 pointData(0,1,1)=12.0;
283 pointData(1,1,1)=13.0;
284 pointData(2,1,1)=14.0;
285 pointData(0,2,1)=15.0;
286 pointData(1,2,1)=16.0;
287 pointData(2,2,1)=17.0;
288
289 pointData(0,0,2)=18.0;
290 pointData(1,0,2)=19.0;
291 pointData(2,0,2)=20.0;
292 pointData(0,1,2)=21.0;
293 pointData(1,1,2)=22.0;
294 pointData(2,1,2)=23.0;
295 pointData(0,2,2)=24.0;
296 pointData(1,2,2)=25.0;
297 pointData(2,2,2)=26.0;
298
299 //
300 // Create object to test
301 cout << "\tCreate rank 3 DataExpanded object." << endl;
302 DataExpanded testData(pointData,FunctionSpace());
303
304 cout << "\tTest slicing (part object)." << endl;
305 DataArrayView::RegionType region;
306 region.push_back(DataArrayView::RegionType::value_type(0,2));
307 region.push_back(DataArrayView::RegionType::value_type(0,2));
308 region.push_back(DataArrayView::RegionType::value_type(0,2));
309 DataAbstract* testData2=testData.getSlice(region);
310
311 //
312 // Verify data values
313 cout << "\tVerify data point values." << endl;
314 for (int k=0;k<testData2->getPointDataView().getShape()[2];k++) {
315 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
316 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
317 assert(testData2->getPointDataView()(i,j,k)==pointData(region[0].first+i,
318 region[1].first+j,
319 region[2].first+k));
320 }
321 }
322 }
323
324 cout << "\tVerify data point attributes." << endl;
325 DataArrayView dataView=testData2->getPointDataView();
326 assert(dataView.getRank()==region.size());
327 assert(dataView.noValues()==(region[0].second-region[0].first)
328 *(region[1].second-region[1].first)
329 *(region[2].second-region[2].first));
330 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
331 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
332 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
333
334 cout << "\tTest slicing (part object)." << endl;
335 DataArrayView::RegionType region2;
336 region2.push_back(DataArrayView::RegionType::value_type(1,3));
337 region2.push_back(DataArrayView::RegionType::value_type(1,3));
338 region2.push_back(DataArrayView::RegionType::value_type(1,3));
339 DataAbstract* testData3=testData.getSlice(region2);
340
341 //
342 // Verify data values
343 cout << "\tVerify data point values." << endl;
344 for (int k=0;k<testData3->getPointDataView().getShape()[2];k++) {
345 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
346 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
347 assert(testData3->getPointDataView()(i,j,k)==pointData(region2[0].first+i,
348 region2[1].first+j,
349 region2[2].first+k));
350 }
351 }
352 }
353
354 cout << "\tVerify data point attributes." << endl;
355 dataView=testData2->getPointDataView();
356 assert(dataView.getRank()==region.size());
357 assert(dataView.noValues()==(region[0].second-region[0].first)
358 *(region[1].second-region[1].first)
359 *(region[2].second-region[2].first));
360 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
361 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
362 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
363
364 delete testData2;
365 delete testData3;
366
367 }
368
369
370 void DataExpandedTestCase::testSliceSetting() {
371
372 cout << endl;
373
374 //
375 // Create a rank 2 pointData
376 DataArrayView::ShapeType shape;
377 shape.push_back(2);
378 shape.push_back(2);
379 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
380 DataArrayView pointData(data,shape);
381
382 //
383 // Assign an arbitrary value
384 pointData(0,0)=0.0;
385 pointData(0,1)=1.0;
386 pointData(1,0)=2.0;
387 pointData(1,1)=3.0;
388
389 //
390 // Create object to test
391 cout << "\tCreate rank 2 DataExpanded object." << endl;
392 DataExpanded testData(pointData,FunctionSpace());
393
394 //
395 // Create another rank 2 pointData
396 DataArrayView::ShapeType shape2;
397 shape2.push_back(3);
398 shape2.push_back(3);
399 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
400 DataArrayView pointData2(data2,shape2);
401
402 //
403 // Assign an arbitrary value
404 pointData2(0,0)=0.1;
405 pointData2(0,1)=1.1;
406 pointData2(0,2)=2.1;
407 pointData2(1,0)=3.1;
408 pointData2(1,1)=4.1;
409 pointData2(1,2)=5.1;
410 pointData2(2,0)=6.1;
411 pointData2(2,1)=7.1;
412 pointData2(2,2)=8.1;
413
414 //
415 // Create object to test
416 cout << "\tCreate second rank 2 DataExpanded object." << endl;
417 DataExpanded testData2(pointData2,FunctionSpace());
418
419 cout << "\tTest slice setting (1)." << endl;
420
421 DataArrayView::RegionType region;
422 region.push_back(DataArrayView::RegionType::value_type(0,2));
423 region.push_back(DataArrayView::RegionType::value_type(0,2));
424
425 DataArrayView::RegionType region2;
426 region2.push_back(DataArrayView::RegionType::value_type(1,3));
427 region2.push_back(DataArrayView::RegionType::value_type(1,3));
428
429 DataAbstract* testData3=testData.getSlice(region);
430
431 testData2.setSlice(testData3,region2);
432
433 //
434 // Verify data values
435 cout << "\tVerify data point values." << endl;
436 for (int j=region2[1].first;j<region2[1].second;j++) {
437 for (int i=region2[0].first;i<region2[0].second;i++) {
438 assert(testData2.getPointDataView()(i,j)==pointData(i-(region[0].second-1),j-(region[1].second-1)));
439 }
440 }
441
442 delete testData3;
443
444 }
445
446 void DataExpandedTestCase::testSliceSetting2() {
447
448 cout << endl;
449
450 //
451 // Create a rank 0 pointData
452 DataArrayView::ShapeType shape;
453 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
454 DataArrayView pointData(data,shape);
455
456 //
457 // Assign an arbitrary value
458 pointData()=0.0;
459
460 //
461 // Create object to test
462 cout << "\tCreate rank 0 DataExpanded object." << endl;
463 DataExpanded testData(pointData,FunctionSpace());
464
465 //
466 // Create a rank 2 pointData
467 DataArrayView::ShapeType shape2;
468 shape2.push_back(3);
469 shape2.push_back(3);
470 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
471 DataArrayView pointData2(data2,shape2);
472
473 //
474 // Assign an arbitrary value
475 pointData2(0,0)=0.1;
476 pointData2(0,1)=1.1;
477 pointData2(0,2)=2.1;
478 pointData2(1,0)=3.1;
479 pointData2(1,1)=4.1;
480 pointData2(1,2)=5.1;
481 pointData2(2,0)=6.1;
482 pointData2(2,1)=7.1;
483 pointData2(2,2)=8.1;
484
485 //
486 // Create object to test
487 cout << "\tCreate rank 2 DataExpanded object." << endl;
488 DataExpanded testData2(pointData2,FunctionSpace());
489
490 cout << "\tTest slice setting (1)." << endl;
491
492 DataArrayView::RegionType region;
493
494 DataArrayView::RegionType region2;
495 region2.push_back(DataArrayView::RegionType::value_type(1,1));
496 region2.push_back(DataArrayView::RegionType::value_type(1,1));
497
498 DataAbstract* testData3=testData.getSlice(region);
499
500 testData2.setSlice(testData3,region2);
501
502 //
503 // Verify data values
504 cout << "\tVerify data point values." << endl;
505 for (int j=region2[1].first;j<region2[1].second;j++) {
506 for (int i=region2[0].first;i<region2[0].second;i++) {
507 assert(testData2.getPointDataView()(i,j)==pointData());
508 }
509 }
510
511 delete testData3;
512
513 }
514
515 void DataExpandedTestCase::testRefValue() {
516
517 cout << endl;
518
519 cout << "\tTest DataExpanded refValue methods." << endl;
520
521 //
522 // Create a rank 1 data array to create a DataExpanded object
523 DataArrayView::ShapeType shape;
524 shape.push_back(3);
525 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
526 DataArrayView dataView(data,shape);
527
528 //
529 // Create another DataArray object to pass to refValue methods
530 DataArray data2(dataView);
531
532 //
533 // Create a DataExpanded object
534 dataView(0)=0.0;
535 dataView(1)=1.0;
536 dataView(2)=2.0;
537 DataExpanded dataExp(dataView,FunctionSpace());
538
539 //
540 // Try out ref value methods
541
542 dataExp.getRefValue(1,data2);
543
544 // check the values loaded into data2 by getRefValue
545 // match the values in dataExp
546 assert(data2.getView()(0)==0.0);
547 assert(data2.getView()(1)==1.0);
548 assert(data2.getView()(2)==2.0);
549
550 data2.getView()(0)=3.0;
551 data2.getView()(1)=4.0;
552 data2.getView()(2)=5.0;
553
554 dataExp.setRefValue(1,data2);
555
556 // check the values now in dataExp match the values
557 // that were supplied in data2
558 assert(dataExp.getPointDataView()(0)==3.0);
559 assert(dataExp.getPointDataView()(1)==4.0);
560 assert(dataExp.getPointDataView()(2)==5.0);
561
562 cout << endl;
563
564 }
565
566 TestSuite* DataExpandedTestCase::suite ()
567 {
568 //
569 // Create the suite of tests to perform.
570 TestSuite *testSuite = new TestSuite ("DataExpandedTestCase");
571 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testAll",&DataExpandedTestCase::testAll));
572 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing",&DataExpandedTestCase::testSlicing));
573 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing2",&DataExpandedTestCase::testSlicing2));
574 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing3",&DataExpandedTestCase::testSlicing3));
575 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting",&DataExpandedTestCase::testSliceSetting));
576 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting2",&DataExpandedTestCase::testSliceSetting2));
577 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testRefValue",&DataExpandedTestCase::testRefValue));
578 return testSuite;
579 }

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26