utestLagrange.cpp 8.83 KB
Newer Older
1
// See LICENCE file at project root
2 3

// ==== CMAKE =====
COULAUD Olivier's avatar
COULAUD Olivier committed
4
// @FUSE_FFT
5
// ================
6
// Keep in private GIT
7
//
8

COULAUD Olivier's avatar
COULAUD Olivier committed
9
#include "ScalFmmConfig.h"
10 11 12 13 14 15 16 17 18 19 20 21 22
#include "Utils/FGlobal.hpp"

#include "Containers/FOctree.hpp"

#include "Files/FFmaGenericLoader.hpp"

#include "Core/FFmmAlgorithm.hpp"

#include "FUTester.hpp"

#include "Components/FSimpleLeaf.hpp"


BRAMAS Berenger's avatar
BRAMAS Berenger committed
23 24 25
#include "Kernels/Uniform/FUnifCell.hpp"
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Uniform/FUnifKernel.hpp"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
/*
  In this test we compare the TestLagrange fmm results with the direct results.
 */


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

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

42
    template <class FReal, class CellClass, class ContainerClass, class KernelClass, class MatrixKernelClass,
43 44 45 46 47 48 49
	class LeafClass, class OctreeClass, class FmmClass>
	void RunTest()	{
		//
		// Load particles
		//
		if(sizeof(FReal) == sizeof(float) ) {
			std::cerr << "No input data available for Float "<< std::endl;
COULAUD Olivier's avatar
COULAUD Olivier committed
50
			exit(EXIT_FAILURE);
51
		}
COULAUD Olivier's avatar
COULAUD Olivier committed
52 53 54 55 56 57
		const std::string parFile( (sizeof(FReal) == sizeof(float))?
				"Test/DirectFloatbfma":
				"UTest/DirectDouble.bfma");
		//
		std::string filename(SCALFMMDataPath+parFile);
		//
58
		FFmaGenericLoader<FReal> loader(filename);
59 60 61 62 63 64
		Print("Number of particles:");
		Print(loader.getNumberOfParticles());

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

65 66
    // Create Matrix Kernel
    const MatrixKernelClass MatrixKernel; // FUKernelTester is only designed to work with 1/R, i.e. matrix kernel ctor takes no argument.
67

68
    // Load particles
69
		FSize nbParticles = loader.getNumberOfParticles() ;
70
		FmaRWParticle<FReal, 8,8>* const particles = new FmaRWParticle<FReal, 8,8>[nbParticles];
71 72

		loader.fillParticle(particles,nbParticles);
73 74 75 76

		// Create octree
		OctreeClass tree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());
    //
77 78
		//   Insert particle in the tree
		//
79
		for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
80
		    tree.insert(particles[idxPart].getPosition(), idxPart, particles[idxPart].getPhysicalValue() );
81 82 83 84 85 86
		}
		//
		/////////////////////////////////////////////////////////////////////////////////////////////////
		// Run FMM computation
		/////////////////////////////////////////////////////////////////////////////////////////////////
		Print("Fmm...");
87
		KernelClass kernels(NbLevels, loader.getBoxWidth(), loader.getCenterOfBox(),&MatrixKernel);
88 89 90 91 92 93 94 95
		FmmClass algo(&tree,&kernels);
		algo.execute();
		//0
		FReal energy= 0.0 , energyD = 0.0 ;
		/////////////////////////////////////////////////////////////////////////////////////////////////
		// Compute direct energy
		/////////////////////////////////////////////////////////////////////////////////////////////////

96
		for(FSize idx = 0 ; idx < loader.getNumberOfParticles()  ; ++idx){
97
		    energyD +=  particles[idx].getPotential()*particles[idx].getPhysicalValue() ;
98 99 100 101 102
		}
		/////////////////////////////////////////////////////////////////////////////////////////////////
		// Compare
		/////////////////////////////////////////////////////////////////////////////////////////////////
		Print("Compute Diff...");
103 104
		FMath::FAccurater<FReal> potentialDiff;
		FMath::FAccurater<FReal> fx, fy, fz;
105 106 107
		{ // Check that each particle has been summed with all other

			tree.forEachLeaf([&](LeafClass* leaf){
108
				const FReal*const potentials     = leaf->getTargets()->getPotentials();
109
				const FReal*const physicalValues = leaf->getTargets()->getPhysicalValues();
110 111 112 113 114
				const FReal*const forcesX        = leaf->getTargets()->getForcesX();
				const FReal*const forcesY        = leaf->getTargets()->getForcesY();
				const FReal*const forcesZ        = leaf->getTargets()->getForcesZ();
				const FSize nbParticlesInLeaf    = leaf->getTargets()->getNbParticles();
				const FVector<FSize>& indexes    = leaf->getTargets()->getIndexes();
115

116 117
				for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
					const FSize indexPartOrig = indexes[idxPart];
118 119 120 121
					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]);
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
					energy   += potentials[idxPart]*physicalValues[idxPart];
				}
			});
		}

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

		Print("Test1 - Error Relative L2 norm Potential ");
158
		uassert(potentialDiff.getRelativeL2Norm() < MaximumDiffPotential);  //1
159
		Print("Test2 - Error RMS L2 norm Potential ");
160
		uassert(potentialDiff.getRMSError() < MaximumDiffPotential);        //2
161
		Print("Test3 - Error Relative L2 norm FX ");
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
		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);               //7
		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
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

	}

	/** 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(){
201
        typedef double FReal;
202 203
		const unsigned int ORDER = 6;
	    // typedefs
204 205 206 207 208 209
	    typedef FP2PParticleContainerIndexed<FReal> ContainerClass;
	    typedef FSimpleLeaf<FReal, ContainerClass >  LeafClass;
	    typedef FInterpMatrixKernelR<FReal> MatrixKernelClass;
	    typedef FUnifCell<FReal,ORDER> CellClass;
	    typedef FOctree<FReal, CellClass,ContainerClass,LeafClass> OctreeClass;
	    typedef FUnifKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER> KernelClass;
210 211
	    typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
		// run test
212
        RunTest<FReal,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass>();
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	}

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

	/** set test */
	void SetTests(){
		AddTest(&TestLagrange::TestUnifKernel,"Test Lagrange Kernel ");
	}
};


// You must do this
TestClass(TestLagrange)