testUnifTensorialAlgorithm.cpp 14.1 KB
Newer Older
1
// See LICENCE file at project root
2 3 4
// author P. Banchard
// Modifs
//  O. Coulaud
5
// ==== CMAKE =====
6
// @FUSE_FFT
7
// ================
8 9
// Keep in private GIT

10 11 12 13 14 15

#include <iostream>

#include <cstdio>
#include <cstdlib>

16
#include "Files/FFmaGenericLoader.hpp"
17 18


19
#include "Kernels/Interpolation/FInterpMatrixKernel_TensorialInteractions.hpp"
20 21
#include "Kernels/Uniform/FUnifCell.hpp"
#include "Kernels/Uniform/FUnifTensorialKernel.hpp"
22

23 24
#include "Components/FSimpleLeaf.hpp"
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
25

26 27
#include "Utils/FParameters.hpp"
#include "Utils/FMemUtils.hpp"
28

29 30
#include "Containers/FOctree.hpp"
#include "Containers/FVector.hpp"
31

32 33
#include "Core/FFmmAlgorithm.hpp"
#include "Core/FFmmAlgorithmThread.hpp"
34

35 36 37 38
#include "Utils/FParameterNames.hpp"

// For std::array<> (i.e. for Tensorial kernels purpose)
#include <array>
39

40 41 42 43 44 45 46
/**
 * This program runs the FMM Algorithm with the Uniform kernel and compares the results with a direct computation.
 */

// Simply create particles and try the kernels
int main(int argc, char* argv[])
{
47 48 49 50 51
    FHelpDescribeAndExit(argc, argv,
                         "Test Uniform Tensorial kernel and compare it with the direct computation.",
                         FParameterDefinitions::OctreeHeight,FParameterDefinitions::NbThreads,
                         FParameterDefinitions::OctreeSubHeight, FParameterDefinitions::InputFile);

52
    typedef double FReal;
53 54 55 56
    const char* const filename       = FParameters::getStr(argc,argv,FParameterDefinitions::InputFile.options, "../Data/test20k.fma");
    const unsigned int TreeHeight    = FParameters::getValue(argc, argv, FParameterDefinitions::OctreeHeight.options, 3);
    const unsigned int SubTreeHeight = FParameters::getValue(argc, argv, FParameterDefinitions::OctreeSubHeight.options, 2);
    const unsigned int NbThreads     = FParameters::getValue(argc, argv, FParameterDefinitions::NbThreads.options, 1);
57 58

#ifdef _OPENMP
59 60
    omp_set_num_threads(NbThreads);
    std::cout << "\n>> Using " << omp_get_max_threads() << " threads.\n" << std::endl;
61
#else
62
    std::cout << "\n>> Sequential version.\n" << std::
63 64
#endif

65 66 67 68
     // init timer
     FTic time;


69
    // typedefs and infos
70
    typedef FInterpMatrixKernel_R_IJ<FReal> MatrixKernelClass;
71
    MatrixKernelClass::printInfo();
72 73 74 75 76 77 78

    // useful features of matrix kernel
    const unsigned int NPV  = MatrixKernelClass::NPV;
    const unsigned int NPOT = MatrixKernelClass::NPOT;
    const unsigned int NRHS = MatrixKernelClass::NRHS;
    const unsigned int NLHS = MatrixKernelClass::NLHS;

79 80 81 82 83
    const FReal CoreWidth = 0.;
    std::cout << "Core width: a=" << CoreWidth << std::endl;
    std::cout << std::endl;

    // Build matrix kernel
84 85 86 87
    const MatrixKernelClass MatrixKernel(CoreWidth);

    // init particles position and physical value
    struct TestParticle{
88
        FPoint<FReal> position;
89 90 91 92 93 94
        FReal forces[3][NPOT];
        FReal physicalValue[NPV];
        FReal potential[NPOT];
    };

    // open particle file
95
    FFmaGenericLoader<FReal> loader(filename);
96 97 98 99

    if(!loader.isOpen()) throw std::runtime_error("Particle file couldn't be opened!");

    TestParticle* const particles = new TestParticle[loader.getNumberOfParticles()];
100
    for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
101
        FPoint<FReal> position;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        FReal physicalValue = 0.0;
        loader.fillParticle(&position,&physicalValue);

        // get copy
        particles[idxPart].position       = position;
        // Set physical values
        for(unsigned idxPV = 0; idxPV<NPV;++idxPV){
            //    //   Either copy same physical value in each component
            particles[idxPart].physicalValue[idxPV]  = physicalValue;
            // ... or set random value
            //      particles[idxPart].physicalValue[idxPV]  = physicalValue*FReal(drand48());
        }
        for(unsigned idxPot = 0; idxPot<NPOT;++idxPot){
            particles[idxPart].potential[idxPot]      = 0.0;
            particles[idxPart].forces[0][idxPot]      = 0.0;
            particles[idxPart].forces[1][idxPot]      = 0.0;
            particles[idxPart].forces[2][idxPot]      = 0.0;
119 120 121
        }
    }

122 123 124 125 126 127
    ////////////////////////////////////////////////////////////////////

    { // begin direct computation
        std::cout << "\nDirect Computation ... " << std::endl;
        time.tic();
        {
128 129
            for(FSize idxTarget = 0 ; idxTarget < loader.getNumberOfParticles() ; ++idxTarget){
                for(FSize idxOther =  idxTarget + 1 ; idxOther < loader.getNumberOfParticles() ; ++idxOther){
130 131 132
                    FP2P::MutualParticlesKIJ(particles[idxTarget].position.getX(), particles[idxTarget].position.getY(),
                                             particles[idxTarget].position.getZ(), particles[idxTarget].physicalValue,
                                             particles[idxTarget].forces[0], particles[idxTarget].forces[1],
133 134 135 136 137 138
                                             particles[idxTarget].forces[2], particles[idxTarget].potential,
                                             particles[idxOther].position.getX(), particles[idxOther].position.getY(),
                                             particles[idxOther].position.getZ(), particles[idxOther].physicalValue,
                                             particles[idxOther].forces[0], particles[idxOther].forces[1],
                                             particles[idxOther].forces[2], particles[idxOther].potential,
                                             &MatrixKernel);
139 140 141 142 143 144 145 146
                }
            }
        }
        time.tac();
        std::cout << "Done  " << "(@Direct Computation = "
                  << time.elapsed() << "s)." << std::endl;

    } // end direct computation
147

148
    ////////////////////////////////////////////////////////////////////
149

150
    {	// begin Lagrange kernel
151

152 153 154 155 156
        // accuracy
        const unsigned int ORDER = 5 ;
        // set box width extension
        // ... either deduce from element size
        const FReal LeafCellWidth = FReal(loader.getBoxWidth()) / FReal(FMath::pow(2.,TreeHeight-1));
157
        //const FReal ElementSize = LeafCellWidth / FReal(3.);
158 159 160 161 162 163 164 165 166 167 168
        //    const FReal BoxWidthExtension = ElementSize; // depends on type of element
        // ... or set to arbitrary value (0. means no extension)
        const FReal BoxWidthExtension = FReal(0.);

        std::cout << "LeafCellWidth=" << LeafCellWidth
                  << ", BoxWidthExtension=" << BoxWidthExtension <<std::endl;

        // stop execution if interactions are homog and box extension is required
        if(MatrixKernelClass::Type==HOMOGENEOUS && BoxWidthExtension>0.)
            throw std::runtime_error("Extension of box width is not yet supported for homogeneous kernels! Work-around: artificially set Type to NON_HOMOGENEOUS.");

169 170 171
        const unsigned int NVALS = 1;

        typedef FP2PParticleContainerIndexed<FReal,NRHS,NLHS,NVALS> ContainerClass;
172

173
        typedef FSimpleLeaf<FReal, ContainerClass >  LeafClass;
174
        typedef FUnifCell<FReal,ORDER,NRHS,NLHS,NVALS> CellClass;
175
        typedef FOctree<FReal, CellClass,ContainerClass,LeafClass> OctreeClass;
176
        typedef FUnifTensorialKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER,NVALS> KernelClass;
177 178 179 180 181 182 183 184 185 186 187 188 189
        typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
        //  typedef FFmmAlgorithmThread<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;

        // init oct-tree
        OctreeClass tree(TreeHeight, SubTreeHeight, loader.getBoxWidth(), loader.getCenterOfBox());


        { // -----------------------------------------------------
            std::cout << "Creating & Inserting " << loader.getNumberOfParticles()
                      << " particles ..." << std::endl;
            std::cout << "\tHeight : " << TreeHeight << " \t sub-height : " << SubTreeHeight << std::endl;
            time.tic();

190
            for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
191 192 193 194 195 196 197 198 199
                // put in tree
                if(NPV==1) // scalar kernels like ONE_OVER_R
                    tree.insert(particles[idxPart].position, idxPart,
                                particles[idxPart].physicalValue[0]);
                else if(NPV==3) // R_IJ or IOR
                    tree.insert(particles[idxPart].position, idxPart,
                                particles[idxPart].physicalValue[0], particles[idxPart].physicalValue[1], particles[idxPart].physicalValue[2]);
                else
                    std::runtime_error("NPV not yet supported in test! Add new case.");
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

                // 
                // [TODO] Fix insertion of multiple physical values using std::array !!
                //
                //// Convert FReal[NVALS] to std::array<FReal,NVALS>
                //std::array<FReal, (NPV+4*NPOT)*NVALS> physicalState;
                //for(int idxVals = 0 ; idxVals < NVALS ; ++idxVals){
                //    for(int idxPV = 0 ; idxPV < NPV ; ++idxPV)
                //        physicalState[idxPV*NVALS+idxVals]=particles[idxPart].physicalValue[idxPV];
                //    physicalState[(NPV+0)*NVALS+idxVals]=0.0;
                //    physicalState[(NPV+1)*NVALS+idxVals]=0.0;
                //    physicalState[(NPV+2)*NVALS+idxVals]=0.0;
                //    physicalState[(NPV+3)*NVALS+idxVals]=0.0;
                //}
                //tree.insert(particles[idxPart].position, idxPart,physicalState);

216 217 218 219 220 221 222 223 224 225
            }

            time.tac();
            std::cout << "Done  " << "(@Creating and Inserting Particles = "
                      << time.elapsed() << "s)." << std::endl;
        } // -----------------------------------------------------

        { // -----------------------------------------------------
            std::cout << "\nLagrange/Uniform grid FMM (ORDER="<< ORDER << ") ... " << std::endl;
            time.tic();
226 227
            KernelClass* kernels = new KernelClass(TreeHeight, loader.getBoxWidth(), loader.getCenterOfBox(),&MatrixKernel,BoxWidthExtension);
            FmmClass algorithm(&tree, kernels);
228 229 230 231 232 233 234 235
            algorithm.execute();
            time.tac();
            std::cout << "Done  " << "(@Algorithm = " << time.elapsed() << "s)." << std::endl;
        } // -----------------------------------------------------


        { // -----------------------------------------------------
            std::cout << "\nError computation ... " << std::endl;
236 237
            FMath::FAccurater<FReal> potentialDiff[NPOT];
            FMath::FAccurater<FReal> fx[NPOT], fy[NPOT], fz[NPOT];
238 239 240 241 242 243 244 245 246 247 248 249 250

            FReal checkPotential[20000][NPOT];
            FReal checkfx[20000][NPOT];

            { // Check that each particle has been summed with all other

                tree.forEachLeaf([&](LeafClass* leaf){
                    for(unsigned idxPot = 0; idxPot<NPOT;++idxPot){

                        const FReal*const potentials = leaf->getTargets()->getPotentials(idxPot);
                        const FReal*const forcesX = leaf->getTargets()->getForcesX(idxPot);
                        const FReal*const forcesY = leaf->getTargets()->getForcesY(idxPot);
                        const FReal*const forcesZ = leaf->getTargets()->getForcesZ(idxPot);
251 252
                        const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
                        const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
253

254 255
                        for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
                            const FSize indexPartOrig = indexes[idxPart];
256 257 258 259

                            //PB: store potential in array[nbParticles]
                            checkPotential[indexPartOrig][idxPot]=potentials[idxPart];
                            checkfx[indexPartOrig][idxPot]=forcesX[idxPart];
260 261
                            //if(idxPart<10)
                            //    std::cout << "  FMM potentials[idxPartOrigin="<< indexPartOrig <<"]=" << potentials[idxPart] << "  DIRECT potentials[idxPartOrigin="<< indexPartOrig <<"]=" << particles[indexPartOrig].potential[idxPot] << std::endl;
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

                            potentialDiff[idxPot].add(particles[indexPartOrig].potential[idxPot],potentials[idxPart]);
                            fx[idxPot].add(particles[indexPartOrig].forces[0][idxPot],forcesX[idxPart]);
                            fy[idxPot].add(particles[indexPartOrig].forces[1][idxPot],forcesY[idxPart]);
                            fz[idxPot].add(particles[indexPartOrig].forces[2][idxPot],forcesZ[idxPart]);
                        }
                    }// NPOT
                });
            }


            // Print for information
            std::cout << "\nRelative Inf/L2 errors: " << std::endl;
            std::cout << "  Potential: " << std::endl;
            for(unsigned idxPot = 0; idxPot<NPOT;++idxPot) {
                std::cout << "    " << idxPot << ": "
                          << potentialDiff[idxPot].getRelativeInfNorm() << ", "
                          << potentialDiff[idxPot].getRelativeL2Norm()
                          << std::endl;
            }
            std::cout << std::endl;
            std::cout << "  Fx: " << std::endl;
            for(unsigned idxPot = 0; idxPot<NPOT;++idxPot) {
                std::cout << "    " << idxPot << ": "
                          << fx[idxPot].getRelativeInfNorm() << ", "
                          << fx[idxPot].getRelativeL2Norm()
                          << std::endl;
            }
            std::cout  << std::endl;
            std::cout << "  Fy: " << std::endl;
            for(unsigned idxPot = 0; idxPot<NPOT;++idxPot) {
                std::cout << "    " << idxPot << ": "
                          << fy[idxPot].getRelativeInfNorm() << ", "
                          << fy[idxPot].getRelativeL2Norm()
                          << std::endl;
            }
            std::cout  << std::endl;
            std::cout << "  Fz: " << std::endl;
            for(unsigned idxPot = 0; idxPot<NPOT;++idxPot) {
                std::cout << "    " << idxPot << ": "
                          << fz[idxPot].getRelativeInfNorm() << ", "
                          << fz[idxPot].getRelativeL2Norm()
                          << std::endl;
            }
            std::cout << std::endl;

        } // -----------------------------------------------------

    } // end Lagrange kernel

    return 0;
313
}