FInterEngine.hpp 38.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// ===================================================================================
// Copyright ScalFmm 2014 I
// 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".
// ===================================================================================


/**
 * @file This file contains a class that inherits from FScalFMMEngine,
 * and will implement the API functions for Interpolations kernels.
 */

#ifndef FINTERENGINE_HPP
#define FINTERENGINE_HPP

#include "FScalFMMEngine.hpp"
26

27
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
28 29
#include "Components/FTypedLeaf.hpp"

30 31
#include "Arranger/FOctreeArranger.hpp"
#include "Arranger/FArrangerPeriodic.hpp"
32
#include "Arranger/FBasicParticleContainerIndexedMover.hpp"
33
#include "Arranger/FParticleTypedIndexedMover.hpp"
34

35 36
#include "Core/FFmmAlgorithmThread.hpp"
#include "Core/FFmmAlgorithm.hpp"
37
#include "Core/FFmmAlgorithmPeriodic.hpp"
38
#include "Core/FFmmAlgorithmThreadTsm.hpp"
39 40 41 42 43 44


/**
 * @class FInterEngine implements API for Interpolations kernels, its
 * templates can be ChebCell/ChebKernel or UnifCell/UnifKernel
 */
45
template<class FReal, class InterCell,class InterKernel,class LeafClass,
46
         class MatrixKernelClass = FInterpMatrixKernelR<FReal> >
47
class FInterEngine : public FScalFMMEngine<FReal>{
48
private:
49 50 51
    //Typedefs
    typedef FP2PParticleContainerIndexed<FReal>           ContainerClass;
    typedef FTypedLeaf<FReal,ContainerClass>                    LeafClassTyped;
52

53 54
    //Typedef on the Typed OctreeClass
    typedef FOctree<FReal,InterCell,ContainerClass,LeafClass>           OctreeClass;
55

56

57 58
    //Pointer to the kernel to be executed
    InterKernel * kernel;
59
    MatrixKernelClass * matrix;
60
    //Link to the tree
61
    OctreeClass * octree;
62 63 64

    // ArrangerClass * arranger;

65

66 67
public:
    /**
68 69
     * @brief Constructor : build the tree and the interpolation
     * kernel
70 71 72 73 74
     * @param TreeHeight Height of the tree
     * @param BoxWidth box Width
     * @param BoxCenter double[3] coordinate of the center of the
     * simulation box
     */
75
    FInterEngine(scalfmm_kernel_type KernelType) :
76 77
        kernel(nullptr), matrix(nullptr), octree(nullptr)/*,arranger(nullptr)*/{
        FScalFMMEngine<FReal>::kernelType = KernelType;
78 79
    }

80
    void build_tree(int TreeHeight, FReal BoxWidth , FReal * BoxCenter,User_Scalfmm_Cell_Descriptor notUsedHere){
81
        octree = new OctreeClass(TreeHeight,FMath::Min(3,TreeHeight-1),BoxWidth,FPoint<FReal>(BoxCenter));
82
        this->matrix = new MatrixKernelClass();
83
        this->kernel = new InterKernel(TreeHeight,BoxWidth,FPoint<FReal>(BoxCenter),matrix);
84 85
    }

86

87 88
    //TODO free kernel too
    ~FInterEngine(){
89
        delete matrix;
90 91 92 93 94
        if(octree){
            delete octree;
        }
        if(kernel){
            delete kernel;
95
        }
96 97 98
        // if(arranger){
        //     delete arranger;
        // }
99 100 101
    }

    //Inserting array of position
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    //Need to be disabled if Source/Target is used
    void tree_insert_particles_xyz(int NbPositions, FReal * XYZ, PartType type){
        if(type == BOTH){
            for(FSize idPart = 0; idPart<NbPositions ; ++idPart){
                octree->insert(FPoint<FReal>(&XYZ[3*idPart]),idPart);
            }
            FScalFMMEngine<FReal>::nbPart += NbPositions;
        }else{
            if(type==SOURCE){
                for(FSize idPart = 0; idPart<NbPositions ; ++idPart){
                    octree->insert(FPoint<FReal>(&XYZ[3*idPart]),FParticleTypeSource,idPart);
                }
                FScalFMMEngine<FReal>::nbPart += NbPositions;
            }else{
                for(FSize idPart = 0; idPart<NbPositions ; ++idPart){
                    octree->insert(FPoint<FReal>(&XYZ[3*idPart]),FParticleTypeTarget,idPart);
                }
                FScalFMMEngine<FReal>::nbPart += NbPositions;
            }
121 122 123 124
        }
    }

    //Inserting arrayS of position
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    //Need to be disabled if Source/Target is used
    void tree_insert_particles(int NbPositions, FReal * X, FReal * Y, FReal * Z, PartType type){
        if(type == BOTH){
            for(FSize idPart = 0; idPart<NbPositions ; ++idPart){
                octree->insert(FPoint<FReal>(X[idPart],Y[idPart],Z[idPart]),idPart);
            }
            FScalFMMEngine<FReal>::nbPart += NbPositions;
        }else{
            if(type==SOURCE){
                for(FSize idPart = 0; idPart<NbPositions ; ++idPart){
                    octree->insert(FPoint<FReal>(X[idPart],Y[idPart],Z[idPart]),FParticleTypeSource,idPart);
                }
                FScalFMMEngine<FReal>::nbPart += NbPositions;
            }else{
                for(FSize idPart = 0; idPart<NbPositions ; ++idPart){
                    octree->insert(FPoint<FReal>(X[idPart],Y[idPart],Z[idPart]),FParticleTypeTarget,idPart);
                }
                FScalFMMEngine<FReal>::nbPart += NbPositions;
            }
144 145 146
        }
    }

147 148 149 150 151 152 153 154 155
    // void tree_abstract_insert(int NbPartToInsert, int nbAttributeToInsert, int * strideForEachAtt,
    //                           FReal* rawDatas){
    //     FAssertLF(nbAttributeToInsert > 2,"Need space to store positions, thus nbAttributeToInsert must be >= 3\nExiting ... \n");
    //     FAssertLF(nbAttributeToInsert < 15,"Cannot instanciate more than 15 Attribute per Particules\n");
    //     FRunIf::Run<int,3,15,1,RunContainer>(nbAttributeToInsert,);
    //     generic_tree_abstract_insert<ContainerClass,LeafClass,InterCell,nbAttributeToInsert>(octree,
    //                                                                                          NbPartToInsert,strideForEachAtt,rawDatas);
    // }

156
    //Set the physical values
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 184
    void set_physical_values(int nbPhysicalValues,FReal * physicalValues, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            octree->forEachLeaf([&] (LeafClass * leaf){
                    ContainerClass * sources = leaf->getSrc();
                    const FVector<FSize>& indexes = sources->getIndexes();
                    FSize nbPartThere = sources->getNbParticles();
                    for(int idx=0 ; idx<nbPartThere ; ++idx){
                        sources->getPhysicalValues()[idx] = physicalValues[indexes[idx]];
                        ++checkCount;
                    }
                });
        }
        else{ // type must be equal to TARGETS or BOTH
            octree->forEachLeaf([&] (LeafClass * leaf){
                    ContainerClass * targets = leaf->getTargets();
                    const FVector<FSize>& indexes = targets->getIndexes();
                    FSize nbPartThere = targets->getNbParticles();
                    for(int idx=0 ; idx<nbPartThere ; ++idx){
                        targets->getPhysicalValues()[idx] = physicalValues[indexes[idx]];
                        ++checkCount;
                    }
                });
        }
        if(checkCount < nbPhysicalValues){std::cout << "Not all "<<nbPhysicalValues <<" parts has been set (only "<<checkCount<<")" << std::endl;}
        else{
            if(checkCount > nbPhysicalValues){std::cout << "More parts than  "<<nbPhysicalValues <<" has been set"<< std::endl;}
        }
185 186 187 188 189
    }

    //Set only a subpart of physical values
    //Algorithm : loop over each leaf, and then search in user array
    //if any index matches
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    void set_physical_values_npart( int nbPhysicalValues, int* idxOfParticles, FReal * physicalValues, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * sources = leaf->getSrc();
                    const FVector<FSize>& indexes = sources->getIndexes();
                    FSize nbPartThere = sources->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbPhysicalValues && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                sources->getPhysicalValues()[idxPart] = physicalValues[iterPart];
                                checkCount++;
                                notFoundYet = false;
                            }
                            else{
                                ++iterPart;
                            }
209
                        }
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                    }
                });
        }else{//Parts are target
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * targets = leaf->getTargets();
                    const FVector<FSize>& indexes = targets->getIndexes();
                    FSize nbPartTarget = targets->getNbParticles();
                    //Targets part
                    for(FSize idxPart = 0 ; idxPart<nbPartTarget ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbPhysicalValues && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                targets->getPhysicalValues()[idxPart] = physicalValues[iterPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
230 231
                        }
                    }
232 233 234 235 236 237
                });
        }
        if(checkCount < nbPhysicalValues){std::cout << "Not all "<<nbPhysicalValues <<" parts has been set"<< std::endl;}
        else{
            if(checkCount > nbPhysicalValues){std::cout << "More parts than  "<<nbPhysicalValues <<" has been set"<< std::endl;}
        }
238 239
    }

240

241
    //get back the physical values
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    void get_physical_values( int nbPhysicalValues, FReal * physicalValues, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * sources = leaf->getSrc();
                    const FVector<FSize>& indexes = sources->getIndexes();
                    FSize nbPartThere = sources->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        physicalValues[indexes[idxPart]] = sources->getPhysicalValues()[idxPart];
                        checkCount++;
                    }
                });
        }
        else{//Get the targets forces
            octree->forEachLeaf([&](LeafClass* leaf){
                        ContainerClass * targets = leaf->getTargets();
                        const FVector<FSize>& indexes = targets->getIndexes();
                        FSize nbPartThere = targets->getNbParticles();
                        for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                            physicalValues[indexes[idxPart]] = targets->getPhysicalValues()[idxPart];
                            checkCount++;
                        }
                    });
        }
        if(checkCount < nbPhysicalValues){std::cout << "Not all "<<nbPhysicalValues <<" parts has been read"<< std::endl;}
        else{
            if(checkCount > nbPhysicalValues){std::cout << "More parts than  "<<nbPhysicalValues <<" has been read"<< std::endl;}
        }
270 271
    }

272

273
    //Same algorithm as in set_physical_values_npart
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    void get_physical_values_npart( int nbPhysicalValues, int* idxOfParticles, FReal * physicalValues, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * sources = leaf->getSrc();
                    const FVector<FSize>& indexes = sources->getIndexes();
                    FSize nbPartThere = sources->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbPhysicalValues && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                physicalValues[iterPart] = sources->getPhysicalValues()[idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
293
                        }
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
                    }
                });
        }else{ //Target
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * targets = leaf->getTargets();
                    const FVector<FSize>& indexes = targets->getIndexes();
                    FSize nbPartThere = targets->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbPhysicalValues && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                physicalValues[iterPart] = targets->getPhysicalValues()[idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
313 314
                        }
                    }
315 316 317 318 319 320
                });
        }
        if(checkCount < nbPhysicalValues){std::cout << "Not all "<<nbPhysicalValues <<" parts has been read"<< std::endl;}
        else{
            if(checkCount > nbPhysicalValues){std::cout << "More parts than  "<<nbPhysicalValues <<" has been read"<< std::endl;}
        }
321 322
    }

323 324
    void get_forces_xyz( int nbParts, FReal * forcesToFill, PartType type){
        FScalFMMEngine<FReal>::template generic_get_forces_xyz<ContainerClass,LeafClass,InterCell>(octree,nbParts,forcesToFill,type);
325 326
    }

327 328
    void get_forces(int nbParts, FReal * fX, FReal* fY, FReal* fZ, PartType type){
        FScalFMMEngine<FReal>::template generic_get_forces<ContainerClass,LeafClass,InterCell>(octree,nbParts,fX,fY,fZ,type);
329 330
    }

331 332
    void get_forces_nbpart(int nbParts, int* idxOfParticles ,FReal * fX, FReal* fY, FReal* fZ, PartType type){
        FScalFMMEngine<FReal>::template generic_get_forces_xyz_npart<ContainerClass,LeafClass,InterCell>(octree,nbParts,idxOfParticles,fX,fY,fZ,type);
333
    }
334

335 336
    void get_forces_xyz_nbpart(int nbParts, int* idxOfParticles, FReal * forcesToFill, PartType type){
        FScalFMMEngine<FReal>::template generic_get_forces_xyz_npart<ContainerClass,LeafClass,InterCell>(octree,nbParts,idxOfParticles,forcesToFill,type);
337 338
    }

339

340
    //To set initial condition
341 342 343 344
    void set_forces_xyz( int nbParts, FReal * forcesToRead, PartType type){
        int checkCount = 0;
        if(type == SOURCE || type==BOTH){
            octree->forEachLeaf([&](LeafClass* leaf){
345
                ContainerClass * sources = leaf->getSrc();
346
                const FVector<FSize>& indexes = sources->getIndexes();
347
                FSize nbPartThere = sources->getNbParticles();
348
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
349 350 351
                    sources->getForcesX()[idxPart] = forcesToRead[indexes[idxPart]*3+0];
                    sources->getForcesY()[idxPart] = forcesToRead[indexes[idxPart]*3+1];
                    sources->getForcesZ()[idxPart] = forcesToRead[indexes[idxPart]*3+2];
352 353 354 355 356 357 358 359 360 361 362 363 364 365
                    checkCount++;
                }
            });
        }
        else{//Set force on target
            octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * targets = leaf->getTargets();
                const FVector<FSize>& indexes = targets->getIndexes();
                FSize nbPartThere = targets->getNbParticles();
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                    targets->getForcesX()[idxPart] = forcesToRead[indexes[idxPart]*3+0];
                    targets->getForcesY()[idxPart] = forcesToRead[indexes[idxPart]*3+1];
                    targets->getForcesZ()[idxPart] = forcesToRead[indexes[idxPart]*3+2];
                    checkCount++;
366 367
                }
            });
368 369 370 371 372
        }
        if(checkCount < nbParts){std::cout << "Not all "<<nbParts <<" forces has been read"<< std::endl;}
        else{
            if(checkCount > nbParts){std::cout << "More parts than  "<<nbParts <<" forces has been read"<< std::endl;}
        }
373
    }
374 375 376 377 378

    void set_forces_xyz_npart( int nbParts, int* idxOfParticles, FReal * forcesToRead, PartType type){
        int checkCount = 0;
        if(type == SOURCE || type==BOTH){
            octree->forEachLeaf([&](LeafClass* leaf){
379
                ContainerClass * sources = leaf->getSrc();
380
                const FVector<FSize>& indexes = sources->getIndexes();
381
                FSize nbPartThere = sources->getNbParticles();
382
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
383 384 385 386
                    int iterPart = 0;
                    bool notFoundYet = true;
                    while(iterPart < nbParts && notFoundYet){
                        if(indexes[idxPart] == idxOfParticles[iterPart]){
387 388 389
                            sources->getForcesX()[idxPart] = forcesToRead[iterPart];
                            sources->getForcesY()[idxPart] = forcesToRead[iterPart];
                            sources->getForcesZ()[idxPart] = forcesToRead[iterPart];
390
                            notFoundYet = false;
391
                            checkCount++;
392 393 394 395 396 397 398
                        }
                        else{
                            ++iterPart;
                        }
                    }
                }
            });
399 400 401 402 403
        }else{ //Targets
            octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * targets = leaf->getTargets();
                const FVector<FSize>& indexes = targets->getIndexes();
                FSize nbPartThere = targets->getNbParticles();
404
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
405 406 407 408
                    int iterPart = 0;
                    bool notFoundYet = true;
                    while(iterPart < nbParts && notFoundYet){
                        if(indexes[idxPart] == idxOfParticles[iterPart]){
409 410 411
                            targets->getForcesX()[idxPart] = forcesToRead[iterPart];
                            targets->getForcesY()[idxPart] = forcesToRead[iterPart];
                            targets->getForcesZ()[idxPart] = forcesToRead[iterPart];
412
                            notFoundYet = false;
413
                            checkCount++;
414 415 416 417 418 419 420
                        }
                        else{
                            ++iterPart;
                        }
                    }
                }
            });
421 422 423 424 425
        }
        if(checkCount < nbParts){std::cout << "Not all "<<nbParts <<" forces has been read"<< std::endl;}
        else{
            if(checkCount > nbParts){std::cout << "More parts than  "<<nbParts <<" forces has been read"<< std::endl;}
        }
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
    void set_forces( int nbParts, FReal * fX, FReal* fY, FReal* fZ, PartType type){
        int checkCount = 0;
        if(type == SOURCE || type==BOTH){
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * sources = leaf->getSrc();
                    const FVector<FSize>& indexes = sources->getIndexes();
                    FSize nbPartThere = sources->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        sources->getForcesX()[idxPart] = fX[indexes[idxPart]];
                        sources->getForcesY()[idxPart] = fY[indexes[idxPart]];
                        sources->getForcesZ()[idxPart] = fZ[indexes[idxPart]];
                        checkCount++;
                    }
                });
        }else{//Targets
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * targets = leaf->getTargets();
                    const FVector<FSize>& indexes = targets->getIndexes();
                    FSize nbPartThere = targets->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        targets->getForcesX()[idxPart] = fX[indexes[idxPart]];
                        targets->getForcesY()[idxPart] = fY[indexes[idxPart]];
                        targets->getForcesZ()[idxPart] = fZ[indexes[idxPart]];
                        checkCount++;
                    }
                });
        }
        if(checkCount < nbParts){std::cout << "Not all "<<nbParts <<" forces has been read"<< std::endl;}
        else{
            if(checkCount > nbParts){std::cout << "More parts than  "<<nbParts <<" forces has been read"<< std::endl;}
        }
459 460
    }

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
    void set_forces_npart( int nbParts, int* idxOfParticles, FReal * fX, FReal* fY, FReal* fZ, PartType type){
        int checkCount = 0;
        if(type == SOURCE || type==BOTH){
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * sources = leaf->getSrc();
                    const FVector<FSize>& indexes = sources->getIndexes();
                    FSize nbPartThere = sources->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbParts && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                sources->getForcesX()[idxPart] = fX[indexes[idxPart]];
                                sources->getForcesY()[idxPart] = fY[indexes[idxPart]];
                                sources->getForcesZ()[idxPart] = fZ[indexes[idxPart]];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
482
                        }
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
                    }
                });
        }else{//Targets
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * targets = leaf->getTargets();
                    const FVector<FSize>& indexes = targets->getIndexes();
                    FSize nbPartThere = targets->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbParts && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                targets->getForcesX()[idxPart] = fX[indexes[idxPart]];
                                targets->getForcesY()[idxPart] = fY[indexes[idxPart]];
                                targets->getForcesZ()[idxPart] = fZ[indexes[idxPart]];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
504 505
                        }
                    }
506 507 508 509 510 511
                });
        }
        if(checkCount < nbParts){std::cout << "Not all "<<nbParts <<" forces has been read"<< std::endl;}
        else{
            if(checkCount > nbParts){std::cout << "More parts than  "<<nbParts <<" forces has been read"<< std::endl;}
        }
512 513
    }

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547


    /**
     *  Position related methods
     */
    void get_positions_xyz(int NbPositions, double * positionsToFill, PartType type){
        FScalFMMEngine<FReal>::template generic_get_positions_xyz<ContainerClass,LeafClass,InterCell>(octree,NbPositions,positionsToFill,type);
    }
    void get_positions_xyz_npart(int NbPositions, int * idxOfParticles, double * positionsToFill,PartType type){
        FScalFMMEngine<FReal>::template generic_get_positions_xyz_npart<ContainerClass,LeafClass,InterCell>(octree,NbPositions,idxOfParticles,positionsToFill,type);
    }
    void get_positions( int NbPositions, double *X, double *Y , double *Z, PartType type){
        FScalFMMEngine<FReal>::template generic_get_positions<ContainerClass,LeafClass,InterCell>(octree,NbPositions,X,Y,Z,type);
    }
    void get_positions_npart(int NbPositions, int * idxOfParticles,double * X, double * Y , double * Z,PartType type){
        FScalFMMEngine<FReal>::template generic_get_positions_npart<ContainerClass,LeafClass,InterCell>(octree,NbPositions,idxOfParticles,X,Y,Z,type);
    }
    void set_positions_xyz(int NbPositions, FReal * updatedXYZ, PartType type){
        FScalFMMEngine<FReal>::template generic_set_positions_xyz<ContainerClass,LeafClass,InterCell>(octree,NbPositions,updatedXYZ,type);
    }
    void set_positions(int NbPositions, FReal * X, FReal * Y, FReal * Z, PartType type){
        FScalFMMEngine<FReal>::template generic_set_positions<ContainerClass,LeafClass,InterCell>(octree,NbPositions,X,Y,Z,type);
    }
    void set_positions_xyz_npart(int NbPositions, int* idxOfParticles, FReal * updatedXYZ, PartType type){
        FScalFMMEngine<FReal>::template generic_set_positions_xyz_npart<ContainerClass,LeafClass,InterCell>(octree,NbPositions,idxOfParticles,updatedXYZ,type);
    }
    void set_positions_npart(int NbPositions, int* idxOfParticles, FReal * X, FReal * Y , FReal * Z, PartType type){
        FScalFMMEngine<FReal>::template generic_set_positions_npart<ContainerClass,LeafClass,InterCell>(octree,NbPositions,idxOfParticles,X,Y,Z,type);
    }
    void add_to_positions_xyz(int NbPositions,FReal * updatedXYZ,PartType type){
        FScalFMMEngine<FReal>::template generic_add_to_positions_xyz<ContainerClass,LeafClass,InterCell>(octree,NbPositions,updatedXYZ,type);
    }
    void add_to_positions(int NbPositions,FReal * X, FReal * Y , FReal * Z, PartType type){
        FScalFMMEngine<FReal>::template generic_add_to_positions<ContainerClass,LeafClass,InterCell>(octree,NbPositions,X,Y,Z,type);
548 549
    }

550 551 552 553 554
    //Set the potentials
    void set_potentials(int nbPotentials,FReal * potentialsToRead, PartType type){
        int checkCount = 0;
        if(type == SOURCE || type==BOTH){
            octree->forEachLeaf([&](LeafClass* leaf){
555
                ContainerClass * sources = leaf->getSrc();
556
                const FVector<FSize>& indexes = sources->getIndexes();
557
                FSize nbPartThere = sources->getNbParticles();
558
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
559 560
                    sources->getPotentials()[idxPart] = potentialsToRead[indexes[idxPart]];
                    checkCount++;
561 562
                }
            });
563 564 565 566 567
        }else{//Targets
            octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * targets = leaf->getTargets();
                const FVector<FSize>& indexes = targets->getIndexes();
                FSize nbPartThere = targets->getNbParticles();
568
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
569 570
                    targets->getPotentials()[idxPart] = potentialsToRead[indexes[idxPart]];
                    checkCount++;
571 572
                }
            });
573 574 575 576 577
        }
        if(checkCount < nbPotentials){std::cout << "Not all "<<nbPotentials <<" forces has been read"<< std::endl;}
        else{
            if(checkCount > nbPotentials){std::cout << "More parts than  "<<nbPotentials <<" forces has been read"<< std::endl;}
        }
578 579
    }

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
    //Set only a subpart of potentials
    //Algorithm : loop over each leaf, and then search in user array
    //if any index matches
    void set_potentials_npart( int nbPotentials, int* idxOfParticles, FReal * potentialsToRead, PartType type){
        int checkCount = 0;
        if(type == SOURCE || type==BOTH){
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * sources = leaf->getSrc();
                    const FVector<FSize>& indexes = sources->getIndexes();
                    FSize nbPartThere = sources->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbPotentials && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                sources->getPotentials()[idxPart] = potentialsToRead[iterPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
602
                        }
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
                    }
                });
        }else{//Targets
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * targets = leaf->getTargets();
                    const FVector<FSize>& indexes = targets->getIndexes();
                    FSize nbPartThere = targets->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbPotentials && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                targets->getPotentials()[idxPart] = potentialsToRead[iterPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
622 623
                        }
                    }
624 625 626 627 628 629
                });
        }
        if(checkCount < nbPotentials){std::cout << "Not all "<<nbPotentials <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > nbPotentials){std::cout << "More parts than  "<<nbPotentials <<" potentials has been read"<< std::endl;}
        }
630 631
    }

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
    //get back the potentials
    void get_potentials( int nbPotentials, FReal * potentialsToFill, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * sources = leaf->getSrc();
                    FSize nbPartThere = sources->getNbParticles();
                    const FVector<FSize>& indexes = sources->getIndexes();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        potentialsToFill[indexes[idxPart]] = sources->getPotentials()[idxPart];
                        checkCount++;
                    }
                });
        }else{//Targets
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * targets = leaf->getTargets();
                    const FVector<FSize>& indexes = targets->getIndexes();
                    FSize nbPartThere = targets->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        potentialsToFill[indexes[idxPart]] = targets->getPotentials()[idxPart];
                        checkCount++;
                    }
                });
        }
        if(checkCount < nbPotentials){std::cout << "Not all "<<nbPotentials <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > nbPotentials){std::cout << "More parts than  "<<nbPotentials <<" potentials has been read"<< std::endl;}
        }
660 661
    }

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    //Same algorithm as in set_potentials_npart
    void get_potentials_npart( int nbPotentials, int* idxOfParticles, FReal * potentialsToFill, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * sources = leaf->getSrc();
                    const FVector<FSize>& indexes = sources->getIndexes();
                    FSize nbPartThere = sources->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbPotentials && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                potentialsToFill[indexes[idxPart]] = sources->getPotentials()[idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
682
                        }
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
                    }
                });
        }else{
            octree->forEachLeaf([&](LeafClass* leaf){
                    ContainerClass * targets = leaf->getTargets();
                    const FVector<FSize>& indexes = targets->getIndexes();
                    FSize nbPartThere = targets->getNbParticles();
                    for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
                        int iterPart = 0;
                        bool notFoundYet = true;
                        while(iterPart < nbPotentials && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                potentialsToFill[indexes[idxPart]] = targets->getPotentials()[idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
702 703
                        }
                    }
704 705 706 707 708 709
                });
        }
        if(checkCount < nbPotentials){std::cout << "Not all "<<nbPotentials <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > nbPotentials){std::cout << "More parts than  "<<nbPotentials <<" potentials has been read"<< std::endl;}
        }
710 711
    }

712
    //Simple call to FScalFMMEngine method with good template
713
    void reset_tree(Callback_reset_cell /*not used*/){
714
        FScalFMMEngine<FReal>::template generic_reset_tree<ContainerClass,InterCell,LeafClass>(octree);
715 716
    }

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

    // void update_tree(){
    //     if(arranger){
    //         arranger->rearrange();
    //     }
    //     else{
    //         if(FScalFMMEngine<FReal>::Algorithm == 2){ //case in wich the periodic algorithm is used
    //             arranger = new ArrangerClassPeriodic(octree);
    //             arranger->rearrange();
    //         }
    //         else{
    //             arranger = new ArrangerClass(octree);
    //             arranger->rearrange();
    //         }
    //     }
    // }
733 734 735


    void execute_fmm(){
736
        switch(FScalFMMEngine<FReal>::Algorithm){
737 738 739
        case 0:
            {
                typedef FFmmAlgorithm<OctreeClass,InterCell,ContainerClass,InterKernel,LeafClass> AlgoClassSeq;
740 741 742
                AlgoClassSeq* algoSeq = new AlgoClassSeq(octree,kernel);
                algoSeq->execute();
                FScalFMMEngine<FReal>::algoTimer = algoSeq;
743 744 745 746 747
                break;
            }
        case 1:
            {
                typedef FFmmAlgorithmThread<OctreeClass,InterCell,ContainerClass,InterKernel,LeafClass> AlgoClassThread;
748 749 750
                AlgoClassThread* algoThread = new AlgoClassThread(octree,kernel);
                algoThread->execute();
                FScalFMMEngine<FReal>::algoTimer = algoThread;
751 752
                break;
            }
753 754
        case 2:
            {
755
                typedef FFmmAlgorithmPeriodic<FReal,OctreeClass,InterCell,ContainerClass,InterKernel,LeafClass> AlgoClassPeriodic;
756
                AlgoClassPeriodic algoPeriod(octree,2);
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
757
                algoPeriod.setKernel(kernel);
758 759 760
                algoPeriod.execute();
                break;
            }
761 762 763 764 765 766 767 768
        case 3:
            {
                // typedef FFmmAlgorithmThreadTsm<OctreeClass,InterCell,ContainerClass,InterKernel,LeafClass> AlgoClassTargetSource;
                // AlgoClassTargetSource* algoTS = new AlgoClassTargetSource(octree,kernel);
                // algoTS->execute();
                // FScalFMMEngine<FReal>::algoTimer = algoTS;
                // break;
            }
769
        default :
770
            std::cout<< "No algorithm found (probably for strange reasons) : "<< FScalFMMEngine<FReal>::Algorithm <<" exiting" << std::endl;
771 772 773
        }
    }

774 775 776
    void intern_dealloc_handle(Callback_free_cell unUsed){
        //this->~FInterEngine();
    }
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

    void print_everything(){
        octree->forEachLeaf([&](LeafClass * leaf){
                ContainerClass * sources = leaf->getSrc();
                ContainerClass * targets = leaf->getTargets();
                const FVector<FSize>& indexesSources = sources->getIndexes();
                const FVector<FSize>& indexesTargets = targets->getIndexes();
                FSize nbPartSource = sources->getNbParticles();
                FSize nbPartTarget = targets->getNbParticles();
                for(int i=0 ; i<nbPartSource ; ++i){
                    printf("Src : Leaf : %p Part : %lld/%lld, pos: %e,%e,%e phy: %e, forces: %e,%e,%e pot %e\n",
                           leaf,indexesSources[i],nbPartSource,
                           sources->getPositions()[0][i],sources->getPositions()[1][i],sources->getPositions()[2][i],
                           sources->getPhysicalValues()[i],
                           sources->getForcesX()[i],sources->getForcesY()[i],sources->getForcesZ()[i],
                           sources->getPotentials()[i]);
                }
                for(int i=0 ; i<nbPartTarget ; ++i){
                    printf("Tgt : Leaf : %p Part : %lld/%lld, pos %e,%e,%e phy: %e, forces: %e,%e,%e pot %e\n",
                           leaf,indexesTargets[i],nbPartTarget,
                           targets->getPositions()[0][i],targets->getPositions()[1][i],targets->getPositions()[2][i],
                           targets->getPhysicalValues()[i],
                           targets->getForcesX()[i],targets->getForcesY()[i],targets->getForcesZ()[i],
                           targets->getPotentials()[i]);
                }

            });
    }
805 806 807 808
};


#endif