FUnifInterpolator.hpp 24 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#ifndef FUNIFINTERPOLATOR_HPP
#define FUNIFINTERPOLATOR_HPP


#include "./../Interpolation/FInterpMapping.hpp"

#include "./FUnifTensor.hpp"
#include "./FUnifRoots.hpp"

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



/**
 * @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 53
  typedef FUnifRoots<FReal, ORDER>  BasisType;
  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 124

    // 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
    const FReal ChildWidth(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 221 222 223 224 225 226 227 228 229 230
  }


  /**
   * 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,
231
                            const FPoint<FReal> *const LocalPoints,
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
                            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]);

    }

  }



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

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

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

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

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

  for(int idxPart = 0 ; idxPart < inParticles->getNbParticles() ; ++idxPart){
    // map global position to [-1,1]
434
    map(FPoint<FReal>(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]), localPosition); // 15 flops
435 436 437 438 439 440 441 442
    // 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
    }

443
    for(int idxRhs = 0 ; idxRhs < nRhs ; ++idxRhs){
444

445
      // compute weight
446 447 448 449 450 451 452 453
      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
454 455 456 457 458

      // 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) {
459 460 461 462 463
            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
464
          }
465 466
        }
      }
467

468
    } // nRhs
469 470 471 472 473 474 475 476 477

  } // 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)
 */
478
template <class FReal, int ORDER, class MatrixKernelClass, int NVALS>
479
template <class ContainerClass>
480
inline void FUnifInterpolator<FReal, ORDER,MatrixKernelClass,NVALS>::applyL2P(const FPoint<FReal>& center,
481 482 483
                                                                 const FReal width,
                                                                 const FReal *const localExpansion,
                                                                 ContainerClass *const inParticles) const
484 485
{
  // loop over particles
486 487
  const map_glob_loc<FReal> map(center, width);
  FPoint<FReal> localPosition;
488 489 490 491 492

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

493
  const  int nParticles = inParticles->getNbParticles();
494 495

  for(int idxPart = 0 ; idxPart < nParticles ; ++ idxPart){
496 497

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

    // evaluate Lagrange polynomial at local position
    FReal L_of_x[ORDER][3];
    for (unsigned int o=0; o<ORDER; ++o) {
503
      L_of_x[o][0] = BasisType::L(o, localPosition.getX()); // 3 * ORDER*(ORDER-1) flops
504 505 506 507
      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
    }

508
    // loop over dim of local expansions
509
    for(int idxLhs = 0 ; idxLhs < nLhs ; ++idxLhs){
510 511 512 513 514 515
      // 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; 
516

517 518


519
      // interpolate and increment target value
520 521 522
      FReal targetValue[nVals];
      for(int idxVals = 0 ; idxVals < nVals ; ++idxVals)
        targetValue[idxVals]=0.;
523 524 525 526
      {
        for (unsigned int l=0; l<ORDER; ++l) {
          for (unsigned int m=0; m<ORDER; ++m) {
            for (unsigned int n=0; n<ORDER; ++n) {
527 528 529 530 531 532
              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];

533 534 535 536 537
            } // ORDER * 4 flops
          } // ORDER * ORDER * 4 flops
        } // ORDER * ORDER * ORDER * 4 flops
      }

538 539 540 541 542 543 544 545
      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
546

547
    } // idxLhs
548 549 550 551 552 553 554 555 556

  } // 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)
 */
557
template <class FReal, int ORDER, class MatrixKernelClass, int NVALS>
558
template <class ContainerClass>
559
inline void FUnifInterpolator<FReal, ORDER,MatrixKernelClass,NVALS>::applyL2PGradient(const FPoint<FReal>& center,
560 561 562
                                                                         const FReal width,
                                                                         const FReal *const localExpansion,
                                                                         ContainerClass *const inParticles) const
563 564 565 566 567 568
{
  ////////////////////////////////////////////////////////////////////
  // TENSOR-PRODUCT INTERPOLUTION NOT IMPLEMENTED YET HERE!!! ////////
  ////////////////////////////////////////////////////////////////////

  // setup local to global mapping
569 570
  const map_glob_loc<FReal> map(center, width);
  FPoint<FReal> Jacobian;
571 572
  map.computeJacobian(Jacobian);
  const FReal jacobian[3] = {Jacobian.getX(), Jacobian.getY(), Jacobian.getZ()};
573
  FPoint<FReal> localPosition;
574 575 576 577 578 579 580
  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];

581 582
//  const unsigned int nParticles = inParticles->getNbParticles();

583 584 585
  for(int idxPart = 0 ; idxPart < inParticles->getNbParticles() ; ++ idxPart){

    // map global position to [-1,1]
586
    map(FPoint<FReal>(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]), localPosition);
587 588 589

    // evaluate Lagrange polynomials of source particle
    for (unsigned int o=0; o<ORDER; ++o) {
590
      L_of_x[o][0] = BasisType::L(o, localPosition.getX()); // 3 * ORDER*(ORDER-1) flops 
591 592
      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
593
      dL_of_x[o][0] = BasisType::dL(o, localPosition.getX()); // TODO verify 3 * ORDER*(ORDER-1) flops
594 595 596 597
      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
    }

598
    for(int idxLhs = 0 ; idxLhs < nLhs ; ++idxLhs){
599 600
      const unsigned int idxPot = idxLhs / nPV; 
      const unsigned int idxPV  = idxLhs % nPV; 
601 602

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

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

613 614 615 616 617 618 619 620 621 622 623
              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
624 625 626 627 628
            } // ORDER * 4 flops
          } // ORDER * ORDER * 4 flops
        } // ORDER * ORDER * ORDER * 4 flops

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

636 637 638 639 640 641
      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);
642

643 644 645 646 647 648
        // 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
649

650
  }
651

652 653 654 655
}


#endif /* FUNIFINTERPOLATOR_HPP */