Phoebe developer's documentation  1.1.0
Phonon and Electron Boltzmann Equations
utilities.h
1 #ifndef UTILS_H
2 #define UTILS_H
3 
4 #include <tuple>
5 #include <string>
6 #include <vector>
7 #include "eigen.h"
8 
9 // returns the remainder of the division between two integers.
10 // Note: c++ defines the % operator such that: (a/b)*b + a%b == a (for b!=0)
11 // this works as mod() in fortran or % in python when a and b are positive
12 // But the behavior is not the same for negative integers
13 // the function below can be used instead
14 int mod(const int &a, const int &b);
15 
16 // returns -1 if val<0, 0 if val=0, and 1 if val>0
17 template<typename T> int sgn(T &val) {
18  return (T(0) < val) - (val < T(0));
19 }
20 
21 int compress3Indices(const int &i1, const int &i2, const int &i3,
22  const int &size1, const int &size2, const int &size3);
23 
24 std::tuple<int, int, int> decompress3Indices(const int &iTot,
25  const int &size1, const int &size2, const int &size3);
26 
27 int compress2Indices(const int &i1, const int &i2, const int &size1,
28  const int &size2);
29 
30 std::tuple<int, int> decompress2Indices(const int &iTot, const int &size1,
31  const int &size2);
32 
33 // helper to break up strings by commas and spaces
34 std::vector<std::string> tokenize(const std::string str);
35 
36 // A function to allocate a dynamically sized array. It tricks the
37 // compiler into thinking the size is a constant via the const identifier
38 // on the argument. This resolves issues with VLAs -- see crystal.cpp
39 template <typename T> T* allocate(T *&array, const unsigned int size){
40  array = new T [size];
41  return array;
42 }
43 
50 template<typename T, typename Parameter>
51 class NamedType {
52 public:
53  explicit NamedType(T const &value) :
54  value_(value) {
55  }
56  T& get() {
57  return value_;
58  }
59  T const& get() const {
60  return value_;
61  }
62 private:
63  T value_;
64 };
65 
69 
73 
79 
83 
87 
92 
99 std::tuple<double, double> memoryUsage();
100 
109 template<typename Vector>
110 std::vector<Vector> splitVector(const Vector& v, unsigned chunkSize) {
111  using Iterator = typename Vector::const_iterator;
112  std::vector<Vector> vectorChunks;
113  Iterator it = v.cbegin();
114  const Iterator end = v.cend();
115 
116  while (it != end) {
117  Vector v2;
118  std::back_insert_iterator<Vector> inserter(v2);
119  const auto num_to_copy = std::min(static_cast<unsigned>(
120  std::distance(it, end)), chunkSize);
121  std::copy(it, it + num_to_copy, inserter);
122  vectorChunks.push_back(std::move(v2));
123  std::advance(it, num_to_copy);
124  }
125 
126  return vectorChunks;
127 }
128 
129 double findMaxRelativeDifference(const Eigen::Tensor<double,3> &x,
130  const Eigen::Tensor<double,3> &xRef);
131 
136 std::vector<std::string> split(const std::string &s, char delimiter);
137 
138 #endif
139 
Class for implementing strong typing.
Definition: utilities.h:51