output.hh 4.83 KB
Newer Older
1 2 3 4 5 6
#ifndef DUNE_DORIE_OUTPUT_WRITER_HH
#define DUNE_DORIE_OUTPUT_WRITER_HH

namespace Dune{
namespace Dorie{

7
/// Dummy Output Writer class. Does nothing.
8
template<typename Traits>
9 10
class OutputWriterBase
{
11 12
private:
	using RF = typename Traits::RF;
13 14 15 16 17
public:
	OutputWriterBase () = default;
	virtual ~OutputWriterBase () = default;

	/// Do nothing
18
	virtual void write_vtk_output (const RF time) {};
19 20
};

21 22 23 24 25 26
/// Default VTK Output Writer.
/** \tparam Traits BaseTraits struct
 *  \tparam GFS Solution GFS
 *  \tparam Param Parameter class
 *  \tparam U Solution Vector
 */
27 28
template<typename Traits, typename GFS, typename Param, typename U>
class OutputWriter : public OutputWriterBase<Traits>
29 30 31
{
private:

32
	using RF = typename Traits::RF;
33
	using GV = typename Traits::GV;
34
	
35 36 37 38 39 40
	using VTKWriter = Dune::VTKSequenceWriter<GV>;
	using UDGF = Dune::PDELab::DiscreteGridFunction<GFS,U>;
	using FluxDGF = Dune::Dorie::GradientFluxAdapter<GFS,U,Param>;
	using ConDGF = Dune::Dorie::ConductivityAdapter<Traits,Param>;
	using WaterDGF = Dune::Dorie::WaterContentAdapter<Traits,Param,UDGF>;
	using SatDGF = Dune::Dorie::SaturationAdapter<Traits,Param,UDGF>;
41

42
	VTKWriter vtkwriter;
43
	UDGF udgf;
44 45 46 47 48 49
	FluxDGF fluxdgf;
	ConDGF condgf;
	WaterDGF waterdgf;
	SatDGF satdgf;

	const Dune::VTK::OutputType output_type;
50 51

public:
52 53

	/// Set up the VTK output writer. Create adapters and stack them to the sequence writer
54 55 56 57 58 59
	/** \param _inifile Parameter file parser
	 *  \param gv LeafGridView
	 *  \param gfs Solution GFS
	 *  \param param Parameter class
	 *  \param unew Solution vector to write
	 */
60
	OutputWriter (Dune::ParameterTree& _inifile, GV& gv, GFS& gfs, Param& param, U& unew)
61
	: OutputWriterBase<Traits>(),
62
		vtkwriter(gv,
63 64 65
			_inifile.get<std::string>("output.fileName"),
			_inifile.get<std::string>("output.outputPath"),
			"./"),
66 67 68 69 70
		udgf(gfs,unew),
		fluxdgf(gfs,unew,param,udgf),
		condgf(gv,param),
		waterdgf(gv,param,udgf),
		satdgf(gv,param,udgf),
71 72 73 74 75 76 77 78 79
		output_type(_inifile.get<bool>("output.asciiVtk") ? 
			Dune::VTK::OutputType::ascii : Dune::VTK::OutputType::base64)
	{
		auto udgf_vtk = std::make_shared<Dune::PDELab::VTKGridFunctionAdapter<UDGF>>(udgf,"head");
		auto fluxdgf_vtk = std::make_shared<Dune::PDELab::VTKGridFunctionAdapter<FluxDGF>>(fluxdgf,"flux");
		auto condgf_vtk = std::make_shared<Dune::PDELab::VTKGridFunctionAdapter<ConDGF>>(condgf,"K_0");
		auto waterdgf_vtk = std::make_shared<Dune::PDELab::VTKGridFunctionAdapter<WaterDGF>>(waterdgf,"theta_w");
		auto satdgf_vtk = std::make_shared<Dune::PDELab::VTKGridFunctionAdapter<SatDGF>>(satdgf,"Theta");

80 81 82 83 84 85 86 87 88 89 90 91 92 93
		if (_inifile.get<bool>("output.vertexData")) {
			vtkwriter.addVertexData(udgf_vtk);
			vtkwriter.addVertexData(fluxdgf_vtk);
			vtkwriter.addVertexData(condgf_vtk);
			vtkwriter.addVertexData(waterdgf_vtk);
			vtkwriter.addVertexData(satdgf_vtk);
		}
		else {
			vtkwriter.addCellData(udgf_vtk);
			vtkwriter.addCellData(fluxdgf_vtk);
			vtkwriter.addCellData(condgf_vtk);
			vtkwriter.addCellData(waterdgf_vtk);
			vtkwriter.addCellData(satdgf_vtk);
		}
94 95 96
	}

	/// Write the VTK files for the current solution unew
97 98
	/** \param time Current simulation time, will be saved into output
	 *  \throw Dune::IOError Cannot write output 
99 100
	 *  	(VTKWriter has bad exception handling)
	 */
101
	void write_vtk_output (const RF time) override
102 103
	{
		try{
104
			vtkwriter.write(time,output_type);
105 106 107 108 109
		}
		catch(...){
			DUNE_THROW(Dune::IOError,"Cannot write VTK output!");
		}
	}
110

111 112
};

113 114
/// Factory class for creating an OutputWriter based on Traits
/** \tparam Traits Dune::Dorie::BaseTraits defining the Simulation
115 116
 *  The constexpr boolean enable_adaptivity in Dune::Dorie::BaseTraits will
 *  define which create() function is enabled at compile time.
117
 */
118
template<typename Traits, typename GFS, typename Param, typename U>
119
struct OutputWriterFactory
120 121
{
private:
122
	static constexpr bool enabled = Traits::write_output;
123

124 125
	using OWB = OutputWriterBase<Traits>;

126 127 128 129 130 131
	Dune::ParameterTree& inifile;
	typename Traits::GV& gv;
	GFS& gfs;
	Param& param;
	U& u;

132 133
public:

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	/// Create the factory. References will be forwarded to the writer.
	/** \param _inifile Parameter file parser
	 *  \param _gv GridView
	 *  \param _gfs GridFunctionSpace of the solution
	 *  \param _param Parameter class
	 *  \param _u Solution vector to write
	 */
	OutputWriterFactory (Dune::ParameterTree& _inifile, typename Traits::GV& _gv, GFS& _gfs, Param& _param, U& _u) :
		inifile(_inifile),
		gv(_gv),
		gfs(_gfs),
		param(_param),
		u(_u)
	{ }


150
	/// Create a dummy OutputWriter
151
	template<bool active = enabled>
152
	std::enable_if_t<!active,std::unique_ptr<OWB>> create ()
153
	{
154
		return std::make_unique<OWB>();
155 156
	}

157
	/// Create a true OutputWriter
158
	template<bool active = enabled>
159
	std::enable_if_t<active,std::unique_ptr<OWB>> create ()
160
	{
161
		std::unique_ptr<OWB> p;
162
		p = std::make_unique<OutputWriter<Traits,GFS,Param,U>>(inifile,gv,gfs,param,u);
163 164
		return p;
	}
165 166 167 168 169 170
};

} // namespace Dorie
} // namespace Dune

#endif // DUNE_DORIE_OUTPUT_WRITER_HH