FileWriter.hh 7.82 KB
Newer Older
Praetorius, Simon's avatar
Praetorius, Simon committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// 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.

/** \file FileWriter.hh */

#ifndef AMDIS_FILEWRITER_HH
#define AMDIS_FILEWRITER_HH

#include "boost/lexical_cast.hpp"
#include "FileWriter.h"
#include "Initfile.h"
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
#include "VtkVectorWriter.h"
#include "PngWriter.h"
#include "PovrayWriter.h"
#include "DofWriter.h"
#include "ArhWriter.h"
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
#include "SystemVector.h"
34
#include "DataCollector.h"
Praetorius, Simon's avatar
Praetorius, Simon committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

#if HAVE_PARALLEL_DOMAIN_AMDIS
#include <mpi.h>
#endif

namespace AMDiS {

  using boost::lexical_cast;

  template<typename T>
  FileWriterTemplated<T>::FileWriterTemplated(std::string str, Mesh *m, DOFVector<T> *vec)
    : name(str),
      mesh(m)
  {
    initialize();

    feSpace = vec->getFeSpace();

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


  template<typename T>
  FileWriterTemplated<T>::FileWriterTemplated(std::string name_,
			 Mesh *mesh_, 
			 std::vector<DOFVector<T>*> vecs)
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriterTemplated<T>::FileWriterTemplated()");

    initialize();

    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;
  }


  template<typename T>
  FileWriterTemplated<T>::FileWriterTemplated(std::string name_,
			 Mesh *mesh_,
			 SystemVector *vecs)
    : name(name_),
      mesh(mesh_)
  {
    ERROR("SystemVector contains DOFVectors of type double, so the FileWriterTemplated<not double> can not be used!\n");
  }


  template<typename T>
  FileWriterTemplated<T>::~FileWriterTemplated()
  {
    // 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]; 
  }
  

  template<typename T>
  void FileWriterTemplated<T>::initialize()
  {
    amdisMeshExt = ".mesh";
    amdisDataExt = ".dat";
    paraviewFileExt = ".vtu";
    paraviewParallelFileExt = ".pvtu";
    periodicFileExt = ".per";
    writeAMDiSFormat = 0;
    writeParaViewFormat = 0;
    writeParaViewVectorFormat = 0;
    writeAs3dVector = false;
    writeParaViewAnimation = 0;
    writePeriodicFormat = 0;
    writePngFormat = 0;
    writePovrayFormat = 0;
    writeDofFormat = 0;
    writeArhFormat = 0;
    pngType = 0;
    appendIndex = 0;
    indexLength = 5;
    indexDecimals = 3;
    tsModulo = 1;
123
124
    timeModulo = -1.0;
    lastWriteTime = 0.0;
Praetorius, Simon's avatar
Praetorius, Simon committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    nTmpSolutions = 0;
    paraviewAnimationFrames.resize(0),
    compression = NONE;

    readParameters();
  }


  template<typename T>
  void FileWriterTemplated<T>::readParameters()
  {
    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 vector format", writeParaViewVectorFormat);
    Parameters::get(name + "->write vector as 3d vector", writeAs3dVector);
    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);
153
    Parameters::get(name + "->write after timestep", timeModulo);
Praetorius, Simon's avatar
Praetorius, Simon committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

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

    std::string compressionStr = "";
    Parameters::get(name + "->compression", compressionStr);
    if (compressionStr == "gzip" || compressionStr == "gz") {
      compression = GZIP;
    } else if (compressionStr == "bzip2" || compressionStr == "bz2") {
      compression = BZIP2;
    }
  }


  template<typename T>
  void FileWriterTemplated<T>::writeFiles(AdaptInfo *adaptInfo,
175
176
177
178
					  bool force,
					  int level,
					  Flag flag,
					  bool (*writeElem)(ElInfo*))
Praetorius, Simon's avatar
Praetorius, Simon committed
179
180
181
  {
    FUNCNAME("FileWriterTemplated<T>::writeFiles()");

182
    if (timeModulo > 0.0) {
183
      if ((lastWriteTime != 0.0 && adaptInfo->getTime() < lastWriteTime + timeModulo) && !force)
184
185
186
187
188
	return;
    } else {
      if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
	return;
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
189

190
191
    lastWriteTime = adaptInfo->getTime();
    
Praetorius, Simon's avatar
Praetorius, Simon committed
192
193
    // Containers, which store the data to be written;
    std::vector<DataCollector<T>*> dataCollectors(solutionVecs.size());
194
    
Praetorius, Simon's avatar
Praetorius, Simon committed
195
196
197
    if (writeElem) {
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
	dataCollectors[i] = new DataCollector<T>(feSpace, solutionVecs[i], 
198
						 level, flag, writeElem);
Praetorius, Simon's avatar
Praetorius, Simon committed
199
200
201
    } else {
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
	dataCollectors[i] = new DataCollector<T>(feSpace, solutionVecs[i], 
202
203
204
						 traverseLevel, 
						 flag | traverseFlag, 
						 writeElement);
Praetorius, Simon's avatar
Praetorius, Simon committed
205
    }
206
    
Praetorius, Simon's avatar
Praetorius, Simon committed
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    std::string fn = filename;

#if HAVE_PARALLEL_DOMAIN_AMDIS
    std::string paraFilename = fn;
    fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
    std::string postfix = "";
#endif

    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];

      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
      postfix += timeStr + paraviewFileExt;
#endif
    } else {
#if HAVE_PARALLEL_DOMAIN_AMDIS
      postfix += paraviewFileExt;
#endif
    }

    if (writeParaViewVectorFormat) {
      VtkVectorWriter::Impl<T> vtkVectorWriter(&dataCollectors, writeAs3dVector);
      vtkVectorWriter.setCompression(compression);
      vtkVectorWriter.writeFile(fn + paraviewFileExt);

#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0)
	vtkVectorWriter.writeParallelFile(paraFilename + paraviewParallelFileExt,
					  MPI::COMM_WORLD.Get_size(),
					  filename, postfix);
#endif
	
      MSG("ParaView file written to %s\n", (fn + paraviewFileExt).c_str());
    }
    
    if (writeParaViewAnimation) {
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
	VtkWriter::updateAnimationFile(adaptInfo,
				      paraFilename + paraviewParallelFileExt,
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
      VtkWriter::updateAnimationFile(adaptInfo,
				    fn + paraviewFileExt,
				    &paraviewAnimationFrames,
				    filename + ".pvd");
#endif
    }
    
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
      delete dataCollectors[i];
  }
}

#endif