FChebInterpolator.hpp 64.5 KB
Newer Older
BRAMAS Berenger's avatar
BRAMAS Berenger committed
1
// ===================================================================================
2 3 4 5 6 7 8 9 10 11 12 13 14
// 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".
BRAMAS Berenger's avatar
BRAMAS Berenger committed
15
// ===================================================================================
16 17 18 19 20 21 22 23
#ifndef FCHEBINTERPOLATOR_HPP
#define FCHEBINTERPOLATOR_HPP


#include "./FChebMapping.hpp"
#include "./FChebTensor.hpp"
#include "./FChebRoots.hpp"

24
#include "../../Utils/FBlas.hpp"
25

26 27 28 29 30 31 32 33 34


/**
 * @author Matthias Messner (matthias.matthias@inria.fr)
 * Please read the license
 */

/**
 * @class FChebInterpolator
35
 *
36
 * The class @p FChebInterpolator defines the anterpolation (M2M) and
37
 * interpolation (L2L) concerning operations.
38 39 40 41 42 43 44 45 46 47
 */
template <int ORDER>
class FChebInterpolator : FNoCopyable
{
  // compile time constants and types
  enum {nnodes = TensorTraits<ORDER>::nnodes};
  typedef FChebRoots< ORDER>  BasisType;
  typedef FChebTensor<ORDER> TensorType;

  FReal T_of_roots[ORDER][ORDER];
48
  FReal T[ORDER * (ORDER-1)];
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
    unsigned int node_ids[nnodes][3];
    FReal* ChildParentInterpolator[8];

    // permutations (only needed in the tensor product interpolation case)
    unsigned int perm[3][nnodes];

    ////////////////////////////////////////////////////////////////////
    // needed for P2M
    struct IMN2MNI {
        enum {size = ORDER * (ORDER-1) * (ORDER-1)};
        unsigned int imn[size], mni[size];
        IMN2MNI() {
            unsigned int counter = 0;
            for (unsigned int i=0; i<ORDER; ++i) {
                for (unsigned int m=0; m<ORDER-1; ++m) {
                    for (unsigned int n=0; n<ORDER-1; ++n) {
                        imn[counter] = n*(ORDER-1)*ORDER + m*ORDER + i;
                        mni[counter] = i*(ORDER-1)*(ORDER-1) + n*(ORDER-1) + m;
                        counter++;
                    }
                }
            }
        }
    } perm0;

    struct JNI2NIJ {
        enum {size = ORDER * ORDER * (ORDER-1)};
        unsigned int jni[size], nij[size];
        JNI2NIJ() {
            unsigned int counter = 0;
            for (unsigned int i=0; i<ORDER; ++i) {
                for (unsigned int j=0; j<ORDER; ++j) {
                    for (unsigned int n=0; n<ORDER-1; ++n) {
                        jni[counter] = i*(ORDER-1)*ORDER + n*ORDER + j;
                        nij[counter] = j*ORDER*(ORDER-1) + i*(ORDER-1) + n;
                        counter++;
                    }
                }
            }
        }
    } perm1;

    struct KIJ2IJK {
        enum {size = ORDER * ORDER * ORDER};
        unsigned int kij[size], ijk[size];
        KIJ2IJK() {
            unsigned int counter = 0;
            for (unsigned int i=0; i<ORDER; ++i) {
                for (unsigned int j=0; j<ORDER; ++j) {
                    for (unsigned int k=0; k<ORDER; ++k) {
                        kij[counter] = j*ORDER*ORDER + i*ORDER + k;
                        ijk[counter] = k*ORDER*ORDER + j*ORDER + i;
                        counter++;
                    }
                }
            }
        }
    } perm2;
    ////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////
    // needed for L2P
    struct IJK2JKI {
        enum {size = ORDER * ORDER * ORDER};
        unsigned int ijk[size], jki[size];
        IJK2JKI() {
            unsigned int counter = 0;
            for (unsigned int i=0; i<ORDER; ++i) {
                for (unsigned int j=0; j<ORDER; ++j) {
                    for (unsigned int k=0; k<ORDER; ++k) {
                        ijk[counter] = k*ORDER*ORDER + j*ORDER + i;
                        jki[counter] = i*ORDER*ORDER + k*ORDER + j;
                        counter++;
                    }
                }
            }
        }
        void permute(const FReal *const IN, FReal *const OUT) const
        { for (unsigned int i=0; i<size; ++i) OUT[jki[i]] = IN[ijk[i]]; }
    } perm3;

    struct IJK2KIJ {
        enum {size = ORDER * ORDER * ORDER};
        unsigned int ijk[size], kij[size];
        IJK2KIJ() {
            unsigned int counter = 0;
            for (unsigned int i=0; i<ORDER; ++i) {
                for (unsigned int j=0; j<ORDER; ++j) {
                    for (unsigned int k=0; k<ORDER; ++k) {
                        ijk[counter] = k*ORDER*ORDER + j*ORDER + i;
                        kij[counter] = j*ORDER*ORDER + i*ORDER + k;
                        counter++;
                    }
                }
            }
        }
        void permute(const FReal *const IN, FReal *const OUT) const
        { for (unsigned int i=0; i<size; ++i) OUT[kij[i]] = IN[ijk[i]]; }
    } perm4;

    struct LJK2JKL {
        enum {size = (ORDER-1) * ORDER * ORDER};
        unsigned int ljk[size], jkl[size];
        LJK2JKL() {
            unsigned int counter = 0;
            for (unsigned int l=0; l<ORDER-1; ++l) {
                for (unsigned int j=0; j<ORDER; ++j) {
                    for (unsigned int k=0; k<ORDER; ++k) {
                        ljk[counter] = k*ORDER*(ORDER-1) + j*(ORDER-1) + l;
                        jkl[counter] = l*ORDER*ORDER + k*ORDER + j;
                        counter++;
                    }
                }
            }
        }
        void permute(const FReal *const IN, FReal *const OUT) const
        { for (unsigned int i=0; i<size; ++i) OUT[jkl[i]] = IN[ljk[i]]; }
    } perm5;

    struct LJK2KLJ {
        enum {size = (ORDER-1) * ORDER * ORDER};
        unsigned int ljk[size], klj[size];
        LJK2KLJ() {
            unsigned int counter = 0;
            for (unsigned int l=0; l<ORDER-1; ++l) {
                for (unsigned int j=0; j<ORDER; ++j) {
                    for (unsigned int k=0; k<ORDER; ++k) {
                        ljk[counter] = k*ORDER*(ORDER-1) + j*(ORDER-1) + l;
                        klj[counter] = j*(ORDER-1)*ORDER + l*ORDER + k;
                        counter++;
                    }
                }
            }
        }
        void permute(const FReal *const IN, FReal *const OUT) const
        { for (unsigned int i=0; i<size; ++i) OUT[klj[i]] = IN[ljk[i]]; }
    } perm6;

    struct MKI2KIM {
        enum {size = (ORDER-1) * ORDER * ORDER};
        unsigned int mki[size], kim[size];
        MKI2KIM() {
            unsigned int counter = 0;
            for (unsigned int m=0; m<ORDER-1; ++m) {
                for (unsigned int k=0; k<ORDER; ++k) {
                    for (unsigned int i=0; i<ORDER; ++i) {
                        mki[counter] = i*ORDER*(ORDER-1) + k*(ORDER-1) + m;
                        kim[counter] = m*ORDER*ORDER + i*ORDER + k;
                        counter++;
                    }
                }
            }
        }
        void permute(const FReal *const IN, FReal *const OUT) const
        { for (unsigned int i=0; i<size; ++i) OUT[kim[i]] = IN[mki[i]]; }
    } perm7;

    struct MKL2KLM {
        enum {size = (ORDER-1) * ORDER * (ORDER-1)};
        unsigned int mkl[size], klm[size];
        MKL2KLM() {
            unsigned int counter = 0;
            for (unsigned int m=0; m<ORDER-1; ++m) {
                for (unsigned int k=0; k<ORDER; ++k) {
                    for (unsigned int l=0; l<ORDER-1; ++l) {
                        mkl[counter] = l*ORDER*(ORDER-1) + k*(ORDER-1) + m;
                        klm[counter] = m*(ORDER-1)*ORDER + l*ORDER + k;
                        counter++;
                    }
                }
            }
        }
        void permute(const FReal *const IN, FReal *const OUT) const
        { for (unsigned int i=0; i<size; ++i) OUT[klm[i]] = IN[mkl[i]]; }
    } perm8;

    struct NLM2LMN {
        enum {size = (ORDER-1) * (ORDER-1) * (ORDER-1)};
        unsigned int nlm[size], lmn[size];
        NLM2LMN() {
            unsigned int counter = 0;
            for (unsigned int n=0; n<ORDER-1; ++n) {
                for (unsigned int l=0; l<ORDER-1; ++l) {
                    for (unsigned int m=0; m<ORDER-1; ++m) {
                        nlm[counter] = m*(ORDER-1)*(ORDER-1) + l*(ORDER-1) + n;
                        lmn[counter] = n*(ORDER-1)*(ORDER-1) + m*(ORDER-1) + l;
                        counter++;
                    }
                }
            }
        }
        void permute(const FReal *const IN, FReal *const OUT) const
        { for (unsigned int i=0; i<size; ++i) OUT[lmn[i]] = IN[nlm[i]]; }
    } perm9;

    ////////////////////////////////////////////////////////////////////



    /**
     * Initialize the child - parent - interpolator, it is basically the matrix
     * S which is precomputed and reused for all M2M and L2L operations, ie for
     * all non leaf inter/anterpolations.
     */
    void initM2MandL2L()
    {
        FPoint ParentRoots[nnodes], ChildRoots[nnodes];
        const FReal ParentWidth(2.);
        const FPoint ParentCenter(0., 0., 0.);
        FChebTensor<ORDER>::setRoots(ParentCenter, ParentWidth, ParentRoots);

        FPoint ChildCenter;
        const FReal ChildWidth(1.);

        // loop: child cells
        for (unsigned int child=0; child<8; ++child) {

            // allocate memory
            ChildParentInterpolator[child] = new FReal [nnodes * nnodes];

            // set child info
            FChebTensor<ORDER>::setRelativeChildCenter(child, ChildCenter);
            FChebTensor<ORDER>::setRoots(ChildCenter, ChildWidth, ChildRoots);

            // assemble child - parent - interpolator
            assembleInterpolator(nnodes, ChildRoots, ChildParentInterpolator[child]);
        }
    }

    /**
     * Initialize the child - parent - interpolator, it is basically the matrix
     * S which is precomputed and reused for all M2M and L2L operations, ie for
     * all non leaf inter/anterpolations.
     */
    void initTensorM2MandL2L()
    {
        FPoint ParentRoots[nnodes];
        FReal ChildCoords[3][ORDER];
        const FReal ParentWidth(2.);
        const FPoint ParentCenter(0., 0., 0.);
        FChebTensor<ORDER>::setRoots(ParentCenter, ParentWidth, ParentRoots);

        FPoint ChildCenter;
        const FReal ChildWidth(1.);

        // loop: child cells
        for (unsigned int child=0; child<8; ++child) {

            // set child info
            FChebTensor<ORDER>::setRelativeChildCenter(child, ChildCenter);
            FChebTensor<ORDER>::setChebyshevRoots(ChildCenter, ChildWidth, ChildCoords);

            // allocate memory
            ChildParentInterpolator[child] = new FReal [3 * ORDER*ORDER];
            assembleInterpolator(ORDER, ChildCoords[0], ChildParentInterpolator[child]);
            assembleInterpolator(ORDER, ChildCoords[1], ChildParentInterpolator[child] + 1 * ORDER*ORDER);
            assembleInterpolator(ORDER, ChildCoords[2], ChildParentInterpolator[child] + 2 * ORDER*ORDER);
        }


        // init permutations
        for (unsigned int i=0; i<ORDER; ++i) {
            for (unsigned int j=0; j<ORDER; ++j) {
                for (unsigned int k=0; k<ORDER; ++k) {
                    const unsigned int index = k*ORDER*ORDER + j*ORDER + i;
                    perm[0][index] = k*ORDER*ORDER + j*ORDER + i;
                    perm[1][index] = i*ORDER*ORDER + k*ORDER + j;
                    perm[2][index] = j*ORDER*ORDER + i*ORDER + k;
                }
            }
        }

    }
322

323 324 325


public:
326 327 328 329 330 331 332
    /**
     * Constructor: Initialize the Chebyshev polynomials at the Chebyshev
     * roots/interpolation point
     */
    explicit FChebInterpolator()
    {
        // initialize chebyshev polynomials of root nodes: T_o(x_j)
333 334
    for (unsigned int o=1; o<ORDER; ++o)
      for (unsigned int j=0; j<ORDER; ++j)
messner's avatar
messner committed
335
        T_of_roots[o][j] = FReal(BasisType::T(o, FReal(BasisType::roots[j])));
336

337
        // initialize chebyshev polynomials of root nodes: T_o(x_j)
338 339 340
    for (unsigned int o=1; o<ORDER; ++o)
      for (unsigned int j=0; j<ORDER; ++j)
        T[(o-1)*ORDER + j] = FReal(BasisType::T(o, FReal(BasisType::roots[j])));
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 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


        // initialize root node ids
        TensorType::setNodeIds(node_ids);

        // initialize interpolation operator for non M2M and L2L (non leaf
        // operations)
        //this -> initM2MandL2L();     // non tensor-product interpolation
        this -> initTensorM2MandL2L(); // tensor-product interpolation
    }


    /**
     * Destructor: Delete dynamically allocated memory for M2M and L2L operator
     */
    ~FChebInterpolator()
    {
        for (unsigned int child=0; child<8; ++child)
            delete [] ChildParentInterpolator[child];
    }


    /**
     * Assembles the interpolator \f$S_\ell\f$ of size \f$N\times
     * \ell^3\f$. Here local points is meant as points whose global coordinates
     * have already been mapped to the reference interval [-1,1].
     *
     * @param[in] NumberOfLocalPoints
     * @param[in] LocalPoints
     * @param[out] Interpolator
     */
    void assembleInterpolator(const unsigned int NumberOfLocalPoints,
                  const FPoint *const LocalPoints,
                  FReal *const Interpolator) const
    {
        // values of chebyshev polynomials of source particle: T_o(x_i)
        FReal T_of_x[ORDER][3];
        // loop: local points (mapped in [-1,1])
        for (unsigned int m=0; m<NumberOfLocalPoints; ++m) {
            // evaluate chebyshev polynomials at local points
            for (unsigned int o=1; o<ORDER; ++o) {
                T_of_x[o][0] = BasisType::T(o, LocalPoints[m].getX());
                T_of_x[o][1] = BasisType::T(o, LocalPoints[m].getY());
                T_of_x[o][2] = BasisType::T(o, LocalPoints[m].getZ());
            }

            // assemble interpolator
            for (unsigned int n=0; n<nnodes; ++n) {
                //Interpolator[n*nnodes + m] = FReal(1.);
                Interpolator[n*NumberOfLocalPoints + m] = FReal(1.);
                for (unsigned int d=0; d<3; ++d) {
                    const unsigned int j = node_ids[n][d];
                    FReal S_d = FReal(1.) / ORDER;
                    for (unsigned int o=1; o<ORDER; ++o)
                        S_d += FReal(2.) / ORDER * T_of_x[o][d] * T_of_roots[o][j];
                    //Interpolator[n*nnodes + m] *= S_d;
                    Interpolator[n*NumberOfLocalPoints + m] *= S_d;
                }

            }

        }

    }


    void assembleInterpolator(const unsigned int M, const FReal *const x, FReal *const S) const
    {
        // values of chebyshev polynomials of source particle: T_o(x_i)
        FReal T_of_x[ORDER];

        // loop: local points (mapped in [-1,1])
        for (unsigned int m=0; m<M; ++m) {
            // evaluate chebyshev polynomials at local points
            for (unsigned int o=1; o<ORDER; ++o)
                T_of_x[o] = BasisType::T(o, x[m]);

            for (unsigned int n=0; n<ORDER; ++n) {
                S[n*M + m] = FReal(1.) / ORDER;
                for (unsigned int o=1; o<ORDER; ++o)
                    S[n*M + m] += FReal(2.) / ORDER * T_of_x[o] * T_of_roots[o][n];
            }

        }

    }



    const FReal *const *const getChildParentInterpolator() const
    { return ChildParentInterpolator; }
    const unsigned int *const getPermutationsM2ML2L(unsigned int i) const
    { return perm[i]; }






    /**
     * Particle to moment: application of \f$S_\ell(y,\bar y_n)\f$
     * (anterpolation, it is the transposed interpolation)
     */
    template <class ContainerClass>
    void applyP2M(const FPoint& center,
                                const FReal width,
                                FReal *const multipoleExpansion,
                                const ContainerClass *const sourceParticles) const;



    /**
     * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ (interpolation)
     */
    template <class ContainerClass>
    void applyL2P(const FPoint& center,
                                const FReal width,
                                const FReal *const localExpansion,
                                ContainerClass *const localParticles) const;


    /**
     * Local to particle operation: application of \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
     */
    template <class ContainerClass>
    void applyL2PGradient(const FPoint& center,
                                                const FReal width,
                                                const FReal *const localExpansion,
                                                ContainerClass *const localParticles) const;

    /**
     * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ and
     * \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
     */
    template <class ContainerClass>
    void applyL2PTotal(const FPoint& center,
                                         const FReal width,
                                         const FReal *const localExpansion,
                                         ContainerClass *const localParticles) const;


    /*
    void applyM2M(const unsigned int ChildIndex,
                                const FReal *const ChildExpansion,
                                FReal *const ParentExpansion) const
    {
        FBlas::gemtva(nnodes, nnodes, FReal(1.),
                                    ChildParentInterpolator[ChildIndex],
                                    const_cast<FReal*>(ChildExpansion), ParentExpansion);
    }

    void applyL2L(const unsigned int ChildIndex,
                                const FReal *const ParentExpansion,
                                FReal *const ChildExpansion) const
    {
        FBlas::gemva(nnodes, nnodes, FReal(1.),
                                 ChildParentInterpolator[ChildIndex],
                                 const_cast<FReal*>(ParentExpansion), ChildExpansion);
    }
    */



    void applyM2M(const unsigned int ChildIndex,
                                const FReal *const ChildExpansion,
                                FReal *const ParentExpansion) const
    {
        FReal Exp[nnodes], PermExp[nnodes];
        // ORDER*ORDER*ORDER * (2*ORDER-1)
        FBlas::gemtm(ORDER, ORDER, ORDER*ORDER, FReal(1.),
                                 ChildParentInterpolator[ChildIndex], ORDER,
                                 const_cast<FReal*>(ChildExpansion), ORDER, PermExp, ORDER);

        for (unsigned int n=0; n<nnodes; ++n)	Exp[n] = PermExp[perm[1][n]];
        // ORDER*ORDER*ORDER * (2*ORDER-1)
        FBlas::gemtm(ORDER, ORDER, ORDER*ORDER, FReal(1.),
                                 ChildParentInterpolator[ChildIndex] + 2 * ORDER*ORDER, ORDER,
                                 Exp, ORDER, PermExp, ORDER);

        for (unsigned int n=0; n<nnodes; ++n)	Exp[perm[1][n]] = PermExp[perm[2][n]];
        // ORDER*ORDER*ORDER * (2*ORDER-1)
        FBlas::gemtm(ORDER, ORDER, ORDER*ORDER, FReal(1.),
                                 ChildParentInterpolator[ChildIndex] + 1 * ORDER*ORDER, ORDER,
                                 Exp, ORDER, PermExp, ORDER);

        for (unsigned int n=0; n<nnodes; ++n)	ParentExpansion[perm[2][n]] += PermExp[n];
    }


    void applyL2L(const unsigned int ChildIndex,
                                const FReal *const ParentExpansion,
                                FReal *const ChildExpansion) const
    {
        FReal Exp[nnodes], PermExp[nnodes];
        // ORDER*ORDER*ORDER * (2*ORDER-1)
        FBlas::gemm(ORDER, ORDER, ORDER*ORDER, FReal(1.),
                                ChildParentInterpolator[ChildIndex], ORDER,
                                const_cast<FReal*>(ParentExpansion), ORDER, PermExp, ORDER);

        for (unsigned int n=0; n<nnodes; ++n)	Exp[n] = PermExp[perm[1][n]];
        // ORDER*ORDER*ORDER * (2*ORDER-1)
        FBlas::gemm(ORDER, ORDER, ORDER*ORDER, FReal(1.),
                                ChildParentInterpolator[ChildIndex] + 2 * ORDER*ORDER, ORDER,
                                Exp, ORDER, PermExp, ORDER);

        for (unsigned int n=0; n<nnodes; ++n)	Exp[perm[1][n]] = PermExp[perm[2][n]];
        // ORDER*ORDER*ORDER * (2*ORDER-1)
        FBlas::gemm(ORDER, ORDER, ORDER*ORDER, FReal(1.),
                                ChildParentInterpolator[ChildIndex] + 1 * ORDER*ORDER, ORDER,
                                Exp, ORDER, PermExp, ORDER);

        for (unsigned int n=0; n<nnodes; ++n)	ChildExpansion[perm[2][n]] += PermExp[n];
    }
    // total flops count: 3 * ORDER*ORDER*ORDER * (2*ORDER-1)
555
};
556 557 558



559 560 561 562 563 564 565 566 567 568




/**
 * Particle to moment: application of \f$S_\ell(y,\bar y_n)\f$
 * (anterpolation, it is the transposed interpolation)
 */
template <int ORDER>
template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
569
inline void FChebInterpolator<ORDER>::applyP2M(const FPoint& center,
570 571 572
                                             const FReal width,
                                             FReal *const multipoleExpansion,
                                             const ContainerClass *const inParticles) const
573
{
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
    // set all multipole expansions to zero
    FBlas::setzero(nnodes, multipoleExpansion);

    // allocate stuff
    const map_glob_loc map(center, width);
    FPoint localPosition;

    FReal W1 = FReal(0.);
    FReal W2[3][ ORDER-1];
    FReal W4[3][(ORDER-1)*(ORDER-1)];
    FReal W8[   (ORDER-1)*(ORDER-1)*(ORDER-1)];
    for(unsigned int i=0; i<(ORDER-1); ++i) W2[0][i] = W2[1][i] = W2[2][i] = FReal(0.);
    for(unsigned int i=0; i<(ORDER-1)*(ORDER-1); ++i)	W4[0][i] = W4[1][i] = W4[2][i] = FReal(0.);
    for(unsigned int i=0; i<(ORDER-1)*(ORDER-1)*(ORDER-1); ++i)	W8[i] = FReal(0.);

    // loop over source particles
590 591 592 593 594
    const FReal*const physicalValues = inParticles->getPhysicalValues();
    const FReal*const positionsX = inParticles->getPositions()[0];
    const FReal*const positionsY = inParticles->getPositions()[1];
    const FReal*const positionsZ = inParticles->getPositions()[2];
    for(int idxPart = 0 ; idxPart < inParticles->getNbParticles() ; ++idxPart){
595
        // map global position to [-1,1]
596
        map(FPoint(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]), localPosition); // 15 flops
597 598 599 600 601 602 603 604 605 606 607 608 609 610

        FReal T_of_x[3][ORDER];
        T_of_x[0][0] = FReal(1.); T_of_x[0][1] = localPosition.getX();
        T_of_x[1][0] = FReal(1.); T_of_x[1][1] = localPosition.getY();
        T_of_x[2][0] = FReal(1.); T_of_x[2][1] = localPosition.getZ();
        const FReal x2 = FReal(2.) * T_of_x[0][1]; // 1 flop
        const FReal y2 = FReal(2.) * T_of_x[1][1]; // 1 flop
        const FReal z2 = FReal(2.) * T_of_x[2][1]; // 1 flop
        for (unsigned int j=2; j<ORDER; ++j) {
            T_of_x[0][j] = x2 * T_of_x[0][j-1] - T_of_x[0][j-2]; // 2 flops
            T_of_x[1][j] = y2 * T_of_x[1][j-1] - T_of_x[1][j-2]; // 2 flops
            T_of_x[2][j] = z2 * T_of_x[2][j-1] - T_of_x[2][j-2]; // 2 flops
        }

611
        const FReal weight = physicalValues[idxPart];
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 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
        W1 += weight; // 1 flop
        for (unsigned int i=1; i<ORDER; ++i) {
            const FReal wx = weight * T_of_x[0][i]; // 1 flop
            const FReal wy = weight * T_of_x[1][i]; // 1 flop
            const FReal wz = weight * T_of_x[2][i]; // 1 flop
            W2[0][i-1] += wx; // 1 flop
            W2[1][i-1] += wy; // 1 flop
            W2[2][i-1] += wz; // 1 flop
            for (unsigned int j=1; j<ORDER; ++j) {
                const FReal wxy = wx * T_of_x[1][j]; // 1 flop
                const FReal wxz = wx * T_of_x[2][j]; // 1 flop
                const FReal wyz = wy * T_of_x[2][j]; // 1 flop
                W4[0][(j-1)*(ORDER-1) + (i-1)] += wxy; // 1 flop
                W4[1][(j-1)*(ORDER-1) + (i-1)] += wxz; // 1 flop
                W4[2][(j-1)*(ORDER-1) + (i-1)] += wyz; // 1 flop
                for (unsigned int k=1; k<ORDER; ++k) {
                    const FReal wxyz = wxy * T_of_x[2][k]; // 1 flop
                    W8[(k-1)*(ORDER-1)*(ORDER-1) + (j-1)*(ORDER-1) + (i-1)] += wxyz; // 1 flop
                } // flops: (ORDER-1) * 2
            } // flops: (ORDER-1) * (6 + (ORDER-1) * 2)
        } // flops: (ORDER-1) * (6 + (ORDER-1) * (6 + (ORDER-1) * 2))

    } // flops: N * (18 + (ORDER-2) * 6 + (ORDER-1) * (6 + (ORDER-1) * (6 + (ORDER-1) * 2)))

    ////////////////////////////////////////////////////////////////////

    // loop over interpolation points
    FReal F2[3][ORDER];
    FReal F4[3][ORDER*ORDER];
    FReal F8[   ORDER*ORDER*ORDER];
    {
        // compute W2: 3 * ORDER*(2*(ORDER-1)-1) flops
        FBlas::gemv(ORDER, ORDER-1, FReal(1.), const_cast<FReal*>(T), W2[0], F2[0]);
        FBlas::gemv(ORDER, ORDER-1, FReal(1.), const_cast<FReal*>(T), W2[1], F2[1]);
        FBlas::gemv(ORDER, ORDER-1, FReal(1.), const_cast<FReal*>(T), W2[2], F2[2]);

        // compute W4: 3 * [ORDER*(ORDER-1)*(2*(ORDER-1)-1) + ORDER*ORDER*(2*(ORDER-1)-1)]
        FReal C[ORDER * (ORDER-1)];
        FBlas::gemmt(ORDER, ORDER-1, ORDER-1, FReal(1.), const_cast<FReal*>(T), ORDER, W4[0], ORDER-1, C,     ORDER);
        FBlas::gemmt(ORDER, ORDER-1, ORDER,   FReal(1.), const_cast<FReal*>(T), ORDER, C,     ORDER,   F4[0], ORDER);
        FBlas::gemmt(ORDER, ORDER-1, ORDER-1, FReal(1.), const_cast<FReal*>(T), ORDER, W4[1], ORDER-1, C,     ORDER);
        FBlas::gemmt(ORDER, ORDER-1, ORDER,   FReal(1.), const_cast<FReal*>(T), ORDER, C,     ORDER,   F4[1], ORDER);
        FBlas::gemmt(ORDER, ORDER-1, ORDER-1, FReal(1.), const_cast<FReal*>(T), ORDER, W4[2], ORDER-1, C,     ORDER);
        FBlas::gemmt(ORDER, ORDER-1, ORDER,   FReal(1.), const_cast<FReal*>(T), ORDER, C,     ORDER,   F4[2], ORDER);

        // compute W8: 3 * (2*(ORDER-1)-1) * [ORDER*(ORDER-1)*(ORDER-1) + ORDER*ORDER*(ORDER-1) + ORDER*ORDER*ORDER]
        FReal D[ORDER * (ORDER-1) * (ORDER-1)];
        FBlas::gemm(ORDER, ORDER-1, (ORDER-1)*(ORDER-1), FReal(1.),	const_cast<FReal*>(T), ORDER, W8, ORDER-1, D, ORDER);
        FReal E[(ORDER-1) * (ORDER-1) * ORDER];
        for (unsigned int s=0; s<perm0.size; ++s)	E[perm0.mni[s]] = D[perm0.imn[s]];
        FReal F[ORDER * (ORDER-1) * ORDER];
        FBlas::gemm(ORDER, ORDER-1, ORDER*(ORDER-1), FReal(1.), const_cast<FReal*>(T), ORDER, E, ORDER-1, F, ORDER);
        FReal G[(ORDER-1) * ORDER * ORDER];
        for (unsigned int s=0; s<perm1.size; ++s)	G[perm1.nij[s]] = F[perm1.jni[s]];
        FReal H[ORDER * ORDER * ORDER];
        FBlas::gemm(ORDER, ORDER-1, ORDER*ORDER, FReal(1.), const_cast<FReal*>(T), ORDER, G, ORDER-1, H, ORDER);
        for (unsigned int s=0; s<perm2.size; ++s)	F8[perm2.ijk[s]] = H[perm2.kij[s]];
    }

    // assemble multipole expansions
    for (unsigned int i=0; i<ORDER; ++i) {
        for (unsigned int j=0; j<ORDER; ++j) {
            for (unsigned int k=0; k<ORDER; ++k) {
                const unsigned int idx = k*ORDER*ORDER + j*ORDER + i;
                multipoleExpansion[idx] = (W1 +
                                                                     FReal(2.) * (F2[0][i] + F2[1][j] + F2[2][k]) +
                                                                     FReal(4.) * (F4[0][j*ORDER+i] + F4[1][k*ORDER+i] + F4[2][k*ORDER+j]) +
                                                                     FReal(8.) *  F8[idx]) / nnodes; // 11 * ORDER*ORDER*ORDER flops
            }
        }
    }
683

684 685 686
}


687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
///**
// * Particle to moment: application of \f$S_\ell(y,\bar y_n)\f$
// * (anterpolation, it is the transposed interpolation)
// */
//template <int ORDER>
//template <class ContainerClass>
//inline void FChebInterpolator<ORDER>::applyP2M(const FPoint& center,
//																							 const FReal width,
//																							 FReal *const multipoleExpansion,
//																							 const ContainerClass *const sourceParticles) const
//{
//	// set all multipole expansions to zero
//	FBlas::setzero(nnodes, multipoleExpansion);
//
//	// allocate stuff
//	const map_glob_loc map(center, width);
//	FPoint localPosition;
//	FReal T_of_x[ORDER][3];
//	FReal S[3], c1;
//	//
//	FReal xpx,ypy,zpz ;
//	c1 = FReal(8.) / nnodes ; // 1 flop
//	// loop over source particles
//	typename ContainerClass::ConstBasicIterator iter(*sourceParticles);
//	while(iter.hasNotFinished()){
//
//		// map global position to [-1,1]
//		map(iter.data().getPosition(), localPosition); // 15 flops
//
//		// evaluate chebyshev polynomials of source particle: T_o(x_i)
//		T_of_x[0][0] = FReal(1.);	T_of_x[1][0] = localPosition.getX();
//		T_of_x[0][1] = FReal(1.);	T_of_x[1][1] = localPosition.getY();
//		T_of_x[0][2] = FReal(1.);	T_of_x[1][2] = localPosition.getZ();
//		xpx = FReal(2.) * localPosition.getX() ; // 1 flop
//		ypy = FReal(2.) * localPosition.getY() ; // 1 flop
//		zpz = FReal(2.) * localPosition.getZ() ; // 1 flop
//
//		for (unsigned int o=2; o<ORDER; ++o) {
//			T_of_x[o][0] = xpx * T_of_x[o-1][0] - T_of_x[o-2][0]; // 2 flops
//			T_of_x[o][1] = ypy * T_of_x[o-1][1] - T_of_x[o-2][1];	// 2 flops
//			T_of_x[o][2] = zpz * T_of_x[o-1][2] - T_of_x[o-2][2]; // 2 flops
//		} // flops: (ORDER-1) * 6
//		
//		// anterpolate
//		const FReal sourceValue = iter.data().getPhysicalValue();
//		for (unsigned int n=0; n<nnodes; ++n) {
//			const unsigned int j[3] = {node_ids[n][0], node_ids[n][1], node_ids[n][2]};
//			S[0] = FReal(0.5) + T_of_x[1][0] * T_of_roots[1][j[0]]; // 2 flops 
//			S[1] = FReal(0.5) + T_of_x[1][1] * T_of_roots[1][j[1]]; // 2 flops
//			S[2] = FReal(0.5) + T_of_x[1][2] * T_of_roots[1][j[2]]; // 2 flops
//			for (unsigned int o=2; o<ORDER; ++o) {
//				S[0] += T_of_x[o][0] * T_of_roots[o][j[0]]; // 2 flops
//				S[1] += T_of_x[o][1] * T_of_roots[o][j[1]]; // 2 flops
//				S[2] += T_of_x[o][2] * T_of_roots[o][j[2]]; // 2 flops
//			} // flops: (ORDER-2) * 6
//
//			// gather contributions
//			multipoleExpansion[n]	+= c1 *	S[0] * S[1] * S[2] *	sourceValue; // 4 flops
//		} // flops: ORDER*ORDER*ORDER * (10 + (ORDER-2) * 6)
//
//		// increment source iterator
//		iter.gotoNext();
//	} // flops: M * (18 + (ORDER-1) * 6 + ORDER*ORDER*ORDER * (10 + (ORDER-2) * 6))
//}



754 755 756 757 758
/**
 * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ (interpolation)
 */
template <int ORDER>
template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
759
inline void FChebInterpolator<ORDER>::applyL2P(const FPoint& center,
760 761 762
                                             const FReal width,
                                             const FReal *const localExpansion,
                                             ContainerClass *const inParticles) const
763
{
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    FReal f1;
    FReal W2[3][ ORDER-1];
    FReal W4[3][(ORDER-1)*(ORDER-1)];
    FReal W8[   (ORDER-1)*(ORDER-1)*(ORDER-1)];
    { // sum over interpolation points
        f1 = FReal(0.);
        for(unsigned int i=0; i<ORDER-1; ++i)	                   W2[0][i] = W2[1][i] = W2[2][i] = FReal(0.);
        for(unsigned int i=0; i<(ORDER-1)*(ORDER-1); ++i)        W4[0][i] = W4[1][i] = W4[2][i] = FReal(0.);
        for(unsigned int i=0; i<(ORDER-1)*(ORDER-1)*(ORDER-1); ++i)	W8[i] = FReal(0.);

        for (unsigned int idx=0; idx<nnodes; ++idx) {
            const unsigned int i = node_ids[idx][0];
            const unsigned int j = node_ids[idx][1];
            const unsigned int k = node_ids[idx][2];

            f1 += localExpansion[idx]; // 1 flop

            for (unsigned int l=0; l<ORDER-1; ++l) {
                const FReal wx = T[l*ORDER+i] * localExpansion[idx]; // 1 flops
                const FReal wy = T[l*ORDER+j] * localExpansion[idx]; // 1 flops
                const FReal wz = T[l*ORDER+k] * localExpansion[idx]; // 1 flops
                W2[0][l] += wx; // 1 flops
                W2[1][l] += wy; // 1 flops
                W2[2][l] += wz; // 1 flops
                for (unsigned int m=0; m<ORDER-1; ++m) {
                    const FReal wxy = wx * T[m*ORDER + j]; // 1 flops
                    const FReal wxz = wx * T[m*ORDER + k]; // 1 flops
                    const FReal wyz = wy * T[m*ORDER + k]; // 1 flops
                    W4[0][m*(ORDER-1)+l] += wxy; // 1 flops
                    W4[1][m*(ORDER-1)+l] += wxz; // 1 flops
                    W4[2][m*(ORDER-1)+l] += wyz; // 1 flops
                    for (unsigned int n=0; n<ORDER-1; ++n) {
                        const FReal wxyz = wxy * T[n*ORDER + k]; // 1 flops
                        W8[n*(ORDER-1)*(ORDER-1) + m*(ORDER-1) + l]	+= wxyz; // 1 flops
                    } // (ORDER-1) * 2 flops
                } // (ORDER-1) * (6 + (ORDER-1)*2) flops
            } // (ORDER-1) * (6 + (ORDER-1) * (6 + (ORDER-1)*2)) flops
        } // ORDER*ORDER*ORDER * (1 + (ORDER-1) * (6 + (ORDER-1) * (6 + (ORDER-1)*2))) flops

    }


    // loop over particles
    const map_glob_loc map(center, width);
    FPoint localPosition;
809 810 811 812 813 814 815 816 817 818 819

    //const FReal*const physicalValues = inParticles->getPhysicalValues();
    const FReal*const positionsX = inParticles->getPositions()[0];
    const FReal*const positionsY = inParticles->getPositions()[1];
    const FReal*const positionsZ = inParticles->getPositions()[2];
    //FReal*const forcesX = inParticles->getForcesX();
    //FReal*const forcesY = inParticles->getForcesY();
    //FReal*const forcesZ = inParticles->getForcesZ();
    FReal*const potentials = inParticles->getPotentials();

    for(int idxPart = 0 ; idxPart < inParticles->getNbParticles() ; ++ idxPart){
820 821

        // map global position to [-1,1]
822
        map(FPoint(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]), localPosition); // 15 flops
823

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
        FReal T_of_x[3][ORDER];
        {
            T_of_x[0][0] = FReal(1.); T_of_x[0][1] = localPosition.getX();
            T_of_x[1][0] = FReal(1.); T_of_x[1][1] = localPosition.getY();
            T_of_x[2][0] = FReal(1.); T_of_x[2][1] = localPosition.getZ();
            const FReal x2 = FReal(2.) * T_of_x[0][1]; // 1 flop
            const FReal y2 = FReal(2.) * T_of_x[1][1]; // 1 flop
            const FReal z2 = FReal(2.) * T_of_x[2][1]; // 1 flop
            for (unsigned int j=2; j<ORDER; ++j) {
                T_of_x[0][j] = x2 * T_of_x[0][j-1] - T_of_x[0][j-2]; // 2 flops
                T_of_x[1][j] = y2 * T_of_x[1][j-1] - T_of_x[1][j-2]; // 2 flops
                T_of_x[2][j] = z2 * T_of_x[2][j-1] - T_of_x[2][j-2]; // 2 flops
            }
        }

        // interpolate and increment target value
840
        FReal targetValue = potentials[idxPart];
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
        {
            FReal f2, f4, f8;
            {
                f2 = f4 = f8 = FReal(0.);
                for (unsigned int l=1; l<ORDER; ++l) {
                    f2 +=
                        T_of_x[0][l] * W2[0][l-1] +
                        T_of_x[1][l] * W2[1][l-1] +
                        T_of_x[2][l] * W2[2][l-1]; // 6 flops
                    for (unsigned int m=1; m<ORDER; ++m) {
                        f4 +=
                            T_of_x[0][l] * T_of_x[1][m] * W4[0][(m-1)*(ORDER-1)+(l-1)] +
                            T_of_x[0][l] * T_of_x[2][m] * W4[1][(m-1)*(ORDER-1)+(l-1)] +
                            T_of_x[1][l] * T_of_x[2][m] * W4[2][(m-1)*(ORDER-1)+(l-1)]; // 9 flops
                        for (unsigned int n=1; n<ORDER; ++n) {
                            f8 +=
                                T_of_x[0][l] * T_of_x[1][m] * T_of_x[2][n] *
                                W8[(n-1)*(ORDER-1)*(ORDER-1) + (m-1)*(ORDER-1) + (l-1)];
                        } // ORDER * 4 flops
                    } // ORDER * (9 + ORDER * 4) flops
                } // ORDER * (ORDER * (9 + ORDER * 4)) flops
            }
            targetValue = (f1 + FReal(2.)*f2 + FReal(4.)*f4 + FReal(8.)*f8) / nnodes; // 7 flops
        } // 7 + ORDER * (ORDER * (9 + ORDER * 4)) flops

        // set potential
867
        potentials[idxPart] += (targetValue);
868
    } // N * (7 + ORDER * (ORDER * (9 + ORDER * 4))) flops
869 870 871
}


872 873 874 875 876 877 878
//	FReal F2[3][ORDER-1];
//	FBlas::gemtv(ORDER, ORDER-1, FReal(1.), const_cast<FReal*>(T), const_cast<FReal*>(localExpansion), F2[0]);
//	for (unsigned int i=1; i<ORDER*ORDER; ++i)
//		FBlas::gemtva(ORDER, ORDER-1, FReal(1.), const_cast<FReal*>(T),
//									const_cast<FReal*>(localExpansion) + ORDER*i, F2[0]);
//	for (unsigned int i=0; i<ORDER-1; ++i)
//		std::cout << W2[0][i] << "\t" << F2[0][i] << std::endl;
879

880 881 882 883 884 885 886 887
//	FReal F2[(ORDER-1) * ORDER*ORDER];
//	FBlas::gemtm(ORDER, ORDER-1, ORDER*ORDER, FReal(1.), const_cast<FReal*>(T), ORDER,
//							 const_cast<FReal*>(localExpansion), ORDER, F2, ORDER-1);
//	FReal F[ORDER-1]; FBlas::setzero(ORDER-1, F);
//	for (unsigned int i=0; i<ORDER-1; ++i)
//		for (unsigned int j=0; j<ORDER*ORDER; ++j) F[i] += F2[j*(ORDER-1) + i];
//	for (unsigned int i=0; i<ORDER-1; ++i)
//		std::cout << W2[0][i] << "\t" << F[i] << std::endl;
888 889


890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 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
///**
// * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ (interpolation)
// */
//template <int ORDER>
//template <class ContainerClass>
//inline void FChebInterpolator<ORDER>::applyL2P(const FPoint& center,
//																							 const FReal width,
//																							 const FReal *const localExpansion,
//																							 ContainerClass *const localParticles) const
//{
//	// allocate stuff
//	const map_glob_loc map(center, width);
//	FPoint localPosition;
//	FReal T_of_x[ORDER][3];
//	FReal xpx,ypy,zpz ;
//	FReal S[3],c1;
//	//
//	c1 = FReal(8.) / nnodes ;
//	typename ContainerClass::BasicIterator iter(*localParticles);
//	while(iter.hasNotFinished()){
//			
//		// map global position to [-1,1]
//		map(iter.data().getPosition(), localPosition); // 15 flops
//
//		// evaluate chebyshev polynomials of source particle: T_o(x_i)
//		T_of_x[0][0] = FReal(1.);	T_of_x[1][0] = localPosition.getX();
//		T_of_x[0][1] = FReal(1.);	T_of_x[1][1] = localPosition.getY();
//		T_of_x[0][2] = FReal(1.);	T_of_x[1][2] = localPosition.getZ();
//		xpx = FReal(2.) * localPosition.getX() ; // 1 flop
//		ypy = FReal(2.) * localPosition.getY() ; // 1 flop
//		zpz = FReal(2.) * localPosition.getZ() ; // 1 flop
//		for (unsigned int o=2; o<ORDER; ++o) {
//			T_of_x[o][0] = xpx * T_of_x[o-1][0] - T_of_x[o-2][0]; // 2 flop
//			T_of_x[o][1] = ypy * T_of_x[o-1][1] - T_of_x[o-2][1]; // 2 flop
//			T_of_x[o][2] = zpz * T_of_x[o-1][2] - T_of_x[o-2][2]; // 2 flop
//		} // (ORDER-2) * 6 flops
//
//		// interpolate and increment target value
//		FReal targetValue = iter.data().getPotential();
//		for (unsigned int n=0; n<nnodes; ++n) {
//			const unsigned int j[3] = {node_ids[n][0], node_ids[n][1], node_ids[n][2]};
//			S[0] = T_of_x[1][0] * T_of_roots[1][j[0]]; // 1 flops
//			S[1] = T_of_x[1][1] * T_of_roots[1][j[1]]; // 1 flops
//			S[2] = T_of_x[1][2] * T_of_roots[1][j[2]]; // 1 flops
//			for (unsigned int o=2; o<ORDER; ++o) {
//				S[0] += T_of_x[o][0] * T_of_roots[o][j[0]]; // 2 flops
//				S[1] += T_of_x[o][1] * T_of_roots[o][j[1]]; // 2 flops
//				S[2] += T_of_x[o][2] * T_of_roots[o][j[2]]; // 2 flops
//			} // (ORDER-2) * 6 flops 
//			// gather contributions
//			S[0] += FReal(0.5); // 1 flops
//			S[1] += FReal(0.5); // 1 flops
//			S[2] += FReal(0.5); // 1 flops
//			targetValue	+= S[0] * S[1] * S[2] * localExpansion[n]; // 4 flops
//		} // ORDER*ORDER*ORDER * (10 + (ORDER-2) * 6) flops
//		// scale
//		targetValue *= c1; // 1 flops
//
//		// set potential
//		iter.data().setPotential(targetValue);
//
//		// increment target iterator
//		iter.gotoNext();
//	} // N * ORDER*ORDER*ORDER * (10 + (ORDER-2) * 6) flops
//}
955 956 957 958 959 960






Matthias Messner's avatar
Matthias Messner committed
961 962 963 964 965 966
/**
 * Local to particle operation: application of \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
 */
template <int ORDER>
template <class ContainerClass>
inline void FChebInterpolator<ORDER>::applyL2PGradient(const FPoint& center,
967 968 969
                                                     const FReal width,
                                                     const FReal *const localExpansion,
                                                     ContainerClass *const inParticles) const
Matthias Messner's avatar
Matthias Messner committed
970
{
971 972 973 974 975 976 977 978 979 980 981 982 983
    ////////////////////////////////////////////////////////////////////
    // TENSOR-PRODUCT INTERPOLUTION NOT IMPLEMENTED YET HERE!!! ////////
    ////////////////////////////////////////////////////////////////////

    // setup local to global mapping
    const map_glob_loc map(center, width);
    FPoint Jacobian;
    map.computeJacobian(Jacobian);
    const FReal jacobian[3] = {Jacobian.getX(), Jacobian.getY(), Jacobian.getZ()};
    FPoint localPosition;
    FReal T_of_x[ORDER][3];
    FReal U_of_x[ORDER][3];
    FReal P[3];
Matthias Messner's avatar
Matthias Messner committed
984

985 986 987 988 989 990 991 992 993 994
    const FReal*const physicalValues = inParticles->getPhysicalValues();
    const FReal*const positionsX = inParticles->getPositions()[0];
    const FReal*const positionsY = inParticles->getPositions()[1];
    const FReal*const positionsZ = inParticles->getPositions()[2];
    FReal*const forcesX = inParticles->getForcesX();
    FReal*const forcesY = inParticles->getForcesY();
    FReal*const forcesZ = inParticles->getForcesZ();
    //FReal*const potentials = inParticles->getPotentials();

    for(int idxPart = 0 ; idxPart < inParticles->getNbParticles() ; ++ idxPart){
995 996

        // map global position to [-1,1]
997
        map(FPoint(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]), localPosition);
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

        // evaluate chebyshev polynomials of source particle
        // T_0(x_i) and T_1(x_i)
        T_of_x[0][0] = FReal(1.);	T_of_x[1][0] = localPosition.getX();
        T_of_x[0][1] = FReal(1.);	T_of_x[1][1] = localPosition.getY();
        T_of_x[0][2] = FReal(1.);	T_of_x[1][2] = localPosition.getZ();
        // U_0(x_i) and U_1(x_i)
        U_of_x[0][0] = FReal(1.);	U_of_x[1][0] = localPosition.getX() * FReal(2.);
        U_of_x[0][1] = FReal(1.);	U_of_x[1][1] = localPosition.getY() * FReal(2.);
        U_of_x[0][2] = FReal(1.);	U_of_x[1][2] = localPosition.getZ() * FReal(2.);
        for (unsigned int o=2; o<ORDER; ++o) {
            // T_o(x_i)
            T_of_x[o][0] = FReal(2.)*localPosition.getX()*T_of_x[o-1][0] - T_of_x[o-2][0];
            T_of_x[o][1] = FReal(2.)*localPosition.getY()*T_of_x[o-1][1] - T_of_x[o-2][1];
            T_of_x[o][2] = FReal(2.)*localPosition.getZ()*T_of_x[o-1][2] - T_of_x[o-2][2];
            // U_o(x_i)
            U_of_x[o][0] = FReal(2.)*localPosition.getX()*U_of_x[o-1][0] - U_of_x[o-2][0];
            U_of_x[o][1] = FReal(2.)*localPosition.getY()*U_of_x[o-1][1] - U_of_x[o-2][1];
            U_of_x[o][2] = FReal(2.)*localPosition.getZ()*U_of_x[o-1][2] - U_of_x[o-2][2];
        }

        // scale, because dT_o/dx = oU_{o-1}
        for (unsigned int o=2; o<ORDER; ++o) {
            U_of_x[o-1][0] *= FReal(o);
            U_of_x[o-1][1] *= FReal(o);
            U_of_x[o-1][2] *= FReal(o);
        }

        // apply P and increment forces
        FReal forces[3] = {FReal(0.), FReal(0.), FReal(0.)};
        for (unsigned int n=0; n<nnodes; ++n) {

            // tensor indices of chebyshev nodes
            const unsigned int j[3] = {node_ids[n][0], node_ids[n][1], node_ids[n][2]};

            // f0 component //////////////////////////////////////
            P[0] = U_of_x[0][0] * T_of_roots[1][j[0]];
            P[1] = T_of_x[1][1] * T_of_roots[1][j[1]];
            P[2] = T_of_x[1][2] * T_of_roots[1][j[2]];
            for (unsigned int o=2; o<ORDER; ++o) {
                P[0] += U_of_x[o-1][0] * T_of_roots[o][j[0]];
                P[1] += T_of_x[o  ][1] * T_of_roots[o][j[1]];
                P[2] += T_of_x[o  ][2] * T_of_roots[o][j[2]];
            }
            P[0] *= FReal(2.);
            P[1] *= FReal(2.); P[1] += FReal(1.);
            P[2] *= FReal(2.); P[2] += FReal(1.);
            forces[0]	+= P[0] * P[1] * P[2] * localExpansion[n];

            // f1 component //////////////////////////////////////
            P[0] = T_of_x[1][0] * T_of_roots[1][j[0]];
            P[1] = U_of_x[0][1] * T_of_roots[1][j[1]];
            P[2] = T_of_x[1][2] * T_of_roots[1][j[2]];
            for (unsigned int o=2; o<ORDER; ++o) {
                P[0] += T_of_x[o  ][0] * T_of_roots[o][j[0]];
                P[1] += U_of_x[o-1][1] * T_of_roots[o][j[1]];
                P[2] += T_of_x[o  ][2] * T_of_roots[o][j[2]];
            }
            P[0] *= FReal(2.); P[0] += FReal(1.);
            P[1] *= FReal(2.);
            P[2] *= FReal(2.); P[2] += FReal(1.);
            forces[1]	+= P[0] * P[1] * P[2] * localExpansion[n];

            // f2 component //////////////////////////////////////
            P[0] = T_of_x[1][0] * T_of_roots[1][j[0]];
            P[1] = T_of_x[1][1] * T_of_roots[1][j[1]];
            P[2] = U_of_x[0][2] * T_of_roots[1][j[2]];
            for (unsigned int o=2; o<ORDER; ++o) {
                P[0] += T_of_x[o  ][0] * T_of_roots[o][j[0]];
                P[1] += T_of_x[o  ][1] * T_of_roots[o][j[1]];
                P[2] += U_of_x[o-1][2] * T_of_roots[o][j[2]];
            }
            P[0] *= FReal(2.); P[0] += FReal(1.);
            P[1] *= FReal(2.); P[1] += FReal(1.);
            P[2] *= FReal(2.);
            forces[2]	+= P[0] * P[1] * P[2] * localExpansion[n];
        }

        // scale forces
        forces[0] *= jacobian[0] / nnodes;
        forces[1] *= jacobian[1] / nnodes;
        forces[2] *= jacobian[2] / nnodes;

        // set computed forces
1082 1083 1084
        forcesX[idxPart] += forces[0] * physicalValues[idxPart];
        forcesY[idxPart] += forces[1] * physicalValues[idxPart];
        forcesZ[idxPart] += forces[2] * physicalValues[idxPart];
1085
    }
Matthias Messner's avatar
Matthias Messner committed
1086
}
1087

1088 1089 1090 1091 1092 1093 1094

/**
 * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ and
 * \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
 */
template <int ORDER>
template <class ContainerClass>
COULAUD Olivier's avatar
COULAUD Olivier committed
1095
inline void FChebInterpolator<ORDER>::applyL2PTotal(const FPoint& center,
1096 1097 1098
                                                    const FReal width,
                                                    const FReal *const localExpansion,
                                                    ContainerClass *const inParticles) const
1099
{
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    FReal f1;
    FReal W2[3][ ORDER-1];
    FReal W4[3][(ORDER-1)*(ORDER-1)];
    FReal W8[   (ORDER-1)*(ORDER-1)*(ORDER-1)];

    //{ // sum over interpolation points
    //	f1 = FReal(0.);
    //	for(unsigned int i=0; i<ORDER-1; ++i)	                   W2[0][i] = W2[1][i] = W2[2][i] = FReal(0.);
    //	for(unsigned int i=0; i<(ORDER-1)*(ORDER-1); ++i)        W4[0][i] = W4[1][i] = W4[2][i] = FReal(0.);
    //	for(unsigned int i=0; i<(ORDER-1)*(ORDER-1)*(ORDER-1); ++i)	W8[i] = FReal(0.);
    //
    //	for (unsigned int idx=0; idx<nnodes; ++idx) {
    //		const unsigned int i = node_ids[idx][0];
    //		const unsigned int j = node_ids[idx][1];
    //		const unsigned int k = node_ids[idx][2];
    //
    //		f1 += localExpansion[idx]; // 1 flop
    //
    //		for (unsigned int l=0; l<ORDER-1; ++l) {
    //			const FReal wx = T[l*ORDER+i] * localExpansion[idx]; // 1 flops
    //			const FReal wy = T[l*ORDER+j] * localExpansion[idx]; // 1 flops
    //			const FReal wz = T[l*ORDER+k] * localExpansion[idx]; // 1 flops
    //			W2[0][l] += wx; // 1 flops
    //			W2[1][l] += wy; // 1 flops
    //			W2[2][l] += wz; // 1 flops
    //			for (unsigned int m=0; m<ORDER-1; ++m) {
    //				const FReal wxy = wx * T[m*ORDER + j]; // 1 flops
    //				const FReal wxz = wx * T[m*ORDER + k]; // 1 flops
    //				const FReal wyz = wy * T[m*ORDER + k]; // 1 flops
    //				W4[0][m*(ORDER-1)+l] += wxy; // 1 flops
    //				W4[1][m*(ORDER-1)+l] += wxz; // 1 flops
    //				W4[2][m*(ORDER-1)+l] += wyz; // 1 flops
    //				for (unsigned int n=0; n<ORDER-1; ++n) {
    //					const FReal wxyz = wxy * T[n*ORDER + k]; // 1 flops
    //					W8[n*(ORDER-1)*(ORDER-1) + m*(ORDER-1) + l]	+= wxyz; // 1 flops
    //				} // (ORDER-1) * 2 flops
    //			} // (ORDER-1) * (6 + (ORDER-1)*2) flops
    //		} // (ORDER-1) * (6 + (ORDER-1) * (6 + (ORDER-1)*2)) flops
    //
    //	} // ORDER*ORDER*ORDER * (1 + (ORDER-1) * (6 + (ORDER-1) * (6 + (ORDER-1)*2))) flops
    //
    //}

    {
        // for W2
        FReal lE[nnodes];
        FReal F2[(ORDER-1) * ORDER*ORDER];
        // for W4
        FReal F4[ORDER * ORDER*(ORDER-1)];
        FReal G4[(ORDER-1) * ORDER*(ORDER-1)];
        // for W8
        FReal G8[ORDER * (ORDER-1)*(ORDER-1)];

        // sum local expansions
        f1 = FReal(0.);
        for (unsigned int idx=0; idx<nnodes; ++idx)	f1 += localExpansion[idx]; // 1 flop

        //////////////////////////////////////////////////////////////////
        // IMPORTANT: NOT CHANGE ORDER OF COMPUTATIONS!!! ////////////////
        //////////////////////////////////////////////////////////////////

        // W2[0] ///////////////// (ORDER-1)*ORDER*ORDER * 2*ORDER
        FBlas::gemtm(ORDER, ORDER-1, ORDER*ORDER, FReal(1.), const_cast<FReal*>(T), ORDER,
                                 const_cast<FReal*>(localExpansion), ORDER, F2, ORDER-1);
        for (unsigned int l=0; l<ORDER-1; ++l) { W2[0][l] = F2[l];
            for (unsigned int j=1; j<ORDER*ORDER; ++j) W2[0][l] += F2[j*(ORDER-1) + l];	}
        // W4[0] ///////////////// ORDER*(ORDER-1)*(ORDER-1) + 2*ORDER
        perm5.permute(F2, F4);
        FBlas::gemtm(ORDER, ORDER-1, ORDER*(ORDER-1), FReal(1.), const_cast<FReal*>(T), ORDER, F4, ORDER, G4, ORDER-1);
        for (unsigned int l=0; l<ORDER-1; ++l)
            for (unsigned int m=0; m<ORDER-1; ++m) { W4[0][m*(ORDER-1)+l] = G4[l*ORDER*(ORDER-1) + m];
                for (unsigned int k=1; k<ORDER; ++k) W4[0][m*(ORDER-1)+l] += G4[l*ORDER*(ORDER-1) + k*(ORDER-1) + m];	}
        // W8 //////////////////// (ORDER-1)*(ORDER-1)*(ORDER-1) * (2*ORDER-1)
        perm8.permute(G4, G8);
        FReal F8[(ORDER-1)*(ORDER-1)*(ORDER-1)];
        FBlas::gemtm(ORDER, ORDER-1, (ORDER-1)*(ORDER-1), FReal(1.), const_cast<FReal*>(T), ORDER, G8, ORDER, F8, ORDER-1);
        perm9.permute(F8, W8);
        // W4[1] ///////////////// ORDER*(ORDER-1)*(ORDER-1) + 2*ORDER
        perm6.permute(F2, F4);
        FBlas::gemtm(ORDER, ORDER-1, (ORDER-1)*ORDER, FReal(1.), const_cast<FReal*>(T), ORDER, F4, ORDER, G4, ORDER-1);
        for (unsigned int l=0; l<ORDER-1; ++l)
            for (unsigned int n=0; n<ORDER-1; ++n) { W4[1][n*(ORDER-1)+l] = G4[l*(ORDER-1) + n];
                for (unsigned int j=1; j<ORDER; ++j) W4[1][n*(ORDER-1)+l] += G4[j*(ORDER-1)*(ORDER-1) + l*(ORDER-1) + n];	}
        // W2[1] ///////////////// (ORDER-1)*ORDER*ORDER * 2*ORDER
        perm3.permute(localExpansion, lE);
        FBlas::gemtm(ORDER, ORDER-1, ORDER*ORDER, FReal(1.), const_cast<FReal*>(T), ORDER, lE, ORDER, F2, ORDER-1);
        for (unsigned int i=0; i<ORDER-1; ++i) { W2[1][i] = F2[i];
            for (unsigned int j=1; j<ORDER*ORDER; ++j) W2[1][i] += F2[j*(ORDER-1) + i]; }
        // W4[2] ///////////////// ORDER*(ORDER-1)*(ORDER-1) + 2*ORDER
        perm7.permute(F2, F4);
        FBlas::gemtm(ORDER, ORDER-1, (ORDER-1)*ORDER, FReal(1.), const_cast<FReal*>(T), ORDER, F4, ORDER, G4, ORDER-1);
        for (unsigned int m=0; m<ORDER-1; ++m)
            for (unsigned int n=0; n<ORDER-1; ++n) { W4[2][n*(ORDER-1)+m] = G4[m*ORDER*(ORDER-1) + n];
                for (unsigned int i=1; i<ORDER; ++i) W4[2][n*(ORDER-1)+m] += G4[m*ORDER*(ORDER-1) + i*(ORDER-1) + n];	}
        // W2[2] ///////////////// (ORDER-1)*ORDER*ORDER * 2*ORDER
        perm4.permute(localExpansion, lE);
        FBlas::gemtm(ORDER, ORDER-1, ORDER*ORDER, FReal(1.), const_cast<FReal*>(T), ORDER, lE, ORDER, F2, ORDER-1);
        for (unsigned int i=0; i<ORDER-1; ++i) { W2[2][i] = F2[i];
            for (unsigned int j=1; j<ORDER*ORDER; ++j) W2[2][i] += F2[j*(ORDER-1) + i]; }
    }


    // loop over particles
    const map_glob_loc map(center, width);
    FPoint Jacobian;
    map.computeJacobian(Jacobian); // 6 flops
    const FReal jacobian[3] = {Jacobian.getX(), Jacobian.getY(), Jacobian.getZ()};
    FPoint localPosition;
1208

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    const FReal*const physicalValues = inParticles->getPhysicalValues();
    const FReal*const positionsX = inParticles->getPositions()[0];
    const FReal*const positionsY = inParticles->getPositions()[1];
    const FReal*const positionsZ = inParticles->getPositions()[2];
    FReal*const forcesX = inParticles->getForcesX();
    FReal*const forcesY = inParticles->getForcesY();
    FReal*const forcesZ = inParticles->getForcesZ();
    FReal*const potentials = inParticles->getPotentials();

    for(int idxPart = 0 ; idxPart < inParticles->getNbParticles() ; ++ idxPart){
1219 1220

        // map global position to [-1,1]
1221
        map(FPoint(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]), localPosition); // 15 flops
1222

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
        FReal U_of_x[3][ORDER];
        FReal T_of_x[3][ORDER];
        {
            T_of_x[0][0] = FReal(1.); T_of_x[0][1] = localPosition.getX();
            T_of_x[1][0] = FReal(1.); T_of_x[1][1] = localPosition.getY();
            T_of_x[2][0] = FReal(1.); T_of_x[2][1] = localPosition.getZ();
            const FReal x2 = FReal(2.) * T_of_x[0][1]; // 1 flop
            const FReal y2 = FReal(2.) * T_of_x[1][1]; // 1 flop
            const FReal z2 = FReal(2.) * T_of_x[2][1]; // 1 flop
            U_of_x[0][0] = FReal(1.);	U_of_x[0][1] = x2;
            U_of_x[1][0] = FReal(1.);	U_of_x[1][1] = y2;
            U_of_x[2][0] = FReal(1.);	U_of_x[2][1] = z2;
            for (unsigned int j=2; j<ORDER; ++j) {
                T_of_x[0][j] = x2 * T_of_x[0][j-1] - T_of_x[0][j-2]; // 2 flops
                T_of_x[1][j] = y2 * T_of_x[1][j-1] - T_of_x[1][j-2]; // 2 flops
                T_of_x[2][j] = z2 * T_of_x[2][j-1] - T_of_x[2][j-2]; // 2 flops
                U_of_x[0][j] = x2 * U_of_x[0][j-1] - U_of_x[0][j-2]; // 2 flops
                U_of_x[1][j] = y2 * U_of_x[1][j-1] - U_of_x[1][j-2]; // 2 flops
                U_of_x[2][j] = z2 * U_of_x[2][j-1] - U_of_x[2][j-2]; // 2 flops
            }
            // scale, because dT_j/dx = jU_{j-1}
            for (unsigned int j=2; j<ORDER; ++j) {
                U_of_x[0][j-1] *= FReal(j); // 1 flops
                U_of_x[1][j-1] *= FReal(j); // 1 flops
                U_of_x[2][j-1] *= FReal(j); // 1 flops
            }
        } // 3 + (ORDER-2)*15

        // apply P and increment forces
        FReal potential = FReal(0.);
        FReal forces[3] = {FReal(0.), FReal(0.), FReal(0.)};
        {
            FReal f2[4], f4[4], f8[4];
            for (unsigned int i=0; i<4; ++i) f2[i] = f4[i] = f8[i] = FReal(0.);
            {
                for (unsigned int l=1; l<ORDER; ++l) {
                    const FReal w2[3] = {W2[0][l-1], W2[1][l-1], W2[2][l-1]};
                    f2[0] += T_of_x[0][l  ] * w2[0] + T_of_x[1][l] * w2[1] + T_of_x[2][l] * w2[2]; // 6 flops
                    f2[1] += U_of_x[0][l-1] * w2[0]; // 2 flops
                    f2[2] += U_of_x[1][l-1] * w2[1]; // 2 flops
                    f2[3] += U_of_x[2][l-1] * w2[2]; // 2 flops
                    for (unsigned int m=1; m<ORDER; ++m) {
                        const unsigned int w4idx = (m-1)*(ORDER-1)+(l-1);
                        const FReal w4[3] = {W4[0][w4idx], W4[1][w4idx], W4[2][w4idx]};
                        f4[0] +=
                            T_of_x[0][l] * T_of_x[1][m] * w4[0] +
                            T_of_x[0][l] * T_of_x[2][m] * w4[1] +
                            T_of_x[1][l] * T_of_x[2][m] * w4[2]; // 9 flops
                        f4[1] += U_of_x[0][l-1] * T_of_x[1][m]   * w4[0] + U_of_x[0][l-1] * T_of_x[2][m]   * w4[1]; // 6 flops
                        f4[2] += T_of_x[0][l]   * U_of_x[1][m-1] * w4[0] + U_of_x[1][l-1] * T_of_x[2][m]   * w4[2]; // 6 flops
                        f4[3] += T_of_x[0][l]   * U_of_x[2][m-1] * w4[1] + T_of_x[1][l]   * U_of_x[2][m-1] * w4[2]; // 6 flops
                        for (unsigned int n=1; n<ORDER; ++n) {
                            const FReal w8 = W8[(n-1)*(ORDER-1)*(ORDER-1) + (m-1)*(ORDER-1) + (l-1)];
                            f8[0] += T_of_x[0][l]   * T_of_x[1][m]   * T_of_x[2][n]   * w8; // 4 flops
                            f8[1] += U_of_x[0][l-1] * T_of_x[1][m]   * T_of_x[2][n]   * w8; // 4 flops
                            f8[2] += T_of_x[0][l]   * U_of_x[1][m-1] * T_of_x[2][n]   * w8; // 4 flops
                            f8[3] += T_of_x[0][l]   * T_of_x[1][m]   * U_of_x[2][n-1] * w8; // 4 flops
                        } // (ORDER-1) * 16 flops
                    } // (ORDER-1) * (27 + (ORDER-1) * 16) flops
                } // (ORDER-1) * ((ORDER-1) * (27 + (ORDER-1) * 16)) flops
            }
            potential = (f1 + FReal(2.)*f2[0] + FReal(4.)*f4[0] + FReal(8.)*f8[0]) / nnodes; // 7 flops
            forces[0] = (     FReal(2.)*f2[1] + FReal(4.)*f4[1] + FReal(8.)*f8[1]) * jacobian[0] / nnodes; // 7 flops
            forces[1] = (     FReal(2.)*f2[2] + FReal(4.)*f4[2] + FReal(8.)*f8[2]) * jacobian[1] / nnodes; // 7 flops
            forces[2] = (     FReal(2.)*f2[3] + FReal(4.)*f4[3] + FReal(8.)*f8[3]) * jacobian[2] / nnodes; // 7 flops
        } // 28 + (ORDER-1) * ((ORDER-1) * (27 + (ORDER-1) * 16)) flops

        // set computed potential
1291
        potentials[idxPart] += (potential); // 1 flop
1292

1293
        // set computed forces
1294 1295 1296
        forcesX[idxPart] += forces[0] * physicalValues[idxPart];
        forcesY[idxPart] += forces[1] * physicalValues[idxPart];
        forcesZ[idxPart] += forces[2] * physicalValues[idxPart]; // 6 flops
1297

1298
    } // N * (38 + (ORDER-2)*15 + (ORDER-1)*((ORDER-1) * (27 + (ORDER-1) * 16))) + 6 flops
1299 1300 1301
}


1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
///**
// * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ and
// * \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
// */
//template <int ORDER>
//template <class ContainerClass>
//inline void FChebInterpolator<ORDER>::applyL2PTotal(const FPoint& center,
//																										const FReal width,
//																										const FReal *const localExpansion,
//																										ContainerClass *const localParticles) const
//{
//	// setup local to global mapping
//	const map_glob_loc map(center, width);
//	FPoint Jacobian;
//	map.computeJacobian(Jacobian); // 6 flops
//	const FReal jacobian[3] = {Jacobian.getX(), Jacobian.getY(), Jacobian.getZ()}; 
//	FPoint localPosition;
//	FReal T_of_x[ORDER][3];
//	FReal U_of_x[ORDER][3];
//	FReal P[6];
//	//
//	FReal xpx,ypy,zpz ;
//	FReal c1 = FReal(8.0) / nnodes; // 1 flop
//	//
//	typename ContainerClass::BasicIterator iter(*localParticles);
//	while(iter.hasNotFinished()){
//			
//		// map global position to [-1,1]
//		map(iter.data().getPosition(), localPosition); // 15 flops
//			
//		// evaluate chebyshev polynomials of source particle
//		// T_0(x_i) and T_1(x_i)
//		xpx = FReal(2.) * localPosition.getX(); // 1 flop
//		ypy = FReal(2.) * localPosition.getY(); // 1 flop
//		zpz = FReal(2.) * localPosition.getZ(); // 1 flop
//		//
//		T_of_x[0][0] = FReal(1.);	T_of_x[1][0] = localPosition.getX();
//		T_of_x[0][1] = FReal(1.);	T_of_x[1][1] = localPosition.getY();
//		T_of_x[0][2] = FReal(1.);	T_of_x[1][2] = localPosition.getZ();
//		U_of_x[0][0] = FReal(1.);	U_of_x[1][0] = xpx;
//		U_of_x[0][1] = FReal(1.);	U_of_x[1][1] = ypy;
//		U_of_x[0][2] = FReal(1.);	U_of_x[1][2] = zpz;
//		for (unsigned int o=2; o<ORDER; ++o) {
//			T_of_x[o][0] = xpx * T_of_x[o-1][0] - T_of_x[o-2][0]; // 2 flops 
//			T_of_x[o][1] = ypy * T_of_x[o-1][1] - T_of_x[o-2][1]; // 2 flops
//			T_of_x[o][2] = zpz * T_of_x[o-1][2] - T_of_x[o-2][2]; // 2 flops
//			U_of_x[o][0] = xpx * U_of_x[o-1][0] - U_of_x[o-2][0]; // 2 flops
//			U_of_x[o][1] = ypy * U_of_x[o-1][1] - U_of_x[o-2][1]; // 2 flops
//			U_of_x[o][2] = zpz * U_of_x[o-1][2] - U_of_x[o-2][2]; // 2 flops
//		}
//
//		// scale, because dT_o/dx = oU_{o-1}
//		for (unsigned int o=2; o<ORDER; ++o) {
//			U_of_x[o-1][0] *= FReal(o); // 1 flops
//			U_of_x[o-1][1] *= FReal(o); // 1 flops
//			U_of_x[o-1][2] *= FReal(o); // 1 flops
//		}
//
//		// apply P and increment forces
//		FReal potential = FReal(0.);
//		FReal forces[3] = {FReal(0.), FReal(0.), FReal(0.)};
//		//
//		// Optimization:
//		//   Here we compute 1/2 S and 1/2 P  rather S and F like in the paper
//		for (unsigned int n=0; n<nnodes; ++n) {
//		  
//		  // tensor indices of chebyshev nodes
//		  const unsigned int j[3] = {node_ids[n][0], node_ids[n][1], node_ids[n][2]};
//		  //
//		  P[0] = FReal(0.5) + T_of_x[1][0] * T_of_roots[1][j[0]]; // 2 flops 
//		  P[1] = FReal(0.5) + T_of_x[1][1] * T_of_roots[1][j[1]]; // 2 flops
//		  P[2] = FReal(0.5) + T_of_x[1][2] * T_of_roots[1][j[2]]; // 2 flops
//		  P[3] = U_of_x[0][0] * T_of_roots[1][j[0]]; // 1 flop
//		  P[4] = U_of_x[0][1] * T_of_roots[1][j[1]]; // 1 flop
//		  P[5] = U_of_x[0][2] * T_of_roots[1][j[2]]; // 1 flop
//		  for (unsigned int o=2; o<ORDER; ++o) {
//		    P[0] += T_of_x[o  ][0] * T_of_roots[o][j[0]]; // 2 flop
//		    P[1] += T_of_x[o  ][1] * T_of_roots[o][j[1]]; // 2 flop
//		    P[2] += T_of_x[o  ][2] * T_of_roots[o][j[2]]; // 2 flop
//		    P[3] += U_of_x[o-1][0] * T_of_roots[o][j[0]]; // 2 flop
//		    P[4] += U_of_x[o-1][1] * T_of_roots[o][j[1]]; // 2 flop
//		    P[5] += U_of_x[o-1][2] * T_of_roots[o][j[2]]; // 2 flop
//		  }
//		  //
//		  potential	+= P[0] * P[1] * P[2] * localExpansion[n]; // 4 flops
//		  forces[0]	+= P[3] * P[1] * P[2] * localExpansion[n]; // 4 flops
//		  forces[1]	+= P[0] * P[4] * P[2] * localExpansion[n]; // 4 flops
//		  forces[2]	+= P[0] * P[1] * P[5] * localExpansion[n]; // 4 flops
//		}
//		//
//		potential *= c1 ; // 1 flop
//		forces[0] *= jacobian[0] *c1; // 2 flops 
//		forces[1] *= jacobian[1] *c1; // 2 flops
//		forces[2] *= jacobian[2] *c1; // 2 flops
//		// set computed potential
//		iter.data().incPotential(potential); // 1 flop
//		
//		// set computed forces
//		iter.data().incForces(forces[0] * iter.data().getPhysicalValue(),
//													forces[1] * iter.data().getPhysicalValue(),
//													forces[2] * iter.data().getPhysicalValue()); // 6 flops
//
//		// increment iterator
//		iter.gotoNext();
//	}
//}


1410
#endif
1411 1412 1413 1414 1415 1416 1417 1418








1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
        ////struct IMN2MNI {
        ////	enum {size = ORDER * (ORDER-1) * (ORDER-1)};
        ////	unsigned int imn[size], mni[size];
        ////	IMN2MNI() {
        ////		unsigned int counter = 0;
        ////		for (unsigned int i=0; i<ORDER; ++i) {
        ////			for (unsigned int m=0; m<ORDER-1; ++m) {
        ////				for (unsigned int n=0; n<ORDER-1; ++n) {
        ////					imn[counter] = n*(ORDER-1)*ORDER + m*ORDER + i;
        ////					mni[counter] = i*(ORDER-1)*(ORDER-1) + n*(ORDER-1) + m;
        ////					counter++;
        ////				}
        ////			}
        ////		}
        ////	}
        ////} perm0;
        //
        ////for (unsigned int i=0; i<ORDER; ++i) {
        ////	for (unsigned int m=0; m<ORDER-1; ++m) {
        ////		for (unsigned int n=0; n<ORDER-1; ++n) {
        ////			const unsigned int a = n*(ORDER-1)*ORDER + m*ORDER + i;
        ////			const unsigned int b = i*(ORDER-1)*(ORDER-1) + n*(ORDER-1) + m;
        ////			E[b] = D[a];
        ////		}
        ////	}
        ////}

        ////struct JNI2NIJ {
        ////	enum {size = ORDER * ORDER * (ORDER-1)};
        ////	unsigned int jni[size], nij[size];
        ////	JNI2NIJ() {
        ////		unsigned int counter = 0;
        ////		for (unsigned int i=0; i<ORDER; ++i) {
        ////			for (unsigned int j=0; j<ORDER; ++j) {
        ////				for (unsigned int n=0; n<ORDER-1; ++n) {
        ////					jni[counter] = i*(ORDER-1)*ORDER + n*ORDER + j;
        ////					nij[counter] = j*ORDER*(ORDER-1) + i*(ORDER-1) + n;
        ////					counter++;
        ////				}
        ////			}
        ////		}
        ////	}
        ////} perm1;
        //
        ////for (unsigned int i=0; i<ORDER; ++i) {
        ////	for (unsigned int j=0; j<ORDER; ++j) {
        ////		for (unsigned int n=0; n<ORDER-1; ++n) {
        ////			const unsigned int a = i*(ORDER-1)*ORDER + n*ORDER + j;
        ////			const unsigned int b = j*ORDER*(ORDER-1) + i*(ORDER-1) + n;
        ////			G[b] = F[a];
        ////		}
        ////	}
        ////}

        ////struct KIJ2IJK {
        ////	enum {size = ORDER * ORDER * ORDER};
        ////	unsigned int kij[size], ijk[size];
        ////	KIJ2IJK() {
        ////		unsigned int counter = 0;
        ////		for (unsigned int i=0; i<ORDER; ++i) {
        ////			for (unsigned int j=0; j<ORDER; ++j) {
        ////				for (unsigned int k=0; k<ORDER; ++k) {
        ////					kij[counter] = j*ORDER*ORDER + i*ORDER + k;
        ////					ijk[counter] = k*ORDER*ORDER + j*ORDER + i;
        ////					counter++;
        ////				}
        ////			}
        ////		}
        ////	}
        ////} perm2;
        //
        ////for (unsigned int i=0; i<ORDER; ++i) {
        ////	for (unsigned int j=0; j<ORDER; ++j) {
        ////		for (unsigned int k=0; k<ORDER; ++k) {
        ////			const unsigned int a = j*ORDER*ORDER + i*ORDER + k;
        ////			const unsigned int b = k*ORDER*ORDER + j*ORDER + i;
        ////			F8[b] = H[a];
        ////		}
        ////	}
        ////}

        //FReal T_of_y[ORDER * (ORDER-1)];
        //for (unsigned int o=1; o<ORDER; ++o)
        //	for (unsigned int j=0; j<ORDER; ++j)
        //		T_of_y[(o-1)*ORDER + j] = FReal(FChebRoots<ORDER>::T(o, FReal(FChebRoots<ORDER>::roots[j])));

    //struct SumP2M {
    //	unsigned int f2[3][nnodes], f4[3][nnodes];
    //	SumP2M() {
    //		for (unsigned int i=0; i<ORDER; ++i) {
    //			for (unsigned int j=0; j<ORDER; ++j) {
    //				for (unsigned int k=0; k<ORDER; ++k) {
    //					const unsigned int idx = k*ORDER*ORDER + j*ORDER + i;
    //					f2[0][idx] = i;
    //					f2[1][idx] = j;
    //					f2[2][idx] = k;
    //					f4[0][idx] = j*ORDER+i;
    //					f4[1][idx] = k*ORDER+i;
    //					f4[2][idx] = k*ORDER+j;
    //				}
    //			}
    //		}
    //	}
    //} idx0;
    //
    //for (unsigned int i=0; i<nnodes; ++i)
    //	multipoleExpansion[i] = (W1 +
    //													 FReal(2.) * (F2[0][idx0.f2[0][i]] + F2[1][idx0.f2[1][i]] + F2[2][idx0.f2[2][i]]) +
    //													 FReal(4.) * (F4[0][idx0.f4[0][i]] + F4[1][idx0.f4[1][i]] + F4[2][idx0.f4[2][i]]) +
    //													 FReal(8.) *  F8[i]) / nnodes;