1 #ifndef COSMO_UTILS_ARRAY_H 2 #define COSMO_UTILS_ARRAY_H 8 #include "TriCubicInterpolator.h" 13 template<
typename IT,
typename RT>
28 init(n_in, n_in, n_in);
33 init(nx_in, ny_in, nz_in);
42 void setName(std::string name_in)
47 void init(IT nx_in, IT ny_in, IT nz_in)
57 #pragma omp parallel for 58 for(IT i=0; i<pts; ++i)
64 IT _IT_mod(IT n, IT d)
const 76 #pragma omp parallel for reduction(+:res) 77 for(IT i=0; i<pts; ++i)
87 return sum() / (RT)pts;
93 #pragma omp parallel for 94 for(IT i = 0; i<pts; i++)
97 if(_array[i] < min_res)
106 #pragma omp parallel for 107 for(IT i = 0; i<pts; i++)
111 if(_array[i] > max_res)
122 #pragma omp parallel for 123 for(IT i = 0; i<pts; i++)
127 if(fabs(_array[i]) > max_res)
128 max_res = fabs(_array[i]);
139 #pragma omp parallel for reduction(+:L2) 140 for(IT i=0; i<pts; ++i)
142 L2 += _array[i] * _array[i];
149 IT idx(IT i_in, IT j_in, IT k_in)
151 IT i=i_in, j=j_in, k=k_in;
155 if(i_in < 0 || i_in >= nx) i = (i_in%nx + nx)%nx;
156 if(j_in < 0 || j_in >= ny) j = (j_in%ny + ny)%ny;
157 if(k_in < 0 || k_in >= nz) k = (k_in%nz + nz)%nz;
158 return ( i*ny*nz + j*nz + k );
161 RT& operator()(IT i, IT j, IT k)
175 this->pts = other.pts;
176 this->name = other.name;
178 #pragma omp parallel for 179 for(IT i=0; i<pts; ++i)
181 this->_array[i] = other._array[i];
187 RT& operator[](IT idx)
194 RT getInterpolatedValue(RT i_in, RT j_in, RT k_in)
196 IT il = i_in < 0 ? (IT) i_in - 1 : (IT) i_in;
198 IT jl = j_in < 0 ? (IT) j_in - 1 : (IT) j_in;
200 IT kl = k_in < 0 ? (IT) k_in - 1 : (IT) k_in;
203 RT c00 = _array[idx(il, jl, kl)]*(1-id) + _array[idx(il+1, jl, kl)]*id;
204 RT c01 = _array[idx(il, jl, kl+1)]*(1-id) + _array[idx(il+1, jl, kl+1)]*id;
205 RT c10 = _array[idx(il, jl+1, kl)]*(1-id) + _array[idx(il+1, jl+1, kl)]*id;
206 RT c11 = _array[idx(il, jl+1, kl+1)]*(1-id) + _array[idx(il+1, jl+1, kl+1)]*id;
207 RT c0 = c00*(1-jd) + c10*jd;
208 RT c1 = c01*(1-jd) + c11*jd;
210 return c0*(1-kd) + c1*kd;
214 RT CINT(RT u, RT p0, RT p1, RT p2, RT p3)
217 (u*u*(2.0 - u) - u)*p0
218 + (u*u*(3.0*u - 5.0) + 2)*p1
219 + (u*u*(4.0 - 3.0*u) + u)*p2
224 RT getTriCubicInterpolatedValue(RT i_in, RT j_in, RT k_in)
226 IT il = i_in < 0 ? (IT) i_in - 1 : (IT) i_in;
232 _array[idx(il-1, 0, 0)], _array[idx(il, 0, 0)],
233 _array[idx(il+1, 0, 0)], _array[idx(il+2, 0, 0)]);
236 IT jl = j_in < 0 ? (IT) j_in - 1 : (IT) j_in;
238 IT kl = k_in < 0 ? (IT) k_in - 1 : (IT) k_in;
242 RT * F_i_j_kd =
new RT[16];
243 for(IT i=0; i<4; ++i)
244 for(IT j=0; j<4; ++j)
245 F_i_j_kd[i*4+j] = CINT(kd,
246 _array[idx(il+i-1, jl+j-1, kl-1)], _array[idx(il+i-1, jl+j-1, kl+0)],
247 _array[idx(il+i-1, jl+j-1, kl+1)], _array[idx(il+i-1, jl+j-1, kl+2)]);
250 RT * F_i_jd_kd =
new RT[4];
251 for(IT i=0; i<4; ++i)
252 F_i_jd_kd[i] = CINT(jd, F_i_j_kd[i*4+0], F_i_j_kd[i*4+1], F_i_j_kd[i*4+2], F_i_j_kd[i*4+3]);
255 RT Fijk = CINT(
id, F_i_jd_kd[0], F_i_jd_kd[1], F_i_jd_kd[2], F_i_jd_kd[3]);
265 void cosmoArraySwap(T & arr1, T & arr2)
267 std::swap(arr1.nx, arr2.nx);
268 std::swap(arr1.ny, arr2.ny);
269 std::swap(arr1.nz, arr2.nz);
270 std::swap(arr1.pts, arr2.pts);
271 std::swap(arr1.name, arr2.name);
272 std::swap(arr1._array, arr2._array);