FUnifInterpolator.hpp 24.1 KB
Newer Older
1
// ===================================================================================
2
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas, Matthias Messner
3 4 5 6 7 8 9 10 11 12 13 14 15
// 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".
// ===================================================================================
16 17 18
// Keep in private GIT
// @SCALFMM_PRIVATE

19 20 21 22
#ifndef FUNIFINTERPOLATOR_HPP
#define FUNIFINTERPOLATOR_HPP


COULAUD Olivier's avatar
COULAUD Olivier committed
23
#include "../Interpolation/FInterpMapping.hpp"
24

COULAUD Olivier's avatar
COULAUD Olivier committed
25 26
#include "FUnifTensor.hpp"
#include "FUnifRoots.hpp"
27

COULAUD Olivier's avatar
COULAUD Olivier committed
28
#include "Utils/FBlas.hpp"
29 30 31 32 33 34 35 36 37 38 39 40 41 42



/**
 * @author Pierre Blanchard (pierre.blanchard@inria.fr)
 * Please read the license
 */

/**
 * @class FUnifInterpolator
 *
 * The class @p FUnifInterpolator defines the anterpolation (M2M) and
 * interpolation (L2L) concerning operations.
 */
43
template < class FReal,int ORDER, class MatrixKernelClass, int NVALS = 1>
44 45 46
class FUnifInterpolator : FNoCopyable
{
  // compile time constants and types
47 48
  enum {nnodes = TensorTraits<ORDER>::nnodes,
        nRhs = MatrixKernelClass::NRHS,
49
        nLhs = MatrixKernelClass::NLHS,
50 51
        nPV = MatrixKernelClass::NPV,
        nVals = NVALS};
52
  typedef FUnifRoots<FReal, ORDER>   BasisType;
53
  typedef FUnifTensor<FReal, ORDER> TensorType;
54 55

  unsigned int node_ids[nnodes][3];
56 57 58 59 60 61 62 63 64 65 66 67 68

  // 8 Non-leaf (i.e. M2M/L2L) interpolators 
  // x1 per level if box is extended
  // only 1 is required for all levels if extension is 0
  FReal*** ChildParentInterpolator;

  // Tree height (needed by M2M/L2L if cell width is extended)
  const int TreeHeight;
  // Root cell width (only used by M2M/L2L)
  const FReal RootCellWidth;
  // Cell width extension (only used by M2M/L2L, kernel handles extension for P2M/L2P)
  const FReal CellWidthExtension;

69 70 71 72 73 74 75

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

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


76
  // PB: use improved version of M2M/L2L
77 78 79 80 81
  /**
   * 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.
   */
82
/*
83 84
  void initM2MandL2L()
  {
85
    FPoint<FReal> ParentRoots[nnodes], ChildRoots[nnodes];
86
    const FReal ParentWidth(2.);
87
    const FPoint<FReal> ParentCenter(0., 0., 0.);
88
    FUnifTensor<FReal,ORDER>::setRoots(ParentCenter, ParentWidth, ParentRoots);
89

90
    FPoint<FReal> ChildCenter;
91 92 93 94 95 96 97 98 99
    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
100 101
      FUnifTensor<FReal,ORDER>::setRelativeChildCenter(child, ChildCenter);
      FUnifTensor<FReal,ORDER>::setRoots(ChildCenter, ChildWidth, ChildRoots);
102 103 104 105 106

      // assemble child - parent - interpolator
      assembleInterpolator(nnodes, ChildRoots, ChildParentInterpolator[child]);
    }
  }
107
*/
108 109 110 111 112 113

  /**
   * 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.
   */
114
  void initTensorM2MandL2L(const int TreeLevel, const FReal ParentWidth)
115 116
  {
    FReal ChildCoords[3][ORDER];
117
    FPoint<FReal> ChildCenter;
118 119 120 121 122 123

    // Ratio of extended cell widths (definition: child ext / parent ext)
    const FReal ExtendedCellRatio = 
      FReal(FReal(ParentWidth)/FReal(2.) + CellWidthExtension) / FReal(ParentWidth + CellWidthExtension);

    // Child cell width
124
    const FReal ChildWidth(FReal(2.)*ExtendedCellRatio);
125 126 127 128 129

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

      // set child info
130 131
      FUnifTensor<FReal, ORDER>::setRelativeChildCenter(child, ChildCenter, ExtendedCellRatio);
      FUnifTensor<FReal, ORDER>::setPolynomialsRoots(ChildCenter, ChildWidth, ChildCoords);
132 133

      // allocate memory
134 135 136 137
      ChildParentInterpolator[TreeLevel][child] = new FReal [3 * ORDER*ORDER];
      assembleInterpolator(ORDER, ChildCoords[0], ChildParentInterpolator[TreeLevel][child]);
      assembleInterpolator(ORDER, ChildCoords[1], ChildParentInterpolator[TreeLevel][child] + 1 * ORDER*ORDER);
      assembleInterpolator(ORDER, ChildCoords[2], ChildParentInterpolator[TreeLevel][child] + 2 * ORDER*ORDER);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    }


    // 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;
        }
      }
    }

  }



public:
  /**
   * Constructor: Initialize the Lagrange polynomials at the equispaced
   * roots/interpolation point
161 162 163 164 165
     *
     * PB: Input parameters ONLY affect the computation of the M2M/L2L ops.
     * These parameters are ONLY required in the context of extended bbox.
     * If no M2M/L2L is required then the interpolator can be built with 
     * the default ctor.
166
   */
167 168 169 170 171 172
  explicit FUnifInterpolator(const int inTreeHeight=3,
                             const FReal inRootCellWidth=FReal(1.), 
                             const FReal inCellWidthExtension=FReal(0.))
  : TreeHeight(inTreeHeight), 
    RootCellWidth(inRootCellWidth),
    CellWidthExtension(inCellWidthExtension)
173 174 175 176
  {
    // initialize root node ids
    TensorType::setNodeIds(node_ids);

177
    // initialize interpolation operator for M2M and L2L (non leaf operations)
178 179 180

    // allocate 8 arrays per level
    ChildParentInterpolator = new FReal**[TreeHeight];
181
    for ( int l=0; l<TreeHeight; ++l){
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
      ChildParentInterpolator[l] = new FReal*[8];
      for (unsigned int c=0; c<8; ++c)
        ChildParentInterpolator[l][c]=nullptr;        
    }

    // Set number of non-leaf ios that actually need to be computed
    unsigned int reducedTreeHeight; // = 2 + nb of computed nl ios
    if(CellWidthExtension==0.) // if no cell extension, then ...
      reducedTreeHeight = 3; // cmp only 1 non-leaf io
    else 
      reducedTreeHeight = TreeHeight; // cmp 1 non-leaf io per level

    // Init non-leaf interpolators
    FReal CellWidth = RootCellWidth / FReal(2.); // at level 1
    CellWidth /= FReal(2.);                      // at level 2
        
    for (unsigned int l=2; l<reducedTreeHeight; ++l) {
          
      //this -> initM2MandL2L(l,CellWidth);     // non tensor-product interpolation
      this -> initTensorM2MandL2L(l,CellWidth); // tensor-product interpolation

      // update cell width
      CellWidth /= FReal(2.);                    // at level l+1 
    }
206 207 208 209 210 211 212 213
  }


  /**
   * Destructor: Delete dynamically allocated memory for M2M and L2L operator
   */
  ~FUnifInterpolator()
  {
214
    for ( int l=0; l<TreeHeight; ++l) {
215 216 217
      for (unsigned int child=0; child<8; ++child)
        if(ChildParentInterpolator[l][child] != nullptr)
          delete [] ChildParentInterpolator[l][child];
218 219 220
        delete [] ChildParentInterpolator[l];
    }
    delete [] ChildParentInterpolator;
221 222 223 224 225 226 227 228 229 230 231 232 233
  }


  /**
   * 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,
234
                            const FPoint<FReal> *const LocalPoints,
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
                            FReal *const Interpolator) const
  {
    // values of Lagrange polynomials of source particle: L_o(x_i)
    FReal L_of_x[ORDER][3];
    // loop: local points (mapped in [-1,1])
    for (unsigned int m=0; m<NumberOfLocalPoints; ++m) {
      // evaluate Lagrange polynomials at local points
      for (unsigned int o=0; o<ORDER; ++o) {
        L_of_x[o][0] = BasisType::L(o, LocalPoints[m].getX());
        L_of_x[o][1] = BasisType::L(o, LocalPoints[m].getY());
        L_of_x[o][2] = BasisType::L(o, LocalPoints[m].getZ());
      }

      // assemble interpolator
      for (unsigned int n=0; n<nnodes; ++n) {
        Interpolator[n*NumberOfLocalPoints + m] = FReal(1.);
        for (unsigned int d=0; d<3; ++d) {
          const unsigned int j = node_ids[n][d];
          // The Lagrange case is much simpler than the Chebyshev case
          // as no summation is required
          Interpolator[n*NumberOfLocalPoints + m] *= L_of_x[j][d];
        }

      }

    }

  }


  void assembleInterpolator(const unsigned int M, const FReal *const x, FReal *const S) const
  {
    // loop: local points (mapped in [-1,1])
    for (unsigned int m=0; m<M; ++m) {
      // evaluate Lagrange polynomials at local points
      for (unsigned int o=0; o<ORDER; ++o)
        S[o*M + m] = BasisType::L(o, x[m]);

    }

  }



279
  const FReal *const * getChildParentInterpolator() const
280
  { return ChildParentInterpolator; }
281
  const unsigned int * getPermutationsM2ML2L(unsigned int i) const
282 283 284 285 286 287 288 289 290 291 292 293
  { 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>
294
  void applyP2M(const FPoint<FReal>& center,
295 296 297 298 299 300 301 302 303 304
                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>
305
  void applyL2P(const FPoint<FReal>& center,
306 307 308 309 310 311 312 313 314
                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>
315
  void applyL2PGradient(const FPoint<FReal>& center,
316 317 318 319 320 321 322 323 324
                        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>
325
  void applyL2PTotal(const FPoint<FReal>& center,
326 327 328 329
                     const FReal width,
                     const FReal *const localExpansion,
                     ContainerClass *const localParticles) const;

330
  // PB: ORDER^6 version of applyM2M/L2L
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
  /*
    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);
    }
  */

351
  // PB: improved version of applyM2M/L2L also applies to Lagrange interpolation
352
  // PB: Multidim version handled in kernel !
353 354
  void applyM2M(const unsigned int ChildIndex,
                const FReal *const ChildExpansion,
355 356
                FReal *const ParentExpansion,
                const unsigned int TreeLevel = 2) const
357 358 359 360
  {
    FReal Exp[nnodes], PermExp[nnodes];
    // ORDER*ORDER*ORDER * (2*ORDER-1)
    FBlas::gemtm(ORDER, ORDER, ORDER*ORDER, FReal(1.),
361
                 ChildParentInterpolator[TreeLevel][ChildIndex], ORDER,
362 363 364 365 366
                 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.),
367
                 ChildParentInterpolator[TreeLevel][ChildIndex] + 2 * ORDER*ORDER, ORDER,
368 369 370 371 372
                 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.),
373
                 ChildParentInterpolator[TreeLevel][ChildIndex] + 1 * ORDER*ORDER, ORDER,
374 375 376 377 378 379 380 381
                 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,
382 383
                FReal *const ChildExpansion,
                const unsigned int TreeLevel = 2) const
384 385 386 387
  {
    FReal Exp[nnodes], PermExp[nnodes];
    // ORDER*ORDER*ORDER * (2*ORDER-1)
    FBlas::gemm(ORDER, ORDER, ORDER*ORDER, FReal(1.),
388
                ChildParentInterpolator[TreeLevel][ChildIndex], ORDER,
389 390 391 392 393
                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.),
394
                ChildParentInterpolator[TreeLevel][ChildIndex] + 2 * ORDER*ORDER, ORDER,
395 396 397 398 399
                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.),
400
                ChildParentInterpolator[TreeLevel][ChildIndex] + 1 * ORDER*ORDER, ORDER,
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
                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)
};







/**
 * Particle to moment: application of \f$S_\ell(y,\bar y_n)\f$
 * (anterpolation, it is the transposed interpolation)
 */
418
template <class FReal, int ORDER, class MatrixKernelClass, int NVALS>
419
template <class ContainerClass>
420
inline void FUnifInterpolator<FReal, ORDER,MatrixKernelClass,NVALS>::applyP2M(const FPoint<FReal>& center,
421 422 423
                                                                 const FReal width,
                                                                 FReal *const multipoleExpansion,
                                                                 const ContainerClass *const inParticles) const
424 425 426
{

  // allocate stuff
427 428
  const map_glob_loc<FReal> map(center, width);
  FPoint<FReal> localPosition;
429 430 431 432 433 434

  // loop over source particles
  const FReal*const positionsX = inParticles->getPositions()[0];
  const FReal*const positionsY = inParticles->getPositions()[1];
  const FReal*const positionsZ = inParticles->getPositions()[2];

435
  for(FSize idxPart = 0 ; idxPart < inParticles->getNbParticles() ; ++idxPart){
436
    // map global position to [-1,1]
437
    map(FPoint<FReal>(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]), localPosition); // 15 flops
438 439 440 441 442 443 444 445
    // evaluate Lagrange polynomial at local position
    FReal L_of_x[ORDER][3];
    for (unsigned int o=0; o<ORDER; ++o) {
      L_of_x[o][0] = BasisType::L(o, localPosition.getX()); // 3 * ORDER*(ORDER-1) flops PB: TODO confirm
      L_of_x[o][1] = BasisType::L(o, localPosition.getY()); // 3 * ORDER*(ORDER-1) flops
      L_of_x[o][2] = BasisType::L(o, localPosition.getZ()); // 3 * ORDER*(ORDER-1) flops
    }

446
    for(int idxRhs = 0 ; idxRhs < nRhs ; ++idxRhs){
447

448
      // compute weight
449 450 451 452 453 454 455 456
      FReal weight[nVals];
      for(int idxVals = 0 ; idxVals < nVals ; ++idxVals){

        // read physicalValue
        const FReal*const physicalValues = inParticles->getPhysicalValues(idxVals,idxRhs);
        weight[idxVals] = physicalValues[idxPart];

      } // nVals
457 458 459 460 461

      // 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) {
462 463 464 465 466
            const unsigned int idx = idxRhs*nVals*nnodes + k*ORDER*ORDER + j*ORDER + i;              
            const FReal S = L_of_x[i][0] * L_of_x[j][1] * L_of_x[k][2];

            for(int idxVals = 0 ; idxVals < nVals ; ++idxVals)
              multipoleExpansion[idxVals*nnodes+idx] += S * weight[idxVals]; // 3 * ORDER*ORDER*ORDER flops
467
          }
468 469
        }
      }
470

471
    } // nRhs
472 473 474 475 476 477 478 479 480

  } // flops: N * (3 * ORDER*ORDER*ORDER + 3 * 3 * ORDER*(ORDER-1)) flops

}


/**
 * Local to particle operation: application of \f$S_\ell(x,\bar x_m)\f$ (interpolation)
 */
481
template <class FReal, int ORDER, class MatrixKernelClass, int NVALS>
482
template <class ContainerClass>
483
inline void FUnifInterpolator<FReal, ORDER,MatrixKernelClass,NVALS>::applyL2P(const FPoint<FReal>& center,
484 485 486
                                                                 const FReal width,
                                                                 const FReal *const localExpansion,
                                                                 ContainerClass *const inParticles) const
487 488
{
  // loop over particles
489 490
  const map_glob_loc<FReal> map(center, width);
  FPoint<FReal> localPosition;
491 492 493 494 495

  const FReal*const positionsX = inParticles->getPositions()[0];
  const FReal*const positionsY = inParticles->getPositions()[1];
  const FReal*const positionsZ = inParticles->getPositions()[2];

496
  const  FSize nParticles = inParticles->getNbParticles();
497

498
  for(FSize idxPart = 0 ; idxPart < nParticles ; ++ idxPart){
499 500

    // map global position to [-1,1]
501
    map(FPoint<FReal>(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]), localPosition); // 15 flops
502 503 504 505

    // evaluate Lagrange polynomial at local position
    FReal L_of_x[ORDER][3];
    for (unsigned int o=0; o<ORDER; ++o) {
506
      L_of_x[o][0] = BasisType::L(o, localPosition.getX()); // 3 * ORDER*(ORDER-1) flops
507 508 509 510
      L_of_x[o][1] = BasisType::L(o, localPosition.getY()); // 3 * ORDER*(ORDER-1) flops
      L_of_x[o][2] = BasisType::L(o, localPosition.getZ()); // 3 * ORDER*(ORDER-1) flops
    }

511
    // loop over dim of local expansions
512
    for(int idxLhs = 0 ; idxLhs < nLhs ; ++idxLhs){
513 514 515 516 517 518
      // distribution over potential components:
      // We sum the multidim contribution of PhysValue
      // This was originally done at M2L step but moved here 
      // because their storage is required by the force computation.
      // In fact : f_{ik}(x)=w_j(x) \nabla_{x_i} K_{ij}(x,y)w_j(y))
      const unsigned int idxPot = idxLhs / nPV; 
519

520 521


522
      // interpolate and increment target value
523 524 525
      FReal targetValue[nVals];
      for(int idxVals = 0 ; idxVals < nVals ; ++idxVals)
        targetValue[idxVals]=0.;
526 527 528 529
      {
        for (unsigned int l=0; l<ORDER; ++l) {
          for (unsigned int m=0; m<ORDER; ++m) {
            for (unsigned int n=0; n<ORDER; ++n) {
530 531 532 533 534 535
              const unsigned int idx = idxLhs*nVals*nnodes + n*ORDER*ORDER + m*ORDER + l;
              const FReal S = L_of_x[l][0] * L_of_x[m][1] * L_of_x[n][2];

              for(int idxVals = 0 ; idxVals < nVals ; ++idxVals)
                targetValue[idxVals] += S * localExpansion[idxVals*nnodes+idx];

536 537 538 539 540
            } // ORDER * 4 flops
          } // ORDER * ORDER * 4 flops
        } // ORDER * ORDER * ORDER * 4 flops
      }

541 542 543 544 545 546 547 548
      for(int idxVals = 0 ; idxVals < nVals ; ++idxVals){

        // get potential
        FReal*const potentials = inParticles->getPotentials(idxVals,idxPot);
        // add contribution to potential
        potentials[idxPart] += (targetValue[idxVals]);

      }// NVALS
549

550
    } // idxLhs
551 552 553 554 555 556 557 558 559

  } // N * (4 * ORDER * ORDER * ORDER + 9 * ORDER*(ORDER-1) ) flops
}



/**
 * Local to particle operation: application of \f$\nabla_x S_\ell(x,\bar x_m)\f$ (interpolation)
 */
560
template <class FReal, int ORDER, class MatrixKernelClass, int NVALS>
561
template <class ContainerClass>
562
inline void FUnifInterpolator<FReal, ORDER,MatrixKernelClass,NVALS>::applyL2PGradient(const FPoint<FReal>& center,
563 564 565
                                                                         const FReal width,
                                                                         const FReal *const localExpansion,
                                                                         ContainerClass *const inParticles) const
566 567 568 569 570 571
{
  ////////////////////////////////////////////////////////////////////
  // TENSOR-PRODUCT INTERPOLUTION NOT IMPLEMENTED YET HERE!!! ////////
  ////////////////////////////////////////////////////////////////////

  // setup local to global mapping
572 573
  const map_glob_loc<FReal> map(center, width);
  FPoint<FReal> Jacobian;
574 575
  map.computeJacobian(Jacobian);
  const FReal jacobian[3] = {Jacobian.getX(), Jacobian.getY(), Jacobian.getZ()};
576
  FPoint<FReal> localPosition;
577 578 579 580 581 582 583
  FReal L_of_x[ORDER][3];
  FReal dL_of_x[ORDER][3];

  const FReal*const positionsX = inParticles->getPositions()[0];
  const FReal*const positionsY = inParticles->getPositions()[1];
  const FReal*const positionsZ = inParticles->getPositions()[2];

584
//  const FSize nParticles = inParticles->getNbParticles();
585

586
  for(FSize idxPart = 0 ; idxPart < inParticles->getNbParticles() ; ++ idxPart){
587 588

    // map global position to [-1,1]
589
    map(FPoint<FReal>(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]), localPosition);
590 591 592

    // evaluate Lagrange polynomials of source particle
    for (unsigned int o=0; o<ORDER; ++o) {
593
      L_of_x[o][0] = BasisType::L(o, localPosition.getX()); // 3 * ORDER*(ORDER-1) flops 
594 595
      L_of_x[o][1] = BasisType::L(o, localPosition.getY()); // 3 * ORDER*(ORDER-1) flops
      L_of_x[o][2] = BasisType::L(o, localPosition.getZ()); // 3 * ORDER*(ORDER-1) flops
596
      dL_of_x[o][0] = BasisType::dL(o, localPosition.getX()); // TODO verify 3 * ORDER*(ORDER-1) flops
597 598 599 600
      dL_of_x[o][1] = BasisType::dL(o, localPosition.getY()); // TODO verify 3 * ORDER*(ORDER-1) flops
      dL_of_x[o][2] = BasisType::dL(o, localPosition.getZ()); // TODO verify 3 * ORDER*(ORDER-1) flops
    }

601
    for(int idxLhs = 0 ; idxLhs < nLhs ; ++idxLhs){
602 603
      const unsigned int idxPot = idxLhs / nPV; 
      const unsigned int idxPV  = idxLhs % nPV; 
604 605

      // interpolate and increment forces value
606 607 608 609
      FReal forces[nVals][3];
      for(int idxVals = 0 ; idxVals < nVals ; ++idxVals)
        forces[idxVals][0]=forces[idxVals][1]=forces[idxVals][2]=FReal(0.);   

610 611 612 613
      {
        for (unsigned int l=0; l<ORDER; ++l) {
          for (unsigned int m=0; m<ORDER; ++m) {
            for (unsigned int n=0; n<ORDER; ++n) {
614
              const unsigned int idx = idxLhs*nVals*nnodes + n*ORDER*ORDER + m*ORDER + l;
615

616 617 618 619 620 621 622 623 624 625 626
              const FReal PX = dL_of_x[l][0] * L_of_x[m][1] * L_of_x[n][2];
              const FReal PY = L_of_x[l][0] * dL_of_x[m][1] * L_of_x[n][2];
              const FReal PZ = L_of_x[l][0] * L_of_x[m][1] * dL_of_x[n][2];

              for(int idxVals = 0 ; idxVals < nVals ; ++idxVals){

                forces[idxVals][0] += PX * localExpansion[idxVals*nnodes + idx];
                forces[idxVals][1] += PY * localExpansion[idxVals*nnodes + idx];
                forces[idxVals][2] += PZ * localExpansion[idxVals*nnodes + idx];
                
              } // NVALS
627 628 629 630 631
            } // ORDER * 4 flops
          } // ORDER * ORDER * 4 flops
        } // ORDER * ORDER * ORDER * 4 flops

        // scale forces
632 633 634 635 636
        for(int idxVals = 0 ; idxVals < nVals ; ++idxVals){
          forces[idxVals][0] *= jacobian[0];
          forces[idxVals][1] *= jacobian[1];
          forces[idxVals][2] *= jacobian[2];
        } // NVALS
637 638
      }

639 640 641 642 643 644
      for(int idxVals = 0 ; idxVals < nVals ; ++idxVals){

        const FReal*const physicalValues = inParticles->getPhysicalValues(idxVals,idxPV);
        FReal*const forcesX = inParticles->getForcesX(idxVals,idxPot);
        FReal*const forcesY = inParticles->getForcesY(idxVals,idxPot);
        FReal*const forcesZ = inParticles->getForcesZ(idxVals,idxPot);
645

646 647 648 649 650 651
        // set computed forces
        forcesX[idxPart] += forces[idxVals][0] * physicalValues[idxPart];
        forcesY[idxPart] += forces[idxVals][1] * physicalValues[idxPart];
        forcesZ[idxPart] += forces[idxVals][2] * physicalValues[idxPart];
      } // NVALS
    } // NLHS
652

653
  }
654

655 656 657 658
}


#endif /* FUNIFINTERPOLATOR_HPP */