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

// ==== CMAKE =====
// @FUSE_BLAS
COULAUD Olivier's avatar
COULAUD Olivier committed
19
// @FUSE_FFT
BRAMAS Berenger's avatar
BRAMAS Berenger committed
20
// @SCALFMM_PRIVATE
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// ==============
#include <array>

#include "ScalFmmConfig.h"
#include "Utils/FGlobal.hpp"

#include "Containers/FOctree.hpp"

#include "Files/FFmaGenericLoader.hpp"

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

#include "FUTester.hpp"

#include "Components/FSimpleLeaf.hpp"


#include "Kernels/Chebyshev/FChebCell.hpp"
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Chebyshev/FChebSymKernel.hpp"
42
#include "Kernels/Chebyshev/FChebKernel.hpp"
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

#include "Kernels/Uniform/FUnifCell.hpp"
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Uniform/FUnifKernel.hpp"


#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
/*
  In this test we compare the spherical FMM results and the direct results.
 */


/** the test class
 *
 */
class TestInterpolationKernel : public FUTester<TestInterpolationKernel> {

BRAMAS Berenger's avatar
BRAMAS Berenger committed
60 61 62 63
    ///////////////////////////////////////////////////////////
    // The tests!
    ///////////////////////////////////////////////////////////

64
    template <class FReal, class CellClass, class ContainerClass, class KernelClass, class MatrixKernelClass,
BRAMAS Berenger's avatar
BRAMAS Berenger committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
              class LeafClass, class OctreeClass, class FmmClass, const int NVals>
    void RunTest()	{
        // Warning in make test the exec dir it Build/UTests
        // Load particles
        //
        // 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);
        //
82
        FFmaGenericLoader<FReal> loader(filename);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
83 84 85 86 87 88 89 90 91 92 93
        if(!loader.isOpen()){
            Print("Cannot open particles file.");
            uassert(false);
            return;
        }
        Print("Number of particles:");
        Print(loader.getNumberOfParticles());

        const int NbLevels        = 4;
        const int SizeSubLevels = 2;

94
        // std::cout << "\nInterpolation FMM (ORDER="<< ORDER << ") ... " << std::endl;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
95 96 97 98 99

        // Create Matrix Kernel
        const MatrixKernelClass MatrixKernel; // FUKernelTester is only designed to work with 1/R, i.e. matrix kernel ctor takes no argument.
        //
        FSize nbParticles = loader.getNumberOfParticles() ;
100
        FmaRWParticle<FReal, 8,8>* const particles = new FmaRWParticle<FReal, 8,8>[nbParticles];
BRAMAS Berenger's avatar
BRAMAS Berenger committed
101 102

        loader.fillParticle(particles,nbParticles);
103

BRAMAS Berenger's avatar
BRAMAS Berenger committed
104 105
        // Create octree
        OctreeClass tree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());
106
        // Insert particle in the tree
107
        for(FSize idxPart = 0 ; idxPart < nbParticles ; ++idxPart){
108 109 110 111 112 113 114 115
            // Convert FReal[NVALS] to std::array<FReal,NVALS>
            std::array<FReal, (1+4*1)*NVals> physicalState;
            for(int idxVals = 0 ; idxVals < NVals ; ++idxVals){
                physicalState[0*NVals+idxVals]= particles[idxPart].getPhysicalValue();
                physicalState[1*NVals+idxVals]=0.0;
                physicalState[2*NVals+idxVals]=0.0;
                physicalState[3*NVals+idxVals]=0.0;
                physicalState[4*NVals+idxVals]=0.0;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
116
            }
117 118 119 120
            // put in tree
            tree.insert(particles[idxPart].getPosition(), idxPart, physicalState);
        }

BRAMAS Berenger's avatar
BRAMAS Berenger committed
121 122 123 124 125 126 127 128

        // Run FMM
        Print("Fmm...");
        KernelClass kernels(NbLevels, loader.getBoxWidth(), loader.getCenterOfBox(),&MatrixKernel);
        FmmClass algo(&tree,&kernels);
        algo.execute();
        //
        FReal energy= 0.0 , energyD = 0.0 ;
129
        for(FSize idx = 0 ; idx < loader.getNumberOfParticles()  ; ++idx){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
130 131 132 133 134
            energyD +=  particles[idx].getPotential()*particles[idx].getPhysicalValue() ;
        }
        //
        // Compare
        Print("Compute Diff...");
135 136
        FMath::FAccurater<FReal> potentialDiff[NVals];
        FMath::FAccurater<FReal> fx, fy, fz;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
137 138 139 140 141 142 143 144 145
        {
            tree.forEachLeaf([&](LeafClass* leaf){
                //
                for(int idxVals = 0 ; idxVals < NVals ; ++idxVals){
                    const FReal* const physicalValues = leaf->getTargets()->getPhysicalValues(idxVals);
                    const FReal*const potentials = leaf->getTargets()->getPotentials(idxVals);
                    const FReal*const forcesX = leaf->getTargets()->getForcesX(idxVals);
                    const FReal*const forcesY = leaf->getTargets()->getForcesY(idxVals);
                    const FReal*const forcesZ = leaf->getTargets()->getForcesZ(idxVals);
146 147
                    const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
                    const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
BRAMAS Berenger's avatar
BRAMAS Berenger committed
148

149
                    for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
150

151
                        const FSize indexPartOrig = indexes[idxPart];
BRAMAS Berenger's avatar
BRAMAS Berenger committed
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 190 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 224
                        //					std::cout << " index "<< indexPartOrig << "   "  << particles[indexPartOrig].getPotential() << "   " << potentials[idxPart] << std::endl;
                        potentialDiff[idxVals].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[] particles;
        energy /=NVals;
        // Print for information
        double errorPotRL2=0.0, errorPotRMS=0.0;
        Print("Potential diff is = ");
        for(int idxVals = 0 ; idxVals < NVals ; ++idxVals){
            printf("   Charge: %d\n",		idxVals);
            printf("         Pot L2Norm     %e\n",potentialDiff[idxVals].getL2Norm());
            printf("         Pot RL2Norm   %e\n",potentialDiff[idxVals].getRelativeL2Norm());
            printf("         Pot RMSError   %e\n",potentialDiff[idxVals].getRMSError());
            errorPotRL2 = std::max(errorPotRL2, potentialDiff[idxVals].getRelativeL2Norm());
            errorPotRMS = std::max(errorPotRMS, potentialDiff[idxVals].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);

        Print("Test1 - Error Relative L2 norm Potential ");
        uassert(errorPotRL2 < MaximumDiffPotential);    //1
        Print("Test2 - Error RMS L2 norm Potential ");
        uassert(errorPotRMS< MaximumDiffPotential);  //2
        Print("Test3 - Error Relative L2 norm FX ");
        uassert(fx.getRelativeL2Norm()  < MaximumDiffForces);                       //3
        Print("Test4 - Error RMS L2 norm FX ");
        uassert(fx.getRMSError() < MaximumDiffForces);                      //4
        Print("Test5 - Error Relative L2 norm FY ");
        uassert(fy.getRelativeL2Norm()  < MaximumDiffForces);                       //5
        Print("Test6 - Error RMS L2 norm FY ");
        uassert(fy.getRMSError() < MaximumDiffForces);                      //6
        Print("Test7 - Error Relative L2 norm FZ ");
        uassert(fz.getRelativeL2Norm()  < MaximumDiffForces);                      //8
        Print("Test8 - Error RMS L2 norm FZ ");
        uassert(fz.getRMSError() < 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) /energyD< MaximumDiffPotential);                     //10  Total Energy


        // Compute multipole local rhs diff
225 226
        FMath::FAccurater<FReal> localDiff;
        FMath::FAccurater<FReal> multiPoleDiff;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
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 257 258 259 260 261 262 263 264 265
        tree.forEachCell([&](CellClass* cell){
            for( int idxRhs = 1 ; idxRhs < NVals ; ++idxRhs){
                localDiff.add(cell->getLocal(0), cell->getLocal(idxRhs), cell->getVectorSize());
                multiPoleDiff.add(cell->getMultipole(0), cell->getMultipole(idxRhs), cell->getVectorSize());
            }
        });
        Print("Local diff is = ");
        Print(localDiff.getL2Norm());
        Print(localDiff.getInfNorm());
        Print("Multipole diff is = ");
        Print(multiPoleDiff.getL2Norm());
        Print(multiPoleDiff.getInfNorm());

        uassert(localDiff.getL2Norm()  < 1e-10);
        uassert(localDiff.getInfNorm() < 1e-10);
        uassert(multiPoleDiff.getL2Norm()  < 1e-10);
        uassert(multiPoleDiff.getInfNorm() < 1e-10);
    }

    /** If memstas is running print the memory used */
    void PostTest() {
        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";
        }
    }


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


    /** TestUnifKernel */
    void TestUnifKernel(){
266
        typedef double FReal;
COULAUD Olivier's avatar
COULAUD Olivier committed
267
        const int NVals = 3;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
268 269
        const unsigned int ORDER = 6 ;
        // run test
270
        typedef FInterpMatrixKernelR<FReal> MatrixKernelClass;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
271 272


273
        typedef FP2PParticleContainerIndexed<FReal,1,1,NVals> ContainerClass;
274
        typedef FSimpleLeaf<FReal, ContainerClass >  LeafClass;
275
        typedef FUnifCell<FReal,ORDER,1,1,NVals> CellClass;
276 277
        typedef FOctree<FReal, CellClass,ContainerClass,LeafClass> OctreeClass;
        typedef FUnifKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER,NVals> KernelClass;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
278 279
        typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;

280
        RunTest<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass, NVals>();
BRAMAS Berenger's avatar
BRAMAS Berenger committed
281 282 283 284
    }

    /** TestChebSymKernel */
    void TestChebSymKernel(){
285
        typedef double FReal;
COULAUD Olivier's avatar
COULAUD Olivier committed
286
        const int NVals = 3;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
287
        const unsigned int ORDER = 6;
288
        typedef FP2PParticleContainerIndexed<FReal,1,1,NVals> ContainerClass;
289 290
        typedef FSimpleLeaf<FReal, ContainerClass> LeafClass;
        typedef FInterpMatrixKernelR<FReal> MatrixKernelClass;
291
        typedef FChebCell<FReal,ORDER, 1, 1, NVals> CellClass;
292 293
        typedef FOctree<FReal, CellClass,ContainerClass,LeafClass> OctreeClass;
        typedef FChebSymKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER, NVals> KernelClass;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
294 295
        typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
        // run test
296
        RunTest<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass, NVals>();
BRAMAS Berenger's avatar
BRAMAS Berenger committed
297 298
    }

299 300
    /** TestChebKernel */
    void TestChebKernel(){
301
        typedef double FReal;
302 303
        const int NVals = 3;
        const unsigned int ORDER = 6;
304
        typedef FP2PParticleContainerIndexed<FReal,1,1,NVals> ContainerClass;
305 306
        typedef FSimpleLeaf<FReal, ContainerClass> LeafClass;
        typedef FInterpMatrixKernelR<FReal> MatrixKernelClass;
307
        typedef FChebCell<FReal,ORDER, 1, 1, NVals> CellClass;
308 309
        typedef FOctree<FReal, CellClass,ContainerClass,LeafClass> OctreeClass;
        typedef FChebKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER, NVals> KernelClass;
310 311
        typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
        // run test
312
        RunTest<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass, NVals>();
313
    }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
314 315 316 317 318 319 320 321 322 323

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

    /** set test */
    void SetTests(){

        AddTest(&TestInterpolationKernel::TestUnifKernel,"Test Lagrange/Uniform grid FMM");
        AddTest(&TestInterpolationKernel::TestChebSymKernel,"Test Symmetric Chebyshev Kernel with 16 small SVDs and symmetries");
324 325
        AddTest(&TestInterpolationKernel::TestChebKernel,"Test Chebyshev Kernel with 1 large SVD");
        
BRAMAS Berenger's avatar
BRAMAS Berenger committed
326
    }
327 328 329 330 331 332 333 334 335 336
};


// You must do this
TestClass(TestInterpolationKernel)