FAdaptChebSymKernel.hpp 16.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef FADAPTCHEBSYMKERNEL_HPP
#define FADAPTCHEBSYMKERNEL_HPP
// ===================================================================================
// Copyright ScalFmm 2011 INRIA,
// 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".
// ===================================================================================

#include "Utils/FGlobal.hpp"
19

20
#include "Utils/FPoint.hpp"
21

22 23 24
#include "Adaptive/FAdaptiveCell.hpp"
#include "Adaptive/FAdaptiveKernelWrapper.hpp"
#include "Adaptive/FAbstractAdaptiveKernel.hpp"
25 26 27 28
#include "Kernels/Chebyshev/FChebSymKernel.hpp"

class FTreeCoordinate;

29 30 31
// ==== CMAKE =====
// @FUSE_BLAS
// ================
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

// for verbosity only!!!
//#define COUNT_BLOCKED_INTERACTIONS

// if timings should be logged
//#define LOG_TIMINGS

/**
 * @author O. Coulaud
 * @class FAdaptChebSymKernel
 * @brief
 * Please read the license
 *
 * This kernels implement the Chebyshev interpolation based FMM operators
 * exploiting the symmetries in the far-field. It implements all interfaces
 * (P2P, P2M, M2M, M2L, L2L, L2P) which are required by the FFmmAlgorithm and
 * FFmmAlgorithmThread.
 *
 * @tparam CellClass Type of cell
 * @tparam ContainerClass Type of container to store particles
 * @tparam MatrixKernelClass Type of matrix kernel function
 * @tparam ORDER Chebyshev interpolation order
 */

56 57 58 59 60
template< class CellClass, class ContainerClass, class MatrixKernelClass, int ORDER, int NVALS = 1>
class FAdaptiveChebSymKernel : FChebSymKernel<CellClass, ContainerClass, MatrixKernelClass, ORDER, NVALS>
, public FAbstractAdaptiveKernel<CellClass, ContainerClass> {
	//
	typedef FChebSymKernel<CellClass, ContainerClass, MatrixKernelClass, ORDER, NVALS>	KernelBaseClass;
61 62 63 64 65
	enum {order = ORDER,
        nnodes = TensorTraits<ORDER>::nnodes};

  const MatrixKernelClass *const MatrixKernel;

66 67
public:

68 69 70 71 72 73 74 75 76 77 78 79 80 81
	using KernelBaseClass::P2M;
	using KernelBaseClass::M2M;
	using KernelBaseClass::M2L;
	using KernelBaseClass::finishedLevelM2L;
	using KernelBaseClass::L2L;
	using KernelBaseClass::L2P;
	using KernelBaseClass::P2P;
	using KernelBaseClass::P2PRemote;
	//	/**
	//	 * The constructor initializes all constant attributes and it reads the
	//	 * precomputed and compressed M2L operators from a binary file (an
	//	 * runtime_error is thrown if the required file is not valid).
	//	 */
	FAdaptiveChebSymKernel(const int inTreeHeight, const FReal inBoxWidth,
82
                         const FPoint& inBoxCenter, const MatrixKernelClass *const inMatrixKernel) : KernelBaseClass(inTreeHeight, inBoxWidth, inBoxCenter, inMatrixKernel), MatrixKernel(inMatrixKernel)
83 84 85
	{}
	//	/** Copy constructor */
	FAdaptiveChebSymKernel(const FAdaptiveChebSymKernel& other)
86
  : KernelBaseClass(other), MatrixKernel(other.MatrixKernel)
87
		{	}
88

89 90 91 92 93
	//
	//	/** Destructor */
		~FAdaptiveChebSymKernel()
		{
			//this->~KernelBaseClass() ;
94
		}
95
	void P2M(CellClass* const pole, const int cellLevel, const ContainerClass* const particles) override {
96 97 98

		const FPoint CellCenter(KernelBaseClass::getCellCenter(pole->getCoordinate(),cellLevel));
		const FReal BoxWidth = KernelBaseClass::BoxWidth / FMath::pow(2.0,cellLevel);
99

100
		for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){
101 102 103
      // 1) apply Sy
			KernelBaseClass::Interpolator->applyP2M(CellCenter, BoxWidth,
                                              pole->getMultipole(idxRhs), particles);
104 105
		}

106
	}
107

108 109 110 111 112 113 114 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 140 141 142 143 144 145 146 147
	void M2M(CellClass* const pole, const int poleLevel, const CellClass* const subCell, const int subCellLevel) override {

    const FPoint subCellCenter(KernelBaseClass::getCellCenter(subCell->getCoordinate(),subCellLevel));
    const FReal subCellWidth(KernelBaseClass::BoxWidth / FReal(FMath::pow(2.0,subCellLevel))); 

    const FPoint poleCellCenter(KernelBaseClass::getCellCenter(pole->getCoordinate(),poleLevel));
    const FReal poleCellWidth(KernelBaseClass::BoxWidth / FReal(FMath::pow(2.0, poleLevel))); 

    ////////////////////////////////////////////////////////////////////////////
    /// p^6 version
    // allocate memory
    FReal* subChildParentInterpolator = new FReal [nnodes * nnodes];

    // set child info
    FPoint ChildRoots[nnodes], localChildRoots[nnodes];
    FChebTensor<ORDER>::setRoots(subCellCenter, subCellWidth, ChildRoots);

    // map global position of roots to local position in parent cell
    const map_glob_loc map(poleCellCenter, poleCellWidth);
    for (unsigned int n=0; n<nnodes; ++n)
      map(ChildRoots[n], localChildRoots[n]);

    // assemble child - parent - interpolator
    KernelBaseClass::Interpolator->assembleInterpolator(nnodes, localChildRoots, subChildParentInterpolator);

    for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){

      // 1) apply Sy (using tensor product M2M with an interpolator computed on the fly)

      // Do NOT reset multipole expansion !
      //FBlas::scal(nnodes, FReal(0.), pole->getMultipole(idxRhs));

      /// p^6 version
      FBlas::gemtva(nnodes, nnodes, FReal(1.),
                    subChildParentInterpolator,
                    const_cast<FReal*>(subCell->getMultipole(idxRhs)), pole->getMultipole(idxRhs));


    }// NVALS

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
	void P2L(CellClass* const local, const int localLevel, const ContainerClass* const particles) override {

    // Target cell: local
    const FReal localCellWidth(KernelBaseClass::BoxWidth / FReal(FMath::pow(2.0, localLevel))); 
    const FPoint localCellCenter(KernelBaseClass::getCellCenter(local->getCoordinate(),localLevel));

    // interpolation points of target (X) cell
    FPoint X[nnodes];
    FChebTensor<order>::setRoots(localCellCenter, localCellWidth, X);

    // read positions
    const FReal*const positionsX = particles->getPositions()[0];
    const FReal*const positionsY = particles->getPositions()[1];
    const FReal*const positionsZ = particles->getPositions()[2];

    for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){

      // read physicalValue
      const FReal*const physicalValues = particles->getPhysicalValues();

      // apply P2L
      for (unsigned int idxPart=0; idxPart<particles->getNbParticles(); ++idxPart){

        const FPoint y = FPoint(positionsX[idxPart],
                                positionsY[idxPart],
                                positionsZ[idxPart]);

        for (unsigned int m=0; m<nnodes; ++m)
          local->getLocal(idxRhs)[m]+=MatrixKernel->evaluate(X[m], y) * physicalValues[idxPart];

      }

    }// NVALS

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
	void M2L(CellClass* const local, const int localLevel, const CellClass* const pole, const int poleLevel) override {

    // Source cell: pole
    const FReal poleCellWidth(KernelBaseClass::BoxWidth / FReal(FMath::pow(2.0, poleLevel))); 
    const FPoint poleCellCenter(KernelBaseClass::getCellCenter(pole->getCoordinate(),poleLevel));

    // Target cell: local
    const FReal localCellWidth(KernelBaseClass::BoxWidth / FReal(FMath::pow(2.0, localLevel))); 
    const FPoint localCellCenter(KernelBaseClass::getCellCenter(local->getCoordinate(),localLevel));

    // interpolation points of source (Y) and target (X) cell
    FPoint X[nnodes], Y[nnodes];
    FChebTensor<order>::setRoots(poleCellCenter, poleCellWidth, Y);
    FChebTensor<order>::setRoots(localCellCenter, localCellWidth, X);


    for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){

      // Dense M2L
      const FReal *const MultipoleExpansion = pole->getMultipole(idxRhs);
    
      for (unsigned int m=0; m<nnodes; ++m)
        for (unsigned int n=0; n<nnodes; ++n){
          local->getLocal(idxRhs)[m]+=MatrixKernel->evaluate(X[m], Y[n]) * MultipoleExpansion[n];
          
        }
    }
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
	void M2P(const CellClass* const pole, const int poleLevel, ContainerClass* const particles) override {

    // Source cell: pole
    const FReal poleCellWidth(KernelBaseClass::BoxWidth / FReal(FMath::pow(2.0, poleLevel))); 
    const FPoint poleCellCenter(KernelBaseClass::getCellCenter(pole->getCoordinate(),poleLevel));

    // interpolation points of source (Y) cell
    FPoint Y[nnodes];
    FChebTensor<order>::setRoots(poleCellCenter, poleCellWidth, Y);

    // read positions
    const FReal*const positionsX = particles->getPositions()[0];
    const FReal*const positionsY = particles->getPositions()[1];
    const FReal*const positionsZ = particles->getPositions()[2];

    for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){

      const FReal *const MultipoleExpansion = pole->getMultipole(idxRhs);
    
      // apply M2P
      for (unsigned int idxPart=0; idxPart<particles->getNbParticles(); ++idxPart){

        const FPoint x = FPoint(positionsX[idxPart],positionsY[idxPart],positionsZ[idxPart]);

        FReal targetValue=0.;
        {
          for (unsigned int n=0; n<nnodes; ++n)
            targetValue +=
              MatrixKernel->evaluate(x, Y[n]) * MultipoleExpansion[/*idxLhs*nnodes+*/n];
        }

        // get potential
        FReal*const potentials = particles->getPotentials(/*idxPot*/);
        // add contribution to potential
        potentials[idxPart] += (targetValue);

      }// Particles

    }// NVALS

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 283 284 285 286 287 288 289 290 291 292
	void L2L(const CellClass* const local, const int localLevel, CellClass* const subCell, const int subCellLevel) override {

    const FPoint subCellCenter(KernelBaseClass::getCellCenter(subCell->getCoordinate(),subCellLevel));
    const FReal subCellWidth(KernelBaseClass::BoxWidth / FReal(FMath::pow(2.0,subCellLevel))); 

    const FPoint localCenter(KernelBaseClass::getCellCenter(local->getCoordinate(),localLevel));
    const FReal localWidth(KernelBaseClass::BoxWidth / FReal(FMath::pow(2.0,localLevel))); 

    ////////////////////////////////////////////////////////////////////////////
    /// p^6 version
    // allocate memory
    FReal* subChildParentInterpolator = new FReal [nnodes * nnodes];

    // set child info
    FPoint ChildRoots[nnodes], localChildRoots[nnodes];
    FChebTensor<ORDER>::setRoots(subCellCenter, subCellWidth, ChildRoots);

    // map global position of roots to local position in parent cell
    const map_glob_loc map(localCenter, localWidth);
    for (unsigned int n=0; n<nnodes; ++n)
      map(ChildRoots[n], localChildRoots[n]);

    // assemble child - parent - interpolator
    KernelBaseClass::Interpolator->assembleInterpolator(nnodes, localChildRoots, subChildParentInterpolator);

    for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){

      // 2) apply Sx

      /// p^6 version
      FBlas::gemva(nnodes, nnodes, FReal(1.),
                   subChildParentInterpolator,
                   const_cast<FReal*>(local->getLocal(idxRhs)), subCell->getLocal(idxRhs));

    }// NVALS

293 294
	}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	void L2P(const CellClass* const local, const int cellLevel, ContainerClass* const particles)  override {

    const FPoint CellCenter(KernelBaseClass::getCellCenter(local->getCoordinate(),cellLevel));
		const FReal BoxWidth = KernelBaseClass::BoxWidth / FMath::pow(2.0,cellLevel);

    for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){

      // 2.a) apply Sx
      KernelBaseClass::Interpolator->applyL2P(CellCenter, BoxWidth,
                                              local->getLocal(idxRhs), particles);

      // 2.b) apply Px (grad Sx)
      KernelBaseClass::Interpolator->applyL2PGradient(CellCenter, BoxWidth,
                                                      local->getLocal(idxRhs), particles);

    }

312 313
	}

314 315 316 317 318 319
	void P2P(ContainerClass* target, const ContainerClass* sources)  override {
//		long long int*const particlesAttributes = target->getDataDown();
//		for(int idxPart = 0 ; idxPart < target->getNbParticles() ; ++idxPart){
//			particlesAttributes[idxPart] += sources->getNbParticles();
//		}
	}
320

321 322 323 324 325 326 327 328 329 330
	bool preferP2M(const ContainerClass* const particles) override {
		return particles->getNbParticles() < 10;
	}
	bool preferP2M(const int /*atLevel*/, const ContainerClass*const particles[], const int nbContainers) override {
		int counterParticles = 0;
		for(int idxContainer = 0 ; idxContainer < nbContainers ; ++idxContainer){
			counterParticles += particles[idxContainer]->getNbParticles();
		}
		return counterParticles < 10;
	}
331 332
};

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
//
//template < class CellClass,	class ContainerClass,	class MatrixKernelClass, int ORDER, int NVALS = 1>
//class FAdaptChebSymKernel
//		: public FChebSymKernel<CellClass, ContainerClass, MatrixKernelClass, ORDER, NVALS>
//{
//	typedef FChebSymKernel<CellClass, ContainerClass, MatrixKernelClass, ORDER, NVALS>	KernelBaseClass;
//
//#ifdef LOG_TIMINGS
//	FTic time;
//	FReal t_m2l_1, t_m2l_2, t_m2l_3;
//#endif
//
//public:
//	/**
//	 * The constructor initializes all constant attributes and it reads the
//	 * precomputed and compressed M2L operators from a binary file (an
//	 * runtime_error is thrown if the required file is not valid).
//	 */
//	FAdaptChebSymKernel(const int inTreeHeight,
//			const FReal inBoxWidth,
//			const FPoint& inBoxCenter)
//: KernelBaseClass(inTreeHeight, inBoxWidth, inBoxCenter)
//{
//
//#ifdef LOG_TIMINGS
//		t_m2l_1 = FReal(0.);
//		t_m2l_2 = FReal(0.);
//		t_m2l_3 = FReal(0.);
//#endif
//}
//
//
//	/** Copy constructor */
//	FAdaptChebSymKernel(const FAdaptChebSymKernel& other)
//	: KernelBaseClass(other)
//	{	}
//
//
//
//	/** Destructor */
//	~FAdaptChebSymKernel()
//	{
//		this->~KernelBaseClass() ;
//#ifdef LOG_TIMINGS
//		std::cout << "- Permutation took " << t_m2l_1 << "s"
//				<< "\n- GEMMT and GEMM took " << t_m2l_2 << "s"
//				<< "\n- Unpermutation took " << t_m2l_3 << "s"
//				<< std::endl;
//#endif
//	}
//
//
//	void P2MAdapt(CellClass* const ParentCell,  const int &level)
//	{
//		const FPoint LeafCellCenter(KernelBaseClass::getLeafCellCenter(ParentCell->getCoordinate()));
//		const FReal BoxWidth = KernelBaseClass::BoxWidthLeaf*FMath::pow(2.0,KernelBaseClass::TreeHeight-level);
//		//
//		for(int i = 0 ; i <ParentCell->getLeavesSize(); ++i ){
//			//
//			for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){
//				KernelBaseClass::Interpolator->applyP2M(LeafCellCenter, BoxWidth,
//						ParentCell->getMultipole(idxRhs), ParentCell->getLeaf(i)->getSrc());
//			}
//		}
//	}
//	void M2MAdapt(CellClass* const FRestrict ParentCell, const int &TreeLevel, const int &numberOfM2M,
//			const int * FRestrict ChildLevel , const CellClass*const FRestrict *const FRestrict ChildCells)
//	{
//		for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){
//			//            // apply Sy
//			for (unsigned int ChildIndex=0; ChildIndex < 8; ++ChildIndex){
//				if (ChildCells[ChildIndex]){
//					//			KernelBaseClass::Interpolator->applyM2M(ChildIndex, ChildCells[ChildIndex]->getMultipole(idxRhs), ParentCell->getMultipole(idxRhs));
//				}
//			}
//		}
//	}
//
//
//
//	void M2L(CellClass* const FRestrict TargetCell,
//			const CellClass* SourceCells[343],
//			const int /*NumSourceCells*/,
//			const int TreeLevel)
//	{
//
//	}
//
//
//	void L2L(const CellClass* const FRestrict ParentCell,
//			CellClass* FRestrict *const FRestrict ChildCells,
//			const int /*TreeLevel*/)
//	{
//		//        for(int idxRhs = 0 ; idxRhs < NVALS ; ++idxRhs){
//		//            // apply Sx
//		//            for (unsigned int ChildIndex=0; ChildIndex < 8; ++ChildIndex){
//		//                if (ChildCells[ChildIndex]){
//		//                    AbstractBaseClass::Interpolator->applyL2L(ChildIndex, ParentCell->getLocal(idxRhs), ChildCells[ChildIndex]->getLocal(idxRhs));
//		//                }
//		//            }
//		//        }
//	}
//
//	void L2P(const CellClass* const LeafCell,
//			ContainerClass* const TargetParticles)
//	{
//		KernelBaseClass::L2P(LeafCell,TargetParticles) ;
//	}
//
//	//    void P2P(const FTreeCoordinate& /* LeafCellCoordinate */, // needed for periodic boundary conditions
//	//                     ContainerClass* const FRestrict TargetParticles,
//	//                     const ContainerClass* const FRestrict /*SourceParticles*/,
//	//                     ContainerClass* const NeighborSourceParticles[27],
//	//                     const int /* size */)
//	//    {
//	//        DirectInteractionComputer<MatrixKernelClass::Identifier, NVALS>::P2P(TargetParticles,NeighborSourceParticles);
//	//    }
//	//
//	//
//	//    void P2PRemote(const FTreeCoordinate& /*inPosition*/,
//	//                   ContainerClass* const FRestrict inTargets, const ContainerClass* const FRestrict /*inSources*/,
//	//                   ContainerClass* const inNeighbors[27], const int /*inSize*/){
//	//       DirectInteractionComputer<MatrixKernelClass::Identifier, NVALS>::P2PRemote(inTargets,inNeighbors,27);
//	//    }
//
//};
//
//
461 462 463 464 465 466 467 468 469






#endif //FADAPTCHEBSYMKERNELS_HPP

// [--END--]