VM2D 1.14
Vortex methods for 2D flows simulation
Loading...
Searching...
No Matches
TimesGen.h
Go to the documentation of this file.
1/*--------------------------------*- VM2D -*-----------------*---------------*\
2| ## ## ## ## #### ##### | | Version 1.14 |
3| ## ## ### ### ## ## ## ## | VM2D: Vortex Method | 2026/03/06 |
4| ## ## ## # ## ## ## ## | for 2D Flow Simulation *----------------*
5| #### ## ## ## ## ## | Open Source Code |
6| ## ## ## ###### ##### | https://www.github.com/vortexmethods/VM2D |
7| |
8| Copyright (C) 2017-2026 I. Marchevsky, K. Sokol, E. Ryatina, A. Kolganova |
9*-----------------------------------------------------------------------------*
10| File name: TimesGen.h |
11| Info: Source code of VM2D |
12| |
13| This file is part of VM2D. |
14| VM2D is free software: you can redistribute it and/or modify it |
15| under the terms of the GNU General Public License as published by |
16| the Free Software Foundation, either version 3 of the License, or |
17| (at your option) any later version. |
18| |
19| VM2D is distributed in the hope that it will be useful, but WITHOUT |
20| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21| FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
22| for more details. |
23| |
24| You should have received a copy of the GNU General Public License |
25| along with VM2D. If not, see <http://www.gnu.org/licenses/>. |
26\*---------------------------------------------------------------------------*/
27
28
40#ifndef TIMESGEN_H
41#define TIMESGEN_H
42
43#include <chrono>
44#include <map>
45#include <memory>
46#include "defs.h"
47
48namespace VMlib
49{
50 class WorldGen;
51
58 class vmTimer
59 {
61 const std::string label;
62
63 using Clock = std::chrono::high_resolution_clock;
64
66 mutable bool active = false;
67
69 mutable std::chrono::duration<float> duration_;
70
72 mutable Clock::time_point start_ = Clock::now();
73
75 mutable Clock::time_point stop_ = Clock::now();
76
77 // Запрет на копирование счетчика
78 vmTimer(const vmTimer&) = delete;
79 vmTimer& operator=(const vmTimer&) = delete;
80
81 public:
82 //using ns = std::chrono::nanoseconds;
83 //using mks = std::chrono::microseconds;
84 //using ms = std::chrono::milliseconds;
85 //using s = std::chrono::seconds;
86
88 using ms = std::chrono::duration<float, std::chrono::milliseconds::period>;
89
91 using s = std::chrono::duration<float, std::chrono::seconds::period>;
92
97 vmTimer(const std::string& timerLabel = "") : label(timerLabel) { reset(); }
98 ~vmTimer() = default;
99
101 const vmTimer& reset() const
102 {
103 duration_ = std::chrono::seconds(0);
104 active = false;
105 return *this;
106 }
107
109 const vmTimer& start() const
110 {
111 if (!active)
112 {
113 start_ = Clock::now();
114 active = true;
115 }
116 else
117 {
118 std::cout << "Timer " << label << " was not stopped before being start!" << std::endl;
119 exit(-15);
120 }
121 return *this;
122 }
123
125 const vmTimer& stop() const
126 {
127 if (active)
128 {
129 stop_ = Clock::now();
131 active = false;
132 }
133 else
134 {
135 std::cout << "Timer " << label << " was not started before being stopped!" << std::endl;
136 exit(-15);
137 }
138 return *this;
139 }
140
141
142 template<typename T = ms>
143 double duration() const
144 {
145 return std::chrono::duration_cast<T>(duration_).count();
146 }
147 };
148
149
157 {
158 private:
160 const WorldGen& W;
161
162 protected:
164 std::vector<std::string> timerLabelList;
165
167 std::map<std::string, std::unique_ptr<vmTimer>> timer;
168
169 public:
171 TimersGen(const WorldGen& W_, std::vector<std::string> labels);
172
174 void start(const std::string& timerLabel);
175
177 void stop(const std::string& timerLabel);
178
180 void resetAll();
181
183 template<typename T = vmTimer::s>
184 double durationStep() const
185 {
186 return (timer.at("Step"))->duration<T>();
187 }
188
190 void GenerateStatHeader();
191
193 void GenerateStatString(size_t stepNo, double curTime, size_t N);
194 };
195
196
197
198
200 {
201 protected:
204 static void ToZeroPeriod(timePeriod& period)
205 {
206 period.first = 0;
207 period.second = 0;
208 }//ToZero(...)
209
210 public:
213
215 virtual ~TimesGen() {};
216
217
219 virtual void GenerateStatHeader() const = 0;
220
222 virtual void GenerateStatString() const = 0;
223
225 virtual void ToZero() = 0;
226
230 static double dT(const timePeriod& t)
231 {
232 return (t.second - t.first);
233 }//dT(...)
234 };
235
236}//namespace VMlib
237
238#endif
Класс для сбора статистики времени исполнения основных шагов алгоритма и вывода ее в файл
Definition TimesGen.h:157
std::vector< std::string > timerLabelList
Список имен счетчиков
Definition TimesGen.h:164
void GenerateStatString(size_t stepNo, double curTime, size_t N)
Формирование очередной строки файла временной статистики
Definition TimesGen.cpp:111
void GenerateStatHeader()
Формирование заголовка файла временной статистики
Definition TimesGen.cpp:87
void stop(const std::string &timerLabel)
Останов счетчика
Definition TimesGen.cpp:68
void start(const std::string &timerLabel)
Запуск счетчика
Definition TimesGen.cpp:55
void resetAll()
Сброс всех счетчиков
Definition TimesGen.cpp:81
const WorldGen & W
Константная ссылка на решаемую задачу
Definition TimesGen.h:160
double durationStep() const
Вывод счетчика всего шага в секундах
Definition TimesGen.h:184
std::map< std::string, std::unique_ptr< vmTimer > > timer
Ассоциативный массив { имя, счетчик }.
Definition TimesGen.h:167
static void ToZeroPeriod(timePeriod &period)
Definition TimesGen.h:204
static double dT(const timePeriod &t)
Definition TimesGen.h:230
virtual void GenerateStatHeader() const =0
Генерация заголовка файла временной статистики
TimesGen()
Конструктор
Definition TimesGen.h:212
virtual void GenerateStatString() const =0
Сохранение строки со статистикой в файл временной статистики
virtual ~TimesGen()
Деструктор
Definition TimesGen.h:215
virtual void ToZero()=0
Обнуление состояния временной статистики
Класс, опеделяющий текущую решаемую задачу
Definition WorldGen.h:57
Класс засекания времени
Definition TimesGen.h:59
double duration() const
Definition TimesGen.h:143
std::chrono::high_resolution_clock Clock
Definition TimesGen.h:63
Clock::time_point start_
Отметка последнего включения счетчика
Definition TimesGen.h:72
const vmTimer & stop() const
Останов работающего счетчика времени
Definition TimesGen.h:125
const std::string label
Имя счетчика времени
Definition TimesGen.h:61
std::chrono::duration< float, std::chrono::milliseconds::period > ms
Основная единица измерения времени — миллисекунды
Definition TimesGen.h:88
~vmTimer()=default
vmTimer(const vmTimer &)=delete
vmTimer(const std::string &timerLabel="")
Конструктор, принимающий на вход имя счетчика
Definition TimesGen.h:97
Clock::time_point stop_
Отметка последнего выключения счетчика
Definition TimesGen.h:75
std::chrono::duration< float, std::chrono::seconds::period > s
Дополнительная единица измерения времени — секунды
Definition TimesGen.h:91
const vmTimer & start() const
Запуск (первый или повторный) счетчика времени
Definition TimesGen.h:109
const vmTimer & reset() const
Сброс счетчика времени
Definition TimesGen.h:101
vmTimer & operator=(const vmTimer &)=delete
std::chrono::duration< float > duration_
Хранит накопденную от предыдущих запусков продолжительность работы счетчика
Definition TimesGen.h:69
bool active
Признак того, что счетчик работает
Definition TimesGen.h:66
Описание базовых вспомогательных функций
std::pair< double, double > timePeriod
Тип для хранения начала и конца промежутка времени
Definition defs.h:79