LagrangeInterpolationFMM.cpp 9.78 KB
Newer Older
1
// See LICENCE file at project root
2 3 4

// ==== CMAKE =====
// @FUSE_FFT
5
// @FUSE_BLAS
6
//  ==== Git =====
7

8 9 10 11 12 13 14 15 16
// ================

#include <iostream>

#include <cstdio>
#include <cstdlib>
#include <string>

#include "ScalFmmConfig.h"
COULAUD Olivier's avatar
COULAUD Olivier committed
17
#include "Utils/FGlobal.hpp"
18

COULAUD Olivier's avatar
COULAUD Olivier committed
19 20
#include "Utils/FParameters.hpp"
#include "Utils/FParameterNames.hpp"
21

COULAUD Olivier's avatar
COULAUD Olivier committed
22 23
#include "Files/FFmaGenericLoader.hpp"
// UFMM
24 25 26
#include "Kernels/Uniform/FUnifCell.hpp"
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
#include "Kernels/Uniform/FUnifKernel.hpp"
COULAUD Olivier's avatar
COULAUD Olivier committed
27
// Leaves 
28 29 30
#include "Components/FSimpleLeaf.hpp"
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"

COULAUD Olivier's avatar
COULAUD Olivier committed
31

32 33 34 35 36

#include "Containers/FOctree.hpp"

#ifdef _OPENMP
#include "Core/FFmmAlgorithmThread.hpp"
COULAUD Olivier's avatar
COULAUD Olivier committed
37
#include "Core/FFmmAlgorithmSectionTask.hpp"
38 39 40 41 42
#else
#include "Core/FFmmAlgorithm.hpp"
#endif


43 44
#include <memory>

45 46 47 48 49 50 51 52 53 54 55 56 57 58
/**
 * This program runs the FMM Algorithm with the uniform interpolation kernel and compares the results with a direct computation.
 */
/// \file  LagrangeInterpolationFMM.cpp
//!
//! \brief This program runs the FMM Algorithm with the interpolation kernel based on uniform (grid points) interpolation (1/r kernel)
//!  \authors B. Bramas, O. Coulaud
//!
//!  This code is a short example to use the  Interpolation approach for the 1/r kernel


// Simply create particles and try the kernels
int main(int argc, char* argv[])
{
COULAUD Olivier's avatar
COULAUD Olivier committed
59 60 61 62 63
  FHelpDescribeAndExit(argc, argv,
		       "Driver for Lagrange interpolation kernel  (1/r kernel).",
		       FParameterDefinitions::InputFile, FParameterDefinitions::OctreeHeight,
		       FParameterDefinitions::OctreeSubHeight, FParameterDefinitions::InputFile,FParameterDefinitions::OutputFile,
		       FParameterDefinitions::NbThreads);
64 65


COULAUD Olivier's avatar
COULAUD Olivier committed
66 67 68 69 70
  const std::string defaultFile(SCALFMMDataPath+"unitCubeXYZQ100.bfma" );
  const std::string filename                = FParameters::getStr(argc,argv,FParameterDefinitions::InputFile.options, defaultFile.c_str());
  const unsigned int TreeHeight        = FParameters::getValue(argc, argv, FParameterDefinitions::OctreeHeight.options, 5);
  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 73


#ifdef _OPENMP
COULAUD Olivier's avatar
COULAUD Olivier committed
74 75
  omp_set_num_threads(NbThreads);
  std::cout << "\n>> Using " << omp_get_max_threads() << " threads.\n" << std::endl;
76
#else
COULAUD Olivier's avatar
COULAUD Olivier committed
77
  std::cout << "\n>> Sequential version.\n" << std::endl;
78
#endif
COULAUD Olivier's avatar
COULAUD Olivier committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  //
  std::cout <<     "Parameters  "<< std::endl
	    <<     "      Octree Depth      "<< TreeHeight <<std::endl
	    <<        "      SubOctree depth " << SubTreeHeight <<std::endl
	    <<     "      Input file  name: " <<filename <<std::endl
	    <<     "      Thread number:  " << NbThreads <<std::endl
	    <<std::endl;
  //
  // init timer
  FTic time;

  // open particle file
  ////////////////////////////////////////////////////////////////////
  //
  typedef double FReal;
  FFmaGenericLoader<FReal> loader(filename);
  //
  ////////////////////////////////////////////////////////////////////
  // begin Lagrange kernel
  // accuracy
  const unsigned int ORDER = 7;
  // typedefs
  typedef FP2PParticleContainerIndexed<FReal>                     ContainerClass;
  typedef FSimpleLeaf<FReal,  ContainerClass >                        LeafClass;
  typedef FUnifCell<FReal,ORDER>                                            CellClass;
  typedef FOctree<FReal, CellClass,ContainerClass,LeafClass>  OctreeClass;
  //
  typedef FInterpMatrixKernelR<FReal>                                                                              MatrixKernelClass;
  const MatrixKernelClass MatrixKernel;
  typedef FUnifKernel<FReal,CellClass,ContainerClass,MatrixKernelClass,ORDER>  KernelClass;
  //
#ifdef _OPENMP
COULAUD Olivier's avatar
COULAUD Olivier committed
111 112
   //typedef FFmmAlgorithmThread<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
   typedef FFmmAlgorithmSectionTask<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
COULAUD Olivier's avatar
COULAUD Olivier committed
113 114 115 116 117 118 119 120 121 122 123 124
#else
  typedef FFmmAlgorithm<OctreeClass,CellClass,ContainerClass,KernelClass,LeafClass> FmmClass;
#endif
  // 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();
125
    //
COULAUD Olivier's avatar
COULAUD Olivier committed
126 127
    FPoint<FReal> position;
    FReal physicalValue = 0.0;
128
    //
COULAUD Olivier's avatar
COULAUD Olivier committed
129 130 131 132 133 134 135 136 137 138 139 140 141
    for(FSize idxPart = 0 ; idxPart < loader.getNumberOfParticles() ; ++idxPart){
      //
      // Read particle per particle from file
      loader.fillParticle(&position,&physicalValue);
      //
      // put particle in octree
      tree.insert(position, idxPart, physicalValue);
    }

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

COULAUD Olivier's avatar
COULAUD Olivier committed
143 144 145 146
  { // -----------------------------------------------------
    std::cout << "\nLagrange FMM (ORDER="<< ORDER << ") ... " << std::endl;

    time.tic();
147
    //
COULAUD Olivier's avatar
COULAUD Olivier committed
148
    std::unique_ptr<KernelClass> kernels(new KernelClass(TreeHeight, loader.getBoxWidth(), loader.getCenterOfBox(),&MatrixKernel));
149
    //
COULAUD Olivier's avatar
COULAUD Olivier committed
150
    FmmClass algo(&tree, kernels.get());
151
    //
COULAUD Olivier's avatar
COULAUD Olivier committed
152
    algo.execute();   // Here the call of the FMM algorithm
153
    //
COULAUD Olivier's avatar
COULAUD Olivier committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    time.tac();
    std::cout << "Timers Far Field \n"
	      << "P2M " << algo.getTime(FAlgorithmTimers::P2MTimer) << " seconds\n"
	      << "M2M " << algo.getTime(FAlgorithmTimers::M2MTimer) << " seconds\n"
	      << "M2L " << algo.getTime(FAlgorithmTimers::M2LTimer) << " seconds\n"
	      << "L2L " << algo.getTime(FAlgorithmTimers::L2LTimer) << " seconds\n"
	      << "P2P and L2P " << algo.getTime(FAlgorithmTimers::NearTimer) << " seconds\n"
	      << std::endl;


    std::cout << "Done  " << "(@Algorithm = " << time.elapsed() << " s) ." << std::endl;
  }
  // -----------------------------------------------------
  //
  // Some output
  //
  //
  { // -----------------------------------------------------
    FSize N1=0, N2= loader.getNumberOfParticles()/2, N3= loader.getNumberOfParticles() -1; ;
    FReal energy =0.0 ;
174
    //
COULAUD Olivier's avatar
COULAUD Olivier committed
175
    //   Loop over all leaves
176
    //
COULAUD Olivier's avatar
COULAUD Olivier committed
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
    std::cout <<std::endl<<" &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& "<<std::endl;
    std::cout << std::scientific;
    std::cout.precision(10) ;

    tree.forEachLeaf([&](LeafClass* leaf){
	const FReal*const posX = leaf->getTargets()->getPositions()[0];
	const FReal*const posY = leaf->getTargets()->getPositions()[1];
	const FReal*const posZ = leaf->getTargets()->getPositions()[2];

	const FReal*const potentials = leaf->getTargets()->getPotentials();
	const FReal*const forcesX = leaf->getTargets()->getForcesX();
	const FReal*const forcesY = leaf->getTargets()->getForcesY();
	const FReal*const forcesZ = leaf->getTargets()->getForcesZ();
	const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
	const FReal*const physicalValues = leaf->getTargets()->getPhysicalValues();

	const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();

	for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
	  const FSize indexPartOrig = indexes[idxPart];
	  if ((indexPartOrig == N1) || (indexPartOrig == N2) || (indexPartOrig == N3)  ) {
	    std::cout << "Index "<< indexPartOrig <<"  potential  " << potentials[idxPart]
		      << " Pos "<<posX[idxPart]<<" "<<posY[idxPart]<<" "<<posZ[idxPart]
		      << "   Forces: " << forcesX[idxPart] << " " << forcesY[idxPart] << " "<< forcesZ[idxPart] <<std::endl;
	  }
	  energy += potentials[idxPart]*physicalValues[idxPart] ;
	}
      });
    std::cout <<std::endl<<"Energy: "<< energy<<std::endl;
    std::cout <<std::endl<<" &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& "<<std::endl<<std::endl;

  }
  // -----------------------------------------------------
  if(FParameters::existParameter(argc, argv, FParameterDefinitions::OutputFile.options)){
    std::string name(FParameters::getStr(argc,argv,FParameterDefinitions::OutputFile.options,   "output.fma"));
    FFmaGenericWriter<FReal> writer(name) ;
213
    //
COULAUD Olivier's avatar
COULAUD Olivier committed
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
    FSize NbPoints = loader.getNumberOfParticles();
    FReal * particles ;
    particles = new FReal[8*NbPoints] ;
    memset(particles,0,8*NbPoints*sizeof(FReal));
    FSize j = 0 ;
    tree.forEachLeaf([&](LeafClass* leaf){
	//
	// Input
	const FReal*const posX = leaf->getTargets()->getPositions()[0];
	const FReal*const posY = leaf->getTargets()->getPositions()[1];
	const FReal*const posZ = leaf->getTargets()->getPositions()[2];
	const FReal*const physicalValues = leaf->getTargets()->getPhysicalValues();
	const FVector<FSize>& indexes = leaf->getTargets()->getIndexes();
	//
	// Computed data
	const FReal*const potentials = leaf->getTargets()->getPotentials();
	const FReal*const forcesX = leaf->getTargets()->getForcesX();
	const FReal*const forcesY = leaf->getTargets()->getForcesY();
	const FReal*const forcesZ = leaf->getTargets()->getForcesZ();
	//
	const FSize nbParticlesInLeaf = leaf->getTargets()->getNbParticles();
	for(FSize idxPart = 0 ; idxPart < nbParticlesInLeaf ; ++idxPart){
	  j = 8*indexes[idxPart];
	  particles[j]      = posX[idxPart] ;
	  particles[j+1]  = posY[idxPart] ;
	  particles[j+2]  = posZ[idxPart] ;
	  particles[j+3]  = physicalValues[idxPart] ;
	  particles[j+4]  = potentials[idxPart] ;
	  particles[j+5]  =  forcesX[idxPart] ;
	  particles[j+6]  =  forcesY[idxPart] ;
	  particles[j+7]  =  forcesZ[idxPart] ;
	}
      });

    writer.writeHeader( loader.getCenterOfBox(), loader.getBoxWidth() ,  NbPoints, sizeof(FReal), 8) ;
    writer.writeArrayOfReal(particles,  8 , NbPoints);

    delete[] particles;
252

COULAUD Olivier's avatar
COULAUD Olivier committed
253 254 255 256 257 258
    //
    std::string name1( "output.fma");
    //
    FFmaGenericWriter<FReal> writer1(name1) ;
    writer1.writeDistributionOfParticlesFromOctree(&tree,NbPoints) ;
  }
259

260

COULAUD Olivier's avatar
COULAUD Olivier committed
261
  return 0;
262
}