testOctreeRearrangeTsm.cpp 11.9 KB
Newer Older
1
// ===================================================================================
2 3 4 5
// Copyright ScalFmm 2016 INRIA, Olivier Coulaud, Bérenger Bramas,
// Matthias Messner olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the
// FMM.
6
//
7
// This software is governed by the CeCILL-C and LGPL licenses and
8
// abiding by the rules of distribution of free software.
9 10 11
// An extension to the license is given to allow static linking of scalfmm
// inside a proprietary application (no matter its license).
// See the main license file for more details.
12 13 14 15
//
// 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
16 17 18
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// ===================================================================================

#include <iostream>

#include <cstdio>
#include <cstdlib>

#include "../../Src/Utils/FParameters.hpp"
#include "../../Src/Utils/FTic.hpp"

#include "../../Src/Containers/FOctree.hpp"
#include "../../Src/Containers/FVector.hpp"

#include "../../Src/Components/FSimpleLeaf.hpp"
#include "../../Src/Components/FTypedLeaf.hpp"
#include "../../Src/Utils/FPoint.hpp"

#include "../../Src/Components/FBasicParticleContainer.hpp"
#include "../../Src/Components/FBasicCell.hpp"
#include "../../Src/Arranger/FBasicParticleContainerIndexedMover.hpp"
#include "../../Src/Arranger/FParticleTypedIndexedMover.hpp"
#include "../../Src/Kernels/P2P/FP2PParticleContainerIndexed.hpp"

#include "../../Src/Arranger/FOctreeArranger.hpp"

#include "../../Src/Utils/FParameterNames.hpp"


// Simply create particles and try the kernels
int main(int argc, char ** argv){

    FHelpDescribeAndExit(argc, argv,
                         "Put the particles into a tree, then change the position of some particles and update the tree.\n"
                         "This method should be used to avoid the tree reconstruction.",
                         FParameterDefinitions::NbParticles, FParameterDefinitions::OctreeHeight,
                         FParameterDefinitions::OctreeSubHeight);

56 57
    typedef double FReal;

58
    typedef FBasicCell                      CellClass;
59
    typedef FP2PParticleContainerIndexed<FReal>      ContainerClass;
60

61
    typedef FTypedLeaf< FReal, ContainerClass >                     LeafClass;
62
    typedef FOctree<FReal, CellClass, ContainerClass , LeafClass >  OctreeClass;
63

64 65
    typedef FParticleTypedIndexedMover<FReal, OctreeClass, ContainerClass> MoverClass;
    typedef FOctreeArranger<FReal, OctreeClass, ContainerClass, MoverClass> ArrangerClass;
66 67 68 69 70 71 72

    ///////////////////////What we do/////////////////////////////
    std::cout << ">> This executable has to be used to test the FMM algorithm.\n";
    //////////////////////////////////////////////////////////////

    const int NbLevels          = FParameters::getValue(argc,argv,FParameterDefinitions::OctreeHeight.options, 7);
    const int SizeSubLevels     = FParameters::getValue(argc,argv,FParameterDefinitions::OctreeSubHeight.options, 3);
73
    const FSize NbPart_Source     = FParameters::getValue(argc,argv,FParameterDefinitions::NbParticles.options, FSize(200000));
74 75 76 77 78 79 80 81 82 83 84 85
    const int NbPart_Target     = 10000;

    FTic counter;

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

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

    const FReal BoxWidth = 1.0;
    const FReal BoxCenter = 0.5;

86
    OctreeClass tree(NbLevels, SizeSubLevels, BoxWidth, FPoint<FReal>(BoxCenter,BoxCenter,BoxCenter));
87 88 89 90 91 92 93 94 95 96 97

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

    std::cout << "Creating & Inserting " << NbPart_Source << " particles sources and "<< NbPart_Target <<" particles target" << std::endl;
    std::cout << "\tHeight : " << NbLevels << " \t sub-height : " << SizeSubLevels << std::endl;
    counter.tic();


    {

98
        FPoint<FReal> particleToFill;
99
        for(FSize idxPart = 0 ; idxPart < NbPart_Source; ++idxPart){
100 101 102 103
            particleToFill.setPosition(
                                       (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2)),
                                       (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2)),
                                       (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2)));
104
            tree.insert(particleToFill,FParticleType::FParticleTypeSource,idxPart);
105 106
        }

107
        for(FSize idxPart = 0 ; idxPart < NbPart_Target; ++idxPart){
108 109 110 111
            particleToFill.setPosition(
                                       (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2)),
                                       (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2)),
                                       (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2)));
112
            tree.insert(particleToFill,FParticleType::FParticleTypeTarget,idxPart);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        }
    }

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



    //////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////
    ArrangerClass arrange(&tree);

    // In order to test multiple displacement of particles
    //    for(int ite=0 ; ite<10 ; ++ite){
    std::cout << "Working on particles ..." << std::endl;
    counter.tic();

    { // Create new position for each particles
        OctreeClass::Iterator octreeIterator(&tree);
        octreeIterator.gotoBottomLeft();
        do{
            ContainerClass* particles_sources = octreeIterator.getCurrentListTargets();
            ContainerClass* particles_targets = octreeIterator.getCurrentListSrc();
136
            for(FSize idxPart = 0; idxPart < particles_sources->getNbParticles() ; ++idxPart){
137 138 139 140
                particles_sources->getWPositions()[0][idxPart] = (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2));
                particles_sources->getWPositions()[1][idxPart] = (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2));
                particles_sources->getWPositions()[2][idxPart] = (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2));
            }
141
            for(FSize idxPart = 0; idxPart < particles_targets->getNbParticles() ; ++idxPart){
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
                particles_targets->getWPositions()[0][idxPart] = (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2));
                particles_targets->getWPositions()[1][idxPart] = (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2));
                particles_targets->getWPositions()[2][idxPart] = (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2));
            }
        } while(octreeIterator.moveRight());
    }

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


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

    std::cout << "Arrange ..." << std::endl;
    counter.tic();

159
    //FOctreeArranger<FReal,OctreeClass, ContainerClass, TestParticle, Converter<TestParticle> > arrange(&tree);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    arrange.rearrange();


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

    //////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////
    //    }
    std::cout << "Test ..." << std::endl;
    counter.tic();

    { // Check that each particle has been put into the right leaf
        long counterPart_source = 0;
        long counterPart_target = 0;
        OctreeClass::Iterator octreeIterator(&tree);
        octreeIterator.gotoBottomLeft();
        do{
            const MortonIndex leafIndex = octreeIterator.getCurrentGlobalIndex();

            ContainerClass* particles_targets = octreeIterator.getCurrentLeaf()->getTargets();
            ContainerClass* particles_sources = octreeIterator.getCurrentLeaf()->getSrc();;
            //Check for current sources
183
            for(FSize idxPart = 0; idxPart < particles_sources->getNbParticles() ; ++idxPart){
184
                const FPoint<FReal> particlePosition( particles_sources->getWPositions()[0][idxPart],
185 186 187 188 189 190 191 192 193 194
                                               particles_sources->getWPositions()[1][idxPart],
                                               particles_sources->getWPositions()[2][idxPart]);
                const MortonIndex particleIndex = tree.getMortonFromPosition( particlePosition );
                if( leafIndex != particleIndex){
                    std::cout << "Index problem, should be " << leafIndex <<
                        " particleIndex "<< particleIndex << std::endl;
                }

            }
            //Check for current targets
195
            for(FSize idxPart = 0; idxPart < particles_targets->getNbParticles() ; ++idxPart){
196
                const FPoint<FReal> particlePosition( particles_targets->getWPositions()[0][idxPart],
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 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
                                               particles_targets->getWPositions()[1][idxPart],
                                               particles_targets->getWPositions()[2][idxPart]);

                const MortonIndex particleIndex = tree.getMortonFromPosition( particlePosition );
                if( leafIndex != particleIndex){
                    std::cout << "Index problem, should be " << leafIndex <<
                        " particleIndex "<< particleIndex << std::endl;
                }

            }

            //Sum the parts
            counterPart_target += octreeIterator.getCurrentListTargets()->getNbParticles();
            counterPart_source += octreeIterator.getCurrentListSrc()->getNbParticles();
            //Check
            if(octreeIterator.getCurrentListTargets()->getNbParticles() == 0
               && octreeIterator.getCurrentListSrc()->getNbParticles() == 0){
                std::cout << "Problem, leaf is empty at index " << leafIndex << std::endl;
            }
        } while(octreeIterator.moveRight());

        if( counterPart_source != NbPart_Source || counterPart_target != NbPart_Target ){
            std::cout <<"Wrong particles number, src : should be " << NbPart_Source << " but is " << counterPart_source << "\n tgt : should be " << NbPart_Target << " but is " << counterPart_target << std::endl;
        }
    }

    { // Check that each particle has been summed with all other
        OctreeClass::Iterator octreeIterator(&tree);
        OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);

        const int heightMinusOne = NbLevels - 1;
        for(int idxLevel = 1 ; idxLevel < heightMinusOne ; ++idxLevel ){
            // for each cells
            do{
                int countChild = 0;
                CellClass** const child = octreeIterator.getCurrentChild();
                for(int idxChild = 0 ; idxChild < 8 ; ++idxChild ){
                    if( child[idxChild] ){
                        countChild += 1;
                    }
                }

                if(countChild == 0){
                    std::cout << "Problem at level " << idxLevel << " cell has no child " << octreeIterator.getCurrentGlobalIndex() << std::endl;
                }

            } while(octreeIterator.moveRight());

            avoidGotoLeftIterator.moveDown();
            octreeIterator = avoidGotoLeftIterator;
        }
    }

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

    return 0;
}