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

Santiago Ospina's avatar
Santiago Ospina committed
4
5
6
7
8
9
#include <dune/common/parametertreeparser.hh>

#include <dune/grid/io/file/vtk/vtksequencewriter.hh>

#include "../solver/util_vtk_adapters.hh"

10
11
12
namespace Dune{
namespace Dorie{

13
/// Dummy Output Writer class. Does nothing.
14
template<typename Traits>
15
16
class OutputWriterBase
{
17
18
private:
	using RF = typename Traits::RF;
19
20
21
22
23
public:
	OutputWriterBase () = default;
	virtual ~OutputWriterBase () = default;

	/// Do nothing
24
	virtual void write_vtk_output (const RF time) {};
25
26
};

27
28
29
30
31
32
/// Default VTK Output Writer.
/** \tparam Traits BaseTraits struct
 *  \tparam GFS Solution GFS
 *  \tparam Param Parameter class
 *  \tparam U Solution Vector
 */
33
34
template<typename Traits, typename GFS, typename Param, typename U>
class OutputWriter : public OutputWriterBase<Traits>
35
36
37
{
private:

38
	using RF = typename Traits::RF;
39
	using GV = typename Traits::GV;
40
	
41
42
43
44
45
46
	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>;
47

48
	VTKWriter vtkwriter;
49
	UDGF udgf;
50
51
52
53
54
55
	FluxDGF fluxdgf;
	ConDGF condgf;
	WaterDGF waterdgf;
	SatDGF satdgf;

	const Dune::VTK::OutputType output_type;
56
57

public:
58
59

	/// Set up the VTK output writer. Create adapters and stack them to the sequence writer
60
61
62
63
64
65
	/** \param _inifile Parameter file parser
	 *  \param gv LeafGridView
	 *  \param gfs Solution GFS
	 *  \param param Parameter class
	 *  \param unew Solution vector to write
	 */
66
	OutputWriter (Dune::ParameterTree& _inifile, GV& gv, GFS& gfs, Param& param, U& unew)
67
	: OutputWriterBase<Traits>(),
68
		vtkwriter(gv,
69
70
			_inifile.get<std::string>("output.fileName"),
			_inifile.get<std::string>("output.outputPath"),
71
72
			"./",
			Dune::VTK::nonconforming),
73
74
75
76
77
		udgf(gfs,unew),
		fluxdgf(gfs,unew,param,udgf),
		condgf(gv,param),
		waterdgf(gv,param,udgf),
		satdgf(gv,param,udgf),
78
79
80
81
82
83
84
85
86
		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");

87
88
89
90
91
92
93
94
95
96
97
98
99
100
		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);
		}
101
102
103
	}

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

118
119
};

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

131
132
	using OWB = OutputWriterBase<Traits>;

133
134
135
136
137
138
	Dune::ParameterTree& inifile;
	typename Traits::GV& gv;
	GFS& gfs;
	Param& param;
	U& u;

139
140
public:

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	/// 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)
	{ }


157
	/// Create a dummy OutputWriter
158
	template<bool active = enabled>
159
	std::enable_if_t<!active,std::unique_ptr<OWB>> create ()
160
	{
161
		return std::make_unique<OWB>();
162
163
	}

164
	/// Create a true OutputWriter
165
	template<bool active = enabled>
166
	std::enable_if_t<active,std::unique_ptr<OWB>> create ()
167
	{
168
		std::unique_ptr<OWB> p;
169
		p = std::make_unique<OutputWriter<Traits,GFS,Param,U>>(inifile,gv,gfs,param,u);
170
171
		return p;
	}
172
173
174
175
176
177
};

} // namespace Dorie
} // namespace Dune

#endif // DUNE_DORIE_OUTPUT_WRITER_HH