Skip to content
Snippets Groups Projects
FileWriter.cc 5.95 KiB
Newer Older
//
// 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.


#include "FileWriter.h"
Praetorius, Simon's avatar
Praetorius, Simon committed
#include "FileWriter.hh"
#include "Initfile.h"
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
#include "VtkVectorWriter.h"
#include "VtkVectorWriter.hh"
#include "PovrayWriter.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
#include "DofWriter.h"
#include "ArhWriter.h"
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
#include "DataCollector.hh"
#if HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
#include <mpi.h>
Praetorius, Simon's avatar
Praetorius, Simon committed
  template<>
  FileWriterTemplated<double>::FileWriterTemplated(std::string name_,
			 Mesh *mesh_,
			 SystemVector *vecs)
    : name(name_),
      mesh(mesh_)
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
    FUNCNAME("FileWriterTemplated<T>::FileWriterTemplated()");

    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);
  }
Praetorius, Simon's avatar
Praetorius, Simon committed
  template<>
  void FileWriterTemplated<double>::writeFiles(AdaptInfo *adaptInfo,
			      bool force,
			      int level,
			      Flag flag,
			      bool (*writeElem)(ElInfo*))
  {
Praetorius, Simon's avatar
Praetorius, Simon committed
    FUNCNAME("FileWriterTemplated<T>::writeFiles()");
Praetorius, Simon's avatar
Praetorius, Simon committed
    if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force)
    // Containers, which store the data to be written;
    std::vector<DataCollector<>*> dataCollectors(solutionVecs.size());
Thomas Witkowski's avatar
Thomas Witkowski committed
    if (writeElem) {
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Praetorius, Simon's avatar
Praetorius, Simon committed
	dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i],
					      level, flag, writeElem);
Thomas Witkowski's avatar
Thomas Witkowski committed
    } else {
      for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
Praetorius, Simon's avatar
Praetorius, Simon committed
	dataCollectors[i] = new DataCollector<>(feSpace, solutionVecs[i],
					      traverseLevel,
					      flag | traverseFlag,
    std::string fn = filename;
#if HAVE_PARALLEL_DOMAIN_AMDIS
    std::string paraFilename = fn;
    fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
    std::string postfix = "";
    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
      sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
      sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);

      fn += timeStr;
#if HAVE_PARALLEL_DOMAIN_AMDIS
      paraFilename += timeStr;
#endif
    } else {
#if HAVE_PARALLEL_DOMAIN_AMDIS
      postfix += paraviewFileExt;
#endif
    }

    if (writeAMDiSFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
      MacroWriter::writeMacro(dataCollectors[0],
			      const_cast<char*>((fn +  amdisMeshExt).c_str()),
      MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());
      ValueWriter::writeValues(dataCollectors[0],
			       (fn + amdisDataExt).c_str(),
			       adaptInfo ? adaptInfo->getTime() : 0.0);
Praetorius, Simon's avatar
Praetorius, Simon committed
      MSG("value file written to %s\n", (fn + amdisDataExt).c_str());
    }

    if (writePeriodicFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
      MacroWriter::writePeriodicFile(dataCollectors[0],
				     (fn + periodicFileExt).c_str());
      MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
    }
    if (writeParaViewFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
      VtkWriter vtkWriter(&dataCollectors);
      vtkWriter.setCompression(compression);
Praetorius, Simon's avatar
Praetorius, Simon committed
      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,
				    dataCollectors.size());
#endif
      MSG("ParaView file written to %s\n", (fn + paraviewFileExt).c_str());
    if (writeParaViewVectorFormat) {
Praetorius, Simon's avatar
Praetorius, Simon committed
      VtkVectorWriter::writeFile(solutionVecs, fn + paraviewFileExt, true, writeAs3dVector);
Praetorius, Simon's avatar
Praetorius, Simon committed
//       VtkVectorWriter::Impl<double> vtkVectorWriter(&dataCollectors, writeAs3dVector);
//       vtkVectorWriter.setCompression(compression);
//       vtkVectorWriter.setWriteAs3dVector(writeAs3dVector);
//       vtkVectorWriter.writeFile(fn + paraviewFileExt);
      MSG("ParaView file written to %s\n", (fn + paraviewFileExt).c_str());
    }
    if (writeParaViewAnimation) {
#if HAVE_PARALLEL_DOMAIN_AMDIS
      if (MPI::COMM_WORLD.Get_rank() == 0) {
	VtkWriter::updateAnimationFile(adaptInfo,
				      paraFilename + paraviewParallelFileExt,
				      &paraviewAnimationFrames,
				      filename + ".pvd");

      }
#else
      VtkWriter::updateAnimationFile(adaptInfo,
				    fn + paraviewFileExt,
				    &paraviewAnimationFrames,
				    filename + ".pvd");
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed

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

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

    if (writePngFormat) {
      PngWriter pngWriter(dataCollectors[0]);
      pngWriter.writeFile(fn + ".png", pngType);
      MSG("PNG image file written to %s\n", (fn + ".png").c_str());

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

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

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