FileWriter.cc 8.75 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"
Thomas Witkowski's avatar
Thomas Witkowski committed
22
#include "DofWriter.h"
23
#include "ArhWriter.h"
24 25 26 27 28 29
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"

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

34 35
namespace AMDiS {

36 37
  using boost::lexical_cast;

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

    initialize();
45

46
    feSpace = vec->getFeSpace();
47

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

52

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

61
    initialize();
62

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

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

71

Thomas Witkowski's avatar
Thomas Witkowski committed
72
  FileWriter::FileWriter(std::string name_, 
73
			 Mesh *mesh_,
Thomas Witkowski's avatar
Thomas Witkowski committed
74
			 DOFVector<WorldVector<double> > *vec)
75 76 77 78 79 80 81 82
    : name(name_),
      mesh(mesh_)
  {
    FUNCNAME("FileWriter::FileWriter()");

    initialize();
    
    // Create the temporal DOFVectors for all components of WorldVector.
Thomas Witkowski's avatar
Thomas Witkowski committed
83 84 85
    nTmpSolutions = (*vec)[0].getSize();
    solutionVecs.resize(nTmpSolutions);
    for (int i = 0; i < nTmpSolutions; i++)
86
      solutionVecs[i] = new DOFVector<double>(vec->getFeSpace(), "");
87 88 89 90 91

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

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


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

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

    readParameters();
135 136
  }

137

138 139 140 141
  void FileWriter::readParameters()
  {
    FUNCNAME("FileWriter::readParamters()");

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    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 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
165

166
    std::string compressionStr = "";
167
    Parameters::get(name + "->compression", compressionStr);
168
    if (compressionStr == "gzip" || compressionStr == "gz") {
169
      compression = GZIP;
170
    } else if (compressionStr == "bzip2" || compressionStr == "bz2") {
171 172
      compression = BZIP2;
    }
173 174
  }

175

176 177 178 179 180 181 182
  void FileWriter::writeFiles(AdaptInfo *adaptInfo,
			      bool force,
			      int level,
			      Flag flag,
			      bool (*writeElem)(ElInfo*))
  {
    FUNCNAME("FileWriter::writeFiles()");
183

184 185 186
    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
      return;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
190
    if (writeElem) {
191
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
192
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
193
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
194
    } else {
195
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
196
	dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], 
197 198 199
					      traverseLevel, 
					      flag | traverseFlag, 
					      writeElement);
Thomas Witkowski's avatar
Thomas Witkowski committed
200 201
    }

202
    std::string fn = filename;
203

204
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
205
    std::string paraFilename = fn;
206
    fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
Thomas Witkowski's avatar
Thomas Witkowski committed
207
    std::string postfix = "";
208 209
#endif

210 211 212 213 214 215 216 217
    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
218
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
219 220 221
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
222 223
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
224
      postfix += timeStr + paraviewFileExt;
Thomas Witkowski's avatar
Thomas Witkowski committed
225
#endif
226 227 228
    }

    if (writeAMDiSFormat) {
229
      MacroWriter::writeMacro(dataCollectors[0], 
230 231
			      const_cast<char*>((fn +  amdisMeshExt).c_str()), 
			      adaptInfo ? adaptInfo->getTime() : 0.0);
232
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());
233
      
234
      ValueWriter::writeValues(dataCollectors[0],
235 236 237 238 239 240
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());   
    }

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

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

261
    if (writeParaViewAnimation) {
Thomas Witkowski's avatar
Thomas Witkowski committed
262 263 264 265 266 267 268 269 270
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
	VtkWriter vtkWriter(&dataCollectors);
	vtkWriter.updateAnimationFile(paraFilename + paraviewParallelFileExt, 
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
271
      VtkWriter vtkWriter(&dataCollectors);
Thomas Witkowski's avatar
Thomas Witkowski committed
272 273
      vtkWriter.updateAnimationFile(fn + paraviewFileExt, 
				    &paraviewAnimationFrames,
274
				    filename + ".pvd");
Thomas Witkowski's avatar
Thomas Witkowski committed
275
#endif
276
    }
277

Thomas Witkowski's avatar
Thomas Witkowski committed
278 279 280 281 282 283 284

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


285 286 287 288
    if (writeArhFormat)
      ArhWriter::write(fn + ".arh", feSpace->getMesh(), solutionVecs);


289
#ifdef HAVE_PNG
290 291
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
292
      pngWriter.writeFile(fn + ".png", pngType);
293

294
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
295
    }
296
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
297 298 299 300 301 302 303 304

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

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

305
    
Thomas Witkowski's avatar
Thomas Witkowski committed
306
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
307
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
308 309
  }

310
}