Arh2Reader.cc 10.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/

#include <fstream>
#include <stdint.h>
#include <boost/filesystem.hpp>

#include "Arh2Reader.h"
#include "detail/Arh2Reader.h"
27
28
29
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

namespace AMDiS { namespace io {

  namespace Arh2Reader
  {
    using namespace std;
 
    void readByName(string filename, 
		      DOFVector<double>& vec,
		      bool writeParallel,
		      int nProcs)
    {
      readByName(filename, &vec, writeParallel, nProcs);
    }

    void readByName(string filename, 
		      DOFVector<double>* vec,
		      bool writeParallel,
		      int nProcs)
    {
      vector<DOFVector<double>*> vecs(0);
      vecs.push_back(vec);
52
      detail::readFile(filename, NULL, vecs, writeParallel, nProcs, true);
53
54
55
56
57
58
59
    }
    
    void readByName(string filename,
		    vector<DOFVector<double>*> vecs,
		    bool writeParallel,
		    int nProcs)
    {
60
      detail::readFile(filename, NULL, vecs, writeParallel, nProcs, true);
61
62
63
64
65
66
67
68
69
70
71
72
    }
    
    void readByName(string filename,
		    SystemVector *sysVec,
		    bool writeParallel,
		    int nProcs)
    {
      vector<DOFVector<double>*> vecs(0);
      for(int i = 0 ; i < sysVec->getSize(); i++)
      {
	vecs.push_back(sysVec->getDOFVector(i));
      }
73
      detail::readFile(filename, NULL, vecs, writeParallel, nProcs, true);
74
75
76
77
78
79
80
81
82
83
84
85
    }

    void readFile(string filename, 
			  SystemVector *sysVec, 
			  bool writeParallel,
			  int nProcs)
    {
      vector<DOFVector<double>*> vecs(0);
      for(int i = 0 ; i < sysVec->getSize(); i++)
      {
	vecs.push_back(sysVec->getDOFVector(i));
      }
86
      detail::readFile(filename, NULL, vecs, writeParallel, nProcs);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    }

    void readFile(string filename,
			DOFVector<double>* vec0,
			DOFVector<double>* vec1,
			DOFVector<double>* vec2,
			bool writeParallel,
			int nProcs)
    {
      vector<DOFVector<double>*> vecs(0);
      
      if(vec0 || vec1 || vec2)
	vecs.push_back(vec0);
      if(vec1 || vec2)
	vecs.push_back(vec1);
      if(vec2)
	vecs.push_back(vec2);

105
      detail::readFile(filename, NULL, vecs, writeParallel, nProcs);
106
107
108
109
110
111
112
    }

    void readFile(string filename,
			vector<DOFVector<double>*> vecs,
			bool writeParallel,
			int nProcs)
    {
113
      detail::readFile(filename, NULL, vecs, writeParallel, nProcs);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    }
    
    void readFile(string filename,
		      Mesh* mesh,
		      bool writeParallel,
		      int nProcs)
    {
      vector<DOFVector<double>*> vecs(0);
      detail::readFile(filename, mesh, vecs, writeParallel, nProcs);
    }

    int readNumOfValueVectors(string filename, bool writeParallel)
    {
      FUNCNAME("Arh2Reader::readNumOfValueVectors");
      
      ifstream file;
      
      if(writeParallel) 
      {
	using boost::lexical_cast;
	int sPos = filename.find(".arh");
	TEST_EXIT(sPos >= 0)("Failed to find file postfix!\n");
	string name = filename.substr(0, sPos);
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
	filename = name + "-p" + lexical_cast<string>(MPI::COMM_WORLD.Get_rank()) + "-.arh";
#endif
      }
      
      file.open(filename.c_str(), ios::in | ios::binary);
      TEST_EXIT(file.is_open())
	  ("Cannot open file %s\n", filename.c_str());

      uint32_t nValueVectors = 0;

      detail::firstRead(file);
      long pos = file.tellg();
      file.seekg(pos + 16);
      file.read(reinterpret_cast<char*>(&nValueVectors), 4);
      
      file.close();
      return nValueVectors;
    }

    int readHeaderSize(string filename, bool writeParallel)
    {
      FUNCNAME("Arh2Reader::readHeaderSize");
      
      ifstream file;
      
      if (writeParallel)
      {
	using boost::lexical_cast;
	int sPos = filename.find(".arh");
	TEST_EXIT(sPos >= 0)("Failed to find file postfix!\n");
	string name = filename.substr(0, sPos);
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
	filename = name + "-p" + lexical_cast<string>(MPI::COMM_WORLD.Get_rank()) + "-.arh";
#endif
      }
      
      file.open(filename.c_str(), ios::in | ios::binary);
      TEST_EXIT(file.is_open())
	  ("Cannot open file %s\n", filename.c_str());
    
      uint32_t headerLen = 0;
      detail::firstRead(file);
      file.read(reinterpret_cast<char*>(&headerLen), 4);

      file.close();
      return headerLen;
    }

    bool isReadable(string filename, bool writeParallel)
    {
      FUNCNAME("Arh2Reader::isReadable");
      
      ifstream file;
      
      if(writeParallel)
      {
	using boost::lexical_cast;
	int sPos = filename.find(".arh");
	TEST_EXIT(sPos >= 0)("Failed to find file postfix!\n");
	string name = filename.substr(0, sPos);
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
	filename = name + "-p" + lexical_cast<string>(MPI::COMM_WORLD.Get_rank()) + "-.arh";
#endif
      }
      
      file.open(filename.c_str(), ios::in | ios::binary);
      TEST_EXIT(file.is_open())
	  ("Cannot open file %s\n", filename.c_str());
      
      string typeId(4, ' ');
      uint8_t major = 0, minor = 0;
     
      file.read(const_cast<char*>(typeId.data()), 4);
      file.read(reinterpret_cast<char*>(&major), 1);
      file.read(reinterpret_cast<char*>(&minor), 1);
      
      file.close();
      return (typeId == "arh2" && major == MAJOR && minor <= MINOR) ? true : false;
    }
    
    void readMeta(string filename,
			    DOFVector<double>* vec0,
			    DOFVector<double>* vec1,
			    DOFVector<double>* vec2)
    {
      vector<DOFVector<double>*> vecs;
      if (vec0)
	vecs.push_back(vec0);
      if (vec1)
	vecs.push_back(vec1);
      if (vec2)
	vecs.push_back(vec2);

      readMeta(filename, vecs);
    }
    
    void readMeta(string filename, vector<DOFVector<double>*> vecs)
    {
      FUNCNAME("Arh2Reader::readMeta()");
      
238
      Mesh* mesh = NULL;
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
      for (size_t i = 0; i < vecs.size(); i++)
      {
	if(vecs[i])
	{
	  if(!mesh)
	    mesh = vecs[i]->getFeSpace()->getMesh();
	  else
	    TEST_EXIT(mesh == vecs[i]->getFeSpace()->getMesh())
	      ("The mesh of the DOFVectors should be the same for Reader because in one file there is only one mesh.\n");
	}
      }
      if(!mesh)
      {
	WARNING("You haven't specified the target.\n");
	return;
      }
      // === Read the meta arh file. ===

      string arhPrefix = "";
      map<int, int> elInRank;
      map<int, int> elCodeSize;
      int nProc = readMetaData(filename, elInRank, elCodeSize, arhPrefix);
      

      // === Check which arh files must be read by current rank. ===

      // Set of all file indices which should be read to restore all macro elements.
      std::set<int> readArhFiles;

      TraverseStack stack;
      ElInfo *elInfo = stack.traverseFirst(mesh, 0, Mesh::CALL_EL_LEVEL);
      while (elInfo) {
	int macroElIndex = elInfo->getElement()->getIndex();
	TEST_EXIT(elInRank.count(macroElIndex))("Should not happen!\n");
	readArhFiles.insert(elInRank[macroElIndex]);
	elInfo = stack.traverseNext(elInfo);
      } 


      // === Read the individual arh files. ===

      boost::filesystem::path p(filename.c_str());
      boost::filesystem::path directory = p.parent_path();

      for (std::set<int>::iterator it = readArhFiles.begin();
	  it != readArhFiles.end(); ++it) {
	string arhFilename = directory.string() + "/" + arhPrefix;
	if (nProc == 1)
	  arhFilename += ".arh";
	else
	  arhFilename += "-p" + boost::lexical_cast<string>(*it) + "-.arh";

291
	MSG("ARH2 file read from: %s\n", arhFilename.c_str());
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
	detail::read(arhFilename, mesh, vecs);
      }
    }
    
    int readMetaData(string filename,
				map<int, int> &elInRank,
				map<int, int> &elCodeSize,
				string &arhPrefix)
    {
      FUNCNAME("Arh2Reader::readMetaData()");
      
      ifstream file;
      file.open(filename.c_str());
      TEST_EXIT(file.is_open())
	("Cannot open arh meta file \"%s\"\n", filename.c_str());

      string readStr = "";
      file >> readStr;
      arhPrefix = "";
      file >> arhPrefix;
      int nProc;
      file >> nProc;
314
315
      
      MSG("found nProc %d in file %s \n",nProc,filename.c_str());
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

      // Maps to each macro element index the arh file index it is stored in.
      for (int i = 0; i < nProc; i++) {
	int tmp;
	file >> tmp;
	TEST_EXIT(tmp == i)("Should not happen!\n");
	int nMacroEl;
	file >> nMacroEl;
	for (int j = 0; j < nMacroEl; j++) {
	  int elIndex, codeSize;
	  file >> elIndex;
	  file >> codeSize;
	  elInRank[elIndex] = i;
	  elCodeSize[elIndex] = codeSize;
	}
      }

      file.close();

      return nProc;
    }
    
    int readMetaData(string filename)
    {
      FUNCNAME("Arh2Reader::readMetaData()");

      ifstream file;
      file.open(filename.c_str());
      TEST_EXIT(file.is_open())
	("Cannot open arh meta file \"%s\"\n", filename.c_str());

      string readStr = "";
      file >> readStr;
      file >> readStr;
      int nProc;
      file >> nProc;
      file.close();
      return nProc;
    }
355
356
357

    int readMetaFromArh(std::string filename,
			std::map<int, int> &elInRank,
358
359
			std::map<int, int> &elCodeSize)
    {
360
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS      
361
      int nProcs = MPI::COMM_WORLD.Get_size();
362
363
364
365
366
367
#else
      int nProcs = -1;
#endif
      
      // check for consistency
      // That is, if there are no more or less files as nProcs
368
369
      int i = 0;
      for (; i < nProcs + 1; i++) {
370
371
372
373
374
	string fn = filename + "-p" + boost::lexical_cast<string>(i) + "-.arh";
	if(!boost::filesystem::exists(fn)) break;
      }
      TEST_EXIT(i == nProcs)
	("Number of arh files doesn't match number of processors \n");
375
376
377
378
      
      // data format: (rank ; (elIndex; elCodeSize) )  
      vector<std::set<pair<int, int> > > data(nProcs); 
      
379
380
381
      //collect data      
      for (int i = 0; i < nProcs; i++) {
	string fn = filename + "-p" + boost::lexical_cast<string>(i) + "-.arh";
382
	detail::readMetaFromSgArh(fn, i, data);
383
      }
384
      
385
386
387
388
389
390
391
392
393
394
      //make elInRank and elCodeSize-Map
      for (int i = 0; i < nProcs; i++) {
	for (std::set<pair<int, int> >::iterator it = data[i].begin();
	     it != data[i].end(); ++it){
	  elInRank[it->first]=i;
	  elCodeSize[it->first]=it->second;
	}
      }
      return nProcs;
    }
395
396
    
  } // end namespace Arh2Reader
397
  } } // end namespace io, AMDiS