FileWriter.cc 8.12 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 {

Thomas Witkowski's avatar
Thomas Witkowski committed
22
  FileWriter::FileWriter(std::string str, Mesh *m, DOFVector<double> *vec)
23
24
    : name(str),
      mesh(m)
25
  {
26
27
28
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
29
30
31

    feSpace = vec->getFESpace();

Thomas Witkowski's avatar
Thomas Witkowski committed
32
33
    solutionVecs.resize(1);
    solutionVecs[0] = vec;
34
35
  }

36

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

45
    initialize();
46

47
    for (int i = 0; i < static_cast<int>(vecs.size()); i++)
48
49
50
51
      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
52
    solutionVecs = vecs;
53
54
  }

55

Thomas Witkowski's avatar
Thomas Witkowski committed
56
  FileWriter::FileWriter(std::string name_, 
57
58
59
60
61
62
63
64
65
66
			 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
67
68
69
70
    nTmpSolutions = (*vec)[0].getSize();
    solutionVecs.resize(nTmpSolutions);
    for (int i = 0; i < nTmpSolutions; i++)
      solutionVecs[i] = new DOFVector<double>(vec->getFESpace(), "");
71
72
73
74
75

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

    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
88
89
90
    if (nTmpSolutions > 0)
      for (int i = 0; i < nTmpSolutions; i++)
        delete solutionVecs[i]; 
91
92
93
94
95
96
97
98
  }
  

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

    readParameters();
118
119
  }

120
121
122
123
124
125
126
127
128
129
130
  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);
131
    GET_PARAMETER(0, name + "->ParaView animation", "%d", &writeParaViewAnimation);
Thomas Witkowski's avatar
Thomas Witkowski committed
132
    GET_PARAMETER(0, name + "->ParaView ext", &paraviewFileExt);    
133
134
    GET_PARAMETER(0, name + "->Periodic format", "%d", &writePeriodicFormat);
    GET_PARAMETER(0, name + "->Periodic ext", &periodicFileExt);
135
    GET_PARAMETER(0, name + "->PNG format", "%d", &writePngFormat);
136
    GET_PARAMETER(0, name + "->PNG type", "%d", &pngType);
137
138
139
140
    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);
141

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
165
    if (writeElem) {
166
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
167
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
168
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
169
    } else {
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
173
174
					      traverseLevel, 
					      flag | traverseFlag, 
					      writeElement);
Thomas Witkowski's avatar
Thomas Witkowski committed
175
176
    }

177
    std::string fn = filename;
178

179
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
180
181
182
    std::string paraFilename = fn;
    std::string postfix = "";
    char f[15];
183
184
185
186
    sprintf(f, "-p%d-", MPI::COMM_WORLD.Get_rank());
    fn += f;
#endif

187
188
189
190
191
192
193
194
    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
195
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
196
197
198
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
199
200
201
202
203
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
      postfix += timeStr;
      postfix += paraviewFileExt;
#endif
204
205
206
    }

    if (writeTecPlotFormat) {
Thomas Witkowski's avatar
Thomas Witkowski committed
207
      TecPlotWriter<DOFVector<double> >::writeValues(solutionVecs[0], 
208
						     const_cast<char*>((fn + tecplotExt).c_str()), 
Thomas Witkowski's avatar
Thomas Witkowski committed
209
						     solutionVecs[0]->getName().c_str());
210
211
212
213
214
215
      MSG("TecPlot file written to %s\n", (fn + tecplotExt).c_str());
    }

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

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


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

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

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

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

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

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

270
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
271
    }
272
    
273

Thomas Witkowski's avatar
Thomas Witkowski committed
274
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
275
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
276
277
  }

278
}