1 #ifndef LIBGEODECOMP_IO_WRITER_H
2 #define LIBGEODECOMP_IO_WRITER_H
3 
4 #include <libgeodecomp/config.h>
5 
6 #ifdef LIBGEODECOMP_WITH_MPI
7 #include <mpi.h>
8 #endif
9 
10 #include <libgeodecomp/parallelization/monolithicsimulator.h>
11 
12 #include <string>
13 #include <stdexcept>
14 
15 namespace LibGeoDecomp {
16 
17 enum WriterEvent {
18     WRITER_INITIALIZED,
19     WRITER_STEP_FINISHED,
20     WRITER_ALL_DONE
21 };
22 
23 template <class CELL_TYPE> class MonolithicSimulator;
24 
25 /**
26  * Writer and ParallelWriter are the superclasses for all output
27  * formats. Writer is for use with MonolithicSimulator and its heirs.
28  * It defines three callbacks which are invoked by the simulator. The
29  * prefix may be used by file-based IO objects to generate file names.
30  * Usually one will add a time step number and a suitable extension.
31  */
32 template<typename CELL_TYPE>
33 class Writer
34 {
35 public:
36     friend class Serialization;
37     friend class WriterTest;
38 
39     typedef typename MonolithicSimulator<CELL_TYPE>::GridType GridType;
40     typedef typename APITraits::SelectTopology<CELL_TYPE>::Value Topology;
41     const static int DIM = Topology::DIM;
42     static const unsigned NANO_STEPS = APITraits::SelectNanoSteps<CELL_TYPE>::VALUE;
43 
44     /**
45      * initializes a writer using prefix which subclasses may
46      * use to generate filenames. period should be used by
47      * them to control how many time steps lie between outputs. The
48      * Writer will register at sim which in turn will delete
49      * the Writer. Thus a writer should always be constructed via
50      * new(), unless sim is 0.
51      */
Writer(const std::string & prefix,const unsigned period)52     Writer(
53         const std::string& prefix,
54         const unsigned period) :
55         prefix(prefix),
56         period(period)
57     {
58         if (period == 0) {
59             throw std::invalid_argument("period must be positive");
60         }
61     }
62 
~Writer()63     virtual ~Writer()
64     {}
65 
66     /**
67      * "virtual copy constructor". This function may be called
68      * whenever a deep copy of a writer is needed instead of a plain
69      * pointer copy.
70      *
71      * Advice to implementers: use CRTP (
72      * http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
73      * ) to implemenent this automagically -- see other Writer
74      * implemenent for advice on this subject.
75      */
76     virtual Writer *clone() const = 0;
77 
78     /**
79      * is called back from sim after each simulation step. event
80      * specifies the phase in which the simulation is currently in.
81      * This may be used for instance to open/close files at the
82      * beginning/end of the simulation.
83      */
84     virtual void stepFinished(const GridType& grid, unsigned step, WriterEvent event) = 0;
85 
getPeriod()86     const unsigned& getPeriod() const
87     {
88         return period;
89     }
90 
getPrefix()91     const std::string& getPrefix() const
92     {
93         return prefix;
94     }
95 
96 protected:
97     std::string prefix;
98     unsigned period;
99 };
100 
101 }
102 
103 #endif
104