FileWriter.cc 8.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#include "FileWriter.h"
#include "SystemVector.h"
#include "Parameters.h"
#include "TecPlotWriter.h"
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
13
#include "OpenMP.h"
14
15
16

namespace AMDiS {

17
  FileWriter::FileWriter(const std::string &name_, 
18
19
20
21
22
			 Mesh *mesh_,
			 DOFVector<double> *vec)
    : name(name_),
      mesh(mesh_)
  {
23
24
25
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
26
27
28
29
30

    feSpace = vec->getFESpace();

    solutionVecs_.resize(1);
    solutionVecs_[0] = vec;
Thomas Witkowski's avatar
Thomas Witkowski committed
31
32

    dataCollectors_.resize(1);
33
34
  }

35

36
  FileWriter::FileWriter(const std::string &name_, 
37
			 Mesh *mesh_,
38
			 std::vector< DOFVector<double>* > vecs)
39
40
41
42
43
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

44
    initialize();
45
46
47
48
49
50
51
52

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

    feSpace = vecs[0]->getFESpace();
    solutionVecs_ = vecs;
Thomas Witkowski's avatar
Thomas Witkowski committed
53
54

    dataCollectors_.resize(vecs.size());
55
56
  }

57

58
  FileWriter::FileWriter(const std::string &name_, 
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
			 Mesh *mesh_,
			 DOFVector< WorldVector<double> > *vec)
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
    
    // Create the temporal DOFVectors for all components of WorldVector.
    nTmpSolutions_ = (*vec)[0].getSize();
    solutionVecs_.resize(nTmpSolutions_);
    for (int i = 0; i < nTmpSolutions_; i++) {
      solutionVecs_[i] = NEW DOFVector<double>(vec->getFESpace(), "");
    } 

    // Copy the components of the WorldVectors to different DOFVectors
    // of double values.
    DOFVector< WorldVector<double> >::Iterator it(vec, USED_DOFS);
    int counter = 0;
    for (it.reset(); !it.end(); ++it, counter++) {
      for (int i = 0; i < nTmpSolutions_; i++) {
	(*solutionVecs_[i])[counter] = (*it)[i];
      }
    }

    feSpace = vec->getFESpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
86
87

    dataCollectors_.resize(nTmpSolutions_);
88
89
90
91
92
93
94
95
96
97
98
99
  }


  FileWriter::~FileWriter()
  {
    // Do not forget to delete temporal solution vector, if there have been
    // some created in the constructor.
    if (nTmpSolutions_ > 0) {
      for (int i = 0; i < nTmpSolutions_; i++) {
        DELETE solutionVecs_[i]; 
      }
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
100
101
102
103
104
105

    for (int i = 0; i < static_cast<int>(dataCollectors_.size()); i++) {
      if (dataCollectors_[i]) {
	DELETE dataCollectors_[i];
      }
    }
106
107
108
109
110
111
112
113
114
115
116
117
118
  }
  

  void FileWriter::initialize()
  {
    tecplotExt = ".tec";
    amdisMeshExt = ".mesh";
    amdisDataExt = ".dat";
    paraViewFileExt = ".vtu";
    periodicFileExt = ".per";
    writeTecPlotFormat = 0;
    writeAMDiSFormat = 0;
    writeParaViewFormat = 0;
119
    writeParaViewAnimation = 0;
120
121
122
123
124
125
    writePeriodicFormat = 0;
    appendIndex = 0;
    indexLength = 5;
    indexDecimals = 3;
    tsModulo = 1;
    nTmpSolutions_ = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
126
127
128
    delayWriting_ = 0;
    writingIsDelayed_ = false;
    delayedFilename_ = "";
129
    paraViewAnimationFrames_.resize(0);
130
    compression = NONE;
131
132

    readParameters();
133
134
  }

135
136
137
138
139
140
141
142
143
144
145
  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);
146
147
    GET_PARAMETER(0, name + "->ParaView animation", "%d", &writeParaViewAnimation);
    GET_PARAMETER(0, name + "->ParaView ext", &paraViewFileExt);    
148
149
150
151
152
153
    GET_PARAMETER(0, name + "->Periodic format", "%d", &writePeriodicFormat);
    GET_PARAMETER(0, name + "->Periodic ext", &periodicFileExt);
    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);
Thomas Witkowski's avatar
Thomas Witkowski committed
154
    GET_PARAMETER(0, name + "->delay", "%d", &delayWriting_);
155

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

164
165
    TEST_EXIT(!delayWriting_ || amdisHaveOpenMP)
      ("Delayed writing only possible with OpenMP support!\n");
166
167
168
169
170
171
172
173
174
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    if (writingIsDelayed_) {
      ERROR_EXIT("This should not happen!\n");
    }

    if (writeElem) {
      for (int i = 0; i < static_cast<int>(dataCollectors_.size()); i++) {
	dataCollectors_[i] = NEW DataCollector(feSpace, solutionVecs_[i], 
				  level, flag, writeElem);
      }
    } else {
      for (int i = 0; i < static_cast<int>(dataCollectors_.size()); i++) {
	dataCollectors_[i] = NEW DataCollector(feSpace, solutionVecs_[i], 
				  traverseLevel, flag | traverseFlag, writeElement);
      }
    }

195
    std::string fn = filename;
196
197
198
199
200
201
202
203
204

    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
205
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
206
207
208
209
210
211
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
    }


Thomas Witkowski's avatar
Thomas Witkowski committed
212
213
214
    if (delayWriting_) {
      if (writeTecPlotFormat || writeAMDiSFormat || writePeriodicFormat) {
	ERROR_EXIT("Delay writing only supported for ParaView file format!\n");
215
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
216
217
218

      for (int i = 0; i < static_cast<int>(dataCollectors_.size()); i++) {
	dataCollectors_[i]->fillAllData();
219
220
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
221
222
      writingIsDelayed_ = true;
      delayedFilename_ = fn;
Thomas Witkowski's avatar
Thomas Witkowski committed
223
224
225
      
      MSG("Delayed writing of ParaView file %s\n", (fn + paraViewFileExt).c_str());

Thomas Witkowski's avatar
Thomas Witkowski committed
226
227
228
      return;
    }
 
229
230
231
232
233
234
235
236
237
238
    if (writeTecPlotFormat) {
      TecPlotWriter<DOFVector<double> >::writeValues(solutionVecs_[0], 
						     const_cast<char*>((fn + tecplotExt).c_str()), 
						     solutionVecs_[0]->getName().c_str());
      MSG("TecPlot file written to %s\n", (fn + tecplotExt).c_str());
    }

    if (writeAMDiSFormat) {
      TEST_EXIT(mesh)("no mesh\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
239
240
      MacroWriter::writeMacro(dataCollectors_[0], 
			     const_cast<char*>((fn +  amdisMeshExt).c_str()), 
241
242
243
244
			     adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());


Thomas Witkowski's avatar
Thomas Witkowski committed
245
      ValueWriter::writeValues(dataCollectors_[0],
246
247
248
249
250
251
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());   
    }

    if (writePeriodicFormat) {
Thomas Witkowski's avatar
Thomas Witkowski committed
252
      MacroWriter::writePeriodicFile(dataCollectors_[0], 
253
254
255
256
257
				     (fn + periodicFileExt).c_str());
      MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
    }
    
    if (writeParaViewFormat) {
Thomas Witkowski's avatar
Thomas Witkowski committed
258
      VtkWriter vtkWriter(&dataCollectors_);     
259
260
      vtkWriter.setCompression(compression);
      vtkWriter.writeFile(const_cast<char*>((fn + paraViewFileExt).c_str()));      
Thomas Witkowski's avatar
Thomas Witkowski committed
261

262
263
264
      MSG("ParaView file written to %s\n", (fn + paraViewFileExt).c_str());
    }

265
    if (writeParaViewAnimation) {
Thomas Witkowski's avatar
Thomas Witkowski committed
266
      VtkWriter vtkWriter(&dataCollectors_);
267
268
      vtkWriter.updateAnimationFile(fn + paraViewFileExt, 
				    &paraViewAnimationFrames_, 
Thomas Witkowski's avatar
Thomas Witkowski committed
269
				    const_cast<char*>((filename + ".pvd").c_str()));
270
    }
271
    
272

Thomas Witkowski's avatar
Thomas Witkowski committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
    for (int i = 0; i < static_cast<int>(dataCollectors_.size()); i++) {
      DELETE dataCollectors_[i];
    }
  }

  void FileWriter::writeDelayedFiles()
  {
    if (!writingIsDelayed_) {
      return;
    }

    if (writeParaViewFormat) {
      VtkWriter vtkWriter(&dataCollectors_);     
      vtkWriter.writeFile(const_cast<char*>((delayedFilename_ + paraViewFileExt).c_str()));
    }

    if (writeParaViewAnimation) {
      VtkWriter vtkWriter(&dataCollectors_);
      vtkWriter.updateAnimationFile(delayedFilename_ + paraViewFileExt, 
				    &paraViewAnimationFrames_, 
				    const_cast<char*>((filename + ".pvd").c_str()));
294
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
295
296
297
298
299
300
301

    for (int i = 0; i < static_cast<int>(dataCollectors_.size()); i++) {
      DELETE dataCollectors_[i];
    }

    writingIsDelayed_ = false;
    delayedFilename_ = "";
302
303
  }
}