Schnek
diagnostic.hpp
1 /*
2  * diagnostic.hpp
3  *
4  * Created on: 23 Oct 2012
5  * Author: hschmitz
6  * Email: holger@notjustphysics.com
7  *
8  * Copyright 2012 Holger Schmitz
9  *
10  * This file is part of Schnek.
11  *
12  * Schnek is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * Schnek is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with Schnek. If not, see <http://www.gnu.org/licenses/>.
24  *
25  */
26 
27 #ifndef SCHNEK_DIAGNOSTIC_HPP_
28 #define SCHNEK_DIAGNOSTIC_HPP_
29 
30 #include "../variables/block.hpp"
31 #include "../util/singleton.hpp"
32 
33 #include <boost/shared_ptr.hpp>
34 #include <fstream>
35 
36 namespace schnek {
37 
46 class DiagnosticInterface : public Block
47 {
48  protected:
50  std::string fname;
52  int append;
53  public:
57  virtual ~DiagnosticInterface() {}
58  protected:
59 
60  virtual void open(const std::string &) {}
61  virtual void write() {}
62  virtual void close() {}
63  virtual bool singleOut() { return false; }
64  void initParameters(BlockParameters&);
65 
66  bool appending();
67  std::string parsedFileName(int rank, int timeCounter);
68  std::string parsedFileName(int rank, double physicalTime);
69 };
70 
72 {
73  private:
75  int interval;
76  public:
78  void execute(bool master, int rank, int timeCounter);
79  protected:
80  void initParameters(BlockParameters&);
81 };
82 
84 {
85  private:
87  double deltaTime;
88  double nextOutput;
89  int count;
90  public:
92  void execute(bool master, int rank, double physicalTime);
93  double getNextOutput();
94  protected:
95  void initParameters(BlockParameters&);
96 };
97 
98 typedef boost::shared_ptr<DiagnosticInterface> pDiagnosticInterface;
99 typedef std::list<pDiagnosticInterface> DiagList;
100 
101 class DiagnosticManager : public Singleton<DiagnosticManager>
102 {
103  private:
104  std::list<IntervalDiagnostic*> intervalDiags;
105  std::list<DeltaTimeDiagnostic*> deltaTimeDiags;
106 
108  int *timecounter;
109  double *physicalTime;
110  bool usePhysicalTime;
111  bool master;
112  int rank;
113 
114  friend class Singleton<DiagnosticManager>;
115  friend class CreateUsingNew<DiagnosticManager>;
116  public:
117  void addIntervalDiagnostic(IntervalDiagnostic*);
118  void addDeltaTimeDiagnostic(DeltaTimeDiagnostic*);
119  void execute();
120 
121  void setTimeCounter(int *timecounter);
122  void setPhysicalTime(double *physicalTime);
123  void setMaster(bool master);
124  void setRank(int rank);
125 
126  double adjustDeltaT(double deltaT);
127  private:
128  DiagnosticManager();
129 };
130 
131 template<class Type, typename PointerType = boost::shared_ptr<Type>, class DiagnosticType = IntervalDiagnostic>
132 class SimpleDiagnostic : public DiagnosticType
133 {
134  private:
136  std::string fieldName;
137  bool single_out;
138  protected:
139  PointerType field;
140  public:
141  SimpleDiagnostic() { single_out=false; }
142  virtual ~SimpleDiagnostic();
143  protected:
144  bool singleOut() { return single_out; }
145  void initParameters(BlockParameters&);
146  void init();
147  std::string getFieldName() { return fieldName; }
148 
156  virtual bool isDerived() { return false; }
157  public:
158  void setSingleOut(bool single_out_) { single_out = single_out_; }
159 };
160 
161 template<class Type, typename PointerType = boost::shared_ptr<Type>, class DiagnosticType = IntervalDiagnostic >
162 class SimpleFileDiagnostic : public SimpleDiagnostic<Type, PointerType, DiagnosticType>
163 {
164  private:
165  std::ofstream output;
166  protected:
167  void open(const std::string &);
168  void write();
169  void close();
170 };
171 
172 } // namespace schnek
173 
174 
175 #include "diagnostic.t"
176 
177 #endif // SCHNEK_DIAGNOSTIC_HPP_
Definition: diagnostic.hpp:46
virtual bool isDerived()
Definition: diagnostic.hpp:156
Definition: singleton.hpp:79
Definition: algo.hpp:30
Definition: diagnostic.hpp:162
DiagnosticInterface()
Default constructor.
Definition: diagnostic.cpp:36
virtual ~DiagnosticInterface()
Virtual destructor.
Definition: diagnostic.hpp:57
Definition: diagnostic.hpp:101
Definition: singleton.hpp:42
Definition: diagnostic.hpp:132
std::string fname
The file name into which to write.
Definition: diagnostic.hpp:50
Definition: block.hpp:65
Definition: diagnostic.hpp:83
int append
Append data at every write to the same file?
Definition: diagnostic.hpp:52
Definition: blockparameters.hpp:150
Definition: diagnostic.hpp:71