testUnifTensorialAlgorithm.cpp 14.9 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright ScalFmm 2011 INRIA,
// 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
// author P. Banchard
// Modifs
//  O. Coulaud
19
// ==== CMAKE =====
20
// @FUSE_FFT
21
// ================
22 23
// Keep in private GIT

24 25 26 27 28 29

#include <iostream>

#include <cstdio>
#include <cstdlib>

30
#include "Files/FFmaGenericLoader.hpp"
31 32


33
#include "Kernels/Interpolation/FInterpMatrixKernel_TensorialInteractions.hpp"
34 35
#include "Kernels/Uniform/FUnifCell.hpp"
#include "Kernels/Uniform/FUnifTensorialKernel.hpp"
36

37 38
#include "Components/FSimpleLeaf.hpp"
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"
39

40 41
#include "Utils/FParameters.hpp"
#include "Utils/FMemUtils.hpp"
42

43 44
#include "Containers/FOctree.hpp"
#include "Containers/FVector.hpp"
45

46 47
#include "Core/FFmmAlgorithm.hpp"
#include "Core/FFmmAlgorithmThread.hpp"
48

49 50 51 52
#include "Utils/FParameterNames.hpp"

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

54 55 56 57 58 59 60
/**
 * 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[])
{
61 62 63 64 65
    FHelpDescribeAndExit(argc, argv,
                         "Test Uniform Tensorial kernel and compare it with the direct computation.",
                         FParameterDefinitions::OctreeHeight,FParameterDefinitions::NbThreads,
                         FParameterDefinitions::OctreeSubHeight, FParameterDefinitions::InputFile);

66
    typedef double FReal;
67 68 69 70
    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);
71 72

#ifdef _OPENMP
73 74
    omp_set_num_threads(NbThreads);
    std::cout << "\n>> Using " << omp_get_max_threads() << " threads.\n" << std::endl;
75
#else
76
    std::cout << "\n>> Sequential version.\n" << std::
77 78
#endif

79 80 81 82
     // init timer
     FTic time;


83
    // typedefs and infos
84
    typedef FInterpMatrixKernel_R_IJ<FReal> MatrixKernelClass;
85
    MatrixKernelClass::printInfo();
86 87 88 89 90 91 92

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

93 94 95 96 97
    const FReal CoreWidth = 0.;
    std::cout << "Core width: a=" << CoreWidth << std::endl;
    std::cout << std::endl;

    // Build matrix kernel
98 99 100 101
    const MatrixKernelClass MatrixKernel(CoreWidth);

    // init particles position and physical value
    struct TestParticle{
102
        FPoint<FReal> position;
103 104 105 106 107 108
        FReal forces[3][NPOT];
        FReal physicalValue[NPV];
        FReal potential[NPOT];
    };

    // open particle file
109
    FFmaGenericLoader<FReal> loader(filename);
110 111 112 113

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

    TestParticle* const particles = new TestParticle[loader.getNumberOfParticles()];
114
    for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
115
        FPoint<FReal> position;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        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;
133 134 135
        }
    }

136 137 138 139 140 141
    ////////////////////////////////////////////////////////////////////

    { // begin direct computation
        std::cout << "\nDirect Computation ... " << std::endl;
        time.tic();
        {
142 143
            for(FSize idxTarget = 0 ; idxTarget < loader.getNumberOfParticles() ; ++idxTarget){
                for(FSize idxOther =  idxTarget + 1 ; idxOther < loader.getNumberOfParticles() ; ++idxOther){
144 145 146
                    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],
147 148 149 150 151 152
                                             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);
153 154 155 156 157 158 159 160
                }
            }
        }
        time.tac();
        std::cout << "Done  " << "(@Direct Computation = "
                  << time.elapsed() << "s)." << std::endl;

    } // end direct computation
161

162
    ////////////////////////////////////////////////////////////////////
163

164
    {	// begin Lagrange kernel
165

166 167 168 169 170
        // 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));
171
        //const FReal ElementSize = LeafCellWidth / FReal(3.);
172 173 174 175 176 177 178 179 180 181 182
        //    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.");

183 184 185
        const unsigned int NVALS = 1;

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

187
        typedef FSimpleLeaf<FReal, ContainerClass >  LeafClass;
188
        typedef FUnifCell<FReal,ORDER,NRHS,NLHS,NVALS> CellClass;
189
        typedef FOctree<FReal, CellClass,ContainerClass,LeafClass> OctreeClass;
190
        typedef FUnifTensorialKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER,NVALS> KernelClass;
191 192 193 194 195 196 197 198 199 200 201 202 203
        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();

204
            for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
205 206 207 208 209 210 211 212 213
                // 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.");
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

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

230 231 232 233 234 235 236 237 238 239
            }

            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();
240 241
            KernelClass* kernels = new KernelClass(TreeHeight, loader.getBoxWidth(), loader.getCenterOfBox(),&MatrixKernel,BoxWidthExtension);
            FmmClass algorithm(&tree, kernels);
242 243 244 245 246 247 248 249
            algorithm.execute();
            time.tac();
            std::cout << "Done  " << "(@Algorithm = " << time.elapsed() << "s)." << std::endl;
        } // -----------------------------------------------------


        { // -----------------------------------------------------
            std::cout << "\nError computation ... " << std::endl;
250 251
            FMath::FAccurater<FReal> potentialDiff[NPOT];
            FMath::FAccurater<FReal> fx[NPOT], fy[NPOT], fz[NPOT];
252 253 254 255 256 257 258 259 260 261 262 263 264

            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);
265 266
                        const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
                        const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
267

268 269
                        for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
                            const FSize indexPartOrig = indexes[idxPart];
270 271 272 273

                            //PB: store potential in array[nbParticles]
                            checkPotential[indexPartOrig][idxPot]=potentials[idxPart];
                            checkfx[indexPartOrig][idxPot]=forcesX[idxPart];
274 275
                            //if(idxPart<10)
                            //    std::cout << "  FMM potentials[idxPartOrigin="<< indexPartOrig <<"]=" << potentials[idxPart] << "  DIRECT potentials[idxPartOrigin="<< indexPartOrig <<"]=" << particles[indexPartOrig].potential[idxPot] << std::endl;
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 313 314 315 316 317 318 319 320 321 322 323 324 325 326

                            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;
327
}