// =================================================================================== // 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 #include "../../Components/FAbstractKernels.hpp" #include "../../Utils/FMemUtils.hpp" #include "../../Utils/FDebug.hpp" #include "../P2P/FP2P.hpp" /** * @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. */ //TODO spécifier les arguments. template< class CellClass, class ContainerClass, int P, int order> class FTaylorKernel : public FAbstractKernels { private: //Size of the multipole and local vectors static const int SizeVector = ((P+1)*(P+2)*(P+3))*order/6; //////////////////////////////////////////////////// // 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 FReal factorials[2*P+1]; //< This contains the factorial until P FReal arrayDX[P+2],arrayDY[P+2],arrayDZ[P+2] ; //< Working arrays // For debugging purpose FILE * out; //////////////////////////////////////////////////// // Private method //////////////////////////////////////////////////// /////////////////////////////////////////////////////// // 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]; } } /** 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()); } /** * @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 FReal widthAtCurrentLevel = widthAtLeafLevel*FReal(1 << (treeHeight-(inLevel+1))); 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); //For debug purpose //printf("%f,%f,%f\n",cCenter.getX(),cCenter.getY(),cCenter.getZ()); return cCenter; } /** * @brief Incrementation of powers in Taylor expansion * Result : ...,[2,0,0],[1,1,0],[1,0,1],[0,2,0]... 3-tuple are sorted * by size then alphabetical order. */ void incPowers(int * const FRestrict a, int *const FRestrict b, int *const FRestrict c) { 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; } } } } } /** * @brief Give the index of array from the corresponding 3-tuple * powers. */ int powerToIdx(const int a,const int b,const int c) { int t,p = a+b+c; if(p==0) {return 0;} else { int res = p*(p+1)*(p+2)/6; t=p-a; res+=t*(t+1)/2+c; return res; } } /* Return the factorial of a number */ FReal fact(const int a){ if(a<0) { printf("fact :: Error factorial negative!! a=%d\n",a); return FReal(0); } FReal result = 1; for(int i = 1 ; i <= a ; ++i){ result *= FReal(i); } return result; } /* Return the product of factorial of 3 numbers */ FReal fact3int(const int a,const int b,const int c) { return ( factorials[a]*factorials[b]* factorials[c]) ; } /* Return the combine of a paire of number */ FReal combin(const int& a, const int& b){ if(a-b<0) {printf("combin :: Error combin negative!! a=%d b=%d\n",a,b); exit(-1) ; } return factorials[a]/ (factorials[b]* factorials[a-b]) ; } /** @brief Init the derivative array for using of following formula * from "A cartesian tree-code for screened coulomb interactions" * * @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! */ void initDerivative(const FReal & dx ,const FReal & dy ,const FReal & dz , FReal * tab) { FReal R2 = dx*dx+dy*dy+dz*dz; printf("dx : %f dy : %f dz : %f\n",dx,dy,dz); tab[0]=FReal(1)/FMath::Sqrt(R2); FReal R3 = tab[0]/(R2); tab[1]= -dx*R3; //Derivative in (1,0,0) il doit y avoir un - 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) for(int c=0 ; c<=9 ; ++c){ //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,0,0,0,c,tab[c]); } } /** @brief Compute and store the derivative for a given tuple. * Derivative are used for the M2L * *\f[ * \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] * \f] * where \f$ \mathbf{k} = (k_1,k_2,k_3) \f$ */ void computeFullDerivative( FReal dx, FReal dy, FReal dz, // Distance from distant center to local center FReal * yetComputed) { 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,c,idxTarget,yetComputed[idxTarget]); } //printf(" Psi_{0,0,c} computed \n"); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,c,idxTarget,yetComputed[idxTarget]); } //printf(" Psi_{0,1,c} computed \n"); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,c,idxTarget,yetComputed[idxTarget]); } //printf(" Psi_{0,2,c} computed \n"); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,0,idxTarget,yetComputed[idxTarget]); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,c,idxTarget,yetComputed[idxTarget]); } } //printf(" Psi_{0,b,c} computed \n"); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,c,idxTarget,yetComputed[idxTarget]); } //printf(" Psi_{1,0,c} computed \n"); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,1,1,1,idxTarget,yetComputed[idxTarget]); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,c,idxTarget,yetComputed[idxTarget]); } //printf(" Psi_{1,1,c} computed \n"); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,c,idxTarget,yetComputed[idxTarget]); } } //printf(" Psi_{1,b,c} computed \n"); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,0,0,idxTarget,yetComputed[idxTarget]); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,0,1,idxTarget,yetComputed[idxTarget]); //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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,1,0,idxTarget,yetComputed[idxTarget]); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,0,c,idxTarget,yetComputed[idxTarget]); } 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,1,c,idxTarget,yetComputed[idxTarget]); } 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,0,idxTarget,yetComputed[idxTarget]); 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,1,idxTarget,yetComputed[idxTarget]); } for(c=2 ; c <= 2*P-b-a ; ++c){ //Computation of derivatives Psi_{a,b,c} with a >= 2 // |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= 0 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; //printf("just computed %f, a=%d, b=%d, c=%d target: %d %f\n",dx,a,b,c,idxTarget,yetComputed[idxTarget]); } } } } } //printf(" Psi_{a,b,c} computed \n"); } ///////////////////////////////// ///////// Public Methods //////// ///////////////////////////////// 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)) { this->precomputeFactorials() ; } /* Default destructor */ virtual ~FTaylorKernel(){ fclose(out); } /**P2M * @brief Fill the Multipole with the field created by the cell * particles. * * Formula : * \f[ * M_{k} = \sum_{j=0}^{N}{ q_j * \frac{|k|!}{k! k!} (x_c-x_j)^{k}} * \f] * 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. */ void P2M(CellClass* const pole, const ContainerClass* const particles) { out = fopen("./res_3.data","a+"); //Variables computed for each power of Multipole int a,b,c ; FReal facto, coeff; //Copying cell center position once and for all const FPoint& cellCenter = getLeafCenter(pole->getCoordinate()); printf("P2M :: pole : X: %f, Y: %f, Z:%f \n",cellCenter.getX(),cellCenter.getY(),cellCenter.getZ()); FReal * multipole = pole->getMultipole(); FMemUtils::memset(multipole,0,SizeVector*FReal(0.0)); // // Iterator over Particles // int nbPart = particles->getNbParticles(); const FReal* const * positions = particles->getPositions(); const FReal* posX = positions[0]; const FReal* posY = positions[1]; const FReal* posZ = positions[2]; const FReal* phyValue = particles->getPhysicalValues(); // // Iterating over Particles // FReal xc = cellCenter.getX(), yc = cellCenter.getY(), zc = cellCenter.getZ() ; for(int idPart=0 ; idPart coeff %f M= %f\n ", cellCenter.getX(),cellCenter.getY(),cellCenter.getZ(),a+b+c,a,b,c,factorials[a+b+c]/fact3int(a,b,c),multipole[i]); incPowers(&a,&b,&c); } // } std::cout << std::endl; // for(int l=0 , idx = 0; l<= P ; ++l) // length of i + j + k = l // { // for( c=0 ; c <= l ; ++c) // { // for( b = 0 ; b<= l-c ; ++b) // { // for( a = l-c-b ; a+b+c==l; --a, ++idx) // { // std::cout << "P2M>> "<< idx << " = (i,j,k) = ("<< a << " , " <getCoordinate(),inLevel); printf("M2M :: fatherCell : X: %f, Y: %f, Z:%f\n",cellCenter.getX(),cellCenter.getY(),cellCenter.getZ()); FReal * mult = pole->getMultipole(); FMemUtils::memset(pole,FReal(0.0),SizeVector*FReal(0.0)); //Iteration over the eight children int idxChild; FReal coeff; for(idxChild=0 ; idxChild<8 ; ++idxChild) { if(child[idxChild]){ const FPoint& childCenter = getCellCenter(child[idxChild]->getCoordinate(),inLevel+1); printf("M2M :: child cells : X: %f, Y: %f, Z:%f\n",childCenter.getX(),childCenter.getY(),childCenter.getZ()); //const FReal * const multChild = child[idxChild]->getMultipole(); FReal multChild[SizeVector]; FMemUtils::memset(multChild,0,SizeVector*sizeof(FReal(0.0))); multChild[1]=1; //Set the distance between centers of cells dx = cellCenter.getX() - childCenter.getX(); dy = cellCenter.getY() - childCenter.getY(); dz = cellCenter.getZ() - childCenter.getZ(); printf("M2M :: dx=%f, dy=%f, dz=%f\n",dx,dy,dz); // dz = ((FReal )(2*(1 & idxChild)-1))*boxSize; // dy = ((FReal )(2*((1 << 1) & idxChild)-1))*boxSize; // dx = ((FReal )(2*((1 << 2) & idxChild)-1))*boxSize; // printf("Distances dans le M2M : %f %f %f boxSize : %f \n",dx,dy,dz,boxSize); // 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] ; // printf(" M2M arrayD? ,i : %d, locForce : %f %f %f\n",i-1, arrayDX[i-1], arrayDY[i-1], arrayDZ[i-1] ); } // for(int idxMult = 0 ; idxMult %f\n", mult[idxMult] += Nk*value; incPowers(&a,&b,&c); } //For debugging purposes int x=0,y=0,z=0; for(int idxChk=0 ; idxChkgetCoordinate(),inLevel); if(locCenter.getX() == FReal(-3)){ fprintf(out,"M2l :: pole_target : X: %f, Y: %f, Z:%f\n",locCenter.getX(),locCenter.getY(),locCenter.getZ()); } FReal * iterLocal = local->getLocal(); FMemUtils::memset(iterLocal,0,SizeVector*sizeof(FReal(0.0))); FReal yetComputed[sizeDerivative]; for(idxNeigh=0 ; idxNeigh<343 ; ++idxNeigh){ //Need to test if current neighbor is one of the interaction list if(distantNeighbors[idxNeigh]){ //Derivatives are computed iteratively FMemUtils::memset(yetComputed,0,sizeDerivative*sizeof(FReal(0.0))); // // Compute derivatives on locCenter - curDistCenter // target source FPoint curDistCenter = getCellCenter(distantNeighbors[idxNeigh]->getCoordinate(),inLevel); FReal dx = locCenter.getX()-curDistCenter.getX(); FReal dy = locCenter.getY()-curDistCenter.getY(); FReal dz = locCenter.getZ()-curDistCenter.getZ(); //Computation of all the derivatives needed computeFullDerivative(dx,dy,dz,yetComputed); //Iteration over Multipole / Local int al=0,bl=0,cl=0; // For local array int am,bm,cm; // For distant array // //Iterating over local array : n for(int i=0 ; i< SizeVector ; ++i){ FReal fctl = fact3int(al,bl,cl); FReal coeffL = factorials[al+bl+cl]/(fctl*fctl); // //Iterator over multipole array const FReal * multipole = distantNeighbors[idxNeigh]->getMultipole(); //For debugging purposes //FReal multipole[SizeVector]; //FMemUtils::memset(multipole,0,SizeVector*sizeof(FReal(0.0))); //multipole[3]=FReal(1); FReal tmp = 0.0 ; //Iterating over multipole array : k // Loc(al,bl,cl) = N(al,bl,cl)/((al,bl,cl)!*(al,bl,cl)!) sum_(am,bm,cm) Psi[am+al,bm+bl,cm+cl] * M[am,bm,cm] // am=0; bm=0; cm=0; //printf("al= %d, bl=%d, cl=%d ==> i =%d \n",al,bl,cl,i); for(int j = 0 ; j < SizeVector ; ++j){ //corresponding powers am,bm,cm int idxPsi = powerToIdx(al+am,bl+bm,cl+cm); tmp += yetComputed[idxPsi]*multipole[j]; //printf(" j= %d, am=%d, bm=%d, cm=%d,, aml=%d, bml=%d, cml=%d, psi[%d]=%f\n",j,am,bm,cm,am+al,bm+bl,cm+cl,powerToIdx(al+am,bl+bm,cl+cm),yetComputed[powerToIdx(al+am,bl+bm,cl+cm)]); //updating a,b,c incPowers(&am,&bm,&cm); } iterLocal[i] = tmp*coeffL ; incPowers(&al,&bl,&cl); } // For Debugging .......................................................... int x=0,y=0,z=0; for(int dby=0 ; dby %f\n",curDistCenter.getX(),x,y,z,iterLocal[dby]); incPowers(&x,&y,&z); } x = y = z = 0; for(int dby=0 ; dby derive : %f\n",curDistCenter.getX(),x,y,z,yetComputed[dby]); incPowers(&x,&y,&z); } } } } /** *@brief Translate the local expansion of parent cell to child cell * Sur une cellule, \f$\mathcal{C}_l\f$, du niveau \f$l\f$ de centre \f$\mathbf{x}_p\f$, on a le développement local du potentiel suivant * \f[ * V(x) = \sum_{\mathbf{k}=0}^{P}{O_\mathbf{k}\; (\mathbf{x}-\mathbf{x}_p)^\mathbf{k}}.\f] *Soit \f$\mathbf{x}_f\f$ le centre d'une cellule fille de \f$\mathcal{C}_l\f$, le potentiel s'écrit alors * \f[ V(x) = \sum_{\mathbf{n}=0}^{P}{L_\mathbf{n} (\mathbf{x}-\mathbf{x}_f)^\mathbf{n}} \f] * avec * \f[ L_\mathbf{n} = \sum_{\mathbf{k}=\mathbf{n}}^{\mathbf{p}}{C^\mathbf{n}_\mathbf{k} O_\mathbf{k-n}\;(\mathbf{x}_f-\mathbf{x}_p)^\mathbf{k-n}}.\f] * La formule est implémentée en introduisant un changement d'indice \f$\mathbf{r}=\mathbf{k}-\mathbf{n}\f$. On obtient alors : * \f[ L_\mathbf{n} = \sum_{\mathbf{r}=0}^{\mathbf{p}-\mathbf{n}}{C^\mathbf{n}_\mathbf{r+n} O_\mathbf{r}\;(\mathbf{x}_f-\mathbf{x}_p)^\mathbf{r}}.\f] * */ void L2L(const CellClass* const FRestrict fatherCell, CellClass* FRestrict * const FRestrict childCell, const int inLevel) { FPoint &locCenter = getCellCenter(fatherCell->getCoordinate(),inLevel); // Get father local expansion const FReal* fatherExpansion = fatherCell->getLocal() ; FReal dx, dy, dz, coeff; int ap, bp, cp, af, bf, cf; // // For all children for(int idxChild = 0 ; idxChild < 8 ; ++idxChild){ // if child exists if(childCell[idxChild]){ FReal* childExpansion = childCell[idxChild]->getLocal() ; FPoint & childCenter =getCellCenter(childCell[idxChild]->getCoordinate(),inLevel+1); //Set the distance between centers of cells // Child - father dx = childCenter.getX()-locCenter.getX(); dy = childCenter.getY()-locCenter.getY(); dz = childCenter.getZ()-locCenter.getZ(); // 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] ; } // //iterator over child's local expansion (to be filled) af=0; bf=0; cf=0; for(int k=0 ; kgetCoordinate()); //Iterator over particles int nbPart = particles->getNbParticles(); // //Iteration over Local array // const FReal * iterLocal = local->getLocal(); const FReal * const * positions = particles->getPositions(); const FReal * posX = positions[0]; const FReal * posY = positions[1]; const FReal * posZ = positions[2]; FReal * const forceX = particles->getForcesX(); FReal * const forceY = particles->getForcesY(); FReal * const forceZ = particles->getForcesZ(); // FReal * const targetsPotentials = particles->getPotentials(); printf("L2P : Cell : %f, fx = %f, fy = %f, fz = %f\n\n",locCenter.getX(),forceX[0],forceY[0],forceZ[0]); FReal * const phyValues = particles->getPhysicalValues(); //Iteration over particles for(int i=0 ; i