FileWriter.cc 8.69 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 24 25 26 27
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
28
#include "OpenMP.h"
29

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
    pngType = 0;
125 126 127 128
    appendIndex = 0;
    indexLength = 5;
    indexDecimals = 3;
    tsModulo = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
129 130
    nTmpSolutions = 0;
    paraviewAnimationFrames.resize(0),
131
    compression = NONE;
132 133

    readParameters();
134 135
  }

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
161 162
    GET_PARAMETER(0, name + "->DOF format", "%d", &writeDofFormat);

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

172

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

181 182 183
    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force) 
      return;

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

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

199
    std::string fn = filename;
200

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

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

      fn += timeStr;
Thomas Witkowski's avatar
Thomas Witkowski committed
219 220
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
221
      postfix += timeStr + paraviewFileExt;
Thomas Witkowski's avatar
Thomas Witkowski committed
222
#endif
223 224 225
    }

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

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
275 276 277 278 279 280 281

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


282
#ifdef HAVE_PNG
283 284
    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
285
      pngWriter.writeFile(fn + ".png", pngType);
286

287
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());
288
    }
289
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
290 291 292 293 294 295 296 297

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

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

298
    
Thomas Witkowski's avatar
Thomas Witkowski committed
299
    for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
300
      delete dataCollectors[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
301 302
  }

303
}