FileWriter.cc 8.69 KB
Newer Older
1
#include "boost/lexical_cast.hpp"
2
3
4
5
6
7
8
#include "FileWriter.h"
#include "SystemVector.h"
#include "Parameters.h"
#include "TecPlotWriter.h"
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
9
#include "PngWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
10
#include "PovrayWriter.h"
11
12
13
14
15
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
16
#include "OpenMP.h"
17

18
19
20
21
#if HAVE_PARALLEL_DOMAIN_AMDIS
#include "mpi.h"
#endif

22
23
namespace AMDiS {

24
25
  using boost::lexical_cast;

Thomas Witkowski's avatar
Thomas Witkowski committed
26
  FileWriter::FileWriter(std::string str, Mesh *m, DOFVector<double> *vec)
27
28
    : name(str),
      mesh(m)
29
  {
30
31
32
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
33
34
35

    feSpace = vec->getFESpace();

Thomas Witkowski's avatar
Thomas Witkowski committed
36
37
    solutionVecs.resize(1);
    solutionVecs[0] = vec;
38
39
  }

40

Thomas Witkowski's avatar
Thomas Witkowski committed
41
42
  FileWriter::FileWriter(std::string name_, 
			 Mesh *mesh_, 
43
			 std::vector< DOFVector<double>* > vecs)
44
45
46
47
48
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

49
    initialize();
50

51
    for (int i = 0; i < static_cast<int>(vecs.size()); i++)
52
53
54
55
      TEST_EXIT(vecs[0]->getFESpace() == vecs[i]->getFESpace())
	("All FESpace have to be equal!\n");

    feSpace = vecs[0]->getFESpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
56
    solutionVecs = vecs;
57
58
  }

59

Thomas Witkowski's avatar
Thomas Witkowski committed
60
  FileWriter::FileWriter(std::string name_, 
61
62
63
64
65
66
67
68
69
70
			 Mesh *mesh_,
			 DOFVector< WorldVector<double> > *vec)
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
    
    // Create the temporal DOFVectors for all components of WorldVector.
Thomas Witkowski's avatar
Thomas Witkowski committed
71
72
73
74
    nTmpSolutions = (*vec)[0].getSize();
    solutionVecs.resize(nTmpSolutions);
    for (int i = 0; i < nTmpSolutions; i++)
      solutionVecs[i] = new DOFVector<double>(vec->getFESpace(), "");
75
76
77
78
79

    // 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
80
81
82
    for (it.reset(); !it.end(); ++it, counter++)
      for (int i = 0; i < nTmpSolutions; i++)
	(*solutionVecs[i])[counter] = (*it)[i];
83
84
85
86
87
88
89
90
91

    feSpace = vec->getFESpace();
  }


  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
92
93
94
    if (nTmpSolutions > 0)
      for (int i = 0; i < nTmpSolutions; i++)
        delete solutionVecs[i]; 
95
96
97
98
99
100
101
102
  }
  

  void FileWriter::initialize()
  {
    tecplotExt = ".tec";
    amdisMeshExt = ".mesh";
    amdisDataExt = ".dat";
Thomas Witkowski's avatar
Thomas Witkowski committed
103
104
    paraviewFileExt = ".vtu";
    paraviewParallelFileExt = ".pvtu";
105
106
107
108
    periodicFileExt = ".per";
    writeTecPlotFormat = 0;
    writeAMDiSFormat = 0;
    writeParaViewFormat = 0;
109
    writeParaViewAnimation = 0;
110
    writePeriodicFormat = 0;
111
    writePngFormat = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
112
    writePovrayFormat = 0;
113
    pngType = 0;
114
115
116
117
    appendIndex = 0;
    indexLength = 5;
    indexDecimals = 3;
    tsModulo = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
118
119
    nTmpSolutions = 0;
    paraviewAnimationFrames.resize(0),
120
    compression = NONE;
121
122

    readParameters();
123
124
  }

125
126
127
128
129
130
131
132
133
134
135
  void FileWriter::readParameters()
  {
    FUNCNAME("FileWriter::readParamters()");

    GET_PARAMETER(0, name + "->filename", &filename);
    GET_PARAMETER(0, name + "->TecPlot format", "%d", &writeTecPlotFormat);
    GET_PARAMETER(0, name + "->TecPlot ext", &tecplotExt);
    GET_PARAMETER(0, name + "->AMDiS format", "%d", &writeAMDiSFormat);
    GET_PARAMETER(0, name + "->AMDiS mesh ext", &amdisMeshExt);
    GET_PARAMETER(0, name + "->AMDiS data ext", &amdisDataExt);
    GET_PARAMETER(0, name + "->ParaView format", "%d", &writeParaViewFormat);
136
    GET_PARAMETER(0, name + "->ParaView animation", "%d", &writeParaViewAnimation);
Thomas Witkowski's avatar
Thomas Witkowski committed
137
    GET_PARAMETER(0, name + "->ParaView ext", &paraviewFileExt);    
138
139
    GET_PARAMETER(0, name + "->Periodic format", "%d", &writePeriodicFormat);
    GET_PARAMETER(0, name + "->Periodic ext", &periodicFileExt);
140
    GET_PARAMETER(0, name + "->PNG format", "%d", &writePngFormat);
141
    GET_PARAMETER(0, name + "->PNG type", "%d", &pngType);
142
143
144
145
    GET_PARAMETER(0, name + "->append index", "%d", &appendIndex);
    GET_PARAMETER(0, name + "->index length", "%d", &indexLength);
    GET_PARAMETER(0, name + "->index decimals", "%d", &indexDecimals);
    GET_PARAMETER(0, name + "->write every i-th timestep", "%d", &tsModulo);
146

Thomas Witkowski's avatar
Thomas Witkowski committed
147
148
149
150
151
    GET_PARAMETER(0, name + "->Povray format", "%d", &writePovrayFormat);
    GET_PARAMETER(0, name + "->Povray template", "%d", &povrayTemplate);
    GET_PARAMETER(0, name + "->Povray camera location", &povrayCameraLocation);
    GET_PARAMETER(0, name + "->Povray camera look_at", &povrayCameraLookAt);

152
153
    std::string compressionStr = "";
    GET_PARAMETER(0, name + "->compression", &compressionStr);
154
    if (compressionStr == "gzip" || compressionStr == "gz") {
155
      compression = GZIP;
156
    } else if (compressionStr == "bzip2" || compressionStr == "bz2") {
157
158
      compression = BZIP2;
    }
159
160
161
162
163
164
165
166
167
  }

  void FileWriter::writeFiles(AdaptInfo *adaptInfo,
			      bool force,
			      int level,
			      Flag flag,
			      bool (*writeElem)(ElInfo*))
  {
    FUNCNAME("FileWriter::writeFiles()");
Thomas Witkowski's avatar
Thomas Witkowski committed
168
    
169
170
171
    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
      return;

172
    // Containers, which store the data to be written;
Thomas Witkowski's avatar
Thomas Witkowski committed
173
    std::vector< DataCollector* > dataCollectors(solutionVecs.size());
174

Thomas Witkowski's avatar
Thomas Witkowski committed
175
    if (writeElem) {
176
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
177
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
178
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
179
    } else {
180
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
181
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
182
183
184
					      traverseLevel, 
					      flag | traverseFlag, 
					      writeElement);
Thomas Witkowski's avatar
Thomas Witkowski committed
185
186
    }

187
    std::string fn = filename;
188

189
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
190
    std::string paraFilename = fn;
191
    fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
Thomas Witkowski's avatar
Thomas Witkowski committed
192
    std::string postfix = "";
193
194
#endif

195
196
197
198
199
200
201
202
    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
203
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
204
205
206
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
207
208
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
209
      postfix += timeStr + paraviewFileExt;
Thomas Witkowski's avatar
Thomas Witkowski committed
210
#endif
211
212
213
    }

    if (writeTecPlotFormat) {
Thomas Witkowski's avatar
Thomas Witkowski committed
214
      TecPlotWriter<DOFVector<double> >::writeValues(solutionVecs[0], 
215
						     const_cast<char*>((fn + tecplotExt).c_str()), 
Thomas Witkowski's avatar
Thomas Witkowski committed
216
						     solutionVecs[0]->getName().c_str());
217
218
219
220
      MSG("TecPlot file written to %s\n", (fn + tecplotExt).c_str());
    }

    if (writeAMDiSFormat) {
221
      MacroWriter::writeMacro(dataCollectors[0], 
Thomas Witkowski's avatar
Thomas Witkowski committed
222
			     const_cast<char*>((fn +  amdisMeshExt).c_str()), 
223
224
225
			     adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());

226
      ValueWriter::writeValues(dataCollectors[0],
227
228
229
230
231
232
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());   
    }

    if (writePeriodicFormat) {
233
      MacroWriter::writePeriodicFile(dataCollectors[0], 
234
235
236
237
238
				     (fn + periodicFileExt).c_str());
      MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
    }
    
    if (writeParaViewFormat) {
239
      VtkWriter vtkWriter(&dataCollectors);     
240
      vtkWriter.setCompression(compression);
Thomas Witkowski's avatar
Thomas Witkowski committed
241
242
243
244
245
246
247
248
      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
249

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

253
    if (writeParaViewAnimation) {
Thomas Witkowski's avatar
Thomas Witkowski committed
254
255
256
257
258
259
260
261
262
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
	VtkWriter vtkWriter(&dataCollectors);
	vtkWriter.updateAnimationFile(paraFilename + paraviewParallelFileExt, 
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
263
      VtkWriter vtkWriter(&dataCollectors);
Thomas Witkowski's avatar
Thomas Witkowski committed
264
265
      vtkWriter.updateAnimationFile(fn + paraviewFileExt, 
				    &paraviewAnimationFrames,
266
				    filename + ".pvd");
Thomas Witkowski's avatar
Thomas Witkowski committed
267
#endif
268
    }
269

270
#ifdef HAVE_PNG
271
272
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
273
      pngWriter.writeFile(fn + ".png", pngType);
274

275
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
276
    }
277
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
278
279
280
281
282
283
284
285

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

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

286
    
Thomas Witkowski's avatar
Thomas Witkowski committed
287
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
288
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
289
290
  }

291
}