/[escript]/trunk/tools/escriptconvert/escriptconvert.cpp
ViewVC logotype

Contents of /trunk/tools/escriptconvert/escriptconvert.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3091 - (show annotations)
Fri Aug 13 07:47:57 2010 UTC (10 years, 1 month ago) by caltinay
File size: 7186 byte(s)
-fixed cleanup on error when invoking escriptconvert in parallel
-fixed saving VTK files on one rank if data was loaded in chunks
-cleaned up EscriptDataset and split a couple of methods

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 #include <weipa/EscriptDataset.h>
15 #include <weipa/FinleyMesh.h>
16 #include <weipa/DataVar.h>
17
18 #if USE_SILO
19 #include <silo.h>
20 #endif
21
22 #include <cstring>
23 #include <fstream>
24 #include <iostream>
25 #include <sstream>
26
27 using namespace std;
28 using namespace weipa;
29
30 string insertTimestep(const string& fString, int timeStep, int tsMultiplier)
31 {
32 string s(fString);
33 size_t pos;
34 if ((pos = s.find("%")) != s.npos) {
35 size_t endpos = pos+1;
36 while (endpos<s.length() && s[endpos] != 'd')
37 endpos++;
38 string fmtStr = s.substr(pos, endpos-pos+1);
39 char ts[255];
40 snprintf(ts, 255, fmtStr.c_str(), timeStep*tsMultiplier);
41 s.replace(pos, endpos-pos+1, ts);
42 }
43 return s;
44 }
45
46 int usage()
47 {
48 #if USE_SILO
49 cerr << "Usage: escriptconvert {-vtk|-silo} <file.esd>" << endl;
50 #else
51 cerr << "Note: escriptconvert was compiled without Silo support!" << endl;
52 cerr << "Usage: escriptconvert [-vtk] <file.esd>" << endl;
53 #endif
54 fflush(stderr);
55 return -1;
56 }
57
58 void cleanup()
59 {
60 #if HAVE_MPI
61 MPI_Finalize();
62 #endif
63 }
64
65 int main(int argc, char** argv)
66 {
67 #if HAVE_MPI
68 MPI_Init(&argc, &argv);
69 #endif
70
71 // turn off for debugging purposes
72 bool writeMultiMesh = true;
73
74 // whether time-varying datasets should use the same mesh (from T=0)
75 // TODO: Add a command line option for this
76 bool writeMeshOnce = true;
77 bool doVTK = false, doSilo = false;
78 string esdFile;
79
80 #if USE_SILO
81 if (argc != 3) {
82 cleanup();
83 return usage();
84 }
85
86 if (!strcmp(argv[1], "-vtk")) {
87 doVTK = true;
88 } else if (!strcmp(argv[1], "-silo")) {
89 doSilo = true;
90 } else {
91 cleanup();
92 return usage();
93 }
94 esdFile = string(argv[2]);
95
96 #else // !USE_SILO
97 if (argc == 2) {
98 esdFile = string(argv[1]);
99 } else if (argc == 3) {
100 if (strcmp(argv[1], "-vtk")) {
101 cleanup();
102 return usage();
103 }
104 esdFile = string(argv[2]);
105 } else {
106 cleanup();
107 return usage();
108 }
109 doVTK = true;
110 #endif
111
112 ifstream in(esdFile.c_str());
113 if (!in.is_open()) {
114 cerr << "Could not open " << esdFile << "." << endl;
115 cleanup();
116 return -1;
117 }
118
119 // first line (header) should be "#escript datafile Vx.y"
120 char line[256];
121 in.getline(line, 256);
122 int major, minor;
123 if (sscanf(line, "#escript datafile V%d.%d", &major, &minor) != 2) {
124 cerr << esdFile << " is not a valid escript datafile." << endl;
125 in.close();
126 cleanup();
127 return -1;
128 }
129
130 int nParts=0, nTimesteps=1, tsMultiplier=1;
131 string meshFile;
132 StringVec varFiles;
133 StringVec varNames;
134
135 while (in.good()) {
136 in.getline(line, 256);
137 if (line[0] == '#' || strlen(line) == 0)
138 continue;
139 int iVal;
140 char sVal[256];
141 if (sscanf(line, "N=%d", &iVal) == 1)
142 nParts = iVal;
143 else if (sscanf(line, "T=%d", &iVal) == 1)
144 nTimesteps = iVal;
145 else if (sscanf(line, "DT=%d", &iVal) == 1)
146 tsMultiplier = iVal;
147 else if (sscanf(line, "M=%s", sVal) == 1)
148 meshFile = sVal;
149 else if (sscanf(line, "V=%s", sVal) == 1 && strchr(sVal, ':')) {
150 // split into filename and variable name
151 char* colon = strchr(sVal, ':');
152 *colon = 0;
153 varFiles.push_back(sVal);
154 varNames.push_back(colon+1);
155 } else {
156 cerr << esdFile << " is not a valid escript datafile." << endl;
157 in.close();
158 cleanup();
159 return -1;
160 }
161 }
162
163 in.close();
164
165 if (nParts < 1 || meshFile == "" || nTimesteps < 1 || tsMultiplier < 1) {
166 cerr << esdFile << " is not a valid escript datafile." << endl;
167 cleanup();
168 return -1;
169 }
170
171 cout << "Converting " << esdFile << "..." << endl;
172
173 MeshBlocks meshFromTzero;
174
175 // load and save all timesteps
176 for (int timeStep = 0; timeStep < nTimesteps; timeStep++) {
177 StringVec varFilesTS;
178 StringVec::const_iterator it;
179
180 // look for "%d" in filename and replace by timestep*multiplier if found
181 for (it=varFiles.begin(); it!=varFiles.end(); it++) {
182 string v = insertTimestep(*it, timeStep, tsMultiplier);
183 if (nParts > 1)
184 v.append(".nc.%04d");
185 else
186 v.append(".nc");
187 varFilesTS.push_back(v);
188 }
189
190 if (nTimesteps > 1)
191 cout << "T = " << timeStep << endl;
192
193 EscriptDataset* ds;
194 #if HAVE_MPI
195 ds = new EscriptDataset(MPI_COMM_WORLD);
196 #else
197 ds = new EscriptDataset();
198 #endif
199
200 if (writeMeshOnce && timeStep > 0) {
201 if (!ds->loadNetCDF(meshFromTzero, varFilesTS, varNames)) {
202 delete ds;
203 break;
204 }
205 } else {
206 string meshTS = insertTimestep(meshFile, timeStep, tsMultiplier);
207 if (nParts > 1)
208 meshTS.append(".nc.%04d");
209 else
210 meshTS.append(".nc");
211
212 if (!ds->loadNetCDF(meshTS, varFilesTS, varNames, nParts)) {
213 delete ds;
214 break;
215 }
216 }
217
218 string baseName(esdFile);
219 size_t dot = baseName.rfind('.');
220 if (dot != baseName.npos)
221 baseName.erase(dot, baseName.length()-dot);
222
223 ds->setCycleAndTime(timeStep, (double)timeStep);
224
225 ostringstream outFilename;
226 outFilename << baseName;
227 if (nTimesteps > 1)
228 outFilename << "." << timeStep;
229 if (doSilo) {
230 outFilename << ".silo";
231 ds->saveSilo(outFilename.str(), writeMultiMesh);
232 } else {
233 outFilename << ".vtu";
234 ds->saveVTK(outFilename.str());
235 }
236
237 // keep mesh from first timestep if it should be reused
238 if (writeMeshOnce && nTimesteps > 1 && timeStep == 0) {
239 meshFromTzero = ds->getConvertedDomain();
240 meshFile = outFilename.str();
241 MeshBlocks::iterator meshIt;
242 if (doSilo) {
243 for (meshIt = meshFromTzero.begin();
244 meshIt != meshFromTzero.end();
245 meshIt++)
246 {
247 // Prepend Silo mesh paths with the filename of the mesh
248 // to be used
249 string fullSiloPath = meshFile + string(":");
250 fullSiloPath += (*meshIt)->getSiloPath();
251 (*meshIt)->setSiloPath(fullSiloPath);
252 }
253 }
254 }
255
256 delete ds;
257 }
258
259 // clean up
260 MeshBlocks::iterator meshIt;
261
262 cout << "All done." << endl;
263 cleanup();
264
265 return 0;
266 }
267

  ViewVC Help
Powered by ViewVC 1.1.26