-
Notifications
You must be signed in to change notification settings - Fork 0
/
velocity_blocks.h
251 lines (217 loc) · 8.34 KB
/
velocity_blocks.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#ifndef VELOCITY_BLOCKS_H
#define VELOCITY_BLOCKS_H
#include <iostream>
#include "common.h"
namespace vblock {
namespace interpmethod {
enum Method {
NGP, /**< Nearest grid point, i.e., no interpolation.*/
CIC, /**< Cloud in cell, i.e., linear interpolation.*/
TSC
};
}
template<int PADDING,int METHOD> void accum_xyz(Realf* array,const Real* coords,const Realf& value);
template<int PADDING,typename T> void addToFine_x(const T& octant,const T* coarseOffset);
template<int METHOD,typename REAL> Realf interp_xy(const REAL* pos,Realf* data);
template<int METHOD,typename REAL> Realf interp_xz(const REAL* pos,Realf* data);
template<int METHOD,typename REAL> Realf interp_yz(const REAL* pos,Realf* data);
template<int METHOD,typename REAL> Realf interp_xyz(const REAL* pos,Realf* data);
template<typename T> T index(const T& i,const T& j,const T& k);
template<typename T> T nbrIndex(const T& i_off,const T& j_off,const T& k_off);
template<int PADDING,typename T> T padIndex(const T& i,const T& j,const T& k);
// ***** DEFINITIONS OF TEPLATE FUNCTION ***** //
template<int PAD,int METHOD> inline
void accum_xyz(Realf* array,const Real* pos,const Realf& value) {
switch (METHOD) {
case interpmethod::NGP:
{
const int i = static_cast<int>(pos[0]);
const int j = static_cast<int>(pos[1]);
const int k = static_cast<int>(pos[2]);
array[padIndex<PAD>(i+1,j+1,k+1)] += value;
}
break;
case interpmethod::CIC:
{
const int i = static_cast<int>(pos[0] + 0.5);
const int j = static_cast<int>(pos[1] + 0.5);
const int k = static_cast<int>(pos[2] + 0.5);
const Realf W_x = pos[0]-i+0.5;
const Realf W_y = pos[1]-j+0.5;
const Realf W_z = pos[2]-k+0.5;
array[padIndex<PAD>(i ,j ,k )] += value * (1-W_x)*(1-W_y)*(1-W_z);
array[padIndex<PAD>(i+1,j ,k )] += value * ( W_x )*(1-W_y)*(1-W_z);
array[padIndex<PAD>(i ,j+1,k )] += value * (1-W_x)*( W_y )*(1-W_z);
array[padIndex<PAD>(i+1,j+1,k )] += value * ( W_x )*( W_y )*(1-W_z);
array[padIndex<PAD>(i ,j ,k+1)] += value * (1-W_x)*(1-W_y)*( W_z );
array[padIndex<PAD>(i+1,j ,k+1)] += value * ( W_x )*(1-W_y)*( W_z );
array[padIndex<PAD>(i ,j+1,k+1)] += value * (1-W_x)*( W_y )*( W_z );
array[padIndex<PAD>(i+1,j+1,k+1)] += value * ( W_x )*( W_y )*( W_z );
}
break;
case interpmethod::TSC:
{
int indices[3];
indices[0] = static_cast<int>(pos[0]);
indices[1] = static_cast<int>(pos[1]);
indices[2] = static_cast<int>(pos[2]);
Realf sf[9];
sf[0] = 0.5*(indices[0]+1-pos[0])*(indices[0]+1-pos[0]);
sf[2] = 0.5*(pos[0] -indices[0] )*(pos[0] -indices[0] );
sf[1] = 1-sf[0]-sf[2];
sf[3] = 0.5*(indices[1]+1-pos[1])*(indices[1]+1-pos[1]);
sf[5] = 0.5*(pos[1] - indices[1] )*(pos[1] - indices[1] );
sf[4] = 1-sf[3]-sf[5];
sf[6] = 0.5*(indices[2]+1-pos[2])*(indices[2]+1-pos[2]);
sf[8] = 0.5*(pos[2] -indices[2] )*(pos[2] -indices[2] );
sf[7] = 1-sf[6]-sf[8];
for (int k_off=-1; k_off<2; ++k_off) for (int j_off=-1; j_off<2; ++j_off) for (int i_off=-1; i_off<2; ++i_off) {
const Realf shapeFactor = sf[0+i_off+1] * sf[3+j_off+1] * sf[6+k_off+1];
array[padIndex<PAD>(indices[0]+i_off+1,indices[1]+j_off+1,indices[2]+k_off+1)] += value*shapeFactor;
}
}
break;
default:
std::cerr << "Unknown accumulation method in " << __FILE__ << ' ' << __LINE__ << std::endl;
exit(1);
break;
}
}
template<int PAD,typename T> inline
void addToFine_x(const T& j_fine,const T& k_fine,const T* coarseOffset,Realf* fineArray,const Realf* coarseArray) {
/*const T k_coarse = coarseOffset[2] + 2*(octant / 4);
const T j_coarse = coarseOffset[1] + 2*((octant - 4*(octant/4))/2);
const T i_coarse = coarseOffset[0] + 2*(octant % 2);*/
/*
std::cerr << octant << ' ';
std::cerr << j_fine << ' ' << k_fine << " <- ";
std::cerr << i_coarse << ' ' << j_coarse << ' ' << k_coarse;
std::cerr << std::endl;*/
for (T i_fine=0; i_fine<WID; ++i_fine) {
const T coarseIndex = padIndex<PAD>(coarseOffset[0]+i_fine/2,coarseOffset[1]+j_fine/2,coarseOffset[2]+k_fine/2);
fineArray[index(i_fine,j_fine,k_fine)] += coarseArray[coarseIndex];
//fineArray[index(i_fine,j_fine,k_fine)] += coarseArray[padIndex<PAD>(i_coarse+i_fine/2,j_coarse+j_fine/2,k_coarse+k_fine/2)];
}
}
template<typename T> inline
T nbrIndex(const T& i_off,const T& j_off,const T& k_off) {
return (k_off+1)*9 + (j_off+1)*3 + i_off+1;
}
template<typename T> inline
T index(const T& i,const T& j,const T& k) {
return k*WID2 + j*WID + i;
}
template<int METHOD,typename REAL> inline
Realf interp_xy(const REAL* pos,const Realf* data) {
switch (METHOD) {
case interpmethod::NGP:
{
const int i = static_cast<int>(pos[0]);
const int j = static_cast<int>(pos[1]);
const int k = static_cast<int>(pos[2]);
return data[index(i,j,k)];
}
break;
case interpmethod::CIC:
{
const int i = static_cast<int>(pos[0] - 0.5);
const int j = static_cast<int>(pos[1] - 0.5);
const int k = static_cast<int>(pos[2] - 0.5);
Realf W_x = pos[0]-i-0.5;
Realf W_y = pos[1]-j-0.5;
return data[index(i+0,j+0,k)]*(1-W_x)*(1-W_y)
+ data[index(i+1,j+0,k)]*( W_x )*(1-W_y)
+ data[index(i+0,j+1,k)]*(1-W_x)*( W_y )
+ data[index(i+1,j+1,k)]*( W_x )*( W_y );
}
break;
default:
std::cerr << "Unknown interpolation method in " << __FILE__ << ' ' << __LINE__ << std::endl;
exit(1);
break;
}
}
template<int METHOD,typename REAL> inline
Realf interp_xz(const REAL* pos,const Realf* data) {
switch (METHOD) {
case interpmethod::NGP:
{
const int i = static_cast<int>(pos[0]);
const int j = static_cast<int>(pos[1]);
const int k = static_cast<int>(pos[2]);
return data[index(i,j,k)];
}
break;
case interpmethod::CIC:
{
const int i = static_cast<int>(pos[0] - 0.5);
const int j = static_cast<int>(pos[1] - 0.5);
const int k = static_cast<int>(pos[2] - 0.5);
Realf W_x = pos[0]-i-0.5;
Realf W_z = pos[2]-k-0.5;
return data[index(i+0,j,k+0)]*(1-W_x)*(1-W_z)
+ data[index(i+1,j,k+0)]*( W_x )*(1-W_z)
+ data[index(i+0,j,k+1)]*(1-W_x)*( W_z )
+ data[index(i+1,j,k+1)]*( W_x )*( W_z );
}
break;
default:
std::cerr << "Unknown interpolation method in " << __FILE__ << ' ' << __LINE__ << std::endl;
exit(1);
break;
}
}
template<int METHOD,typename REAL> inline
Realf interp_yz(const REAL* pos,const Realf* data) {
switch (METHOD) {
case interpmethod::NGP:
{
const int i = static_cast<int>(pos[0]);
const int j = static_cast<int>(pos[1]);
const int k = static_cast<int>(pos[2]);
return data[index(i,j,k)];
}
break;
case interpmethod::CIC:
{
const int i = static_cast<int>(pos[0] - 0.5);
const int j = static_cast<int>(pos[1] - 0.5);
const int k = static_cast<int>(pos[2] - 0.5);
Realf W_y = pos[1]-j-0.5;
Realf W_z = pos[2]-k-0.5;
return data[index(i,j+0,k+0)]*(1-W_y)*(1-W_z)
+ data[index(i,j+1,k+0)]*( W_y )*(1-W_z)
+ data[index(i,j+0,k+1)]*(1-W_y)*( W_z )
+ data[index(i,j+1,k+1)]*( W_y )*( W_z );
}
break;
default:
std::cerr << "Unknown interpolation method in " << __FILE__ << ' ' << __LINE__ << std::endl;
exit(1);
break;
}
}
template<int METHOD,typename REAL> inline
Realf interp_xyz(const REAL* pos,const Realf* data) {
switch (METHOD) {
case interpmethod::NGP:
{
const int i = static_cast<int>(pos[0]);
const int j = static_cast<int>(pos[1]);
const int k = static_cast<int>(pos[2]);
return data[index(i,j,k)];
}
break;
default:
std::cerr << "Unknown interpolation method in " << __FILE__ << ' ' << __LINE__ << std::endl;
exit(1);
break;
}
}
template<int PADDING,typename T> inline
T padIndex(const T& i,const T& j,const T& k) {
const T W = WID+2*PADDING;
return k*W*W + j*W + i;
}
}; // namespace vblock
#endif