FileWriter.cc 9.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
//
// 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.


13
#include "boost/lexical_cast.hpp"
14
#include "FileWriter.h"
15
#include "Initfile.h"
16 17 18
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
19 20
#include "VtkVectorWriter.h"
#include "VtkVectorWriter.hh"
21
#include "PngWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
22
#include "PovrayWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
23
#include "DofWriter.h"
24
#include "ArhWriter.h"
25 26 27 28 29
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
30
#include "DataCollector.hh"
31

32
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
33
#include <mpi.h>
34 35
#endif

36 37
namespace AMDiS {

38 39
  using boost::lexical_cast;

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

    initialize();
47

48
    feSpace = vec->getFeSpace();
49

Thomas Witkowski's avatar
Thomas Witkowski committed
50 51
    solutionVecs.resize(1);
    solutionVecs[0] = vec;
52 53
  }

54

Thomas Witkowski's avatar
Thomas Witkowski committed
55 56
  FileWriter::FileWriter(std::string name_, 
			 Mesh *mesh_, 
Thomas Witkowski's avatar
Thomas Witkowski committed
57
			 std::vector<DOFVector<double>*> vecs)
58 59 60 61 62
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

63
    initialize();
64

65
    for (int i = 0; i < static_cast<int>(vecs.size()); i++)
66 67
      TEST_EXIT(vecs[0]->getFeSpace() == vecs[i]->getFeSpace())
	("All FeSpace have to be equal!\n");
68

69
    feSpace = vecs[0]->getFeSpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
70
    solutionVecs = vecs;
71 72
  }

73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  FileWriter::FileWriter(std::string name_,
			 Mesh *mesh_,
			 SystemVector *vecs)
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

    initialize();

    for (int i = 0; i < static_cast<int>(vecs->getSize()); i++)
      TEST_EXIT(vecs->getDOFVector(0)->getFeSpace() == vecs->getDOFVector(i)->getFeSpace())
	("All FeSpace have to be equal!\n");

    feSpace = vecs->getDOFVector(0)->getFeSpace();
    solutionVecs.resize(vecs->getSize());
    for (int i = 0; i < static_cast<int>(vecs->getSize()); i++)
      solutionVecs[i] = vecs->getDOFVector(i);
  }
  

Thomas Witkowski's avatar
Thomas Witkowski committed
95
  FileWriter::FileWriter(std::string name_, 
96
			 Mesh *mesh_,
Thomas Witkowski's avatar
Thomas Witkowski committed
97
			 DOFVector<WorldVector<double> > *vec)
98 99 100 101 102 103 104 105
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
    
    // Create the temporal DOFVectors for all components of WorldVector.
Thomas Witkowski's avatar
Thomas Witkowski committed
106 107 108
    nTmpSolutions = (*vec)[0].getSize();
    solutionVecs.resize(nTmpSolutions);
    for (int i = 0; i < nTmpSolutions; i++)
109
      solutionVecs[i] = new DOFVector<double>(vec->getFeSpace(), "");
110 111 112 113 114

    // 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
115 116 117
    for (it.reset(); !it.end(); ++it, counter++)
      for (int i = 0; i < nTmpSolutions; i++)
	(*solutionVecs[i])[counter] = (*it)[i];
118

119
    feSpace = vec->getFeSpace();
120 121 122 123 124 125 126
  }


  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
127 128 129
    if (nTmpSolutions > 0)
      for (int i = 0; i < nTmpSolutions; i++)
        delete solutionVecs[i]; 
130 131 132 133 134 135 136
  }
  

  void FileWriter::initialize()
  {
    amdisMeshExt = ".mesh";
    amdisDataExt = ".dat";
Thomas Witkowski's avatar
Thomas Witkowski committed
137 138
    paraviewFileExt = ".vtu";
    paraviewParallelFileExt = ".pvtu";
139 140 141
    periodicFileExt = ".per";
    writeAMDiSFormat = 0;
    writeParaViewFormat = 0;
142
    writeParaViewVectorFormat = 0;
143
    writeParaViewAnimation = 0;
144
    writePeriodicFormat = 0;
145
    writePngFormat = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
146
    writePovrayFormat = 0;
147
    writeDofFormat = 0;
148
    writeArhFormat = 0;
149
    pngType = 0;
150 151 152 153
    appendIndex = 0;
    indexLength = 5;
    indexDecimals = 3;
    tsModulo = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
154 155
    nTmpSolutions = 0;
    paraviewAnimationFrames.resize(0),
156
    compression = NONE;
157 158

    readParameters();
159 160
  }

161

162 163 164 165
  void FileWriter::readParameters()
  {
    FUNCNAME("FileWriter::readParamters()");

166 167 168 169 170
    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);
171
    Parameters::get(name + "->ParaView vector format", writeParaViewVectorFormat);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    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);

    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);
Thomas Witkowski's avatar
Thomas Witkowski committed
190

191
    std::string compressionStr = "";
192
    Parameters::get(name + "->compression", compressionStr);
193
    if (compressionStr == "gzip" || compressionStr == "gz") {
194
      compression = GZIP;
195
    } else if (compressionStr == "bzip2" || compressionStr == "bz2") {
196 197
      compression = BZIP2;
    }
198 199
  }

200

201 202 203 204 205 206 207
  void FileWriter::writeFiles(AdaptInfo *adaptInfo,
			      bool force,
			      int level,
			      Flag flag,
			      bool (*writeElem)(ElInfo*))
  {
    FUNCNAME("FileWriter::writeFiles()");
208

209 210 211
    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
      return;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
215
    if (writeElem) {
216
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
217
	dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i], 
218
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
219
    } else {
220
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
221
	dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i], 
222 223 224
					      traverseLevel, 
					      flag | traverseFlag, 
					      writeElement);
Thomas Witkowski's avatar
Thomas Witkowski committed
225 226
    }

227
    std::string fn = filename;
228

229
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
230
    std::string paraFilename = fn;
231
    fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
Thomas Witkowski's avatar
Thomas Witkowski committed
232
    std::string postfix = "";
233 234
#endif

235 236 237 238 239 240 241 242
    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
243
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
244 245 246
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
247 248
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
249
      postfix += timeStr + paraviewFileExt;
Thomas Witkowski's avatar
Thomas Witkowski committed
250
#endif
251 252 253
    }

    if (writeAMDiSFormat) {
254
      MacroWriter::writeMacro(dataCollectors[0], 
255 256
			      const_cast<char*>((fn +  amdisMeshExt).c_str()), 
			      adaptInfo ? adaptInfo->getTime() : 0.0);
257
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());
258
      
259
      ValueWriter::writeValues(dataCollectors[0],
260 261 262 263 264 265
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());   
    }

    if (writePeriodicFormat) {
266
      MacroWriter::writePeriodicFile(dataCollectors[0], 
267 268 269 270 271
				     (fn + periodicFileExt).c_str());
      MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
    }
    
    if (writeParaViewFormat) {
272
      VtkWriter vtkWriter(&dataCollectors);     
273
      vtkWriter.setCompression(compression);
Thomas Witkowski's avatar
Thomas Witkowski committed
274 275 276 277 278 279 280 281
      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
282

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

286 287 288 289 290 291 292 293 294 295 296
    if (writeParaViewVectorFormat) {
      VtkVectorWriter::writeFile(solutionVecs, fn + paraviewFileExt);

#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0)
	VtkVectorWriter::writeFile(solutionVecs, fn + paraviewFileExt, true);
#endif

      MSG("ParaView file written to %s\n", (fn + paraviewFileExt).c_str());
    }
    
297
    if (writeParaViewAnimation) {
Thomas Witkowski's avatar
Thomas Witkowski committed
298 299
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
300
	VtkWriter::updateAnimationFile(paraFilename + paraviewParallelFileExt,
Thomas Witkowski's avatar
Thomas Witkowski committed
301 302 303 304 305
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
306
      VtkWriter::updateAnimationFile(fn + paraviewFileExt,
Thomas Witkowski's avatar
Thomas Witkowski committed
307
				    &paraviewAnimationFrames,
308
				    filename + ".pvd");
Thomas Witkowski's avatar
Thomas Witkowski committed
309
#endif
310
    }
311

Thomas Witkowski's avatar
Thomas Witkowski committed
312 313 314 315 316 317 318

    if (writeDofFormat) {
      DofWriter dofWriter(feSpace);
      dofWriter.writeFile(fn + ".dof", solutionVecs);
    }


319 320 321 322
    if (writeArhFormat)
      ArhWriter::write(fn + ".arh", feSpace->getMesh(), solutionVecs);


323
#ifdef HAVE_PNG
324 325
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
326
      pngWriter.writeFile(fn + ".png", pngType);
327

328
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
329
    }
330
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
331 332 333 334 335 336 337 338

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

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

339
    
Thomas Witkowski's avatar
Thomas Witkowski committed
340
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
341
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
342 343
  }

344
}