PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
vecMethods.h
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2026 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 "util/io.h"
18#include <stdexcept>
19#include <chrono>
20#include <numeric> // std::iota
21#include <algorithm> // std::sort, std::stable_sort
22#include <map>
23
24using namespace std::chrono;
25
26namespace util {
27
32namespace methods {
33
39template <typename T>
40inline size_t maxIndex(const std::vector<T> &data) {
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};
51
57template <typename T>
58inline size_t minIndex(const std::vector<T> &data) {
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};
69
75template <typename T>
76inline T max(const std::vector<T> &data) {
78};
79
85template <typename T>
86inline T min(const std::vector<T> &data) {
88};
89
95template <typename T>
96inline std::pair<size_t, T> maxAndMaxIndex(const std::vector<T> &data) {
98 return {data[i], i};
99};
100
106template <typename T>
107inline std::pair<size_t, T> minAndMinIndex(const std::vector<T> &data) {
109 return {data[i], i};
110};
111
112
118template <typename T>
119inline size_t maxIndex(const std::vector<T> &data,
120 size_t data_start, size_t data_end) {
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};
155
161inline size_t maxLengthIndex(const std::vector<util::Point> &data) {
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
167};
168
174inline size_t minLengthIndex(const std::vector<util::Point> &data) {
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
180};
181
187inline double maxLength(const std::vector<util::Point> &data) {
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
193};
194
200inline double minLength(const std::vector<util::Point> &data) {
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
206};
207
213inline std::pair<double, size_t> maxLengthAndMaxLengthIndex(const std::vector<util::Point> &data) {
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
219 return {length_data[i], i};
220};
221
227inline std::pair<double, size_t> minLengthAndMinLengthIndex(const std::vector<util::Point> &data) {
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
233 return {length_data[i], i};
234};
235
236
242template <typename T>
243inline T add(const std::vector<T> &data) {
244 return std::reduce(data.begin(), data.end());
245};
246
247
254inline bool isFree(const int &i, const unsigned int &dof) {
255 return !(i >> dof & 1UL);
256};
257
259inline bool isFree(const uint8_t &i, const unsigned int &dof) {
260 return !(i >> dof & 1UL);
261};
262
269template <typename T>
270inline bool isInList(const T &i, const std::vector<T> &list) {
271 for (const auto &j : list)
272 if (j == i)
273 return true;
274
275 return false;
276};
277
284inline bool isTagInList(const std::string &tag, const std::vector<std::string> &tags) {
285 return isInList(tag, tags);
286};
287
293template <typename T>
294inline void addToList(const T &i, std::vector<T> &list) {
295 for (const auto &j : list)
296 if (j == i)
297 return;
298
299 list.emplace_back(i);
300};
301
309inline float timeDiff(std::chrono::steady_clock::time_point begin,
310 std::chrono::steady_clock::time_point end, std::string unit = "microseconds") {
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};
323
330template <typename T_out>
331inline T_out getKeyData(std::string key, std::map<std::string, T_out> &data_map, bool issue_err = false) {
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};
343
344
350template <typename T_out>
351inline void appendKeyData(std::string key, T_out data, std::map<std::string, T_out> &data_map, bool issue_err = false) {
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};
367
373template <typename T_out>
374inline void setKeyData(std::string key, T_out data, std::map<std::string, T_out> &data_map, bool issue_err = false) {
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};
386
394template <class T>
395inline T l2Dist(const std::vector<T> &x1, const std::vector<T> &x2) {
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}
406
407} // namespace methods
408
409} // namespace util
Collects a message with stream syntax for use in an exception.
Definition io.h:52
Definition contact.h:20
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 vecMethods.h:213
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 vecMethods.h:351
T_out getKeyData(std::string key, std::map< std::string, T_out > &data_map, bool issue_err=false)
Get data for a key.
Definition vecMethods.h:331
size_t maxLengthIndex(const std::vector< util::Point > &data)
Returns the index that has maximum length of point from list of points.
Definition vecMethods.h:161
std::pair< size_t, T > minAndMinIndex(const std::vector< T > &data)
Returns the minimum and index of minimum from list of data.
Definition vecMethods.h:107
T l2Dist(const std::vector< T > &x1, const std::vector< T > &x2)
Computes l2 distance between two vectors.
Definition vecMethods.h:395
double minLength(const std::vector< util::Point > &data)
Returns the minimum length of point from list of points.
Definition vecMethods.h:200
T max(const std::vector< T > &data)
Returns the maximum from list of data.
Definition vecMethods.h:76
size_t minIndex(const std::vector< T > &data)
Returns the index corresponding to minimum from list of data.
Definition vecMethods.h:58
T min(const std::vector< T > &data)
Returns the minimim from list of data.
Definition vecMethods.h:86
bool isInList(const T &i, const std::vector< T > &list)
Find if data is in the list.
Definition vecMethods.h:270
size_t minLengthIndex(const std::vector< util::Point > &data)
Returns the index that has minimum length of point from list of points.
Definition vecMethods.h:174
T add(const std::vector< T > &data)
Returns the sum of data.
Definition vecMethods.h:243
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 vecMethods.h:309
bool isTagInList(const std::string &tag, const std::vector< std::string > &tags)
Returns true if tag is found in the list of tags.
Definition vecMethods.h:284
std::pair< size_t, T > maxAndMaxIndex(const std::vector< T > &data)
Returns the maximum and index of maximum from list of data.
Definition vecMethods.h:96
bool isFree(const int &i, const unsigned int &dof)
Returns true if degree of freedom is free.
Definition vecMethods.h:254
void addToList(const T &i, std::vector< T > &list)
Add element to the list.
Definition vecMethods.h:294
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 vecMethods.h:227
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 vecMethods.h:374
size_t maxIndex(const std::vector< T > &data)
Returns the index corresponding to maximum from list of data.
Definition vecMethods.h:40
double maxLength(const std::vector< util::Point > &data)
Returns the maximum length of point from list of points.
Definition vecMethods.h:187
Collection of methods useful in simulation.
Definition constants.h:14