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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6812 - (show annotations)
Fri May 3 00:36:47 2019 UTC (6 months, 1 week ago) by aellery
File size: 8740 byte(s)
Another silo fix


1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2018 by The University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Apache License, version 2.0
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 *
15 *****************************************************************************/
16
17 #include "EscriptDatasetTestCase.h"
18
19 #include <weipa/EscriptDataset.h>
20 #include <weipa/WeipaException.h>
21
22 #include <escript/DataFactory.h>
23 #include <escript/FunctionSpaceFactory.h>
24
25 #if USE_DUDLEY
26 #include <dudley/DomainFactory.h>
27 #endif
28 #if USE_FINLEY
29 #include <finley/DomainFactory.h>
30 #endif
31 #if USE_RIPLEY
32 #include <ripley/Brick.h>
33 #endif
34 #if USE_SPECKLEY
35 #include <speckley/Brick.h>
36 #endif
37
38 #include <cppunit/TestCaller.h>
39
40 using namespace CppUnit;
41 using namespace escript;
42 using namespace weipa;
43 using namespace std;
44
45 TestSuite* EscriptDatasetTestCase::suite()
46 {
47 TestSuite *testSuite = new TestSuite("EscriptDatasetTestCase");
48 testSuite->addTest(new TestCaller<EscriptDatasetTestCase>(
49 "testBase",&EscriptDatasetTestCase::testBase));
50 #if USE_DUDLEY
51 testSuite->addTest(new TestCaller<EscriptDatasetTestCase>(
52 "testDudley",&EscriptDatasetTestCase::testDudley));
53 #endif
54 #if USE_FINLEY
55 testSuite->addTest(new TestCaller<EscriptDatasetTestCase>(
56 "testFinley",&EscriptDatasetTestCase::testFinley));
57 #endif
58 #if USE_RIPLEY
59 testSuite->addTest(new TestCaller<EscriptDatasetTestCase>(
60 "testRipley",&EscriptDatasetTestCase::testRipley));
61 #endif
62 #if USE_SPECKLEY
63 testSuite->addTest(new TestCaller<EscriptDatasetTestCase>(
64 "testSpeckley",&EscriptDatasetTestCase::testSpeckley));
65 #endif
66 return testSuite;
67 }
68
69 void EscriptDatasetTestCase::testBase()
70 {
71 cout << endl;
72 cout << "\tTest default constructor." << endl;
73 EscriptDataset_ptr dataset(new EscriptDataset());
74
75 cout << "\tTest saveSilo without data." << endl;
76 CPPUNIT_ASSERT(dataset->saveSilo("dummy") == false);
77
78 cout << "\tTest saveVTK without data." << endl;
79 CPPUNIT_ASSERT_THROW(dataset->saveVTK("dummy"),weipa::WeipaException);
80
81 cout << "\tTest getConvertedDomain without data." << endl;
82 CPPUNIT_ASSERT(dataset->getConvertedDomain().size() == 0);
83
84 cout << "\tTest getVariables without data." << endl;
85 CPPUNIT_ASSERT(dataset->getVariables().size() == 0);
86
87 cout << "\tTest getMeshVariables without data." << endl;
88 CPPUNIT_ASSERT(dataset->getMeshVariables().size() == 0);
89 }
90
91 #if USE_DUDLEY
92 void EscriptDatasetTestCase::testDudley()
93 {
94 JMPI info=makeInfo(MPI_COMM_WORLD);
95 Domain_ptr dom(dudley::brick(info));
96 cout << "Running Dudley tests..." << endl;
97 runDomainTests(dom);
98 }
99 #endif
100
101 #if USE_FINLEY
102 void EscriptDatasetTestCase::testFinley()
103 {
104 JMPI info=makeInfo(MPI_COMM_WORLD);
105 Domain_ptr dom(finley::brick(info));
106 cout << "Running Finley tests..." << endl;
107 runDomainTests(dom);
108 }
109 #endif
110
111 #if USE_RIPLEY
112 void EscriptDatasetTestCase::testRipley()
113 {
114 Domain_ptr dom(new ripley::Brick(5,4,3, 0,0,0, 1,1,1));
115 cout << "Running Ripley tests..." << endl;
116 runDomainTests(dom);
117 }
118 #endif
119
120 #if USE_SPECKLEY
121 void EscriptDatasetTestCase::testSpeckley()
122 {
123 for (int i = 2; i < 11; i++) {
124 Domain_ptr dom(new speckley::Brick(i, 5,4,3, 0,0,0, 1,1,1));
125 cout << "Running Speckley order " << i << " tests..." << endl;
126 runDomainTests(dom);
127 }
128 }
129 #endif
130
131 void EscriptDatasetTestCase::runDomainTests(Domain_ptr dom)
132 {
133 EscriptDataset_ptr dataset(new EscriptDataset());
134 Data data = Scalar(0.0, continuousFunction(*dom), true);
135
136 cout << "\tTest addData with NULL domain." << endl;
137 CPPUNIT_ASSERT(dataset->addData(data, "foo", "bar") == false);
138
139 cout << "\tTest setDomain." << endl;
140 CPPUNIT_ASSERT(dataset->setDomain(dom.get()) == true);
141 CPPUNIT_ASSERT(dataset->getMeshVariables().size() > 0);
142
143 cout << "\tTest bogus setDomain call." << endl;
144 CPPUNIT_ASSERT(dataset->setDomain(dom.get()) == false);
145
146 cout << "\tTest getConvertedDomain." << endl;
147 DomainChunks chunks = dataset->getConvertedDomain();
148 CPPUNIT_ASSERT(chunks.size() > 0);
149
150 StringVec varfiles, varnames;
151 varfiles.push_back("testvar%04d.nc");
152 varnames.push_back("testvar");
153 cout << "\tTest bogus loadNetCDF call 1." << endl;
154 CPPUNIT_ASSERT(dataset->loadNetCDF("mesh%04d.nc", varfiles, varnames, 1) == false);
155
156 cout << "\tTest bogus loadNetCDF call 2." << endl;
157 CPPUNIT_ASSERT(dataset->loadNetCDF(chunks, varfiles, varnames) == false);
158
159 cout << "\tTest addData with valid data." << endl;
160 CPPUNIT_ASSERT(dataset->addData(data, "testvar", "cm") == true);
161 CPPUNIT_ASSERT(dataset->getVariables().size() == 1);
162
163 cout << "\tTest set/getCycleAndTime." << endl;
164 dataset->setCycleAndTime(42, 3.1415);
165 CPPUNIT_ASSERT(dataset->getCycle() == 42);
166 CPPUNIT_ASSERT(dataset->getTime()-3.1415 < 0.001);
167
168 dataset->setMetadataSchemaString("xmlns:test=\"http://myschema.com/test\"",
169 "<MyValue>4711</MyValue>");
170 dataset->setMeshLabels("x-axis", "y-axis", "z-axis");
171 dataset->setMeshUnits("km", "cm", "mm");
172 dataset->setSaveMeshData(true);
173
174 #ifdef ESYS_HAVE_SILO
175 cout << "\tTest saveSilo." << endl;
176 CPPUNIT_ASSERT(dataset->saveSilo("domaintest.silo") == true);
177 ifstream f("domaintest.silo");
178 CPPUNIT_ASSERT(f.is_open());
179 f.close();
180 #endif
181
182 cout << "\tTest saveVTK." << endl;
183 CPPUNIT_ASSERT_NO_THROW(dataset->saveVTK("domaintest.vtu"));
184 checkVTKfile("domaintest.vtu");
185 }
186
187 int EscriptDatasetTestCase::getDataArrayLength(std::istream& is)
188 {
189 int length=0;
190 char line[256];
191 while (is.good()) {
192 is.getline(line, 256);
193 string s(line);
194 if (s.find("</DataArray") != 0)
195 length++;
196 else
197 break;
198 }
199 return length;
200 }
201
202 void EscriptDatasetTestCase::checkVTKfile(std::string filename)
203 {
204 ifstream f(filename.c_str());
205 CPPUNIT_ASSERT(f.is_open());
206
207 char line[256];
208 int numPoints=0, numCells=0;
209 while (f.good()) {
210 f.getline(line, 256);
211 string s(line);
212 size_t pp = s.find("NumberOfPoints=");
213 size_t cp = s.find("NumberOfCells=");
214 if (pp!=s.npos && cp!=s.npos) {
215 stringstream ss;
216 string tmp(s.substr(pp+16));
217 ss.str(tmp);
218 ss >> numPoints;
219 tmp = s.substr(cp+15);
220 ss.str(tmp);
221 ss >> numCells;
222 break;
223 }
224 }
225 CPPUNIT_ASSERT(numPoints>0);
226 CPPUNIT_ASSERT(numCells>0);
227
228 bool pointsFound=false, cellsFound=false;
229 int numPointData=0, numCellData=0;
230
231 while (f.good()) {
232 f.getline(line, 256);
233 string s(line);
234 if (s.compare("<Points>") == 0) {
235 pointsFound=true;
236 // check node coordinates
237 while (f.good() && s.find("</Points>") != 0) {
238 f.getline(line, 256);
239 s = line;
240 if (s.find("<DataArray") == 0) {
241 CPPUNIT_ASSERT_EQUAL(numPoints, getDataArrayLength(f));
242 }
243 }
244 } else if (s.find("<Cells>") == 0) {
245 cellsFound=true;
246 // check cell info (connectivity, offsets, types)
247 while (f.good() && s.find("</Cells>") != 0) {
248 f.getline(line, 256);
249 s = line;
250 if (s.find("<DataArray") == 0) {
251 CPPUNIT_ASSERT_EQUAL(numCells, getDataArrayLength(f));
252 }
253 }
254 } else if (s.compare("<PointData>") == 0) {
255 // check nodal data
256 while (f.good() && s.find("</PointData>") != 0) {
257 f.getline(line, 256);
258 s = line;
259 if (s.find("<DataArray") == 0) {
260 numPointData++;
261 CPPUNIT_ASSERT_EQUAL(numPoints, getDataArrayLength(f));
262 }
263 }
264 } else if (s.find("<CellData>") == 0) {
265 // check cell data
266 while (f.good() && s.find("</CellData>") != 0) {
267 f.getline(line, 256);
268 s = line;
269 if (s.find("<DataArray") == 0) {
270 numCellData++;
271 CPPUNIT_ASSERT_EQUAL(numCells, getDataArrayLength(f));
272 }
273 }
274 }
275 }
276
277 CPPUNIT_ASSERT(pointsFound);
278 CPPUNIT_ASSERT(cellsFound);
279 CPPUNIT_ASSERT(numPointData>0);
280 CPPUNIT_ASSERT(numCellData>0);
281 }
282

  ViewVC Help
Powered by ViewVC 1.1.26