testUnifTensorialInterpolator.cpp 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// ===================================================================================
// Ce LOGICIEL "ScalFmm" est couvert par le copyright Inria 20xx-2012.
// Inria détient tous les droits de propriété sur le LOGICIEL, et souhaite que
// la communauté scientifique l'utilise afin de le tester et de l'évaluer.
// Inria donne gracieusement le droit d'utiliser ce LOGICIEL. Toute utilisation
// dans un but lucratif ou à des fins commerciales est interdite sauf autorisation
// expresse et préalable d'Inria.
// Toute utilisation hors des limites précisées ci-dessus et réalisée sans l'accord
// expresse préalable d'Inria constituerait donc le délit de contrefaçon.
// Le LOGICIEL étant un produit en cours de développement, Inria ne saurait assurer
// aucune responsabilité et notamment en aucune manière et en aucun cas, être tenu
// de répondre d'éventuels dommages directs ou indirects subits par l'utilisateur.
// Tout utilisateur du LOGICIEL s'engage à communiquer à Inria ses remarques
// relatives à l'usage du LOGICIEL
// ===================================================================================

// ==== CMAKE =====
18
// @FUSE_FFT
19 20 21 22 23 24 25 26 27
// ================


#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

28 29 30
#include "Utils/FTic.hpp"
#include "Utils/FMath.hpp"
#include "Utils/FBlas.hpp"
31

32
#include "Containers/FVector.hpp"
33

34 35
#include "Utils/FAssert.hpp"
#include "Utils/FPoint.hpp"
36 37


38 39 40
#include "Kernels/Uniform/FUnifInterpolator.hpp"
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Uniform/FUnifTensor.hpp"
41 42

// Check DFT
43 44
#include "Utils/FDft.hpp"
#include "Utils/FComplexe.hpp"
45 46


47 48
#include "Kernels/P2P/FP2PParticleContainer.hpp"
#include "Components/FSimpleLeaf.hpp"
49 50 51 52 53




/**
54 55 56
 * In this file we compute the interactions (direct and Unif FM-approximate) for
 * a tensorial interaction kernel (R_ij) as well as the forces (comparison 
 * with direct computation using R_ijk kernel).
57 58 59
 */

int main(int, char **){
60

61 62
  typedef FInterpMatrixKernel_R_IJ MatrixKernelClass;
  typedef FInterpMatrixKernel_R_IJK RIJKMatrixKernelClass; // PB: force computation
63 64
  const double a = 0.0; // core width (Beware! if diff from 0. then Kernel should be NON HOMOGENEOUS !!!)

65
  const unsigned int ncmp = MatrixKernelClass::NCMP;
66 67
  const unsigned int nrhs = MatrixKernelClass::NRHS;
  const unsigned int nlhs = MatrixKernelClass::NLHS;
68 69
  const unsigned int npot = MatrixKernelClass::NPOT;

70 71
  typedef FP2PParticleContainer<nrhs,nlhs> ContainerClass;
  typedef FSimpleLeaf<ContainerClass> LeafClass;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  ///////////////////////What we do/////////////////////////////
  std::cout << "\nTask: Compute interactions between source particles in leaf Y and target\n";
  std::cout << " particles in leaf X. Compare the fast summation K ~ Lx K Ly' with the\n";
  std::cout << " direct computation.\n" << std::endl;
  //////////////////////////////////////////////////////////////

  const FReal FRandMax = FReal(RAND_MAX);
  FTic time;


  // Leaf size
  FReal width = FReal(3.723);

  ////////////////////////////////////////////////////////////////////
  LeafClass X;
  FPoint cx(0., 0., 0.);
  const unsigned long M = 5000;
  std::cout << "Fill the leaf X of width " << width
            << " centered at cx=" << cx << " with M=" << M << " target particles" << std::endl;
  {
93
    
94 95 96 97
    for(unsigned long i=0; i<M; ++i){
      FReal x = (FReal(rand())/FRandMax - FReal(.5)) * width + cx.getX();
      FReal y = (FReal(rand())/FRandMax - FReal(.5)) * width + cx.getY();
      FReal z = (FReal(rand())/FRandMax - FReal(.5)) * width + cx.getZ();
98
      // PB: need to know the actual value of NRHS (=3 here)
99
      X.push(FPoint(x, y, z), FReal(rand())/FRandMax, FReal(rand())/FRandMax, FReal(rand())/FRandMax);
100 101 102 103 104 105
    }
  }


  ////////////////////////////////////////////////////////////////////
  LeafClass Y;
106 107
    FPoint cy(FReal(2.)*width, 0., 0.);
  //FPoint cy(FReal(2.)*width, FReal(2.)*width, 0.);
108 109 110 111 112 113 114 115 116

  const unsigned long N = 5000;
  std::cout << "Fill the leaf Y of width " << width
            << " centered at cy=" << cy	<< " with N=" << N << " source particles" << std::endl;
  {
    for(unsigned long i=0; i<N; ++i){
      FReal x = (FReal(rand())/FRandMax - FReal(.5)) * width + cy.getX();
      FReal y = (FReal(rand())/FRandMax - FReal(.5)) * width + cy.getY();
      FReal z = (FReal(rand())/FRandMax - FReal(.5)) * width + cy.getZ();
117
      // PB: need to know the actual value of NRHS (=3 here)
118
      Y.push(FPoint(x, y, z), FReal(rand())/FRandMax, FReal(rand())/FRandMax, FReal(rand())/FRandMax);
119 120 121 122 123 124 125
    }
  }



  ////////////////////////////////////////////////////////////////////
  // approximative computation
126
  const unsigned int ORDER = 6;
127 128 129
  const unsigned int nnodes = TensorTraits<ORDER>::nnodes;
  typedef FUnifInterpolator<ORDER,MatrixKernelClass> InterpolatorClass;
  InterpolatorClass S;
130 131
  MatrixKernelClass MatrixKernel;
  RIJKMatrixKernelClass RIJKMatrixKernel;
132 133 134 135 136 137 138 139

  std::cout << "\nCompute interactions approximatively, interpolation order = " << ORDER << " ..." << std::endl;

  std::cout << "\nP2M ... " << std::flush;
  time.tic();
  // Anterpolate: W_n = \sum_j^N S(y_j,\bar y_n) * w_j
  FReal W[nrhs*nnodes]; // multipole expansion
  // tensorial case interpolate same Y for each component
140
  S.applyP2M(cy, width, W, Y.getSrc()); // the multipole expansions are set to 0 in S.applyP2M
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  std::cout << "took " << time.tacAndElapsed() << "s" << std::endl;

  std::cout << "M2L ... " << std::flush;
  time.tic();
  // Multipole to local: F_m = \sum_n^L K(\bar x_m, \bar y_n) * W_n
  FPoint rootsX[nnodes], rootsY[nnodes];
  FUnifTensor<ORDER>::setRoots(cx, width, rootsX);
  FUnifTensor<ORDER>::setRoots(cy, width, rootsY);

  FReal F[nlhs*nnodes]; // local expansion
  for (unsigned int i=0; i<nnodes*nlhs; ++i) F[i] = FReal(0.);

  for (unsigned int i=0; i<nnodes; ++i) {
    for (unsigned int j=0; j<nnodes; ++j){
      
156 157 158
      for (unsigned int idxLhs=0; idxLhs<nlhs; ++idxLhs){
        unsigned int idxRhs = idxLhs % npot;
        unsigned int d = MatrixKernel.getPosition(idxLhs);
159

160
        F[i+idxLhs*nnodes] += MatrixKernelClass(a,d).evaluate(rootsX[i], rootsY[j]) * W[j+idxRhs*nnodes];
161

162
      }
163 164 165 166 167 168 169 170 171 172 173 174 175 176
    }
  }
  std::cout << "took " << time.tacAndElapsed() << "s" << std::endl;

//  std::cout<< "F via direct applyM2L: "<<std::endl;
//  for (unsigned int d=0; d<nlhs; ++d){
//    for (unsigned int i=0; i<nnodes; ++i)
//      std::cout<< F[i+d*nnodes] << ", ";
//    std::cout<<std::endl;
//  }
//  std::cout<<std::endl;

  ////////////////////////////////////////////////////////////////////////////
  // Store M2L in K and apply K
177
  FReal K[ncmp*nnodes*nnodes]; // local expansion
178 179 180
  for (unsigned int i=0; i<nnodes; ++i) {
    for (unsigned int j=0; j<nnodes; ++j){

181
      for (unsigned int d=0; d<ncmp; ++d){
182
        K[d*nnodes*nnodes+i*nnodes+j] = MatrixKernelClass(a,d).evaluate(rootsX[i], rootsY[j]);        
183 184 185 186 187 188 189 190 191 192 193
      }

    }
  }
  std::cout<< "Apply M2L in usual sense: ";
  time.tic();
  for (unsigned int i=0; i<nnodes*nlhs; ++i) F[i] = FReal(0.);

  for (unsigned int i=0; i<nnodes; ++i)
    for (unsigned int j=0; j<nnodes; ++j){

194 195 196
      for (unsigned int idxLhs=0; idxLhs<nlhs; ++idxLhs){
        unsigned int idxRhs = idxLhs % npot;
        unsigned int d = MatrixKernel.getPosition(idxLhs);
197

198
        F[i+idxLhs*nnodes] += K[d*nnodes*nnodes+i*nnodes+j] * W[j+idxRhs*nnodes];
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

    }
  }

  time.tac();
  std::cout << "took " << time.elapsed() << "sec." << std::endl;

//  std::cout<< "F via store and applyM2L: "<<std::endl;
//  for (unsigned int d=0; d<nlhs; ++d){
//    for (unsigned int i=0; i<nnodes; ++i)
//      std::cout<< F[i+d*nnodes] << ", ";
//    std::cout<<std::endl;
//  }
//  std::cout<<std::endl;

  /////////////////////////////////////////////////////////////////////////////////////
  // PB: Verify storage improvement works (indexing etc...)
  // 1) store circulant matrix
  const unsigned int rc = (2*ORDER-1)*(2*ORDER-1)*(2*ORDER-1);
218
  FReal C[ncmp*rc];
219 220 221 222 223 224 225 226 227 228 229 230 231

  typedef FUnifTensor<ORDER> TensorType;
  unsigned int node_diff[nnodes*nnodes];
  TensorType::setNodeIdsDiff(node_diff);
  unsigned int node_ids_pairs[rc][2];
  TensorType::setNodeIdsPairs(node_ids_pairs);

  unsigned int ido=0;

  for(unsigned int l=0; l<2*ORDER-1; ++l)
    for(unsigned int m=0; m<2*ORDER-1; ++m)
      for(unsigned int n=0; n<2*ORDER-1; ++n){

232
        for (unsigned int d=0; d<ncmp; ++d){
233 234

          C[d*rc + ido]
235
            = MatrixKernelClass(a,d).evaluate(rootsX[node_ids_pairs[ido][0]], 
236
                                              rootsY[node_ids_pairs[ido][1]]);
237 238 239 240 241 242 243 244
        }
        
        ido++;
      }

//  // Display C (gathers every values of K that need to be stored,
//  // corresponds to the first line of the padded matrix (reverse order?))
//  std::cout<<"C="<<std::endl;
245
//    for (unsigned int d=0; d<ncmp; ++d){
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
//      for (unsigned int n=0; n<rc; ++n)
//        std::cout<< C[n + d*rc] << ", ";
//      std::cout<<std::endl;
//    }
//  std::cout<<std::endl;

  //////////////////////////////////////////////////////////////////////////////////////////////////////
  // K is a block Toeplitz matrix
  // i.e. a blockwise Toeplitz matrix where the block also have the Toeplitz structure.
  // e.g. for ORDER=3: K=[K_{1,1} K_{1,2} K_{1,3},  where K_{i,j}=[k11 k12 k13,
  //                      K_{2,1} K_{1,1} K_{1,2},                 k21 k11 k12,
  //                      K_{3,1} K_{2,1} K_{1,1}];                k31 k21 k11];
  // K is of size order^3 x order^3
  // (i.e. order^2 x order^2 Toeplitz blocks of size order x order),
  // K is very close to be Toeplitz itself and even circulant.
  // In order to actually embed K into a circulant matrix C one just
  // needs to insert (ORDER-1) extra lines/columns (to each block).

//  std::cout<< "K=" <<std::endl;
//  for (unsigned int i=0; i<nnodes; ++i){
//    for (unsigned int j=0; j<nnodes; ++j){
//      std::cout<< K[i*nnodes+j]<<", ";
//    }
//    std::cout<<std::endl;
//  }
//  std::cout<<std::endl;

//  // Check matrix node_diff
//  std::cout<< "node_diff=" <<std::endl;
//  for (unsigned int i=0; i<nnodes; ++i){
//    for (unsigned int j=0; j<nnodes; ++j){
//      std::cout<< node_diff[i*nnodes+j] <<", ";
//    }
//    std::cout<<std::endl;
//  }
//  std::cout<<std::endl;

//  // Expected ido for the (2*ORDER-1)^3x(2*ORDER-1)^3 circulant matrix
//  for (unsigned int i=0; i<rc; ++i){
//    for (unsigned int j=0; j<rc; ++j){
//      if(i>j) std::cout<< i-j-1 << ", ";
//      else std::cout<< rc+i-j-1 << ", ";
//    } std::cout<<std::endl;
//  } std::cout<<std::endl;

//  // kernel evaluated at previous ido returns a circulant matrix
//  for (unsigned int i=0; i<rc/2; ++i){
//    for (unsigned int j=0; j<rc/2; ++j){
//      if(i>j) std::cout<< C[i-j-1] << ", ";
//      else std::cout<< C[rc+i-j-1] << ", ";
//    } std::cout<<std::endl;
//  } std::cout<<std::endl;

  // In 1D the Zero Padding consists in
  // inserting ORDER-1 zeros in the multipole exp
  // in order to apply the (ORDER+ORDER-1)x(ORDER+ORDER-1)
  // circulant matrix to it.
  // Let us extend it to the 3D case:
  FReal MultExp[nrhs*nnodes]; FReal PaddedMultExp[nrhs*rc];
  for (unsigned int i=0; i<nrhs*nnodes; ++i) MultExp[i]=W[i];
  FReal LocalExp[nlhs*nnodes]; FReal PaddedLocalExp[nlhs*rc];
  FBlas::setzero(nlhs*nnodes,LocalExp);
  FBlas::setzero(nlhs*rc,PaddedLocalExp);

//  std::cout<< "Expected LocalExp: "<<std::endl;
//  for (unsigned int d=0; d<nlhs; ++d){
//    for (unsigned int i=0; i<nnodes; ++i)
//      std::cout<< F[i+d*nnodes] << ", ";
//    std::cout<<std::endl;
//  }

  /////////////////////////////////////////////////////////////////////////////////////
  // Application of circulant Toeplitz system in PHYSICAL SPACE
  std::cout<< "Apply circulant M2L in Physical space: ";
  time.tic();
  for (unsigned int i=0; i<nnodes; ++i){

    // Pad Multipole Expansion with respect to current row
    FBlas::setzero(nrhs*rc,PaddedMultExp);
    for (unsigned int j=0; j<nnodes; ++j)
        for (unsigned int d=0; d<nrhs; ++d)
          PaddedMultExp[node_diff[i*nnodes+j] + d*rc]=MultExp[j + d*nnodes];

//    std::cout<< "Padded MultExp for row i=" << i << ": "<<std::endl;
//    for (unsigned int p=0; p<rc; ++p)
//      std::cout<< PaddedMultExp[p] << ", ";
//    std::cout<<std::endl;

    // Application of M2L in PHYSICAL SPACE
    for (unsigned int pj=0; pj<rc; ++pj)
336 337 338
      for (unsigned int idxLhs=0; idxLhs<nlhs; ++idxLhs){
        unsigned int idxRhs = idxLhs % npot;
        unsigned int d = MatrixKernel.getPosition(idxLhs);
339

340
        LocalExp[i + idxLhs*nnodes]+=C[pj + d*rc]*PaddedMultExp[pj + idxRhs*rc];
341

342
      }
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

  }// end i
  time.tac();
  std::cout << "took " << time.elapsed() << "sec." << std::endl;

//  std::cout<< "LocalExp via product in PHYSICAL SPACE: "<<std::endl;
//  for (unsigned int d=0; d<nlhs; ++d){
//    for (unsigned int i=0; i<nnodes; ++i)
//      std::cout<< LocalExp[i+d*nnodes] << ", ";
//    std::cout<<std::endl;
//  }
//  std::cout<<std::endl;

  /////////////////////////////////////////////////////////////////////////////////////
  // Efficient application of the Toeplitz system in FOURIER SPACE

  // Init DFT
360 361
  const int dimfft = 1;
  const int steps[dimfft] = {rc};
362
  //FDft Dft(rc); // direct version
363 364
  FFft<dimfft> Dft; // fast version
  Dft.buildDFT(steps);
365 366

  // Get first COLUMN of K and Store in T
367
  FReal T[ncmp*rc];
368 369 370 371 372 373 374 375 376 377 378 379 380
  // use permutations
  unsigned int perm[rc];
  for(unsigned int p=0; p<rc; ++p){
    if(p>0) perm[p]=p-1;
    else perm[p]=rc+p-1;
//    std::cout << "perm["<< p << "]="<< perm[p] << std::endl;
  }

  for (unsigned int i=0; i<rc; ++i){
    // keep this lines commented to see what permutation accounts for:
    //  for (unsigned int j=0; j<rc; ++j){
//      if(i>0) T[i]=C[i-0-1];
//      else T[i]=C[rc+i-0-1];
381
    for (unsigned int d=0; d<ncmp; ++d)
382 383 384 385 386 387 388 389 390 391
      T[i + d*rc]=C[perm[i] + d*rc];
  //  }
  }

//  std::cout<< "First column of C[rc x rc]: "<<std::endl;
//  for (unsigned int p=0; p<rc; ++p)
//    std::cout<< T[p] << ", ";
//  std::cout<<std::endl;

  // Apply DFT to T
392
  FComplexe FT[ncmp*rc];
393
  //  for (unsigned int n=0; n<rc; ++n) FT[n]=FComplexe(0.0,0.0);
394
  FBlas::c_setzero(ncmp*rc,reinterpret_cast<FReal*>(FT));
395 396

  // if first COLUMN (T) of C is used
397
  for (unsigned int d=0; d<ncmp; ++d)
398
    Dft.applyDFT(T+d*rc,FT+d*rc);
399
//  // if first ROW of C is used
400
//  Dft.applyDFT(C,FT);
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

  FComplexe FPMultExp[nrhs*rc];
  FComplexe FPLocalExp[nlhs*rc];
  FReal PLocalExp[nlhs*rc];

  //for (unsigned int n=0; n<rc; ++n) FPLocalExp[n]=FComplexe(0.0,0.0);
  FBlas::c_setzero(nlhs*rc,reinterpret_cast<FReal*>(FPLocalExp));

  FBlas::setzero(nlhs*rc,PLocalExp);

  // Pad physical MultExp
  FBlas::setzero(nrhs*rc,PaddedMultExp); //part of padding
  for (unsigned int idRhs=0; idRhs<nrhs; ++idRhs)
    for (unsigned int j=0; j<nnodes; ++j){
      // if first COLUMN (T) of C is used
      PaddedMultExp[node_diff[j*nnodes]+idRhs*rc]=MultExp[j+idRhs*nnodes];
//    // if first ROW of C is used
//    PaddedMultExp[node_diff[j]]=MultExp[j];
    }

//    std::cout<< "Padded MultExp: "<<std::endl;
//    for (unsigned int p=0; p<rc; ++p)
//      std::cout<< PaddedMultExp[p] << ", ";
//    std::cout<<std::endl;


  // Set transformed MultExp to 0
  //  for (unsigned int n=0; n<rc; ++n) FPMultExp[n]=FComplexe(0.0,0.0);
  FBlas::c_setzero(nrhs*rc,reinterpret_cast<FReal*>(FPMultExp));

  // Transform PaddedMultExp
  for (unsigned int idxRhs=0; idxRhs<nrhs; ++idxRhs) // apply nrhs 1 dimensionnal FFT
433
    Dft.applyDFT(PaddedMultExp+idxRhs*rc,FPMultExp+idxRhs*rc);
434 435 436 437 438 439 440 441 442 443 444 445

  std::cout<< "Apply M2L in Fourier space: ";
  time.tic();

  // Application of M2L in FOURIER SPACE
  // > Use FMkl::c_had for hadamard product
  // if mkl is used as blas (TODO otherwise use FBlas::c_had())
//  FMkl::c_had(rc,reinterpret_cast<FReal*>(FT),
//            reinterpret_cast<FReal*>(FPMultExp),
//            reinterpret_cast<FReal*>(FPLocalExp));
  // > or perform entrywise product manually
  FComplexe tmpFX;
446 447 448 449 450 451 452 453 454
  for (unsigned int idxLhs=0; idxLhs<nlhs; ++idxLhs){
    unsigned int idxRhs = idxLhs % npot;
    unsigned int d = MatrixKernel.getPosition(idxLhs);

    for (unsigned int pj=0; pj<rc; ++pj){
      tmpFX=FT[pj + d*rc];
      tmpFX*=FPMultExp[pj+idxRhs*rc];
      FPLocalExp[pj+idxLhs*rc]+=tmpFX; // add new contribution +RijYj
    }
455 456 457 458 459 460 461 462 463 464 465

  }
  time.tac();
  std::cout << "took " << time.elapsed() << "sec." << std::endl;

//    std::cout<< "Transfo Padded LocalExp: "<<std::endl;
//    for (unsigned int p=0; p<rc; ++p)
//      std::cout<< FPLocalExp[p] << ", ";
//    std::cout<<std::endl;

  for (unsigned int idxLhs=0; idxLhs<nlhs; ++idxLhs) // apply nrhs 1 dimensionnal FFT
466
    Dft.applyIDFT(FPLocalExp+idxLhs*rc,PLocalExp+idxLhs*rc);
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

//  std::cout<< "Padded LocalExp: "<<std::endl;
//  for (unsigned int p=0; p<rc; ++p)
//    std::cout<< PLocalExp[p] << ", ";
//  std::cout<<std::endl;

  // Unpad
  for (unsigned int idLhs=0; idLhs<nlhs; ++idLhs)
    for (unsigned int j=0; j<nnodes; ++j){
      // if first COLUMN (T) of C is used
      LocalExp[j+idLhs*nnodes]=PLocalExp[node_diff[nnodes-j-1]+idLhs*rc];
//    // if first ROW of C is used
//    LocalExp[j]=PLocalExp[node_diff[j*nnodes]];
    }

//  std::cout<< "Mask to be applied to Padded LocalExp: "<<std::endl;
//  for (unsigned int j=0; j<nnodes; ++j)
//    std::cout<< node_diff[nnodes-j-1] << ", ";
//  std::cout<<std::endl;

//  std::cout<< "LocalExp via product in FOURIER SPACE: "<<std::endl;
//  for (unsigned int d=0; d<nlhs; ++d){
//    for (unsigned int p=0; p<nnodes; ++p)
//      std::cout<< LocalExp[p + d*nnodes] << ", ";
//    std::cout<<std::endl;
//  }
//  std::cout<<std::endl;


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

  std::cout << "L2P (potential) ... " << std::flush;
  time.tic();
  // Interpolate p_i = \sum_m^L S(x_i,\bar x_m) * F_m
501
  S.applyL2P(cx, width, F, X.getTargets());
502 503 504 505 506
  std::cout << "took " << time.tacAndElapsed() << "s" << std::endl;

  std::cout << "L2P (forces) ... " << std::flush;
  time.tic();
  // Interpolate f_i = \sum_m^L P(x_i,\bar x_m) * F_m
507
  S.applyL2PGradient(cx, width, F, X.getTargets());
508 509 510 511 512 513 514
  std::cout << "took " << time.tacAndElapsed() << "s" << std::endl;

  ////////////////////////////////////////////////////////////////////
  // direct computation 
  std::cout << "Compute interactions directly ..." << std::endl;
  time.tic();

515 516 517
  FReal** approx_f = new FReal* [npot];
  FReal**        f = new FReal* [npot];
  for (unsigned int i=0; i<npot; ++i){
518 519 520 521
    approx_f[i] = new FReal [M * 3];
    f[i] = new FReal [M * 3];
    FBlas::setzero(M*3, f[i]);
  }
522

523 524 525
  FReal** approx_p = new FReal* [npot];
  FReal**        p = new FReal* [npot];
  for (unsigned int i=0; i<npot; ++i){
526 527 528 529
    approx_p[i] = new FReal [M];
    p[i] = new FReal [M];
    FBlas::setzero(M, p[i]);
  }
530 531 532 533 534 535 536 537

  { // start direct computation
    unsigned int counter = 0;

    for(int idxPartX = 0 ; idxPartX < X.getSrc()->getNbParticles() ; ++idxPartX){
      const FPoint x = FPoint(X.getSrc()->getPositions()[0][idxPartX],
                              X.getSrc()->getPositions()[1][idxPartX],
                              X.getSrc()->getPositions()[2][idxPartX]);
538 539 540
      const FReal wx[nrhs] = {X.getSrc()->getPhysicalValues(0)[idxPartX],
                              X.getSrc()->getPhysicalValues(1)[idxPartX],
                              X.getSrc()->getPhysicalValues(2)[idxPartX]}; 
541 542 543 544 545

      for(int idxPartY = 0 ; idxPartY < Y.getSrc()->getNbParticles() ; ++idxPartY){
        const FPoint y = FPoint(Y.getSrc()->getPositions()[0][idxPartY],
                                Y.getSrc()->getPositions()[1][idxPartY],
                                Y.getSrc()->getPositions()[2][idxPartY]);
546 547 548 549
        const FReal wy[nrhs] = {Y.getSrc()->getPhysicalValues(0)[idxPartY],
                                Y.getSrc()->getPhysicalValues(1)[idxPartY],
                                Y.getSrc()->getPhysicalValues(2)[idxPartY]};
        
550 551 552 553 554 555 556 557 558 559 560 561 562
//        // 1/R
//        const FReal one_over_r = MatrixKernel.evaluate(x, y);
//
//        // potential
//        p[counter] += one_over_r * wy;
//        // force
//        FPoint force(y - x);
//        force *= one_over_r*one_over_r*one_over_r;
//        f[counter*3 + 0] += force.getX() * wx * wy;
//        f[counter*3 + 1] += force.getY() * wx * wy;
//        f[counter*3 + 2] += force.getZ() * wx * wy;

        // R,ij and (R,ij),k
563
        for (unsigned int i=0; i<npot; ++i) // sum all compo
564 565
          for (unsigned int j=0; j<nrhs; ++j){
            unsigned int d = MatrixKernel.getPosition(i*nrhs+j);
566
            const FReal rij = MatrixKernelClass(a,d).evaluate(x, y);
567
            // potential
568
            p[i][counter] += rij * wy[j];
569
            // force
570
            FReal force_k;
571
            for (unsigned int k=0; k<3; ++k){
572
              unsigned int dk = RIJKMatrixKernel.getPosition((i*nrhs+j)*3+k);
573 574
              // Convention in matrix kernel: R_ij(x-y), while R_ijk(y-x)
              force_k = FReal(-1.) * RIJKMatrixKernelClass(a,dk).evaluate(x, y);
575
              f[i][counter*3 + k] += force_k * wx[j] * wy[j];
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
            }
          }

      }
      counter++;
    }
  } // end direct computation


  time.tac();
  std::cout << "Done in " << time.elapsed() << "sec." << std::endl;


  ////////////////////////////////////////////////////////////////////
  unsigned int counter = 0;
  for(int idxPartX = 0 ; idxPartX < X.getSrc()->getNbParticles() ; ++idxPartX){
592
    for (unsigned int i=0; i<npot; ++i){
593 594 595 596 597 598 599 600
      approx_p[i][counter] = X.getSrc()->getPotentials(i)[idxPartX];
      const FPoint force = FPoint(X.getSrc()->getForcesX(i)[idxPartX],
                                  X.getSrc()->getForcesY(i)[idxPartX],
                                  X.getSrc()->getForcesZ(i)[idxPartX]);
      approx_f[i][counter*3 + 0] = force.getX();
      approx_f[i][counter*3 + 1] = force.getY();
      approx_f[i][counter*3 + 2] = force.getZ();
    }
601 602 603
    counter++;
  }

604
//  std::cout << "Check Potential, forceX, forceY, forceZ " << std::endl;
605
//  for (unsigned int i=0; i<npot; ++i){
606 607 608 609 610 611 612 613 614 615 616
//    std::cout<< "idxLhs="<< i << std::endl;
//    for(int idxPart = 0 ; idxPart < 20 ; ++idxPart){
//      std::cout << approx_p[i][idxPart]     << ", "<< p[i][idxPart] << "|| ";
//      std::cout << approx_f[i][idxPart]     << ", "<< f[i][idxPart] << "|| ";
//      std::cout << approx_f[i][idxPart+M]   << ", "<< f[i][idxPart+M] << "|| ";
//      std::cout << approx_f[i][idxPart+2*M] << ", "<< f[i][idxPart+2*M] << "|| ";
//      std::cout << std::endl;
//    }
//    std::cout << std::endl;
//  }
//  std::cout << std::endl;
617

618 619 620 621 622 623 624 625 626
  std::cout << "\nRelative Inf/L2 errors: " << std::endl;
  std::cout << "  Potential:" << std::endl;
  for(unsigned i = 0; i<npot;++i) {
    std::cout << "    " << i << ": "
              << FMath::FAccurater(p[i],approx_p[i],M).getRelativeInfNorm()<<", " 
              << FMath::FAccurater(p[i],approx_p[i],M).getRelativeL2Norm()
              << std::endl;
  }
  std::cout << std::endl;
627

628 629 630 631 632 633 634
  std::cout << "  Force:" << std::endl;
  for(unsigned i = 0; i<npot;++i) {
    std::cout << "    " << i << ": "
              << FMath::FAccurater(f[i],approx_f[i],3*M).getRelativeInfNorm()<<", " 
              << FMath::FAccurater(f[i],approx_f[i],3*M).getRelativeL2Norm()
              << std::endl;
  }
635 636 637 638 639 640 641 642 643 644 645
  std::cout << std::endl;

  // free memory
  delete [] approx_p;
  delete [] p;
  delete [] approx_f;
  delete [] f;


  return 0;
}