FSphericalRotationKernel.hpp 27.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// ===================================================================================
// Logiciel initial: ScalFmm Version 0.5
// Co-auteurs : Olivier Coulaud, Bérenger Bramas.
// Propriétaires : INRIA.
// Copyright © 2011-2012, diffusé sous les termes et conditions d’une licence propriétaire.
// Initial software: ScalFmm Version 0.5
// Co-authors: Olivier Coulaud, Bérenger Bramas.
// Owners: INRIA.
// Copyright © 2011-2012, spread under the terms and conditions of a proprietary license.
// ===================================================================================
#ifndef FSPHERICALROTATIONKERNEL_HPP
#define FSPHERICALROTATIONKERNEL_HPP

#include "FAbstractSphericalKernel.hpp"
15
#include "../../Utils/FMemUtils.hpp"
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 295 296 297 298 299 300 301 302 303 304 305 306 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

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* This class is the rotation spherical harmonic kernel
*/
template< class ParticleClass, class CellClass, class ContainerClass>
class FSphericalRotationKernel : public FAbstractSphericalKernel<ParticleClass,CellClass,ContainerClass> {
protected:
    typedef FAbstractSphericalKernel<ParticleClass,CellClass,ContainerClass> Parent;

    /** This class define some information to use rotation computation
      */
    struct RotationInfo{
        FReal* rotation_a;
        FReal* rotation_b;

        FComplexe* p_rot_multipole_exp;
        FComplexe* p_rot_local_exp;

        /** Get z vector size */
        static int ZAxisExpensionSize(const int inDevP){
            return int( (inDevP&1) == 0 ? ((inDevP+1) + (inDevP*inDevP)*.25) : ((inDevP+1) + (inDevP*inDevP-1)*.25));
        }

        /** Constructor */
        RotationInfo(const int inDevP){
            rotation_a = new FReal[int( ((inDevP)+1) * ((inDevP)+2) * 0.5 )];
            for(int n = 0 ; n <= inDevP ; ++n){
                for(int m = 0 ; m <= n ; ++m){
                    rotation_a[int(n*(n+1) * 0.5 + m)] = FMath::Sqrt( FReal((n+1+m)*(n+1-m)) / FReal(((2*n+1)*(2*n+3))) );
                }
            }
            rotation_b = new FReal[(inDevP+1) * (inDevP+1)];
            for(int n = 0 ; n <= inDevP ; ++n){
                for(int m = -n ; m < 0 ; ++m){
                    rotation_b[n*(n+1) + m] = -FMath::Sqrt( FReal((n-m-1)*(n-m)) / FReal(((2*n-1)*(2*n+1))) );
                }
                for(int m = 0 ; m <= n ; ++m){
                    rotation_b[n*(n+1) + m] = FMath::Sqrt( FReal((n-m-1)*(n-m)) / FReal(((2*n-1)*(2*n+1))) );
                }
            }
            const int z_size = ZAxisExpensionSize(inDevP);
            p_rot_multipole_exp = new FComplexe[z_size];
            p_rot_local_exp = new FComplexe[z_size];
        }

        /** Destructor */
        ~RotationInfo(){
            delete[] rotation_a;
            delete[] rotation_b;
            delete[] p_rot_multipole_exp;
            delete[] p_rot_local_exp;
        }
    };


    /** This class holds the data need to do a M2L by rotation
      * it is precomputed at the beginning
      */
    struct RotationM2LTransfer {
        const int devP;
        const int expSize;
        FComplexe** rcc_outer;
        FComplexe** rcc_inner;
        FReal* outer_array;

        /** Constructor */
        RotationM2LTransfer(const int inDevP, const int inDevM2lP, const int inExpSize)
            : devP(inDevP), expSize(inExpSize){
            rcc_outer = new FComplexe*[devP + 1];
            rcc_inner = new FComplexe*[devP + 1];
            for( int idxP = 0 ; idxP <= devP ; ++idxP){
                const int rotationSize = ((idxP+1)*(2*idxP+1));
                rcc_outer[idxP] = new FComplexe[rotationSize];
                rcc_inner[idxP] = new FComplexe[rotationSize];
            }
            outer_array = new FReal[inDevM2lP + 1];
        }

        /** Used in the initialisation */
        void spherical_harmonic_Outer_null_order_z_axis(const FReal r){
            const FReal inv_r = FReal(1.0 / r);
            FReal tmp = inv_r;

            // l=0
            outer_array[0] = tmp;

            // l>0
            for(int l = 1 ; l <= devP ; ++l){
                tmp *= inv_r * FReal(l);
                outer_array[l] = tmp;
            }
        }

        /** Used in the initialisation */
        void computeLegendre(const FReal inCosTheta, const FReal inSinTheta, FReal legendre[]){
            const FReal invSinTheta = -inSinTheta;

            legendre[0] = 1.0;        // P_0,0(cosTheta) = 1
            legendre[1] = inCosTheta; // P_1,0(cosTheta) = cosTheta
            legendre[2] = invSinTheta;// P_1,1(cosTheta) = -sinTheta

            int idxCurrentLM  = 3; //current pointer on P_l,m
            int idxCurrentL1M = 1; //pointer on P_{l-1},m => P_1,0
            int idxCurrentL2M = 0; //pointer on P_{l-2},m => P_0,0
            FReal fact = 3.0;

            for(int l = 2; l <= devP ; ++l ){
                // m from 0 to l - 2
                for( int m = 0; m <= l - 2 ; ++m ){
                    legendre[idxCurrentLM] = (inCosTheta * FReal( 2 * l - 1 ) * legendre[idxCurrentL1M]
                                              - FReal( l + m - 1 ) * legendre[idxCurrentL2M] )
                            / FReal( l - m );


                    // progress
                    ++idxCurrentLM;
                    ++idxCurrentL1M;
                    ++idxCurrentL2M;
                }

                // Compute P_l,{l-1}
                legendre[idxCurrentLM++] = inCosTheta * FReal( 2 * l - 1 ) * legendre[idxCurrentL1M];

                // Compute P_l,l
                legendre[idxCurrentLM++] = fact * invSinTheta * legendre[idxCurrentL1M];

                fact += FReal(2.0);
                ++idxCurrentL1M;
            }
        }

        /** Used in the initialisation */
        static int getTranspRotationCoefP(const int n, const int nu, const int m){
            return m*(2*n+1) + (nu+n);
        }
        /** Used in the initialisation */
        static int getRotationCoefP(const int n, const int nu, const int m){
            return (nu)*(2*(n)+1) + (m+n);
        }
        /** Used in the initialisation */
        static int getRotationB(const int n, const int m){
            return n*(n+1) + m;
        }
        /** Used in the initialisation */
        static int getRotationA(const int n, const int m){
            return int(n*(n+1) * 0.5 + (FMath::Abs(m)));
        }

        /** Used in the initialisation */
        static FReal A_div_A(int n, int m, int nu){
            m = FMath::Abs(m);
            nu = FMath::Abs(nu);
            const int min = FMath::Min(m, nu);
            const int max = FMath::Max(m, nu);
            const int i_stop = max - min - 1; /* = n-min - (n-max+1) = n+max - (n+min+1) */

            FReal num = FReal(n-max+1);
            FReal denom = FReal(n+min+1);
            FReal A_min_max = 1;
            for (int i=0; i<=i_stop; ++i, ++num, ++denom){
                A_min_max *= (num/denom);
            }

            if (nu == min)
                return FMath::Sqrt(A_min_max);
            else
                return 1/FMath::Sqrt(A_min_max);
        }

        /** Pre-Compute */
        void rotation_coefficient_container_Fill(const FReal omega,
                                                 const FReal cos_gamma, const FReal sin_gamma,
                                                 const FReal chi, const RotationInfo& rotation_Info){

            FComplexe** rcc_tmp_transposed = new FComplexe*[devP + 1];
            for( int idxP = 0 ; idxP <= devP ; ++idxP){
                const int rotationSize = ((idxP+1)*(2*idxP+1));
                rcc_tmp_transposed[idxP] = new FComplexe[rotationSize];
            }

            FComplexe _pow_of_I_array[7];
            _pow_of_I_array[0].setRealImag(0 , 1 ) /* I^{-3} */;
            _pow_of_I_array[1].setRealImag(-1, 0 ) /* I^{-2} */;
            _pow_of_I_array[2].setRealImag(0 , -1) /* I^{-1} */;
            _pow_of_I_array[3].setRealImag(1 , 0 ) /* I^0 */;
            _pow_of_I_array[4].setRealImag(0 , 1 ) /* I^1 */;
            _pow_of_I_array[5].setRealImag(-1, 0 ) /* I^2 */;
            _pow_of_I_array[6].setRealImag(0 , -1) /* I^3 */;

            const FComplexe* pow_of_I_array = _pow_of_I_array + 3; /* points on I^0 */

            FComplexe* const _precomputed_exp_I_chi_array = new FComplexe[2*devP + 1];
            FComplexe* precomputed_exp_I_chi_array = _precomputed_exp_I_chi_array + devP;

            FComplexe* const _precomputed_exp_I_omega_array  = new FComplexe[2*devP + 1];
            FComplexe* precomputed_exp_I_omega_array = _precomputed_exp_I_omega_array + devP;


            // cos(x) = sin(x + Pi/2)
            for(int m = -devP ; m <= devP ; ++m){
                precomputed_exp_I_chi_array[m].setReal(FMath::Sin(FReal(m)*chi + FMath::FPiDiv2));
                precomputed_exp_I_chi_array[m].setImag(FMath::Sin(FReal(m)*chi));
            }
            for(int nu = -devP ; nu <= devP ; ++nu){
                precomputed_exp_I_omega_array[nu].setReal(FMath::Sin(FReal(nu)*omega + FMath::FPiDiv2));
                precomputed_exp_I_omega_array[nu].setImag(FMath::Sin(FReal(nu)*omega));
            }

            FReal*const ass_Legendre_func_Array = new FReal[expSize];
            FReal* p_ass_Legendre_func_Array = ass_Legendre_func_Array;
            computeLegendre(cos_gamma, sin_gamma, ass_Legendre_func_Array);

            for(int n = 0 ; n <= devP ; ++n){
                // nu == 0:
                FReal c_n_nu = 1;
                rcc_tmp_transposed[n][getTranspRotationCoefP(n,0,0)].setReal(c_n_nu * (*p_ass_Legendre_func_Array));

                ++p_ass_Legendre_func_Array;

                // nu > 0:
                FReal minus_1_pow_nu = -1;
                for(int nu = 1 ; nu <= n; ++nu){
                    c_n_nu /= FMath::Sqrt(FReal((n-nu+1)*(n+nu)));
                    rcc_tmp_transposed[n][getTranspRotationCoefP(n, nu, 0)].setReal(minus_1_pow_nu * c_n_nu * (*p_ass_Legendre_func_Array));
                    rcc_tmp_transposed[n][getTranspRotationCoefP(n, -nu, 0)] = rcc_tmp_transposed[n][getTranspRotationCoefP(n, nu, 0)];
                    minus_1_pow_nu = -minus_1_pow_nu;
                    ++p_ass_Legendre_func_Array;
                } // for nu

                for(int m = 1 ; m <= n ; ++m){
                    for(int nu = -m; nu <= m ; ++nu){
                        const FReal H_nu_minus_1 = ( nu-1 <= -n ?
                                             FReal(0.0) :
                                             (cos_gamma +1) * rotation_Info.rotation_b[getRotationB(n, -nu)]
                                             * rcc_tmp_transposed[n-1][getTranspRotationCoefP(n-1, nu-1, m-1)].getReal());
                        const FReal H_nu_plus_1 = ( nu+1 >= n ?
                                            FReal(0.0) :
                                            (cos_gamma -1) * rotation_Info.rotation_b[getRotationB(n, nu)]
                                            * rcc_tmp_transposed[n-1][getTranspRotationCoefP(n-1, nu+1, m-1)].getReal());

                        const FReal H_nu  = ( FMath::Abs(nu) >= n ?
                                      FReal(0.0) :
                                      sin_gamma * rotation_Info.rotation_a[getRotationA(n-1, nu)]
                                      * rcc_tmp_transposed[n-1][getTranspRotationCoefP(n-1, nu, m-1)].getReal() );


                        rcc_tmp_transposed[n][getTranspRotationCoefP(n, nu, m)].setReal( (FReal(0.5) * (-H_nu_minus_1 - H_nu_plus_1) - H_nu)
                                                                                         / rotation_Info.rotation_b[getRotationB(n, -m)]);
                    } // for nu
                } // for m

                for(int m = 1 ; m <= n ; ++m){
                    for(int nu = -n ; nu <= -m-1; ++nu){
                        rcc_tmp_transposed[n][getTranspRotationCoefP(n, nu, m)] = rcc_tmp_transposed[n][getTranspRotationCoefP(n, -m, -nu)];
                    } // for nu

                    for(int nu = m+1 ; nu <= n; ++nu){
                        rcc_tmp_transposed[n][getTranspRotationCoefP(n, nu, m)] = rcc_tmp_transposed[n][getTranspRotationCoefP(n, m, nu)];
                    } // for nu
                } // for m
            } // for n

            for(int n = 0 ; n <= devP ; ++n){
                for(int nu = 0 ; nu <= n; ++nu){
                    for(int m = -n; m <= n; ++m){
                        FReal A_terms = A_div_A(n, m, nu); /*  A_n^m / A_n^nu */
                        int abs_m_minus_abs_nu_mod4 = (FMath::Abs(m) - FMath::Abs(nu)) % 4; /* can be negative! */
                        const FComplexe p_H_tmp = ( m >= 0 ?
                                        rcc_tmp_transposed[n][getTranspRotationCoefP(n, nu, m)] :
                                        rcc_tmp_transposed[n][getTranspRotationCoefP(n, -nu, -m)]) ;

                        /*************** T_Outer_n^{nu, m}(omega, gamma, chi): ***************/
                        rcc_outer[n][getRotationCoefP(n, nu, m)] = p_H_tmp;
                        /* H_n^{nu, m}(gamma) => T_n^{nu, m}(omega, gamma, chi) */
                        rcc_outer[n][getRotationCoefP(n, nu, m)] *= precomputed_exp_I_chi_array[ + m];
                        rcc_outer[n][getRotationCoefP(n, nu, m)] *= precomputed_exp_I_omega_array[ - nu];
                        /* T_Outer_j^{nu, k}(omega, gamma, chi) = i^{|k|-|nu|} (A_j^nu / A_j^k) T_j^{nu, k}(omega, gamma, chi)     (6) */
                        rcc_outer[n][getRotationCoefP(n, nu, m)] *= pow_of_I_array[abs_m_minus_abs_nu_mod4];
                        rcc_outer[n][getRotationCoefP(n, nu, m)] *= (FReal(1.0) / A_terms);


                        /*************** T_Inner_n^{nu, m}(chi, gamma, omega): ***************/
                        rcc_inner[n][getRotationCoefP(n, nu, m)] = p_H_tmp;
                        /* H_n^{nu, m}(gamma) => T_n^{nu, m}(chi, gamma, omega) */
                        rcc_inner[n][getRotationCoefP(n, nu, m)] *= precomputed_exp_I_omega_array[ + m];
                        rcc_inner[n][getRotationCoefP(n, nu, m)] *= precomputed_exp_I_chi_array[ - nu];
                        /* T_Inner_j^{nu, k}(omega, gamma, chi) = i^{|nu|-|k|} (A_j^k / A_j^nu) T_j^{nu, k}(omega, gamma, chi)    (7) */
                        rcc_inner[n][getRotationCoefP(n, nu, m)] *= pow_of_I_array[- abs_m_minus_abs_nu_mod4];
                        rcc_inner[n][getRotationCoefP(n, nu, m)] *= A_terms;

                    }// for m
                } // for nu
            } // for n


            delete[] (ass_Legendre_func_Array);
            delete[] (_precomputed_exp_I_chi_array);
            delete[] (_precomputed_exp_I_omega_array);
            FMemUtils::DeleteAll( rcc_tmp_transposed, devP);
            delete[] rcc_tmp_transposed;
        }

        /** Pre-compute */
        void transfer_M2L_rotation_Fill(const FSpherical& inSphere, const RotationInfo& rotation_Info){

            // Computes rotation coefficients:
            rotation_coefficient_container_Fill(FMath::FPi, inSphere.getCosTheta(),
                                                inSphere.getSinTheta(), inSphere.getPhi(), rotation_Info);

            // Computes Outer terms:
            spherical_harmonic_Outer_null_order_z_axis(inSphere.getR());


        }

        ~RotationM2LTransfer(){
            FMemUtils::DeleteAll( rcc_outer, devP);
            FMemUtils::DeleteAll( rcc_inner, devP);
            delete[] rcc_outer;
            delete[] rcc_inner;
            delete[] outer_array;
        }
    };

    const int devM2lP;               //< A secondary P

343
    FSmartPointer<RotationM2LTransfer*> preM2LTransitions;   //< The pre-computation for the M2L based on the level and the 189 possibilities
344 345 346 347
    RotationInfo rotation_Info;

    /** To access te pre computed M2L transfer vector */
    int indexM2LTransition(const int idxX,const int idxY,const int idxZ) const {
348
        return (((((idxX+3) * 7) + (idxY+3)) * 7 ) + (idxZ+3));
349 350 351 352 353 354 355
    }

    /** Alloc and init pre-vectors*/
    void allocAndInit(){
        // M2L transfer, there is a maximum of 3 neighbors in each direction,
        // so 6 in each dimension
        preM2LTransitions = new RotationM2LTransfer*[Parent::treeHeight + Parent::periodicLevels];
356
        memset(preM2LTransitions.getPtr(), 0, sizeof(FComplexe*) * (Parent::treeHeight + Parent::periodicLevels));
357 358 359 360
        // We start from the higher level
        FReal treeWidthAtLevel = Parent::boxWidth * FReal(1 << Parent::periodicLevels);
        for(int idxLevel = -Parent::periodicLevels ; idxLevel < Parent::treeHeight ; ++idxLevel ){
            // Allocate data for this level
361
            preM2LTransitions[idxLevel + Parent::periodicLevels] = reinterpret_cast<RotationM2LTransfer*>(new char[(7 * 7 * 7) * sizeof(RotationM2LTransfer)]);
362 363 364 365 366 367 368
            // Precompute transfer vector
            for(int idxX = -3 ; idxX <= 3 ; ++idxX ){
                for(int idxY = -3 ; idxY <= 3 ; ++idxY ){
                    for(int idxZ = -3 ; idxZ <= 3 ; ++idxZ ){
                        new (&preM2LTransitions[idxLevel + Parent::periodicLevels][indexM2LTransition(idxX,idxY,idxZ)]) RotationM2LTransfer(Parent::devP,devM2lP,Parent::harmonic.getExpSize());

                        if(FMath::Abs(idxX) > 1 || FMath::Abs(idxY) > 1 || FMath::Abs(idxZ) > 1){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
369
                            const FPoint relativePos( FReal(-idxX) * treeWidthAtLevel , FReal(-idxY) * treeWidthAtLevel , FReal(-idxZ) * treeWidthAtLevel );
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
                            preM2LTransitions[idxLevel + Parent::periodicLevels][indexM2LTransition(idxX,idxY,idxZ)].transfer_M2L_rotation_Fill(FSpherical(relativePos), rotation_Info);
                        }
                    }
                }
            }
            // We divide the bow per 2 when we go down
            treeWidthAtLevel /= 2;
        }
    }


public:
    /** Constructor
      * @param inDevP the polynomial degree
      * @param inThreeHeight the height of the tree
      * @param inBoxWidth the size of the simulation box
      * @param inPeriodicLevel the number of level upper to 0 that will be requiried
      */
BRAMAS Berenger's avatar
BRAMAS Berenger committed
388
    FSphericalRotationKernel(const int inDevP, const int inTreeHeight, const FReal inBoxWidth, const FPoint& inBoxCenter, const int inPeriodicLevel = 0)
389
        : Parent(inDevP, inTreeHeight, inBoxWidth, inBoxCenter, inPeriodicLevel),
390 391 392
          devM2lP(int(((inDevP*2)+1) * ((inDevP*2)+2) * 0.5)),
          preM2LTransitions(0),
          rotation_Info(inDevP) {
393 394 395 396 397
        allocAndInit();
    }

    /** Copy constructor */
    FSphericalRotationKernel(const FSphericalRotationKernel& other)
398 399 400 401
        : Parent(other), devM2lP(other.devM2lP),
          preM2LTransitions(other.preM2LTransitions),
          rotation_Info(other.devP) {

402 403 404 405
    }

    /** Destructor */
    ~FSphericalRotationKernel(){
406 407 408 409 410 411
        if( preM2LTransitions.isLast() ){
            for(int idxLevel = -Parent::periodicLevels ; idxLevel < Parent::treeHeight ; ++idxLevel ){
                for(int idx = 0 ; idx < 7*7*7 ; ++idx ){
                    preM2LTransitions[idxLevel + Parent::periodicLevels][idx].~RotationM2LTransfer();
                }
                delete[] reinterpret_cast<char*>(preM2LTransitions[idxLevel + Parent::periodicLevels]);
412 413 414 415 416
            }
        }
    }

    /** M2L with a cell and all the existing neighbors */
417 418
    void M2L(CellClass* const FRestrict pole, const CellClass* distantNeighbors[343],
             const int /*size*/, const int inLevel) {
419
        // For all neighbors compute M2L
420 421 422 423 424
        for(int idxNeigh = 0 ; idxNeigh < 343 ; ++idxNeigh){
            if(distantNeighbors[idxNeigh]){
                const RotationM2LTransfer& transitionVector = preM2LTransitions[inLevel + Parent::periodicLevels][idxNeigh];
                multipoleToLocal(pole->getLocal(), distantNeighbors[idxNeigh]->getMultipole(), transitionVector);
            }
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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
        }
    }


    /** M2L With rotation
      */
    void multipoleToLocal(FComplexe*const FRestrict local_exp_target, const FComplexe* const FRestrict multipole_exp_src,
                          const RotationM2LTransfer& transfer_M2L_rotation){

        memset(rotation_Info.p_rot_multipole_exp, 0, RotationInfo::ZAxisExpensionSize(Parent::devP) * sizeof(FComplexe));
        memset(rotation_Info.p_rot_local_exp, 0, RotationInfo::ZAxisExpensionSize(Parent::devP) * sizeof(FComplexe));

        rotation_Rotate_multipole_expansion_terms(multipole_exp_src, transfer_M2L_rotation.rcc_outer, rotation_Info.p_rot_multipole_exp);

        M2L_z_axis(rotation_Info.p_rot_local_exp, rotation_Info.p_rot_multipole_exp, transfer_M2L_rotation.outer_array);

        rotation_Rotate_local_expansion_terms(rotation_Info.p_rot_local_exp, transfer_M2L_rotation.rcc_inner, local_exp_target);
    }

    /** Needed when doing the M2L */
    void rotation_Rotate_multipole_expansion_terms(const FComplexe*const FRestrict multipole_exp,
                                                   const FComplexe* const FRestrict * const FRestrict rcc_outer,
                                                   FComplexe*const FRestrict rot_multipole_exp){

        FComplexe* p_rot_multipole_exp = rot_multipole_exp;

        for(int nu = 0 ; nu <= (Parent::devP/2) ; ++nu){
            for(int j = nu; j <= (Parent::devP-nu) ; ++j){
                const FComplexe* p_rcc_outer = &rcc_outer[j][RotationM2LTransfer::getRotationCoefP(j, nu, j)];
                const FComplexe* p_multipole_exp = &multipole_exp[Parent::harmonic.getPreExpRedirJ(j) + j];
                FReal minus_1_pow_k = FReal(j&1 ? -1 : 1);

                for(int k = -j ; k < 0 ; ++k){ /* k < 0 */
                    p_rot_multipole_exp->incReal( minus_1_pow_k *
                                                  ((p_multipole_exp->getReal() * p_rcc_outer->getReal()) +
                                                   (p_multipole_exp->getImag() * p_rcc_outer->getImag())) );
                    p_rot_multipole_exp->incImag( minus_1_pow_k *
                                                  ((p_multipole_exp->getReal() * p_rcc_outer->getImag()) -
                                                   (p_multipole_exp->getImag() * p_rcc_outer->getReal())) );

                    minus_1_pow_k = -minus_1_pow_k;
                    --p_rcc_outer;
                    --p_multipole_exp;
                } /* for k */

                for(int k = 0; k <= j ; ++k){ /* k >= 0 */
                    p_rot_multipole_exp->incReal(
                                ((p_multipole_exp->getReal() * p_rcc_outer->getReal()) -
                                 (p_multipole_exp->getImag() * p_rcc_outer->getImag())) );
                    p_rot_multipole_exp->incImag(
                                ((p_multipole_exp->getReal() * p_rcc_outer->getImag()) +
                                 (p_multipole_exp->getImag() * p_rcc_outer->getReal())) );

                    --p_rcc_outer;
                    ++p_multipole_exp;
                } /* for k */

                ++p_rot_multipole_exp;
            } /* for j */
        } /* for nu */
    }

    /** Needed when doing the M2L */
    void M2L_z_axis(FComplexe* const FRestrict rot_local_exp,
                    const FComplexe* const FRestrict rot_multipole_exp,
                    const FReal* const outer_array){
        FComplexe* p_rot_local_exp = rot_local_exp;

        for(int j = 0 ; j <= Parent::devP; ++j){
            const FReal* p_outer_array_j = outer_array + j;
            const int stop_for_n = Parent::devP-j;
            const int min_j = FMath::Min(j, stop_for_n);
            for(int k = 0 ; k <= min_j ; ++k){
                const FComplexe* p_rot_multipole_exp = rot_multipole_exp + k * (Parent::devP + 2 - k);
                for(int n = k ; n <= stop_for_n ; ++n){
                    p_rot_local_exp->incReal(p_rot_multipole_exp->getReal() * p_outer_array_j[n]);
                    p_rot_local_exp->incImag(p_rot_multipole_exp->getImag() * p_outer_array_j[n]);
                    ++p_rot_multipole_exp;
                } /* for n */
                ++p_rot_local_exp;
            } /* for k */
        } /* for j */
    }

    /** Needed when doing the M2L */
    void rotation_Rotate_local_expansion_terms(const FComplexe*const rot_local_exp,
                                               const FComplexe*const FRestrict *const FRestrict rcc_inner,
                                               FComplexe*const FRestrict local_exp){
        const int Q = Parent::devP/2;

        FComplexe* FRestrict p_local_exp = local_exp;

        for(int j = 0 ; j <= Q ; ++j){
            const int min_j = j;
            const FComplexe* const FRestrict p_rot_local_exp_j = &rot_local_exp[Parent::harmonic.getPreExpRedirJ(j) + j];

            for (int nu = 0 ; nu <= j; ++nu){
                const FComplexe* FRestrict p_rcc_inner = &rcc_inner[j][RotationM2LTransfer::getRotationCoefP(j, nu, -min_j)];
                const FComplexe* FRestrict p_rot_local_exp = p_rot_local_exp_j;
                FReal minus_1_pow_k = FReal(min_j&1 ? -1 : 1);

                for(int k = -min_j ; k < 0 ; ++k){  /* k < 0 */
                    p_local_exp->incReal( minus_1_pow_k *
                                          ((p_rot_local_exp->getReal() * p_rcc_inner->getReal()) +
                                           (p_rot_local_exp->getImag() * p_rcc_inner->getImag())));
                            p_local_exp->incImag( minus_1_pow_k *
                                                  ((p_rot_local_exp->getReal() * p_rcc_inner->getImag()) -
                                                   (p_rot_local_exp->getImag() * p_rcc_inner->getReal())));

                            minus_1_pow_k = -minus_1_pow_k;
                    --p_rot_local_exp;
                    ++p_rcc_inner;
                } /* for k */

                for(int k = 0; k <= min_j ; ++k){  /* k >= 0 */
                    p_local_exp->incReal(
                                ((p_rot_local_exp->getReal() * p_rcc_inner->getReal()) -
                                 (p_rot_local_exp->getImag() * p_rcc_inner->getImag())));
                            p_local_exp->incImag(
                                ((p_rot_local_exp->getReal() * p_rcc_inner->getImag()) +
                                 (p_rot_local_exp->getImag() * p_rcc_inner->getReal())));

                            ++p_rot_local_exp;
                    ++p_rcc_inner;
                } /* for k */


                ++p_local_exp;
            } /* for nu */
        } /* for j */

        const FComplexe* FRestrict p_rot_local_exp_j = &rot_local_exp[Parent::harmonic.getPreExpRedirJ(Q) + Q];

        for(int j = Q + 1; j <= Parent::devP ; ++j){
            p_rot_local_exp_j += Parent::devP - j +1;
            const int min_j = Parent::devP-j;

            for(int nu = 0 ; nu <= j; ++nu){
                const FComplexe* FRestrict p_rcc_inner = &rcc_inner[j][RotationM2LTransfer::getRotationCoefP(j, nu, -min_j)];
                const FComplexe* FRestrict p_rot_local_exp = p_rot_local_exp_j;
                FReal minus_1_pow_k = FReal(min_j&1 ? -1 : 1);

                for(int k = -min_j ; k < 0; ++k){  /* k < 0 */
                    p_local_exp->incReal( minus_1_pow_k *
                                          ((p_rot_local_exp->getReal() * p_rcc_inner->getReal()) +
                                           (p_rot_local_exp->getImag() * p_rcc_inner->getImag())));
                            p_local_exp->incImag( minus_1_pow_k *
                                                  ((p_rot_local_exp->getReal() * p_rcc_inner->getImag()) -
                                                   (p_rot_local_exp->getImag() * p_rcc_inner->getReal())));

                            minus_1_pow_k = -minus_1_pow_k;
                    --p_rot_local_exp;
                    ++p_rcc_inner;
                } /* for k */
                for(int k = 0; k<=min_j; ++k){  /* k >= 0 */
                    p_local_exp->incReal(
                                ((p_rot_local_exp->getReal() * p_rcc_inner->getReal()) -
                                 (p_rot_local_exp->getImag() * p_rcc_inner->getImag())));
                            p_local_exp->incImag(
                                ((p_rot_local_exp->getReal() * p_rcc_inner->getImag()) +
                                 (p_rot_local_exp->getImag() * p_rcc_inner->getReal())));

                            ++p_rot_local_exp;
                    ++p_rcc_inner;
                } /* for k */
                ++p_local_exp;
            } /* for nu */
        } /* for j */
    }
};



#endif // FSPHERICALROTATIONKERNEL_HPP