#include "boost/lexical_cast.hpp" #include "FileWriter.h" #include "SystemVector.h" #include "Parameters.h" #include "TecPlotWriter.h" #include "ValueWriter.h" #include "MacroWriter.h" #include "VtkWriter.h" #include "PngWriter.h" #include "PovrayWriter.h" #include "FiniteElemSpace.h" #include "AdaptInfo.h" #include "Flag.h" #include "ElInfo.h" #include "Mesh.h" #include "OpenMP.h" #if HAVE_PARALLEL_DOMAIN_AMDIS #include "mpi.h" #endif namespace AMDiS { using boost::lexical_cast; FileWriter::FileWriter(std::string str, Mesh *m, DOFVector *vec) : name(str), mesh(m) { FUNCNAME("FileWriter::FileWriter()"); initialize(); feSpace = vec->getFeSpace(); solutionVecs.resize(1); solutionVecs[0] = vec; } FileWriter::FileWriter(std::string name_, Mesh *mesh_, std::vector< DOFVector* > vecs) : name(name_), mesh(mesh_) { FUNCNAME("FileWriter::FileWriter()"); initialize(); for (int i = 0; i < static_cast(vecs.size()); i++) TEST_EXIT(vecs[0]->getFeSpace() == vecs[i]->getFeSpace()) ("All FeSpace have to be equal!\n"); feSpace = vecs[0]->getFeSpace(); solutionVecs = vecs; } FileWriter::FileWriter(std::string name_, Mesh *mesh_, DOFVector< WorldVector > *vec) : 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(vec->getFeSpace(), ""); // Copy the components of the WorldVectors to different DOFVectors // of double values. DOFVector< WorldVector >::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]; feSpace = vec->getFeSpace(); } 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]; } void FileWriter::initialize() { tecplotExt = ".tec"; amdisMeshExt = ".mesh"; amdisDataExt = ".dat"; paraviewFileExt = ".vtu"; paraviewParallelFileExt = ".pvtu"; periodicFileExt = ".per"; writeTecPlotFormat = 0; writeAMDiSFormat = 0; writeParaViewFormat = 0; writeParaViewAnimation = 0; writePeriodicFormat = 0; writePngFormat = 0; writePovrayFormat = 0; pngType = 0; appendIndex = 0; indexLength = 5; indexDecimals = 3; tsModulo = 1; nTmpSolutions = 0; paraviewAnimationFrames.resize(0), compression = NONE; readParameters(); } void FileWriter::readParameters() { FUNCNAME("FileWriter::readParamters()"); GET_PARAMETER(0, name + "->filename", &filename); GET_PARAMETER(0, name + "->TecPlot format", "%d", &writeTecPlotFormat); GET_PARAMETER(0, name + "->TecPlot ext", &tecplotExt); 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); GET_PARAMETER(0, name + "->ParaView animation", "%d", &writeParaViewAnimation); GET_PARAMETER(0, name + "->ParaView ext", ¶viewFileExt); GET_PARAMETER(0, name + "->Periodic format", "%d", &writePeriodicFormat); GET_PARAMETER(0, name + "->Periodic ext", &periodicFileExt); GET_PARAMETER(0, name + "->PNG format", "%d", &writePngFormat); GET_PARAMETER(0, name + "->PNG type", "%d", &pngType); 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); 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); std::string compressionStr = ""; GET_PARAMETER(0, name + "->compression", &compressionStr); if (compressionStr == "gzip" || compressionStr == "gz") { compression = GZIP; } else if (compressionStr == "bzip2" || compressionStr == "bz2") { compression = BZIP2; } } 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()); if (writeElem) { for (int i = 0; i < static_cast(dataCollectors.size()); i++) dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], level, flag, writeElem); } else { for (int i = 0; i < static_cast(dataCollectors.size()); i++) dataCollectors[i] = new DataCollector(feSpace, solutionVecs[i], traverseLevel, flag | traverseFlag, writeElement); } std::string fn = filename; #if HAVE_PARALLEL_DOMAIN_AMDIS std::string paraFilename = fn; fn += "-p" + lexical_cast(MPI::COMM_WORLD.Get_rank()) + "-"; std::string postfix = ""; #endif 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; postfix += timeStr + paraviewFileExt; #endif } if (writeTecPlotFormat) { TecPlotWriter >::writeValues(solutionVecs[0], const_cast((fn + tecplotExt).c_str()), solutionVecs[0]->getName().c_str()); MSG("TecPlot file written to %s\n", (fn + tecplotExt).c_str()); } if (writeAMDiSFormat) { MacroWriter::writeMacro(dataCollectors[0], const_cast((fn + amdisMeshExt).c_str()), adaptInfo ? adaptInfo->getTime() : 0.0); MSG("macro file written to %s\n", (fn + amdisMeshExt).c_str()); 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 (writeParaViewAnimation) { #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, filename + ".pvd"); #endif } #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()); } #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(dataCollectors.size()); i++) delete dataCollectors[i]; } }