utestSphericalWithPrevious.cpp 7.42 KB
Newer Older
1
// ===================================================================================
2 3 4 5
// Copyright ScalFmm 2016 INRIA, Olivier Coulaud, Bérenger Bramas,
// Matthias Messner olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the
// FMM.
6
//
7
// This software is governed by the CeCILL-C and LGPL licenses and
8
// abiding by the rules of distribution of free software.
9 10 11
// An extension to the license is given to allow static linking of scalfmm
// inside a proprietary application (no matter its license).
// See the main license file for more details.
12
//
13 14 15
// 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
16 17 18
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info".
// "http://www.gnu.org/licenses".
19
// ===================================================================================
20 21


BRAMAS Berenger's avatar
BRAMAS Berenger committed
22
#include "Utils/FGlobal.hpp"
23

BRAMAS Berenger's avatar
BRAMAS Berenger committed
24 25
#include "Containers/FOctree.hpp"
#include "Containers/FVector.hpp"
26

BRAMAS Berenger's avatar
BRAMAS Berenger committed
27 28 29
#include "Kernels/Spherical/FSphericalCell.hpp"
#include "Kernels/Spherical/FSphericalKernel.hpp"
#include "Components/FSimpleLeaf.hpp"
30

BRAMAS Berenger's avatar
BRAMAS Berenger committed
31
#include "Files/FFmaGenericLoader.hpp"
32

BRAMAS Berenger's avatar
BRAMAS Berenger committed
33
#include "Files/FTreeIO.hpp"
34

BRAMAS Berenger's avatar
BRAMAS Berenger committed
35
#include "Core/FFmmAlgorithm.hpp"
36 37

#include "FUTester.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
38
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
39

berenger-bramas's avatar
berenger-bramas committed
40
/**
41 42
 * This test compare a previous FMM result with a previous simulation result.
 */
43

44
typedef double FReal;
45
typedef FSphericalCell<FReal>           CellClass;
46
typedef FP2PParticleContainerIndexed<FReal>  ContainerClass;
47

48
typedef FSphericalKernel< FReal, CellClass, ContainerClass >          KernelClass;
49

50 51
typedef FSimpleLeaf<FReal, ContainerClass >                     LeafClass;
typedef FOctree<FReal, CellClass, ContainerClass , LeafClass >  OctreeClass;
52

53
typedef FFmmAlgorithm<OctreeClass, CellClass, ContainerClass, KernelClass, LeafClass > FmmClass;
54

55 56
/** To check if a value is correct */
bool IsSimilar(const FReal good, const FReal other){
57 58
	const FReal Epsilon = FReal(0.0001);
	return (FMath::Abs(good-other)/FMath::Abs(good)) < Epsilon;
59 60
}

berenger-bramas's avatar
berenger-bramas committed
61
/** The test class */
62
class TestSphericalWithPrevious : public FUTester<TestSphericalWithPrevious> {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	/** the test */
	void TestTree(){
		if(sizeof(FReal) == sizeof(float) ) {
			std::cerr << "No input data available for Float "<< std::endl;
			uassert(false);
		}
		//
		//  Load a Tree
		const std::string DataFile = (sizeof(FReal) == sizeof(float))?
				SCALFMMDataPath+"UTest/SphericalPrevious.data.single":
				SCALFMMDataPath+"UTest/SphericalPrevious.data.double";
		//
		// Load particles
		//

		const std::string parFile( (sizeof(FReal) == sizeof(float))?
				"UTest/DirectFloat.bfma":
				"UTest/DirectDouble.bfma");
		//
		std::string filename(SCALFMMDataPath+parFile);
		//
84
		FFmaGenericLoader<FReal> loader(filename);
85 86 87 88 89 90 91 92 93 94 95 96 97
		if(!loader.isOpen()){
			Print("Cannot open particles file.");
			uassert(false);
			return;
		}
		FSize nbParticles = loader.getNumberOfParticles() ;
		//
		const int NbLevels      = 5;
		const int SizeSubLevels = 3;
		const int DevP = 9;
		//
		// Create octree
		//
98
		FSphericalCell<FReal>::Init(DevP);
99 100 101
		//
		OctreeClass testTree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());
		//
102
		for(FSize idxPart = 0 ; idxPart < nbParticles ; ++idxPart){
103
            FPoint<FReal> position;
104 105 106 107 108 109 110 111 112 113 114 115 116 117
			FReal physicalValue = 0.0;
			loader.fillParticle(&position,&physicalValue);
			// put in tree
			testTree.insert(position, idxPart, physicalValue);
		}
		//
		//  Run simulation 1
		KernelClass kernels(DevP, NbLevels, loader.getBoxWidth(), loader.getCenterOfBox());
		FmmClass algo(&testTree,&kernels);
		Print("Run simulation 1 ...");

		algo.execute();

		// If needed save the result
BRAMAS Berenger's avatar
BRAMAS Berenger committed
118
        //FTreeIO<FReal>::Save<OctreeClass, CellClass, LeafClass, ContainerClass >(DataFile.c_str(), testTree);
119 120 121

		// Load previous result
		OctreeClass goodTree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());
122
        FTreeIO<FReal>::Load<OctreeClass, CellClass, LeafClass, ContainerClass >(DataFile.c_str(), goodTree);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

		// Compare the two simulations
		Print("Check the particles...");
		{ // Check that each particle has been summed with all other
			OctreeClass::Iterator testOctreeIterator(&testTree);
			OctreeClass::Iterator goodOctreeIterator(&goodTree);

			testOctreeIterator.gotoBottomLeft();
			goodOctreeIterator.gotoBottomLeft();

			do{
				if(testOctreeIterator.getCurrentGlobalIndex() != goodOctreeIterator.getCurrentGlobalIndex()){
					uassert(false);
					break;
				}

				if(testOctreeIterator.getCurrentListSrc()->getNbParticles() != goodOctreeIterator.getCurrentListSrc()->getNbParticles()){
					uassert(false);
					break;
				}

				const ContainerClass* testLeaf = testOctreeIterator.getCurrentListSrc();
				const ContainerClass* goodLeaf = goodOctreeIterator.getCurrentListSrc();

147
				for(FSize idxPart = 0 ; idxPart < testLeaf->getNbParticles() ; ++idxPart ){
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 210 211 212 213 214 215 216 217 218 219 220 221 222 223
					uassert( IsSimilar(goodLeaf->getPotentials()[idxPart], testLeaf->getPotentials()[idxPart]) );
					uassert( IsSimilar(goodLeaf->getForcesX()[idxPart], testLeaf->getForcesX()[idxPart]) );
					uassert( IsSimilar(goodLeaf->getForcesY()[idxPart], testLeaf->getForcesY()[idxPart]) );
					uassert( IsSimilar(goodLeaf->getForcesZ()[idxPart], testLeaf->getForcesZ()[idxPart]) );
				}

				if(!testOctreeIterator.moveRight()){
					if(goodOctreeIterator.moveRight()){
						uassert(false);
					}
					break;
				}
				if(!goodOctreeIterator.moveRight()){
					uassert(false);
					break;
				}

			} while(true);
		}
		Print("Check the leaves...");
		{ // Ceck if there is number of NbPart summed at level 1
			OctreeClass::Iterator testOctreeIterator(&testTree);
			OctreeClass::Iterator goodOctreeIterator(&goodTree);

			testOctreeIterator.gotoBottomLeft();
			goodOctreeIterator.gotoBottomLeft();

			for(int idxLevel = NbLevels - 1 ; idxLevel > 1 ; --idxLevel ){
				do{
					if(testOctreeIterator.getCurrentGlobalIndex() != goodOctreeIterator.getCurrentGlobalIndex()){
						uassert(false);
						break;
					}

					for(int idxLocal = 0 ; idxLocal < CellClass::GetLocalSize() ; ++idxLocal){
						IsSimilar(testOctreeIterator.getCurrentCell()->getLocal()[idxLocal].getReal(),
								goodOctreeIterator.getCurrentCell()->getLocal()[idxLocal].getReal());
						IsSimilar(testOctreeIterator.getCurrentCell()->getLocal()[idxLocal].getImag(),
								goodOctreeIterator.getCurrentCell()->getLocal()[idxLocal].getImag());
					}

					for(int idxPole = 0 ; idxPole < CellClass::GetPoleSize() ; ++idxPole){
						IsSimilar(testOctreeIterator.getCurrentCell()->getMultipole()[idxPole].getReal(),
								goodOctreeIterator.getCurrentCell()->getMultipole()[idxPole].getReal());
						IsSimilar(testOctreeIterator.getCurrentCell()->getMultipole()[idxPole].getImag(),
								goodOctreeIterator.getCurrentCell()->getMultipole()[idxPole].getImag());
					}

					if(!testOctreeIterator.moveRight()){
						if(goodOctreeIterator.moveRight()){
							uassert(false);
						}
						break;
					}
					if(!goodOctreeIterator.moveRight()){
						uassert(false);
						break;
					}

				} while(true);

				testOctreeIterator.moveUp();
				testOctreeIterator.gotoLeft();

				goodOctreeIterator.moveUp();
				goodOctreeIterator.gotoLeft();
			}
		}
		Print("Over...");
	}


	// set test
	void SetTests(){
		AddTest(&TestSphericalWithPrevious::TestTree,"Test Simu and compare tree");
	}
224 225 226 227 228
};



// You must do this
229
TestClass(TestSphericalWithPrevious)
230 231


232