testAdaptiveChebSymFMM.cpp 8.56 KB
Newer Older
1
// ===================================================================================
2 3
// Copyright ScalFmm 2016 INRIA
//
4 5
// This software is a computer program whose purpose is to compute the FMM.
//
6
// This software is governed by Mozilla Public License Version 2.0 (MPL 2.0) and
7 8 9 10 11
// 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
12 13
// Mozilla Public License Version 2.0 (MPL 2.0) for more details.
// https://www.mozilla.org/en-US/MPL/2.0/
14 15 16 17 18
// ===================================================================================

// ==== CMAKE =====
// @FUSE_BLAS
// ================
19 20
// Keep in private GIT
// @SCALFMM_PRIVATE
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

#include <iostream>
#include <cstdio>

#include "Utils/FParameters.hpp"
#include "Utils/FTic.hpp"

#include "Containers/FOctree.hpp"
#include "Components/FSimpleLeaf.hpp"

#include "Utils/FPoint.hpp"

#include "Files/FFmaGenericLoader.hpp"
#include "Files/FRandomLoader.hpp"

#include "Components/FBasicKernels.hpp"
#include "Components/FSimpleIndexedLeaf.hpp"
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"

#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Chebyshev/FChebCell.hpp"

#include "Adaptive/FAdaptiveCell.hpp"
#include "Adaptive/FAdaptiveKernelWrapper.hpp"
#include "Adaptive/FAbstractAdaptiveKernel.hpp"
#include "Adaptive/FAdaptChebSymKernel.hpp"

#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Chebyshev/FChebCell.hpp"
#include "Adaptive/FAdaptTools.hpp"

#include "Adaptive/FAdaptivePrintKernel.hpp"

#include "Core/FFmmAlgorithm.hpp"
#include "Utils/FParameterNames.hpp"

/** This program show an example of use of the fmm basic algo
 * it also check that each particles is impacted each other particles
 */


// Simply create particles and try the kernels
int main(int argc, char ** argv){
    //
    const FParameterNames LocalOptionMinMultipoleThreshod {{"-sM"}," s_min^M threshold for Multipole (l+1)^2 for Spherical harmonic."};
    const FParameterNames LocalOptionMinLocalThreshod {{"-SL"}," s_min^L threshold for Local  (l+1)^2 for Spherical harmonics."};

    FHelpDescribeAndExit(argc, argv,
            "Test Adaptive kernel and compare it with the direct computation.",
            FParameterDefinitions::OctreeHeight,FParameterDefinitions::NbThreads,
            FParameterDefinitions::OctreeSubHeight, FParameterDefinitions::InputFile,
            LocalOptionMinMultipoleThreshod,LocalOptionMinLocalThreshod);

74
    typedef double FReal;
75
    const unsigned int P = 5 ;
76 77 78 79 80
    typedef FChebCell<FReal,P>                                        CellClass;
    typedef FP2PParticleContainerIndexed<FReal>            ContainerClass;
    typedef FSimpleLeaf<FReal, ContainerClass>    LeafClass;
    typedef FInterpMatrixKernelR<FReal>                               MatrixKernelClass;
    typedef FAdaptiveChebSymKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,P> KernelClass;
81 82
    typedef FAdaptiveCell< CellClass, ContainerClass >                                        CellWrapperClass;
    typedef FAdaptiveKernelWrapper< KernelClass, CellClass, ContainerClass >   KernelWrapperClass;
83
    typedef FOctree<FReal, CellWrapperClass, ContainerClass , LeafClass >                  OctreeClass;
84 85 86 87 88 89 90 91 92 93 94
    typedef FFmmAlgorithm<OctreeClass, CellWrapperClass, ContainerClass, KernelWrapperClass, LeafClass >     FmmClass;

    FTic counter;

    //////////////////////////////////////////////////////////////////////////////////
    const int sminM    = FParameters::getValue(argc,argv,LocalOptionMinMultipoleThreshod.options, P*P*P);
    const int sminL     = FParameters::getValue(argc,argv,LocalOptionMinLocalThreshod.options, P*P*P);
    const std::string fileName(FParameters::getStr(argc,argv,FParameterDefinitions::InputFile.options,   "../Data/noDistprolate50.out.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);

95
    FFmaGenericLoader<FReal> loader(fileName);
96
    const FSize NbPart = loader.getNumberOfParticles() ;
97 98 99 100 101 102 103 104 105 106 107 108 109
    //////////////////////////////////////////////////////////////////////////////////

    OctreeClass tree(TreeHeight, SubTreeHeight, loader.getBoxWidth(), loader.getCenterOfBox());

    //////////////////////////////////////////////////////////////////////////////////

    std::cout << "Creating & Inserting " << NbPart << " particles ..." << std::endl;
    std::cout << "\tHeight : " << TreeHeight << " \t sub-height : " << SubTreeHeight << std::endl;
    std::cout << "         criteria SM:  "<< sminM     <<std::endl
              << "         criteria SL:  "<< sminL     <<std::endl <<std::endl;

    counter.tic();

110
    FmaRWParticle<FReal, 8,8>* const particles = new FmaRWParticle<FReal, 8,8>[NbPart];
111 112
    loader.fillParticle(particles,NbPart);

113
    for(FSize idxPart = 0 ; idxPart < NbPart; ++idxPart){
114
        const FPoint<FReal> PP(particles[idxPart].getPosition() ) ;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        tree.insert(PP, idxPart, particles[idxPart].getPhysicalValue());
    }

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

    //////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////

    std::cout << "Working on particles ..." << std::endl;

    counter.tic();

    const MatrixKernelClass MatrixKernel;
    KernelWrapperClass kernels(TreeHeight, loader.getBoxWidth(), loader.getCenterOfBox(),&MatrixKernel,sminM,sminL);            // FTestKernels FBasicKernels
    FmmClass algo(&tree,&kernels);  //FFmmAlgorithm FFmmAlgorithmThread
    algo.execute();

    counter.tac();
    std::cout << "Done  " << "(@Algorithm = " << counter.elapsed() << " s)." << std::endl;

    /////////////////////////////////////////////////////////////////////////////////////////////////
    // Compute direct energy
    /////////////////////////////////////////////////////////////////////////////////////////////////
    FReal energyD = 0.0 ;
140
    for(FSize idx = 0 ; idx < loader.getNumberOfParticles()  ; ++idx){
141 142 143 144 145 146 147
        energyD +=  particles[idx].getPotential()*particles[idx].getPhysicalValue() ;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////
    // Compare
    /////////////////////////////////////////////////////////////////////////////////////////////////
    {
148 149
        FMath::FAccurater<FReal> potentialDiff;
        FMath::FAccurater<FReal> fx, fy, fz;
150 151 152 153 154 155 156 157 158 159 160
        FReal energy= 0.0;
        { // Check that each particle has been summed with all other

            //    std::cout << "indexPartOrig || DIRECT V fx || FMM V fx" << std::endl;

            tree.forEachCellLeaf([&](CellWrapperClass* cell, LeafClass* leaf){
                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();
161 162
                const FSize nbParticlesInLeaf           = leaf->getTargets()->getNbParticles();
                const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
163

164 165
                for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
                    const FSize indexPartOrig = indexes[idxPart];
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
                    potentialDiff.add(particles[indexPartOrig].getPotential(),potentials[idxPart]);
                    fx.add(particles[indexPartOrig].getForces()[0],forcesX[idxPart]);
                    fy.add(particles[indexPartOrig].getForces()[1],forcesY[idxPart]);
                    fz.add(particles[indexPartOrig].getForces()[2],forcesZ[idxPart]);
                    energy   += potentials[idxPart]*physicalValues[idxPart];

                }
            });
        }

        // Print for information
        std::cout << "Energy [relative L2 error] "  << FMath::Abs(energy-energyD) /energyD << std::endl;
        std::cout << "Potential " << potentialDiff << std::endl;
        std::cout << "Fx " << fx << std::endl;
        std::cout << "Fy " << fy << std::endl;
        std::cout << "Fz " << fz << std::endl;
    }

    delete[] particles;

    return 0;
}