Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
class_mesh.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 #include "stdafx.h"
11 #include "precision.hpp"
12 #include "class_vec_3d.hpp"
13 
14 // from "core_mesh.hpp"
15 template<typename T> void get_per(Vec_3D<T> &position, size_t per);
16 template<typename T> void get_per(Vec_3D<T> &position, size_t perx, size_t pery, size_t perz);
17 
23 template <typename T>
24 class Mesh_base
25 {
26 public:
27  // CONSTRUCTOR
28  Mesh_base(size_t n1, size_t n2, size_t n3):
29  N1(n1), N2(n2), N3(n3), length(n1*n2*n3), data(length) {}
30 
31  // VARIABLES
32  size_t N1, N2, N3, length; // acces dimensions and length of mesh
33  std::vector<T> data; // data stored on the mesh
34 
35  // METHODS
36  T* real() { return data.data();} // acces data through pointer
37  const T* real() const { return data.data();} // acces data through const pointer
38  void assign(T val)
39  {
40  #pragma omp parallel for
41  for (size_t i = 0; i < length; i++) this->data[i]=val;
42  }
43 
44  // OPERATORS
45  T &operator[](size_t i){ return data[i]; }
46  const T &operator[](size_t i) const{ return data[i]; }
47 
48  T& operator()(size_t i, size_t j, size_t k){ return data[i*N2*N3+j*N3+k]; }
49  const T& operator()(size_t i, size_t j, size_t k) const{ return data[i*N2*N3+j*N3+k]; }
50 
51  T& operator()(size_t i, size_t j){ return data[i*N3+j]; }
52  const T& operator()(size_t i, size_t j) const{ return data[i*N3+j]; }
53 
54  template<typename U> T& operator()(Vec_3D<U> pos)
55  {
56  get_per(pos, N1, N2, N3);
57  return data[size_t(pos[0])*N2*N3+size_t(pos[1])*N3+size_t(pos[2])];
58  }
59 
60  template<typename U> const T& operator()(Vec_3D<U> pos) const
61  {
62  get_per(pos, N1, N2, N3);
63  return data[size_t(pos[0])*N2*N3+size_t(pos[1])*N3+size_t(pos[2])];
64  }
65 
66  Mesh_base& operator+=(const T& rhs)
67  {
68  #pragma omp parallel for
69  for (size_t i = 0; i < length; i++) this->data[i]+=rhs;
70  return *this;
71  }
72 
73  Mesh_base& operator-=(const T& rhs){ return *this+=-rhs; }
74 
75  Mesh_base& operator*=(const T& rhs)
76  {
77  #pragma omp parallel for
78  for (size_t i = 0; i < length; i++) this->data[i]*=rhs;
79  return *this;
80  }
81 
82  Mesh_base& operator/=(const T& rhs)
83  {
84  #pragma omp parallel for
85  for (size_t i = 0; i < length; i++) this->data[i]/=rhs;
86  return *this;
87  }
88 
89 };
90 
95 class Mesh : public Mesh_base<FTYPE_t>
96 {
97 public:
98  // CONSTRUCTORS & DESTRUCTOR
99  Mesh(size_t n): Mesh_base(n, n, n+2), N(n) {}
100 
101  // VARIABLES
102  size_t N; // acces dimension of mesh
103 
104  // METHODS
105 
111  FFTW_COMPLEX_TYPE* complex() { return reinterpret_cast<FFTW_COMPLEX_TYPE*>(data.data());}
112 
118  const FFTW_COMPLEX_TYPE* complex() const { return reinterpret_cast<const FFTW_COMPLEX_TYPE*>(data.data());}
119 
120  void reset_part(bool part)
121  {/* nullify real (part = 0) or complex (part = 1) part of a field */
122  #pragma omp parallel for
123  for (size_t i = part; i < this->length; i+=2){
124  data[i] = 0;
125  }
126  }
127 
128  void reset_re() { reset_part(0); }
129  void reset_im() { reset_part(1); }
130 
131  // OPERATORS
133 
134  template<typename U> FTYPE_t& operator()(Vec_3D<U> pos)
135  {
136  get_per(pos, N);
137  return data[size_t(pos[0])*N2*N3+size_t(pos[1])*N3+size_t(pos[2])];
138  }
139 
140  template<typename U> const FTYPE_t& operator()(Vec_3D<U> pos) const
141  {
142  get_per(pos, N);
143  return data[size_t(pos[0])*N2*N3+size_t(pos[1])*N3+size_t(pos[2])];
144  }
145 };
T & operator[](size_t i)
Definition: class_mesh.hpp:45
Mesh_base & operator-=(const T &rhs)
Definition: class_mesh.hpp:73
const T * real() const
Definition: class_mesh.hpp:37
FFTW_COMPLEX_TYPE * complex()
get fftw_complex pointer to data
Definition: class_mesh.hpp:111
Mesh(size_t n)
Definition: class_mesh.hpp:99
define container Vec_3D
T * real()
Definition: class_mesh.hpp:36
const double & operator()(Vec_3D< U > pos) const
Definition: class_mesh.hpp:140
const T & operator[](size_t i) const
Definition: class_mesh.hpp:46
: creates a mesh of N*N*(N+2) cells
Definition: class_mesh.hpp:95
const FFTW_COMPLEX_TYPE * complex() const
get const fftw_complex pointer to data
Definition: class_mesh.hpp:118
Mesh_base(size_t n1, size_t n2, size_t n3)
Definition: class_mesh.hpp:28
std::vector< T > data
Definition: class_mesh.hpp:33
const T & operator()(size_t i, size_t j) const
Definition: class_mesh.hpp:52
system include files and for project-specific include files that are used frequently but are changed ...
T & operator()(size_t i, size_t j, size_t k)
Definition: class_mesh.hpp:48
size_t N1
Definition: class_mesh.hpp:32
size_t N3
Definition: class_mesh.hpp:32
void reset_re()
Definition: class_mesh.hpp:128
T & operator()(size_t i, size_t j)
Definition: class_mesh.hpp:51
size_t N
Definition: class_mesh.hpp:102
Mesh_base & operator+=(const T &rhs)
Definition: class_mesh.hpp:66
void reset_part(bool part)
Definition: class_mesh.hpp:120
single / double / long double definitions
const T & operator()(size_t i, size_t j, size_t k) const
Definition: class_mesh.hpp:49
size_t length
Definition: class_mesh.hpp:32
double & operator()(Vec_3D< U > pos)
Definition: class_mesh.hpp:134
void get_per(Vec_3D< T > &position, size_t per)
Definition: core_mesh.cpp:66
const T & operator()(Vec_3D< U > pos) const
Definition: class_mesh.hpp:60
T & operator()(Vec_3D< U > pos)
Definition: class_mesh.hpp:54
void assign(T val)
Definition: class_mesh.hpp:38
: class handling basic mesh functions, the most important are creating and destroing the underlying d...
Definition: class_mesh.hpp:24
: class handling basic 3D-vector functions, definitions
#define FFTW_COMPLEX_TYPE
Definition: precision.hpp:28
Mesh_base & operator*=(const T &rhs)
Definition: class_mesh.hpp:75
size_t N2
Definition: class_mesh.hpp:32
Mesh_base & operator/=(const T &rhs)
Definition: class_mesh.hpp:82
void reset_im()
Definition: class_mesh.hpp:129