FileWriter.cc 8.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
#include "boost/lexical_cast.hpp"
14
#include "FileWriter.h"
15
#include "Initfile.h"
16
17
18
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
19
#include "PngWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
20
#include "PovrayWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
21
#include "DofWriter.h"
22
#include "ArhWriter.h"
23
24
25
26
27
28
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"

29
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
30
#include <mpi.h>
31
32
#endif

33
34
namespace AMDiS {

35
36
  using boost::lexical_cast;

Thomas Witkowski's avatar
Thomas Witkowski committed
37
  FileWriter::FileWriter(std::string str, Mesh *m, DOFVector<double> *vec)
38
39
    : name(str),
      mesh(m)
40
  {
41
42
43
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
44

45
    feSpace = vec->getFeSpace();
46

Thomas Witkowski's avatar
Thomas Witkowski committed
47
48
    solutionVecs.resize(1);
    solutionVecs[0] = vec;
49
50
  }

51

Thomas Witkowski's avatar
Thomas Witkowski committed
52
53
  FileWriter::FileWriter(std::string name_, 
			 Mesh *mesh_, 
Thomas Witkowski's avatar
Thomas Witkowski committed
54
			 std::vector<DOFVector<double>*> vecs)
55
56
57
58
59
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

60
    initialize();
61

62
    for (int i = 0; i < static_cast<int>(vecs.size()); i++)
63
64
      TEST_EXIT(vecs[0]->getFeSpace() == vecs[i]->getFeSpace())
	("All FeSpace have to be equal!\n");
65

66
    feSpace = vecs[0]->getFeSpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
67
    solutionVecs = vecs;
68
69
  }

70

Thomas Witkowski's avatar
Thomas Witkowski committed
71
  FileWriter::FileWriter(std::string name_, 
72
			 Mesh *mesh_,
Thomas Witkowski's avatar
Thomas Witkowski committed
73
			 DOFVector<WorldVector<double> > *vec)
74
75
76
77
78
79
80
81
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
    
    // Create the temporal DOFVectors for all components of WorldVector.
Thomas Witkowski's avatar
Thomas Witkowski committed
82
83
84
    nTmpSolutions = (*vec)[0].getSize();
    solutionVecs.resize(nTmpSolutions);
    for (int i = 0; i < nTmpSolutions; i++)
85
      solutionVecs[i] = new DOFVector<double>(vec->getFeSpace(), "");
86
87
88
89
90

    // Copy the components of the WorldVectors to different DOFVectors
    // of double values.
    DOFVector< WorldVector<double> >::Iterator it(vec, USED_DOFS);
    int counter = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
91
92
93
    for (it.reset(); !it.end(); ++it, counter++)
      for (int i = 0; i < nTmpSolutions; i++)
	(*solutionVecs[i])[counter] = (*it)[i];
94

95
    feSpace = vec->getFeSpace();
96
97
98
99
100
101
102
  }


  FileWriter::~FileWriter()
  {
    // Do not forget to delete temporal solution vector, if there have been
    // some created in the constructor.
Thomas Witkowski's avatar
Thomas Witkowski committed
103
104
105
    if (nTmpSolutions > 0)
      for (int i = 0; i < nTmpSolutions; i++)
        delete solutionVecs[i]; 
106
107
108
109
110
111
112
  }
  

  void FileWriter::initialize()
  {
    amdisMeshExt = ".mesh";
    amdisDataExt = ".dat";
Thomas Witkowski's avatar
Thomas Witkowski committed
113
114
    paraviewFileExt = ".vtu";
    paraviewParallelFileExt = ".pvtu";
115
116
117
    periodicFileExt = ".per";
    writeAMDiSFormat = 0;
    writeParaViewFormat = 0;
118
    writeParaViewAnimation = 0;
119
    writePeriodicFormat = 0;
120
    writePngFormat = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
121
    writePovrayFormat = 0;
122
    writeDofFormat = 0;
123
    writeArhFormat = 0;
124
    pngType = 0;
125
126
127
128
    appendIndex = 0;
    indexLength = 5;
    indexDecimals = 3;
    tsModulo = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
129
130
    nTmpSolutions = 0;
    paraviewAnimationFrames.resize(0),
131
    compression = NONE;
132
133

    readParameters();
134
135
  }

136

137
138
139
140
  void FileWriter::readParameters()
  {
    FUNCNAME("FileWriter::readParamters()");

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    Parameters::get(name + "->filename", filename);
    Parameters::get(name + "->AMDiS format", writeAMDiSFormat);
    Parameters::get(name + "->AMDiS mesh ext", amdisMeshExt);
    Parameters::get(name + "->AMDiS data ext", amdisDataExt);
    Parameters::get(name + "->ParaView format", writeParaViewFormat);
    Parameters::get(name + "->ParaView animation", writeParaViewAnimation);
    Parameters::get(name + "->ParaView ext", paraviewFileExt);    
    Parameters::get(name + "->Periodic format", writePeriodicFormat);
    Parameters::get(name + "->Periodic ext", periodicFileExt);
    Parameters::get(name + "->PNG format", writePngFormat);
    Parameters::get(name + "->PNG type", pngType);
    Parameters::get(name + "->append index", appendIndex);
    Parameters::get(name + "->index length", indexLength);
    Parameters::get(name + "->index decimals", indexDecimals);
    Parameters::get(name + "->write every i-th timestep", tsModulo);

    Parameters::get(name + "->Povray format", writePovrayFormat);
    Parameters::get(name + "->Povray template", povrayTemplate);
    Parameters::get(name + "->Povray camera location", povrayCameraLocation);
    Parameters::get(name + "->Povray camera look_at", povrayCameraLookAt);

    Parameters::get(name + "->DOF format", writeDofFormat);
    Parameters::get(name + "->ARH format", writeArhFormat);
Thomas Witkowski's avatar
Thomas Witkowski committed
164

165
    std::string compressionStr = "";
166
    Parameters::get(name + "->compression", compressionStr);
167
    if (compressionStr == "gzip" || compressionStr == "gz") {
168
      compression = GZIP;
169
    } else if (compressionStr == "bzip2" || compressionStr == "bz2") {
170
171
      compression = BZIP2;
    }
172
173
  }

174

175
176
177
178
179
180
181
  void FileWriter::writeFiles(AdaptInfo *adaptInfo,
			      bool force,
			      int level,
			      Flag flag,
			      bool (*writeElem)(ElInfo*))
  {
    FUNCNAME("FileWriter::writeFiles()");
182

183
184
185
    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
      return;

186
    // Containers, which store the data to be written;
187
    std::vector<DataCollector*> dataCollectors(solutionVecs.size());
188

Thomas Witkowski's avatar
Thomas Witkowski committed
189
    if (writeElem) {
190
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
191
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
192
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
193
    } else {
194
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
195
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
196
197
198
					      traverseLevel, 
					      flag | traverseFlag, 
					      writeElement);
Thomas Witkowski's avatar
Thomas Witkowski committed
199
200
    }

201
    std::string fn = filename;
202

203
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
204
    std::string paraFilename = fn;
205
    fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
Thomas Witkowski's avatar
Thomas Witkowski committed
206
    std::string postfix = "";
207
208
#endif

209
210
211
212
213
214
215
216
    if (appendIndex) {
      TEST_EXIT(indexLength <= 99)("index lenght > 99\n");
      TEST_EXIT(indexDecimals <= 97)("index decimals > 97\n");
      TEST_EXIT(indexDecimals < indexLength)("index length <= index decimals\n");
    
      char formatStr[9];
      char timeStr[20];

Thomas Witkowski's avatar
Thomas Witkowski committed
217
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
218
219
220
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
221
222
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
223
      postfix += timeStr + paraviewFileExt;
Thomas Witkowski's avatar
Thomas Witkowski committed
224
#endif
225
226
227
    }

    if (writeAMDiSFormat) {
228
      MacroWriter::writeMacro(dataCollectors[0], 
229
230
			      const_cast<char*>((fn +  amdisMeshExt).c_str()), 
			      adaptInfo ? adaptInfo->getTime() : 0.0);
231
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());
232
      
233
      ValueWriter::writeValues(dataCollectors[0],
234
235
236
237
238
239
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());   
    }

    if (writePeriodicFormat) {
240
      MacroWriter::writePeriodicFile(dataCollectors[0], 
241
242
243
244
245
				     (fn + periodicFileExt).c_str());
      MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
    }
    
    if (writeParaViewFormat) {
246
      VtkWriter vtkWriter(&dataCollectors);     
247
      vtkWriter.setCompression(compression);
Thomas Witkowski's avatar
Thomas Witkowski committed
248
249
250
251
252
253
254
255
      vtkWriter.writeFile(fn + paraviewFileExt);      

#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0)
	vtkWriter.writeParallelFile(paraFilename + paraviewParallelFileExt,
				    MPI::COMM_WORLD.Get_size(),
				    filename, postfix);
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
256

Thomas Witkowski's avatar
Thomas Witkowski committed
257
      MSG("ParaView file written to %s\n", (fn + paraviewFileExt).c_str());
258
259
    }

260
    if (writeParaViewAnimation) {
Thomas Witkowski's avatar
Thomas Witkowski committed
261
262
263
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
	VtkWriter vtkWriter(&dataCollectors);
Thomas Witkowski's avatar
Thomas Witkowski committed
264
265
	vtkWriter.updateAnimationFile(adaptInfo,
				      paraFilename + paraviewParallelFileExt, 
Thomas Witkowski's avatar
Thomas Witkowski committed
266
267
268
269
270
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
271
      VtkWriter vtkWriter(&dataCollectors);
Thomas Witkowski's avatar
Thomas Witkowski committed
272
273
      vtkWriter.updateAnimationFile(adaptInfo,
				    fn + paraviewFileExt, 
Thomas Witkowski's avatar
Thomas Witkowski committed
274
				    &paraviewAnimationFrames,
275
				    filename + ".pvd");
Thomas Witkowski's avatar
Thomas Witkowski committed
276
#endif
277
    }
278

Thomas Witkowski's avatar
Thomas Witkowski committed
279
280
281
282
283
284
285

    if (writeDofFormat) {
      DofWriter dofWriter(feSpace);
      dofWriter.writeFile(fn + ".dof", solutionVecs);
    }


286
287
288
289
    if (writeArhFormat)
      ArhWriter::write(fn + ".arh", feSpace->getMesh(), solutionVecs);


290
#ifdef HAVE_PNG
291
292
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
293
      pngWriter.writeFile(fn + ".png", pngType);
294

295
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
296
    }
297
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
298
299
300
301
302
303
304
305

    if (writePovrayFormat) {
      PovrayWriter povrayWriter(this, dataCollectors[0]);
      povrayWriter.writeFile(fn + ".pov");

      MSG("Povray script written to %s\n", (fn + ".pov").c_str());
    }

306
    
Thomas Witkowski's avatar
Thomas Witkowski committed
307
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
308
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
309
310
  }

311
}