-
Notifications
You must be signed in to change notification settings - Fork 1
/
Level.hpp
294 lines (245 loc) · 7.86 KB
/
Level.hpp
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#ifndef LEVEL_H
#define LEVEL_H
#include "Box.hpp"
#include "Transfer_Function.hpp"
#include "Transfer_Vector.hpp"
#include "Translation_Function.hpp"
#include "Reterpolator.hpp"
typedef list<Trans_Vector> TVList;
typedef TVList::iterator TVIter;
typedef list<Close_Vector> CVList;
typedef CVList::iterator CVIter;
typedef vector<Transfer_Function*> TFList;
typedef TFList::iterator TFIter;
typedef vector<Translation_Function*> TLList;
typedef TLList::iterator TLIter;
typedef list<Box*> BoxSet;
typedef BoxSet::iterator BoxIter;
template <int DIM>
class Level
{
const static int BRANCH = 1 << DIM; // The branching factor = 2^DIM
double boxSize; // Size of a box this levels contains
BoxSet boxList; // The list of nonempty boxes this level contains
Quadrature* quad; // Quadrature for numerical functions of this level
NFunction scratchSpace; // Scratch space of size quadrature for computation
TVList transList; // The list of transfer vectors
CVList closeList; // The list of close vectors (necessary?)
TFList transferFunc; // The list of transfer functions (necessary?)
TLList translationFunc; // The list of translation functions
Interpolator* upReterp;
Anterpolator* downReterp;
public:
// Constructors
Level( double boxSize_ = 0 )
: boxSize(boxSize_),
quad(NULL), translationFunc(BRANCH),
upReterp(NULL), downReterp(NULL) {}
// Destructor
~Level()
{
// Delete Transfer Functions
for( TFIter tfi = transferFunc.begin();
tfi != transferFunc.end();
++tfi ) {
Transfer_Function* T = *tfi;
delete T;
}
// Delete Translation Functions
for( TLIter tli = translationFunc.begin();
tli != translationFunc.end();
++tli ) {
Translation_Function* TL = *tli;
delete TL;
}
// Delete Quadrature and Reterps
delete quad;
delete upReterp;
delete downReterp;
}
template <typename FUNC>
inline void defineQuadrature( FUNC K, double eps )
{
// Construct the Quadrature
quad = new Quadrature( K, boxSize, eps );
cerr << *quad << endl;
// Construct some scratchspace for efficiency
scratchSpace = NFunction( quad );
// Initialize all the box multipole and locals with the quadrature
for( BoxIter bi = boxbegin(); bi != boxend(); ++bi ) {
Box* b = *bi;
b->makeMultipole( quad );
b->makeLocal( quad );
}
}
inline Quadrature* getQuad()
{
return quad;
}
inline NFunction& getScratch()
{
return scratchSpace;
}
inline void zeroFields()
{
// Zero the box multipole and locals
for( BoxIter bi = boxbegin(); bi != boxend(); ++bi ) {
Box* b = *bi;
b->getMultipole().zero();
b->getLocal().zero();
}
}
inline void defineTransfers()
{
transList.sort();
Trans_Vector LastTV;
for( TVIter tvi = transList.begin(); tvi != transList.end(); ++tvi ) {
Trans_Vector& tv = *tvi;
if( LastTV.x != tv.x || LastTV.y != tv.y || LastTV.z != tv.z ) {
// Compute a new Transfer Function
Vec3 r0( tv.x * boxSize, tv.y * boxSize, tv.z * boxSize );
std::cerr << "tv = " << tv << " r0 = " << r0 << std::endl;
tv.T = new Transfer_Function( quad, r0 );
} else {
// This Transfer Function is the same as the last one
tv.T = LastTV.T;
}
LastTV = tv;
}
}
// Define the translation functions from a lower level
inline void defineTranslations()
{
// Depending on BRANCH, construct the translation vectors
for( int k = 0; k < BRANCH; ++k ) {
// Generate the kth translation vector
// Could do this alot better with the NTree...
Vec3 r;
if( DIM == 1 ) {
if( k == 0 ) r = Vec3( boxSize/4, 0, 0);
if( k == 1 ) r = Vec3(-boxSize/4, 0, 0);
}
if( DIM == 2 ) {
if( k == 0 ) r = Vec3( boxSize/4, boxSize/4, 0);
if( k == 1 ) r = Vec3( boxSize/4,-boxSize/4, 0);
if( k == 2 ) r = Vec3(-boxSize/4, boxSize/4, 0);
if( k == 3 ) r = Vec3(-boxSize/4,-boxSize/4, 0);
}
if( DIM == 3 ) {
if( k == 0 ) r = Vec3( boxSize/4, boxSize/4, boxSize/4);
if( k == 1 ) r = Vec3( boxSize/4, boxSize/4,-boxSize/4);
if( k == 2 ) r = Vec3( boxSize/4,-boxSize/4, boxSize/4);
if( k == 3 ) r = Vec3( boxSize/4,-boxSize/4,-boxSize/4);
if( k == 4 ) r = Vec3(-boxSize/4, boxSize/4, boxSize/4);
if( k == 5 ) r = Vec3(-boxSize/4, boxSize/4,-boxSize/4);
if( k == 6 ) r = Vec3(-boxSize/4,-boxSize/4, boxSize/4);
if( k == 7 ) r = Vec3(-boxSize/4,-boxSize/4,-boxSize/4);
}
translationFunc[k] = new Translation_Function( quad, r );
}
}
// Get the translation function from a child box b
// to its parent box in this level
inline Translation_Function& getTranslationUp( Box* b )
{
return *translationFunc[b->n & (BRANCH-1)];
}
// Get the translation function from a parent box to a child box b
// in this level
inline Translation_Function& getTranslationDown( Box* b )
{
return *translationFunc[(b->n & (BRANCH-1)) ^ (BRANCH-1)];
}
// Define an interpolator from the quadrature of this level
// to the quadrature of level qB
inline void defineInterp( Quadrature* qB )
{
upReterp = new Interpolator( quad, qB );
}
inline Interpolator& getInterp()
{
return *upReterp;
}
// Define an anterpolator from the quadrature of this level
// to the quadrature of level qb
inline void defineAnterp( Quadrature* qb )
{
downReterp = new Anterpolator( quad, qb );
}
inline Anterpolator& getAnterp()
{
return *downReterp;
}
inline double getBoxSize()
{
return boxSize;
}
inline void addBox( Box* b )
{
boxList.push_back(b);
}
inline void addTransfer( Box* b1, Box* b2 )
{
Vec3 r0 = b2->center - b1->center;
r0 /= boxSize;
r0.x = round(r0.x); r0.y = round(r0.y); r0.z = round(r0.z);
//cerr << "Add Transfer " << b1->n << "->" << b2->n << ": " << r0 << endl;
transList.push_back( Trans_Vector(b1, b2, r0) );
//cerr << "Add Transfer " << b2->n << "->" << b1->n << ": " << -r0 << endl;
transList.push_back( Trans_Vector(b2, b1, -r0) );
}
inline void addClose( Box* b1, Box* b2 )
{
//cerr << "Add Close " << b1->n << "<->" << b2->n << endl;
closeList.push_back( Close_Vector(b1, b2) );
}
inline void applyTransferFunctions()
{
for( TVIter tvi = transList.begin(); tvi != transList.end(); ++tvi ) {
//cout << *tvi << endl;
Transfer_Function* T = tvi->T;
Box* b1 = tvi->b1;
Box* b2 = tvi->b2;
b2->getLocal().addProduct( *T, b1->getMultipole() );
}
}
// Computes the direct product
// omega[m] += sum_n K(p[m] - p[n]) * psi[n]
// for all pairs (m,n) that have been determined to be close
template <typename FUNC>
inline void applyClose( FUNC K,
const vector<complex>& psi,
const vector<Vec3>& p,
vector<complex>& omega )
{
for( CVIter cvi = closeList.begin(); cvi != closeList.end(); ++cvi ) {
Box* b1 = cvi->b1;
Box* b2 = cvi->b2;
// For all pairs of points inside box b1 and b2
const Box::pointIter nStart = b1->pointIndex.begin();
const Box::pointIter nEnd = b1->pointIndex.end();
const Box::pointIter mStart = b2->pointIndex.begin();
const Box::pointIter mEnd = b2->pointIndex.end();
for( Box::pointIter ni = nStart; ni != nEnd; ++ni ) {
int n = *ni;
const Vec3& pn = p[n];
for( Box::pointIter mi = mStart; mi != mEnd; ++mi ) {
int m = *mi;
double r = pn.dist(p[m]);
complex E = K(r);
omega[m] += psi[n] * E;
omega[n] += psi[m] * E;
}
}
}
}
inline BoxIter boxbegin()
{
return boxList.begin();
}
inline BoxIter boxend()
{
return boxList.end();
}
};
#endif