PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
util::methods Namespace Reference

Provides fast methods to add/subtract list of data, to find maximum/minimum from list of data. More...

Functions

template<typename T >
size_t maxIndex (const std::vector< T > &data)
 Returns the index corresponding to maximum from list of data.
 
template<typename T >
size_t minIndex (const std::vector< T > &data)
 Returns the index corresponding to minimum from list of data.
 
template<typename T >
max (const std::vector< T > &data)
 Returns the maximum from list of data.
 
template<typename T >
min (const std::vector< T > &data)
 Returns the minimim from list of data.
 
template<typename T >
std::pair< size_t, T > maxAndMaxIndex (const std::vector< T > &data)
 Returns the maximum and index of maximum from list of data.
 
template<typename T >
std::pair< size_t, T > minAndMinIndex (const std::vector< T > &data)
 Returns the minimum and index of minimum from list of data.
 
template<typename T >
size_t maxIndex (const std::vector< T > &data, size_t data_start, size_t data_end)
 Returns the index corresponding to maximum from list of data.
 
size_t maxLengthIndex (const std::vector< util::Point > &data)
 Returns the index that has maximum length of point from list of points.
 
size_t minLengthIndex (const std::vector< util::Point > &data)
 Returns the index that has minimum length of point from list of points.
 
double maxLength (const std::vector< util::Point > &data)
 Returns the maximum length of point from list of points.
 
double minLength (const std::vector< util::Point > &data)
 Returns the minimum length of point from list of points.
 
std::pair< double, size_t > maxLengthAndMaxLengthIndex (const std::vector< util::Point > &data)
 Returns the maximum length of point and index from list of points.
 
std::pair< double, size_t > minLengthAndMinLengthIndex (const std::vector< util::Point > &data)
 Returns the minimum length of point and index from list of points.
 
template<typename T >
add (const std::vector< T > &data)
 Returns the sum of data.
 
bool isFree (const int &i, const unsigned int &dof)
 Returns true if degree of freedom is free.
 
bool isFree (const uint8_t &i, const unsigned int &dof)
 Returns true if degree of freedom is free.
 
template<typename T >
bool isInList (const T &i, const std::vector< T > &list)
 Find if data is in the list.
 
bool isTagInList (const std::string &tag, const std::vector< std::string > &tags)
 Returns true if tag is found in the list of tags.
 
template<typename T >
void addToList (const T &i, std::vector< T > &list)
 Add element to the list.
 
float timeDiff (std::chrono::steady_clock::time_point begin, std::chrono::steady_clock::time_point end, std::string unit="microseconds")
 Returns difference between two times.
 
template<typename T_out >
T_out getKeyData (std::string key, std::map< std::string, T_out > &data_map, bool issue_err=false)
 Get data for a key.
 
template<typename T_out >
void appendKeyData (std::string key, T_out data, std::map< std::string, T_out > &data_map, bool issue_err=false)
 Append value to data associated with key.
 
template<typename T_out >
void setKeyData (std::string key, T_out data, std::map< std::string, T_out > &data_map, bool issue_err=false)
 Set value to data associated with key.
 

Detailed Description

Provides fast methods to add/subtract list of data, to find maximum/minimum from list of data.

Function Documentation

◆ add()

template<typename T >
T util::methods::add ( const std::vector< T > &  data)
inline

Returns the sum of data.

Parameters
dataList of real numbers
Returns
value Sum of the numbers

Definition at line 238 of file methods.h.

238 {
239 return std::reduce(data.begin(), data.end());
240};

◆ addToList()

template<typename T >
void util::methods::addToList ( const T &  i,
std::vector< T > &  list 
)
inline

Add element to the list.

Parameters
iItem to add
listVector of elements

Definition at line 289 of file methods.h.

289 {
290 for (const auto &j : list)
291 if (j == i)
292 return;
293
294 list.emplace_back(i);
295};

◆ appendKeyData()

template<typename T_out >
void util::methods::appendKeyData ( std::string  key,
T_out  data,
std::map< std::string, T_out > &  data_map,
bool  issue_err = false 
)
inline

Append value to data associated with key.

Parameters
keyKey to append the data to

Definition at line 344 of file methods.h.

344 {
345
346 if (data_map.find(key) == data_map.end()) {
347 if (issue_err) {
348 std::cerr << "Error: key = " << key << " does not exist in data map.\n";
349 exit(EXIT_FAILURE);
350 }
351 else {
352 data_map[key] = data;
353 }
354 }
355 else {
356 data_map[key] = data_map[key] + data;
357 }
358};

◆ getKeyData()

template<typename T_out >
T_out util::methods::getKeyData ( std::string  key,
std::map< std::string, T_out > &  data_map,
bool  issue_err = false 
)
inline

Get data for a key.

Parameters
keyKey to access the data
Returns
data Value of data

Definition at line 325 of file methods.h.

325 {
326
327 if (issue_err) {
328 if (data_map.find(key) == data_map.end()) {
329 std::cerr << "Error: key = " << key << " does not exist in data map.\n";
330 exit(EXIT_FAILURE);
331 }
332 }
333
334 return data_map[key];
335};

◆ isFree() [1/2]

bool util::methods::isFree ( const int &  i,
const unsigned int &  dof 
)
inline

Returns true if degree of freedom is free.

Parameters
iFixity value
dofDegree of freedom to probe (dof = 0 for x, dof = 1 for y, and dof = 2 for z)
Returns
True True if dof is free

Definition at line 249 of file methods.h.

249 {
250 return !(i >> dof & 1UL);
251};

Referenced by model::DEMModel::init().

Here is the caller graph for this function:

◆ isFree() [2/2]

bool util::methods::isFree ( const uint8_t &  i,
const unsigned int &  dof 
)
inline

Returns true if degree of freedom is free.

Parameters
iFixity value
dofDegree of freedom to probe (dof = 0 for x, dof = 1 for y, and dof = 2 for z)
Returns
True True if dof is free

Definition at line 254 of file methods.h.

254 {
255 return !(i >> dof & 1UL);
256};

◆ isInList()

template<typename T >
bool util::methods::isInList ( const T &  i,
const std::vector< T > &  list 
)
inline

Find if data is in the list.

Parameters
tagTag to search
tagsList of tags
Returns
True True if tag exists

Definition at line 265 of file methods.h.

265 {
266 for (const auto &j : list)
267 if (j == i)
268 return true;
269
270 return false;
271};

Referenced by util::geometry::isNumberOfParamForGeometryValid(), and isTagInList().

Here is the caller graph for this function:

◆ isTagInList()

bool util::methods::isTagInList ( const std::string &  tag,
const std::vector< std::string > &  tags 
)
inline

Returns true if tag is found in the list of tags.

Parameters
tagTag to search
tagsList of tags
Returns
True True if tag exists

Definition at line 279 of file methods.h.

279 {
280 return isInList(tag, tags);
281};

References isInList().

Referenced by rw::writer::VtkParticleWriter::appendNodes(), model::DEMModel::createGeometryAtSite(), util::geometry::createGeomObjectOld(), model::DEMModel::output(), inp::Input::setParticleDeck(), model::DEMModel::setupQuadratureData(), and model::DEMModel::updateGeometryObjectsPostInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ max()

template<typename T >
T util::methods::max ( const std::vector< T > &  data)
inline

Returns the maximum from list of data.

Parameters
dataList of real numbers
Returns
value Maximum value

Definition at line 74 of file methods.h.

74 {
75 return data[util::methods::maxIndex(data)];
76};
size_t maxIndex(const std::vector< T > &data)
Returns the index corresponding to maximum from list of data.
Definition methods.h:38

References maxIndex().

Referenced by model::DEMModel::init(), and model::DEMModel::updateContactNeighborSearchParameters().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ maxAndMaxIndex()

template<typename T >
std::pair< size_t, T > util::methods::maxAndMaxIndex ( const std::vector< T > &  data)
inline

Returns the maximum and index of maximum from list of data.

Parameters
dataList of real numbers
Returns
pair Maximum value and index

Definition at line 94 of file methods.h.

94 {
95 auto i = util::methods::maxIndex(data);
96 return {data[i], i};
97};

References maxIndex().

Here is the call graph for this function:

◆ maxIndex() [1/2]

template<typename T >
size_t util::methods::maxIndex ( const std::vector< T > &  data)
inline

Returns the index corresponding to maximum from list of data.

Parameters
dataList of real numbers
Returns
i Index with a maximum value

Definition at line 38 of file methods.h.

38 {
39
40 // initialize original index locations
41 std::vector<size_t> idx(data.size());
42 std::iota(idx.begin(), idx.end(), 0);
43
44 std::stable_sort(idx.begin(), idx.end(),
45 [&data](size_t i1, size_t i2) {return data[i1] > data[i2];});
46
47 return idx[0];
48};

Referenced by util::geometry::ComplexGeomObject::center(), max(), maxAndMaxIndex(), maxLength(), maxLengthAndMaxLengthIndex(), maxLengthIndex(), and model::DEMModel::updateContactNeighborSearchParameters().

Here is the caller graph for this function:

◆ maxIndex() [2/2]

template<typename T >
size_t util::methods::maxIndex ( const std::vector< T > &  data,
size_t  data_start,
size_t  data_end 
)
inline

Returns the index corresponding to maximum from list of data.

Parameters
dataList of real numbers
Returns
i Index with a maximum value

Definition at line 117 of file methods.h.

118 {
119
120 if (data.size() == 0) {
121 std::cerr << "Error: maxIndex() is called with data of size " << data.size()
122 << ".\n";
123 exit(EXIT_FAILURE);
124 }
125
126 if (data_end == 0 or data_end > data.size()) {
127 std::cerr << "Error: maxIndex() data_end = " << data_end
128 << " is not valid for the data of size " << data.size()
129 << ".\n";
130 exit(EXIT_FAILURE);
131 }
132
133 if (data_start > data.size() - 1) {
134 std::cerr << "Error: maxIndex() data_start = " << data_start
135 << " is not valid for the data of size " << data.size()
136 << ".\n";
137 exit(EXIT_FAILURE);
138 }
139
140 // initialize original index locations
141 std::vector<size_t> idx(data_end - data_start);
142 std::iota(idx.begin(), idx.end() , 0);
143
144 std::stable_sort(idx.begin(), idx.end(),
145 [&data, &data_start](size_t i1, size_t i2)
146 {return data[i1 + data_start] > data[i2 + data_start];});
147
148 return idx[0] + data_start;
149};

◆ maxLength()

double util::methods::maxLength ( const std::vector< util::Point > &  data)
inline

Returns the maximum length of point from list of points.

Parameters
dataList of points
Returns
value Maximum length of point

Definition at line 182 of file methods.h.

182 {
183 std::vector<double> length_data(data.size());
184 for (size_t i = 0; i < data.size(); i++)
185 length_data[i] = data[i].length();
186
187 return length_data[util::methods::maxIndex(length_data)];
188};

References maxIndex().

Here is the call graph for this function:

◆ maxLengthAndMaxLengthIndex()

std::pair< double, size_t > util::methods::maxLengthAndMaxLengthIndex ( const std::vector< util::Point > &  data)
inline

Returns the maximum length of point and index from list of points.

Parameters
dataList of points
Returns
pair Maximum length of point and index

Definition at line 208 of file methods.h.

208 {
209 std::vector<double> length_data(data.size());
210 for (size_t i = 0; i < data.size(); i++)
211 length_data[i] = data[i].length();
212
213 auto i = util::methods::maxIndex(length_data);
214 return {length_data[i], i};
215};

References maxIndex().

Referenced by model::DEMModel::checkStop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ maxLengthIndex()

size_t util::methods::maxLengthIndex ( const std::vector< util::Point > &  data)
inline

Returns the index that has maximum length of point from list of points.

Parameters
dataList of points
Returns
i Index with maximum length of point

Definition at line 156 of file methods.h.

156 {
157 std::vector<double> length_data(data.size());
158 for (size_t i = 0; i < data.size(); i++)
159 length_data[i] = data[i].length();
160
161 return util::methods::maxIndex(length_data);
162};

References maxIndex().

Here is the call graph for this function:

◆ min()

template<typename T >
T util::methods::min ( const std::vector< T > &  data)
inline

Returns the minimim from list of data.

Parameters
dataList of real numbers
Returns
value Minimum value

Definition at line 84 of file methods.h.

84 {
85 return data[util::methods::minIndex(data)];
86};
size_t minIndex(const std::vector< T > &data)
Returns the index corresponding to minimum from list of data.
Definition methods.h:56

References minIndex().

Here is the call graph for this function:

◆ minAndMinIndex()

template<typename T >
std::pair< size_t, T > util::methods::minAndMinIndex ( const std::vector< T > &  data)
inline

Returns the minimum and index of minimum from list of data.

Parameters
dataList of real numbers
Returns
pair Minimum value and index

Definition at line 105 of file methods.h.

105 {
106 auto i = util::methods::minIndex(data);
107 return {data[i], i};
108};

References minIndex().

Here is the call graph for this function:

◆ minIndex()

template<typename T >
size_t util::methods::minIndex ( const std::vector< T > &  data)
inline

Returns the index corresponding to minimum from list of data.

Parameters
dataList of real numbers
Returns
i Index with a minimum value

Definition at line 56 of file methods.h.

56 {
57
58 // initialize original index locations
59 std::vector<size_t> idx(data.size());
60 std::iota(idx.begin(), idx.end(), 0);
61
62 std::stable_sort(idx.begin(), idx.end(),
63 [&data](size_t i1, size_t i2) {return data[i1] < data[i2];});
64
65 return idx[0];
66};

Referenced by min(), minAndMinIndex(), minLength(), minLengthAndMinLengthIndex(), and minLengthIndex().

Here is the caller graph for this function:

◆ minLength()

double util::methods::minLength ( const std::vector< util::Point > &  data)
inline

Returns the minimum length of point from list of points.

Parameters
dataList of points
Returns
value Minimum length of point

Definition at line 195 of file methods.h.

195 {
196 std::vector<double> length_data(data.size());
197 for (size_t i = 0; i < data.size(); i++)
198 length_data[i] = data[i].length();
199
200 return length_data[util::methods::minIndex(length_data)];
201};

References minIndex().

Here is the call graph for this function:

◆ minLengthAndMinLengthIndex()

std::pair< double, size_t > util::methods::minLengthAndMinLengthIndex ( const std::vector< util::Point > &  data)
inline

Returns the minimum length of point and index from list of points.

Parameters
dataList of points
Returns
pair Minimum length of point and index

Definition at line 222 of file methods.h.

222 {
223 std::vector<double> length_data(data.size());
224 for (size_t i = 0; i < data.size(); i++)
225 length_data[i] = data[i].length();
226
227 auto i = util::methods::minIndex(length_data);
228 return {length_data[i], i};
229};

References minIndex().

Here is the call graph for this function:

◆ minLengthIndex()

size_t util::methods::minLengthIndex ( const std::vector< util::Point > &  data)
inline

Returns the index that has minimum length of point from list of points.

Parameters
dataList of points
Returns
i Index with minimum length of point

Definition at line 169 of file methods.h.

169 {
170 std::vector<double> length_data(data.size());
171 for (size_t i = 0; i < data.size(); i++)
172 length_data[i] = data[i].length();
173
174 return util::methods::minIndex(length_data);
175};

References minIndex().

Here is the call graph for this function:

◆ setKeyData()

template<typename T_out >
void util::methods::setKeyData ( std::string  key,
T_out  data,
std::map< std::string, T_out > &  data_map,
bool  issue_err = false 
)
inline

Set value to data associated with key.

Parameters
keyKey to append the data to

Definition at line 366 of file methods.h.

366 {
367
368 if (data_map.find(key) == data_map.end()) {
369 if (issue_err) {
370 std::cerr << "Error: key = " << key << " does not exist in data map.\n";
371 exit(EXIT_FAILURE);
372 }
373 }
374
375 data_map[key] = data;
376};

◆ timeDiff()

float util::methods::timeDiff ( std::chrono::steady_clock::time_point  begin,
std::chrono::steady_clock::time_point  end,
std::string  unit = "microseconds" 
)
inline

Returns difference between two times.

Parameters
beginBeginning time
endEnding time
unitUnit in which time difference is to be returned
Returns
time Time difference

Definition at line 304 of file methods.h.

305 {
306 if (unit == "microseconds")
307 return std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
308 else if (unit == "milliseconds")
309 return std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
310 else if (unit == "seconds")
311 return std::chrono::duration_cast<std::chrono::seconds>(end - begin).count();
312 else {
313 std::cerr << "Unit = " << unit << " not valid.\n";
314 exit(EXIT_FAILURE);
315 }
316};

Referenced by model::DEMModel::computeForces(), peridynamics::Model::computeForces(), model::DEMModel::init(), model::DEMModel::integrate(), twoparticle_demo::Model::integrate(), main(), fe::metisGraphPartition(), nsearch::NFlannSearchKd< dim >::setInputCloud(), test::testGraphPartitioning(), test::testPeriDEM(), test::testTaskflow(), and test::testTriElemTime().

Here is the caller graph for this function: