testOctreeRearrange.cpp 8.66 KB
Newer Older
1
// ===================================================================================
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright ScalFmm 2011 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.
//
// 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".
15
// ===================================================================================
16
17
18

#include <iostream>

19
20
#include <cstdio>
#include <cstdlib>
21

22
23
#include "../../Src/Utils/FParameters.hpp"
#include "../../Src/Utils/FTic.hpp"
24

25
26
#include "../../Src/Containers/FOctree.hpp"
#include "../../Src/Containers/FVector.hpp"
27

28
#include "../../Src/Components/FSimpleLeaf.hpp"
29

BRAMAS Berenger's avatar
BRAMAS Berenger committed
30
#include "../../Src/Utils/FPoint.hpp"
31

32
33
#include "../../Src/Components/FBasicParticleContainer.hpp"
#include "../../Src/Components/FBasicCell.hpp"
34

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

37
38
39
40
41
42
struct TestParticle{
    FPoint position;
    const FPoint& getPosition(){
        return position;
    }
};
43

44
45
template <class ParticleClass>
class Converter {
46
public:
47
48
49
50
51
52
53
54
    template <class ContainerClass>
    static ParticleClass GetParticle(ContainerClass* container, const int idxExtract){
        TestParticle part;
        part.position.setPosition(
                    container->getPositions()[0][idxExtract],
                    container->getPositions()[1][idxExtract],
                    container->getPositions()[2][idxExtract]);
        return part;
55
56
57
    }

    template <class OctreeClass>
58
59
    static void Insert(OctreeClass* tree, const ParticleClass& part){
        tree->insert(part.position);
60
61
    }
};
62
63
64

// Simply create particles and try the kernels
int main(int argc, char ** argv){
65
66
    typedef FBasicCell                      CellClass;
    typedef FBasicParticleContainer<0>      ContainerClass;
67

68
69
    typedef FSimpleLeaf< ContainerClass >                     LeafClass;
    typedef FOctree< CellClass, ContainerClass , LeafClass >  OctreeClass;
70
71
72
73
74
75
76

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

    const int NbLevels          = FParameters::getValue(argc,argv,"-h", 7);
    const int SizeSubLevels     = FParameters::getValue(argc,argv,"-sh", 3);
berenger-bramas's avatar
berenger-bramas committed
77
    const int NbPart           = FParameters::getValue(argc,argv,"-nb", 2000000);
78
79
80

    FTic counter;

BRAMAS Berenger's avatar
BRAMAS Berenger committed
81
    srand48 ( 1 ); // volontary set seed to constant
82
83
84
85
86
87
88

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

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

BRAMAS Berenger's avatar
BRAMAS Berenger committed
89
    OctreeClass tree(NbLevels, SizeSubLevels, BoxWidth, FPoint(BoxCenter,BoxCenter,BoxCenter));
90
91
92
93
94
95
96
97
98
99
100

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

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


    {

101
        FPoint particleToFill;
102
103
        for(int idxPart = 0 ; idxPart < NbPart ; ++idxPart){
            particleToFill.setPosition(
BRAMAS Berenger's avatar
BRAMAS Berenger committed
104
105
106
                        (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2)),
                        (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2)),
                        (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2)));
107
108
109
110
111
112
113
114
115
116
117
118
119
            tree.insert(particleToFill);
        }
    }

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

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

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

120
    { // Create new position for each particles
121
        OctreeClass::Iterator octreeIterator(&tree);
122
123
        octreeIterator.gotoBottomLeft();
        do{
124
125
            ContainerClass* particles = octreeIterator.getCurrentListTargets();
            for(int idxPart = 0; idxPart < particles->getNbParticles() ; ++idxPart){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
126
127
128
                particles->getWPositions()[0][idxPart] = (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2));
                particles->getWPositions()[1][idxPart] = (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2));
                particles->getWPositions()[2][idxPart] = (BoxWidth*FReal(drand48())) + (BoxCenter-(BoxWidth/2));
129
130
131
132
133
134
135
136
137
138
139
140
141
142
            }
        } while(octreeIterator.moveRight());
    }

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


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

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

143
    FOctreeArranger<OctreeClass, ContainerClass, TestParticle, Converter<TestParticle> > arrange(&tree);
144
145
146
147
148
149
150
151
152
153
154
155
    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
berenger-bramas's avatar
berenger-bramas committed
156
157
        long counterPart = 0;

158
        OctreeClass::Iterator octreeIterator(&tree);
159
160
161
162
        octreeIterator.gotoBottomLeft();
        do{
            const MortonIndex leafIndex = octreeIterator.getCurrentGlobalIndex();

163
164
165
166
167
168
169
            ContainerClass* particles = octreeIterator.getCurrentListTargets();
            for(int idxPart = 0; idxPart < particles->getNbParticles() ; ++idxPart){
                const FPoint particlePosition( particles->getWPositions()[0][idxPart],
                                               particles->getWPositions()[1][idxPart],
                                               particles->getWPositions()[2][idxPart]);

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

            }
176
177
178

            counterPart += octreeIterator.getCurrentListTargets()->getNbParticles();
            if(octreeIterator.getCurrentListTargets()->getNbParticles() == 0){
179
180
181
                std::cout << "Problem, leaf is empty at index " << leafIndex << std::endl;
            }
        } while(octreeIterator.moveRight());
berenger-bramas's avatar
berenger-bramas committed
182
183
184
185

        if( counterPart != NbPart ){
            std::cout <<"Wrong particles number, should be " << NbPart << " but is " << counterPart << std::endl;
        }
186
187
188
    }

    { // Check that each particle has been summed with all other
189
190
        OctreeClass::Iterator octreeIterator(&tree);
        OctreeClass::Iterator avoidGotoLeftIterator(octreeIterator);
191
192
193
194
195
196
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

        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;
}


224