7#ifndef MAIA_FUNCTIONS_H_
8#define MAIA_FUNCTIONS_H_
38#define TERM(exitval) TERMM(exitval, "")
39#define TERMM(exitval, msg) \
41 mTerm(exitval, AT_, msg); \
45#define TERMM_IF_COND(termCondition, msg) \
53#define TERMM_IF_NOT_COND(termNotCondition, msg) \
55 TERMM_IF_COND(!(termNotCondition), msg); \
59#ifdef MAIA_TIMER_CHECKS
61#define CHECK_TIMERS_IO(message) \
63 maia::dlb::g_dlbTimerController.checkIOTimerStatus(AT_); \
66#define CHECK_TIMERS_IO(message) \
75 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
76 const MFloat t_user = (tp.tv_sec + (
MFloat)tp.tv_nsec / 1e9);
85inline Real
ABS(
const Real x) {
90constexpr inline T
mMin(
const T& x,
const T&
y) {
91 return std::min(x,
y);
94constexpr inline T
mMax(
const T& x,
const T&
y) {
95 return std::max(x,
y);
98inline T
MIN3(
const T& x,
const T&
y,
const T& z) {
99 return std::min(std::min(x,
y), z);
102inline T
MAX3(
const T& x,
const T&
y,
const T& z) {
103 return std::max(std::max(x,
y), z);
107constexpr const T&
Clamp(
const T& v,
const T& lo,
const T& hi) {
108 TERMM_IF_COND((hi < lo),
"");
109 return (v < lo) ? lo : (hi < v) ? hi : v;
119constexpr Real
POW2(
const Real x) {
123constexpr Real
POW3(
const Real x) {
127constexpr Real
POW4(
const Real x) {
128 return (x * x * x * x);
131constexpr Real
POW5(
const Real x) {
132 return (x * x * x * x * x);
135constexpr Real
POW6(
const Real x) {
136 return (x * x * x * x * x * x);
147 return (exponent == 0) ? 1 : base *
POWX(base, exponent - 1);
151typename std::enable_if<std::is_unsigned<T>::value,
int>::type
inline constexpr signum(T x) {
156typename std::enable_if<std::is_signed<T>::value,
int>::type
inline constexpr signum(T x) {
157 return (T(0) < x) - (x < T(0));
162typename std::enable_if<std::is_unsigned<T>::value,
int>::type
inline constexpr signum0(T x) {
167typename std::enable_if<std::is_signed<T>::value,
int>::type
inline constexpr signum0(T x) {
168 return (T(0) <= x) - (x < T(0));
181#define SUTHERLANDLAW(T) (((T)*sqrt((T)) * (m_sutherlandPlusOne)) / ((T) + (m_sutherlandConstant)))
190#define IDX_LSSETDIR(i, j, k) (((i)*m_maxNoSets + (k)) * m_noDirs + (j))
199#define IDX_LSSETNODES(i, j, k) (((i)*m_noLevelSetsUsedForMb + (k)) * m_noCellNodes + (j))
209#define IDX_LSSET(i, j) ((i)*m_maxNoSets + (j))
220#define IDX_LSSETMB(i, j) ((i)*m_noLevelSetsUsedForMb + (j))
244#define ASSERT(condition, message) \
247 std::cerr << "Assertion `" #condition "` failed in " << __FILE__ << " line " << __LINE__ << ": " << message \
249 mTerm(1, AT_, "ASSERTION FAILED"); \
253#define ASSERT(condition, message) \
255 } while(false && (condition))
258#ifdef MAIA_ASSERT_ACCESSORS
259#define ASSERT_ACCESSOR(condition, message) ASSERT(condition, message)
261#define ASSERT_ACCESSOR(condition, message) \
263 } while(false && (condition))
267template <
class T,
class U>
271template <
class T,
class U>
280 return std::fabs(
a -
b) < eps;
301 return approx(std::fabs(std::round(
a) -
a), 0.0, eps);
318 ASSERT(exp >= 0,
"Error in ipow: exponent is less than zero (must be greater or equal to zero)!");
327 MInt result_test = result * base;
328 ASSERT(((base != 0 && result_test / base == result) || base == 0),
"Error in ipow: signed integer overflow!");
341 const MInt minLevel,
const MFloat* targetGridCenterOfGravity,
342 const MFloat targetGridLengthLevel0,
const MInt targetGridMinLevel);
358 dat2.b[0] = dat1.b[3];
359 dat2.b[1] = dat1.b[2];
360 dat2.b[2] = dat1.b[1];
361 dat2.b[3] = dat1.b[0];
372 dat2.b[0] = dat1.b[3];
373 dat2.b[1] = dat1.b[2];
374 dat2.b[2] = dat1.b[1];
375 dat2.b[3] = dat1.b[0];
386 dat2.b[0] = dat1.b[7];
387 dat2.b[1] = dat1.b[6];
388 dat2.b[2] = dat1.b[5];
389 dat2.b[3] = dat1.b[4];
390 dat2.b[4] = dat1.b[3];
391 dat2.b[5] = dat1.b[2];
392 dat2.b[6] = dat1.b[1];
393 dat2.b[7] = dat1.b[0];
420static inline std::string ltrim(std::string& s) {
421 s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](
int ch) { return !std::isspace(ch); }));
426static inline std::string rtrim(std::string& s) {
427 s.erase(std::find_if(s.rbegin(), s.rend(), [](
int ch) { return !std::isspace(ch); }).base(), s.end());
432static inline std::string trim(std::string& s) {
438template <
class ContainerT>
439inline void tokenize(
const std::string& str, ContainerT& tokens,
const std::string& delimiters =
" ",
440 MBool trimEmpty =
false) {
441 std::string::size_type pos = 0;
442 std::string::size_type lastPos = 0;
443 std::string::size_type length = str.length();
445 using value_type =
typename ContainerT::value_type;
446 using size_type =
typename ContainerT::size_type;
448 while(lastPos < length + 1) {
449 pos = str.find_first_of(delimiters, lastPos);
450 if(pos == std::string::npos) {
454 if(pos != lastPos || !trimEmpty) {
455 tokens.push_back(value_type(str.data() + lastPos, (size_type)pos - lastPos));
464 if(stat(dir.c_str(), &s) < 0) {
466 mkdir(dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IXGRP | S_IXGRP);
void mTerm(const MInt errorCode, const MString &location, const MString &message)
constexpr Real POW4(const Real x)
T MIN3(const T &x, const T &y, const T &z)
ATTRIBUTES1(ATTRIBUTE_NORETURN) void mTerm(const MInt errorCode
controlled termination of maia with a user defined error message
const MString const MString & message
constexpr Real POW3(const Real x)
constexpr const T & Clamp(const T &v, const T &lo, const T &hi)
std::enable_if< std::is_unsigned< T >::value, int >::type constexpr signum(T x)
MBool isApproxInt(const T &, const T)
MBool approx< MFloat, MFloat >(const MFloat &a, const MFloat &b, const MFloat eps)
constexpr Real POW6(const Real x)
constexpr Real POW5(const Real x)
constexpr Real POW2(const Real x)
void checkMultiSolverGridExtents(const MInt nDim, const MFloat *centerOfGravity, const MFloat lengthLevel0, const MInt minLevel, const MFloat *targetGridCenterOfGravity, const MFloat targetGridLengthLevel0, const MInt targetGridMinLevel)
Checks if the given grid extents and cell sizes match when creating a multisolver grid and correspond...
MInt copyFile(const MString &fromName, const MString &toName)
Copies file fromName to file toName.
MBool approx(const T &, const U &, const T)
MFloat cpuTime()
Return the process cpu time (user time) (high-resolution timer - do not use clock())
MInt loadPointCoordinatesFromFile(const MString inputFileName, const MInt nDim, std::vector< MFloat > &coordinates)
Loads point coordinates from an input file.
constexpr T mMin(const T &x, const T &y)
MInt ipow(MInt base, MInt exp)
Integer exponent function for non-negative exponents.
void writeMemoryStatistics(const MPI_Comm comm, const MInt noDomains, const MInt domainId, const MString at, const MString comment="")
Write memory statistics.
MBool isApproxInt< MFloat >(const MFloat &a, const MFloat eps)
Return true if argument is approximately an integer.
double doubleSwap(double f)
T constexpr POWX(T base, MUint exponent)
Compile time power calculation.
void createDir(const std::string &dir)
constexpr T mMax(const T &x, const T &y)
std::enable_if< std::is_unsigned< T >::value, int >::type constexpr signum0(T x)
T MAX3(const T &x, const T &y, const T &z)
void tokenize(const std::string &str, ContainerT &tokens, const std::string &delimiters=" ", MBool trimEmpty=false)
MBool fileExists(const MString &fileName)
Returns true if the file fileName exists, false otherwise.
std::basic_string< char > MString