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

3
#include "Utils/FGlobal.hpp"
4

BRAMAS Berenger's avatar
BRAMAS Berenger committed
5 6
#include "Containers/FOctree.hpp"
#include "Containers/FVector.hpp"
7

BRAMAS Berenger's avatar
BRAMAS Berenger committed
8 9 10
#include "Kernels/Rotation/FRotationCell.hpp"
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
11

BRAMAS Berenger's avatar
BRAMAS Berenger committed
12 13 14
#include "Components/FTypedLeaf.hpp"
#include "Extensions/FExtendCellType.hpp"
#include "Kernels/Rotation/FRotationKernel.hpp"
15

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

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

#include "FUTester.hpp"


/** the test class the rotation and target source model.
26 27
 *
 */
28
class TestRotationDirectTsm : public FUTester<TestRotationDirectTsm> {
29
	/** The test method to factorize all the test based on different kernels */
30
    template <class FReal, class CellClass, class ContainerClass, class KernelClass, class LeafClass,
31 32 33 34 35 36 37
	class OctreeClass, class FmmClass>
	void RunTest(){
		// Warning in make test the exec dir it Build/UTests
		// Load particles
		const int nbSources = 5000;
		const int nbTargets = 5000;

38
		FRandomLoader<FReal> loader(nbSources + nbTargets);
39 40 41 42 43 44 45 46 47 48 49 50

		Print("Number of particles:");
		Print(loader.getNumberOfParticles());

		const int NbLevels      = 4;
		const int SizeSubLevels = 3;

		// Create octree
		OctreeClass tree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());

		const FReal physicalValue = 0.10;
		//
51
		FmaRWParticle<FReal, 8,8>* const particlesTargets = new FmaRWParticle<FReal, 8,8>[nbTargets];
52
		for(FSize idxPart = 0 ; idxPart < nbTargets ; ++idxPart){
53
            FPoint<FReal> position;
54 55
			loader.fillParticle(&position);
			// put in tree
56
            tree.insert(position, FParticleType::FParticleTypeTarget, idxPart, physicalValue);
57
			// get copy
58 59 60 61 62 63
			particlesTargets[idxPart].setPosition(position);
			*(particlesTargets[idxPart].setPhysicalValue()) = physicalValue;
			*(particlesTargets[idxPart].setPotential())        = 0.0;
			particlesTargets[idxPart].setForces()[0]        = 0.0;
			particlesTargets[idxPart].setForces()[1]        = 0.0;
			particlesTargets[idxPart].setForces()[2]        = 0.0;
64 65
		}

66
		FmaRWParticle<FReal, 8,8>* const particlesSources = new FmaRWParticle<FReal, 8,8>[nbSources];
67
		for(FSize idxPart = 0 ; idxPart < nbSources ; ++idxPart){
68
            FPoint<FReal> position;
69 70
			loader.fillParticle(&position);
			// put in tree
71
            tree.insert(position, FParticleType::FParticleTypeSource, idxPart, physicalValue);
72
			// get copy
73 74
			particlesSources[idxPart].setPosition(position);
			*(particlesSources[idxPart].setPhysicalValue()) = physicalValue;
75 76 77 78 79 80 81 82 83 84 85 86 87
		}


		// Run FMM
		Print("Fmm...");
		//KernelClass kernels(NbLevels,loader.getBoxWidth());
		KernelClass kernels(NbLevels,loader.getBoxWidth(), loader.getCenterOfBox());
		FmmClass algo(&tree,&kernels);
		algo.execute();
		//
		//

		// Run direct computation
88
        Print("Direct...");
89 90
		for(int idxTarget = 0 ; idxTarget < nbTargets ; ++idxTarget){
			for(int idxOther = 0 ; idxOther < nbSources ; ++idxOther){
91
                FP2PR::NonMutualParticles(
92
						particlesTargets[idxTarget].getPosition().getX(), particlesTargets[idxTarget].getPosition().getY(),
93 94 95 96 97
                        particlesTargets[idxTarget].getPosition().getZ(),particlesTargets[idxTarget].getPhysicalValue(),
                        &particlesTargets[idxTarget].setForces()[0],&particlesTargets[idxTarget].setForces()[1],
                        &particlesTargets[idxTarget].setForces()[2],particlesTargets[idxTarget].setPotential(),
                        particlesSources[idxOther].getPosition().getX(), particlesSources[idxOther].getPosition().getY(),
						particlesSources[idxOther].getPosition().getZ(),particlesSources[idxOther].getPhysicalValue());
98 99 100 101 102 103 104 105 106 107
			}
		}

		//
		// Assert
		/////////////////////////////////////////////////////////////////////////////////////////////////
		// Compute direct energy
		/////////////////////////////////////////////////////////////////////////////////////////////////
		FReal energy= 0.0 , energyD = 0.0 ;
		for(int idx = 0 ; idx <  nbTargets  ; ++idx){
108
			energyD +=  particlesTargets[idx].getPotential()*particlesTargets[idx].getPhysicalValue() ;
109 110 111 112 113
		}
		/////////////////////////////////////////////////////////////////////////////////////////////////
		// Compare
		/////////////////////////////////////////////////////////////////////////////////////////////////
		Print("Compute Diff...");
114 115
		FMath::FAccurater<FReal> potentialDiff;
		FMath::FAccurater<FReal> fx, fy, fz;
116 117 118 119 120 121 122 123 124
		{ // Check that each particle has been summed with all other

			tree.forEachLeaf([&](LeafClass* leaf){
				if( leaf->getTargets()->getNbParticles() ){
					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();
125 126
					const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
					const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
127

128 129
					for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
						const FSize indexPartOrig = indexes[idxPart];
130 131 132 133
						potentialDiff.add(particlesTargets[indexPartOrig].getPotential(),potentials[idxPart]);
						fx.add(particlesTargets[indexPartOrig].getForces()[0],forcesX[idxPart]);
						fy.add(particlesTargets[indexPartOrig].getForces()[1],forcesY[idxPart]);
						fz.add(particlesTargets[indexPartOrig].getForces()[2],forcesZ[idxPart]);
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
						energy   += potentials[idxPart]*physicalValues[idxPart];
					}
				}
			});
		}

		delete[] particlesTargets;
		delete[] particlesSources;
		//
		// 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 ");
		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
	}

	/** 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";
		}
	}

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

	static const int P = 9;

	/** Rotation */
	void TestRotation(){
210
        typedef double FReal;
211
        typedef FTypedRotationCell<FReal,P>    CellClass;
212
		typedef FP2PParticleContainerIndexed<FReal>  ContainerClass;
213

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

216
        typedef FTypedLeaf<FReal,ContainerClass >                     LeafClass;
217
		typedef FOctree<FReal, CellClass, ContainerClass , LeafClass >  OctreeClass;
218 219 220

		typedef FFmmAlgorithmTsm<OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass > FmmClass;

221
        RunTest<FReal, CellClass, ContainerClass, KernelClass, LeafClass, OctreeClass, FmmClass>();
222 223 224
	}

	void TestRotationThread(){
225
        typedef double FReal;
226
        typedef FTypedRotationCell<FReal,P>    CellClass;
227
		typedef FP2PParticleContainerIndexed<FReal>  ContainerClass;
228

229
        typedef FRotationKernel<FReal, CellClass, ContainerClass, P >          KernelClass;
230

231
        typedef FTypedLeaf<FReal,ContainerClass >                     LeafClass;
232
		typedef FOctree<FReal, CellClass, ContainerClass , LeafClass >  OctreeClass;
233 234 235

		typedef FFmmAlgorithmThreadTsm<OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass > FmmClass;

236
        RunTest<FReal, CellClass, ContainerClass, KernelClass, LeafClass, OctreeClass, FmmClass>();
237 238 239 240 241 242 243 244 245 246 247
	}

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

	/** set test */
	void SetTests(){
		AddTest(&TestRotationDirectTsm::TestRotation,"Test Rotation Kernel TSM");
		AddTest(&TestRotationDirectTsm::TestRotationThread,"Test Rotation Kernel TSM thread");
	}
248 249 250 251 252 253 254 255
};


// You must do this
TestClass(TestRotationDirectTsm)