CosmoGRaPH v0.0
FRW.h
1 #ifndef COSMO_FRW
2 #define COSMO_FRW
3 
4 #define PI_L 3.141592653589793238462643383279502884L
5 #include <vector>
6 
7 namespace cosmo
8 {
9 
11 template<typename RT>
12 class FRW
13 {
14  // metric variables in FRW
15  RT phi = 0.0;
16  RT K = 0.0;
17  RT alpha = 0.0;
18  // densities and corresponding EOS "w"s
19  std::vector< std::pair<RT,RT> > fluids;
20  int num_fluids = 0;
21 
22  // RK variables
23  RT phi_K1 = 0.0, K_K1 = 0.0, alpha_K1 = 0.0,
24  phi_K2 = 0.0, K_K2 = 0.0, alpha_K2 = 0.0,
25  phi_K3 = 0.0, K_K3 = 0.0, alpha_K3 = 0.0,
26  phi_K4 = 0.0, K_K4 = 0.0, alpha_K4 = 0.0;
27  std::vector<RT> fluids_K1 = {0},
28  fluids_K2 = {0},
29  fluids_K3 = {0},
30  fluids_K4 = {0};
31 
32  // variables to return
33  RT phi_get = 0.0, K_get = 0.0, alpha_get = 0.0,
34  rho_get = 0.0, S_get = 0.0;
35 
36 public:
37  FRW(RT phi_in, RT K_in)
38  {
39  phi_get = phi = phi_in;
40  K_get = K = K_in;
41  alpha_get = alpha = 1.0;
42  num_fluids = 0;
43  rho_get = 0.0;
44  S_get = 0.0;
45  }
46 
47  // "set" variables
48  void set_phi(RT phi_in)
49  {
50  phi_get = phi = phi_in;
51  }
52  void set_K(RT K_in)
53  {
54  K_get = K = K_in;
55  }
56  void set_alpha(RT alpha_in)
57  {
58  alpha_get = alpha = alpha_in;
59  }
60  void addFluid(RT rho_in, RT w_in)
61  {
62  fluids.emplace_back(rho_in, w_in);
63  num_fluids += 1;
64  fluids_K1.resize(num_fluids);
65  fluids_K2.resize(num_fluids);
66  fluids_K3.resize(num_fluids);
67  fluids_K4.resize(num_fluids);
68  rho_get = 0.0;
69  S_get = 0.0;
70  for(int n=0; n<num_fluids; ++n)
71  {
72  RT rho = fluids[n].first;
73  rho_get += rho;
74  S_get += 3.0*rho*fluids[n].second;
75  }
76  }
77 
78  // get variables
79  RT get_phi() { return phi_get; }
80  RT get_K() { return K_get; }
81  RT get_alpha() { return alpha_get; }
82  RT get_rho() { return rho_get; }
83  RT get_S() { return S_get; }
84 
85  // RK calculations
86  void P1_step(RT h);
87  void P2_step(RT h);
88  void P3_step(RT h);
89  void RK_total_step(RT h);
90 
91 };
92 
93 template <typename RT>
94 void FRW<RT>::P1_step(RT h)
95 {
96  // Phi evolution
97  phi_K1 = -1.0*K/6.0;
98 
99  // K evolution
100  RT K_source = 0.0;
101  for(int n=0; n<num_fluids; ++n)
102  {
103  std::pair<RT,RT> x = fluids[n];
104  K_source += 4.0*PI_L*x.first*(1.0 + 3.0*x.second);
105  }
106  K_K1 = 1.0/3.0*K*K + K_source;
107 
108  // matter fields
109  for(int n=0; n<num_fluids; ++n)
110  {
111  std::pair<RT,RT> x = fluids[n];
112  // d/dt rho = -3*H*rho*(1+w)
113  RT rho = x.first;
114  fluids_K1[n] = K*rho*(1.0 + x.second);
115  }
116 
117  // BSSNSim will expect these returned at this point
118  phi_get = phi + (h/2.0)*phi_K1;
119  K_get = K + (h/2.0)*K_K1;
120  // matter fields
121  rho_get = 0.0;
122  S_get = 0.0;
123  for(int n=0; n<num_fluids; ++n)
124  {
125  RT rho = fluids[n].first + (h/2.0)*fluids_K1[n];
126  rho_get += rho;
127  S_get += 3.0*rho*fluids[n].second;
128  }
129 }
130 
131 template <typename RT>
132 void FRW<RT>::P2_step(RT h)
133 {
134  // Phi evolution
135  RT K_interm = K + (h/2.0)*K_K1;
136  phi_K2 = -1.0*K_interm/6.0;
137 
138  // K evolution
139  RT K_source = 0.0;
140  for(int n=0; n<num_fluids; ++n)
141  {
142  std::pair<RT,RT> x = fluids[n];
143  RT rho_interm = fluids[n].first + (h/2.0)*fluids_K1[n];
144  K_source += 4.0*PI_L*rho_interm*(1.0 + 3.0*x.second);
145  }
146  K_K2 = 1.0/3.0*K_interm*K_interm + K_source;
147 
148  // matter fields
149  for(int n=0; n<num_fluids; ++n)
150  {
151  std::pair<RT,RT> x = fluids[n];
152  // d/dt rho = -3*H*rho*(1+w)
153  RT rho_interm = fluids[n].first + (h/2.0)*fluids_K1[n];
154  fluids_K2[n] = K_interm*rho_interm*(1.0 + x.second);
155  }
156 
157  // BSSNSim will expect these returned at this point
158  phi_get = phi + (h/2.0)*phi_K2;
159  K_get = K + (h/2.0)*K_K2;
160  // matter fields
161  rho_get = 0.0;
162  S_get = 0.0;
163  for(int n=0; n<num_fluids; ++n)
164  {
165  RT rho = fluids[n].first + (h/2.0)*fluids_K2[n];
166  rho_get += rho;
167  S_get += 3.0*rho*fluids[n].second;
168  }
169 }
170 
171 template <typename RT>
172 void FRW<RT>::P3_step(RT h)
173 {
174  // Phi evolution
175  RT K_interm = K + (h/2.0)*K_K2;
176  phi_K3 = -1.0*K_interm/6.0;
177 
178  // K evolution
179  RT K_source = 0.0;
180  for(int n=0; n<num_fluids; ++n)
181  {
182  std::pair<RT,RT> x = fluids[n];
183  RT rho_interm = fluids[n].first + (h/2.0)*fluids_K2[n];
184  K_source += 4.0*PI_L*rho_interm*(1.0 + 3.0*x.second);
185  }
186  K_K3 = 1.0/3.0*K_interm*K_interm + K_source;
187 
188  // matter fields
189  for(int n=0; n<num_fluids; ++n)
190  {
191  std::pair<RT,RT> x = fluids[n];
192  // d/dt rho = -3*H*rho*(1+w)
193  RT rho_interm = fluids[n].first + (h/2.0)*fluids_K2[n];
194  fluids_K3[n] = K_interm*rho_interm*(1.0 + x.second);
195  }
196 
197  // BSSNSim will expect these returned at this point
198  phi_get = phi + h*phi_K3;
199  K_get = K + h*K_K3;
200  // matter fields
201  rho_get = 0.0;
202  S_get = 0.0;
203  for(int n=0; n<num_fluids; ++n)
204  {
205  RT rho = fluids[n].first + h*fluids_K3[n];
206  rho_get += rho;
207  S_get += 3.0*rho*fluids[n].second;
208  }
209 }
210 
211 template <typename RT>
212 void FRW<RT>::RK_total_step(RT h)
213 {
214  // Phi evolution
215  RT K_interm = K + h*K_K3;
216  phi_K4 = -1.0*K_interm/6.0;
217 
218  // K evolution
219  RT K_source = 0.0;
220  for(int n=0; n<num_fluids; ++n)
221  {
222  std::pair<RT,RT> x = fluids[n];
223  RT rho_interm = fluids[n].first + h*fluids_K3[n];
224  K_source += 4.0*PI_L*rho_interm*(1.0 + 3.0*x.second);
225  }
226  K_K4 = 1.0/3.0*K_interm*K_interm + K_source;
227 
228  // matter fields
229  for(int n=0; n<num_fluids; ++n)
230  {
231  std::pair<RT,RT> x = fluids[n];
232  // d/dt rho = -3*H*rho*(1+w)
233  RT rho_interm = fluids[n].first + h*fluids_K3[n];
234  fluids_K4[n] = K_interm*rho_interm*(1.0 + x.second);
235  }
236 
237  // RK update step
238  phi += h/6.0*(phi_K1 + 2.0*phi_K2 + 2.0*phi_K3 + phi_K4);
239  K += h/6.0*(K_K1 + 2.0*K_K2 + 2.0*K_K3 + K_K4);
240  for(int n=0; n<num_fluids; ++n)
241  fluids[n].first += h/6.0*(fluids_K1[n] + 2.0*fluids_K2[n] + 2.0*fluids_K3[n] + fluids_K4[n]);
242 
243  // BSSNSim will expect these returned at this point
244  phi_get = phi;
245  K_get = K;
246  rho_get = 0.0;
247  S_get = 0.0;
248  for(int n=0; n<num_fluids; ++n)
249  {
250  RT rho = fluids[n].first;
251  rho_get += rho;
252  S_get += 3.0*rho*fluids[n].second;
253  }
254 }
255 
256 } // namespace cosmo
257 
258 #endif
Definition: bardeen.cc:5
Definition: FRW.h:12