www.cemf.ir
stridedRange.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 #ifndef __stridedRange_hpp__
22 #define __stridedRange_hpp__
23 
24 #include "types.hpp"
25 #include "typeInfo.hpp"
26 #include "dictionary.hpp"
27 
28 #include "streams.hpp"
29 
30 namespace pFlow
31 {
32 
33 template<typename T>
34 class
36 {
37 protected:
38 
39  T begin_ = 0;
40 
41  T end_ = 1;
42 
43  T stride_ = 1;
44 
45  static inline const T maxVal = largestPositive<T>();
46  static inline const T minVal = largestNegative<T>();
47 
48 public:
49 
50  TypeInfoTemplateNV11("stridedRange",T);
51 
52  stridedRange()=default;
53 
54  stridedRange(T begin, T end, T stride)
55  :
56  begin_(begin),
57  end_(end),
58  stride_(stride)
59  {}
60 
61  stridedRange(T begin, T stride)
62  :
63  begin_(begin),
64  end_(maxVal),
65  stride_(stride)
66  {}
67 
68 
69  // it should be in the form of begin:stride:end
70  stridedRange(const word& rangeString)
71  {
72  if(!parseRange(rangeString,begin_,end_, stride_))
73  {
75  "bad input for the range. It should have the form of begin:stride:end \n";
76  fatalExit;
77  }
78  }
79 
80  stridedRange(const dictionary& dict)
81  :
82  begin_(dict.getVal<T>("begin")),
83  end_(dict.getVal<T>("end")),
84  stride_(dict.getVal<T>("stride"))
85  {}
86 
87  inline
88  T begin()const
89  {
90  return begin_;
91  }
92 
93  T end()const
94  {
95  return end_;
96  }
97 
98  T stride()const
99  {
100  return stride_;
101  }
102 
103  inline
104  bool isInRange(T val)const
105  {
106  return val>= begin_ && val<= end_;
107  }
108 
109  inline
110  bool isMember(T val, T epsilon = 0)const
111  {
112 
113  if(!isInRange(val)) return false;
114  if(T dist = val-begin_; abs(dist%stride_)<= epsilon) return true;
115  if(equal(val,begin_))return true;
116  if(equal(val,end_))return true;
117  return false;
118  }
119 
120  static
121  bool parseRange(const word& rangeString, T& begin, T& end, T& stride)
122  {
123  if(std::count(
124  rangeString.begin(),
125  rangeString.end(),
126  ':') != 2)
127  return false;
128 
129  auto col1 = rangeString.find_first_of(":");
130 
131  if(col1 == 0 || col1==std::string::npos)return false;
132 
133  auto beginCh = rangeString.substr(0,col1);
134 
135  auto col2 = rangeString.find_first_of(":",col1+1);
136 
137  if(col1 == col2 || col2==std::string::npos)return false;
138 
139  auto strideCh = rangeString.substr(col1+1,col2-col1-1);
140 
141  auto endCh = rangeString.substr(col2+1);
142 
143  if(!readValue(beginCh,begin))return false;
144  if(!readValue(strideCh, stride))return false;
145  if(!readValue(endCh, end))return false;
146 
147  return true;
148 
149  }
150 };
151 
152 template<>
153 inline
154 bool stridedRange<real>::isMember(real val, real epsilon)const
155  {
156 
157  if(!isInRange(val)) return false;
158  real dist = val-begin_;
159  if(abs(
160  (dist-(static_cast<uint64>((dist+0.01*epsilon)/stride_)*stride_))
161  )<= epsilon) return true;
162  if(equal(val,begin_))return true;
163  if(equal(val,end_))return true;
164  return false;
165 }
166 
167 
168 }
169 
170 #endif //__stridedRange_hpp__
count
auto count(const Vector< T, Allocator > &vec, const T &val)
pFlow::readValue
bool readValue(const word &w, real &val)
Definition: bTypesFunctions.hpp:191
pFlow::real
float real
Definition: builtinTypes.hpp:45
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::stridedRange::stridedRange
stridedRange(T begin, T end, T stride)
Definition: stridedRange.hpp:54
types.hpp
pFlow::stridedRange::begin
T begin() const
Definition: stridedRange.hpp:88
pFlow::equal
INLINE_FUNCTION_HD bool equal(const box &b1, const box &b2, real tol=smallValue)
Definition: box.hpp:135
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::stridedRange
Definition: stridedRange.hpp:34
pFlow::stridedRange::stridedRange
stridedRange(T begin, T stride)
Definition: stridedRange.hpp:61
pFlow
Definition: demGeometry.hpp:27
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::stridedRange::isInRange
bool isInRange(T val) const
Definition: stridedRange.hpp:104
dictionary.hpp
pFlow::stridedRange::end
T end() const
Definition: stridedRange.hpp:93
pFlow::abs
Vector< T, Allocator > abs(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:84
pFlow::stridedRange::stride
T stride() const
Definition: stridedRange.hpp:98
streams.hpp
pFlow::stridedRange::stridedRange
stridedRange(const dictionary &dict)
Definition: stridedRange.hpp:80
TypeInfoTemplateNV11
#define TypeInfoTemplateNV11(tName, Type)
Definition: typeInfo.hpp:242
pFlow::stridedRange::isMember
bool isMember(T val, T epsilon=0) const
Definition: stridedRange.hpp:110
pFlow::uint64
unsigned long long int uint64
Definition: builtinTypes.hpp:58
typeInfo.hpp
pFlow::stridedRange::stridedRange
stridedRange(const word &rangeString)
Definition: stridedRange.hpp:70
pFlow::dictionary
Dictionary holds a set of data entries or sub-dictionaries that are enclosed in a curely braces or ar...
Definition: dictionary.hpp:67
pFlow::stridedRange::parseRange
static bool parseRange(const word &rangeString, T &begin, T &end, T &stride)
Definition: stridedRange.hpp:121