utestSphericalWithPrevious.cpp 7.7 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
#include "../Src/Containers/FOctree.hpp"
#include "../Src/Containers/FVector.hpp"

14 15 16
#include "../Src/Kernels/Spherical/FSphericalCell.hpp"
#include "../Src/Kernels/Spherical/FSphericalKernel.hpp"
#include "../Src/Kernels/Spherical/FSphericalParticle.hpp"
17
#include "../Src/Components/FSimpleLeaf.hpp"
18 19 20 21 22 23 24 25 26

#include "../Src/Files/FFmaBinLoader.hpp"
#include "../Src/Files/FTreeIO.hpp"

#include "../Src/Core/FFmmAlgorithm.hpp"

#include "FUTester.hpp"


berenger-bramas's avatar
berenger-bramas committed
27 28
/**
  * This test compare a previous FMM result with a previous simulation result.
29 30
  */

berenger-bramas's avatar
berenger-bramas committed
31 32 33

typedef FSphericalParticle       ParticleClass;
typedef FSphericalCell           CellClass;
34 35
typedef FVector<ParticleClass>  ContainerClass;

36
typedef FSphericalKernel<ParticleClass, CellClass, ContainerClass >          KernelClass;
37

38 39 40 41 42
typedef FSimpleLeaf<ParticleClass, ContainerClass >                     LeafClass;
typedef FOctree<ParticleClass, CellClass, ContainerClass , LeafClass >  OctreeClass;

typedef FFmmAlgorithm<OctreeClass, ParticleClass, CellClass, ContainerClass, KernelClass, LeafClass > FmmClass;

43 44
/** To check if a value is correct */
bool IsSimilar(const FReal good, const FReal other){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
45
    const FReal Epsilon = FReal(0.0001);
46 47 48
    return (FMath::Abs(good-other)/FMath::Abs(good)) < Epsilon;
}

berenger-bramas's avatar
berenger-bramas committed
49
/** The test class */
50
class TestSphericalWithPrevious : public FUTester<TestSphericalWithPrevious> {
berenger-bramas's avatar
berenger-bramas committed
51
    /** the test */
52
    void TestTree(){
53
        // Warning in make test the exec dir it Build/UTests
54 55
        const char* const DataFile = "../../Data/utestSpherical.data";
        const char* const ParticleFile = "../../Data/utestSpherical.bin.fma";
56

57 58
        const int NbLevels      = 5;
        const int SizeSubLevels = 3;
berenger-bramas's avatar
berenger-bramas committed
59
        const int DevP = 9;
60

61
        // Load the particles file
62
        FFmaBinLoader<ParticleClass> loader(ParticleFile);
63 64
        if(!loader.isOpen()){
            Print("Cannot open particles file.");
65
            uassert(false);
66 67 68
            return;
        }

69
        // Create octree
70
        FSphericalCell::Init(DevP);
71
        OctreeClass testTree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());
72
        loader.fillTree(testTree);
73

74
        // Run simulation
75
        KernelClass kernels(DevP, NbLevels, loader.getBoxWidth(), loader.getCenterOfBox());
76 77 78
        FmmClass algo(&testTree,&kernels);
        algo.execute();

79
        // If needed save the result
berenger-bramas's avatar
berenger-bramas committed
80
        // FTreeIO::Save<OctreeClass, CellClass, ParticleClass, ContainerClass >(DataFile, testTree);
81

82
        // Load previous result
83
        OctreeClass goodTree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());
berenger-bramas's avatar
berenger-bramas committed
84
        FTreeIO::Load<OctreeClass, CellClass, ParticleClass, ContainerClass >(DataFile, goodTree);
85

86
        // Compare the two simulations
87 88
        Print("Check the particles...");
        { // Check that each particle has been summed with all other
89 90
            OctreeClass::Iterator testOctreeIterator(&testTree);
            OctreeClass::Iterator goodOctreeIterator(&goodTree);
91 92 93 94 95 96

            testOctreeIterator.gotoBottomLeft();
            goodOctreeIterator.gotoBottomLeft();

            do{
                if(testOctreeIterator.getCurrentGlobalIndex() != goodOctreeIterator.getCurrentGlobalIndex()){
97
                    uassert(false);
98 99 100 101
                    break;
                }

                if(testOctreeIterator.getCurrentListSrc()->getSize() != goodOctreeIterator.getCurrentListSrc()->getSize()){
102
                    uassert(false);
103 104 105
                    break;
                }

106 107
                ContainerClass::BasicIterator goodIter(*goodOctreeIterator.getCurrentListTargets());
                ContainerClass::BasicIterator testIter(*testOctreeIterator.getCurrentListTargets());
108 109

                while( goodIter.hasNotFinished() ){
110 111 112 113
                    uassert( IsSimilar(goodIter.data().getPotential(), testIter.data().getPotential()) );
                    uassert( IsSimilar(goodIter.data().getPosition().getX(), testIter.data().getPosition().getX()) );
                    uassert( IsSimilar(goodIter.data().getPosition().getY(), testIter.data().getPosition().getY()) );
                    uassert( IsSimilar(goodIter.data().getPosition().getZ(), testIter.data().getPosition().getZ()) );
114 115 116 117 118 119 120 121

                    goodIter.gotoNext();
                    testIter.gotoNext();
                }


                if(!testOctreeIterator.moveRight()){
                    if(goodOctreeIterator.moveRight()){
122
                        uassert(false);
123 124 125 126
                    }
                    break;
                }
                if(!goodOctreeIterator.moveRight()){
127
                    uassert(false);
128 129 130 131 132 133 134
                    break;
                }

            } while(true);
        }
        Print("Check the leaves...");
        { // Ceck if there is number of NbPart summed at level 1
135 136
            OctreeClass::Iterator testOctreeIterator(&testTree);
            OctreeClass::Iterator goodOctreeIterator(&goodTree);
137 138 139 140 141 142 143

            testOctreeIterator.gotoBottomLeft();
            goodOctreeIterator.gotoBottomLeft();

            for(int idxLevel = NbLevels - 1 ; idxLevel > 1 ; --idxLevel ){
                do{
                    if(testOctreeIterator.getCurrentGlobalIndex() != goodOctreeIterator.getCurrentGlobalIndex()){
144
                        uassert(false);
145 146 147
                        break;
                    }

148
                    for(int idxLocal = 0 ; idxLocal < CellClass::GetLocalSize() ; ++idxLocal){
149
                        IsSimilar(testOctreeIterator.getCurrentCell()->getLocal()[idxLocal].getReal(),
150
                                         goodOctreeIterator.getCurrentCell()->getLocal()[idxLocal].getReal());
151
                        IsSimilar(testOctreeIterator.getCurrentCell()->getLocal()[idxLocal].getImag(),
152 153
                                         goodOctreeIterator.getCurrentCell()->getLocal()[idxLocal].getImag());
                    }
154

155
                    for(int idxPole = 0 ; idxPole < CellClass::GetPoleSize() ; ++idxPole){
156
                        IsSimilar(testOctreeIterator.getCurrentCell()->getMultipole()[idxPole].getReal(),
157
                                         goodOctreeIterator.getCurrentCell()->getMultipole()[idxPole].getReal());
158
                        IsSimilar(testOctreeIterator.getCurrentCell()->getMultipole()[idxPole].getImag(),
159 160
                                         goodOctreeIterator.getCurrentCell()->getMultipole()[idxPole].getImag());
                    }
161 162 163

                    if(!testOctreeIterator.moveRight()){
                        if(goodOctreeIterator.moveRight()){
164
                            uassert(false);
165 166 167 168
                        }
                        break;
                    }
                    if(!goodOctreeIterator.moveRight()){
169
                        uassert(false);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
                        break;
                    }

                } while(true);

                testOctreeIterator.moveUp();
                testOctreeIterator.gotoLeft();

                goodOctreeIterator.moveUp();
                goodOctreeIterator.gotoLeft();
            }
        }
        Print("Over...");
    }


    // set test
    void SetTests(){
188
        AddTest(&TestSphericalWithPrevious::TestTree,"Test Simu and compare tree");
189 190 191 192 193 194
    }
};



// You must do this
195
TestClass(TestSphericalWithPrevious)
196 197


198