FileWriter.cc 8.45 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 15 16 17 18 19
#include "FileWriter.h"
#include "SystemVector.h"
#include "Parameters.h"
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
20
#include "PngWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
21
#include "PovrayWriter.h"
22 23 24 25 26
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
27
#include "OpenMP.h"
28

29 30 31 32
#if HAVE_PARALLEL_DOMAIN_AMDIS
#include "mpi.h"
#endif

33 34
namespace AMDiS {

35 36
  using boost::lexical_cast;

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

    initialize();
44

45
    feSpace = vec->getFeSpace();
46

Thomas Witkowski's avatar
Thomas Witkowski committed
47 48
    solutionVecs.resize(1);
    solutionVecs[0] = vec;
49 50
  }

51

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

60
    initialize();
61

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

66
    feSpace = vecs[0]->getFeSpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
67
    solutionVecs = vecs;
68 69
  }

70

Thomas Witkowski's avatar
Thomas Witkowski committed
71
  FileWriter::FileWriter(std::string name_, 
72 73 74 75 76 77 78 79 80 81
			 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
82 83 84
    nTmpSolutions = (*vec)[0].getSize();
    solutionVecs.resize(nTmpSolutions);
    for (int i = 0; i < nTmpSolutions; i++)
85
      solutionVecs[i] = new DOFVector<double>(vec->getFeSpace(), "");
86 87 88 89 90

    // 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
91 92 93
    for (it.reset(); !it.end(); ++it, counter++)
      for (int i = 0; i < nTmpSolutions; i++)
	(*solutionVecs[i])[counter] = (*it)[i];
94

95
    feSpace = vec->getFeSpace();
96 97 98 99 100 101 102
  }


  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
103 104 105
    if (nTmpSolutions > 0)
      for (int i = 0; i < nTmpSolutions; i++)
        delete solutionVecs[i]; 
106 107 108 109 110 111 112
  }
  

  void FileWriter::initialize()
  {
    amdisMeshExt = ".mesh";
    amdisDataExt = ".dat";
Thomas Witkowski's avatar
Thomas Witkowski committed
113 114
    paraviewFileExt = ".vtu";
    paraviewParallelFileExt = ".pvtu";
115 116 117
    periodicFileExt = ".per";
    writeAMDiSFormat = 0;
    writeParaViewFormat = 0;
118
    writeParaViewAnimation = 0;
119
    writePeriodicFormat = 0;
120
    writePngFormat = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
121
    writePovrayFormat = 0;
122
    pngType = 0;
123 124 125 126
    appendIndex = 0;
    indexLength = 5;
    indexDecimals = 3;
    tsModulo = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
127 128
    nTmpSolutions = 0;
    paraviewAnimationFrames.resize(0),
129
    compression = NONE;
130 131

    readParameters();
132 133
  }

134 135 136 137 138 139 140 141 142
  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);
143
    GET_PARAMETER(0, name + "->ParaView animation", "%d", &writeParaViewAnimation);
Thomas Witkowski's avatar
Thomas Witkowski committed
144
    GET_PARAMETER(0, name + "->ParaView ext", &paraviewFileExt);    
145 146
    GET_PARAMETER(0, name + "->Periodic format", "%d", &writePeriodicFormat);
    GET_PARAMETER(0, name + "->Periodic ext", &periodicFileExt);
147
    GET_PARAMETER(0, name + "->PNG format", "%d", &writePngFormat);
148
    GET_PARAMETER(0, name + "->PNG type", "%d", &pngType);
149 150 151 152
    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);
153

Thomas Witkowski's avatar
Thomas Witkowski committed
154 155 156 157 158
    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);

159 160
    std::string compressionStr = "";
    GET_PARAMETER(0, name + "->compression", &compressionStr);
161
    if (compressionStr == "gzip" || compressionStr == "gz") {
162
      compression = GZIP;
163
    } else if (compressionStr == "bzip2" || compressionStr == "bz2") {
164 165
      compression = BZIP2;
    }
166 167
  }

168

169 170 171 172 173 174 175
  void FileWriter::writeFiles(AdaptInfo *adaptInfo,
			      bool force,
			      int level,
			      Flag flag,
			      bool (*writeElem)(ElInfo*))
  {
    FUNCNAME("FileWriter::writeFiles()");
176

177 178 179
    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
      return;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
183
    if (writeElem) {
184
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
185
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
186
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
187
    } else {
188
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
189
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
190 191 192
					      traverseLevel, 
					      flag | traverseFlag, 
					      writeElement);
Thomas Witkowski's avatar
Thomas Witkowski committed
193 194
    }

195
    std::string fn = filename;
196

197
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
198
    std::string paraFilename = fn;
199
    fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
Thomas Witkowski's avatar
Thomas Witkowski committed
200
    std::string postfix = "";
201 202
#endif

203 204 205 206 207 208 209 210
    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
211
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
212 213 214
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
215 216
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
217
      postfix += timeStr + paraviewFileExt;
Thomas Witkowski's avatar
Thomas Witkowski committed
218
#endif
219 220 221
    }

    if (writeAMDiSFormat) {
222
      MacroWriter::writeMacro(dataCollectors[0], 
223 224
			      const_cast<char*>((fn +  amdisMeshExt).c_str()), 
			      adaptInfo ? adaptInfo->getTime() : 0.0);
225
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());
226
      
227
      ValueWriter::writeValues(dataCollectors[0],
228 229 230 231 232 233
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());   
    }

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

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

254
    if (writeParaViewAnimation) {
Thomas Witkowski's avatar
Thomas Witkowski committed
255 256 257 258 259 260 261 262 263
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
	VtkWriter vtkWriter(&dataCollectors);
	vtkWriter.updateAnimationFile(paraFilename + paraviewParallelFileExt, 
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
264
      VtkWriter vtkWriter(&dataCollectors);
Thomas Witkowski's avatar
Thomas Witkowski committed
265 266
      vtkWriter.updateAnimationFile(fn + paraviewFileExt, 
				    &paraviewAnimationFrames,
267
				    filename + ".pvd");
Thomas Witkowski's avatar
Thomas Witkowski committed
268
#endif
269
    }
270

271
#ifdef HAVE_PNG
272 273
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
274
      pngWriter.writeFile(fn + ".png", pngType);
275

276
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
277
    }
278
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
279 280 281 282 283 284 285 286

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

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

287
    
Thomas Witkowski's avatar
Thomas Witkowski committed
288
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
289
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
290 291
  }

292
}