/[escript]/trunk/weipa/test/EscriptDatasetTestCase.cpp
ViewVC logotype

Contents of /trunk/weipa/test/EscriptDatasetTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3344 - (show annotations)
Thu Nov 11 23:26:52 2010 UTC (10 years, 8 months ago) by caltinay
File size: 6822 byte(s)
Phew!
-escript, finley, and dudley now uses weipa's saveVTK implementation
-moved tests from finley to weipa accordingly; dudley still to do
-rebaselined all test files
-fixed a few issues in weipa.saveVTK, e.g. saving metadata without schema
-added a deprecation warning to esys.escript.util.saveVTK
-todo: change doco, tests and other places to use weipa.saveVTK


1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 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/DataFactory.h"
16 #include "finley/CppAdapter/MeshAdapterFactory.h"
17 #include "weipa/EscriptDataset.h"
18 #include "EscriptDatasetTestCase.h"
19
20 using namespace CppUnitTest;
21 using namespace escript;
22 using namespace weipa;
23 using namespace std;
24
25 void EscriptDatasetTestCase::setUp()
26 {
27 // This is called before each test is run
28 }
29
30 void EscriptDatasetTestCase::tearDown()
31 {
32 // This is called after each test has been run
33 }
34
35 void EscriptDatasetTestCase::testAll()
36 {
37 cout << endl;
38 cout << "\tTest default constructor." << endl;
39 EscriptDataset_ptr dataset(new EscriptDataset());
40
41 cout << "\tTest saveSilo without data." << endl;
42 assert(dataset->saveSilo("dummy") == false);
43
44 cout << "\tTest saveVTK without data." << endl;
45 assert(dataset->saveVTK("dummy") == false);
46
47 cout << "\tTest getConvertedDomain without data." << endl;
48 assert(dataset->getConvertedDomain().size() == 0);
49
50 cout << "\tTest getVariables without data." << endl;
51 assert(dataset->getVariables().size() == 0);
52
53 cout << "\tTest getMeshVariables without data." << endl;
54 assert(dataset->getMeshVariables().size() == 0);
55
56 // instantiate a domain and data
57 Domain_ptr dom(finley::brick());
58 escript::Data data = Scalar(0.0, continuousFunction(*dom), true);
59
60 cout << "\tTest addData with NULL domain." << endl;
61 assert(dataset->addData(data, "foo", "bar") == false);
62
63 cout << "\tTest setDomain." << endl;
64 assert(dataset->setDomain(dom.get()) == true);
65 assert(dataset->getMeshVariables().size() > 0);
66
67 cout << "\tTest bogus setDomain call." << endl;
68 assert(dataset->setDomain(dom.get()) == false);
69
70 cout << "\tTest getConvertedDomain." << endl;
71 DomainChunks chunks = dataset->getConvertedDomain();
72 assert(chunks.size() > 0);
73
74 StringVec varfiles, varnames;
75 varfiles.push_back("testvar%04d.nc");
76 varnames.push_back("testvar");
77 cout << "\tTest bogus loadNetCDF call 1." << endl;
78 assert(dataset->loadNetCDF("mesh%04d.nc", varfiles, varnames, 1) == false);
79
80 cout << "\tTest bogus loadNetCDF call 2." << endl;
81 assert(dataset->loadNetCDF(chunks, varfiles, varnames) == false);
82
83 cout << "\tTest addData with valid data." << endl;
84 assert(dataset->addData(data, "testvar", "cm") == true);
85 assert(dataset->getVariables().size() == 1);
86
87 cout << "\tTest set/getCycleAndTime." << endl;
88 dataset->setCycleAndTime(42, 3.1415);
89 assert(dataset->getCycle() == 42);
90 assert(dataset->getTime()-3.1415 < 0.001);
91
92 dataset->setMetadataSchemaString("xmlns:test=\"http://myschema.com/test\"",
93 "<MyValue>4711</MyValue>");
94 dataset->setMeshLabels("x-axis", "y-axis", "z-axis");
95 dataset->setMeshUnits("km", "cm", "mm");
96
97 #if USE_SILO
98 cout << "\tTest saveSilo." << endl;
99 assert(dataset->saveSilo("weipatest.silo") == true);
100 ifstream f("weipatest.silo");
101 assert(f.is_open());
102 f.close();
103 #endif
104
105 cout << "\tTest saveVTK." << endl;
106 assert(dataset->saveVTK("weipatest.vtu") == true);
107 checkVTKfile("weipatest.vtu");
108
109 //varnames.push_back("dummy");
110 //cout << "\tTest loadNetCDF with invalid params." << endl;
111 //assert(dataset->loadNetCDF(blocks, varfiles, varnames) == false);
112 }
113
114 TestSuite* EscriptDatasetTestCase::suite()
115 {
116 //
117 // create the suite of tests to perform.
118 TestSuite *testSuite = new TestSuite("EscriptDatasetTestCase");
119
120 testSuite->addTest(new TestCaller<EscriptDatasetTestCase>(
121 "testAll",&EscriptDatasetTestCase::testAll));
122 return testSuite;
123 }
124
125 int EscriptDatasetTestCase::getDataArrayLength(std::istream& is)
126 {
127 int length=0;
128 char line[256];
129 while (is.good()) {
130 is.getline(line, 256);
131 string s(line);
132 if (s.find("</DataArray") != 0)
133 length++;
134 else
135 break;
136 }
137 return length;
138 }
139
140 void EscriptDatasetTestCase::checkVTKfile(std::string filename)
141 {
142 ifstream f(filename.c_str());
143 assert(f.is_open());
144
145 char line[256];
146 int numPoints=0, numCells=0;
147 while (f.good()) {
148 f.getline(line, 256);
149 string s(line);
150 size_t pp = s.find("NumberOfPoints=");
151 size_t cp = s.find("NumberOfCells=");
152 if (pp!=s.npos && cp!=s.npos) {
153 stringstream ss;
154 string tmp(s.substr(pp+16));
155 ss.str(tmp);
156 ss >> numPoints;
157 tmp = s.substr(cp+15);
158 ss.str(tmp);
159 ss >> numCells;
160 break;
161 }
162 }
163 assert(numPoints>0);
164 assert(numCells>0);
165
166 bool pointsFound=false, cellsFound=false;
167 int numPointData=0, numCellData=0;
168
169 while (f.good()) {
170 f.getline(line, 256);
171 string s(line);
172 if (s.compare("<Points>") == 0) {
173 pointsFound=true;
174 // check node coordinates
175 while (f.good() && s.find("</Points>") != 0) {
176 f.getline(line, 256);
177 s = line;
178 if (s.find("<DataArray") == 0) {
179 assertLongsEqual(numPoints, getDataArrayLength(f));
180 }
181 }
182 } else if (s.find("<Cells>") == 0) {
183 cellsFound=true;
184 // check cell info (connectivity, offsets, types)
185 while (f.good() && s.find("</Cells>") != 0) {
186 f.getline(line, 256);
187 s = line;
188 if (s.find("<DataArray") == 0) {
189 assertLongsEqual(numCells, getDataArrayLength(f));
190 }
191 }
192 } else if (s.compare("<PointData>") == 0) {
193 // check nodal data
194 while (f.good() && s.find("</PointData>") != 0) {
195 f.getline(line, 256);
196 s = line;
197 if (s.find("<DataArray") == 0) {
198 numPointData++;
199 assertLongsEqual(numPoints, getDataArrayLength(f));
200 }
201 }
202 } else if (s.find("<CellData>") == 0) {
203 // check cell data
204 while (f.good() && s.find("</CellData>") != 0) {
205 f.getline(line, 256);
206 s = line;
207 if (s.find("<DataArray") == 0) {
208 numCellData++;
209 assertLongsEqual(numCells, getDataArrayLength(f));
210 }
211 }
212 }
213 }
214
215 assert(pointsFound);
216 assert(cellsFound);
217 assert(numPointData>0);
218 assert(numCellData>0);
219 }
220

  ViewVC Help
Powered by ViewVC 1.1.26