FScalFMMEngine.hpp 52.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// ===================================================================================
// 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 contain a class, gathering all the function that
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
19 20
 * can be called by the ScalFMM API. Documentation for each function
 * can be found in the C Header.
21 22 23
 *
 */

24 25
#ifndef FSCALFMMENGINE_HPP
#define FSCALFMMENGINE_HPP
26 27


28 29 30 31
#include "Utils/FAssert.hpp"

//For tree
#include "Components/FSimpleLeaf.hpp"
32

33 34
#include "Kernels/P2P/FP2PParticleContainerIndexed.hpp"

35
//For interpolation
36
#include "Kernels/Interpolation/FInterpMatrixKernel.hpp"
37 38 39 40

//For lagrange interpolation
// #include "Kernels/Uniform/FUnifCell.hpp"
// #include "Kernels/Uniform/FUnifKernel.hpp"
41 42 43 44

//For chebyshev Interpolation
#include "Kernels/Chebyshev/FChebCell.hpp"
#include "Kernels/Chebyshev/FChebSymKernel.hpp"
45 46 47 48 49
#include "Utils/FAlgorithmTimers.hpp"
#include "Components/FParticleType.hpp"
#include "Components/FTypedLeaf.hpp"
#include "Containers/FOctree.hpp"
#include "Utils/FTemplate.hpp"
50
#include "Core/FCoreCommon.hpp"
51 52 53 54

/**
 * @class FScalFMMEngine
 */
55
template<class FReal>
56
class FScalFMMEngine{
57

58 59
protected:
    scalfmm_kernel_type kernelType;
60

61
    scalfmm_algorithm Algorithm;
62
    FVector<bool>* progress;
63
    int nbPart;
64
    FAlgorithmTimers * algoTimer;
65
    FAbstractAlgorithm * abstrct;
66
public:
67

68
    FScalFMMEngine() : Algorithm(multi_thread), progress(nullptr), nbPart(0), algoTimer(nullptr), abstrct(nullptr){
69 70 71 72
        progress = new FVector<bool>();
    }

    virtual ~FScalFMMEngine() {
73 74 75 76 77
        //Do not delete algoTimer because abstract and algoTimer are two pointers on the same thing
        if(abstrct){
            delete abstrct;
            abstrct = nullptr;
            algoTimer = nullptr;
78
        }
79
        delete progress;
80 81 82 83
    }

    //First function displayed there are common function for every
    //kernel
84
    scalfmm_kernel_type getKernelType(){
85 86 87 88 89 90 91 92 93 94 95
        return this->kernelType;
    }

    //To change default algorithm
    void algorithm_config(scalfmm_algorithm config){
        this->Algorithm = config;
    }


    //Functions displayed there are function that are to be redefined
    //by specific Engine
96

97
    //Function about the tree
98 99 100
    virtual void build_tree(int TreeHeight,FReal BoxWidth,FReal* BoxCenter,
                            Scalfmm_Cell_Descriptor user_cell_descriptor,
                            Scalfmm_Leaf_Descriptor user_leaf_descriptor){
101 102 103
        FAssertLF(0,"Nothing has been done yet, exiting");
    }

104
    virtual void tree_insert_particles( int NbPositions, FReal * arrayX, FReal * arrayY, FReal * arrayZ, PartType type){
105
        FAssertLF(0,"No tree instancied, exiting ...\n");
106 107
    }

108
    virtual void tree_insert_particles_xyz( int NbPositions, FReal * XYZ, PartType type){
109
        FAssertLF(0,"No tree instancied, exiting ...\n");
110 111
    }

112
    virtual void set_physical_values( int nbPhysicalValues, FReal * physicalValues, PartType type){
113
        FAssertLF(0,"No tree instancied, exiting ...\n");
114 115
    }

116
    virtual void get_physical_values( int nbPhysicalValues, FReal * physicalValues, PartType type){
117
        FAssertLF(0,"No tree instancied, exiting ...\n");
118 119 120
    }

    virtual void set_physical_values_npart( int nbPhysicalValues,
121
                                            int* idxOfParticles, FReal * physicalValues, PartType type){
122
        FAssertLF(0,"No tree instancied, exiting ...\n");
123 124
    }
    virtual void get_physical_values_npart( int nbPhysicalValues,
125
                                            int* idxOfParticles, FReal * physicalValues, PartType type){
126
        FAssertLF(0,"No tree instancied, exiting ...\n");
127 128
    }

129
    virtual void get_forces_xyz( int nbParts, FReal * forcesToFill, PartType type){
130
    }
131 132

    virtual void get_forces(int nbParts, FReal * fX, FReal* fY, FReal* fZ, PartType type){
133
    }
134 135

    virtual void get_forces_npart(int nbParts, int* idxOfParticles ,FReal * fX, FReal* fY, FReal* fZ, PartType type){
136
    }
137 138 139 140 141 142 143 144

    virtual void get_forces_xyz_npart(int nbParts, int* idxOfParticles, FReal * forcesToFill, PartType type){
    }

    virtual void add_to_positions_xyz(int NbPositions,FReal * updatedXYZ,PartType type){
    }

    virtual void add_to_positions(int NbPositions,FReal * X, FReal * Y , FReal * Z, PartType type){
145
    }
146

147 148 149 150 151
    virtual void tree_abstract_insert(int NbPartToInsert, int nbAttributeToInsert, int * strideForEachAtt,
                                      FReal* rawDatas){
    }


152
    //To set initial condition
153
    virtual void set_forces_xyz( int nbParts, FReal * forcesToFill, PartType type){
154
        FAssertLF(0,"No tree instancied, exiting ...\n");
155
    }
156
    virtual void set_forces_xyz_npart( int nbParts, int* idxOfParticles, FReal * forcesToFill, PartType type){
157
        FAssertLF(0,"No tree instancied, exiting ...\n");
158
    }
159
    virtual void set_forces( int nbParts, FReal * fX, FReal* fY, FReal* fZ, PartType type){
160
        FAssertLF(0,"No tree instancied, exiting ...\n");
161
    }
162
    virtual void set_forces_npart( int nbParts, int* idxOfParticles, FReal * fX, FReal* fY, FReal* fZ, PartType type){
163
        FAssertLF(0,"No tree instancied, exiting ...\n");
164 165
    }
    //To deal with potential
166
    virtual void get_potentials( int nbParts, FReal * potentialsToFill, PartType type){
167
        FAssertLF(0,"No tree instancied, exiting ...\n");
168
    }
169
    virtual void set_potentials( int nbParts, FReal * potentialsToRead, PartType type){
170
        FAssertLF(0,"No tree instancied, exiting ...\n");
171
    }
172
    virtual void get_potentials_npart( int nbParts, int* idxOfParticles, FReal * potentialsToFill, PartType type){
173
        FAssertLF(0,"No tree instancied, exiting ...\n");
174
    }
175
    virtual void set_potentials_npart( int nbParts, int* idxOfParticles, FReal * potentialsToRead, PartType type){
176
        FAssertLF(0,"No tree instancied, exiting ...\n");
177
    }
178
    virtual void apply_on_each_leaf(Callback_apply_on_leaf function){
179 180
        FAssertLF(0,"No tree instancied, exiting ...\n");
    }
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207



    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_get_forces_xyz(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
                                int nbParts, FReal * forcesToFill, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            std::cout << "No meaning to retrieve source forces ... " << std::endl;
        }
        else{ //Targets OR Both
            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){
                        forcesToFill[indexes[idxPart]*3+0] = targets->getForcesX()[idxPart];
                        forcesToFill[indexes[idxPart]*3+1] = targets->getForcesY()[idxPart];
                        forcesToFill[indexes[idxPart]*3+2] = targets->getForcesZ()[idxPart];
                        checkCount++;
                    }
                });
        }
        if(checkCount < nbParts){std::cout << "Not all "<<nbParts <<" parts has been read (only "<<checkCount<<")"<< std::endl;}
        else{
            if(checkCount > nbParts){std::cout << "More parts than  "<<nbParts <<" has been read"<< std::endl;}
        }
208
    }
209 210 211 212 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

    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_get_forces_xyz_npart(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
                                      int nbParts, int* idxOfParticles , FReal * forcesToFill, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            std::cout << "No meaning to retrieve source forces ... " << std::endl;
        }
        else{ //Targets OR Both
            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]){
                                forcesToFill[iterPart*3+0] = targets->getForcesX()[idxPart];
                                forcesToFill[iterPart*3+1] = targets->getForcesY()[idxPart];
                                forcesToFill[iterPart*3+2] = targets->getForcesZ()[idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
                        }
                    }
                });
        }
        if(checkCount < nbParts){std::cout << "Not all "<<nbParts <<" parts has been read"<< std::endl;}
        else{
            if(checkCount > nbParts){std::cout << "More parts than  "<<nbParts <<" has been read"<< std::endl;}
        }
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 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

    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_get_forces(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
                            int nbParts, FReal * fX, FReal* fY, FReal* fZ, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            std::cout << "No meaning to retrieve source forces ... " << std::endl;
        }
        else{ //Targets OR Both
            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){
                        fX[indexes[idxPart]] = targets->getForcesX()[idxPart];
                        fY[indexes[idxPart]] = targets->getForcesY()[idxPart];
                        fZ[indexes[idxPart]] = targets->getForcesZ()[idxPart];
                        checkCount++;
                    }
                });
        }
        if(checkCount < nbParts){std::cout << "Not all "<<nbParts <<" parts has been read"<< std::endl;}
        else{
            if(checkCount > nbParts){std::cout << "More parts than  "<<nbParts <<" has been read"<< std::endl;}
        }
    }

    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_get_forces_nbpart(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
                                   int nbParts, int* idxOfParticles ,FReal * fX, FReal* fY, FReal* fZ, PartType type){
        int checkCount = 0;
        if(type == SOURCE){
            std::cout << "No meaning to retrieve source forces ... " << std::endl;
        }
        else{ //Targets OR Both
            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]){
                                fX[iterPart] = targets->getForcesX()[idxPart];
                                fY[iterPart] = targets->getForcesY()[idxPart];
                                fZ[iterPart] = targets->getForcesZ()[idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
                        }
                    }
                });
        }
        if(checkCount < nbParts){std::cout << "Not all "<<nbParts <<" parts has been read"<< std::endl;}
        else{
            if(checkCount > nbParts){std::cout << "More parts than  "<<nbParts <<" has been read"<< std::endl;}
        }
    }

    //Arranger parts : following function provide a way to move parts
    //inside the tree
    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_add_to_positions_xyz(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
                                      int NbPositions,FReal * updatedXYZ, 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->getWPositions()[0][idxPart] += updatedXYZ[indexes[idxPart]*3+0];
                        sources->getWPositions()[1][idxPart] += updatedXYZ[indexes[idxPart]*3+1];
                        sources->getWPositions()[2][idxPart] += updatedXYZ[indexes[idxPart]*3+2];
                        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->getWPositions()[0][idxPart] += updatedXYZ[indexes[idxPart]*3+0];
                        targets->getWPositions()[1][idxPart] += updatedXYZ[indexes[idxPart]*3+1];
                        targets->getWPositions()[2][idxPart] += updatedXYZ[indexes[idxPart]*3+2];
                        checkCount++;
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
        update_tree();
    }


    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_add_to_positions(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
                                  int NbPositions,FReal * X, FReal * Y , FReal * Z, 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->getWPositions()[0][idxPart] += X[indexes[idxPart]];
                        sources->getWPositions()[1][idxPart] += Y[indexes[idxPart]];
                        sources->getWPositions()[2][idxPart] += Z[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->getWPositions()[0][idxPart] += X[indexes[idxPart]];
                        targets->getWPositions()[1][idxPart] += Y[indexes[idxPart]];
                        targets->getWPositions()[2][idxPart] += Z[indexes[idxPart]];
                        checkCount++;
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
        update_tree();
    }

    //Not yet done
    void add_to_positions_xyz_npart( int NbPositions, int* idxOfParticles, FReal * updatedXYZ, PartType type){
        FAssertLF(0,"Not Yet done ...\n");
    }
    void add_to_positions_npart( int NbPositions, int* idxOfParticles,
                                 FReal * X, FReal * Y , FReal * Z, PartType type){
        FAssertLF(0,"Not Yet done ...\n");
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

    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_set_positions_xyz(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
                                   int NbPositions, FReal * updatedXYZ, 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->getWPositions()[0][idxPart] = updatedXYZ[indexes[idxPart]*3+0];
                        sources->getWPositions()[1][idxPart] = updatedXYZ[indexes[idxPart]*3+1];
                        sources->getWPositions()[2][idxPart] = updatedXYZ[indexes[idxPart]*3+2];
                        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->getWPositions()[0][idxPart] = updatedXYZ[indexes[idxPart]*3+0];
                        targets->getWPositions()[1][idxPart] = updatedXYZ[indexes[idxPart]*3+1];
                        targets->getWPositions()[2][idxPart] = updatedXYZ[indexes[idxPart]*3+2];
                        checkCount++;
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
        update_tree();
    }

428

429 430
    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_set_positions(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
431
                               int NbPositions, FReal * X, FReal * Y, FReal * Z, PartType type){
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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 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
        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->getWPositions()[0][idxPart] = X[indexes[idxPart]];
                        sources->getWPositions()[1][idxPart] = Y[indexes[idxPart]];
                        sources->getWPositions()[2][idxPart] = Z[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->getWPositions()[0][idxPart] = X[indexes[idxPart]];
                        targets->getWPositions()[1][idxPart] = Y[indexes[idxPart]];
                        targets->getWPositions()[2][idxPart] = Z[indexes[idxPart]];
                        checkCount++;
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
        update_tree();
    }

    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_set_positions_npart(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
                                     int NbPositions,int* idxOfParticles,FReal * X, FReal * Y , FReal * Z, 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->getWPositions()[0][idxPart] = X[indexes[idxPart]];
                        sources->getWPositions()[1][idxPart] = Y[indexes[idxPart]];
                        sources->getWPositions()[2][idxPart] = Z[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->getWPositions()[0][idxPart] = X[indexes[idxPart]];
                        targets->getWPositions()[1][idxPart] = Y[indexes[idxPart]];
                        targets->getWPositions()[2][idxPart] = Z[indexes[idxPart]];
                        checkCount++;
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
        update_tree();
    }

    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_set_positions_xyz_npart(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
                                         int NbPositions,int * idxOfParticles,FReal * updatedXYZ, 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->getWPositions()[0][idxPart] = updatedXYZ[indexes[idxPart]*3+0];
                        sources->getWPositions()[1][idxPart] = updatedXYZ[indexes[idxPart]*3+1];
                        sources->getWPositions()[2][idxPart] = updatedXYZ[indexes[idxPart]*3+2];
                        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->getWPositions()[0][idxPart] = updatedXYZ[indexes[idxPart]*3+0];
                        targets->getWPositions()[1][idxPart] = updatedXYZ[indexes[idxPart]*3+1];
                        targets->getWPositions()[2][idxPart] = updatedXYZ[indexes[idxPart]*3+2];
                        checkCount++;
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
        update_tree();
    }

    virtual void set_positions_xyz_npart( int NbPositions, int* idxOfParticles, FReal * updatedXYZ, PartType type){
538
        FAssertLF(0,"No tree instancied, exiting ...\n");
539
    }
540 541
    virtual void set_positions_npart( int NbPositions, int* idxOfParticles,
                                      FReal * X, FReal * Y , FReal * Z, PartType type){
542
        FAssertLF(0,"No tree instancied, exiting ...\n");
543
    }
544
    virtual void set_positions_xyz( int NbPositions, FReal * updatedXYZ, PartType type){
545
        FAssertLF(0,"No tree instancied, exiting ...\n");
546
    }
547
    virtual void set_positions( int NbPositions, FReal * X, FReal * Y , FReal * Z, PartType type){
548
        FAssertLF(0,"No tree instancied, exiting ...\n");
549
    }
550 551 552 553 554 555 556 557 558
    virtual void get_positions_xyz(int NbPositions,FReal * updatedXYZ, PartType type){
    }
    virtual void get_positions(int NbPositions,FReal * X,FReal * Y,FReal * Z, PartType type){
    }
    virtual void get_positions_xyz_npart(int NbPositions,int * idxOfPart, FReal * updatedXYZ, PartType type){
    }
    virtual void get_positions_npart(int NbPositions,int * idxOfPart, FReal * X,FReal * Y,FReal * Z, PartType type){
    }

559 560 561 562 563 564

    //Function to update the tree
    virtual void update_tree(){
        FAssertLF(0,"No tree instancied, exiting ...\n");
    }

565 566
    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_get_positions_xyz(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
567
                                   int NbPositions, FReal * positionsToFill, PartType type){
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
        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){
                        positionsToFill[indexes[idxPart]*3+0] = sources->getPositions()[0][idxPart];
                        positionsToFill[indexes[idxPart]*3+1] = sources->getPositions()[1][idxPart];
                        positionsToFill[indexes[idxPart]*3+2] = sources->getPositions()[2][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){
                        positionsToFill[indexes[idxPart]*3+0] = targets->getPositions()[0][idxPart];
                        positionsToFill[indexes[idxPart]*3+1] = targets->getPositions()[1][idxPart];
                        positionsToFill[indexes[idxPart]*3+2] = targets->getPositions()[2][idxPart];
                        checkCount++;
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
598
    }
599 600 601

    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_get_positions_xyz_npart(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
602
                                         int NbPositions, int * idxOfParticles, FReal * positionsToFill, PartType type){
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
        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 < NbPositions && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                positionsToFill[iterPart] =  sources->getPositions()[0][idxPart];
                                positionsToFill[iterPart] =  sources->getPositions()[1][idxPart];
                                positionsToFill[iterPart] =  sources->getPositions()[2][idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
                        }
                    }
                });
        }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 < NbPositions && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                positionsToFill[iterPart] =  targets->getPositions()[0][idxPart];
                                positionsToFill[iterPart] =  targets->getPositions()[1][idxPart];
                                positionsToFill[iterPart] =  targets->getPositions()[2][idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
                        }
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
653
    }
654 655 656

    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_get_positions(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
657
                               int NbPositions, FReal * X, FReal * Y , FReal * Z, PartType type){
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
        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){
                        X[indexes[idxPart]] = sources->getPositions()[0][idxPart];
                        Y[indexes[idxPart]] = sources->getPositions()[1][idxPart];
                        Z[indexes[idxPart]] = sources->getPositions()[2][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){
                        X[indexes[idxPart]] = targets->getPositions()[0][idxPart];
                        Y[indexes[idxPart]] = targets->getPositions()[1][idxPart];
                        Z[indexes[idxPart]] = targets->getPositions()[2][idxPart];
                        checkCount++;
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
688
    }
689 690 691

    template<class ContainerClass,class LeafClass,class CellClass>
    void generic_get_positions_npart(FOctree<FReal,CellClass,ContainerClass,LeafClass> * octree,
692
                                     int NbPositions, int * idxOfParticles,FReal * X, FReal * Y , FReal * Z,PartType type){
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
        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 < NbPositions && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                X[iterPart] =  sources->getPositions()[0][idxPart];
                                Y[iterPart] =  sources->getPositions()[1][idxPart];
                                Z[iterPart] =  sources->getPositions()[2][idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
                        }
                    }
                });
        }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 < NbPositions && notFoundYet){
                            if(indexes[idxPart] == idxOfParticles[iterPart]){
                                X[iterPart] =  targets->getPositions()[0][idxPart];
                                Y[iterPart] =  targets->getPositions()[1][idxPart];
                                Z[iterPart] =  targets->getPositions()[2][idxPart];
                                notFoundYet = false;
                                checkCount++;
                            }
                            else{
                                ++iterPart;
                            }
                        }
                    }
                });
        }
        if(checkCount < NbPositions){std::cout << "Not all "<<NbPositions <<" potentials has been read"<< std::endl;}
        else{
            if(checkCount > NbPositions){std::cout << "More parts than  "<<NbPositions <<" potentials has been read"<< std::endl;}
        }
743 744
    }

745
    virtual void apply_on_cell(Callback_apply_on_cell function){
746 747
    }

748
    template<class ContainerClass, class CellClass, class LeafClass>
749
    void generic_apply_on_cell(FOctree<FReal,CellClass,ContainerClass,LeafClass> * tree){
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
        //Reset forces and potentials
        tree->forEachLeaf([&](LeafClass * leaf){
                ContainerClass * targets = leaf->getTargets();
                FSize nbPartTarget = targets->getNbParticles();
                //Set potential to 0
                FReal * potentialsTarget = targets->getPotentialsArray();
                memset(potentialsTarget,0,sizeof(FReal)*nbPartTarget);
                //Set forces to 0
                FReal * forcesX = targets->getForcesXArray();
                FReal * forcesY = targets->getForcesYArray();
                FReal * forcesZ = targets->getForcesZArray();
                memset(forcesX,0,sizeof(FReal)*nbPartTarget);
                memset(forcesY,0,sizeof(FReal)*nbPartTarget);
                memset(forcesZ,0,sizeof(FReal)*nbPartTarget);
            });

        //Reset multipole and local development
        tree->forEachCell([&](CellClass * cell){
                cell->resetToInitialState();
            });
    }
771 772 773

    //User define Kernel Part
    virtual void user_kernel_config( Scalfmm_Kernel_Descriptor userKernel, void * userDatas){
774
        FAssertLF(0,"No user kernel defined, exiting ...\n");
775 776
    }

777
    virtual void execute_fmm(){
778
        FAssertLF(0,"No kernel set, cannot execute anything, exiting ...\n");
779
    }
780 781 782
    virtual void execute_fmm_far_field(){
        FAssertLF(0,"No kernel set, cannot execute anything, exiting ...\n");
    }
783

784 785 786 787
    virtual void intern_dealloc_handle(Callback_free_cell userDeallocator){
        FAssertLF(0,"No kernel set, cannot execute anything, exiting ...\n");
    }

788 789

    virtual void print_everything(){
790
    }
791

792 793 794 795 796
    /**
     * Monitoring Function, once the FMM has ended, it's possible to
     * get the time spent in each operator.
     */
    virtual void get_timers(FReal * Timers){
797 798 799 800 801 802 803
        Timers[0] = algoTimer->Timers["P2M"].elapsed();
        Timers[1] = algoTimer->Timers["M2M"].elapsed();
        Timers[2] = algoTimer->Timers["M2L"].elapsed();
        Timers[3] = algoTimer->Timers["L2L"].elapsed();
        Timers[4] = algoTimer->Timers["L2P"].elapsed();
        Timers[5] = algoTimer->Timers["P2P"].elapsed();
        Timers[6] = algoTimer->Timers["NearField"].elapsed();
804
    }
805

806
    virtual int get_nb_timers(){
807
        return FAlgorithmTimers::nbTimers;
808
    }
809 810 811 812 813

    virtual void set_upper_limit(int upperLimit){
        FAssertLF(0,"This feature is not available with Chebyshev Kernel, please use your own kernel or do not use it.\n Exiting anyways...\n");
    }

814 815 816 817 818 819 820 821 822
    virtual void create_local_partition(int nbPoints, double * particleXYZ, double ** localArrayFilled, FSize ** indexesFilled, FSize * outputNbPoint){
        FAssertLF(0,"Either no MPI used or wrong initiation function called\n");
    }
    virtual void create_global_partition(int nbPoints, double * particleXYZ, double ** localArrayFilled, FSize ** indexesFilled, FSize * outputNbPoint){
        FAssertLF(0,"Either no MPI used or wrong initiation function called\n");
    }
    virtual void generic_partition(FSize nbThings, size_t sizeofthing, void * arrayOfThing, void ** newArray){
        FAssertLF(0,"Either no MPI used or wrong initiation function called\n");
    }
823 824
};

825
template<class FReal>
826
struct ScalFmmCoreHandle {
827

828 829 830
    struct ScalFmmCoreConfig {
        // Read/Write parameter
        int treeHeight;     //  Number of level in the octree
831 832
        FReal boxWidth;    // Simulation box size (root level)
        FPoint<FReal> boxCenter; // Center position of the box simulation(FReal[3])
833 834 835
    };

    ScalFmmCoreConfig config;
836
    FScalFMMEngine<FReal>* engine;
837 838 839 840
};



841 842 843 844 845
extern "C" void scalfmm_build_tree(scalfmm_handle Handle,int TreeHeight,double BoxWidth,
                                   double* BoxCenter,
                                   Scalfmm_Cell_Descriptor user_cell_descriptor,
                                   Scalfmm_Leaf_Descriptor user_leaf_descriptor){
    ((ScalFmmCoreHandle<double> *) Handle)->engine->build_tree(TreeHeight,BoxWidth, BoxCenter, user_cell_descriptor, user_leaf_descriptor);
846
}
847

848 849 850
extern "C" void scalfmm_tree_insert_particles(scalfmm_handle Handle, int NbPositions, double * arrayX, double * arrayY, double * arrayZ,
                                              PartType type){
    ((ScalFmmCoreHandle<double> *) Handle)->engine->tree_insert_particles(NbPositions, arrayX, arrayY, arrayZ, type);
851 852
}

853 854
extern "C" void scalfmm_tree_insert_particles_xyz(scalfmm_handle Handle, int NbPositions, double * XYZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->tree_insert_particles_xyz(NbPositions, XYZ,type);
855 856
}

857 858
extern "C" void scalfmm_set_physical_values(scalfmm_handle Handle, int nbPhysicalValues, double * physicalValues, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_physical_values(nbPhysicalValues, physicalValues, type);
859 860
}

861 862
extern "C" void scalfmm_get_physical_values(scalfmm_handle Handle, int nbPhysicalValues, double * physicalValues, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_physical_values(nbPhysicalValues, physicalValues, type);
863 864 865
}

extern "C" void scalfmm_set_physical_values_npart(scalfmm_handle Handle, int nbPhysicalValues,
866 867
                                                  int* idxOfParticles, double * physicalValues, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_physical_values_npart(nbPhysicalValues,
868
                                                                               idxOfParticles, physicalValues, type);
869 870
}
extern "C" void scalfmm_get_physical_values_npart(scalfmm_handle Handle, int nbPhysicalValues,
871 872
                                                  int* idxOfParticles, double * physicalValues, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_physical_values_npart(nbPhysicalValues,
873
                                                                               idxOfParticles, physicalValues, type);
874 875 876
}

//To get the result
877 878
extern "C" void scalfmm_get_forces_xyz(scalfmm_handle Handle, int nbParts, double * forcesToFill, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_forces_xyz(nbParts, forcesToFill, type);
879 880
}

881 882
extern "C" void scalfmm_get_forces_xyz_npart(scalfmm_handle Handle, int nbParts, int* idxOfParticles, double * forcesToFill, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_forces_xyz_npart(nbParts, idxOfParticles, forcesToFill, type);
883
}
884 885
extern "C" void scalfmm_get_forces(scalfmm_handle Handle, int nbParts, double * fX, double* fY, double* fZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_forces(nbParts,fX, fY, fZ, type);
886 887
}

888 889
extern "C" void scalfmm_get_forces_npart(scalfmm_handle Handle, int nbParts, int* idxOfParticles, double * fX, double* fY, double* fZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_forces_npart(nbParts, idxOfParticles, fX, fY, fZ, type);
890 891
}

892
//To set iniital condition
893 894
extern "C" void scalfmm_set_forces_xyz(scalfmm_handle Handle, int nbParts, double * forcesToFill, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_forces_xyz(nbParts, forcesToFill, type);
895 896
}

897 898
extern "C" void scalfmm_set_forces_xyz_npart(scalfmm_handle Handle, int nbParts, int* idxOfParticles, double * forcesToFill, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_forces_xyz_npart(nbParts, idxOfParticles, forcesToFill, type);
899 900
}

901 902
extern "C" void scalfmm_set_forces(scalfmm_handle Handle, int nbParts, double * fX, double* fY, double* fZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_forces(nbParts, fX, fY, fZ, type);
903 904
}

905 906
extern "C" void scalfmm_set_forces_npart(scalfmm_handle Handle, int nbParts, int* idxOfParticles, double * fX, double* fY, double* fZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_forces_npart(nbParts, idxOfParticles, fX, fY, fZ, type);
907 908 909
}

//To deal with potential
910 911
extern "C" void scalfmm_get_potentials(scalfmm_handle Handle, int nbParts, double * potentialsToFill, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_potentials(nbParts, potentialsToFill, type);
912 913
}

914 915
extern "C" void scalfmm_set_potentials(scalfmm_handle Handle, int nbParts, double * potentialsToFill, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_potentials(nbParts, potentialsToFill, type);
916 917
}

918 919
extern "C" void scalfmm_get_potentials_npart(scalfmm_handle Handle, int nbParts, int* idxOfParticles, double * potentialsToFill, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_potentials_npart(nbParts, idxOfParticles, potentialsToFill, type);
920 921
}

922 923
extern "C" void scalfmm_set_potentials_npart(scalfmm_handle Handle, int nbParts, int* idxOfParticles, double * potentialsToFill, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_potentials_npart(nbParts, idxOfParticles, potentialsToFill, type);
924 925 926
}


927 928 929
// //To deal with positions
// //Out of the box behavior
// extern "C" void scalfmm_out_of_the_box_config(scalfmm_handle Handle,scalfmm_out_of_box_behavior config){
930
//     ((ScalFmmCoreHandle<double> * ) Handle)->engine->out_of_the_box_config(config);
931
// }
932

933
//Update
934 935
extern "C" void scalfmm_add_to_positions_xyz(scalfmm_handle Handle, int NbPositions, double * updatedXYZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->add_to_positions_xyz(NbPositions, updatedXYZ, type);
936 937
}

938 939
extern "C" void scalfmm_add_to_positions(scalfmm_handle Handle, int NbPositions, double * X, double * Y , double * Z, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->add_to_positions(NbPositions, X, Y, Z, type);
940 941
}

942 943
extern "C" void scalfmm_add_to_positions_xyz_npart(scalfmm_handle Handle, int NbPositions, int* idxOfParticles, double * updatedXYZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->add_to_positions_xyz_npart(NbPositions, idxOfParticles, updatedXYZ, type);
944 945
}

946
extern "C" void scalfmm_add_to_positions_npart(scalfmm_handle Handle, int NbPositions, int* idxOfParticles,
947 948
                                               double * X, double * Y , double * Z, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->add_to_positions_npart(NbPositions, idxOfParticles, X, Y, Z, type);
949
}
950
//Set new positions
951 952
extern "C" void scalfmm_set_positions_xyz(scalfmm_handle Handle, int NbPositions, double * updatedXYZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_positions_xyz(NbPositions, updatedXYZ, type);
953 954
}

955 956
extern "C" void scalfmm_set_positions(scalfmm_handle Handle, int NbPositions, double * X, double * Y , double * Z, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_positions(NbPositions, X, Y , Z, type);
957 958
}

959 960
extern "C" void scalfmm_set_positions_xyz_npart(scalfmm_handle Handle, int NbPositions, int* idxOfParticles, double * updatedXYZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_positions_xyz_npart(NbPositions, idxOfParticles, updatedXYZ, type);
961 962
}

963
extern "C" void scalfmm_set_positions_npart(scalfmm_handle Handle, int NbPositions, int* idxOfParticles,
964 965
                                            double * X, double * Y , double * Z, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_positions_npart(NbPositions, idxOfParticles, X, Y, Z, type);
966
}
967 968

extern "C" void scalfmm_update_tree(scalfmm_handle Handle){
969
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->update_tree();
970 971
}

972
//Get back positions
973 974
extern "C" void scalfmm_get_positions_xyz(scalfmm_handle Handle, int NbPositions, double * updatedXYZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_positions_xyz(NbPositions, updatedXYZ, type);
975 976
}

977 978
extern "C" void scalfmm_get_positions(scalfmm_handle Handle, int NbPositions, double * X, double * Y , double * Z, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_positions(NbPositions, X, Y , Z, type);
979 980
}

981 982
extern "C" void scalfmm_get_positions_xyz_npart(scalfmm_handle Handle, int NbPositions, int* idxOfParticles, double * updatedXYZ, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_positions_xyz_npart(NbPositions, idxOfParticles, updatedXYZ, type);
983 984
}

985
extern "C" void scalfmm_get_positions_npart(scalfmm_handle Handle, int NbPositions, int* idxOfParticles,
986 987
                                            double * X, double * Y , double * Z, PartType type){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_positions_npart(NbPositions, idxOfParticles, X, Y, Z, type);
988
}
989

990 991
//To choose algorithm
extern "C" void scalfmm_algorithm_config(scalfmm_handle Handle, scalfmm_algorithm config){
992
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->algorithm_config(config);
993 994
}

995
//Executing FMM
996
extern "C" void scalfmm_execute_fmm(scalfmm_handle Handle){
997
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->execute_fmm();
998
}
999

1000 1001 1002 1003 1004 1005
//Executing FMM
extern "C" void scalfmm_execute_fmm_far_field(scalfmm_handle Handle){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->execute_fmm_far_field();
}


1006
extern "C" void scalfmm_user_kernel_config(scalfmm_handle Handle, Scalfmm_Kernel_Descriptor userKernel, void * userDatas){
1007
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->user_kernel_config(userKernel,userDatas);
1008 1009
}

1010 1011 1012
//Monitoring functions
extern "C" void scalfmm_get_timers(scalfmm_handle Handle, double * Timers){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_timers(Timers);
1013 1014
}

1015 1016
extern "C" int scalfmm_get_nb_timers(scalfmm_handle Handle){
    return ((ScalFmmCoreHandle<double> * ) Handle)->engine->get_nb_timers();
1017 1018
}

1019 1020 1021 1022
// extern "C" void scalfmm_tree_abstract_insert(scalfmm_handle Handle, int NbPartToInsert, int nbAttributeToInsert, int * strideForEachAtt,
//                                              double* rawDatas){
//     ((ScalFmmCoreHandle<double> * ) Handle)->engine->tree_abstract_insert(NbPartToInsert,nbAttributeToInsert,strideForEachAtt,rawDatas);
// }
1023

PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
/**
 * These functions are just translating functions.
 */

//< This function fill the childFullPosition[3] with [-1;1] to know the position of a child relatively to
//< its position from its parent
extern "C" void scalfmm_utils_parentChildPosition(int childPosition, int* childFullPosition){
    childFullPosition[2] = (childPosition%2 ? 1 : -1);
    childFullPosition[1] = ((childPosition/2)%2 ? 1 : -1);
    childFullPosition[0] = ((childPosition/4)%2 ? 1 : -1);
}

//< This function fill the childFullPosition[3] with [-3;3] to know the position of a interaction
//< cell relatively to its position from the target
extern "C" void scalfmm_utils_interactionPosition(int interactionPosition, int* srcPosition){
    srcPosition[2] = interactionPosition%7 - 3;
    srcPosition[1] = (interactionPosition/7)%7 - 3;
    srcPosition[0] = (interactionPosition/49)%7 - 3;
}


1045 1046
extern "C" void scalfmm_apply_on_cell(scalfmm_handle Handle, Callback_apply_on_cell function){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->apply_on_cell(function);
1047 1048
}

1049 1050
extern "C" void scalfmm_print_everything(scalfmm_handle Handle){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->print_everything();
1051
}
1052

1053 1054 1055 1056
extern "C" void scalfmm_set_upper_limit(scalfmm_handle Handle, int upperLimit){
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->set_upper_limit(upperLimit);
}

1057
extern "C" void scalfmm_apply_on_leaf(scalfmm_handle Handle, Callback_apply_on_leaf function){
1058 1059 1060
    ((ScalFmmCoreHandle<double> * ) Handle)->engine->apply_on_each_leaf(function);
}

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
#ifdef SCALFMM_USE_MPI
extern "C" void scalfmm_create_local_partition(scalfmm_handle handle, int nbPoints, double * particleXYZ, double ** localArrayFilled,
                                               FSize ** indexesFilled, FSize * outputNbPoint){
    ((ScalFmmCoreHandle<double> * ) handle)->engine->create_local_partition(nbPoints,particleXYZ,localArrayFilled,indexesFilled,outputNbPoint);
}

extern "C" void scalfmm_create_global_partition(scalfmm_handle handle, int nbPoints, double * particleXYZ, double ** localArrayFilled,
                                                FSize ** indexesFilled, FSize * outputNbPoint){
    ((ScalFmmCoreHandle<double> * ) handle)->engine->create_global_partition(nbPoints,particleXYZ,localArrayFilled,indexesFilled,outputNbPoint);
}

extern "C" void scalfmm_generic_partition(scalfmm_handle handle, FSize nbThings, size_t sizeofthing, void * arrayOfThing, void ** newArray){
    ((ScalFmmCoreHandle<double> * ) handle)->engine->generic_partition(nbThings,sizeofthing,arrayOfThing,newArray);
}

1076 1077 1078 1079
extern "C" void scalfmm_call_delete(void * inPtr){
    delete [] static_cast<double*>(inPtr);
}

1080 1081
#endif

1082
#endif