Skip to content
Snippets Groups Projects
Commit 721fda7d authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

Merge branch 'issue/parallel_filewriter' into 'master'

parallel filewriter

See merge request !76
parents 78b54590 ac7e15fe
No related branches found
No related tags found
1 merge request!76parallel filewriter
......@@ -71,6 +71,7 @@ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/amdis)
add_subdirectory("common")
add_subdirectory("functions")
add_subdirectory("gridfunctions")
add_subdirectory("io")
add_subdirectory("linearalgebra")
add_subdirectory("localoperators")
add_subdirectory("operations")
......
......@@ -5,8 +5,7 @@
#include <dune/functions/functionspacebases/lagrangebasis.hh>
#include <dune/grid/io/file/vtk/vtkwriter.hh>
#include <dune/grid/io/file/vtk/vtksequencewriter.hh>
//#include <dune/geometry/referenceelements.hh>
#include <dune/grid/io/file/vtk/subsamplingvtkwriter.hh>
#include <dune/typetree/childextraction.hh>
#include <amdis/FileWriterInterface.hpp>
......@@ -15,6 +14,7 @@
#include <amdis/common/StaticSize.hpp>
#include <amdis/common/ValueCategory.hpp>
#include <amdis/gridfunctions/DiscreteFunction.hpp>
#include <amdis/io/VTKSequenceWriter.hpp>
namespace AMDiS
{
......@@ -86,9 +86,11 @@ namespace AMDiS
vtkWriter_ = std::make_shared<Dune::VTKWriter<GridView>>(gridView());
if (animation_)
vtkSeqWriter_ = std::make_shared<Dune::VTKSequenceWriter<GridView>>(vtkWriter_, filename_, dir_, "");
vtkSeqWriter_ = std::make_shared<VTKSequenceWriter<GridView>>(vtkWriter_);
vtkWriter_->addVertexData(discreteFct_, Dune::VTK::FieldInfo(name_, VTKFieldType<Range>, VTKFieldSize<Range>));
test_exit(dir_ == "." || filesystem::exists(dir_), "Output directory '{}' does not exist!",dir_);
}
void init(std::string const&, tag::unknown) {}
......@@ -96,12 +98,11 @@ namespace AMDiS
/// Implements \ref FileWriterInterface::writeFiles
void writeFiles(AdaptInfo& adaptInfo, bool force) override
{
test_exit(dir_ == "." || filesystem::exists(dir_), "Output directory '{}' does not exist!",dir_);
filesystem::create_directories(dir_ + "/_piecefiles");
if (vtkSeqWriter_)
vtkSeqWriter_->write(adaptInfo.time(), mode_);
vtkSeqWriter_->pwrite(adaptInfo.time(), filename_, dir_, "_piecefiles", mode_);
else if (vtkWriter_)
vtkWriter_->write(filesystem::path({dir_, filename_}).string(), mode_);
vtkWriter_->pwrite(filename_, dir_, "_piecefiles", mode_);
}
protected:
......@@ -114,7 +115,7 @@ namespace AMDiS
DiscreteFunction<GB,VT,TP> discreteFct_;
std::shared_ptr<Dune::VTKWriter<GridView>> vtkWriter_;
std::shared_ptr<Dune::VTKSequenceWriter<GridView>> vtkSeqWriter_;
std::shared_ptr<VTKSequenceWriter<GridView>> vtkSeqWriter_;
// write .pvd if animation=true, otherwise write only .vtu
bool animation_;
......
install(FILES
VtkSequenceWriter.hpp
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/amdis/io)
#pragma once
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <memory>
#include <dune/grid/io/file/vtk/common.hh>
#include <dune/common/path.hh>
#include <dune/grid/io/file/vtk/vtkwriter.hh>
namespace AMDiS
{
/// \brief class to write pvd-files which contains a list of all collected vtk-files
/**
* Write pvd-file suitable for easy visualization with
* <a href="http://www.vtk.org/">The Visualization Toolkit (VTK)</a>.
*
* \tparam GridView Grid view of the grid we are writing
*/
template <class GridView>
class VTKSequenceWriter
{
using VTKWriter = Dune::VTKWriter<GridView>;
public:
/// \brief Set up the VTKSequenceWriter class
/**
* \param vtkWriter Writer object used to write the individual time step data files
*/
VTKSequenceWriter(std::shared_ptr<VTKWriter> vtkWriter)
: vtkWriter_(std::move(vtkWriter))
{}
/// \brief Set up the VTKSequenceWriter class by creating a timestep writer of type \ref VTKWriter
/**
* \param gridView GridView object passed to the constructor of the VTKWriter
* \param args... Additional arguments forwarded to the VTKWriter constructor.
*/
template <class... Args>
VTKSequenceWriter(GridView const& gridView, Args&&... args)
: VTKSequenceWriter<GridView>(std::make_shared<VTKWriter>(gridView, FWD(args)...))
{}
virtual ~VTKSequenceWriter() = default;
/// accessor for the underlying VTKWriter instance
std::shared_ptr<VTKWriter> const& vtkWriter() const
{
return vtkWriter_;
}
/// \brief Adds a field of cell data to the VTK file
template <class... Args>
void addCellData(Args&&... args)
{
vtkWriter_->addCellData(FWD(args)...);
}
/// \brief Adds a field of vertex data to the VTK file
template <class... Args>
void addVertexData (Args&&... args)
{
vtkWriter_->addVertexData(FWD(args)...);
}
/// \brief Writes VTK data for the given time,
/**
* \param time The time(step) for the data to be written.
* \param name The basename of the .pvd file (without file extension)
* \param type VTK output type.
*/
virtual void write(double time, std::string const& name, Dune::VTK::OutputType type = Dune::VTK::ascii)
{
pwrite(time, name, ".", "", type);
}
/// \brief Writes VTK data for the given time,
/**
* \param time The time(step) for the data to be written.
* \param name The basename of the .pvd file (without file extension)
* \param path The directory where to put the .pvd file
* \param extendpath The (relative) subdirectory to path, where to put the timestep files
* \param type VTK output type.
*/
virtual void pwrite(double time, std::string const& name, std::string const& path, std::string const& extendpath,
Dune::VTK::OutputType type = Dune::VTK::ascii)
{
// remember current time step
unsigned int count = timesteps_.size();
timesteps_.push_back(time);
// write VTK file
vtkWriter_->pwrite(seqName(name, count), Dune::concatPaths(path, extendpath), "", type);
// write pvd file ... only on rank 0
if (Environment::mpiRank() == 0) {
std::ofstream pvdFile;
pvdFile.exceptions(std::ios_base::badbit | std::ios_base::failbit |
std::ios_base::eofbit);
std::string pvdname = path + "/" + name + ".pvd";
pvdFile.open(pvdname.c_str());
pvdFile << "<?xml version=\"1.0\"?>\n"
<< "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"" << Dune::VTK::getEndiannessString() << "\">\n"
<< "<Collection>\n";
for (unsigned int i = 0; i <= count; ++i) {
std::string piecepath = extendpath;
std::string fullname = getParallelHeaderName(seqName(name,i), piecepath, Environment::mpiSize());
pvdFile << "<DataSet timestep=\"" << timesteps_[i]
<< "\" part=\"0\" file=\""
<< fullname << "\"/>\n";
}
pvdFile << "</Collection>\n"
<< "</VTKFile>\n" << std::flush;
pvdFile.close();
}
}
protected:
// create sequence name
static std::string seqName(std::string const& name, unsigned int count)
{
std::stringstream n;
n.fill('0');
n << name << "-" << std::setw(5) << count;
return n.str();
}
static std::string getParallelHeaderName(std::string const& name, std::string const& path, int commSize)
{
std::ostringstream s;
if (path.size() > 0) {
s << path;
if (path[path.size()-1] != '/')
s << '/';
}
s << 's' << std::setw(4) << std::setfill('0') << commSize << '-';
s << name;
if (GridView::dimension > 1)
s << ".pvtu";
else
s << ".pvtp";
return s.str();
}
private:
std::shared_ptr<VTKWriter> vtkWriter_;
std::vector<double> timesteps_;
};
} // end namespace AMDiS
......@@ -28,8 +28,7 @@ dune_add_test(SOURCES DOFVectorTest.cpp
LINK_LIBRARIES amdis)
dune_add_test(SOURCES DiscreteFunctionTest.cpp
LINK_LIBRARIES amdis
CMD_ARGS "${CMAKE_SOURCE_DIR}/examples/init/ellipt.dat.2d")
LINK_LIBRARIES amdis)
dune_add_test(SOURCES ExpressionsTest.cpp
LINK_LIBRARIES amdis)
......@@ -65,8 +64,7 @@ dune_add_test(SOURCES ISTLCommTest.cpp
CMAKE_GUARD "MPI_FOUND AND dune-istl_FOUND")
dune_add_test(SOURCES IntegrateTest.cpp
LINK_LIBRARIES amdis
CMD_ARGS "${CMAKE_SOURCE_DIR}/examples/init/ellipt.dat.2d")
LINK_LIBRARIES amdis)
dune_add_test(SOURCES MarkerTest.cpp
LINK_LIBRARIES amdis)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment