utestRotation.cpp 10.7 KB
Newer Older
1
// See LICENCE file at project root
2

BRAMAS Berenger's avatar
BRAMAS Berenger committed
3 4 5
#include "Utils/FGlobal.hpp"
#include "Utils/FTic.hpp"
#include "Utils/FTemplate.hpp"
6
#include "Utils/FMath.hpp"
7

BRAMAS Berenger's avatar
BRAMAS Berenger committed
8 9
#include "Containers/FOctree.hpp"
#include "Containers/FVector.hpp"
10

BRAMAS Berenger's avatar
BRAMAS Berenger committed
11 12
#include "Kernels/Rotation/FRotationCell.hpp"
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
13

BRAMAS Berenger's avatar
BRAMAS Berenger committed
14 15
#include "Components/FSimpleLeaf.hpp"
#include "Kernels/Rotation/FRotationKernel.hpp"
16

BRAMAS Berenger's avatar
BRAMAS Berenger committed
17
#include "Files/FFmaGenericLoader.hpp"
18

BRAMAS Berenger's avatar
BRAMAS Berenger committed
19
#include "Core/FFmmAlgorithm.hpp"
20 21 22 23 24

#include "FUTester.hpp"


/** the test class
25 26
 *
 */
27 28
class TestRotationDirect : public FUTester<TestRotationDirect> {
    /** The test method to factorize all the test based on different kernels */
29 30
    template <class FReal, class CellClass, class ContainerClass, class KernelClass, class LeafClass,
              class OctreeClass, class FmmClass, int ORDER>
31
    void RunTest(){
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        //
        // Load particles
        //
        if(sizeof(FReal) == sizeof(float) ) {
            std::cerr << "No input data available for Float "<< std::endl;
            exit(EXIT_FAILURE);
        }
        const std::string parFile( (sizeof(FReal) == sizeof(float))?
                                       "Test/DirectFloat.bfma":
                                       "UTest/DirectDouble.bfma");
        //
        std::string filename(SCALFMMDataPath+parFile);
        //
        FFmaGenericLoader<FReal> loader(filename);
        if(!loader.isOpen()){
            Print("Cannot open particles file.");
            uassert(false);
            return;
        }
        //
        FSize nbParticles = loader.getNumberOfParticles() ;
        Print("Number of particles:");
        Print(nbParticles);

        std::cout << "Approximation order: " << ORDER << std::endl;

        const int NbLevels      = 4;
        const int SizeSubLevels = 2;
        //
        FmaRWParticle<FReal, 8,8>* const particles = new FmaRWParticle<FReal, 8,8>[nbParticles];

        loader.fillParticle(particles,nbParticles);
        //
        // Create octree
        OctreeClass tree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());
        //   Insert particle in the tree
        //
        FReal  sum = 0.0, a= 0.0;

71
        for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
            FPoint<FReal> position(particles[idxPart].getPosition() );
            //
            tree.insert(particles[idxPart].getPosition() , idxPart, particles[idxPart].getPhysicalValue());
            //
            sum += FMath::Abs(particles[idxPart].getPhysicalValue()) ;
            a = std::max(a,position.getX()*position.getX()+position.getY()*position.getY()+position.getZ()*position.getZ());

        }
        double CorErr = sum/a;


        // Run FMM
        Print("Fmm...");
        //KernelClass kernels(NbLevels,loader.getBoxWidth());
        KernelClass* kernels = new KernelClass(NbLevels,loader.getBoxWidth(), loader.getCenterOfBox());
        FmmClass algo(&tree,kernels);
        FTic timer;
        algo.execute();
        timer.tac();
        std::cout << "Computation Time: " << timer.elapsed() << " for order "<<  ORDER <<std::endl;
        //
        FReal energy= 0.0 , energyD = 0.0 ;
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // Compute direct energy
        /////////////////////////////////////////////////////////////////////////////////////////////////

98
        for(FSize idx = 0 ; idx < loader.getNumberOfParticles()  ; ++idx){
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
            energyD +=  particles[idx].getPotential()*particles[idx].getPhysicalValue() ;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // 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();
115 116
                const FSize nbParticlesInLeaf           = leaf->getTargets()->getNbParticles();
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
117

118 119
                for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
                    const FSize indexPartOrig = indexes[idxPart];
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 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
                    potentialDiff.add(particles[indexPartOrig].getPotential(),potentials[idxPart]);
                    fx.add(particles[indexPartOrig].getForces()[0],forcesX[idxPart]);
                    fy.add(particles[indexPartOrig].getForces()[1],forcesY[idxPart]);
                    fz.add(particles[indexPartOrig].getForces()[2],forcesZ[idxPart]);
                    energy   += potentials[idxPart]*physicalValues[idxPart];
                }
            });
        }
        delete kernels;
        delete[] particles;

        // Print for information

        Print("Potential diff is = ");
        printf("         Pot L2Norm     %e\n",potentialDiff.getL2Norm());
        printf("         Pot RL2Norm   %e\n",potentialDiff.getRelativeL2Norm());
        printf("         Pot RMSError   %e\n",potentialDiff.getRMSError());
        Print("Fx diff is = ");
        printf("         Fx L2Norm     %e\n",fx.getL2Norm());
        printf("         Fx RL2Norm   %e\n",fx.getRelativeL2Norm());
        printf("         Fx RMSError   %e\n",fx.getRMSError());
        Print("Fy diff is = ");
        printf("        Fy L2Norm     %e\n",fy.getL2Norm());
        printf("        Fy RL2Norm   %e\n",fy.getRelativeL2Norm());
        printf("        Fy RMSError   %e\n",fy.getRMSError());
        Print("Fz diff is = ");
        printf("        Fz L2Norm     %e\n",fz.getL2Norm());
        printf("        Fz RL2Norm   %e\n",fz.getRelativeL2Norm());
        printf("        Fz RMSError   %e\n",fz.getRMSError());
        FReal L2error = (fx.getRelativeL2Norm()*fx.getRelativeL2Norm() + fy.getRelativeL2Norm()*fy.getRelativeL2Norm()  + fz.getRelativeL2Norm() *fz.getRelativeL2Norm()  );
        printf(" Total L2 Force Error= %e\n",FMath::Sqrt(L2error)) ;
        printf("  Energy Error  =   %.12e\n",FMath::Abs(energy-energyD));
        printf("  Energy FMM    =   %.12e\n",FMath::Abs(energy));
        printf("  Energy DIRECT =   %.12e\n",FMath::Abs(energyD));

        // Assert
        //	const FReal MaximumDiffPotential = FReal(9e-3);
        //	const FReal MaximumDiffForces     = FReal(9e-2);
        double epsilon = 1.0/FMath::pow2(ORDER);
        const FReal MaximumDiffPotential = FReal(CorErr*epsilon);
        const FReal MaximumDiffForces     = FReal(10*CorErr*epsilon);
        printf(" Criteria error - Epsilon  %e  \n",epsilon);

        Print("Test1 - Error Relative L2 norm Potential ");
        uassert(potentialDiff.getRelativeL2Norm() < MaximumDiffPotential);    //1
        Print("Test2 - Error RMS L2 norm Potential ");
        FReal CoerrRMS = potentialDiff.getl2Dot()/FMath::Sqrt(static_cast<FReal>(nbParticles));

        uassert(potentialDiff.getRMSError() < CoerrRMS*MaximumDiffPotential);  //2
        Print("Test3 - Error Relative L2 norm FX ");
        uassert(fx.getRelativeL2Norm()  < MaximumDiffForces);
        CoerrRMS = fx.getl2Dot()/FMath::Sqrt(static_cast<FReal>(nbParticles));
        //3
        Print("Test4 - Error RMS L2 norm FX ");
        uassert(fx.getRMSError() < CoerrRMS*MaximumDiffForces);                      //4
        Print("Test5 - Error Relative L2 norm FY ");
        uassert(fy.getRelativeL2Norm()  < MaximumDiffForces);                       //5
        Print("Test6 - Error RMS L2 norm FY ");
        CoerrRMS = fy.getl2Dot()/FMath::Sqrt(static_cast<FReal>(nbParticles));
        uassert(fy.getRMSError() < CoerrRMS*MaximumDiffForces);                      //6
        Print("Test7 - Error Relative L2 norm FZ ");
        uassert(fz.getRelativeL2Norm()  < MaximumDiffForces);                      //8
        Print("Test8 - Error RMS L2 norm FZ ");
        CoerrRMS = fz.getl2Dot()/FMath::Sqrt(static_cast<FReal>(nbParticles));
        uassert(fz.getRMSError() < CoerrRMS*MaximumDiffForces);                                           //8
        Print("Test9 - Error Relative L2 norm F ");
        uassert(L2error              < MaximumDiffForces);                                            //9   Total Force
        Print("Test10 - Relative error Energy ");
        uassert(FMath::Abs(energy-energyD) /FMath::Abs(energyD)< MaximumDiffPotential);                     //10  Total Energy
189 190 191 192 193 194


    }

    /** If memstas is running print the memory used */
    void PostTest() {
195 196 197 198 199
        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";
        }
200 201 202 203 204 205
    }

    ///////////////////////////////////////////////////////////
    // The tests!
    ///////////////////////////////////////////////////////////

206
    //    static const int P = 9;
207 208

    /** Rotation */
209
    template<int P>
210
    void TestRotation(){
211
        typedef double FReal;
212
        typedef FRotationCell<FReal,P>              CellClass;
213
        typedef FP2PParticleContainerIndexed<FReal>  ContainerClass;
214

215
        typedef FRotationKernel<FReal,CellClass, ContainerClass, P >          KernelClass;
216

217 218
        typedef FSimpleLeaf<FReal, ContainerClass >                     LeafClass;
        typedef FOctree<FReal, CellClass, ContainerClass , LeafClass >  OctreeClass;
219

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

222
        RunTest<FReal,CellClass, ContainerClass, KernelClass, LeafClass, OctreeClass, FmmClass,P>();
223 224 225 226 227 228 229 230
    }

    ///////////////////////////////////////////////////////////
    // Set the tests!
    ///////////////////////////////////////////////////////////

    /** set test */
    void SetTests(){
231 232 233 234 235 236 237
        FForAllThis::For<int, 6, 40, 2, TestRotationDirect>(this);
    }

public:
    template <int P>
    void For(){
        AddTest(&TestRotationDirect::TestRotation<P>,"Test Rotation Kernel");
238 239 240 241 242 243
    }
};


// You must do this
TestClass(TestRotationDirect)