phasicFlow/src/phasicFlow/Timer/Timer.hpp

192 lines
3.3 KiB
C++
Raw Normal View History

2022-09-01 17:05:35 +00:00
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
2024-04-08 19:33:08 +00:00
OOOOOOO F luid flow
2022-09-01 17:05:35 +00:00
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
2024-04-08 19:33:08 +00:00
------------------------------------------------------------------------------
2022-09-01 17:05:35 +00:00
Licence:
2024-04-08 19:33:08 +00:00
This file is part of phasicFlow code. It is a free software for simulating
2022-09-01 17:05:35 +00:00
granular and multiphase flows. You can redistribute it and/or modify it under
2024-04-08 19:33:08 +00:00
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
2022-09-01 17:05:35 +00:00
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
2023-03-17 13:05:57 +00:00
#ifndef __Timerr_hpp__
#define __Timerr_hpp__
2022-09-01 17:05:35 +00:00
#include <chrono>
2022-12-09 22:02:54 +00:00
#include "types.hpp"
2022-09-01 17:05:35 +00:00
namespace pFlow
{
2024-04-08 19:33:08 +00:00
// forward
2022-09-01 17:05:35 +00:00
class Timers;
class Timer
{
2024-10-18 19:43:20 +00:00
private:
2022-09-01 17:05:35 +00:00
using timer = std::chrono::high_resolution_clock;
/// start time
2024-04-08 19:33:08 +00:00
timer::time_point start_;
2022-09-01 17:05:35 +00:00
2024-04-08 19:33:08 +00:00
/// number of times start() and end() are called
int32 numIteration_ = 0;
2022-09-01 17:05:35 +00:00
2024-04-08 19:33:08 +00:00
/// sum of time duratios (in seconds) between all start() and end() calls
real accTime_ = 0.0;
2022-09-01 17:05:35 +00:00
2024-04-08 19:33:08 +00:00
/// last time duration
real lastTime_ = 0.0;
2022-09-01 17:05:35 +00:00
2024-04-08 19:33:08 +00:00
/// @brief Accumulative duration for multiple steps between start() and
/// end()
real stepAccTime_ = 0.0;
2024-04-08 19:33:08 +00:00
/// name for the timer
word name_ = "noNameTimer";
2024-04-08 19:33:08 +00:00
/// @brief parrent of timer
Timers* parrent_ = nullptr;
2022-09-01 17:05:35 +00:00
2024-10-18 19:43:20 +00:00
protected:
real averageTimeMax()const;
real accTimersTotalMax()const;
2022-09-01 17:05:35 +00:00
public:
2022-12-09 22:02:54 +00:00
TypeInfo("Timer");
2022-09-01 17:05:35 +00:00
2024-01-18 12:51:06 +00:00
Timer() = default;
2022-09-01 17:05:35 +00:00
2024-01-18 12:51:06 +00:00
explicit Timer(const word& name)
2024-04-08 19:33:08 +00:00
: name_(name)
{
}
2022-09-01 17:05:35 +00:00
2024-01-18 12:51:06 +00:00
Timer(const word& name, Timers* parrent);
2022-09-01 17:05:35 +00:00
2024-04-08 19:33:08 +00:00
const word& name() const
2022-09-01 17:05:35 +00:00
{
return name_;
}
virtual ~Timer();
2024-04-08 19:33:08 +00:00
2022-09-01 17:05:35 +00:00
virtual void removeParrent()
{
parrent_ = nullptr;
}
2024-04-08 19:33:08 +00:00
virtual int32 level() const;
2022-09-01 17:05:35 +00:00
2024-04-08 19:33:08 +00:00
virtual bool master() const
2022-09-01 17:05:35 +00:00
{
return false;
}
2024-10-18 19:43:20 +00:00
Timers* parrent()const
{
return parrent_;
}
inline
2022-09-01 17:05:35 +00:00
void start()
{
2024-04-08 19:33:08 +00:00
start_ = timer::now();
stepAccTime_ = 0;
2022-09-01 17:05:35 +00:00
}
2024-10-18 19:43:20 +00:00
inline
void pause()
2022-09-01 17:05:35 +00:00
{
auto end = timer::now();
2024-04-08 19:33:08 +00:00
stepAccTime_ +=
std::chrono::duration_cast<std::chrono::duration<real> >(
end - start_
)
.count();
}
2024-10-18 19:43:20 +00:00
inline
void resume()
{
start_ = timer::now();
}
2024-10-18 19:43:20 +00:00
inline
void end()
{
pause();
2024-04-08 19:33:08 +00:00
lastTime_ = stepAccTime_;
2022-09-01 17:05:35 +00:00
numIteration_++;
accTime_ += lastTime_;
}
2024-10-18 19:43:20 +00:00
inline
bool timerActive() const
2022-09-01 17:05:35 +00:00
{
2024-04-08 19:33:08 +00:00
return numIteration_ != 0;
2022-09-01 17:05:35 +00:00
}
2024-10-18 19:43:20 +00:00
inline
real lastTime() const
2022-09-01 17:05:35 +00:00
{
return lastTime_;
}
2024-10-18 19:43:20 +00:00
inline
real totalTime() const
2022-09-01 17:05:35 +00:00
{
return accTime_;
}
2024-10-18 19:43:20 +00:00
inline
real averageTime() const
2022-09-01 17:05:35 +00:00
{
2024-04-08 19:33:08 +00:00
return accTime_ / max(numIteration_, 1);
2022-09-01 17:05:35 +00:00
}
2024-10-18 19:43:20 +00:00
virtual
real accTimersTotal() const
2023-03-16 13:49:33 +00:00
{
return totalTime();
}
2022-09-01 17:05:35 +00:00
//// - IO operations
2024-04-08 19:33:08 +00:00
virtual bool write(iOstream& os, bool subTree) const;
2022-09-01 17:05:35 +00:00
virtual bool read(iIstream& is)
{
return true;
}
};
inline iOstream& operator<<(iOstream& os, const Timer& t)
{
t.write(os, false);
return os;
}
inline iIstream& operator>>(iIstream& is, Timer& t)
{
return is;
}
2024-04-08 19:33:08 +00:00
} // namespace pFlow
2022-09-01 17:05:35 +00:00
2022-12-09 22:02:54 +00:00
#endif //__Timer_hpp__