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 <memory>
34 #include <fstream>
35 
36 namespace schnek {
37 
47 class DiagnosticInterface : public Block
48 {
49  protected:
51  std::string fname;
53  int append;
54  public:
58  virtual ~DiagnosticInterface() {}
59  protected:
61  virtual void open(const std::string &) {}
63  virtual void write() {}
65  virtual void close() {}
66 
67  virtual bool singleOut() { return false; }
68  void initParameters(BlockParameters&);
69 
70  bool appending();
71  std::string parsedFileName(int rank, int timeCounter);
72  std::string parsedFileName(int rank, double physicalTime);
73 };
74 
76 {
77  private:
79  int interval;
80  public:
82  void execute(bool master, int rank, int timeCounter);
83  int getInterval();
84  protected:
85  void initParameters(BlockParameters&);
86 };
87 
89 {
90  private:
92  double deltaTime;
93  double nextOutput;
94  int count;
95  public:
97  void execute(bool master, int rank, double physicalTime);
98  double getNextOutput();
99  double getDeltaTime();
100  protected:
101  void initParameters(BlockParameters&);
102 };
103 
104 typedef std::shared_ptr<DiagnosticInterface> pDiagnosticInterface;
105 typedef std::list<pDiagnosticInterface> DiagList;
106 
107 class DiagnosticManager : public Singleton<DiagnosticManager>
108 {
109  private:
110  std::list<IntervalDiagnostic*> intervalDiags;
111  std::list<DeltaTimeDiagnostic*> deltaTimeDiags;
112 
114  int *timecounter;
115  double *physicalTime;
116  bool usePhysicalTime;
117  bool master;
118  int rank;
119 
120  friend class Singleton<DiagnosticManager>;
121  friend class CreateUsingNew<DiagnosticManager>;
122  public:
123  void addIntervalDiagnostic(IntervalDiagnostic*);
124  void addDeltaTimeDiagnostic(DeltaTimeDiagnostic*);
125  void execute();
126 
127  void setTimeCounter(int *timecounter);
128  void setPhysicalTime(double *physicalTime);
129  void setMaster(bool master);
130  void setRank(int rank);
131 
132  double adjustDeltaT(double deltaT);
133  private:
134  DiagnosticManager();
135 };
136 
137 template<class Type, typename PointerType = std::shared_ptr<Type>, class DiagnosticType = IntervalDiagnostic>
138 class SimpleDiagnostic : public DiagnosticType
139 {
140  private:
142  std::string fieldName;
143  bool single_out;
144  protected:
145  PointerType field;
146  public:
147  SimpleDiagnostic() { single_out=false; }
148  virtual ~SimpleDiagnostic();
149  protected:
150  bool singleOut() { return single_out; }
151  void initParameters(BlockParameters&);
152  void init();
153  std::string getFieldName() { return fieldName; }
154 
162  virtual bool isDerived() { return false; }
163  public:
164  void setSingleOut(bool single_out_) { single_out = single_out_; }
165 };
166 
167 template<class Type, typename PointerType = std::shared_ptr<Type>, class DiagnosticType = IntervalDiagnostic >
168 class SimpleFileDiagnostic : public SimpleDiagnostic<Type, PointerType, DiagnosticType>
169 {
170  private:
171  std::ofstream output;
172  protected:
173  void open(const std::string &);
174  void write();
175  void close();
176 };
177 
178 } // namespace schnek
179 
180 
181 #include "diagnostic.t"
182 
183 #endif // SCHNEK_DIAGNOSTIC_HPP_
virtual void open(const std::string &)
Open the output file.
Definition: diagnostic.hpp:61
Definition: diagnostic.hpp:47
virtual bool isDerived()
Definition: diagnostic.hpp:162
virtual void close()
Close the output file.
Definition: diagnostic.hpp:65
virtual void write()
Write into the touput file.
Definition: diagnostic.hpp:63
Definition: singleton.hpp:80
Definition: singleton.hpp:42
Definition: algo.hpp:30
Definition: diagnostic.hpp:168
DiagnosticInterface()
Default constructor.
Definition: diagnostic.cpp:36
virtual ~DiagnosticInterface()
Virtual destructor.
Definition: diagnostic.hpp:58
Definition: diagnostic.hpp:107
Definition: diagnostic.hpp:138
std::string fname
The file name into which to write.
Definition: diagnostic.hpp:51
Definition: block.hpp:65
Definition: diagnostic.hpp:88
int append
Append data at every write to the same file?
Definition: diagnostic.hpp:53
Definition: blockparameters.hpp:149
Definition: diagnostic.hpp:75