Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
continuity_solver.h
Go to the documentation of this file.
1 #ifndef _CONTINUITYSOLVER_HEADER
2 #define _CONTINUITYSOLVER_HEADER
3 #include "../src/multigrid_solver.h"
4 
5  //=============================================
6  // Solving the continuity equation: //
7  // //
8  // fH delta + D[(1+delta) v] = 0 //
9  // //
10  // where delta' = fHdelta (linear approx) //
11  // and v = H0 B0 D_codephi) is the velocity //
12  // field. //
13  // //
14  // Define f(a) H(a)/H0 by setting //
15  // _growthfacHubble //
16  // //
17  // To avoid dividing by 0 we restrict //
18  // (1+delta) >= _rhomin //
19  // //
20  // Taking _rhomax = _rhomin = 1.0 we get //
21  // the completely linear equation //
22  //=============================================
23 
24 template<unsigned int NDIM, typename T>
25 class ContinuitySolver : public MultiGridSolver<NDIM,T> {
26  private:
27 
28  double _rhomin = 0.1;
29  double _rhomax = 1e30;
30  double _growthfachubble = 0.5;
31 
32  public:
33 
35  ContinuitySolver(unsigned int N, bool verbose = true) : MultiGridSolver<NDIM,T>(N, verbose) {} ;
36  ContinuitySolver(unsigned int N, unsigned int Nmin, bool verbose = true) : MultiGridSolver<NDIM,T>(N, Nmin, verbose) {} ;
37  ContinuitySolver(MultiGrid<NDIM,T> &source, bool verbose = true) : MultiGridSolver<NDIM,T>(source.get_N(), source.get_Nmin(), verbose) {
39  }
40 
41  void set_rhomax(double rhomax){
42  _rhomax = rhomax;
43  }
44 
45  void set_rhomin(double rhomin){
46  _rhomin = rhomin;
47  }
48 
50  _growthfachubble = growthfachubble;
51  }
52 
53  T bfunc(T delta_rho){
54  return std::min( std::max(1.0 + delta_rho, _rhomin) , _rhomax);
55  }
56 
57  // Define the PDE. l_operator is the discretized EOM [ L(phi) = 0 ]
58  T l_operator(unsigned int level, std::vector<unsigned int>& index_list, bool addsource){
59  unsigned int i = index_list[0];
60 
61  // Gridspacing
62  T h = 1.0/T( MultiGridSolver<NDIM,T>::get_N(level) );
63 
64  // Solution and pde-source grid at current level
65  T *phi = MultiGridSolver<NDIM,T>::get_y(level);
67 
68  // Compute the kinetic term D[b Df] (in 1D this is phi''_i = phi_{i+1} + phi_{i-1} - 2 phi_{i} )
69  T kinetic = 0.0;
70  T f = phi[i];
71  T boff = bfunc(delta[i]);
72  for(unsigned int j = 0; j < NDIM; j++){
73  unsigned int iminus = index_list[2*j+1];
74  unsigned int iplus = index_list[2*j+2];
75  T fminus = phi[iminus];
76  T fplus = phi[iplus];
77  T bminus = 0.5 * ( bfunc(delta[iminus]) + boff );
78  T bplus = 0.5 * ( bfunc(delta[iplus]) + boff );
79  kinetic += bplus * (fplus - f) - bminus * (f - fminus);
80  }
81 
82  // The right hand side of the PDE with the source term arising
83  // from restricting the equation down to the lower level
84  T source = -_growthfachubble * delta[i];
85  if( level > 0 && addsource ){
87  }
88 
89  // The discretized equation of motion L_{ijk...}(phi)
90  return kinetic/(h*h) - source;
91  }
92 
93  // Differential of the L operator: dL_{ijk...}/dphi_{ijk...}
94  T dl_operator(unsigned int level, std::vector<unsigned int>& index_list){
95  unsigned int i = index_list[0];
96 
97  // Gridspacing
98  T h = 1.0/T( MultiGridSolver<NDIM,T>::get_N(level) );
99 
100  // Solution and pde-source grid at current level
102 
103  // Derivative of the kinetic term
104  T dkinetic = 0.0;
105  T boff = bfunc(delta[i]);
106  for(unsigned int j = 0; j < NDIM; j++){
107  unsigned int iminus = index_list[2*j+1];
108  unsigned int iplus = index_list[2*j+2];
109  T bminus = 0.5 * ( bfunc(delta[iminus]) + boff );
110  T bplus = 0.5 * ( bfunc(delta[iplus]) + boff );
111  dkinetic += -(bplus + bminus);
112  }
113 
114  // The derivative of the source is zero as there is no field dependence
115  T dsource = 0.0;
116 
117  // Check for error
118  T dl = dkinetic/(h*h) - dsource;
119  if(fabs(dl) < 1e-5){
120  std::cout << "Error: dl = " << dl << " delta_rho = " << delta[i] << " b = " << boff << std::endl;
121  exit(1);
122  }
123 
124  return dl;
125  }
126 };
127 
128 #endif
T bfunc(T delta_rho)
T * get_y(size_t level=0)
T * get_external_field(size_t level, size_t field)
size_t get_N(size_t level=0) const
void set_rhomax(double rhomax)
std::ostream & cout()
void set_growthfachubble(double growthfachubble)
DType growthfachubble
Definition: continuity.cpp:31
static T max(const std::vector< T > &data)
Definition: core_app.cpp:61
ContinuitySolver(MultiGrid< NDIM, T > &source, bool verbose=true)
ContinuitySolver(unsigned int N, unsigned int Nmin, bool verbose=true)
T dl_operator(unsigned int level, std::vector< unsigned int > &index_list)
T min(const std::vector< T > &data)
Definition: chameleon.cpp:140
T get_multigrid_source(size_t level, size_t i) const
void add_external_grid(MultiGrid< NDIM, T > *field)
ContinuitySolver(unsigned int N, bool verbose=true)
T l_operator(unsigned int level, std::vector< unsigned int > &index_list, bool addsource)
void set_rhomin(double rhomin)