utestChebyshevDirectPeriodic.cpp 11.3 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright ScalFmm 2011 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.
//
// 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".
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
// ===================================================================================

// ==== CMAKE =====
// @FUSE_BLAS
// ================

#include "../Src/Utils/FGlobal.hpp"

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

#include "../Src/Files/FRandomLoader.hpp"
#include "../Src/Files/FTreeIO.hpp"

#include "../Src/Core/FFmmAlgorithmPeriodic.hpp"

#include "FUTester.hpp"

#include "../Src/Kernels/Chebyshev/FChebParticle.hpp"
#include "../Src/Kernels/Chebyshev/FChebLeaf.hpp"
#include "../Src/Kernels/Chebyshev/FChebCell.hpp"
#include "../Src/Kernels/Chebyshev/FChebMatrixKernel.hpp"
#include "../Src/Kernels/Chebyshev/FChebKernel.hpp"
#include "../Src/Kernels/Chebyshev/FChebSymKernel.hpp"

/*
  In this test we compare the spherical fmm results and the direct results.
*/

/** We need to know the position of the particle in the array */
class IndexedParticle : public FChebParticle {
	int index;
public:
	IndexedParticle(): index(-1){}

	int getIndex() const{
		return index;
	}
	void setIndex( const int inIndex ){
		index = inIndex;
	}
};

/** the test class
 *
 */
class TestChebyshevDirect : public FUTester<TestChebyshevDirect> {
	
	///////////////////////////////////////////////////////////
	// The tests!
	///////////////////////////////////////////////////////////
	
	template <class ParticleClass, class CellClass, class ContainerClass, class KernelClass, class MatrixKernelClass,
						class LeafClass, class OctreeClass, class FmmClass>
	void RunTest(const FReal epsilon)	{
		// Warning in make test the exec dir it Build/UTests
		// Load particles

        const int NbLevels      = 3;
        const int SizeSubLevels = 2;
75 76
        const int PeriodicDeep  = 2;
        const int NbParticles   = 1;
77 78 79 80 81 82 83 84 85 86 87 88

        FRandomLoader<ParticleClass> loader(NbParticles);

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

        // Create octree
        OctreeClass tree(NbLevels, SizeSubLevels, loader.getBoxWidth(), loader.getCenterOfBox());
        ParticleClass* const particles = new ParticleClass[loader.getNumberOfParticles()];
        for(int idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
            loader.fillParticle(particles[idxPart]);
            particles[idxPart].setIndex( idxPart );
BRAMAS Berenger's avatar
BRAMAS Berenger committed
89
            particles[idxPart].setPhysicalValue(FReal(0.10));
90 91 92 93 94
            tree.insert(particles[idxPart]);
        }

        // Run FMM
        Print("Fmm...");
95
        FmmClass algo(&tree,PeriodicDeep );
96 97
        KernelClass kernels(algo.extendedTreeHeight(), algo.extendedBoxCenter(),
                            algo.extendedBoxWidth(), epsilon);
98 99 100 101 102 103
        algo.setKernel(&kernels);
        algo.execute();

        // Run direct computation
        const MatrixKernelClass MatrixKernel;
        Print("Direct...");
104 105

        FTreeCoordinate min, max;
106
        algo.repetitionsIntervals(&min, &max);
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        for(int idxTarget = 0 ; idxTarget < loader.getNumberOfParticles() ; ++idxTarget){
            for(int idxOther = idxTarget + 1 ; idxOther < loader.getNumberOfParticles() ; ++idxOther){
                //kernels.directInteractionMutual(&particles[idxTarget], &particles[idxOther]);
                const FReal wt = particles[idxTarget].getPhysicalValue();
                const FReal ws = particles[idxOther ].getPhysicalValue();
                const FReal one_over_r = MatrixKernel.evaluate(particles[idxTarget].getPosition(),
                            particles[idxOther].getPosition());
                // potential
                particles[idxTarget].incPotential(one_over_r * ws);
                particles[idxOther ].incPotential(one_over_r * wt);
                // force
                FPoint force(particles[idxOther].getPosition() - particles[idxTarget].getPosition());
                force *= ((ws*wt) * (one_over_r*one_over_r*one_over_r));
                particles[idxTarget].incForces(  force.getX(),  force.getY(),  force.getZ());
                particles[idxOther ].incForces( -force.getX(), -force.getY(), -force.getZ());
            }
124 125 126
            for(int idxX = min.getX() ; idxX <= max.getX() ; ++idxX){
                for(int idxY = min.getY() ; idxY <= max.getY() ; ++idxY){
                    for(int idxZ = min.getZ() ; idxZ <= max.getZ() ; ++idxZ){
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 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 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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
                        if(idxX ==0 && idxY == 0 && idxZ == 0) continue;
                        // next lines for test

                        const FPoint offset(loader.getBoxWidth() * FReal(idxX),
                                            loader.getBoxWidth() * FReal(idxY),
                                            loader.getBoxWidth() * FReal(idxZ));

                        for(int idxSource = 0 ; idxSource < NbParticles ; ++idxSource){
                            ParticleClass source = particles[idxSource];
                            source.incPosition(offset.getX(),offset.getY(),offset.getZ());

                            const FReal wt = particles[idxTarget].getPhysicalValue();
                            const FReal ws = source.getPhysicalValue();
                            const FReal one_over_r = MatrixKernel.evaluate(particles[idxTarget].getPosition(),
                                        source.getPosition());
                            // potential
                            particles[idxTarget].incPotential(one_over_r * ws);
                            source.incPotential(one_over_r * wt);
                            // force
                            FPoint force(source.getPosition() - particles[idxTarget].getPosition());
                            force *= ((ws*wt) * (one_over_r*one_over_r*one_over_r));
                            particles[idxTarget].incForces(  force.getX(),  force.getY(),  force.getZ());
                        }
                    }
                }
            }
        }

        // Compare
        Print("Compute Diff...");
        FMath::FAccurater potentialDiff;
        FMath::FAccurater fx, fy, fz;
        { // Check that each particle has been summed with all other
            typename OctreeClass::Iterator octreeIterator(&tree);
            octreeIterator.gotoBottomLeft();

            do{
                typename ContainerClass::BasicIterator leafIter(*octreeIterator.getCurrentListTargets());

                while( leafIter.hasNotFinished() ){
                    const ParticleClass& other = particles[leafIter.data().getIndex()];

                    potentialDiff.add(other.getPotential(),leafIter.data().getPotential());

                    fx.add(other.getForces().getX(),leafIter.data().getForces().getX());

                    fy.add(other.getForces().getY(),leafIter.data().getForces().getY());

                    fz.add(other.getForces().getZ(),leafIter.data().getForces().getZ());

                    leafIter.gotoNext();
                }
            } while(octreeIterator.moveRight());
        }

        delete[] particles;

        // Print for information
        Print("Potential diff is = ");
        Print(potentialDiff.getL2Norm());
        Print(potentialDiff.getInfNorm());
        Print("Fx diff is = ");
        Print(fx.getL2Norm());
        Print(fx.getInfNorm());
        Print("Fy diff is = ");
        Print(fy.getL2Norm());
        Print(fy.getInfNorm());
        Print("Fz diff is = ");
        Print(fz.getL2Norm());
        Print(fz.getInfNorm());

        // Assert
        const FReal MaximumDiffPotential = FReal(9e-5);
        const FReal MaximumDiffForces = FReal(9e-3);

        uassert(potentialDiff.getL2Norm() < MaximumDiffPotential);
        uassert(potentialDiff.getInfNorm() < MaximumDiffPotential);
        uassert(fx.getL2Norm()  < MaximumDiffForces);
        uassert(fx.getInfNorm() < MaximumDiffForces);
        uassert(fy.getL2Norm()  < MaximumDiffForces);
        uassert(fy.getInfNorm() < MaximumDiffForces);
        uassert(fz.getL2Norm()  < MaximumDiffForces);
        uassert(fz.getInfNorm() < MaximumDiffForces);

    }

	/** 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(){
		const unsigned int ORDER = 5;
		const FReal epsilon = FReal(1e-5);
		typedef IndexedParticle ParticleClass;
		typedef FVector<ParticleClass> ContainerClass;
		typedef FChebLeaf<ParticleClass,ContainerClass> LeafClass;
		typedef FChebMatrixKernelR MatrixKernelClass;
		typedef FChebCell<ORDER> CellClass;
		typedef FOctree<ParticleClass,CellClass,ContainerClass,LeafClass> OctreeClass;
		typedef FChebKernel<ParticleClass,CellClass,ContainerClass,MatrixKernelClass,ORDER> KernelClass;
        typedef FFmmAlgorithmPeriodic<OctreeClass,ParticleClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
		// run test
		RunTest<ParticleClass,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass>(epsilon);
	}

	/** TestChebSymKernel */
	void TestChebSymKernel(){
		const unsigned int ORDER = 5;
		const FReal epsilon = FReal(1e-5);
		typedef IndexedParticle ParticleClass;
		typedef FVector<ParticleClass> ContainerClass;
		typedef FChebLeaf<ParticleClass,ContainerClass> LeafClass;
		typedef FChebMatrixKernelR MatrixKernelClass;
		typedef FChebCell<ORDER> CellClass;
		typedef FOctree<ParticleClass,CellClass,ContainerClass,LeafClass> OctreeClass;
		typedef FChebSymKernel<ParticleClass,CellClass,ContainerClass,MatrixKernelClass,ORDER> KernelClass;
        typedef FFmmAlgorithmPeriodic<OctreeClass,ParticleClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
		// run test
		RunTest<ParticleClass,CellClass,ContainerClass,KernelClass,MatrixKernelClass,LeafClass,OctreeClass,FmmClass>(epsilon);
	}



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