Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
grid.h
Go to the documentation of this file.
1 #ifndef _GRID_HEADER
2 #define _GRID_HEADER
3 #include <assert.h>
4 #include <cstring>
5 #include <iosfwd>
6 #include <fstream>
7 #include <complex>
8 #include <vector>
9 #include <climits>
10 
11  //=========================================
12  // //
13  // A simple multidimensional grid-class //
14  // //
15  // Bounds-check for array lookups //
16  // #define _BOUNDSCHECK //
17  // //
18  //=========================================
19 
20 template<size_t NDIM, typename T>
21 class Grid {
22 
23  private:
24 
25  size_t _N; // Number of cells per dim in the grid
26  size_t _Ntot; // Total number of cells in the grid
27  std::vector<T> _y; // The grid data
28 
29  public:
30 
31  // Constructors
32  Grid() : Grid(0, 0.0) {}
33  Grid(size_t N) : Grid(N, 0.0) {}
34  Grid(size_t N, T yini);
35 
36  // Get a pointer to the T-array
37  T* get_y();
38  T const * get_y() const;
39  const std::vector<T>& get_vec() const;
40 
41  // Allow syntax grid[i] to get/set the index = i'th element
42  T& operator[](size_t i);
43  const T& operator[](size_t i) const;
44 
45  // Fetch the index = i element in the grid
46  T get_y(size_t i);
47 
48  // Assign value in the grid
49  void set_y(std::vector<T> &y);
50  void set_y(size_t i, T &value);
51 
52  // Grid-index -> coordinate list [ i = ix1 + N * ix2 + N^2 * ix3 + ... ]
53  std::vector<size_t> index_list(size_t i);
54 
55  // Get some info about the grid
56  size_t get_N() const;
57  size_t get_Ntot() const;
58 
59  // Convert coordiates -> index in the grid
60  size_t grid_index(std::vector<size_t> &index_list);
61  size_t grid_index_3d(size_t ix, size_t iy, size_t iz);
62  size_t grid_index_2d(size_t ix, size_t iy);
63 
64  // Dump a grid to file
65  void dump_to_file(std::string filename);
66 
67  // Read a grid from file into the object
68  void read_from_file(std::string filename);
69 
70  // Maximum (in norm)
71  double max(){
72  double maxval = std::norm(_y[0]);
73 #ifdef OPENMP
74 #pragma omp parallel for reduction(max: maxval)
75 #endif
76  for(size_t i = 0; i < _Ntot; i++){
77  double curval = std::norm(_y[i]);
78  if(curval > maxval) maxval = curval;
79  }
80  return std::sqrt( maxval );
81  }
82 
83  // Maximum (in norm)
84  double min(){
85  double minval = std::norm(_y[0]);
86 #ifdef OPENMP
87 #pragma omp parallel for reduction(min: minval)
88 #endif
89  for(size_t i = 0; i < _Ntot; i++){
90  double curval = std::norm(_y[i]);
91  if(curval < minval) minval = curval;
92  }
93  return std::sqrt( minval );
94  }
95 
96  // Free up all memory and reset all variables
97  void clear();
98 
99  // Operator overloading: add two grids element by element
100  template<size_t NNDIM, typename TT>
102 #ifdef _BOUNDSCHECK
103  assert(this->_N == rhs._N);
104 #endif
105 #ifdef OPENMP
106 #pragma omp parallel for
107 #endif
108  for(size_t i = 0; i < _Ntot; i++)
109  this->_y[i] += rhs._y[i];
110  return *this;
111  }
112 
113  // Operator overloading: subtract two grids element by element
114  template<size_t NNDIM, typename TT>
116 #ifdef _BOUNDSCHECK
117  assert(this->_N == rhs._N);
118 #endif
119 #ifdef OPENMP
120 #pragma omp parallel for
121 #endif
122  for(size_t i = 0; i < _Ntot; i++)
123  this->_y[i] -= rhs._y[i];
124  return *this;
125  }
126 
127  // Operator overloading: multiply two grids element by element
128  template<size_t NNDIM, typename TT>
130 #ifdef _BOUNDSCHECK
131  assert(this->_N == rhs._N);
132 #endif
133 #ifdef OPENMP
134 #pragma omp parallel for
135 #endif
136  for(size_t i = 0; i < _Ntot; i++)
137  this->_y[i] *= rhs._y[i];
138  return *this;
139  }
140 
141  // Operator overloading: multiply two grids element by element
142  template<size_t NNDIM, typename TT>
144 #ifdef _BOUNDSCHECK
145  assert(this->_N == rhs._N);
146 #endif
147 #ifdef OPENMP
148 #pragma omp parallel for
149 #endif
150  for(size_t i = 0; i < _Ntot; i++)
151  this->_y[i] /= rhs._y[i];
152  return *this;
153  }
154 
155  // Operator overloading: multiply every element in grid by scalar
156  Grid<NDIM,T>& operator *=(const T & rhs){
157 #ifdef OPENMP
158 #pragma omp parallel for
159 #endif
160  for(size_t i = 0; i < _Ntot; i++)
161  this->_y[i] *= rhs;
162  return *this;
163  }
164 
165  // Operator overloading: divide every element in grid by scalar
166  Grid<NDIM,T>& operator /=(const T & rhs){
167 #ifdef OPENMP
168 #pragma omp parallel for
169 #endif
170  for(size_t i = 0; i < _Ntot; i++)
171  this->_y[i] /= rhs;
172  return *this;
173  }
174 
175  // The rms-norm, sqrt[ Sum y[i]^2 / Ntot ], of the grid
176  double rms_norm();
177 
178  // Check for NaN and exit if true
179  void check_for_nan(bool exitifnan);
180 };
181 
182 template<size_t NDIM, typename T>
184  lhs += rhs;
185  return lhs;
186 }
187 
188 template<size_t NDIM, typename T>
190  lhs -= rhs;
191  return lhs;
192 }
193 
194 template<size_t NDIM, typename T>
196  lhs *= rhs;
197  return lhs;
198 }
199 
200 template<size_t NDIM, typename T>
202  lhs /= rhs;
203  return lhs;
204 }
205 
206 template<size_t NDIM, typename T>
208  lhs *= rhs;
209  return lhs;
210 }
211 
212 template<size_t NDIM, typename T>
214  lhs /= rhs;
215  return lhs;
216 }
217 
218 template<size_t NDIM, typename T>
220  lhs += rhs;
221  return lhs;
222 }
223 
224 template<size_t NDIM, typename T>
226  lhs -= rhs;
227  return lhs;
228 }
229 
230 template<size_t NDIM, typename T>
232  for(size_t i = 0; i < lhs.get_Ntot(); i++)
233  lhs[i] = sqrt(fabs(lhs[i]));
234  return lhs;
235 }
236 
237 template<size_t NDIM, typename T>
239  double rms = 0.0;
240 #ifdef OPENMP
241 #pragma omp parallel for reduction(+:rms)
242 #endif
243  for(size_t i = 0; i < _Ntot; i++){
244  rms += std::norm(_y[i]);
245  }
246  rms = std::sqrt(rms / double(_Ntot));
247  return rms;
248 }
249 
250 #endif
size_t _Ntot
Definition: grid.h:26
size_t grid_index_2d(size_t ix, size_t iy)
Definition: grid.cpp:130
Grid< NDIM, T > operator/(Grid< NDIM, T > lhs, const Grid< NDIM, T > &rhs)
Definition: grid.h:201
std::vector< size_t > index_list(size_t i)
Definition: grid.cpp:102
double max()
Definition: grid.h:71
std::vector< T > _y
Definition: grid.h:27
void dump_to_file(std::string filename)
Definition: grid.cpp:147
T & operator[](size_t i)
Definition: grid.cpp:58
Definition: grid.h:21
void set_y(std::vector< T > &y)
Definition: grid.cpp:84
size_t grid_index_3d(size_t ix, size_t iy, size_t iz)
Definition: grid.cpp:124
int iz
Definition: mfunc_bm.py:17
size_t get_N() const
Definition: grid.cpp:135
T * get_y()
Definition: grid.cpp:41
Grid< NDIM, T > operator*(Grid< NDIM, T > lhs, const Grid< NDIM, T > &rhs)
Definition: grid.h:195
size_t get_Ntot() const
Definition: grid.cpp:141
const std::vector< T > & get_vec() const
Definition: grid.cpp:51
size_t grid_index(std::vector< size_t > &index_list)
Definition: grid.cpp:112
size_t _N
Definition: grid.h:25
Grid< NDIM, T > sqrt(Grid< NDIM, T > lhs)
Definition: grid.h:231
Grid< NNDIM, TT > & operator+=(const Grid< NNDIM, TT > &rhs)
Definition: grid.h:101
Grid< NDIM, T > operator-(Grid< NDIM, T > lhs, const Grid< NDIM, T > &rhs)
Definition: grid.h:189
void clear()
Definition: grid.cpp:197
void read_from_file(std::string filename)
Definition: grid.cpp:165
Grid< NNDIM, TT > & operator*=(const Grid< NNDIM, TT > &rhs)
Definition: grid.h:129
Grid< NNDIM, TT > & operator/=(const Grid< NNDIM, TT > &rhs)
Definition: grid.h:143
void check_for_nan(bool exitifnan)
Definition: grid.cpp:20
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
Grid()
Definition: grid.h:32
double rms_norm()
Definition: grid.h:238
Grid(size_t N)
Definition: grid.h:33
double min()
Definition: grid.h:84
Grid< NDIM, T > operator+(Grid< NDIM, T > lhs, const Grid< NDIM, T > &rhs)
Definition: grid.h:183
Grid< NNDIM, TT > & operator-=(const Grid< NNDIM, TT > &rhs)
Definition: grid.h:115