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

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

21 22
namespace AMDiS {

23 24
  using boost::lexical_cast;

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

    initialize();
32

33
    feSpace = vec->getFeSpace();
34

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

39

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

48
    initialize();
49

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

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

58

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

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

83
    feSpace = vec->getFeSpace();
84 85 86 87 88 89 90
  }


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

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

    readParameters();
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 + "->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

Thomas Witkowski's avatar
Thomas Witkowski committed
142 143 144 145 146
    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);

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

156

157 158 159 160 161 162 163
  void FileWriter::writeFiles(AdaptInfo *adaptInfo,
			      bool force,
			      int level,
			      Flag flag,
			      bool (*writeElem)(ElInfo*))
  {
    FUNCNAME("FileWriter::writeFiles()");
164

165 166 167
    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
      return;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
171
    if (writeElem) {
172
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
173
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
174
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
175
    } else {
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 179 180
					      traverseLevel, 
					      flag | traverseFlag, 
					      writeElement);
Thomas Witkowski's avatar
Thomas Witkowski committed
181 182
    }

183
    std::string fn = filename;
184

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

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

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
203 204
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
205
      postfix += timeStr + paraviewFileExt;
Thomas Witkowski's avatar
Thomas Witkowski committed
206
#endif
207 208 209
    }

    if (writeAMDiSFormat) {
210
      MacroWriter::writeMacro(dataCollectors[0], 
211 212
			      const_cast<char*>((fn +  amdisMeshExt).c_str()), 
			      adaptInfo ? adaptInfo->getTime() : 0.0);
213
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());
214
      
215
      ValueWriter::writeValues(dataCollectors[0],
216 217 218 219 220 221
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());   
    }

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

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

242
    if (writeParaViewAnimation) {
Thomas Witkowski's avatar
Thomas Witkowski committed
243 244 245 246 247 248 249 250 251
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
	VtkWriter vtkWriter(&dataCollectors);
	vtkWriter.updateAnimationFile(paraFilename + paraviewParallelFileExt, 
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
252
      VtkWriter vtkWriter(&dataCollectors);
Thomas Witkowski's avatar
Thomas Witkowski committed
253 254
      vtkWriter.updateAnimationFile(fn + paraviewFileExt, 
				    &paraviewAnimationFrames,
255
				    filename + ".pvd");
Thomas Witkowski's avatar
Thomas Witkowski committed
256
#endif
257
    }
258

259
#ifdef HAVE_PNG
260 261
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
262
      pngWriter.writeFile(fn + ".png", pngType);
263

264
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
265
    }
266
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
267 268 269 270 271 272 273 274

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

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

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

280
}