utestChebyshevDirectPeriodic.cpp 30.4 KB
Newer Older
1
// See LICENCE file at project root
2 3 4 5 6

// ==== CMAKE =====
// @FUSE_BLAS
// ================

7
#include "Utils/FGlobal.hpp"
8

9 10
#include "Containers/FOctree.hpp"
#include "Containers/FVector.hpp"
11

12
#include "Files/FFmaGenericLoader.hpp"
13 14
#include "Files/FRandomLoader.hpp"
#include "Files/FTreeIO.hpp"
15

16
#include "Core/FFmmAlgorithmPeriodic.hpp"
17
#include "Core/FFmmAlgorithm.hpp"
18
#include "Core/FFmmAlgorithmThread.hpp"
19 20

#include "FUTester.hpp"
21
#include "Utils/FMath.hpp"
22

23

24 25 26 27
#include "Kernels/Chebyshev/FChebCell.hpp"
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Chebyshev/FChebKernel.hpp"
#include "Kernels/Chebyshev/FChebSymKernel.hpp"
28

29 30
#include "Components/FSimpleLeaf.hpp"
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
31

32
/*
COULAUD Olivier's avatar
COULAUD Olivier committed
33 34
  In this test we compare the Chebyshev fmm results and the direct results.
 */
35 36 37 38 39 40


/** the test class
 *
 */
class TestChebyshevDirect : public FUTester<TestChebyshevDirect> {
41 42 43
    ///////////////////////////////////////////////////////////
    // The tests!
    ///////////////////////////////////////////////////////////
COULAUD Olivier's avatar
COULAUD Olivier committed
44

45
    template <class FReal, class CellClass, class ContainerClass, class KernelClass, class MatrixKernelClass,
46
              class LeafClass, class OctreeClass, class FmmClass>
47
    void RunTest(const int PeriodicDeep){
48 49 50 51 52 53 54 55 56 57 58 59 60
        // Configs
        const int NbLevels      = 4;
        const int SizeSubLevels = 2;

        const FSize NbParticles     = 16;
        FRandomLoader<FReal> loader(NbParticles);
        FReal BoxWidth = loader.getBoxWidth();
        FPoint<FReal> CenterOfBox = loader.getCenterOfBox();

        // Create octrees
        OctreeClass tree(NbLevels, SizeSubLevels,BoxWidth,CenterOfBox);

        struct TestParticle{
61
            FPoint<FReal> position;
62 63 64 65 66 67 68 69 70
            FReal physicalValue;
            FReal potential;
            FReal forces[3];
        };

        // Insert the particles
        FReal coeff = -1.0, value = 0.10, sum = 0.0;
        TestParticle* const particles = new TestParticle[loader.getNumberOfParticles()];
        for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
71
            FPoint<FReal> position;
72 73 74 75 76 77 78 79 80 81 82 83 84 85
            loader.fillParticle(&position);
            // put in tree
            value *= coeff ;
            sum   += value ;
            // put in tree
            tree.insert(position, idxPart, value);
            // get copy
            particles[idxPart].position         = position;
            particles[idxPart].physicalValue = value;
            particles[idxPart].potential        = 0.0;
            particles[idxPart].forces[0]        = 0.0;
            particles[idxPart].forces[1]        = 0.0;
            particles[idxPart].forces[2]        = 0.0;
        }
86
        if (FMath::Abs(sum)> 0.00001){
87 88
            std::cerr << "Sum of charges is not equal zero!!! (sum=<<"<<sum<<" )"<<std::endl;
            exit(-1);
89
        }
90
        coeff = FMath::Abs(coeff)* static_cast<int>(NbParticles)  ;
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
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // Run FMM computation
        /////////////////////////////////////////////////////////////////////////////////////////////////
        Print("Fmm...");
        const MatrixKernelClass MatrixKernel;
        FmmClass algo(&tree, PeriodicDeep );
        {

            printf("Levels %d\n", NbLevels);
            printf("PeriodicDeep %d\n", PeriodicDeep);
            printf("Extended tree height %d\n", algo.extendedTreeHeight());
            printf("Width %e\n", BoxWidth);
            printf("Extended Width %e\n", algo.extendedBoxWidth());

            KernelClass *kernels = new KernelClass(algo.extendedTreeHeight(), algo.extendedBoxWidth(), algo.extendedBoxCenter(),&MatrixKernel);
            algo.setKernel(kernels);
            algo.execute();
            delete kernels ;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // Run direct computation
        /////////////////////////////////////////////////////////////////////////////////////////////////
        Print("Direct...");
        FReal energy= 0.0 , energyD = 0.0 ;
        {
            FTreeCoordinate min, max;
            algo.repetitionsIntervals(&min, &max);
            const int totalRepetitions = (max.getX()-min.getX()+1) * (max.getY()-min.getY()+1) * (max.getZ()-min.getZ()+1);
            printf("Repetitions [%d , %d]x[%d , %d]x[%d , %d] = %d (%lld)\n",min.getX(), max.getX(),
                   min.getY(), max.getY(),min.getZ(), max.getZ(),
                   totalRepetitions, algo.theoricalRepetition()*algo.theoricalRepetition()*algo.theoricalRepetition());
            uassert(totalRepetitions == algo.theoricalRepetition()*algo.theoricalRepetition()*algo.theoricalRepetition());

124
            #pragma omp parallel for
125
            for(FSize idxTarget = 0 ; idxTarget < loader.getNumberOfParticles() ; ++idxTarget){
126 127 128 129 130 131 132
                for(FSize idxOther =  0 ; idxOther < loader.getNumberOfParticles() ; ++idxOther){
                    if(idxTarget != idxOther){
                        FP2P::NonMutualParticles(
                                particles[idxTarget].position.getX(), particles[idxTarget].position.getY(),
                                particles[idxTarget].position.getZ(),particles[idxTarget].physicalValue,
                                &particles[idxTarget].forces[0],&particles[idxTarget].forces[1],
                                &particles[idxTarget].forces[2],&particles[idxTarget].potential,
133 134
                                particles[idxOther].position.getX(), particles[idxOther].position.getY(),
                                particles[idxOther].position.getZ(),particles[idxOther].physicalValue,
135 136
                                &MatrixKernel);
                    }
137 138 139 140 141 142 143 144 145 146 147 148

                }
                for(int idxX = min.getX() ; idxX <= max.getX() ; ++idxX){
                    for(int idxY = min.getY() ; idxY <= max.getY() ; ++idxY){
                        for(int idxZ = min.getZ() ; idxZ <= max.getZ() ; ++idxZ){
                            if(idxX ==0 && idxY == 0 && idxZ == 0) continue;

                            const FPoint<FReal> offset(loader.getBoxWidth() * FReal(idxX),
                                                       loader.getBoxWidth() * FReal(idxY),
                                                       loader.getBoxWidth() * FReal(idxZ));

                            for(int idxSource = 0 ; idxSource < NbParticles ; ++idxSource){
149 150 151 152 153 154 155
                                TestParticle source;
                                source.position = particles[idxSource].position;
                                source.physicalValue = particles[idxSource].physicalValue;
                                source.potential = particles[idxSource].potential;
                                source.forces[0] = particles[idxSource].forces[0];
                                source.forces[1] = particles[idxSource].forces[1];
                                source.forces[2] = particles[idxSource].forces[2];
156

157
                                source.position += offset;
158 159 160 161
                                FP2P::NonMutualParticles(
                                            particles[idxTarget].position.getX(), particles[idxTarget].position.getY(),
                                            particles[idxTarget].position.getZ(),particles[idxTarget].physicalValue,
                                            &particles[idxTarget].forces[0],&particles[idxTarget].forces[1],
162 163 164 165
                                            &particles[idxTarget].forces[2],&particles[idxTarget].potential,
                                            source.position.getX(), source.position.getY(),
                                            source.position.getZ(),source.physicalValue,
                                            &MatrixKernel);
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
                            }
                        }
                    }
                }
            }
            for(FSize idx = 0 ; idx < loader.getNumberOfParticles()  ; ++idx){
                energyD +=  particles[idx].potential*particles[idx].physicalValue ;
            }
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // Compare
        /////////////////////////////////////////////////////////////////////////////////////////////////

        Print("Compute Diff...");
        FMath::FAccurater<FReal> potentialDiff;
        FMath::FAccurater<FReal> fx, fy, fz;

        { // Check that each particle has been summed with all other

            tree.forEachLeaf([&](LeafClass* leaf){
                const FReal*const potentials        = leaf->getTargets()->getPotentials();
                const FReal*const physicalValues = leaf->getTargets()->getPhysicalValues();
                const FReal*const forcesX = leaf->getTargets()->getForcesX();
                const FReal*const forcesY = leaf->getTargets()->getForcesY();
                const FReal*const forcesZ = leaf->getTargets()->getForcesZ();
                const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();

                for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
                    const FSize indexPartOrig = indexes[idxPart];
                    potentialDiff.add(particles[indexPartOrig].potential,potentials[idxPart]);
                    fx.add(particles[indexPartOrig].forces[0],forcesX[idxPart]);
                    fy.add(particles[indexPartOrig].forces[1],forcesY[idxPart]);
                    fz.add(particles[indexPartOrig].forces[2],forcesZ[idxPart]);
                    energy+=potentials[idxPart]*physicalValues[idxPart];
                }
            });
        }
        // Print for information
COULAUD Olivier's avatar
COULAUD Olivier committed
205

COULAUD Olivier's avatar
COULAUD Olivier committed
206 207 208
        Print("Potential diff is = ");
        printf("         Pot L2Norm     %e\n",potentialDiff.getL2Norm());
        printf("         Pot RL2Norm   %e\n",potentialDiff.getRelativeL2Norm());
209
        printf("         Pot RMSError   %e\n",potentialDiff.getRMSError());
COULAUD Olivier's avatar
COULAUD Olivier committed
210
        Print("Fx diff is = ");
211 212 213
        printf("         Fx L2Norm     %e\n",fx.getL2Norm());
        printf("         Fx RL2Norm   %e\n",fx.getRelativeL2Norm());
        printf("         Fx RMSError   %e\n",fx.getRMSError());
COULAUD Olivier's avatar
COULAUD Olivier committed
214
        Print("Fy diff is = ");
215 216 217
        printf("        Fy L2Norm     %e\n",fy.getL2Norm());
        printf("        Fy RL2Norm   %e\n",fy.getRelativeL2Norm());
        printf("        Fy RMSError   %e\n",fy.getRMSError());
COULAUD Olivier's avatar
COULAUD Olivier committed
218
        Print("Fz diff is = ");
219 220 221
        printf("        Fz L2Norm     %e\n",fz.getL2Norm());
        printf("        Fz RL2Norm   %e\n",fz.getRelativeL2Norm());
        printf("        Fz RMSError   %e\n",fz.getRMSError());
COULAUD Olivier's avatar
COULAUD Olivier committed
222
        FReal L2error = (fx.getRelativeL2Norm()*fx.getRelativeL2Norm() + fy.getRelativeL2Norm()*fy.getRelativeL2Norm()  + fz.getRelativeL2Norm() *fz.getRelativeL2Norm()  );
223 224 225 226 227
        printf(" Total L2 Force Error= %e\n",FMath::Sqrt(L2error)) ;
        printf("  Energy Error  =   %.12e\n",FMath::Abs(energy-energyD));
        printf("  R Energy Error  =   %.12e\n",FMath::Abs(energy-energyD)/FMath::Abs(energyD));
        printf("  Energy FMM    =   %.12e\n",FMath::Abs(energy));
        printf("  Energy DIRECT =   %.12e\n",FMath::Abs(energyD));
COULAUD Olivier's avatar
COULAUD Olivier committed
228

229
        // Assert
COULAUD Olivier's avatar
COULAUD Olivier committed
230 231 232 233
        const FReal MaximumDiffPotential = FReal(9e-3);
        const FReal MaximumDiffForces     = FReal(9e-2);

        uassert(potentialDiff.getL2Norm() < MaximumDiffPotential);    //1
234
        //     uassert(potentialDiff.getRMSError() < MaximumDiffPotential);  //2
COULAUD Olivier's avatar
COULAUD Olivier committed
235
        uassert(fx.getL2Norm()  < MaximumDiffForces);                       //3
236
        //      uassert(fx.getRMSError() < MaximumDiffForces);                      //4
COULAUD Olivier's avatar
COULAUD Olivier committed
237
        uassert(fy.getL2Norm()  < MaximumDiffForces);                       //5
238
        //       uassert(fy.getRMSError() < MaximumDiffForces);                      //6
COULAUD Olivier's avatar
COULAUD Olivier committed
239
        uassert(fz.getL2Norm()  < MaximumDiffForces);                      //8
240
        //      uassert(fz.getRMSError() < MaximumDiffForces);                                           //8
COULAUD Olivier's avatar
COULAUD Olivier committed
241
        uassert(L2error              < MaximumDiffForces);                                            //9   Total Force
242
        uassert(FMath::Abs(energy-energyD) / FMath::Abs(energyD) < coeff*MaximumDiffPotential);                     //10  Total Energy
243

244
        delete[] particles;
245 246
    }

247

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    template <class FReal, class CellClass, class ContainerClass, class KernelClass, class MatrixKernelClass,
              class LeafClass, class OctreeClass, class FmmClass,
              class FmmClassNonPer>
    void RunTestUpward(const int PeriodicDeep)	{
        // Configs
        const int NbLevels      = 4;
        const int SizeSubLevels = 2;

        const FSize NbParticles     = 150;
        FRandomLoader<FReal> loader(NbParticles);
        FReal BoxWidth = loader.getBoxWidth();
        FPoint<FReal> CenterOfBox = loader.getCenterOfBox();

        // Create octrees
        OctreeClass tree(NbLevels, SizeSubLevels,BoxWidth,CenterOfBox);
        OctreeClass treeNonper(NbLevels, SizeSubLevels,BoxWidth,CenterOfBox);

        struct TestParticle{
            FPoint<FReal> position;
            FReal physicalValue;
            FReal potential;
            FReal forces[3];
        };

        // Insert the particles
        FReal coeff = -1.0, value = 0.10, sum = 0.0;
        for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
            FPoint<FReal> position;
            loader.fillParticle(&position);
            // put in tree
            value *= coeff ;
            sum   += value ;
            // put in tree
            tree.insert(position, idxPart, value);
            treeNonper.insert(position, idxPart, value);
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // Run FMM computation
        /////////////////////////////////////////////////////////////////////////////////////////////////
        Print("Fmm...");
        const MatrixKernelClass MatrixKernel;
        {
            FmmClass algo(&tree, PeriodicDeep );

            printf("Levels %d\n", NbLevels);
            printf("PeriodicDeep %d\n", PeriodicDeep);
            printf("Extended tree height %d\n", algo.extendedTreeHeight());
            printf("Width %e\n", BoxWidth);
            printf("Extended Width %e\n", algo.extendedBoxWidth());

            FTreeCoordinate min, max;
            algo.repetitionsIntervals(&min, &max);
            const int totalRepetitions = (max.getX()-min.getX()+1) * (max.getY()-min.getY()+1) * (max.getZ()-min.getZ()+1);
            printf("Repetitions [%d , %d]x[%d , %d]x[%d , %d] = %d (%lld)\n",min.getX(), max.getX(),
                   min.getY(), max.getY(),min.getZ(), max.getZ(),
                   totalRepetitions, algo.theoricalRepetition()*algo.theoricalRepetition()*algo.theoricalRepetition());

            KernelClass *kernels = new KernelClass(algo.extendedTreeHeight(), algo.extendedBoxWidth(), algo.extendedBoxCenter(),&MatrixKernel);
            algo.setKernel(kernels);
            algo.execute();
            delete kernels ;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // Run upper non periodic FMM computation
        /////////////////////////////////////////////////////////////////////////////////////////////////
        Print("Upper non periodic Fmm...");
        {
            KernelClass *kernels = new KernelClass(NbLevels, BoxWidth, CenterOfBox,&MatrixKernel);
            FmmClassNonPer algoNonPer(&treeNonper, kernels);
            algoNonPer.execute(FFmmP2M | FFmmM2M);
            delete kernels ;

            typename OctreeClass::Iterator iter(&tree);
            typename OctreeClass::Iterator iterNonPer(&treeNonper);

            iter.gotoBottomLeft();
            iterNonPer.gotoBottomLeft();


            for(int idxLevel = NbLevels-1 ; idxLevel >= 2 ; --idxLevel){
                while(true){
                    uassert(iter.getCurrentGlobalIndex() == iterNonPer.getCurrentGlobalIndex());

                    CellClass* cl = iter.getCurrentCell();
                    CellClass* clnonper = iterNonPer.getCurrentCell();

                    FMath::FAccurater<FReal> accurater;

                    for(int idx = 0; idx < cl->getVectorSize(); ++idx){
337 338
                        accurater.add(cl->getMultipoleData().get(0)[idx],
                                      clnonper->getMultipoleData().get(0)[idx]);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
                    }
                    uassert(accurater.getInfNorm() < 1E-10);
                    uassert(accurater.getL2Norm() < 1E-10);

                    const bool hasNext = iter.moveRight();
                    const bool hasNextNonPer = iterNonPer.moveRight();
                    uassert(hasNext == hasNextNonPer);
                    if(!hasNext || !hasNextNonPer){
                        break;
                    }
                }
                iter.moveUp();
                iter.gotoLeft();
                iterNonPer.moveUp();
                iterNonPer.gotoLeft();
            }
        }
    }
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
    template <class FReal, class CellClass, class ContainerClass, class KernelClass, class MatrixKernelClass,
              class LeafClass, class OctreeClass, class FmmClass,
              class FmmClassNonPer>
    void RunTestFake(const int PeriodicDeep)	{
        // Configs
        const int NbLevels      = 4;
        const int SizeSubLevels = 2;

        const FSize NbParticles     = 150;
        FRandomLoader<FReal> loader(NbParticles);
        FReal BoxWidth = loader.getBoxWidth();
        FPoint<FReal> CenterOfBox = loader.getCenterOfBox();

        // Create octrees
        OctreeClass tree(NbLevels, SizeSubLevels,BoxWidth,CenterOfBox);

        struct TestParticle{
            FPoint<FReal> position;
            FReal physicalValue;
            FReal potential;
            FReal forces[3];
        };

        // Insert the particles
        FReal coeff = -1.0, value = 0.10, sum = 0.0;
        TestParticle* const particles = new TestParticle[loader.getNumberOfParticles()];
        for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
            FPoint<FReal> position;
            loader.fillParticle(&position);
            // put in tree
            value *= coeff ;
            sum   += value ;
            // put in tree
            tree.insert(position, idxPart, value);
            // get copy
            particles[idxPart].position         = position;
            particles[idxPart].physicalValue    = value;
            particles[idxPart].potential        = 0.0;
            particles[idxPart].forces[0]        = 0.0;
            particles[idxPart].forces[1]        = 0.0;
            particles[idxPart].forces[2]        = 0.0;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // Run FMM computation
        /////////////////////////////////////////////////////////////////////////////////////////////////
        Print("Fmm...");
        const MatrixKernelClass MatrixKernel;
        FmmClass algo(&tree, PeriodicDeep );
        {

            printf("Levels %d\n", NbLevels);
            printf("PeriodicDeep %d\n", PeriodicDeep);
            printf("Extended tree height %d\n", algo.extendedTreeHeight());
            printf("Width %e\n", BoxWidth);
            printf("Extended Width %e\n", algo.extendedBoxWidth());

            KernelClass *kernels = new KernelClass(algo.extendedTreeHeight(), algo.extendedBoxWidth(), algo.extendedBoxCenter(),&MatrixKernel);
            algo.setKernel(kernels);
            algo.execute();
            delete kernels ;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // Run direct computation
        /////////////////////////////////////////////////////////////////////////////////////////////////
        Print("Direct...");
        printf("Extended tree height boundary %d\n", algo.extendedTreeHeightBoundary());
        printf("Extended Width boundary %e\n", algo.extendedBoxWidthBoundary());
        OctreeClass treeNonper(algo.extendedTreeHeightBoundary(), SizeSubLevels, algo.extendedBoxWidthBoundary(), algo.extendedBoxCenterBoundary());
        {
            FTreeCoordinate min, max;
            algo.repetitionsIntervals(&min, &max);
            const int totalRepetitions = (max.getX()-min.getX()+1) * (max.getY()-min.getY()+1) * (max.getZ()-min.getZ()+1);
            printf("Repetitions [%d , %d]x[%d , %d]x[%d , %d] = %d (%lld)\n",min.getX(), max.getX(),
                   min.getY(), max.getY(),min.getZ(), max.getZ(),
                   totalRepetitions, algo.theoricalRepetition()*algo.theoricalRepetition()*algo.theoricalRepetition());
            uassert(totalRepetitions == algo.theoricalRepetition()*algo.theoricalRepetition()*algo.theoricalRepetition());

            for(int idxX = min.getX() ; idxX <= max.getX() ; ++idxX){
                for(int idxY = min.getY() ; idxY <= max.getY() ; ++idxY){
                    for(int idxZ = min.getZ() ; idxZ <= max.getZ() ; ++idxZ){
                        const FPoint<FReal> offset(loader.getBoxWidth() * FReal(idxX),
                                                   loader.getBoxWidth() * FReal(idxY),
                                                   loader.getBoxWidth() * FReal(idxZ));

                        for(int idxSource = 0 ; idxSource < NbParticles ; ++idxSource){
                            TestParticle source = particles[idxSource];
                            source.position += offset;

                            treeNonper.insert(source.position, idxSource, source.physicalValue);
                        }
                    }
                }
            }
        }
        {
            KernelClass *kernels = new KernelClass(algo.extendedTreeHeightBoundary(), algo.extendedBoxWidthBoundary(), algo.extendedBoxCenterBoundary(),&MatrixKernel);
            FmmClassNonPer algoNonPer(&treeNonper, kernels);
            algoNonPer.execute();
            delete kernels ;
        }
        {
            typename OctreeClass::Iterator iter(&tree);

            iter.gotoBottomLeft();

464
	    //            const int diffLevel = algo.extendedTreeHeightBoundary()-NbLevels;
465 466
            const int idxLevel = NbLevels-1;
            const int offsetAtLevel = (PeriodicDeep == -1? 1<<idxLevel : (3 << idxLevel));
467
            do {
468 469 470 471
                const FTreeCoordinate realCoord = iter.getCurrentGlobalCoordinate();
                const FTreeCoordinate boundaryCoord(realCoord.getX() + offsetAtLevel,
                                                    realCoord.getY() + offsetAtLevel,
                                                    realCoord.getZ() + offsetAtLevel);
472
                const MortonIndex boundaryIndex = boundaryCoord.getMortonIndex();
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

                uassert((boundaryIndex & ~((~0LL)<<3*idxLevel))== iter.getCurrentGlobalIndex());

                ContainerClass* lfnonper = treeNonper.getLeafSrc(boundaryIndex);
                uassert(lfnonper);
                if(lfnonper){
                    ContainerClass* lf = iter.getCurrentLeaf()->getSrc();

                    uassert(lfnonper->getNbParticles() == lf->getNbParticles());

                    if(lfnonper->getNbParticles() == lf->getNbParticles()){
                        FMath::FAccurater<FReal> potentialDiff;
                        FMath::FAccurater<FReal> fx, fy, fz;

                        const FReal*const potentials        = lf->getPotentials();
                        const FReal*const forcesX = lf->getForcesX();
                        const FReal*const forcesY = lf->getForcesY();
                        const FReal*const forcesZ = lf->getForcesZ();
                        const FSize nbParticlesInLeaf = lf->getNbParticles();
                        const FVector<FSize>& indexes = lf->getIndexes();


                        const FReal*const potentialsnonper        = lfnonper->getPotentials();
                        const FReal*const forcesXnonper = lfnonper->getForcesX();
                        const FReal*const forcesYnonper = lfnonper->getForcesY();
                        const FReal*const forcesZnonper = lfnonper->getForcesZ();
                        const FVector<FSize>& indexesnonper = lfnonper->getIndexes();

                        for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
                            uassert(indexes[idxPart] == indexesnonper[idxPart]);
                            potentialDiff.add(potentials[idxPart],potentialsnonper[idxPart]);
                            fx.add(forcesX[idxPart],forcesXnonper[idxPart]);
                            fy.add(forcesY[idxPart],forcesYnonper[idxPart]);
                            fz.add(forcesZ[idxPart],forcesZnonper[idxPart]);
                        }
                        uassert(potentialDiff.getInfNorm() < 1E-10);
                        uassert(potentialDiff.getL2Norm() < 1E-10);
                        uassert(fx.getInfNorm() < 1E-10);
                        uassert(fx.getL2Norm() < 1E-10);
                        uassert(fy.getInfNorm() < 1E-10);
                        uassert(fy.getL2Norm() < 1E-10);
                        uassert(fz.getInfNorm() < 1E-10);
                        uassert(fz.getL2Norm() < 1E-10);
                    }
                }
            } while(iter.moveRight());
        }
        {
            typename OctreeClass::Iterator iter(&tree);

            iter.gotoBottomLeft();

            const int diffLevel = algo.extendedTreeHeightBoundary()-NbLevels;

527
            for(int idxLevel = NbLevels-1 ; idxLevel >= 1 ; --idxLevel){
528
                const int offsetAtLevel = (PeriodicDeep == -1? 1<<idxLevel : (3 << idxLevel));
529
                do {
530 531 532 533
                    const FTreeCoordinate realCoord = iter.getCurrentGlobalCoordinate();
                    const FTreeCoordinate boundaryCoord(realCoord.getX() + offsetAtLevel,
                                                        realCoord.getY() + offsetAtLevel,
                                                        realCoord.getZ() + offsetAtLevel);
534
                    const MortonIndex boundaryIndex = boundaryCoord.getMortonIndex();
535 536 537 538 539 540 541 542 543 544 545

                    uassert((boundaryIndex & ~((~0LL)<<3*idxLevel))== iter.getCurrentGlobalIndex());

                    CellClass* clnonper = treeNonper.getCell(boundaryIndex, idxLevel + diffLevel);
                    uassert(clnonper);
                    if(clnonper){
                        CellClass* cl = iter.getCurrentCell();

                        FMath::FAccurater<FReal> accurater;

                        for(int idx = 0; idx < cl->getVectorSize(); ++idx){
546 547
                            accurater.add(cl->getMultipoleData().get(0)[idx],
                                          clnonper->getMultipoleData().get(0)[idx]);
548 549 550 551 552 553 554
                        }
                        uassert(accurater.getInfNorm() < 1E-10);
                        uassert(accurater.getL2Norm() < 1E-10);

                        FMath::FAccurater<FReal> accuraterLocal;

                        for(int idx = 0; idx < cl->getVectorSize(); ++idx){
555 556
                            accuraterLocal.add(cl->getLocalExpansionData().get(0)[idx],
                                               clnonper->getLocalExpansionData().get(0)[idx]);
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
                        }
                        uassert(accuraterLocal.getInfNorm() < 1E-8);
                        uassert(accuraterLocal.getL2Norm() < 1E-8);
                    }

                } while(iter.moveRight());
                iter.moveUp();
                iter.gotoLeft();
            }
        }
        delete[] particles;
    }

    /** If memstas is running print the memory used */
    void PostTest() {
        if( FMemStats::controler.isUsed() ){
            std::cout << "Memory used at the end " << FMemStats::controler.getCurrentAllocated()
                      << " Bytes (" << FMemStats::controler.getCurrentAllocatedMB() << "MB)\n";
            std::cout << "Max memory used " << FMemStats::controler.getMaxAllocated()
                      << " Bytes (" << FMemStats::controler.getMaxAllocatedMB() << "MB)\n";
            std::cout << "Total memory used " << FMemStats::controler.getTotalAllocated()
                      << " Bytes (" << FMemStats::controler.getTotalAllocatedMB() << "MB)\n";
        }
    }
581 582


583 584 585
    ///////////////////////////////////////////////////////////
    // Set the tests!
    ///////////////////////////////////////////////////////////
586 587


588 589
    /** TestChebSymKernel */
    void TestChebSymKernel(){
590
        const unsigned int ORDER = 7;
591
        typedef double FReal;
592 593 594 595 596 597 598
        typedef FP2PParticleContainerIndexed<FReal> ContainerClass;
        typedef FSimpleLeaf<FReal, ContainerClass> LeafClass;
        typedef FInterpMatrixKernelR<FReal> MatrixKernelClass;
        typedef FChebCell<FReal,ORDER> CellClass;
        typedef FOctree<FReal, CellClass,ContainerClass,LeafClass> OctreeClass;
        typedef FChebSymKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER> KernelClass;
        typedef FFmmAlgorithmPeriodic<FReal,OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
599 600
        //typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClassNonPer;
        typedef FFmmAlgorithmThread<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClassNonPer;
601
        // run test
602 603 604 605 606 607 608 609
        RunTest<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass>(-1);
        RunTest<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass>(0);
        //RunTest<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass>(1);
        RunTestUpward<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass,FmmClassNonPer>(-1);
        RunTestUpward<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass,FmmClassNonPer>(0);
        RunTestUpward<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass,FmmClassNonPer>(1);
        RunTestFake<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass,FmmClassNonPer>(-1);
        RunTestFake<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass,FmmClassNonPer>(0);
610
    }
611 612 613



614 615 616
    ///////////////////////////////////////////////////////////
    // Set the tests!
    ///////////////////////////////////////////////////////////
617

618 619 620 621
    /** set test */
    void SetTests(){
        AddTest(&TestChebyshevDirect::TestChebSymKernel,"Test Chebyshev Kernel with 16 small SVDs and symmetries");
    }
622 623 624 625 626
};


// You must do this
TestClass(TestChebyshevDirect)