Files
phasicFlow/src/phasicFlow/Timer/Timer.hpp

192 lines
3.3 KiB
C++
Raw Normal View History

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