ScalfmmApiCore.cpp 21.8 KB
Newer Older
BRAMAS Berenger's avatar
BRAMAS Berenger committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// ===================================================================================
// Logiciel initial: ScalFmm Version 0.5
// Co-auteurs : Olivier Coulaud, Bérenger Bramas.
// Propriétaires : INRIA.
// Copyright © 2011-2012, diffusé sous les termes et conditions d’une licence propriétaire.
// Initial software: ScalFmm Version 0.5
// Co-authors: Olivier Coulaud, Bérenger Bramas.
// Owners: INRIA.
// Copyright © 2011-2012, spread under the terms and conditions of a proprietary license.
// ===================================================================================
#include "../../Src/Containers/FOctree.hpp"
#include "../../Src/Containers/FVector.hpp"

#include "../../Src/Components/FSimpleLeaf.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
15
#include "../../Src/Components/FBasicCell.hpp"
BRAMAS Berenger's avatar
BRAMAS Berenger committed
16 17 18 19 20 21 22 23 24

#include "../../Src/Utils/FPoint.hpp"

#include "../../Src/Core/FFmmAlgorithm.hpp"
#include "../../Src/Core/FFmmAlgorithmThread.hpp"
#include "../../Src/Core/FFmmAlgorithmTask.hpp"

#include "../../Src/Components/FBasicKernels.hpp"

25 26
#include "../../Src/Components/FBasicParticleContainer.hpp"

27
#ifdef SCALFMM_USE_MPI
BRAMAS Berenger's avatar
BRAMAS Berenger committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include "../../Src/Utils/FMpi.hpp"
#endif

#include "FmmApi.h"

template <class ContainerClass>
class CoreCell : public FBasicCell {
    char* multipole;
    char* local;
    int level;
    FReal positions[3];
    mutable ContainerClass* container;

public:
42
    CoreCell() : multipole(nullptr), local(nullptr), level(0), container(nullptr) {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    }
    void createArrays(const int multipoleSize, const int localSize){
        multipole = new char[multipoleSize];
        local     = new char[localSize];

        memset(multipole, 0, multipoleSize);
        memset(local, 0, localSize);
    }
    ~CoreCell(){
        delete[] multipole;
        delete[] local;
    }

    const void* getMultipole() const{
        return multipole;
    }

    const void* getLocal() const{
        return local;
    }

    void* getMultipole(){
        return multipole;
    }

    void* getLocal(){
        return local;
    }

    void setLevel(const int inLevel){
        level = inLevel;
    }

    int getLevel() const {
        return level;
    }

    void setPosition(const FReal inPositions[3]){
        positions[0] = inPositions[0];
        positions[1] = inPositions[1];
        positions[2] = inPositions[2];
    }

    const FReal* getPosition() const {
        return positions;
    }

    void setContainer(ContainerClass* inContainer) const {
        container = inContainer;
    }

    ContainerClass* getContainer() const {
        return container;
    }
};



101 102
template< class CellClass, class ContainerClass>
class CoreKernel : public FAbstractKernels<CellClass,ContainerClass> {
BRAMAS Berenger's avatar
BRAMAS Berenger committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    void* fmmCore;

public:
    CoreKernel(void* inFmmCore): fmmCore(inFmmCore){
    }

    /** Default destructor */
    virtual ~CoreKernel(){
    }

    /** Do nothing */
    virtual void P2M(CellClass* const cell, const ContainerClass* const container) {
        cell->setContainer(const_cast<ContainerClass*>(container));
        FmmKernel_P2M(fmmCore,(void*) cell);
    }

    /** Do nothing */
    virtual void M2M(CellClass* const FRestrict cell, const CellClass*const FRestrict *const FRestrict children, const int ) {
        for(int idx = 0 ; idx < 8 ; ++idx){
            if( children[idx] ){
                FmmKernel_M2M(fmmCore, (void*)cell, (void*)children[idx]);
            }
        }
    }

    /** Do nothing */
    virtual void M2L(CellClass* const FRestrict cell, const CellClass* interactions[], const int , const int ) {
        for(int idx = 0 ; idx < 343 ; ++idx){
            if( interactions[idx] ){
                FmmKernel_M2L(fmmCore, (void*)cell, (void*)interactions[idx]);
            }
        }
    }

    /** Do nothing */
    virtual void L2L(const CellClass* const FRestrict cell, CellClass* FRestrict *const FRestrict children, const int ) {
        for(int idx = 0 ; idx < 8 ; ++idx){
            if( children[idx] ){
                FmmKernel_L2L(fmmCore, (void*)cell, (void*)children[idx]);
            }
        }
    }

    /** Do nothing */
    virtual void L2P(const CellClass* const cell, ContainerClass* const container){
        cell->setContainer((ContainerClass*)container);
        FmmKernel_L2P(fmmCore, (void*)cell);
    }


    /** Do nothing */
    virtual void P2P(const FTreeCoordinate& ,
                     ContainerClass* const FRestrict targets, const ContainerClass* const FRestrict /*sources*/,
                     ContainerClass* const neighbors[27], const int ){
        CellClass* cell = (CellClass*)targets->getParentCell();
158
        FmmKernel_P2P_inner(fmmCore, cell);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

        for(int idx = 0 ; idx < 27 ; ++idx){
            if( neighbors[idx] ){
                FmmKernel_P2P(fmmCore, (CellClass*)neighbors[idx]->getParentCell(), cell);
            }
        }
    }

    /** Do nothing */
    virtual void P2PRemote(const FTreeCoordinate& ,
                     ContainerClass* const FRestrict , const ContainerClass* const FRestrict ,
                     ContainerClass* const [27], const int ){
        printf("Error remote not implemented!!!!\n");
    }

};


class CoreVector {
    mutable void* parentCell;
    void* fields;
    int sizeOfField;
    void* potentials;
    int sizeOfPential;
    FVector<FReal> positions;
    FVector<int> indexes;
public:
186 187
    CoreVector() : parentCell(nullptr), fields(nullptr), sizeOfField(0),
        potentials(nullptr), sizeOfPential(0){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    }

    ~CoreVector(){
        delete[] (char*)fields;
        delete[] (char*)potentials;
    }

    void setParentCell(void* inCell) const{
        parentCell = inCell;
    }

    void* getParentCell() const{
        return parentCell;
    }

    void allocateFields(const int memSizePerParticles){
        if(fields) delete[](char*)fields;
        fields = new char[getNbParticles() * memSizePerParticles];
        sizeOfField = memSizePerParticles;
        memset(fields, 0, getNbParticles() * memSizePerParticles);
    }

    void* getFields() const{
        return fields;
    }

    int getSizeOfField() const{
        return sizeOfField;
    }

    void allocatePotentials(const int memSizePerParticles){
        if(potentials) delete[](char*)potentials;
        potentials = new char[getNbParticles() * memSizePerParticles];
        sizeOfPential = memSizePerParticles;
        memset(potentials, 0, getNbParticles() * memSizePerParticles);
    }

    void* getPotentials() const{
        return potentials;
    }

    int getSizeOfPotential() const{
        return sizeOfPential;
    }

    const int* getIndexes() const {
        return indexes.data();
    }

    const FReal* getPositions() const {
        return positions.data();
    }

    int getNbParticles() {
        return indexes.getSize();
    }

245
    void push(const FPoint<FReal>& partPosition, const int partIndex){
246 247 248 249
        positions.push(partPosition.getX());
        positions.push(partPosition.getY());
        positions.push(partPosition.getZ());
        indexes.push(partIndex);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
250 251 252
    }
};

253 254

typedef CoreVector          CoreContainerClass;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
255 256

typedef CoreCell<CoreContainerClass>      CoreCellClass;
257
typedef FSimpleLeaf<FReal, CoreContainerClass >                        LeafClass;
258 259
typedef FOctree<CoreCellClass, CoreContainerClass , LeafClass >     OctreeClass;
typedef CoreKernel<CoreCellClass, CoreContainerClass>         CoreKernelClass;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
260

261 262
typedef FFmmAlgorithm<OctreeClass, CoreCellClass, CoreContainerClass, CoreKernelClass, LeafClass >     FmmClass;
typedef FFmmAlgorithmThread<OctreeClass, CoreCellClass, CoreContainerClass, CoreKernelClass, LeafClass >     FmmClassThread;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
263 264 265 266 267 268 269 270


struct ScalFmmCoreHandle {
    struct ScalFmmCoreConfig {
        //paramètres en lecture/écriture :
        int treeHeight;     // hombre de niveaux de l'arbre (int)
        FReal boxWidth;    // taille de la boîte racine (FReal)
        FReal boxCenter[3]; // position du centre de la boîte racine (FReal[3])
271
#ifdef SCALFMM_USE_MPI
BRAMAS Berenger's avatar
BRAMAS Berenger committed
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
        MPI_Comm mpiCom;    // communicateur MPI (MPI_Comm)
#endif
        int nbThreads;      // nombre de threads (int)
        int rhsNumber;      // nombre de seconds membres (int)
    };

    ScalFmmCoreConfig config;
    OctreeClass* octree;
    void *kernelHandle;
};

int FmmCore_init(void **fmmCore) {
    ScalFmmCoreHandle* corehandle = new ScalFmmCoreHandle;
    memset(corehandle, 0, sizeof(corehandle));

    corehandle->config.nbThreads = omp_get_max_threads();

    *fmmCore = corehandle;

    return FMMAPI_NO_ERROR;
} /*alloue et initialise le FmmCore*/


int FmmCore_free(void *fmmCore) {
    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;
    if(corehandle->octree) delete corehandle->octree;
    delete corehandle;
    return FMMAPI_NO_ERROR;
} /*libère le FmmCore*/

int FmmCore_isParameterUsed(void */*fmmCore*/, int *name, int *flag){
    switch( *name ){
    case FMMCORE_ROOT_BOX_WIDTH :
    case FMMCORE_ROOT_BOX_CENTER :
    case FMMCORE_TREE_HEIGHT :
307
#ifdef SCALFMM_USE_MPI
BRAMAS Berenger's avatar
BRAMAS Berenger committed
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
    case FMMCORE_MPI_COMMUNICATOR:
#endif
    case FMMCORE_THREADS_NUMBER:
    case FMMCORE_THREAD_ID:
    case FMMCORE_RHS_NUMBER:
    case  FMMCORE_HANDLES_P2P:
        *flag = FMMAPI_SUPPORTED_PARAMETER;
        break;
    case FMMCORE_LEAF_BOX_WIDTH :
    case FMMCORE_POINTS_PER_LEAF :
        *flag = FMMAPI_UNSUPPORTED_PARAMETER;
        break;
    default:
        *flag = FMMAPI_UNKNOWN_PARAMETER;
    }

    return FMMAPI_NO_ERROR;
}

int FmmCore_setParameter(void *fmmCore, int *name, void*value){
    int flag;

    FmmCore_isParameterUsed(fmmCore, name, &flag);
    if( flag != FMMAPI_SUPPORTED_PARAMETER){
        return flag;
    }

    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;
    switch( *name ){
    case FMMCORE_TREE_HEIGHT :
        corehandle->config.treeHeight = *(int*)value ;
        break;
    case FMMCORE_ROOT_BOX_WIDTH :
        corehandle->config.boxWidth = *(FReal*)value;
        break;
    case FMMCORE_ROOT_BOX_CENTER :
        memcpy(corehandle->config.boxCenter, value, sizeof(FReal)*3);
        break;
346
#ifdef SCALFMM_USE_MPI
BRAMAS Berenger's avatar
BRAMAS Berenger committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    case FMMCORE_MPI_COMMUNICATOR:
        corehandle->config.mpiCom = *(MPI_Comm*)value;
        break;
#endif
    case FMMCORE_THREADS_NUMBER:
        corehandle->config.nbThreads = *(int*)value;
        break;
    case FMMCORE_RHS_NUMBER:
        corehandle->config.rhsNumber = *(int*)value;
        break;
    case FMMCORE_HANDLES_P2P:
    case FMMCORE_THREAD_ID:
        return FMMAPI_UNSUPPORTED_PARAMETER;
    default:
        return FMMAPI_UNKNOWN_PARAMETER;
    }

    return FMMAPI_NO_ERROR;
}

int FmmCore_setParameter(void *fmmCore, int name, void*value){
    return FmmCore_setParameter(fmmCore, &name, value);
}


int FmmCore_getParameter(void *fmmCore, int *name, void*value){
    int flag;

    FmmCore_isParameterUsed(fmmCore, name, &flag);
    if( flag != FMMAPI_SUPPORTED_PARAMETER){
        return flag;
    }

    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;
    switch( *name ){
    case FMMCORE_TREE_HEIGHT :
        *(int*)value = corehandle->config.treeHeight;
        break;
    case FMMCORE_ROOT_BOX_WIDTH :
        *(FReal*)value = corehandle->config.boxWidth;
        break;
    case FMMCORE_ROOT_BOX_CENTER :
        memcpy(value,corehandle->config.boxCenter, sizeof(FReal)*3);
        break;
391
#ifdef SCALFMM_USE_MPI
BRAMAS Berenger's avatar
BRAMAS Berenger committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 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
    case FMMCORE_MPI_COMMUNICATOR:
        *(MPI_Comm*)value = corehandle->config.mpiCom;
        break;
#endif
    case FMMCORE_THREADS_NUMBER:
        *(int*)value = corehandle->config.nbThreads;
        break;
    case FMMCORE_THREAD_ID:
        *(int*)value = omp_get_thread_num();
        break;
    case FMMCORE_RHS_NUMBER:
        *(int*)value = corehandle->config.rhsNumber;
        break;
    case  FMMCORE_HANDLES_P2P:
        *(int*)value = true;
        break;
    default:
        return FMMAPI_UNKNOWN_PARAMETER;
    }

    return FMMAPI_NO_ERROR;
}

int FmmCore_getParameter(void *fmmCore, int name, void*value){
    return FmmCore_getParameter(fmmCore, &name, value);
}


int FmmCore_getRadius(void*fmmCore, void *boxId, FReal *radius) {
    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;
    CoreCellClass* boxhandle = (CoreCellClass*)boxId;
    *radius = corehandle->octree->getBoxWidth() / FReal(1<<boxhandle->getLevel());
    return FMMAPI_NO_ERROR;
} /*Renvoie le rayon de la boîte*/

int FmmCore_getCentre(void*/*fmmCore*/, void *boxId, FReal **centre) {
    CoreCellClass* boxhandle = (CoreCellClass*)boxId;

    (*centre)[0] = boxhandle->getPosition()[0];
    (*centre)[1] = boxhandle->getPosition()[1];
    (*centre)[2] = boxhandle->getPosition()[2];

    return FMMAPI_NO_ERROR;
} /*Renvoie dans le FReal[3] centre les coordonnées du centre*/

int FmmCore_getLevel(void*/*fmmCore*/, void *boxId, int *level) {
    CoreCellClass* boxhandle = (CoreCellClass*)boxId;
    *level = boxhandle->getLevel();
    return FMMAPI_NO_ERROR;
} /*Renvoie dans level le niveau de la boîte boxId dans son arbre*/

int FmmCore_getMultipoleArray(void* /*fmmCore*/, void *boxId, void **F) {
    CoreCellClass* cell = (CoreCellClass*)boxId;
    *F = cell->getMultipole();
    return FMMAPI_NO_ERROR;
} /*Renvoie dans F l'adresse où stocker l'expansion multipôle associée à la boîte boxId*/

int FmmCore_getLocalArray(void* /*fmmCore*/, void *boxId, void **F) {
    CoreCellClass* cell = (CoreCellClass*)boxId;
    *F = cell->getLocal();
    return FMMAPI_NO_ERROR;
} /*Renvoie dans F l'adresse où stocker l'expansion locale associée à la boîte boxId*/

int FmmCore_getCoord(void*/*fmmCore*/, void *boxId, int *coord) {
    CoreCellClass* boxhandle = (CoreCellClass*)boxId;
    coord[0] = boxhandle->getCoordinate().getX();
    coord[1] = boxhandle->getCoordinate().getY();
    coord[2] = boxhandle->getCoordinate().getZ();
    return FMMAPI_NO_ERROR;
} /*Renvoie dans coord la position dans l'arbre*/


/* Données potentiel/champ */
int FmmCore_getSource(void* /*fmmCore*/, void *boxId, FReal** position, void** potential, int *number) {
    CoreCellClass* boxhandle = (CoreCellClass*)boxId;
    CoreContainerClass* sources = boxhandle->getContainer();

    *number = sources->getNbParticles();
    *position = const_cast<FReal*>(sources->getPositions());
    *potential = sources->getPotentials();

    return FMMAPI_NO_ERROR;
} /* Appelé par P2P et P2M pour obtenir le nombre, la position et le potentiel des sources.
Les différents tableaux sont (éventuellement) alloués par le FmmCore. */

int FmmCore_releaseSource(void*fmmCore, void *boxId, void* potential, FReal* position) {
    return FMMAPI_NO_ERROR;
} /* si le core veut libérer ces tableaux potentiel et position.*/

int FmmCore_getTargetPoints(void* /*fmmCore*/, void *boxId, FReal** position, int *number) {
    CoreCellClass* boxhandle = (CoreCellClass*)boxId;
    CoreContainerClass* targets = boxhandle->getContainer();

    *number = targets->getNbParticles();
    *position = const_cast<FReal*>(targets->getPositions());

    return FMMAPI_NO_ERROR;
} /* : Appelé par P2P et L2P pour obtenir le nombre et la position des points cibles.*/

int FmmCore_releaseTargetPoints(void*fmmCore, void *boxId, FReal* position) {
    return FMMAPI_NO_ERROR;
} /* si le core veut libérer ce tableau "position".*/

int FmmCore_getTargetField(void* /*fmmCore*/, void *boxId, FReal* F) {
    CoreCellClass* boxhandle = (CoreCellClass*)boxId;
    CoreContainerClass* targets = boxhandle->getContainer();

    memcpy(F, targets->getFields(), targets->getNbParticles() * targets->getSizeOfField());

    return FMMAPI_NO_ERROR;
} /* obtient dans un tableau F alloué/libéré par L2P/P2P les valeurs des champs aux points cible
(pour le cas où P2P et L2P doivent sommer leurs résultats).*/

int FmmCore_setTargetField(void* /*fmmCore*/, void *boxId, FReal* F) {
    CoreCellClass* boxhandle = (CoreCellClass*)boxId;
    CoreContainerClass* targets = boxhandle->getContainer();

    memcpy(targets->getFields(), F, targets->getNbParticles() * targets->getSizeOfField());

    return FMMAPI_NO_ERROR;
} /* transmets au FmmCore dans F les valeurs des champs aux points cibles mis à jour.*/



/* Entrée/sortie principale */
int FmmCore_setKernelData(void *fmmCore, void *fmmKernel)  {
    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;
    corehandle->kernelHandle = fmmKernel;
    return FMMAPI_NO_ERROR;
} /* stocke l'identifiant du FmmKernel dans le FmmCore. Cela permet par la suite aux
opérateurs FMM d'accéder au FmmKernel, et donc d'accéder aux données spécifiques au kernel
(p.ex. fréquence dans le cas Helmholtz, …)*/

int FmmCore_getKernelData(void*fmmCore, void **fmmKernel)  {
    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;
    *fmmKernel = corehandle->kernelHandle;
    return FMMAPI_NO_ERROR;
} /* récupère l'identifiant du FmmKernel. */


int FmmCore_setPositions(void *fmmCore, int *nb, FReal *position)  {
    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;
BRAMAS Berenger's avatar
BRAMAS Berenger committed
534 535

    corehandle->octree = new OctreeClass(corehandle->config.treeHeight, FMath::Min(3,corehandle->config.treeHeight-1),
536
                                         corehandle->config.boxWidth, FPoint<FReal>(corehandle->config.boxCenter));
BRAMAS Berenger's avatar
BRAMAS Berenger committed
537 538 539 540

    if( corehandle->config.nbThreads != 0){
        omp_set_num_threads(corehandle->config.nbThreads);
    }
BRAMAS Berenger's avatar
BRAMAS Berenger committed
541

542
    for(FSize idxPart = 0 ; idxPart < (*nb) ; ++idxPart){
543
        const FReal* pos = &position[idxPart * 3];
544
        corehandle->octree->insert(FPoint<FReal>(pos[0], pos[1], pos[2]),idxPart);
BRAMAS Berenger's avatar
BRAMAS Berenger committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    }

    return FMMAPI_NO_ERROR;
} /* transmet au FmmCore les potentiels associés aux points sources.
Le tableau potential est alloué et libéré par la routine appelant, le FmmCore doit donc en faire une copie.*/


int FmmCore_setPotentials(void *fmmCore, void *potentials)  {
    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;
    OctreeClass* octree = corehandle->octree;

    void* fmmKernel;
    FmmCore_getKernelData(fmmCore, &fmmKernel);

    int sizeOfPotentials;
    FmmKernel_getParameter(fmmKernel, FMMKERNEL_POTENTIAL_DATA_SIZE, &sizeOfPotentials);

    typename OctreeClass::Iterator octreeIterator(octree);
    octreeIterator.gotoBottomLeft();
    do{
        CoreContainerClass* particles = octreeIterator.getCurrentLeaf()->getSrc();
        particles->allocatePotentials(sizeOfPotentials);

        for(int idx = 0 ; idx < particles->getNbParticles() ; ++idx){
            memcpy(((char*)particles->getPotentials())+idx*sizeOfPotentials,
                   ((char*)potentials)+particles->getIndexes()[idx]*sizeOfPotentials,
                   sizeOfPotentials);
        }

    } while( octreeIterator.moveRight() );

    return FMMAPI_NO_ERROR;
} /* transmet au FmmCore les potentiels associés aux points sources.
Le tableau potential est alloué et libéré par la routine appelant, le FmmCore doit donc en faire une copie.*/

int FmmCore_doComputation(void *fmmCore)  {
    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;

    { // Ceck if there is number of NbPart summed at level 1
584
        FPoint<FReal> corner = corehandle->octree->getBoxCenter();
BRAMAS Berenger's avatar
BRAMAS Berenger committed
585 586 587 588 589 590
        corner -= (corehandle->octree->getBoxWidth()/2);

        FReal boxWidth = corehandle->octree->getBoxWidth()/( 1<< (corehandle->config.treeHeight-1) );

        typename OctreeClass::Iterator octreeIterator(corehandle->octree);
        octreeIterator.gotoBottomLeft();
591
        for(int idxLevel = corehandle->config.treeHeight - 1 ; idxLevel >= 1 ; --idxLevel ){
BRAMAS Berenger's avatar
BRAMAS Berenger committed
592 593 594 595 596 597 598 599 600 601
            int multipoleSize;
            int localSize;

            FmmKernel_getMultipoleArraySize(fmmCore, &multipoleSize);
            FmmKernel_getLocalArraySize(fmmCore, &localSize);

            do{
                octreeIterator.getCurrentCell()->createArrays(multipoleSize, localSize);
                octreeIterator.getCurrentCell()->setLevel(idxLevel);
                const FTreeCoordinate coord = octreeIterator.getCurrentCell()->getCoordinate();
602
                FPoint<FReal> position( coord.getX()*boxWidth + boxWidth/2.0 + corner.getX(),
BRAMAS Berenger's avatar
BRAMAS Berenger committed
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 653 654 655 656 657 658 659 660 661 662
                                 coord.getY()*boxWidth + boxWidth/2.0 + corner.getY(),
                                 coord.getZ()*boxWidth + boxWidth/2.0 + corner.getZ());
                octreeIterator.getCurrentCell()->setPosition(position.getDataValue());
            } while( octreeIterator.moveRight() );

            octreeIterator.moveUp();
            octreeIterator.gotoLeft();

            boxWidth *= FReal(2.0);
        }
    }
    { // Ceck if there is number of NbPart summed at level 1
        void* fmmKernel;
        FmmCore_getKernelData(fmmCore, &fmmKernel);

        int sizeOfFields;
        FmmKernel_getParameter(fmmKernel, FMMKERNEL_FIELD_DATA_SIZE, &sizeOfFields);

        typename OctreeClass::Iterator octreeIterator(corehandle->octree);
        octreeIterator.gotoBottomLeft();
        do{
            octreeIterator.getCurrentLeaf()->getTargets()->setParentCell(octreeIterator.getCurrentCell());
            octreeIterator.getCurrentLeaf()->getTargets()->allocateFields(sizeOfFields);
        } while( octreeIterator.moveRight() );
    }

    if( corehandle->config.nbThreads <= 1){
        CoreKernelClass kernels(fmmCore );
        FmmClass algo(corehandle->octree,&kernels);
        algo.execute();
    }
    else{
        CoreKernelClass kernels(fmmCore );
        FmmClassThread algo(corehandle->octree,&kernels);
        algo.execute();
    }

    return FMMAPI_NO_ERROR;
} /* réalise le produit multipôle. */

/* !!! Warning use *filed and not **field */
int FmmCore_getField(void *fmmCore, void *fields)  {
    ScalFmmCoreHandle* corehandle = (ScalFmmCoreHandle*)fmmCore;

    typename OctreeClass::Iterator octreeIterator(corehandle->octree);
    octreeIterator.gotoBottomLeft();
    do{
        CoreContainerClass* particles = octreeIterator.getCurrentLeaf()->getTargets();
        for(int idx = 0 ; idx < particles->getNbParticles() ; ++idx){
            memcpy(((char*)fields)+particles->getIndexes()[idx]*particles->getSizeOfField(),
                   ((char*)particles->getFields())+idx*particles->getSizeOfField(),
                   particles->getSizeOfField());
        }
    } while(octreeIterator.moveRight());

    return FMMAPI_NO_ERROR;
} /* récupère après le produit multipôle la valeur des champs en chaque point.
Le tableau field doit être alloué et libéré par la routine appelante. */