utestChebyshevMultiRhs.cpp 12.2 KB
Newer Older
1
// ===================================================================================
COULAUD Olivier's avatar
COULAUD Olivier committed
2
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas, Matthias Messner
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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
19
// ==============
20

COULAUD Olivier's avatar
COULAUD Olivier committed
21
#include "ScalFmmConfig.h"
22 23 24 25
#include "../Src/Utils/FGlobal.hpp"

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

26
#include "Files/FFmaGenericLoader.hpp"
27 28 29 30 31 32 33 34 35 36

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

#include "FUTester.hpp"

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


#include "../Src/Kernels/Chebyshev/FChebCell.hpp"
37
#include "../Src/Kernels/Interpolation/FInterpMatrixKernel.hpp"
38 39 40 41 42
#include "../Src/Kernels/Chebyshev/FChebKernel.hpp"
#include "../Src/Kernels/Chebyshev/FChebSymKernel.hpp"

#include "../Src/Kernels/P2P/FP2PParticleContainerIndexed.hpp"
/*
43
  In this test we compare the spherical FMM results and the direct results.
44 45 46 47 48 49 50 51 52 53 54 55 56
*/


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

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

    template <class CellClass, class ContainerClass, class KernelClass, class MatrixKernelClass,
COULAUD Olivier's avatar
COULAUD Olivier committed
57 58
                        class LeafClass, class OctreeClass, class FmmClass, const int NVals>
    void RunTest()	{
59 60
        // Warning in make test the exec dir it Build/UTests
        // Load particles
61 62 63 64 65 66 67 68 69 70 71 72 73 74
		//
		// 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 loader(filename);
75 76 77 78 79 80 81 82
        if(!loader.isOpen()){
            Print("Cannot open particles file.");
            uassert(false);
            return;
        }
        Print("Number of particles:");
        Print(loader.getNumberOfParticles());

83
        const int NbLevels        = 4;
84 85
        const int SizeSubLevels = 2;

86 87 88 89 90 91 92 93 94 95 96
        //
		FSize nbParticles = loader.getNumberOfParticles() ;
		FmaR8W8Particle* const particles = new FmaR8W8Particle[nbParticles];

		loader.fillParticle(particles,nbParticles);
         //
		// Create octree
		OctreeClass tree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());
		//   Insert particle in the tree
		//
		for(int idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
COULAUD Olivier's avatar
COULAUD Olivier committed
97
			tree.insert(particles[idxPart].position , idxPart, particles[idxPart].physicalValue ,0.0,0.0,0.0);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		}
//		//
//        // Create octree
//        for(int idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
//            FPoint position;
//            FReal physicalValue;
//            loader.fillParticle(&position,&physicalValue);
//            // put in tree
//            tree.insert(position, idxPart, physicalValue);
//            // get copy
//            particles[idxPart].position = position;
//            particles[idxPart].physicalValue = physicalValue;
//            particles[idxPart].potential = 0.0;
//            particles[idxPart].forces[0] = 0.0;
//            particles[idxPart].forces[1] = 0.0;
//            particles[idxPart].forces[2] = 0.0;
//        }
115 116 117 118


        // Run FMM
        Print("Fmm...");
COULAUD Olivier's avatar
COULAUD Olivier committed
119
        KernelClass kernels(NbLevels, loader.getBoxWidth(), loader.getCenterOfBox());
120 121
        FmmClass algo(&tree,&kernels);
        algo.execute();
122 123
//
		FReal energy= 0.0 , energyD = 0.0 ;
124 125 126

        // Run direct computation
        Print("Direct...");
127
		for(int idx = 0 ; idx <  loader.getNumberOfParticles()  ; ++idx){
COULAUD Olivier's avatar
COULAUD Olivier committed
128 129 130 131
			particles[idx].potential *= NVals ;
			particles[idx].forces[0] *= NVals ;
			particles[idx].forces[1] *= NVals ;
			particles[idx].forces[2] *= NVals ;
132 133
			energyD +=  particles[idx].potential*particles[idx].physicalValue ;
		}
COULAUD Olivier's avatar
COULAUD Olivier committed
134
		//
135 136 137 138
        // Compare
        Print("Compute Diff...");
        FMath::FAccurater potentialDiff;
        FMath::FAccurater fx, fy, fz;
COULAUD Olivier's avatar
COULAUD Olivier committed
139
		{ // Check that each particle has been summed with all other
140

COULAUD Olivier's avatar
COULAUD Olivier committed
141 142
			tree.forEachLeaf([&](LeafClass* leaf){
				const FReal*const potentials        = leaf->getTargets()->getPotentials();
143
				const FReal*const physicalValues = leaf->getTargets()->getPhysicalValues();
COULAUD Olivier's avatar
COULAUD Olivier committed
144 145 146 147 148 149 150 151 152 153 154 155
				const FReal*const forcesX            = leaf->getTargets()->getForcesX();
				const FReal*const forcesY            = leaf->getTargets()->getForcesY();
				const FReal*const forcesZ            = leaf->getTargets()->getForcesZ();
				const int nbParticlesInLeaf           = leaf->getTargets()->getNbParticles();
				const FVector<int>& indexes      = leaf->getTargets()->getIndexes();

				for(int idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
					const int indexPartOrig = indexes[idxPart];
					potentialDiff.add(particles[indexPartOrig].potential,potentials[idxPart]);
					fx.add(particles[indexPartOrig].forces[0],forcesX[idxPart]);
					fy.add(particles[indexPartOrig].forces[1],forcesY[idxPart]);
					fz.add(particles[indexPartOrig].forces[2],forcesZ[idxPart]);
156
					energy   += potentials[idxPart]*physicalValues[idxPart];
COULAUD Olivier's avatar
COULAUD Olivier committed
157 158 159
				}
			});
		}
160

COULAUD Olivier's avatar
COULAUD Olivier committed
161
		delete[] particles;
162 163

        // Print for information
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		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));
185 186

        // Assert
COULAUD Olivier's avatar
COULAUD Olivier committed
187 188
        const FReal MaximumDiffPotential = FReal(9e-3);
        const FReal MaximumDiffForces     = FReal(9e-2);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

		Print("Test1 - Error Relative L2 norm Potential ");
		uassert(potentialDiff.getRelativeL2Norm() < MaximumDiffPotential);    //1
		Print("Test2 - Error RMS L2 norm Potential ");
		uassert(potentialDiff.getRMSError() < 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

211 212 213 214 215

        // Compute multipole local rhs diff
        FMath::FAccurater localDiff;
        FMath::FAccurater multiPoleDiff;
        tree.forEachCell([&](CellClass* cell){
COULAUD Olivier's avatar
COULAUD Olivier committed
216
            for( int idxRhs = 1 ; idxRhs < NVals ; ++idxRhs){
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
                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!
    ///////////////////////////////////////////////////////////


    /** TestChebKernel */
    void TestChebKernel(){
COULAUD Olivier's avatar
COULAUD Olivier committed
254 255
        const int NVals = 4;
        const unsigned int ORDER = 6 ;
256
        typedef FP2PParticleContainerIndexed<> ContainerClass;
257
        typedef FSimpleLeaf<ContainerClass> LeafClass;
258
        typedef FInterpMatrixKernelR MatrixKernelClass;
COULAUD Olivier's avatar
COULAUD Olivier committed
259
        typedef FChebCell<ORDER, 1, 1, NVals> CellClass;
260
        typedef FOctree<CellClass,ContainerClass,LeafClass> OctreeClass;
COULAUD Olivier's avatar
COULAUD Olivier committed
261
        typedef FChebKernel<CellClass,ContainerClass,MatrixKernelClass,ORDER, NVals> KernelClass;
262 263
        typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
        // run test
COULAUD Olivier's avatar
COULAUD Olivier committed
264
        RunTest<CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass, NVals>();
265 266 267 268
    }

    /** TestChebSymKernel */
    void TestChebSymKernel(){
COULAUD Olivier's avatar
COULAUD Olivier committed
269 270
        const int NVals = 4;
        const unsigned int ORDER = 6;
271
        typedef FP2PParticleContainerIndexed<> ContainerClass;
272
        typedef FSimpleLeaf<ContainerClass> LeafClass;
273
        typedef FInterpMatrixKernelR MatrixKernelClass;
COULAUD Olivier's avatar
COULAUD Olivier committed
274
        typedef FChebCell<ORDER, 1, 1, NVals> CellClass;
275
        typedef FOctree<CellClass,ContainerClass,LeafClass> OctreeClass;
COULAUD Olivier's avatar
COULAUD Olivier committed
276
        typedef FChebSymKernel<CellClass,ContainerClass,MatrixKernelClass,ORDER, NVals> KernelClass;
277 278
        typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
        // run test
COULAUD Olivier's avatar
COULAUD Olivier committed
279
        RunTest<CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass, NVals>();
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    }



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

    /** set test */
    void SetTests(){
        AddTest(&TestChebyshevDirect::TestChebKernel,"Test Chebyshev Kernel with one big SVD");
        AddTest(&TestChebyshevDirect::TestChebSymKernel,"Test Chebyshev Kernel with 16 small SVDs and symmetries");
    }
};


// You must do this
TestClass(TestChebyshevDirect)