FTaylorKernel.hpp 34.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// ===================================================================================
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.  
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info". 
// "http://www.gnu.org/licenses".
// ===================================================================================
#ifndef FTAYLORKERNEL_HPP
#define FTAYLORKERNEL_HPP

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
19
#include "../../Components/FAbstractKernels.hpp"
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
20
#include "../../Utils/FMemUtils.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
21
#include "../../Utils/FLog.hpp"
22
#include "../../Utils/FSmartPointer.hpp"
23
#include "../P2P/FP2PR.hpp"
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
24

25 26 27 28 29 30 31 32
/**
 * @author Cyrille Piacibello 
 * @class FTaylorKernel 
 * 
 * @brief This kernel is an implementation of the different operators
 * needed to compute the Fast Multipole Method using Taylor Expansion
 * for the Far fields interaction.
 */
33 34


35
//TODO spécifier les arguments.
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
36 37
template< class CellClass, class ContainerClass, int P, int order>
class FTaylorKernel : public FAbstractKernels<CellClass,ContainerClass> {
38 39 40
  
private:
  //Size of the multipole and local vectors
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
41
  static const int SizeVector = ((P+1)*(P+2)*(P+3))*order/6;
42

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
43
  
44 45 46 47 48 49 50 51
  ////////////////////////////////////////////////////
  // Object Attributes
  ////////////////////////////////////////////////////
  const FReal boxWidth;               //< the box width at leaf level
  const int   treeHeight;             //< The height of the tree
  const FReal widthAtLeafLevel;       //< width of box at leaf level
  const FReal widthAtLeafLevelDiv2;   //< width of box at leaf leve div 2
  const FPoint boxCorner;             //< position of the box corner
52 53 54 55 56

  ////////////////////////////////////////////////////
  // PreComputed values
  ////////////////////////////////////////////////////
  FReal factorials[2*P+1];           //< This contains the factorial until P
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
57
  FReal arrayDX[P+2],arrayDY[P+2],arrayDZ[P+2] ; //< Working arrays
COULAUD Olivier's avatar
d  
COULAUD Olivier committed
58
  static const int  sizeDerivative = (2*P+1)*(P+1)*(2*P+3)/3; 
59 60 61 62
  FReal _PsiVector[sizeDerivative];  
  FSmartPointer< FReal[343][sizeDerivative] > M2LpreComputedDerivatives;  
  FSmartPointer< int[SizeVector] > preComputedIndirections;  

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
63
  FReal _coeffPoly[SizeVector];
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
64 65


66 67 68 69
  ////////////////////////////////////////////////////
  // Private method
  ////////////////////////////////////////////////////

COULAUD Olivier's avatar
COULAUD Olivier committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  ///////////////////////////////////////////////////////
  // Precomputation
  ///////////////////////////////////////////////////////

  /** Compute the factorial from 0 to P
   * Then the data is accessible in factorials array:
   * factorials[n] = n! with n <= P
   */
  void precomputeFactorials(){
    factorials[0] = 1.0;
    FReal fidx = 1.0;
    for(int idx = 1 ; idx <= 2*P ; ++idx, ++fidx){
      factorials[idx] = fidx * factorials[idx-1];
    }
  }
85 86


87 88 89 90 91 92 93 94
  /** Return the position of a leaf from its tree coordinate */
  FPoint getLeafCenter(const FTreeCoordinate coordinate) const {
    return FPoint(
		  FReal(coordinate.getX()) * widthAtLeafLevel + widthAtLeafLevelDiv2 + boxCorner.getX(),
		  FReal(coordinate.getY()) * widthAtLeafLevel + widthAtLeafLevelDiv2 + boxCorner.getY(),
		  FReal(coordinate.getZ()) * widthAtLeafLevel + widthAtLeafLevelDiv2 + boxCorner.getZ());
  }

95 96 97 98 99 100 101 102 103
  /** 
   * @brief Return the position of the center of a cell from its tree
   *  coordinate 
   * @param FTreeCoordinate
   * @param inLevel the current level of Cell
   */
  FPoint getCellCenter(const FTreeCoordinate coordinate, int inLevel)
  {
    //Set the boxes width needed
104
    FReal widthAtCurrentLevel = widthAtLeafLevel*FReal(1 << (treeHeight-(inLevel+1)));   
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    FReal widthAtCurrentLevelDiv2 = widthAtCurrentLevel/FReal(2);

    //Get the coordinate
    int a = coordinate.getX();
    int b = coordinate.getY();
    int c = coordinate.getZ();
    
    //Set the center real coordinates from box corner and widths.
    FReal X = boxCorner.getX() + FReal(a)*widthAtCurrentLevel + widthAtCurrentLevelDiv2;
    FReal Y = boxCorner.getY() + FReal(b)*widthAtCurrentLevel + widthAtCurrentLevelDiv2;
    FReal Z = boxCorner.getZ() + FReal(c)*widthAtCurrentLevel + widthAtCurrentLevelDiv2;
    
    FPoint cCenter = FPoint(X,Y,Z);
    return cCenter;
  }


122
  /** 
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
123
   * @brief Incrementation of powers in Taylor expansion 
124 125
   * Result : ...,[2,0,0],[1,1,0],[1,0,1],[0,2,0]...  3-tuple are sorted 
   * by size then alphabetical order.
126
   */
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
127
  void incPowers(int * const FRestrict a, int *const FRestrict b, int *const FRestrict c)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  {
    int t = (*a)+(*b)+(*c);
    if(t==0)
      {a[0]=1;}
    else{ if(t==a[0])
	{a[0]--;  b[0]++;}
      else{ if(t==c[0])
	  {a[0]=t+1;  c[0]=0;}
	else{ if(b[0]!=0)
	    {b[0]--; c[0]++;}
	  else{ 
	    b[0]=c[0]+1;
	    a[0]--;
	    c[0]=0;
	  }
	}
      }
    }
  }
  
  /**
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
149
   * @brief Give the index of array from the corresponding 3-tuple
150
   * powers. 
151 152 153
   */
  int powerToIdx(const int a,const int b,const int c)
  {
154 155 156 157 158
    int t,res,p = a+b+c;
    res  = p*(p+1)*(p+2)/6;
    t    = p - a;
    res += t*(t+1)/2+c;
    return res;
159 160 161 162 163 164
  }
  
  /* Return the factorial of a number
   */
  FReal fact(const int a){
    if(a<0) {
165
      printf("fact :: Error factorial negative!! a=%d\n",a);
166 167 168 169 170 171 172 173
      return FReal(0);
    }
    FReal result = 1;
    for(int i = 1 ; i <= a ; ++i){
      result *= FReal(i);
    }
    return result;
  }
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
174

175 176 177 178
  /* Return the product of factorial of 3 numbers
   */
  FReal fact3int(const int a,const int b,const int c)
  {
COULAUD Olivier's avatar
COULAUD Olivier committed
179
    return ( factorials[a]*factorials[b]* factorials[c]) ; 
180
  }
181

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
182
  /* Return the combine of a paire of number
183
   * \f[ C^{b}_{a} \f]
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
184 185
   */
  FReal combin(const int& a, const int& b){
186
    if(a<b)  {printf("combin :: Error combin negative!! a=%d b=%d\n",a,b); exit(-1) ;  }
COULAUD Olivier's avatar
COULAUD Olivier committed
187
        return  factorials[a]/  (factorials[b]* factorials[a-b]) ; 
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
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
  /* PreCompute indirection for M2L translations :
   *
   * During the M2L, derivative vector isn't accessed in an adjacent
   * way, so this function aimed at storing all the indirections in
   * the reading of Derivatives vector.
   *
   */
  void preComputeIndirection()
  {
    this->preComputedIndirections = new int[SizeVector][SizeVector];
    
    int al=0,bl=0,cl=0;
    int alpha;
    //Loop over first index
    for(int k=0; k<SizeVector ; ++k)
      {
	//Loop over multipole expansion coefficients : am, bm, cm.
	for( int ord=0, j= 0 ; ord <= P ; ++ord)
	  {
	    for(int am=ord ; am >= 0 ; --am)
	      {
		alpha = ord-am;
		if ( alpha ==1 )  {
		  for( int i=0 ; i <=1  ; (++i, ++j) )
		    {
		      preComputedIndirections[k][j] = powerToIdx(al+am,bl+1-i,cl+i);
		    }
		}
		else {
		  for( int bm=ord-am ; bm >= 0 ; --bm, ++j)
		    {
		      preComputedIndirections[k][j] = powerToIdx(al+am,bl+bm,cl+(ord-am-bm));
		    }
		}
	      }
	  }
	incPowers(&al,&bl,&cl);
      }
  }
  
  /* Precomputation of all the derivatives that will be needed for the
   * computation of M2L translations :
   *
   * Derivatives depends only on the distance between cells, so for
   * each level, and each possible relative positions (343), we
   * compute all the values needed.
   */
  void preComputeDerivative()
  {
    //Allocation of the memory needed
    M2LpreComputedDerivatives = new FReal[treeHeight][343][sizeDerivative];
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
241
    //This is the width of a box at each level
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
    FReal boxWidthAtLevel = widthAtLeafLevel;
    // from leaf level to the root
    for(int idxLevel = treeHeight-1 ; idxLevel > 0 ; --idxLevel){
      // we compute all possibilities
      for(int idxX = -3 ; idxX <= 3 ; ++idxX ){
	for(int idxY = -3 ; idxY <= 3 ; ++idxY ){
	  for(int idxZ = -3 ; idxZ <= 3 ; ++idxZ ){
	    // if this is not a neighbour
	    if( idxX < -1 || 1 < idxX || idxY < -1 || 1 < idxY || idxZ < -1 || 1 < idxZ ){
	      //Compute the relative position
	      const FPoint relativePosition( -FReal(idxX)*boxWidthAtLevel,
					     -FReal(idxY)*boxWidthAtLevel,
					     -FReal(idxZ)*boxWidthAtLevel);
	      // this is the position in the index system from 0 to 343
	      const int position = ((( (idxX+3) * 7) + (idxY+3))) * 7 + idxZ + 3;
	      initDerivative(relativePosition.getX(),relativePosition.getY(),relativePosition.getZ(),M2LpreComputedDerivatives[idxLevel][position]);
	      computeFullDerivative(relativePosition.getX(),relativePosition.getY(),relativePosition.getZ(),M2LpreComputedDerivatives[idxLevel][position]);
	    }
	  }
	}
      }
      boxWidthAtLevel *= FReal(2);
    }
  }
  
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
267 268 269
  /** @brief Init the derivative array for using of following formula
   * from "A cartesian tree-code for screened coulomb interactions"
   *
270 271
   *  @todo METTRE les fonctions pour intialiser la recurrence. \f$x_i\f$ ?? \f$x_i\f$ ?? 
   *  @todo LA formule ci-dessous n'utilise pas k! 
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
272
   */
273

274 275 276 277 278
  void initDerivative(const FReal & dx ,const FReal & dy ,const FReal & dz  ,   FReal * tab)
  {
    FReal R2 = dx*dx+dy*dy+dz*dz;
    tab[0]=FReal(1)/FMath::Sqrt(R2);   
    FReal R3 = tab[0]/(R2);
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
279
    tab[1]= -dx*R3;                 //Derivative in (1,0,0)
280 281 282 283 284 285 286 287 288 289
    tab[2]= -dy*R3;                 //Derivative in (0,1,0)
    tab[3]= -dz*R3;                 //Derivative in (0,0,1)
    FReal R5 = R3/R2;
    tab[4] = FReal(3)*dx*dx*R5-R3;  //Derivative in (2,0,0)
    tab[5] = FReal(3)*dx*dy*R5;     //Derivative in (1,1,0)
    tab[6] = FReal(3)*dx*dz*R5;     //Derivative in (1,0,1)
    tab[7] = FReal(3)*dy*dy*R5-R3;  //Derivative in (0,2,0)
    tab[8] = FReal(3)*dy*dz*R5;     //Derivative in (0,1,1)
    tab[9] = FReal(3)*dz*dz*R5-R3;  //Derivative in (0,0,2)
  }
290
  
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
291
  /** @brief Compute and store the derivative for a given tuple.
292
   *  Derivative are used for the M2L
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
293
   *
294
   *\f[
295 296 297 298 299 300 301
   * \Psi_{\mathbf{k}}^{c} \left [\left |\mathbf{k}\right |\times \left |
   * \mathbf{x}_i-\mathbf{x}_c\right |^2  \right ]\				
   * = (2\times \left |{\mathbf{k}}\right |-1)
   * \sum_{j=0}^{3}\left [ k_j (x_{i_j}-x_{c_j})
   * \Psi_{\mathbf{k}-e_j,i}^{c}\right ]\ 
   * -(\left |\mathbf{k}\right |-1)   \sum_{j=0}^{3}\left
   * [ k_j(k_j-1) \Psi_{\mathbf{k}-2 e_j,i}^{c} \right]
302
   * \f]
303
   *  where    \f$ \mathbf{k} = (k_1,k_2,k_3) \f$ 
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
304
   */
305 306
  void computeFullDerivative( FReal  dx,  FReal  dy,  FReal  dz, // Distance from distant center to local center
			      FReal * yetComputed)
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
307
  {
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
    initDerivative(dx,dy,dz,yetComputed);
    FReal dist2 =  dx*dx+dy*dy+dz*dz;
    int idxTarget;                      //Index of current yetComputed entry
    int idxSrc1, idxSrc2, idxSrc3,      //Indexes of needed yetComputed entries
      idxSrc4, idxSrc5, idxSrc6;        
    int a=0,b=0,c=0;                    //Powers of expansions
    
    for(c=3 ; c<=2*P ; ++c){
      //Computation of derivatives Psi_{0,0,c}
      // |x-y|^2 * Psi_{0,0,c} + (2*c-1) * dz *Psi_{0,0,c-1} + (c-1)^2 * Psi_{0,0,c-2} = 0
      idxTarget = powerToIdx(0,0,c);
      idxSrc1 = powerToIdx(0,0,c-1);
      idxSrc2 = powerToIdx(0,0,c-2);
      yetComputed[idxTarget] = -(FReal(2*c-1)*dz*yetComputed[idxSrc1] + FReal((c-1)*(c-1))*yetComputed[idxSrc2])/dist2;
    }
    b=1;
    for(c=2 ; c<=2*P-1 ; ++c){
      //Computation of derivatives Psi_{0,1,c}
      // |x-y|^2 * Psi_{0,1,c} + (2*c) * dz *Psi_{0,1,c-1} + c*(c-1) * Psi_{0,1,c-2} + dy*Psi_{0,0,c} = 0
      idxTarget = powerToIdx(0,1,c);
      idxSrc1 = powerToIdx(0,1,c-1);
      idxSrc2 = powerToIdx(0,1,c-2);
      idxSrc3 = powerToIdx(0,0,c);
      yetComputed[idxTarget] = -(FReal(2*c)*dz*yetComputed[idxSrc1] + FReal(c*(c-1))*yetComputed[idxSrc2]+ dy*yetComputed[idxSrc3])/dist2;
    }
    b=2;
    for(c=1 ; c<= 2*P-b ; ++c){
      //Computation of derivatives Psi_{0,2,c}
      //|x-y|^2 * Psi_{0,2,c} + (2*c) * dz *Psi_{0,2,c-1} + (c*(c-1)) * Psi_{0,2,c-2} + 3*dy * Psi_{0,1,c} + Psi_{0,0,c}  = 0
      idxTarget = powerToIdx(0,2,c);
      idxSrc1 = powerToIdx(0,2,c-1);
      idxSrc2 = powerToIdx(0,2,c-2);
      idxSrc3 = powerToIdx(0,1,c);
      idxSrc4 = powerToIdx(0,0,c);
      yetComputed[idxTarget] = -(FReal(2*c)*dz*yetComputed[idxSrc1] + FReal(c*(c-1))*yetComputed[idxSrc2]
				 + FReal(3)*dy*yetComputed[idxSrc3] + yetComputed[idxSrc4])/dist2;
    }
    for(b=3 ; b<= 2*P ; ++b){
      //Computation of derivatives Psi_{0,b,0}
      // |x-y|^2 * Psi_{0,b,0} + (2*b-1) * dy *Psi_{0,b-1,0} + (b-1)^2 * Psi_{0,b-2,c} = 0
      idxTarget = powerToIdx(0,b,0);
      idxSrc1 = powerToIdx(0,b-1,0);
      idxSrc2 = powerToIdx(0,b-2,0);
      yetComputed[idxTarget] = -(FReal(2*b-1)*dy*yetComputed[idxSrc1] + FReal((b-1)*(b-1))*yetComputed[idxSrc2])/dist2;
      for(c=1 ; c<= 2*P-b ; ++c) {
	//Computation of derivatives Psi_{0,b,c}
	//|x-y|^2*Psi_{0,b,c} + (2*c)*dz*Psi_{0,b,c-1} + (c*(c-1))*Psi_{0,b,c-2} + (2*b-1)*dy*Psi_{0,b-1,c} + (b-1)^2 * Psi_{0,b-2,c}  = 0
	idxTarget = powerToIdx(0,b,c);
	idxSrc1 = powerToIdx(0,b,c-1);
	idxSrc2 = powerToIdx(0,b,c-2);
	idxSrc3 = powerToIdx(0,b-1,c);
	idxSrc4 = powerToIdx(0,b-2,c);
	yetComputed[idxTarget] = -(FReal(2*c)*dz*yetComputed[idxSrc1] + FReal(c*(c-1))*yetComputed[idxSrc2]
				   + FReal(2*b-1)*dy*yetComputed[idxSrc3] + FReal((b-1)*(b-1))*yetComputed[idxSrc4])/dist2;
      }
    }
    a=1;
    b=0;
    for(c=2 ; c<= 2*P-1 ; ++c){
      //Computation of derivatives Psi_{1,0,c}
      //|x-y|^2 * Psi_{1,0,c} + (2*c)*dz*Psi_{1,0,c-1} + c*(c-1)*Psi_{1,0,c-2} + dx*Psi_{0,0,c}
      idxTarget = powerToIdx(1,0,c);
      idxSrc1 = powerToIdx(1,0,c-1);
      idxSrc2 = powerToIdx(1,0,c-2);
      idxSrc3 = powerToIdx(0,0,c);
      yetComputed[idxTarget] = -(FReal(2*c)*dz*yetComputed[idxSrc1] + FReal(c*(c-1))*yetComputed[idxSrc2] + dx*yetComputed[idxSrc3])/dist2;
    }
    b=1;
    //Computation of derivatives Psi_{1,1,1}
    //|x-y|^2 * Psi_{1,1,1} + 2*dz*Psi_{1,1,0} + 2*dy*Psi_{1,0,1} + dx*Psi_{0,1,1}
    idxTarget = powerToIdx(1,1,1);
    idxSrc1 = powerToIdx(1,1,0);
    idxSrc2 = powerToIdx(1,0,1);
    idxSrc3 = powerToIdx(0,1,1);
    yetComputed[idxTarget] = -(FReal(2)*dz*yetComputed[idxSrc1] + FReal(2)*dy*yetComputed[idxSrc2] + dx*yetComputed[idxSrc3])/dist2;
    for(c=2 ; c<= 2*P-2 ; ++c){
      //Computation of derivatives Psi_{1,1,c}
      //|x-y|^2 * Psi_{1,1,c} + (2*c)*dz*Psi_{1,1,c-1} + c*(c-1)*Psi_{1,1,c-2} + 2*dy*Psi_{1,0,c} + dx*Psi_{0,1,c}
      idxTarget = powerToIdx(1,1,c);
      idxSrc1 = powerToIdx(1,1,c-1);
      idxSrc2 = powerToIdx(1,1,c-2);
      idxSrc3 = powerToIdx(1,0,c);
      idxSrc4 = powerToIdx(0,1,c);
      yetComputed[idxTarget] = -(FReal(2*c)*dz*yetComputed[idxSrc1] + FReal(c*(c-1))*yetComputed[idxSrc2] 
				 + FReal(2)*dy*yetComputed[idxSrc3]+ dx*yetComputed[idxSrc4])/dist2;
    }
    for(b=2 ; b<= 2*P-a ; ++b){
      for(c=0 ; c<= 2*P-b-1 ; ++c){
	//Computation of derivatives Psi_{1,b,c}
	//|x-y|^2 * Psi_{1,b,c} + (2*b)*dy*Psi_{1,b-1,c} + b*(b-1)*Psi_{1,b-2,c} + (2*c)*dz*Psi_{1,b,c-1} + c*(c-1)*Psi_{1,b,c-2} + dx*Psi_{0,b,c}
	idxTarget = powerToIdx(1,b,c);
	idxSrc1 = powerToIdx(1,b-1,c);
	idxSrc2 = powerToIdx(1,b-2,c);
	idxSrc3 = powerToIdx(1,b,c-1);
	idxSrc4 = powerToIdx(1,b,c-2);
	idxSrc5 = powerToIdx(0,b,c);
	yetComputed[idxTarget] = -(FReal(2*b)*dy*yetComputed[idxSrc1] + FReal(b*(b-1))*yetComputed[idxSrc2] 
				   + FReal(2*c)*dz*yetComputed[idxSrc3]+ FReal(c*(c-1))*yetComputed[idxSrc4]
				   + dx*yetComputed[idxSrc5])/dist2;
      }
    }
    for(a=2 ; a<=2*P ; ++a){
      //Computation of derivatives Psi_{a,0,0}
      // |x-y|^2 * Psi_{a,0,0} + (2*a-1) * dx *Psi_{a-1,0,0} + (a-1)^2 * Psi_{a-2,0,0} = 0
      idxTarget = powerToIdx(a,0,0);
      idxSrc1 = powerToIdx(a-1,0,0);
      idxSrc2 = powerToIdx(a-2,0,0);
      yetComputed[idxTarget] = -(FReal(2*a-1)*dx*yetComputed[idxSrc1] + FReal((a-1)*(a-1))*yetComputed[idxSrc2])/dist2;
      if(a <= 2*P-1){
	//Computation of derivatives Psi_{a,0,1}
	// |x-y|^2 * Psi_{a,0,1} + 2*dz*Psi_{a,0,0} + (2*a-1)*dx*Psi_{a-1,0,1} + (a-1)^2*Psi_{a-2,0,1} = 0
	idxSrc1 = idxTarget;
	idxTarget = powerToIdx(a,0,1);
	idxSrc2 = powerToIdx(a-1,0,1);
	idxSrc3 = powerToIdx(a-2,0,1);
	yetComputed[idxTarget] = -(FReal(2)*dz*yetComputed[idxSrc1] + FReal(2*a-1)*dx*yetComputed[idxSrc2] + FReal((a-1)*(a-1))*yetComputed[idxSrc3])/dist2;
	//Computation of derivatives Psi_{a,1,0}
	// |x-y|^2 * Psi_{a,1,0} + 2*dy*Psi_{a,0,0} + (2*a-1)*dx*Psi_{a-1,1,0} + (a-1)^2*Psi_{a-2,1,0} = 0
	idxTarget = powerToIdx(a,1,0);
	idxSrc2 = powerToIdx(a-1,1,0);
	idxSrc3 = powerToIdx(a-2,1,0);
	yetComputed[idxTarget] = -(FReal(2)*dy*yetComputed[idxSrc1] + FReal(2*a-1)*dx*yetComputed[idxSrc2] + FReal((a-1)*(a-1))*yetComputed[idxSrc3])/dist2;
	if(a <= 2*P-2){
	  b=0;
	  for(c=2 ; c <= 2*P-a ; ++c){
	    //Computation of derivatives Psi_{a,0,c}
	    // |x-y|^2 * Psi_{a,0,c} + 2*c*dz*Psi_{a,0,c-1} + c*(c-1)*Psi_{a,0,c-2} + (2*a-1)*dx*Psi_{a-1,0,c} + (a-1)^2*Psi_{a-2,0,c} = 0
	    idxTarget = powerToIdx(a,0,c);
	    idxSrc1 = powerToIdx(a,0,c-1);
	    idxSrc2 = powerToIdx(a,0,c-2);
	    idxSrc3 = powerToIdx(a-1,0,c);
	    idxSrc4 = powerToIdx(a-2,0,c);
	    yetComputed[idxTarget] = -(FReal(2*c)*dz*yetComputed[idxSrc1] + FReal(c*(c-1))*yetComputed[idxSrc2] 
				       + FReal(2*a-1)*dx*yetComputed[idxSrc3] + FReal((a-1)*(a-1))*yetComputed[idxSrc4])/dist2;
	  }
	  b=1;
	  for(c=1 ; c <= 2*P-a-1 ; ++c){
	    //Computation of derivatives Psi_{a,1,c}
	    // |x-y|^2 * Psi_{a,1,c} + 2*c*dz*Psi_{a,1,c-1} + c*(c-1)*Psi_{a,1,c-2} + 2*a*dx*Psi_{a-1,1,c} + a*(a-1)*Psi_{a-2,1,c} + dy*Psi_{a,0,c}= 0
	    idxTarget = powerToIdx(a,1,c);
	    idxSrc1 = powerToIdx(a,1,c-1);
	    idxSrc2 = powerToIdx(a,1,c-2);
	    idxSrc3 = powerToIdx(a-1,1,c);
	    idxSrc4 = powerToIdx(a-2,1,c);
	    idxSrc5 = powerToIdx(a,0,c);
	    yetComputed[idxTarget] = -(FReal(2*c)*dz*yetComputed[idxSrc1] + FReal(c*(c-1))*yetComputed[idxSrc2] 
				       + FReal(2*a)*dx*yetComputed[idxSrc3] + FReal(a*(a-1))*yetComputed[idxSrc4]
				       + dy*yetComputed[idxSrc5])/dist2;
	  }
	  for(b=2 ; b <= 2*P-a ; ++b){
	    //Computation of derivatives Psi_{a,b,0}
	    // |x-y|^2 * Psi_{a,b,0} + 2*b*dy*Psi_{a,b-1,0} + b*(b-1)*Psi_{a,b-2,0} + (2*a-1)*dx*Psi_{a-1,b,0} + (a-1)^2*Psi_{a-2,b,0} = 0
	    idxTarget = powerToIdx(a,b,0);
	    idxSrc1 = powerToIdx(a,b-1,0);
	    idxSrc2 = powerToIdx(a,b-2,0);
	    idxSrc3 = powerToIdx(a-1,b,0);
	    idxSrc4 = powerToIdx(a-2,b,0);
	    yetComputed[idxTarget] = -(FReal(2*b)*dy*yetComputed[idxSrc1] + FReal(b*(b-1))*yetComputed[idxSrc2]
				       + FReal(2*a-1)*dx*yetComputed[idxSrc3] + FReal((a-1)*(a-1))*yetComputed[idxSrc4])/dist2;
	    if(a+b < 2*P){
	      //Computation of derivatives Psi_{a,b,1}
	      // |x-y|^2 * Psi_{a,b,1} + 2*b*dy*Psi_{a,b-1,1} + b*(b-1)*Psi_{a,b-2,1} + 2*a*dx*Psi_{a-1,b,1} + a*(a-1)*Psi_{a-2,b,1} + dz*Psi_{a,b,0}= 0
	      idxTarget = powerToIdx(a,b,1);
	      idxSrc1 = powerToIdx(a,b-1,1);
	      idxSrc2 = powerToIdx(a,b-2,1);
	      idxSrc3 = powerToIdx(a-1,b,1);
	      idxSrc4 = powerToIdx(a-2,b,1);
	      idxSrc5 = powerToIdx(a,b,0);
	      yetComputed[idxTarget] = -(FReal(2*b)*dy*yetComputed[idxSrc1] + FReal(b*(b-1))*yetComputed[idxSrc2]
					 + FReal(2*a)*dx*yetComputed[idxSrc3] + FReal(a*(a-1))*yetComputed[idxSrc4]
					 + dz*yetComputed[idxSrc5])/dist2;
479
	    }
480 481
	    for(c=2 ; c <= 2*P-b-a ; ++c){
	      //Computation of derivatives Psi_{a,b,c} with a >= 2
482 483
	      // |x-y|^2*Psi_{a,b,c} + (2*a-1)*dx*Psi_{a-1,b,c} + a*(a-2)*Psi_{a-2,b,c} + 2*b*dy*Psi_{a,b-1,c} + b*(b-1)*Psi_{a,b-2,c} 
	      // + 2*c*dz*Psi_{a,b,c-1}} = 0
484 485 486 487 488 489 490 491 492 493
	      idxTarget = powerToIdx(a,b,c);
	      idxSrc1 = powerToIdx(a-1,b,c);
	      idxSrc2 = powerToIdx(a,b-1,c);
	      idxSrc3 = powerToIdx(a,b,c-1);
	      idxSrc4 = powerToIdx(a-2,b,c);
	      idxSrc5 = powerToIdx(a,b-2,c);
	      idxSrc6 = powerToIdx(a,b,c-2);
	      yetComputed[idxTarget] = -(FReal(2*a-1)*dx*yetComputed[idxSrc1] + FReal((a-1)*(a-1))*yetComputed[idxSrc4]
					 + FReal(2*b)*dy*yetComputed[idxSrc2] + FReal(b*(b-1))*yetComputed[idxSrc5]
					 + FReal(2*c)*dz*yetComputed[idxSrc3] + FReal(c*(c-1))*yetComputed[idxSrc6])/dist2;
494
	    }
495
	  }
496
	}
497 498
      }
    }
499
  }
500 501


PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
502 503 504 505
  /////////////////////////////////
  ///////// Public Methods ////////
  /////////////////////////////////

506 507 508 509 510 511 512 513 514 515
public:
  
  /*Constructor, need system information*/
  FTaylorKernel(const int inTreeHeight, const FReal inBoxWidth, const FPoint& inBoxCenter) :
    boxWidth(inBoxWidth),
    treeHeight(inTreeHeight),
    widthAtLeafLevel(inBoxWidth/FReal(1 << (inTreeHeight-1))),
    widthAtLeafLevelDiv2(widthAtLeafLevel/2),
    boxCorner(inBoxCenter.getX()-(inBoxWidth/2),inBoxCenter.getY()-(inBoxWidth/2),inBoxCenter.getZ()-(inBoxWidth/2))
  {
516
    FReal facto;
COULAUD Olivier's avatar
COULAUD Olivier committed
517
    this->precomputeFactorials() ;
518 519 520 521 522
    //    int a = 0, b = 0, c = 0;
    for(int i=0 ,  a = 0, b = 0, c = 0; i<SizeVector ; ++i)
      {
	facto = static_cast<FReal>(fact3int(a,b,c));
	_coeffPoly[i] = FReal(1.0)/(facto);
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
523
	this->incPowers(&a,&b,&c); //inc powers of expansion
524 525 526
      }
    this->preComputeDerivative();
    this->preComputeIndirection();
527 528 529 530 531
  }
  
  /* Default destructor
   */
  virtual ~FTaylorKernel(){
532
    
533 534 535 536 537
  }

  /**P2M 
   * @brief Fill the Multipole with the field created by the cell
   * particles.
538
   *  
539 540
   * Formula :
   * \f[
541
   *   M_{k} = \frac{|k|!}{k! k!} \sum_{j=0}^{N}{ q_j   (x_c-x_j)^{k}}
542
   * \f]
543
   * where \f$x_c\f$ is the centre of the cell and \f$x_j\f$ the \f$j^{th}\f$ particles and \f$q_j\f$ its charge and  \f$N\f$ the particle number.
544 545 546
   */
  void P2M(CellClass* const pole, 
	   const ContainerClass* const particles)
547
  {
548
    //Copying cell center position once and for all
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
549
    const FPoint& cellCenter = getLeafCenter(pole->getCoordinate());
550 551
    
    FReal * FRestrict multipole = pole->getMultipole();
552
    FMemUtils::memset(multipole,0,SizeVector*sizeof(FReal(0.0)));
553 554 555 556
    
    FReal multipole2[SizeVector] ;
    
    int nbPart = particles->getNbParticles(), i;
COULAUD Olivier's avatar
COULAUD Olivier committed
557 558 559 560
    const FReal* const * positions = particles->getPositions();
    const FReal* posX = positions[0];
    const FReal* posY = positions[1];
    const FReal* posZ = positions[2];
561
    
COULAUD Olivier's avatar
COULAUD Olivier committed
562
    const FReal* phyValue = particles->getPhysicalValues();
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
563
  
COULAUD Olivier's avatar
COULAUD Olivier committed
564
    // Iterating over Particles
565
    FReal xc = cellCenter.getX(), yc = cellCenter.getY(), zc = cellCenter.getZ() ;
566
    FReal dx[3] ; 
567
    for(int idPart=0 ; idPart<nbPart ; ++idPart){
568
      
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
      dx[0]         = xc - posX[idPart] ;
      dx[1]         = yc - posY[idPart] ;   
      dx[2]         = zc - posZ[idPart] ;
      
     multipole2[0] = phyValue[idPart]  ;
      
     int leading[3] = {0,0,0 } ;
     for (int k=1, t=1, tail=1; k <= P; ++k, tail=t)
       {
	 for (i = 0; i < 3; ++i) // dérouler la boucle en I ??
	   {
	     int head = leading[i];
	     leading[i] = t;
	     for ( int j = head; j < tail; ++j, ++t)
	       { 
		 multipole2[t] = multipole2[j] *dx[i] ;
	       }
	   } // for i
       }// for k
     //
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
589
     // Does a saxpy be usefull here ?
590 591 592 593 594
     for( i=0 ; i < SizeVector ; ++i) 
       {
	 multipole[i] +=  multipole2[i] ;
	 multipole2[i] = 0.0;
       } 
COULAUD Olivier's avatar
COULAUD Olivier committed
595
    }  // end loop on particles
596 597 598 599 600
    // Multiply by the coefficient
    for( i=0 ; i < SizeVector ; ++i) 
      {
	multipole[i] *=_coeffPoly[i] ;
      }
601 602
  }
  
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
603

604 605 606 607 608 609 610 611 612 613 614 615 616 617
  /**
   * @brief Fill the parent multipole with the 8 values of child multipoles
   * 
   *
   */
  void M2M(CellClass* const FRestrict pole, 
	   const CellClass*const FRestrict *const FRestrict child, 
	   const int inLevel)
  {
    //Powers of expansions
    int a=0,b=0,c=0;
    //Indexes of powers
    int idx_a,idx_b,idx_c;
    //Distance from current child to parent
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
618 619 620
    FReal dx = 0.0;
    FReal dy = 0.0;
    FReal dz = 0.0;
621
    //Center point of parent cell
622
    const FPoint& cellCenter = getCellCenter(pole->getCoordinate(),inLevel);
COULAUD Olivier's avatar
d  
COULAUD Olivier committed
623
    FReal * FRestrict mult = pole->getMultipole();
624
        
625 626
    //Iteration over the eight children
    int idxChild;
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
627
    FReal coeff;
628 629 630
    for(idxChild=0 ; idxChild<8 ; ++idxChild)
      {
	if(child[idxChild]){
631
	  const FPoint& childCenter = getCellCenter(child[idxChild]->getCoordinate(),inLevel+1);
COULAUD Olivier's avatar
d  
COULAUD Olivier committed
632
	  const FReal * FRestrict multChild = child[idxChild]->getMultipole();
633
	  
634
	  //Set the distance between centers of cells
COULAUD Olivier's avatar
COULAUD Olivier committed
635 636 637
	  dx = cellCenter.getX() - childCenter.getX();
	  dy = cellCenter.getY() - childCenter.getY();
	  dz = cellCenter.getZ() - childCenter.getZ();
638
	  
COULAUD Olivier's avatar
COULAUD Olivier committed
639 640 641 642 643 644 645 646 647
	  // Precompute the  arrays of dx^i
	  arrayDX[0] = 1.0 ;
	  arrayDY[0] = 1.0 ;
	  arrayDZ[0] = 1.0 ;
	  for (int i = 1 ; i <= P ; ++i) 	{
	    arrayDX[i] = dx * arrayDX[i-1] ;
	    arrayDY[i] = dy * arrayDY[i-1] ; 
	    arrayDZ[i] = dz * arrayDZ[i-1] ;
	  }
648
	  	  
649 650 651
	  a=0;
	  b=0;
	  c=0;
652 653
	  //FReal value ;
	  FReal value;
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
654
	  //Iteration over parent multipole array
655
	  for(int ordMult = 0,idxMult=0 ; ordMult <= P ; ++ordMult)
656
	    {
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
	      for(a=ordMult ; a>=0 ; --a)
		{
		  if((ordMult-a) == 1){
		      for(int i =0 ; i<= 1 ; ++i,++idxMult)
			{
			  value = 0.0;
			  int idMultiChild;
			  b = 1-i;
			  c = i;
			  //Iteration over the powers to find the cell multipole
			  //involved in the computation of the parent multipole
			  for(idx_a=0 ; idx_a <= a ; ++idx_a){
			    for(idx_b=0 ; idx_b <= b ; ++idx_b){
			      for(idx_c=0 ; idx_c <= c ; ++idx_c){
				//Computation
				//Child multipole involved
				idMultiChild = powerToIdx(a-idx_a,b-idx_b,c-idx_c);
				coeff = FReal(1.0)/fact3int(idx_a,idx_b,idx_c);
				value += multChild[idMultiChild]*coeff*arrayDX[idx_a]*arrayDY[idx_b]*arrayDZ[idx_c];
			      }
			    }
			  }
			  mult[idxMult] += value;
			}
		    }
		  else{
		    for(b=ordMult-a ; b>=0 ; --b, ++idxMult)
		      {
			value = 0.0;
			int idMultiChild;
			c = ordMult-a-b;
			//Iteration over the powers to find the cell multipole
			//involved in the computation of the parent multipole
			for(idx_a=0 ; idx_a <= a ; ++idx_a){
			  for(idx_b=0 ; idx_b <= b ; ++idx_b){
			    for(idx_c=0 ; idx_c <= c ; ++idx_c){
			      //Computation
			      //Child multipole involved
			      idMultiChild = powerToIdx(a-idx_a,b-idx_b,c-idx_c);
			      coeff = FReal(1.0)/fact3int(idx_a,idx_b,idx_c);
			      value += multChild[idMultiChild]*coeff*arrayDX[idx_a]*arrayDY[idx_b]*arrayDZ[idx_c];
			    }
			  }
			}
			mult[idxMult] += value;
		      }
703 704 705 706 707 708
		  }
		}
	    }
	}
      }
  }
COULAUD Olivier's avatar
COULAUD Olivier committed
709
 
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
710 711
  /**
   *@brief Convert the multipole expansion into local expansion The
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
712 713 714 715
   * operator do not use symmetries.
   *
   * Formula : \f[ L_{\mathbf{n}}^{c} = \frac{|n|!}{n! n!}
   * \sum_{\mathbf{k}=0}^{p} \left [ M_\mathbf{k}^c \,
716 717 718
   * \Psi_{\mathbf{,n+k}}( \mathbf{x}_c^{target})\right ] \f]
   * and \f[ \Psi_{\mathbf{,i}}^{c}(\mathbf{x}) =
   * \frac{\partial^i}{\partial x^i} \frac{1}{|x-x_c^{src}|} =  \frac{\partial^{i_1}}{\partial x_1^{i_1}} \frac{\partial^{i_2}}{\partial x_2^{i_2}} \frac{\partial^{i_3}}{\partial x_3^{i_3}} \frac{1}{|x-x_c^{src}|}\f] 
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
719 720 721 722
   *
   * Where \f$x_c^{src}\f$ is the centre of the cell where the
   * multiplole are considered,\f$x_c^{target}\f$ is the centre of the
   * current cell. The cell where we compute the local expansion.
723
   *
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
724
   */
725 726 727
  void M2L(CellClass* const FRestrict local,             // Target cell
	   const CellClass* distantNeighbors[343],       // Sources to be read     
	   const int /*size*/, const int inLevel)
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
728
  {
729
    //Local expansion to be filled
COULAUD Olivier's avatar
d  
COULAUD Olivier committed
730
    FReal * FRestrict iterLocal = local->getLocal();
731 732 733 734 735 736 737
    //index for distant neighbors
    int idxNeigh;
    FReal tmp1;
    int * ind;
    FReal * psiVector;
    const FReal * multipole;
    //Loop over all the possible neighbors
738
    for(idxNeigh=0 ; idxNeigh<343 ; ++idxNeigh){
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
739
      //Need to test if current neighbor is one of the interaction list
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
740
      if(distantNeighbors[idxNeigh]){
741
	
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
742
	//Get the preComputed values for the derivative
743
	psiVector = M2LpreComputedDerivatives[inLevel][idxNeigh];
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
744
	//Multipole to be read
745
	multipole = distantNeighbors[idxNeigh]->getMultipole();
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
746

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
747
	//Iterating over local array : n
748 749 750 751 752 753 754 755 756 757 758 759
	for(int i=0 ; i<SizeVector ; ++i)
	  {
	    //Get the right indexes set
	    ind = this->preComputedIndirections[i];
	    tmp1 = FReal(0);
	    	    
	    //Iterating over multipole array
	    for (int j=0 ; j<SizeVector ; ++j)
	      {
		tmp1 += psiVector[ind[j]]*multipole[j];
	      }
	    iterLocal[i] += tmp1*_coeffPoly[i];
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
760
	  }
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
761 762 763 764
      }
    }
  }

765

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
766 767
  
  /**
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
768
   *@brief Translate the local expansion of parent cell to child cell
769
   *
770 771 772 773 774 775 776 777 778
   * One need to translate the local expansion on a father cell
   * centered in \f$\mathbf{x}_p\f$ to its eight daughters centered in
   * \f$\mathbf{x}_p\f$ .
   *
   * Local expansion for the daughters will be :
   * \f[ \sum_{\mathbf{k}=0}^{|k|<P} L_k * (\mathbf{x}-\mathbf{x}_f)^{\mathbf{k}} \f]
   * with :
   * 
   *\f[ L_{n} = \sum_{\mathbf{k}=\mathbf{n}}^{|k|<P} C_{\mathbf{k}}^{\mathbf{n}} * (\mathbf{x}_f-\mathbf{x}_p)^{\mathbf{k}-\mathbf{n}} \f]
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
779
   */
780

COULAUD Olivier's avatar
COULAUD Olivier committed
781 782
  void L2L(const CellClass* const FRestrict fatherCell, 
	   CellClass* FRestrict * const FRestrict childCell, 
COULAUD Olivier's avatar
COULAUD Olivier committed
783
	   const int inLevel)
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
784
  {
785
    FPoint locCenter = getCellCenter(fatherCell->getCoordinate(),inLevel);
786
    
COULAUD Olivier's avatar
COULAUD Olivier committed
787
    // Get father local expansion
COULAUD Olivier's avatar
d  
COULAUD Olivier committed
788
    const FReal* FRestrict fatherExpansion = fatherCell->getLocal()  ;
789 790 791
    int idxFatherLoc;               //index of Father local expansion to be read.
    FReal dx,  dy,  dz, coeff;      
    int ap, bp, cp, af, bf, cf;     //Indexes of expansion for father and child.
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
792

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
793 794
    // For all children
    for(int idxChild = 0 ; idxChild < 8 ; ++idxChild){
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
795 796

      if(childCell[idxChild]){ // if child exists
797
	
COULAUD Olivier's avatar
d  
COULAUD Olivier committed
798
	FReal* FRestrict childExpansion = childCell[idxChild]->getLocal() ;
799 800
	const FPoint& childCenter = getCellCenter(childCell[idxChild]->getCoordinate(),inLevel+1);
	
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
801
	//Set the distance between centers of cells
COULAUD Olivier's avatar
COULAUD Olivier committed
802
	// Child - father
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
803 804 805
	dx = childCenter.getX()-locCenter.getX();
	dy = childCenter.getY()-locCenter.getY();
	dz = childCenter.getZ()-locCenter.getZ();
COULAUD Olivier's avatar
COULAUD Olivier committed
806 807 808 809 810 811 812 813 814
	// Precompute the  arrays of dx^i
	arrayDX[0] = 1.0 ;
	arrayDY[0] = 1.0 ;
	arrayDZ[0] = 1.0 ;
	for (int i = 1 ; i <= P ; ++i) 	{
	  arrayDX[i] = dx * arrayDX[i-1] ;
	  arrayDY[i] = dy * arrayDY[i-1] ; 
	  arrayDZ[i] = dz * arrayDZ[i-1] ;
	}
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
815
	//iterator over child's local expansion (to be filled)
COULAUD Olivier's avatar
COULAUD Olivier committed
816
	af=0;	bf=0;	cf=0;
817 818
	for(int ordChild=0, idxChEx=0 ; ordChild <= P ; ++ordChild) {
	  for(af=ordChild ; af>=0 ; --af)
819
	    {
820 821 822 823 824 825 826 827 828 829 830 831 832 833
	      if((ordChild-af) == 1){
		for(int i=0 ; i <= 1 ; ++i,++idxChEx)
		  {
		    bf = 1-i;
		    cf = i;
		    //Iterator over parent's local array
		    for(ap=af ; ap<=P ; ++ap){
		      for(bp=bf ; bp<=P ; ++bp){
			for(cp=cf ; ((cp<=P) && (ap+bp+cp) <= P) ; ++cp){
			  idxFatherLoc = powerToIdx(ap,bp,cp);
			  coeff = combin(ap,af) * combin(bp,bf) * combin(cp,cf);
			  childExpansion[idxChEx] += coeff*fatherExpansion[idxFatherLoc]*arrayDX[ap-af]*arrayDY[bp-bf]*arrayDZ[cp-cf] ;
			}
		      }
834
		    }
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
		  }
	      }
	      else{
		for(bf=ordChild-af ; bf>=0 ; --bf, ++idxChEx)
		  {
		    cf = ordChild-af-bf;
		    //Iterator over parent's local array
		    for(ap=af ; ap<=P ; ++ap){
		      for(bp=bf ; bp<=P ; ++bp){
			for(cp=cf ; ((cp<=P) && (ap+bp+cp) <= P) ; ++cp){
			  idxFatherLoc = powerToIdx(ap,bp,cp);
			  coeff = combin(ap,af) * combin(bp,bf) * combin(cp,cf);
			  childExpansion[idxChEx] += coeff*fatherExpansion[idxFatherLoc]*arrayDX[ap-af]*arrayDY[bp-bf]*arrayDZ[cp-cf] ;
			}
		      }
		    }
		  }
	      }
853 854 855
	    }
	}
      }	
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
856
    }
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
857
  }
858 859
  

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
860 861
 
  
862 863
  /**L2P
   *@brief Apply on the particles the force computed from the local expansion to all particles in the cell
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
864
   *
865 866 867
   *  
   * Formula :
   * \f[
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
868
   *   Potential = \sum_{j=0}^{nb_{particles}}{q_j \sum_{k=0}^{P}{ L_k * (x_j-x_c)^{k}}}
869
   * \f]
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
870 871 872
   *
   * where \f$x_c\f$ is the centre of the local cell and \f$x_j\f$ the
   * \f$j^{th}\f$ particles and \f$q_j\f$ its charge.
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
873
   */
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
874
    void L2P(const CellClass* const local, 
875
	     ContainerClass* const particles)
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
876 877
    {
      FPoint locCenter = getLeafCenter(local->getCoordinate());
878
      //Iterator over particles
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
879
      int nbPart = particles->getNbParticles();
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
880
      
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
881
      const FReal * iterLocal = local->getLocal();
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
882 883 884 885 886
      const FReal * const * positions = particles->getPositions();
      const FReal * posX = positions[0];
      const FReal * posY = positions[1];
      const FReal * posZ = positions[2];
      
887 888 889 890 891
      FReal * forceX = particles->getForcesX();
      FReal * forceY = particles->getForcesY();
      FReal * forceZ = particles->getForcesZ();

      FReal * targetsPotentials = particles->getPotentials();
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
892
      
893
      FReal * phyValues = particles->getPhysicalValues();
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
894

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
895
      //Iteration over particles
896
      for(int i=0 ; i<nbPart ; ++i){
897

898 899 900
	FReal dx =  posX[i] - locCenter.getX();
	FReal dy =  posY[i] - locCenter.getY();
	FReal dz =  posZ[i] - locCenter.getZ();
901

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
902 903 904 905 906 907 908 909
	// Precompute an arrays of Array[i] = dx^(i-1)
	arrayDX[0] = 0.0 ; 
	arrayDY[0] = 0.0 ;
	arrayDZ[0] = 0.0 ;
	
	arrayDX[1] = 1.0 ;
	arrayDY[1] = 1.0 ;
	arrayDZ[1] = 1.0 ;
910
	
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
911
	for (int d = 2 ; d <= P+1 ; ++d){ //Array is staggered : Array[i] = dx^(i-1)
912 913 914
	  arrayDX[d] = dx * arrayDX[d-1] ;
	  arrayDY[d] = dy * arrayDY[d-1] ; 
	  arrayDZ[d] = dz * arrayDZ[d-1] ;
COULAUD Olivier's avatar
COULAUD Olivier committed
915
	}
916
	FReal partPhyValue = phyValues[i]; 
917
	FReal  locPot = 0.0, locForceX = 0.0, locForceY = 0.0, locForceZ = 0.0 ;
918
	int a=0,b=0,c=0;
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
	for(int j=0,ord=0 ; ord <= P ; ++ord)
	  {
	    for(a=ord ; a>=0 ; --a)
	      {
		if( (ord-a) == 1){
		    for(int t=0 ; t<=1 ; ++t,++j)
		      {
			b = 1-t;
			c = t;
			FReal locForce     = iterLocal[j];
			// compute the potential
			locPot += iterLocal[j]*arrayDX[a+1]*arrayDY[b+1]*arrayDZ[c+1];
			//Application of forces
			locForceX += FReal(a)*locForce*arrayDX[a]*arrayDY[b+1]*arrayDZ[c+1];
			locForceY += FReal(b)*locForce*arrayDX[a+1]*arrayDY[b]*arrayDZ[c+1];
			locForceZ += FReal(c)*locForce*arrayDX[a+1]*arrayDY[b+1]*arrayDZ[c];
		      }
		  }
		else{
		  for(b=ord-a ; b>=0 ; --b,++j)
		    { 
		      c = ord-a-b;
		      FReal locForce     = iterLocal[j];
		      // compute the potential
		      locPot += iterLocal[j]*arrayDX[a+1]*arrayDY[b+1]*arrayDZ[c+1];
		      //Application of forces
		      locForceX += FReal(a)*locForce*arrayDX[a]*arrayDY[b+1]*arrayDZ[c+1];
		      locForceY += FReal(b)*locForce*arrayDX[a+1]*arrayDY[b]*arrayDZ[c+1];
		      locForceZ += FReal(c)*locForce*arrayDX[a+1]*arrayDY[b+1]*arrayDZ[c];
		    }
		}
	      }
	  }
	targetsPotentials[i]  +=/* partPhyValue*/locPot ;
	forceX[i]             += partPhyValue*locForceX ;
	forceY[i]             += partPhyValue*locForceY ;
	forceZ[i]             += partPhyValue*locForceZ ;
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
956 957
      }
    }
958

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
959 960 961 962 963 964 965 966 967
  /**
   * P2P
   * Particles to particles
   * @param inLeafPosition tree coordinate of the leaf
   * @param targets current boxe targets particles
   * @param sources current boxe sources particles (can be == to targets)
   * @param directNeighborsParticles the particles from direct neighbors (this is an array of list)
   * @param size the number of direct neighbors
   */
968 969 970
  void P2P(const FTreeCoordinate& /*inLeafPosition*/,
	   ContainerClass* const FRestrict targets, const ContainerClass* const FRestrict /*sources*/,
	   ContainerClass* const directNeighborsParticles[27], const int /*size*/)
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
971
  {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
972
    FP2PRT<FReal>::FullMutual<ContainerClass>(targets,directNeighborsParticles,14);
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
973
  }
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
974

975 976 977 978
  /** Use mutual even if it not useful and call particlesMutualInteraction */
  void P2PRemote(const FTreeCoordinate& /*inPosition*/,
         ContainerClass* const FRestrict inTargets, const ContainerClass* const FRestrict /*inSources*/,
         ContainerClass* const inNeighbors[27], const int /*inSize*/){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
979
  FP2PRT<FReal>::FullRemote<ContainerClass>(inTargets,inNeighbors,27);
980 981
  }

982
};
983

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
984
#endif