testFmmAlgorithmPeriodic.cpp 9.56 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9
// Logiciel initial: ScalFmm Version 0.5
// Co-auteurs : Olivier Coulaud, Bérenger Bramas.
// Propriétaires : INRIA.
// Copyright © 2011-2012, diffusé sous les termes et conditions d’une licence propriétaire.
// Initial software: ScalFmm Version 0.5
// Co-authors: Olivier Coulaud, Bérenger Bramas.
// Owners: INRIA.
// Copyright © 2011-2012, spread under the terms and conditions of a proprietary license.
10
// ===================================================================================
11 12 13 14


#include <iostream>

15 16
#include <cstdio>
#include <cstdlib>
17

18 19
#include "../../Src/Utils/FParameters.hpp"
#include "../../Src/Utils/FTic.hpp"
20

21 22
#include "../../Src/Containers/FOctree.hpp"
#include "../../Src/Containers/FVector.hpp"
23

24
#include "../../Src/Components/FSimpleLeaf.hpp"
25

BRAMAS Berenger's avatar
BRAMAS Berenger committed
26
#include "../../Src/Utils/FPoint.hpp"
27

28 29 30
#include "../../Src/Components/FTestParticle.hpp"
#include "../../Src/Components/FTestCell.hpp"
#include "../../Src/Components/FTestKernels.hpp"
31

32
#include "../../Src/Core/FFmmAlgorithmPeriodic.hpp"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

/** This program show an example of use of
  * the fmm basic algo
  * it also check that each particles is impacted each other particles
  */


// Simply create particles and try the kernels
int main(int argc, char ** argv){
    typedef FTestParticle               ParticleClass;
    typedef FTestCell                   CellClass;
    typedef FVector<ParticleClass>      ContainerClass;

    typedef FSimpleLeaf<ParticleClass, ContainerClass >                     LeafClass;
    typedef FOctree<ParticleClass, CellClass, ContainerClass , LeafClass >  OctreeClass;
48
    typedef FTestKernels<ParticleClass, CellClass, ContainerClass >         KernelClass;
49 50 51 52 53 54

    typedef FFmmAlgorithmPeriodic<OctreeClass, ParticleClass, CellClass, ContainerClass, KernelClass, LeafClass >     FmmClass;
    ///////////////////////What we do/////////////////////////////
    std::cout << ">> This executable has to be used to test the FMM algorithm.\n";
    //////////////////////////////////////////////////////////////

55 56 57
    const int NbLevels          = FParameters::getValue(argc,argv,"-h", 7);
    const int SizeSubLevels     = FParameters::getValue(argc,argv,"-sh", 3);
    const long NbPartPerBoxes   = FParameters::getValue(argc,argv,"-nb", 3);
58 59 60 61 62 63 64 65

    FTic counter;

    srand ( 1 ); // volontary set seed to constant

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

66 67 68
    const FReal BoxWidth = 1.0;
    const FReal BoxCenter = 0.5;

BRAMAS Berenger's avatar
BRAMAS Berenger committed
69
    OctreeClass tree(NbLevels, SizeSubLevels, BoxWidth, FPoint(BoxCenter,BoxCenter,BoxCenter));
70 71 72 73

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

74
    std::cout << "Creating & Inserting " << NbPartPerBoxes << " particles per boxes ..." << std::endl;
75 76 77
    std::cout << "\tHeight : " << NbLevels << " \t sub-height : " << SizeSubLevels << std::endl;
    counter.tic();

78
    long NbPart = 0;
79
    {
80 81 82 83 84 85
        const long NbSmallBoxesPerSide = (1 << (NbLevels-1));
        const FReal SmallBoxWidth = BoxWidth / FReal(NbSmallBoxesPerSide);
        const FReal SmallBoxWidthDiv2 = SmallBoxWidth / 2;

        NbPart = NbSmallBoxesPerSide * NbSmallBoxesPerSide * NbSmallBoxesPerSide * NbPartPerBoxes;

86
        FTestParticle particleToFill;
87 88 89 90 91 92 93 94 95 96 97 98

        for(int idxX = 0 ; idxX < NbSmallBoxesPerSide ; ++idxX){
            for(int idxY = 0 ; idxY < NbSmallBoxesPerSide ; ++idxY){
                for(int idxZ = 0 ; idxZ < NbSmallBoxesPerSide ; ++idxZ){
                    particleToFill.setPosition(FReal(idxX)*SmallBoxWidth + SmallBoxWidthDiv2,
                                               FReal(idxY)*SmallBoxWidth + SmallBoxWidthDiv2,
                                               FReal(idxZ)*SmallBoxWidth + SmallBoxWidthDiv2);
                    for(int idxPart = 0 ; idxPart < NbPartPerBoxes ; ++idxPart){
                        tree.insert(particleToFill);
                    }
                }
            }
99 100 101 102 103 104 105 106 107 108 109 110
        }
    }

    counter.tac();
    std::cout << "Done  " << "(@Creating and Inserting Particles = " << counter.elapsed() << "s)." << std::endl;

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

    std::cout << "Working on particles ..." << std::endl;
    counter.tic();

111
    const int PeriodicDeep = 4;
112 113 114
    // FTestKernels FBasicKernels
    KernelClass kernels;
    //FFmmAlgorithm FFmmAlgorithmThread
115
    FmmClass algo( &tree, &kernels, PeriodicDeep);
116 117 118 119 120 121 122 123 124 125
    algo.execute();

    counter.tac();
    std::cout << "Done  " << "(@Algorithm = " << counter.elapsed() << "s)." << std::endl;

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

    //ValidateFMMAlgo<OctreeClass, ParticleClass, CellClass, ContainerClass, LeafClass>(&tree);

126
    { // Check that each particle has been summed with all other
127
        long long int counterNbPart = 0;
128

129
        OctreeClass::Iterator octreeIterator(&tree);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        octreeIterator.gotoBottomLeft();
        do{
            // on each leaf we should have the same number of particles
            if(octreeIterator.getCurrentCell()->getDataUp() != octreeIterator.getCurrentListSrc()->getSize()
                    || NbPartPerBoxes != octreeIterator.getCurrentCell()->getDataUp() ){
                    std::cout << "Problem P2M NbPartPerBoxes = " << NbPartPerBoxes <<
                                 " Data up = " << octreeIterator.getCurrentCell()->getDataUp() <<
                                 " Size = " << octreeIterator.getCurrentListSrc()->getSize() << "\n";
            }
            // we also count the number of particles.
            counterNbPart += octreeIterator.getCurrentListSrc()->getSize();
        } while(octreeIterator.moveRight());

        if( counterNbPart != NbPart){
            std::cout << "Problem global nb part, counter = " << counterNbPart << " created = " << NbPart << std::endl;
        }
    }
    { // Ceck if there is number of NbPart summed at level 1
        long long particlesPerBox = NbPartPerBoxes;

150
        OctreeClass::Iterator octreeIterator(&tree);
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        octreeIterator.gotoBottomLeft();
        for(int idxLevel = NbLevels - 1 ; idxLevel >= 1 ; --idxLevel ){
            do{
                if(octreeIterator.getCurrentCell()->getDataUp() != particlesPerBox){
                    std::cout << "Problem M2M particlesPerBox = " << particlesPerBox <<
                                 " Data up = " << octreeIterator.getCurrentCell()->getDataUp() <<
                                 " level = " << idxLevel << "\n";
                }
            } while(octreeIterator.moveRight());

            octreeIterator.moveUp();
            octreeIterator.gotoLeft();

            particlesPerBox *= 8;
        }
    }
    if( PeriodicDeep ){
        long long int counterL2L = 0;
        {
            long long int particlesPerBox = NbPartPerBoxes * FMath::pow(8,NbLevels-1);

            long long int counterUp[PeriodicDeep];
            for( int idxLevel = 0 ; idxLevel < PeriodicDeep ; ++idxLevel ){
                counterUp[idxLevel] = particlesPerBox;
                particlesPerBox *= 8;
            }

            long long int counterM2L[PeriodicDeep];
            for( int idxLevel = 0 ; idxLevel < PeriodicDeep ; ++idxLevel ){
                counterM2L[idxLevel] = counterUp[idxLevel] * 189;
            }

            for( int idxLevel = PeriodicDeep - 1 ; idxLevel >= 0 ; --idxLevel ){
                counterL2L += counterM2L[idxLevel];
            }
        }
        {
            long long int particlesPerBox = NbPartPerBoxes * FMath::pow(8,NbLevels-2);

190
            OctreeClass::Iterator octreeIterator(&tree);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            for(int idxLevel = 1 ; idxLevel < NbLevels ; ++idxLevel ){
                counterL2L = particlesPerBox * 189 + counterL2L;
                do{
                    if(octreeIterator.getCurrentCell()->getDataDown() != counterL2L){
                        std::cout << "Problem L2L counterL2L = " << counterL2L <<
                                     " Data Down = " << octreeIterator.getCurrentCell()->getDataDown() <<
                                     " level = " << idxLevel << "\n";
                    }
                } while(octreeIterator.moveRight());

                octreeIterator.gotoLeft();
                octreeIterator.moveDown();

                particlesPerBox /= 8;
            }
        }
    }
    { // Check that each particle has been summed with all other
209
        OctreeClass::Iterator octreeIterator(&tree);
210 211
        octreeIterator.gotoBottomLeft();
        do{
212
            ContainerClass::BasicIterator iter(*octreeIterator.getCurrentListTargets());
213 214 215 216 217 218 219 220 221 222 223 224 225 226

            const long long int sumParticles = octreeIterator.getCurrentCell()->getDataDown() + (26 * NbPartPerBoxes) + (NbPartPerBoxes - 1);

            while( iter.hasNotFinished() ){
                if( sumParticles != iter.data().getDataDown()){
                    std::cout << "P2P probleme, should be " << sumParticles <<
                                 " iter.data().getDataDown() "<< iter.data().getDataDown() << std::endl;
                }

                iter.gotoNext();
            }
        } while(octreeIterator.moveRight());
    }

227 228 229 230 231 232 233
    //////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////

    return 0;
}


234