CosmoGRaPH v0.0
RK4Register.h
1 #ifndef COSMO_UTILS_RK4REGISTER_H
2 #define COSMO_UTILS_RK4REGISTER_H
3 
4 #include <string>
5 #include <utility>
6 #include "Array.h"
7 
8 namespace cosmo
9 {
10 
18 template<typename IT, typename RT>
20 {
21  private:
22  std::string name;
23  IT points;
24  RT sim_dt;
25 
26  public:
31 
37  _array_p(), _array_a(), _array_c(), _array_f()
38  {
39  // call init()
40  }
41 
42  RK4Register(IT nx_in, IT ny_in, IT nz_in, RT sim_dt_in):
43  _array_p(nx_in, ny_in, nz_in),
44  _array_a(nx_in, ny_in, nz_in),
45  _array_c(nx_in, ny_in, nz_in),
46  _array_f(nx_in, ny_in, nz_in)
47  {
48  setDt(sim_dt_in);
49 
50  points = nx_in*ny_in*nz_in;
51 
52  // call init()
53  }
54 
64  void init(IT nx_in, IT ny_in, IT nz_in, RT sim_dt_in)
65  {
66  setDt(sim_dt_in);
67 
68  points = nx_in*ny_in*nz_in;
69 
70  _array_p.init(nx_in, ny_in, nz_in);
71  _array_a.init(nx_in, ny_in, nz_in);
72  _array_c.init(nx_in, ny_in, nz_in);
73  _array_f.init(nx_in, ny_in, nz_in);
74  }
75 
79  void setDt(RT sim_dt_in)
80  {
81  sim_dt = sim_dt_in;
82  }
83 
84  ~RK4Register() {}
85 
91  void setName(std::string name_in)
92  {
93  name = name_in;
94  _array_p.setName(name_in + "_p");
95  _array_a.setName(name_in + "_a");
96  _array_c.setName(name_in + "_c");
97  _array_f.setName(name_in + "_f");
98  }
99 
103  void swap_a_c()
104  {
105  std::swap(_array_a.name, _array_c.name);
106  std::swap(_array_a._array, _array_c._array);
107  }
108 
112  void swap_p_f()
113  {
114  std::swap(_array_p.name, _array_f.name);
115  std::swap(_array_p._array, _array_f._array);
116  }
117 
118  void stepInit()
119  {
120  IT i;
121  #pragma omp parallel for default(shared) private(i)
122  for(i=0; i<points; ++i)
123  {
124  _array_a[i] = _array_p[i];
125  _array_f[i] = 0;
126  }
127  }
128 
129  void K1Finalize()
130  {
131  #pragma omp parallel for
132  for(IT i=0; i<points; ++i)
133  {
134  _array_f[i] += sim_dt*_array_c[i]/6.0;
135  _array_c[i] = _array_p[i] + sim_dt*_array_c[i]/2.0;
136  }
137 
138  swap_a_c();
139  }
140 
141  void K2Finalize()
142  {
143  #pragma omp parallel for
144  for(IT i=0; i<points; ++i)
145  {
146  _array_f[i] += sim_dt*_array_c[i]/3.0;
147  _array_c[i] = _array_p[i] + sim_dt*_array_c[i]/2.0;
148  }
149 
150  swap_a_c();
151  }
152 
153  void K3Finalize()
154  {
155  #pragma omp parallel for
156  for(IT i=0; i<points; ++i)
157  {
158  _array_f[i] += sim_dt*_array_c[i]/3.0;
159  _array_c[i] = _array_p[i] + sim_dt*_array_c[i];
160  }
161 
162  swap_a_c();
163  }
164 
165  void K4Finalize()
166  {
167  #pragma omp parallel for
168  for(IT i=0; i<points; ++i)
169  {
170  _array_f[i] += sim_dt*_array_c[i]/6.0 + _array_p[i];
171  _array_c[i] = _array_f[i];
172  _array_p[i] = _array_f[i];
173  }
174 
175  swap_a_c();
176  }
177 
178  RT& _p(const IT & i, const IT & j, const IT & k) { return _array_p(i, j, k); }
179  RT& _a(const IT & i, const IT & j, const IT & k) { return _array_a(i, j, k); }
180  RT& _c(const IT & i, const IT & j, const IT & k) { return _array_c(i, j, k); }
181  RT& _f(const IT & i, const IT & j, const IT & k) { return _array_f(i, j, k); }
182 
183  RT& operator()(const IT & i, const IT & j, const IT & k)
184  {
185  return _array_a(i, j, k);
186  return _array_a[_array_a.idx(i, j, k)];
187  }
188 
189  RT& operator[](IT idx)
190  {
191  return _array_a[idx];
192  }
193 
194 
195 };
196 
197 } // end namespace
198 
199 #endif
Definition: Array.h:14
CosmoArray< IT, RT > _array_a
"_a" register: containes _a_ctive data needed for _c_omputations
Definition: RK4Register.h:28
void swap_a_c()
Swap data in and names of _a and _c registers.
Definition: RK4Register.h:103
RK4 Class for integration.
Definition: RK4Register.h:19
Definition: bardeen.cc:5
void init(IT nx_in, IT ny_in, IT nz_in, RT sim_dt_in)
Initialize class variables; call CosmoArray::init for array members.
Definition: RK4Register.h:64
void setName(std::string name_in)
Set "name" property of instance, CosmoArray member instances.
Definition: RK4Register.h:91
void setDt(RT sim_dt_in)
Set "dt" for RK4Register instance.
Definition: RK4Register.h:79
RK4Register()
Constructor calls CosmoArray constructors; call RK4Register::init to initialize class.
Definition: RK4Register.h:36
CosmoArray< IT, RT > _array_p
"_p" register: contains data from _p_revious step
Definition: RK4Register.h:27
void swap_p_f()
Swap data in and names of _p and _f registers.
Definition: RK4Register.h:112
CosmoArray< IT, RT > _array_c
"_c" register: contains _c_omputed values
Definition: RK4Register.h:29
CosmoArray< IT, RT > _array_f
"_f" register: containes final value of RK4 step
Definition: RK4Register.h:30