PeriDEM 0.3.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 >
T max (const std::vector< T > &data)
 Returns the maximum from list of data.
 
template<typename T >
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 >
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.
 
template<class T >
T l2Dist (const std::vector< T > &x1, const std::vector< T > &x2)
 Computes l2 distance between two vectors.
 

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 243 of file vecMethods.h.

243 {
244 return std::reduce(data.begin(), data.end());
245};
Definition contact.h:20

◆ 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 294 of file vecMethods.h.

294 {
295 for (const auto &j : list)
296 if (j == i)
297 return;
298
299 list.emplace_back(i);
300};

References max().

Referenced by contact::Damping::apply().

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

◆ 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 351 of file vecMethods.h.

351 {
352
353 if (data_map.find(key) == data_map.end()) {
354 if (issue_err) {
355 throw std::runtime_error(
357 << "Error: key = " << key << " does not exist in data map.\n");
358 }
359 else {
360 data_map[key] = data;
361 }
362 }
363 else {
364 data_map[key] = data_map[key] + data;
365 }
366};
Collects a message with stream syntax for use in an exception.
Definition io.h:52

References max().

Here is the call graph for this function:

◆ 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 331 of file vecMethods.h.

331 {
332
333 if (issue_err) {
334 if (data_map.find(key) == data_map.end()) {
335 throw std::runtime_error(
337 << "Error: key = " << key << " does not exist in data map.\n");
338 }
339 }
340
341 return data_map[key];
342};

References max().

Here is the call graph for this function:

◆ 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 254 of file vecMethods.h.

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

References max().

Referenced by PeriDEMModel::init(), and data::ModelData::isDofFree().

Here is the call graph for this function:
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 259 of file vecMethods.h.

259 {
260 return !(i >> dof & 1UL);
261};

References max().

Here is the call graph for this function:

◆ 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 270 of file vecMethods.h.

270 {
271 for (const auto &j : list)
272 if (j == i)
273 return true;
274
275 return false;
276};

References max().

Referenced by geom::isNumberOfParamForGeometryValid(), isTagInList(), loading::needToComputeDof(), and loading::needToProcessParticle().

Here is the call graph for this function:
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 284 of file vecMethods.h.

284 {
285 return isInList(tag, tags);
286};

References isInList(), and max().

Referenced by rw::writer::VtkParticleWriter::appendNodes(), anonymous_namespace{vtkParticleWriter.cpp}::appendPointArraysForNodes(), geom::createGeomObjectOld(), data::setupQuadratureData(), and rw::writeOutput().

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

◆ l2Dist()

template<class T >
T util::methods::l2Dist ( const std::vector< T > &  x1,
const std::vector< T > &  x2 
)
inline

Computes l2 distance between two vectors.

Parameters
x1Vector 1
x2Vector 2
Returns
l2_dist L2 distance

Definition at line 395 of file vecMethods.h.

395 {
396
397 if (x1.size() != x2.size())
398 throw std::invalid_argument("size of two vectors not matching");
399
400 T sum = 0.;
401 for (size_t i=0; i<x1.size(); i++)
402 sum += (x1[i] - x2[i]) * (x1[i] - x2[i]);
403
404 return std::sqrt(sum);
405}

References max().

Referenced by test::testUtilMethods().

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 76 of file vecMethods.h.

76 {
78};
size_t maxIndex(const std::vector< T > &data)
Returns the index corresponding to maximum from list of data.
Definition vecMethods.h:40

References maxIndex().

Referenced by addToList(), data::ModelData::appendKeyData(), appendKeyData(), data::ModelData::getKeyData(), getKeyData(), PeriDEMModel::init(), isFree(), isFree(), isInList(), isTagInList(), l2Dist(), maxIndex(), maxIndex(), maxLength(), maxLengthAndMaxLengthIndex(), maxLengthIndex(), minIndex(), minLength(), minLengthAndMinLengthIndex(), minLengthIndex(), data::ModelData::setKeyData(), setKeyData(), timeDiff(), and contact::Contact::updateSearchParameters().

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 96 of file vecMethods.h.

96 {
98 return {data[i], i};
99};

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 40 of file vecMethods.h.

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

References max().

Referenced by geom::ComplexGeomObject::center(), max(), maxAndMaxIndex(), maxLength(), maxLengthAndMaxLengthIndex(), maxLengthIndex(), and contact::Contact::updateSearchParameters().

Here is the call graph for this function:
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 119 of file vecMethods.h.

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

References max().

Here is the call graph for this function:

◆ 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 187 of file vecMethods.h.

187 {
188 std::vector<double> length_data(data.size());
189 for (size_t i = 0; i < data.size(); i++)
190 length_data[i] = data[i].length();
191
192 return length_data[util::methods::maxIndex(length_data)];
193};

References max(), and 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 213 of file vecMethods.h.

213 {
214 std::vector<double> length_data(data.size());
215 for (size_t i = 0; i < data.size(); i++)
216 length_data[i] = data[i].length();
217
218 auto i = util::methods::maxIndex(length_data);
219 return {length_data[i], i};
220};

References max(), and maxIndex().

Referenced by postprocess::Postprocess::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 161 of file vecMethods.h.

161 {
162 std::vector<double> length_data(data.size());
163 for (size_t i = 0; i < data.size(); i++)
164 length_data[i] = data[i].length();
165
166 return util::methods::maxIndex(length_data);
167};

References max(), and 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 86 of file vecMethods.h.

86 {
88};
size_t minIndex(const std::vector< T > &data)
Returns the index corresponding to minimum from list of data.
Definition vecMethods.h:58

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 107 of file vecMethods.h.

107 {
109 return {data[i], i};
110};

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 58 of file vecMethods.h.

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

References max().

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

Here is the call graph for this function:
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 200 of file vecMethods.h.

200 {
201 std::vector<double> length_data(data.size());
202 for (size_t i = 0; i < data.size(); i++)
203 length_data[i] = data[i].length();
204
205 return length_data[util::methods::minIndex(length_data)];
206};

References max(), and 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 227 of file vecMethods.h.

227 {
228 std::vector<double> length_data(data.size());
229 for (size_t i = 0; i < data.size(); i++)
230 length_data[i] = data[i].length();
231
232 auto i = util::methods::minIndex(length_data);
233 return {length_data[i], i};
234};

References max(), and 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 174 of file vecMethods.h.

174 {
175 std::vector<double> length_data(data.size());
176 for (size_t i = 0; i < data.size(); i++)
177 length_data[i] = data[i].length();
178
179 return util::methods::minIndex(length_data);
180};

References max(), and 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 374 of file vecMethods.h.

374 {
375
376 if (data_map.find(key) == data_map.end()) {
377 if (issue_err) {
378 throw std::runtime_error(
380 << "Error: key = " << key << " does not exist in data map.\n");
381 }
382 }
383
384 data_map[key] = data;
385};

References max().

Here is the call graph for this function:

◆ 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 309 of file vecMethods.h.

310 {
311 if (unit == "microseconds")
312 return std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count();
313 else if (unit == "milliseconds")
314 return std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
315 else if (unit == "seconds")
316 return std::chrono::duration_cast<std::chrono::seconds>(end - begin).count();
317 else {
318 throw std::runtime_error(
320 << "Unit = " << unit << " not valid.\n");
321 }
322};

References max().

Referenced by PeriDEMModel::computeForces(), pd::exchangeGhostDisplacement(), particle::exchangeGhostKinematics(), pd::exchangeGhostTheta(), PeriDEMModel::init(), time_int::Integrator::integrate(), main(), mesh::metisGraphPartition(), nsearch::NFlannSearchKd< dim >::setInputCloud(), test::testGraphPartitioning(), test::testTaskflow(), and test::testTriElemTime().

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