Newer
Older

Thomas Witkowski
committed
//
// 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.

Thomas Witkowski
committed
#include "boost/lexical_cast.hpp"
#include "FileWriter.h"
#include "SystemVector.h"
#include "Parameters.h"
#include "ValueWriter.h"
#include "MacroWriter.h"
#include "VtkWriter.h"
#include "PngWriter.h"
#include "FiniteElemSpace.h"
#include "AdaptInfo.h"
#include "Flag.h"
#include "ElInfo.h"
#include "Mesh.h"
#if HAVE_PARALLEL_DOMAIN_AMDIS
#include "mpi.h"
#endif

Thomas Witkowski
committed
using boost::lexical_cast;
FileWriter::FileWriter(std::string str, Mesh *m, DOFVector<double> *vec)

Thomas Witkowski
committed
FUNCNAME("FileWriter::FileWriter()");
initialize();
feSpace = vec->getFeSpace();
solutionVecs.resize(1);
solutionVecs[0] = vec;

Thomas Witkowski
committed
FileWriter::FileWriter(std::string name_,
Mesh *mesh_,
: name(name_),
mesh(mesh_)
{
FUNCNAME("FileWriter::FileWriter()");

Thomas Witkowski
committed
initialize();
for (int i = 0; i < static_cast<int>(vecs.size()); i++)
TEST_EXIT(vecs[0]->getFeSpace() == vecs[i]->getFeSpace())
("All FeSpace have to be equal!\n");
feSpace = vecs[0]->getFeSpace();

Thomas Witkowski
committed

Thomas Witkowski
committed
Mesh *mesh_,

Thomas Witkowski
committed
: name(name_),
mesh(mesh_)
{
FUNCNAME("FileWriter::FileWriter()");
initialize();
// Create the temporal DOFVectors for all components of WorldVector.
nTmpSolutions = (*vec)[0].getSize();
solutionVecs.resize(nTmpSolutions);
for (int i = 0; i < nTmpSolutions; i++)
solutionVecs[i] = new DOFVector<double>(vec->getFeSpace(), "");

Thomas Witkowski
committed
// Copy the components of the WorldVectors to different DOFVectors
// of double values.
DOFVector< WorldVector<double> >::Iterator it(vec, USED_DOFS);
int counter = 0;
for (it.reset(); !it.end(); ++it, counter++)
for (int i = 0; i < nTmpSolutions; i++)
(*solutionVecs[i])[counter] = (*it)[i];

Thomas Witkowski
committed
feSpace = vec->getFeSpace();

Thomas Witkowski
committed
}
FileWriter::~FileWriter()
{
// Do not forget to delete temporal solution vector, if there have been
// some created in the constructor.
if (nTmpSolutions > 0)
for (int i = 0; i < nTmpSolutions; i++)
delete solutionVecs[i];

Thomas Witkowski
committed
}
void FileWriter::initialize()
{
amdisMeshExt = ".mesh";
amdisDataExt = ".dat";
paraviewFileExt = ".vtu";
paraviewParallelFileExt = ".pvtu";

Thomas Witkowski
committed
periodicFileExt = ".per";
writeAMDiSFormat = 0;
writeParaViewFormat = 0;

Thomas Witkowski
committed
writePeriodicFormat = 0;
writePngFormat = 0;

Thomas Witkowski
committed
writeDofFormat = 0;

Thomas Witkowski
committed
appendIndex = 0;
indexLength = 5;
indexDecimals = 3;
tsModulo = 1;
nTmpSolutions = 0;
paraviewAnimationFrames.resize(0),

Thomas Witkowski
committed
}
void FileWriter::readParameters()
{
FUNCNAME("FileWriter::readParamters()");
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);
std::string compressionStr = "";
Parameters::get(name + "->compression", compressionStr);

Thomas Witkowski
committed
if (compressionStr == "gzip" || compressionStr == "gz") {

Thomas Witkowski
committed
} else if (compressionStr == "bzip2" || compressionStr == "bz2") {

Thomas Witkowski
committed
void FileWriter::writeFiles(AdaptInfo *adaptInfo,
bool force,
int level,
Flag flag,
bool (*writeElem)(ElInfo*))
{
FUNCNAME("FileWriter::writeFiles()");
if ((adaptInfo->getTimestepNumber() % tsModulo != 0) && !force)
return;
// Containers, which store the data to be written;
std::vector<DataCollector*> dataCollectors(solutionVecs.size());
for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i],
for (int i = 0; i < static_cast<int>(dataCollectors.size()); i++)
dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i],
traverseLevel,
flag | traverseFlag,
writeElement);
#if HAVE_PARALLEL_DOMAIN_AMDIS

Thomas Witkowski
committed
fn += "-p" + lexical_cast<std::string>(MPI::COMM_WORLD.Get_rank()) + "-";
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];
sprintf(formatStr, "%%0%d.%df", indexLength, indexDecimals);
sprintf(timeStr, formatStr, adaptInfo ? adaptInfo->getTime() : 0.0);
fn += timeStr;
#if HAVE_PARALLEL_DOMAIN_AMDIS
paraFilename += timeStr;

Thomas Witkowski
committed
postfix += timeStr + paraviewFileExt;
}
if (writeAMDiSFormat) {
MacroWriter::writeMacro(dataCollectors[0],

Thomas Witkowski
committed
const_cast<char*>((fn + amdisMeshExt).c_str()),
adaptInfo ? adaptInfo->getTime() : 0.0);
MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str());

Thomas Witkowski
committed
ValueWriter::writeValues(dataCollectors[0],
(fn + amdisDataExt).c_str(),
adaptInfo ? adaptInfo->getTime() : 0.0);
MSG("value file written to %s\n", (fn + amdisDataExt).c_str());
}
if (writePeriodicFormat) {
MacroWriter::writePeriodicFile(dataCollectors[0],
(fn + periodicFileExt).c_str());
MSG("periodic file written to %s\n", (fn + periodicFileExt).c_str());
}
if (writeParaViewFormat) {
VtkWriter vtkWriter(&dataCollectors);
vtkWriter.setCompression(compression);
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
MSG("ParaView file written to %s\n", (fn + paraviewFileExt).c_str());
#if HAVE_PARALLEL_DOMAIN_AMDIS
if (MPI::COMM_WORLD.Get_rank() == 0) {
VtkWriter vtkWriter(&dataCollectors);
vtkWriter.updateAnimationFile(paraFilename + paraviewParallelFileExt,
¶viewAnimationFrames,
filename + ".pvd");
}
#else
VtkWriter vtkWriter(&dataCollectors);
vtkWriter.updateAnimationFile(fn + paraviewFileExt,
¶viewAnimationFrames,
if (writeDofFormat) {
DofWriter dofWriter(feSpace);
dofWriter.writeFile(fn + ".dof", solutionVecs);
}
if (writeArhFormat)
ArhWriter::write(fn + ".arh", feSpace->getMesh(), solutionVecs);

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

Thomas Witkowski
committed
#endif
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++)