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

16
17
18
19
#if HAVE_PARALLEL_DOMAIN_AMDIS
#include "mpi.h"
#endif

20
21
namespace AMDiS {

22
23
  FileWriter::FileWriter(const std::string &str,
			 Mesh *m,
24
			 DOFVector<double> *vec)
25
26
    : name(str),
      mesh(m)
27
  {
28
29
30
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
31
32
33
34
35
36
37

    feSpace = vec->getFESpace();

    solutionVecs_.resize(1);
    solutionVecs_[0] = vec;
  }

38

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

47
    initialize();
48

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

    feSpace = vecs[0]->getFESpace();
    solutionVecs_ = vecs;
  }

57

58
  FileWriter::FileWriter(const std::string &name_, 
59
60
61
62
63
64
65
66
67
68
69
70
71
			 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++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
72
      solutionVecs_[i] = new DOFVector<double>(vec->getFESpace(), "");
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    } 

    // 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();
  }


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

  void FileWriter::initialize()
  {
    tecplotExt = ".tec";
    amdisMeshExt = ".mesh";
    amdisDataExt = ".dat";
    paraViewFileExt = ".vtu";
    periodicFileExt = ".per";
    writeTecPlotFormat = 0;
    writeAMDiSFormat = 0;
    writeParaViewFormat = 0;
109
    writeParaViewAnimation = 0;
110
    writePeriodicFormat = 0;
111
    writePngFormat = 0;
112
    pngType = 0;
113
114
115
116
117
    appendIndex = 0;
    indexLength = 5;
    indexDecimals = 3;
    tsModulo = 1;
    nTmpSolutions_ = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
118
    paraViewAnimationFrames_.resize(0),
119
    compression = NONE;
120
121

    readParameters();
122
123
  }

124
125
126
127
128
129
130
131
132
133
134
  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);
135
136
    GET_PARAMETER(0, name + "->ParaView animation", "%d", &writeParaViewAnimation);
    GET_PARAMETER(0, name + "->ParaView ext", &paraViewFileExt);    
137
138
    GET_PARAMETER(0, name + "->Periodic format", "%d", &writePeriodicFormat);
    GET_PARAMETER(0, name + "->Periodic ext", &periodicFileExt);
139
    GET_PARAMETER(0, name + "->PNG format", "%d", &writePngFormat);
140
    GET_PARAMETER(0, name + "->PNG type", "%d", &pngType);
141
142
143
144
    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);
145

146
147
148
149
150
151
152
    std::string compressionStr = "";
    GET_PARAMETER(0, name + "->compression", &compressionStr);
    if ((compressionStr == "gzip") || (compressionStr == "gz")) {
      compression = GZIP;
    } else if ((compressionStr == "bzip2") || (compressionStr == "bz2")) {
      compression = BZIP2;
    }
153
154
155
156
157
158
159
160
161
  }

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

166
167
168
    // Containers, which store the data to be written;
    std::vector< DataCollector* > dataCollectors(solutionVecs_.size());

Thomas Witkowski's avatar
Thomas Witkowski committed
169
    if (writeElem) {
170
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
171
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs_[i], 
172
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
173
    } else {
174
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
175
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs_[i], 
176
177
178
					      traverseLevel, 
					      flag | traverseFlag, 
					      writeElement);
Thomas Witkowski's avatar
Thomas Witkowski committed
179
180
    }

181
    std::string fn = filename;
182

183
184
185
186
187
188
#if HAVE_PARALLEL_DOMAIN_AMDIS
    char f[10];
    sprintf(f, "-p%d-", MPI::COMM_WORLD.Get_rank());
    fn += f;
#endif

189
190
191
192
193
194
195
196
    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
197
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
    }

    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");

213
      MacroWriter::writeMacro(dataCollectors[0], 
Thomas Witkowski's avatar
Thomas Witkowski committed
214
			     const_cast<char*>((fn +  amdisMeshExt).c_str()), 
215
216
217
218
			     adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());


219
      ValueWriter::writeValues(dataCollectors[0],
220
221
222
223
224
225
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());   
    }

    if (writePeriodicFormat) {
226
      MacroWriter::writePeriodicFile(dataCollectors[0], 
227
228
229
230
231
				     (fn + periodicFileExt).c_str());
      MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
    }
    
    if (writeParaViewFormat) {
232
      VtkWriter vtkWriter(&dataCollectors);     
233
      vtkWriter.setCompression(compression);
234
      vtkWriter.writeFile(fn + paraViewFileExt);      
Thomas Witkowski's avatar
Thomas Witkowski committed
235

236
237
238
      MSG("ParaView file written to %s\n", (fn + paraViewFileExt).c_str());
    }

239
    if (writeParaViewAnimation) {
240
      VtkWriter vtkWriter(&dataCollectors);
241
242
      vtkWriter.updateAnimationFile(fn + paraViewFileExt, 
				    &paraViewAnimationFrames_, 
243
				    filename + ".pvd");
244
    }
245
246
247

    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
248
      pngWriter.writeFile(fn + ".png", pngType);
249

250
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
251
    }
252
    
253

254
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
255
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
256
257
258
    }
  }

259
}