PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
methods.h
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#pragma once
12
13#include "point.h" // definition of Point
14#include <cstdint> // uint8_t type
15#include <cstring> // string and size_t type
16#include <vector>
17#include <chrono>
18#include <numeric> // std::iota
19#include <algorithm> // std::sort, std::stable_sort
20#include <map>
21
22using namespace std::chrono;
23
24namespace util {
25
30namespace methods {
31
37template <typename T>
38inline size_t maxIndex(const std::vector<T> &data) {
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};
49
55template <typename T>
56inline size_t minIndex(const std::vector<T> &data) {
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};
67
73template <typename T>
74inline T max(const std::vector<T> &data) {
75 return data[util::methods::maxIndex(data)];
76};
77
83template <typename T>
84inline T min(const std::vector<T> &data) {
85 return data[util::methods::minIndex(data)];
86};
87
93template <typename T>
94inline std::pair<size_t, T> maxAndMaxIndex(const std::vector<T> &data) {
95 auto i = util::methods::maxIndex(data);
96 return {data[i], i};
97};
98
104template <typename T>
105inline std::pair<size_t, T> minAndMinIndex(const std::vector<T> &data) {
106 auto i = util::methods::minIndex(data);
107 return {data[i], i};
108};
109
110
116template <typename T>
117inline size_t maxIndex(const std::vector<T> &data,
118 size_t data_start, size_t data_end) {
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};
150
156inline size_t maxLengthIndex(const std::vector<util::Point> &data) {
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};
163
169inline size_t minLengthIndex(const std::vector<util::Point> &data) {
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};
176
182inline double maxLength(const std::vector<util::Point> &data) {
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};
189
195inline double minLength(const std::vector<util::Point> &data) {
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};
202
208inline std::pair<double, size_t> maxLengthAndMaxLengthIndex(const std::vector<util::Point> &data) {
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};
216
222inline std::pair<double, size_t> minLengthAndMinLengthIndex(const std::vector<util::Point> &data) {
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};
230
231
237template <typename T>
238inline T add(const std::vector<T> &data) {
239 return std::reduce(data.begin(), data.end());
240};
241
242
249inline bool isFree(const int &i, const unsigned int &dof) {
250 return !(i >> dof & 1UL);
251};
252
254inline bool isFree(const uint8_t &i, const unsigned int &dof) {
255 return !(i >> dof & 1UL);
256};
257
264template <typename T>
265inline bool isInList(const T &i, const std::vector<T> &list) {
266 for (const auto &j : list)
267 if (j == i)
268 return true;
269
270 return false;
271};
272
279inline bool isTagInList(const std::string &tag, const std::vector<std::string> &tags) {
280 return isInList(tag, tags);
281};
282
288template <typename T>
289inline void addToList(const T &i, std::vector<T> &list) {
290 for (const auto &j : list)
291 if (j == i)
292 return;
293
294 list.emplace_back(i);
295};
296
304inline float timeDiff(std::chrono::steady_clock::time_point begin,
305 std::chrono::steady_clock::time_point end, std::string unit = "microseconds") {
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};
317
324template <typename T_out>
325inline T_out getKeyData(std::string key, std::map<std::string, T_out> &data_map, bool issue_err = false) {
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};
336
337
343template <typename T_out>
344inline void appendKeyData(std::string key, T_out data, std::map<std::string, T_out> &data_map, bool issue_err = false) {
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};
359
365template <typename T_out>
366inline void setKeyData(std::string key, T_out data, std::map<std::string, T_out> &data_map, bool issue_err = false) {
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};
377
378
379
380} // namespace methods
381
382} // namespace util
std::pair< double, size_t > maxLengthAndMaxLengthIndex(const std::vector< util::Point > &data)
Returns the maximum length of point and index from list of points.
Definition methods.h:208
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.
Definition methods.h:344
T_out getKeyData(std::string key, std::map< std::string, T_out > &data_map, bool issue_err=false)
Get data for a key.
Definition methods.h:325
size_t maxLengthIndex(const std::vector< util::Point > &data)
Returns the index that has maximum length of point from list of points.
Definition methods.h:156
std::pair< size_t, T > minAndMinIndex(const std::vector< T > &data)
Returns the minimum and index of minimum from list of data.
Definition methods.h:105
double minLength(const std::vector< util::Point > &data)
Returns the minimum length of point from list of points.
Definition methods.h:195
T max(const std::vector< T > &data)
Returns the maximum from list of data.
Definition methods.h:74
size_t minIndex(const std::vector< T > &data)
Returns the index corresponding to minimum from list of data.
Definition methods.h:56
T min(const std::vector< T > &data)
Returns the minimim from list of data.
Definition methods.h:84
bool isInList(const T &i, const std::vector< T > &list)
Find if data is in the list.
Definition methods.h:265
size_t minLengthIndex(const std::vector< util::Point > &data)
Returns the index that has minimum length of point from list of points.
Definition methods.h:169
T add(const std::vector< T > &data)
Returns the sum of data.
Definition methods.h:238
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.
Definition methods.h:304
bool isTagInList(const std::string &tag, const std::vector< std::string > &tags)
Returns true if tag is found in the list of tags.
Definition methods.h:279
std::pair< size_t, T > maxAndMaxIndex(const std::vector< T > &data)
Returns the maximum and index of maximum from list of data.
Definition methods.h:94
bool isFree(const int &i, const unsigned int &dof)
Returns true if degree of freedom is free.
Definition methods.h:249
void addToList(const T &i, std::vector< T > &list)
Add element to the list.
Definition methods.h:289
std::pair< double, size_t > minLengthAndMinLengthIndex(const std::vector< util::Point > &data)
Returns the minimum length of point and index from list of points.
Definition methods.h:222
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.
Definition methods.h:366
size_t maxIndex(const std::vector< T > &data)
Returns the index corresponding to maximum from list of data.
Definition methods.h:38
double maxLength(const std::vector< util::Point > &data)
Returns the maximum length of point from list of points.
Definition methods.h:182
Collection of methods useful in simulation.