FInterEngine.hpp 38.4 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
#include "Extensions/FExtendCellType.hpp"
35

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


42

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

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

58

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

    // ArrangerClass * arranger;

67

68

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

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

90

91 92
    //TODO free kernel too
    ~FInterEngine(){
93
        delete matrix;
94 95 96 97 98
        if(octree){
            delete octree;
        }
        if(kernel){
            delete kernel;
99
        }
100 101 102
        // if(arranger){
        //     delete arranger;
        // }
103 104 105
    }

    //Inserting array of position
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    //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;
            }
125 126 127 128
        }
    }

    //Inserting arrayS of position
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    //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;
            }
148 149 150
        }
    }

151 152 153 154 155 156 157 158 159
    // 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);
    // }

160
    //Set the physical values
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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;}
        }
189 190 191 192 193
    }

    //Set only a subpart of physical values
    //Algorithm : loop over each leaf, and then search in user array
    //if any index matches
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    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;
                            }
213
                        }
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
                    }
                });
        }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;
                            }
234 235
                        }
                    }
236 237 238 239 240 241
                });
        }
        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;}
        }
242 243
    }

244

245
    //get back the physical values
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    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;}
        }
274 275
    }

276

277
    //Same algorithm as in set_physical_values_npart
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    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;
                            }
297
                        }
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
                    }
                });
        }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;
                            }
317 318
                        }
                    }
319 320 321 322 323 324
                });
        }
        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;}
        }
325 326
    }

327 328
    void get_forces_xyz( int nbParts, FReal * forcesToFill, PartType type){
        FScalFMMEngine<FReal>::template generic_get_forces_xyz<ContainerClass,LeafClass,InterCell>(octree,nbParts,forcesToFill,type);
329 330
    }

331 332
    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);
333 334
    }

335 336
    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);
337
    }
338

339 340
    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);
341 342
    }

343

344
    //To set initial condition
345 346 347 348
    void set_forces_xyz( int nbParts, FReal * forcesToRead, PartType type){
        int checkCount = 0;
        if(type == SOURCE || type==BOTH){
            octree->forEachLeaf([&](LeafClass* leaf){
349
                ContainerClass * sources = leaf->getSrc();
350
                const FVector<FSize>& indexes = sources->getIndexes();
351
                FSize nbPartThere = sources->getNbParticles();
352
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
353 354 355
                    sources->getForcesX()[idxPart] = forcesToRead[indexes[idxPart]*3+0];
                    sources->getForcesY()[idxPart] = forcesToRead[indexes[idxPart]*3+1];
                    sources->getForcesZ()[idxPart] = forcesToRead[indexes[idxPart]*3+2];
356 357 358 359 360 361 362 363 364 365 366 367 368 369
                    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++;
370 371
                }
            });
372 373 374 375 376
        }
        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;}
        }
377
    }
378 379 380 381 382

    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){
383
                ContainerClass * sources = leaf->getSrc();
384
                const FVector<FSize>& indexes = sources->getIndexes();
385
                FSize nbPartThere = sources->getNbParticles();
386
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
387 388 389 390
                    int iterPart = 0;
                    bool notFoundYet = true;
                    while(iterPart < nbParts && notFoundYet){
                        if(indexes[idxPart] == idxOfParticles[iterPart]){
391 392 393
                            sources->getForcesX()[idxPart] = forcesToRead[iterPart];
                            sources->getForcesY()[idxPart] = forcesToRead[iterPart];
                            sources->getForcesZ()[idxPart] = forcesToRead[iterPart];
394
                            notFoundYet = false;
395
                            checkCount++;
396 397 398 399 400 401 402
                        }
                        else{
                            ++iterPart;
                        }
                    }
                }
            });
403 404 405 406 407
        }else{ //Targets
            octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * targets = leaf->getTargets();
                const FVector<FSize>& indexes = targets->getIndexes();
                FSize nbPartThere = targets->getNbParticles();
408
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
409 410 411 412
                    int iterPart = 0;
                    bool notFoundYet = true;
                    while(iterPart < nbParts && notFoundYet){
                        if(indexes[idxPart] == idxOfParticles[iterPart]){
413 414 415
                            targets->getForcesX()[idxPart] = forcesToRead[iterPart];
                            targets->getForcesY()[idxPart] = forcesToRead[iterPart];
                            targets->getForcesZ()[idxPart] = forcesToRead[iterPart];
416
                            notFoundYet = false;
417
                            checkCount++;
418 419 420 421 422 423 424
                        }
                        else{
                            ++iterPart;
                        }
                    }
                }
            });
425 426 427 428 429
        }
        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;}
        }
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 461 462
    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;}
        }
463 464
    }

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    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;
                            }
486
                        }
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
                    }
                });
        }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;
                            }
508 509
                        }
                    }
510 511 512 513 514 515
                });
        }
        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;}
        }
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 548 549 550 551


    /**
     *  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);
552 553
    }

554 555 556 557 558
    //Set the potentials
    void set_potentials(int nbPotentials,FReal * potentialsToRead, PartType type){
        int checkCount = 0;
        if(type == SOURCE || type==BOTH){
            octree->forEachLeaf([&](LeafClass* leaf){
559
                ContainerClass * sources = leaf->getSrc();
560
                const FVector<FSize>& indexes = sources->getIndexes();
561
                FSize nbPartThere = sources->getNbParticles();
562
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
563 564
                    sources->getPotentials()[idxPart] = potentialsToRead[indexes[idxPart]];
                    checkCount++;
565 566
                }
            });
567 568 569 570 571
        }else{//Targets
            octree->forEachLeaf([&](LeafClass* leaf){
                ContainerClass * targets = leaf->getTargets();
                const FVector<FSize>& indexes = targets->getIndexes();
                FSize nbPartThere = targets->getNbParticles();
572
                for(FSize idxPart = 0 ; idxPart<nbPartThere ; ++idxPart){
573 574
                    targets->getPotentials()[idxPart] = potentialsToRead[indexes[idxPart]];
                    checkCount++;
575 576
                }
            });
577 578 579 580 581
        }
        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;}
        }
582 583
    }

584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    //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;
                            }
606
                        }
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
                    }
                });
        }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;
                            }
626 627
                        }
                    }
628 629 630 631 632 633
                });
        }
        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;}
        }
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 660 661 662 663
    //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;}
        }
664 665
    }

666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    //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;
                            }
686
                        }
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
                    }
                });
        }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;
                            }
706 707
                        }
                    }
708 709 710 711 712 713
                });
        }
        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;}
        }
714 715
    }

716
    //Simple call to FScalFMMEngine method with good template
717
    void reset_tree(Callback_reset_cell /*not used*/){
718
        FScalFMMEngine<FReal>::template generic_reset_tree<ContainerClass,InterCell,LeafClass>(octree);
719 720
    }

721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736

    // 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();
    //         }
    //     }
    // }
737 738 739


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

780 781 782
    void intern_dealloc_handle(Callback_free_cell unUsed){
        //this->~FInterEngine();
    }
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810

    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]);
                }

            });
    }
811 812 813 814
};


#endif