24#ifndef DUMUX_IO_VTK_OUTPUT_MODULE_HH
25#define DUMUX_IO_VTK_OUTPUT_MODULE_HH
31#include <dune/common/timer.hh>
32#include <dune/common/fvector.hh>
33#include <dune/common/typetraits.hh>
35#include <dune/geometry/type.hh>
36#include <dune/geometry/multilineargeometry.hh>
38#include <dune/grid/common/mcmgmapper.hh>
39#include <dune/grid/common/partitionset.hh>
40#include <dune/grid/io/file/vtk/vtkwriter.hh>
41#include <dune/grid/io/file/vtk/vtksequencewriter.hh>
42#include <dune/grid/common/partitionset.hh>
59template<
class Gr
idGeometry>
62 using GridView =
typename GridGeometry::GridView;
63 using Field = Vtk::template Field<GridView>;
64 static constexpr int dim = GridView::dimension;
76 const std::string&
name,
78 Dune::VTK::DataMode dm = Dune::VTK::conforming,
86 const auto precisionString = getParamFromGroup<std::string>(
paramGroup,
"Vtk.Precision",
"Float32");
89 writer_ = std::make_shared<Dune::VTKWriter<GridView>>(
gridGeometry.gridView(), dm, coordPrecision);
90 sequenceWriter_ = std::make_unique<Dune::VTKSequenceWriter<GridView>>(writer_,
name);
97 {
return paramGroup_; }
108 template<
typename Vector>
110 const std::string&
name,
124 template<
typename Vector>
126 const std::string&
name,
131 const auto nComp = getNumberOfComponents_(v);
133 const auto numElemDofs =
gridGeometry().elementMapper().size();
134 const auto numVertexDofs =
gridGeometry().vertexMapper().size();
139 if(numElemDofs == numVertexDofs)
140 DUNE_THROW(Dune::InvalidStateException,
"Automatic deduction of FieldType failed. Please explicitly specify FieldType::element or FieldType::vertex.");
142 if(v.size() == numElemDofs)
144 else if(v.size() == numVertexDofs)
147 DUNE_THROW(Dune::RangeError,
"Size mismatch of added field!");
153 if(v.size() != numElemDofs)
154 DUNE_THROW(Dune::RangeError,
"Size mismatch of added field!");
157 if(v.size() != numVertexDofs)
158 DUNE_THROW(Dune::RangeError,
"Size mismatch of added field!");
163 fields_.emplace_back(gridGeometry_.gridView(), gridGeometry_.elementMapper(), v,
name, nComp, 0, dm_,
precision);
165 fields_.emplace_back(gridGeometry_.gridView(), gridGeometry_.vertexMapper(), v,
name, nComp, dim, dm_,
precision);
173 void write(
double time, Dune::VTK::OutputType type = Dune::VTK::ascii)
178 if (dm_ == Dune::VTK::conforming)
179 writeConforming_(time, type);
180 else if (dm_ == Dune::VTK::nonconforming)
181 writeNonConforming_(time, type);
183 DUNE_THROW(Dune::NotImplemented,
"Output for provided VTK data mode");
188 std::cout << Fmt::format(
"Writing output for problem \"{}\". Took {:.2g} seconds.\n", name_, timer.elapsed());
195 const std::string&
name()
const {
return name_; }
196 Dune::VTK::DataMode
dataMode()
const {
return dm_; }
197 Dumux::Vtk::Precision
precision()
const {
return precision_; }
199 Dune::VTKWriter<GridView>&
writer() {
return *writer_; }
202 const std::vector<Field>&
fields()
const {
return fields_; }
206 virtual void writeConforming_(
double time, Dune::VTK::OutputType type)
213 static bool addProcessRank = getParamFromGroup<bool>(this->
paramGroup(),
"Vtk.AddProcessRank");
214 std::vector<int> rank;
217 if (!fields_.empty() || addProcessRank)
219 const auto numCells = gridGeometry_.gridView().size(0);
224 rank.resize(numCells);
226 for (
const auto& element : elements(gridGeometry_.gridView(), Dune::Partitions::interior))
228 const auto eIdxGlobal = gridGeometry_.elementMapper().index(element);
229 rank[eIdxGlobal] = gridGeometry_.gridView().comm().rank();
239 this->
sequenceWriter().addCellData(Field(gridGeometry_.gridView(), gridGeometry_.elementMapper(), rank,
"process rank", 1, 0).get());
242 for (
auto&& field : fields_)
244 if (field.codim() == 0)
246 else if (field.codim() == dim)
249 DUNE_THROW(Dune::RangeError,
"Cannot add wrongly sized vtk scalar field!");
265 virtual void writeNonConforming_(
double time, Dune::VTK::OutputType type)
267 DUNE_THROW(Dune::NotImplemented,
"Non-conforming VTK output");
271 template<
class Vector>
272 std::size_t getNumberOfComponents_(
const Vector& v)
274 if constexpr (Dune::IsIndexable<decltype(std::declval<Vector>()[0])>::value)
280 const GridGeometry& gridGeometry_;
282 const std::string paramGroup_;
283 Dune::VTK::DataMode dm_;
285 Dumux::Vtk::Precision precision_;
287 std::shared_ptr<Dune::VTKWriter<GridView>> writer_;
288 std::unique_ptr<Dune::VTKSequenceWriter<GridView>> sequenceWriter_;
290 std::vector<Field> fields_;
305template<
class Gr
idVariables,
class SolutionVector>
309 using GridGeometry =
typename GridVariables::GridGeometry;
311 using VV =
typename GridVariables::VolumeVariables;
312 using Scalar =
typename GridVariables::Scalar;
314 using GridView =
typename GridGeometry::GridView;
317 dim = GridView::dimension,
318 dimWorld = GridView::dimensionworld
321 using Element =
typename GridView::template Codim<0>::Entity;
322 using VolVarsVector = Dune::FieldVector<Scalar, dimWorld>;
325 static constexpr int dofCodim = isBox ? dim : 0;
327 struct VolVarScalarDataInfo { std::function<Scalar(
const VV&)> get; std::string
name; Dumux::Vtk::Precision precision_; };
328 struct VolVarVectorDataInfo { std::function<VolVarsVector(
const VV&)> get; std::string
name; Dumux::Vtk::Precision precision_; };
329 using Field = Vtk::template Field<GridView>;
338 const SolutionVector&
sol,
339 const std::string&
name,
341 Dune::VTK::DataMode dm = Dune::VTK::conforming,
367 const std::string&
name)
369 volVarScalarDataInfo_.push_back(VolVarScalarDataInfo{f,
name, this->
precision()});
376 template<
class VVV = VolVarsVector,
typename std::enable_if_t<(VVV::dimension > 1),
int> = 0>
378 const std::string&
name)
380 volVarVectorDataInfo_.push_back(VolVarVectorDataInfo{f,
name, this->
precision()});
385 const auto&
problem()
const {
return gridVariables_.curGridVolVars().problem(); }
387 const GridGeometry&
gridGeometry()
const {
return gridVariables_.gridGeometry(); }
388 const SolutionVector&
sol()
const {
return sol_; }
399 void writeConforming_(
double time, Dune::VTK::OutputType type)
override
401 const Dune::VTK::DataMode dm = Dune::VTK::conforming;
408 std::vector<VelocityVector> velocity(velocityOutput_->numFluidPhases());
411 static bool addProcessRank = getParamFromGroup<bool>(this->
paramGroup(),
"Vtk.AddProcessRank");
412 std::vector<double> rank;
415 std::vector<std::vector<Scalar>> volVarScalarData;
416 std::vector<std::vector<VolVarsVector>> volVarVectorData;
419 if (!volVarScalarDataInfo_.empty()
420 || !volVarVectorDataInfo_.empty()
421 || !this->fields().empty()
422 || velocityOutput_->enableOutput()
425 const auto numCells =
gridGeometry().gridView().size(0);
426 const auto numDofs = numDofs_();
429 if (!volVarScalarDataInfo_.empty())
430 volVarScalarData.resize(volVarScalarDataInfo_.size(), std::vector<Scalar>(numDofs));
431 if (!volVarVectorDataInfo_.empty())
432 volVarVectorData.resize(volVarVectorDataInfo_.size(), std::vector<VolVarsVector>(numDofs));
434 if (velocityOutput_->enableOutput())
436 for (
int phaseIdx = 0; phaseIdx < velocityOutput_->numFluidPhases(); ++phaseIdx)
438 if(isBox && dim == 1)
439 velocity[phaseIdx].resize(numCells);
441 velocity[phaseIdx].resize(numDofs);
446 if (addProcessRank) rank.resize(numCells);
448 for (
const auto& element : elements(
gridGeometry().gridView(), Dune::Partitions::interior))
450 const auto eIdxGlobal =
gridGeometry().elementMapper().index(element);
453 auto elemVolVars =
localView(gridVariables_.curGridVolVars());
457 if (velocityOutput_->enableOutput())
459 fvGeometry.bind(element);
460 elemVolVars.bind(element, fvGeometry, sol_);
464 fvGeometry.bindElement(element);
465 elemVolVars.bindElement(element, fvGeometry, sol_);
468 if (!volVarScalarDataInfo_.empty()
469 || !volVarVectorDataInfo_.empty())
471 for (
auto&& scv : scvs(fvGeometry))
473 const auto dofIdxGlobal = scv.dofIndex();
474 const auto& volVars = elemVolVars[scv];
477 for (std::size_t i = 0; i < volVarScalarDataInfo_.size(); ++i)
478 volVarScalarData[i][dofIdxGlobal] = volVarScalarDataInfo_[i].get(volVars);
481 for (std::size_t i = 0; i < volVarVectorDataInfo_.size(); ++i)
482 volVarVectorData[i][dofIdxGlobal] = volVarVectorDataInfo_[i].get(volVars);
487 if (velocityOutput_->enableOutput())
489 auto elemFluxVarsCache =
localView(gridVariables_.gridFluxVarsCache());
490 elemFluxVarsCache.bind(element, fvGeometry, elemVolVars);
492 for (
int phaseIdx = 0; phaseIdx < velocityOutput_->numFluidPhases(); ++phaseIdx)
493 velocityOutput_->calculateVelocity(velocity[phaseIdx], element, fvGeometry, elemVolVars, elemFluxVarsCache, phaseIdx);
498 rank[eIdxGlobal] =
static_cast<double>(
gridGeometry().gridView().comm().rank());
508 for (std::size_t i = 0; i < volVarScalarDataInfo_.size(); ++i)
510 volVarScalarDataInfo_[i].
name, 1, dim, dm, this->
precision()).get() );
511 for (std::size_t i = 0; i < volVarVectorDataInfo_.size(); ++i)
513 volVarVectorDataInfo_[i].
name, dimWorld, dim, dm, this->
precision()).get() );
517 for (std::size_t i = 0; i < volVarScalarDataInfo_.size(); ++i)
519 volVarScalarDataInfo_[i].
name, 1, 0,dm, this->
precision()).get() );
520 for (std::size_t i = 0; i < volVarVectorDataInfo_.size(); ++i)
522 volVarVectorDataInfo_[i].
name, dimWorld, 0,dm, this->
precision()).get() );
526 if (velocityOutput_->enableOutput())
528 if (isBox && dim > 1)
530 for (
int phaseIdx = 0; phaseIdx < velocityOutput_->numFluidPhases(); ++phaseIdx)
532 "velocity_" + velocityOutput_->phaseName(phaseIdx) +
" (m/s)",
533 dimWorld, dim, dm, this->precision()).get() );
538 for (
int phaseIdx = 0; phaseIdx < velocityOutput_->numFluidPhases(); ++phaseIdx)
540 "velocity_" + velocityOutput_->phaseName(phaseIdx) +
" (m/s)",
541 dimWorld, 0, dm, this->precision()).get() );
550 for (
auto&& field : this->
fields())
552 if (field.codim() == 0)
554 else if (field.codim() == dim)
557 DUNE_THROW(Dune::RangeError,
"Cannot add wrongly sized vtk scalar field!");
573 void writeNonConforming_(
double time, Dune::VTK::OutputType type)
override
575 const Dune::VTK::DataMode dm = Dune::VTK::nonconforming;
578 DUNE_THROW(Dune::InvalidStateException,
"Non-conforming output makes no sense for cell-centered schemes!");
585 bool enableVelocityOutput = getParamFromGroup<bool>(this->
paramGroup(),
"Vtk.AddVelocity");
586 if (enableVelocityOutput ==
true && !velocityOutput_->enableOutput())
587 std::cerr <<
"Warning! Velocity output was enabled in the input file"
588 <<
" but no velocity output policy was set for the VTK output module:"
589 <<
" There will be no velocity output."
590 <<
" Use the addVelocityOutput member function of the VTK output module." << std::endl;
592 std::vector<VelocityVector> velocity(velocityOutput_->numFluidPhases());
595 static bool addProcessRank = getParamFromGroup<bool>(this->
paramGroup(),
"Vtk.AddProcessRank");
596 std::vector<double> rank;
599 using ScalarDataContainer = std::vector< std::vector<Scalar> >;
600 using VectorDataContainer = std::vector< std::vector<VolVarsVector> >;
601 std::vector< ScalarDataContainer > volVarScalarData;
602 std::vector< VectorDataContainer > volVarVectorData;
605 if (!volVarScalarDataInfo_.empty()
606 || !volVarVectorDataInfo_.empty()
607 || !this->fields().empty()
608 || velocityOutput_->enableOutput()
611 const auto numCells =
gridGeometry().gridView().size(0);
612 const auto numDofs = numDofs_();
615 if (!volVarScalarDataInfo_.empty())
616 volVarScalarData.resize(volVarScalarDataInfo_.size(), ScalarDataContainer(numCells));
617 if (!volVarVectorDataInfo_.empty())
618 volVarVectorData.resize(volVarVectorDataInfo_.size(), VectorDataContainer(numCells));
620 if (velocityOutput_->enableOutput())
622 for (
int phaseIdx = 0; phaseIdx < velocityOutput_->numFluidPhases(); ++phaseIdx)
624 if(isBox && dim == 1)
625 velocity[phaseIdx].resize(numCells);
627 velocity[phaseIdx].resize(numDofs);
632 if (addProcessRank) rank.resize(numCells);
634 for (
const auto& element : elements(
gridGeometry().gridView(), Dune::Partitions::interior))
636 const auto eIdxGlobal =
gridGeometry().elementMapper().index(element);
640 auto elemVolVars =
localView(gridVariables_.curGridVolVars());
643 for (std::size_t i = 0; i < volVarScalarDataInfo_.size(); ++i)
644 volVarScalarData[i][eIdxGlobal].resize(
numCorners);
645 for (std::size_t i = 0; i < volVarVectorDataInfo_.size(); ++i)
646 volVarVectorData[i][eIdxGlobal].resize(
numCorners);
650 if (velocityOutput_->enableOutput())
652 fvGeometry.bind(element);
653 elemVolVars.bind(element, fvGeometry, sol_);
657 fvGeometry.bindElement(element);
658 elemVolVars.bindElement(element, fvGeometry, sol_);
661 if (!volVarScalarDataInfo_.empty()
662 || !volVarVectorDataInfo_.empty())
664 for (
auto&& scv : scvs(fvGeometry))
666 const auto& volVars = elemVolVars[scv];
669 for (std::size_t i = 0; i < volVarScalarDataInfo_.size(); ++i)
670 volVarScalarData[i][scv.elementIndex()][scv.localDofIndex()] = volVarScalarDataInfo_[i].get(volVars);
673 for (std::size_t i = 0; i < volVarVectorDataInfo_.size(); ++i)
674 volVarVectorData[i][scv.elementIndex()][scv.localDofIndex()] = volVarVectorDataInfo_[i].get(volVars);
679 if (velocityOutput_->enableOutput())
681 auto elemFluxVarsCache =
localView(gridVariables_.gridFluxVarsCache());
682 elemFluxVarsCache.bind(element, fvGeometry, elemVolVars);
684 for (
int phaseIdx = 0; phaseIdx < velocityOutput_->numFluidPhases(); ++phaseIdx)
685 velocityOutput_->calculateVelocity(velocity[phaseIdx], element, fvGeometry, elemVolVars, elemFluxVarsCache, phaseIdx);
690 rank[eIdxGlobal] =
static_cast<double>(
gridGeometry().gridView().comm().rank());
698 for (std::size_t i = 0; i < volVarScalarDataInfo_.size(); ++i)
700 volVarScalarDataInfo_[i].
name, 1, dim, dm, this->
precision()).get() );
702 for (std::size_t i = 0; i < volVarVectorDataInfo_.size(); ++i)
704 volVarVectorDataInfo_[i].
name, dimWorld, dim, dm, this->
precision()).get() );
707 if (velocityOutput_->enableOutput())
711 for (
int phaseIdx = 0; phaseIdx < velocityOutput_->numFluidPhases(); ++phaseIdx)
713 "velocity_" + velocityOutput_->phaseName(phaseIdx) +
" (m/s)",
714 dimWorld, dim, dm, this->precision()).get() );
718 for (
int phaseIdx = 0; phaseIdx < velocityOutput_->numFluidPhases(); ++phaseIdx)
720 "velocity_" + velocityOutput_->phaseName(phaseIdx) +
" (m/s)",
721 dimWorld, 0,dm, this->precision()).get());
729 for (
auto&& field : this->
fields())
731 if (field.codim() == 0)
733 else if (field.codim() == dim)
736 DUNE_THROW(Dune::RangeError,
"Cannot add wrongly sized vtk scalar field!");
752 std::size_t numDofs_()
const {
return dofCodim == dim ?
gridGeometry().vertexMapper().size() :
gridGeometry().elementMapper().size(); }
754 const GridVariables& gridVariables_;
755 const SolutionVector& sol_;
757 std::vector<VolVarScalarDataInfo> volVarScalarDataInfo_;
758 std::vector<VolVarVectorDataInfo> volVarVectorDataInfo_;
760 std::shared_ptr<VelocityOutput> velocityOutput_;
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
The available discretization methods in Dumux.
Vtk field types available in Dumux.
Dune style VTK functions.
Formatting based on the fmt-library which implements std::format of C++20.
GridCache::LocalView localView(const GridCache &gridCache)
Free function to get the local view of a grid cache object.
Definition: localview.hh:38
Precision stringToPrecision(std::string_view precisionName)
Maps a string (e.g. from input) to a Dune precision type.
Definition: precision.hh:39
FieldType
Identifier for vtk field types.
Definition: fieldtype.hh:34
std::size_t numCorners(Shape shape)
Returns the number of corners of a given geometry.
Definition: throatproperties.hh:215
Velocity output for implicit (porous media) models.
Definition: io/velocityoutput.hh:41
std::vector< Dune::FieldVector< Scalar, dimWorld > > VelocityVector
Definition: io/velocityoutput.hh:50
A VTK output module to simplify writing dumux simulation data to VTK format.
Definition: io/vtkoutputmodule.hh:61
Dumux::Vtk::Precision precision() const
Definition: io/vtkoutputmodule.hh:197
const std::string & paramGroup() const
the parameter group for getting parameter from the parameter tree
Definition: io/vtkoutputmodule.hh:96
void write(double time, Dune::VTK::OutputType type=Dune::VTK::ascii)
Definition: io/vtkoutputmodule.hh:173
VtkOutputModuleBase(const GridGeometry &gridGeometry, const std::string &name, const std::string ¶mGroup="", Dune::VTK::DataMode dm=Dune::VTK::conforming, bool verbose=true)
Definition: io/vtkoutputmodule.hh:75
Dune::VTK::DataMode dataMode() const
Definition: io/vtkoutputmodule.hh:196
Dune::VTKWriter< GridView > & writer()
Definition: io/vtkoutputmodule.hh:199
Dune::VTKSequenceWriter< GridView > & sequenceWriter()
Definition: io/vtkoutputmodule.hh:200
const std::string & name() const
Definition: io/vtkoutputmodule.hh:195
const std::vector< Field > & fields() const
Definition: io/vtkoutputmodule.hh:202
virtual ~VtkOutputModuleBase()=default
void addField(const Vector &v, const std::string &name, Dumux::Vtk::Precision precision, Vtk::FieldType fieldType=Vtk::FieldType::automatic)
Add a scalar or vector valued vtk field.
Definition: io/vtkoutputmodule.hh:125
bool verbose() const
Definition: io/vtkoutputmodule.hh:194
void addField(const Vector &v, const std::string &name, Vtk::FieldType fieldType=Vtk::FieldType::automatic)
Add a scalar or vector valued vtk field.
Definition: io/vtkoutputmodule.hh:109
const GridGeometry & gridGeometry() const
Definition: io/vtkoutputmodule.hh:192
export field type
Definition: io/vtkoutputmodule.hh:69
A VTK output module to simplify writing dumux simulation data to VTK format.
Definition: io/vtkoutputmodule.hh:307
const auto & problem() const
Definition: io/vtkoutputmodule.hh:385
const GridVariables & gridVariables() const
Definition: io/vtkoutputmodule.hh:386
void addVolumeVariable(std::function< VolVarsVector(const VolumeVariables &)> &&f, const std::string &name)
Definition: io/vtkoutputmodule.hh:377
VV VolumeVariables
export type of the volume variables for the outputfields
Definition: io/vtkoutputmodule.hh:335
void addVolumeVariable(std::function< Scalar(const VolumeVariables &)> &&f, const std::string &name)
Definition: io/vtkoutputmodule.hh:366
void addVelocityOutput(std::shared_ptr< VelocityOutputType > velocityOutput)
Add a velocity output policy.
Definition: io/vtkoutputmodule.hh:360
const VelocityOutput & velocityOutput() const
Definition: io/vtkoutputmodule.hh:394
const SolutionVector & sol() const
Definition: io/vtkoutputmodule.hh:388
const GridGeometry & gridGeometry() const
Definition: io/vtkoutputmodule.hh:387
VtkOutputModule(const GridVariables &gridVariables, const SolutionVector &sol, const std::string &name, const std::string ¶mGroup="", Dune::VTK::DataMode dm=Dune::VTK::conforming, bool verbose=true)
Definition: io/vtkoutputmodule.hh:337
const std::vector< VolVarScalarDataInfo > & volVarScalarDataInfo() const
Definition: io/vtkoutputmodule.hh:390
const std::vector< VolVarVectorDataInfo > & volVarVectorDataInfo() const
Definition: io/vtkoutputmodule.hh:391
Velocity output for porous media models.