Mise à jour terminée. Pour connaître les apports de la version 13.8.4 par rapport à notre ancienne version vous pouvez lire les "Release Notes" suivantes :
https://about.gitlab.com/releases/2021/02/11/security-release-gitlab-13-8-4-released/
https://about.gitlab.com/releases/2021/02/05/gitlab-13-8-3-released/

FInterpMatrixKernel.hpp 28 KB
Newer Older
1
// ===================================================================================
2
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Berenger Bramas, Matthias Messner
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// 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".
// ===================================================================================
#ifndef FINTERPMATRIXKERNEL_HPP
#define FINTERPMATRIXKERNEL_HPP

19 20
#include <stdexcept>

COULAUD Olivier's avatar
COULAUD Olivier committed
21 22 23 24
#include "Utils/FPoint.hpp"
#include "Utils/FNoCopyable.hpp"
#include "Utils/FMath.hpp"
#include "Utils/FBlas.hpp"
25 26
#include "Utils/FGlobal.hpp"

27

28
// probably not extendable :)
29 30 31 32 33
enum KERNEL_FUNCTION_TYPE {HOMOGENEOUS, NON_HOMOGENEOUS};


/**
 * @author Matthias Messner (matthias.messner@inria.fr)
34
 * @author Pierre Blanchard (pierre.blanchard@inria.fr)
35 36
 * @class FInterpMatrixKernels
 * Please read the license
37
 *
38 39 40 41 42 43 44
 * This class provides the evaluators and scaling functions of the matrix
 * kernels. A matrix kernel should be understood in the sense of a kernel
 * of interaction (or the fundamental solution of a given equation).
 * It can either be scalar (NCMP=1) or tensorial (NCMP>1) depending on the
 * dimension of the equation considered. NCMP denotes the number of components
 * that are actually stored (e.g. 6 for a \f$3\times3\f$ symmetric tensor).
 * Notes on application scheme:
45
 * Let there be a kernel \f$K\f$ such that \f$X_i=K_{ij}Y_j\f$
46 47
 * with \f$X\f$ the lhs of size NLHS and \f$Y\f$ the rhs of size NRHS.
 * The table applyTab provides the indices in the reduced storage table
48
 * corresponding to the application scheme depicted earlier.
49
 *
50 51
 * PB: BEWARE! Homogeneous matrix kernels do not support cell width extension
 * yet. Is it possible to find a reference width and a scale factor such that
52
 * only 1 set of M2L ops can be used for all levels??
53
 *
54 55
 */
struct FInterpAbstractMatrixKernel : FNoCopyable
56
{ 
57 58 59 60 61
    virtual ~FInterpAbstractMatrixKernel(){} // to remove warning
    //virtual FReal evaluate(const FPoint&, const FPoint&) const = 0;
    // I need both functions because required arguments are not always given
    virtual FReal getScaleFactor(const FReal, const int) const = 0;
    virtual FReal getScaleFactor(const FReal) const = 0;
62 63 64 65 66 67 68
};



/// One over r
struct FInterpMatrixKernelR : FInterpAbstractMatrixKernel
{
69 70 71 72 73 74 75 76 77 78 79 80 81
    static const KERNEL_FUNCTION_TYPE Type = HOMOGENEOUS;
    static const unsigned int NCMP = 1; //< number of components
    static const unsigned int NPV  = 1; //< dim of physical values
    static const unsigned int NPOT = 1; //< dim of potentials
    static const unsigned int NRHS = 1; //< dim of mult exp
    static const unsigned int NLHS = 1; //< dim of loc exp

    FInterpMatrixKernelR() {}

    // copy ctor
    FInterpMatrixKernelR(const FInterpMatrixKernelR& /*other*/)
    {}

82 83
  static const char* getID() { return "ONE_OVER_R"; }

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 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
    // returns position in reduced storage
    int getPosition(const unsigned int) const
    {return 0;}

    // evaluate interaction
    template <class ValueClass>
    ValueClass evaluate(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                   const ValueClass& x2, const ValueClass& y2, const ValueClass& z2) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
        return FMath::One<ValueClass>() / FMath::Sqrt(diffx*diffx + diffy*diffy + diffz*diffz);
    }

    // evaluate interaction (blockwise)
    template <class ValueClass>
    void evaluateBlock(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                       const ValueClass& x2, const ValueClass& y2, const ValueClass& z2, ValueClass* block) const
    {
        block[0] = this->evaluate(x1,y1,z1,x2,y2,z2);
    }

    // evaluate interaction and derivative (blockwise)
    template <class ValueClass>
    void evaluateBlockAndDerivative(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                                    const ValueClass& x2, const ValueClass& y2, const ValueClass& z2,
                                    ValueClass block[1], ValueClass blockDerivative[3]) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
        const ValueClass one_over_r = FMath::One<ValueClass>() / FMath::Sqrt(diffx*diffx + diffy*diffy + diffz*diffz);

        const ValueClass one_over_r3 = one_over_r*one_over_r*one_over_r;

        block[0] = one_over_r;

        blockDerivative[0] = one_over_r3 * diffx;
        blockDerivative[1] = one_over_r3 * diffy;
        blockDerivative[2] = one_over_r3 * diffz;
    }

    FReal getScaleFactor(const FReal RootCellWidth, const int TreeLevel) const
    {
        const FReal CellWidth(RootCellWidth / FReal(FMath::pow(2, TreeLevel)));
        return getScaleFactor(CellWidth);
    }

    FReal getScaleFactor(const FReal CellWidth) const
    {
        return FReal(2.) / CellWidth;
    }
137

138 139 140 141 142 143 144 145 146 147
    FReal evaluate(const FPoint& p1, const FPoint& p2) const {
        return evaluate<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ());
    }
    void evaluateBlock(const FPoint& p1, const FPoint& p2, FReal* block) const{
        evaluateBlock<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block);
    }
    void evaluateBlockAndDerivative(const FPoint& p1, const FPoint& p2,
                                    FReal block[1], FReal blockDerivative[3]) const {
        evaluateBlockAndDerivative<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block, blockDerivative);
    }
148 149
};

COULAUD Olivier's avatar
COULAUD Olivier committed
150 151
/// One over r when the box size is rescaled to 1
struct FInterpMatrixKernelRH :FInterpMatrixKernelR{
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    static const KERNEL_FUNCTION_TYPE Type = HOMOGENEOUS;
    static const unsigned int NCMP = 1; //< number of components
    static const unsigned int NPV  = 1; //< dim of physical values
    static const unsigned int NPOT = 1; //< dim of potentials
    static const unsigned int NRHS = 1; //< dim of mult exp
    static const unsigned int NLHS = 1; //< dim of loc exp
    FReal LX,LY,LZ ;

    FInterpMatrixKernelRH() : LX(1.0),LY(1.0),LZ(1.0)
    {	 }

    // copy ctor
    FInterpMatrixKernelRH(const FInterpMatrixKernelRH& other)
        :FInterpMatrixKernelR(other), LX(other.LX), LY(other.LY), LZ(other.LZ)
    {}

168 169
  static const char* getID() { return "ONE_OVER_RH"; }

170 171 172 173 174 175 176 177 178 179 180 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 208 209 210
    // evaluate interaction
    template <class ValueClass>
    ValueClass evaluate(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                   const ValueClass& x2, const ValueClass& y2, const ValueClass& z2) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
        return FMath::One<ValueClass>() / FMath::Sqrt(FMath::ConvertTo<ValueClass,FReal>(LX)*diffx*diffx +
                                       FMath::ConvertTo<ValueClass,FReal>(LY)*diffy*diffy +
                                       FMath::ConvertTo<ValueClass,FReal>(LZ)*diffz*diffz);
    }
    void setCoeff(const FReal& a,  const FReal& b, const FReal& c)
    {LX= a*a ; LY = b*b ; LZ = c *c;}
    // returns position in reduced storage
    int getPosition(const unsigned int) const
    {return 0;}

    template <class ValueClass>
    void evaluateBlock(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                       const ValueClass& x2, const ValueClass& y2, const ValueClass& z2, ValueClass* block) const
    {
        block[0]=this->evaluate(x1,y1,z1,x2,y2,z2);
    }

    // evaluate interaction and derivative (blockwise)
    template <class ValueClass>
    void evaluateBlockAndDerivative(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                                    const ValueClass& x2, const ValueClass& y2, const ValueClass& z2,
                                    ValueClass block[1], ValueClass blockDerivative[3]) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
        const ValueClass one_over_rL = FMath::One<ValueClass>() / FMath::Sqrt(FMath::ConvertTo<ValueClass,FReal>(LX)*diffx*diffx +
                                                          FMath::ConvertTo<ValueClass,FReal>(LY)*diffy*diffy +
                                                          FMath::ConvertTo<ValueClass,FReal>(LZ)*diffz*diffz);
        const ValueClass one_over_rL3 = one_over_rL*one_over_rL*one_over_rL;

        block[0] = one_over_rL;

211 212 213
        blockDerivative[0] = LX * one_over_rL3 * diffx;
        blockDerivative[1] = LY * one_over_rL3 * diffy;
        blockDerivative[2] = LZ * one_over_rL3 * diffz;
214 215 216 217 218 219 220 221 222 223 224 225 226

    }

    FReal getScaleFactor(const FReal RootCellWidth, const int TreeLevel) const
    {
        const FReal CellWidth(RootCellWidth / FReal(FMath::pow(2, TreeLevel)));
        return getScaleFactor(CellWidth);
    }

    FReal getScaleFactor(const FReal CellWidth) const
    {
        return FReal(2.) / CellWidth;
    }
227

228 229 230 231 232 233 234 235 236 237
    FReal evaluate(const FPoint& p1, const FPoint& p2) const{
        return evaluate<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ());
    }
    void evaluateBlock(const FPoint& p1, const FPoint& p2, FReal* block) const{
        evaluateBlock<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block);
    }
    void evaluateBlockAndDerivative(const FPoint& p1, const FPoint& p2,
                                    FReal block[1], FReal blockDerivative[3]) const {
        evaluateBlockAndDerivative<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block, blockDerivative);
    }
COULAUD Olivier's avatar
COULAUD Olivier committed
238
};
239 240 241 242 243


/// One over r^2
struct FInterpMatrixKernelRR : FInterpAbstractMatrixKernel
{
244 245 246 247 248 249 250 251 252 253 254 255 256
    static const KERNEL_FUNCTION_TYPE Type = HOMOGENEOUS;
    static const unsigned int NCMP = 1; //< number of components
    static const unsigned int NPV  = 1; //< dim of physical values
    static const unsigned int NPOT = 1; //< dim of potentials
    static const unsigned int NRHS = 1; //< dim of mult exp
    static const unsigned int NLHS = 1; //< dim of loc exp

    FInterpMatrixKernelRR() {}

    // copy ctor
    FInterpMatrixKernelRR(const FInterpMatrixKernelRR& /*other*/)
    {}

257 258
  static const char* getID() { return "ONE_OVER_R_SQUARED"; }

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
    // returns position in reduced storage
    int getPosition(const unsigned int) const
    {return 0;}

    // evaluate interaction
    template <class ValueClass>
    ValueClass evaluate(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                   const ValueClass& x2, const ValueClass& y2, const ValueClass& z2) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
        return FMath::One<ValueClass>() / FReal(diffx*diffx +
                                 diffy*diffy +
                                 diffz*diffz);
    }

    // evaluate interaction (blockwise)
    template <class ValueClass>
    void evaluateBlock(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                       const ValueClass& x2, const ValueClass& y2, const ValueClass& z2, ValueClass* block) const
    {
        block[0]=this->evaluate(x1,y1,z1,x2,y2,z2);
    }

    // evaluate interaction and derivative (blockwise)
    template <class ValueClass>
    void evaluateBlockAndDerivative(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                                    const ValueClass& x2, const ValueClass& y2, const ValueClass& z2,
                                    ValueClass block[1], ValueClass blockDerivative[3]) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
        const ValueClass r2 = (diffx*diffx +
                               diffy*diffy +
                               diffz*diffz);
        const ValueClass one_over_r2 = FMath::One<ValueClass>() / (r2);
        const ValueClass one_over_r4 = one_over_r2*one_over_r2;

        block[0] = one_over_r2;

        const ValueClass coef = FMath::ConvertTo<ValueClass,FReal>(-2.) * one_over_r4;
302 303 304
        blockDerivative[0] = coef * diffx;
        blockDerivative[1] = coef * diffy;
        blockDerivative[2] = coef * diffz;
305 306 307 308 309 310 311 312 313 314 315 316 317

    }

    FReal getScaleFactor(const FReal RootCellWidth, const int TreeLevel) const
    {
        const FReal CellWidth(RootCellWidth / FReal(FMath::pow(2, TreeLevel)));
        return getScaleFactor(CellWidth);
    }

    FReal getScaleFactor(const FReal CellWidth) const
    {
        return FReal(4.) / (CellWidth*CellWidth);
    }
318 319 320 321 322 323 324 325 326 327 328

    FReal evaluate(const FPoint& p1, const FPoint& p2) const{
        return evaluate<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ());
    }
    void evaluateBlock(const FPoint& p1, const FPoint& p2, FReal* block) const{
        evaluateBlock<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block);
    }
    void evaluateBlockAndDerivative(const FPoint& p1, const FPoint& p2,
                                    FReal block[1], FReal blockDerivative[3]) const {
        evaluateBlockAndDerivative<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block, blockDerivative);
    }
329 330 331 332 333 334 335
};



/// One over r^12 - One over r^6
struct FInterpMatrixKernelLJ : FInterpAbstractMatrixKernel
{
336 337 338 339 340 341 342 343 344 345 346 347 348
    static const KERNEL_FUNCTION_TYPE Type = NON_HOMOGENEOUS;
    static const unsigned int NCMP = 1; //< number of components
    static const unsigned int NPV  = 1; //< dim of physical values
    static const unsigned int NPOT = 1; //< dim of potentials
    static const unsigned int NRHS = 1; //< dim of mult exp
    static const unsigned int NLHS = 1; //< dim of loc exp

    FInterpMatrixKernelLJ() {}

    // copy ctor
    FInterpMatrixKernelLJ(const FInterpMatrixKernelLJ& /*other*/)
    {}

349 350
  static const char* getID() { return "LENNARD_JONES_POTENTIAL"; }

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
    // returns position in reduced storage
    int getPosition(const unsigned int) const
    {return 0;}

    // evaluate interaction
    template <class ValueClass>
    ValueClass evaluate(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                   const ValueClass& x2, const ValueClass& y2, const ValueClass& z2) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
        const ValueClass r = FMath::Sqrt(diffx*diffx +
                                   diffy*diffy +
                                   diffz*diffz);
        const ValueClass r3 = r*r*r;
        const ValueClass one_over_r6 = FMath::One<ValueClass>() / (r3*r3);
        //return one_over_r6 * one_over_r6;
        //return one_over_r6;
        return one_over_r6 * one_over_r6 - one_over_r6;
    }

    // evaluate interaction (blockwise)
    template <class ValueClass>
    void evaluateBlock(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                       const ValueClass& x2, const ValueClass& y2, const ValueClass& z2, ValueClass* block) const
    {
        block[0]=this->evaluate(x1,y1,z1,x2,y2,z2);
    }

    // evaluate interaction and derivative (blockwise)
    template <class ValueClass>
    void evaluateBlockAndDerivative(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                                    const ValueClass& x2, const ValueClass& y2, const ValueClass& z2,
                                    ValueClass block[1], ValueClass blockDerivative[3]) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
        const ValueClass r = FMath::Sqrt(diffx*diffx +
                                    diffy*diffy +
                                    diffz*diffz);
        const ValueClass r2 = r*r;
        const ValueClass r3 = r2*r;
        const ValueClass one_over_r6 = FMath::One<ValueClass>() / (r3*r3);
        const ValueClass one_over_r8 = one_over_r6 / (r2);

        block[0] = one_over_r6 * one_over_r6 - one_over_r6;

        const FReal coef = FMath::ConvertTo<ValueClass,FReal>(12.0)*one_over_r6*one_over_r8 - FMath::ConvertTo<ValueClass,FReal>(6.0)*one_over_r8;
        blockDerivative[0]= coef * diffx;
        blockDerivative[1]= coef * diffy;
        blockDerivative[2]= coef * diffz;

    }

    FReal getScaleFactor(const FReal, const int) const
    {
        // return 1 because non homogeneous kernel functions cannot be scaled!!!
        return FReal(1.0);
    }

    FReal getScaleFactor(const FReal) const
    {
        // return 1 because non homogeneous kernel functions cannot be scaled!!!
        return FReal(1.0);
    }
418

419 420 421 422 423 424 425 426 427 428 429 430


    FReal evaluate(const FPoint& p1, const FPoint& p2) const{
        return evaluate<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ());
    }
    void evaluateBlock(const FPoint& p1, const FPoint& p2, FReal* block) const{
        evaluateBlock<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block);
    }
    void evaluateBlockAndDerivative(const FPoint& p1, const FPoint& p2,
                                    FReal block[1], FReal blockDerivative[3]) const {
        evaluateBlockAndDerivative<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block, blockDerivative);
    }
431 432
};

433 434 435
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
436
// Tensorial Matrix Kernels (NCMP>1)
437
//
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
// The definition of the potential p and force f are extended to the case
// of tensorial interaction kernels:
// p_i(x) = K_{ip}(x,y)w_p(y), \forall i=1..NPOT, p=1..NPV
// f_{ik}= w_p(x)K_{ip,k}(x,y)w_p(y) "
//
// Since the interpolation scheme is such that
// p_i(x) \approx S^m(x) L^{m}_{ip}
// f_{ik}= w_p(x) \nabla_k S^m(x) L^{m}_{ip}
// with
// L^{m}_{ip} = K^{mn}_{ip} S^n(y) w_p(y) (local expansion)
// M^{m}_{p} = S^n(y) w_p(y) (multipole expansion)
// then the multipole exp have NPV components and the local exp NPOT*NPV.
//
// NB1: Only the computation of forces requires that the sum over p is 
// performed at L2P step. It could be done at M2L step for the potential.
//
// NB2: An efficient application of the matrix kernel is highly kernel 
// dependent, we recommand overriding the P2M/M2L/L2P function of the kernel 
// you are using in order to have optimal performances + set your own NRHS/NLHS.
//
458 459 460
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

461 462

/// R_{,ij}
463 464
// PB: IMPORTANT! This matrix kernel does not present the symmetries 
// required by ChebSym kernel => only suited for Unif interpolation
465 466
struct FInterpMatrixKernel_R_IJ : FInterpAbstractMatrixKernel
{
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
    static const KERNEL_FUNCTION_TYPE Type = NON_HOMOGENEOUS;
    static const unsigned int NK   = 3*3; //< total number of components
    static const unsigned int NCMP = 6;   //< number of components
    static const unsigned int NPV  = 3;   //< dim of physical values
    static const unsigned int NPOT = 3;   //< dim of potentials
    static const unsigned int NRHS = NPV; //< dim of mult exp
    static const unsigned int NLHS = NPOT*NRHS; //< dim of loc exp

    // store indices (i,j) corresponding to sym idx
    static const unsigned int indexTab[/*2*NCMP=12*/];

    // store positions in sym tensor (when looping over NRHSxNLHS)
    static const unsigned int applyTab[/*NK=9*/];

    // indices to be set at construction if component-wise evaluation is performed
    const unsigned int _i,_j;

    // Material Parameters
    const FReal _CoreWidth2; // if >0 then kernel is NON homogeneous

    FInterpMatrixKernel_R_IJ(const FReal CoreWidth = 0.0, const unsigned int d = 0)
        : _i(indexTab[d]), _j(indexTab[d+NCMP]), _CoreWidth2(CoreWidth*CoreWidth)
    {}

    // copy ctor
    FInterpMatrixKernel_R_IJ(const FInterpMatrixKernel_R_IJ& other)
        : _i(other._i), _j(other._j), _CoreWidth2(other._CoreWidth2)
    {}

496 497
  static const char* getID() { return "R_IJ"; }

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 538 539 540 541 542 543 544
    // returns position in reduced storage from position in full 3x3 matrix
    unsigned  int getPosition(const unsigned int n) const
    {return applyTab[n];}

    // returns Core Width squared
    FReal getCoreWidth2() const
    {return _CoreWidth2;}

    // evaluate interaction
    template <class ValueClass>
    FReal evaluate(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                   const ValueClass& x2, const ValueClass& y2, const ValueClass& z2) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
        const ValueClass one_over_r = FMath::One<ValueClass>()/FMath::Sqrt(diffx*diffx +
                                                diffy*diffy +
                                                diffz*diffz + FMath::ConvertTo<ValueClass,FReal>(_CoreWidth2));
        const ValueClass one_over_r3 = one_over_r*one_over_r*one_over_r;
        ValueClass ri,rj;

        if(_i==0) ri=diffx;
        else if(_i==1) ri=diffy;
        else if(_i==2) ri=diffz;
        else throw std::runtime_error("Update i!");

        if(_j==0) rj=diffx;
        else if(_j==1) rj=diffy;
        else if(_j==2) rj=diffz;
        else throw std::runtime_error("Update j!");

        if(_i==_j)
            return one_over_r - ri * ri * one_over_r3;
        else
            return - ri * rj * one_over_r3;

    }

    // evaluate interaction (blockwise)
    template <class ValueClass>
    void evaluateBlock(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                       const ValueClass& x2, const ValueClass& y2, const ValueClass& z2, ValueClass* block) const
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
545
        const ValueClass one_over_r = FMath::One<ValueClass>()/FMath::Sqrt(diffx*diffx +
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
                                                diffy*diffy +
                                                diffz*diffz + FMath::ConvertTo<ValueClass,FReal>(_CoreWidth2));
        const ValueClass one_over_r3 = one_over_r*one_over_r*one_over_r;
        const ValueClass r[3] = {diffx,diffy,diffz};

        for(unsigned int d=0;d<NCMP;++d){
            unsigned int i = indexTab[d];
            unsigned int j = indexTab[d+NCMP];

            if(i==j)
                block[d] = one_over_r - r[i] * r[i] * one_over_r3;
            else
                block[d] = - r[i] * r[j] * one_over_r3;
        }
    }

562
    // evaluate interaction and derivative (blockwise)
563
    template <class ValueClass>
564 565 566
    void evaluateBlockAndDerivative(const ValueClass& x1, const ValueClass& y1, const ValueClass& z1,
                                    const ValueClass& x2, const ValueClass& y2, const ValueClass& z2,
                                    ValueClass block[NCMP], ValueClass blockDerivative[NCMP][3]) const
567 568 569 570
    {
        const ValueClass diffx = (x1-x2);
        const ValueClass diffy = (y1-y2);
        const ValueClass diffz = (z1-z2);
571 572 573
        const ValueClass r2[3] = {diffx*diffx,diffy*diffy,diffz*diffz};
        const ValueClass one_over_r2 = FMath::One<ValueClass>() / (r2[0] + r2[1] + r2[2]);
        const ValueClass one_over_r = FMath::Sqrt(one_over_r2);
574 575 576 577
        const ValueClass one_over_r3 = one_over_r2*one_over_r;

        const ValueClass r[3] = {diffx,diffy,diffz};

578 579 580
        const ValueClass Three = FMath::ConvertTo<ValueClass,FReal>(3.);
        const ValueClass MinusOne = -FMath::One<ValueClass>();

581 582 583 584
        for(unsigned int d=0;d<NCMP;++d){
            unsigned int i = indexTab[d];
            unsigned int j = indexTab[d+NCMP];

585 586 587 588 589 590 591 592 593
            // evaluate kernel
            if(i==j)
                block[d] = one_over_r - r2[i] * one_over_r3;
            else
                block[d] = - r[i] * r[j] * one_over_r3;

            // evaluate derivative
            for(unsigned int k = 0 ; k < 3 ; ++k){
              if(i==j){
594
                if(j==k) //i=j=k
595
                  blockDerivative[d][k] = Three * ( MinusOne + r2[i] * one_over_r2 ) * r[i] * one_over_r3;
596
                else //i=j!=k
597 598 599
                  blockDerivative[d][k] = ( MinusOne + Three * r2[i] * one_over_r2 ) * r[k] * one_over_r3;
              }
              else{ //(i!=j)
600
                if(i==k) //i=k!=j
601
                  blockDerivative[d][k] = ( MinusOne + Three * r2[i] * one_over_r2 ) * r[j] * one_over_r3;
602
                else if(j==k) //i!=k=j
603
                  blockDerivative[d][k] = ( MinusOne + Three * r2[j] * one_over_r2 ) * r[i] * one_over_r3;
604
                else //i!=k!=j
605 606 607 608 609
                  blockDerivative[d][k] = Three * r[i] * r[j] * r[k] * one_over_r2 * one_over_r3;
              }
            }// k

        }// NCMP
610 611 612 613 614 615 616 617
    }

    FReal getScaleFactor(const FReal RootCellWidth, const int TreeLevel) const
    {
        const FReal CellWidth(RootCellWidth / FReal(FMath::pow(2, TreeLevel)));
        return getScaleFactor(CellWidth);
    }

618 619
    // R_{,ij} is homogeneous to [L]/[L]^{-2}=[L]^{-1}
    // => scales like ONE_OVER_R
620 621
    FReal getScaleFactor(const FReal CellWidth) const
    {
622
        return FReal(2.) / CellWidth;
623
    }
624

625

626

627 628 629 630 631 632
    FReal evaluate(const FPoint& p1, const FPoint& p2) const{
        return evaluate<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ());
    }
    void evaluateBlock(const FPoint& p1, const FPoint& p2, FReal* block) const{
        evaluateBlock<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block);
    }
633 634 635 636
    void evaluateBlockAndDerivative(const FPoint& p1, const FPoint& p2,
                                    FReal block[NCMP], FReal blockDerivative[NCMP][3]) const {
        evaluateBlockAndDerivative<FReal>(p1.getX(), p1.getY(), p1.getZ(), p2.getX(), p2.getY(), p2.getZ(), block, blockDerivative);
    }
637
};
638 639 640 641 642 643 644



/*!  Functor which provides the interface to assemble a matrix based on the
  number of rows and cols and on the coordinates x and y and the type of the
  generating matrix-kernel function.
*/
645
template <typename MatrixKernelClass>
646 647
class EntryComputer
{
648
    const MatrixKernelClass *const MatrixKernel;
649

650 651
    const unsigned int nx, ny;
    const FPoint *const px, *const py;
652

653
    const FReal *const weights;
654 655

public:
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
    explicit EntryComputer(const MatrixKernelClass *const inMatrixKernel,
                           const unsigned int _nx, const FPoint *const _px,
                           const unsigned int _ny, const FPoint *const _py,
                           const FReal *const _weights = NULL)
        : MatrixKernel(inMatrixKernel),	nx(_nx), ny(_ny), px(_px), py(_py), weights(_weights) {}

    void operator()(const unsigned int xbeg, const unsigned int xend,
                    const unsigned int ybeg, const unsigned int yend,
                    FReal *const data) const
    {
        unsigned int idx = 0;
        if (weights) {
            for (unsigned int j=ybeg; j<yend; ++j)
                for (unsigned int i=xbeg; i<xend; ++i)
                    data[idx++] = weights[i] * weights[j] * MatrixKernel->evaluate(px[i], py[j]);
        } else {
            for (unsigned int j=ybeg; j<yend; ++j)
                for (unsigned int i=xbeg; i<xend; ++i)
                    data[idx++] = MatrixKernel->evaluate(px[i], py[j]);
        }

        /*
678 679 680 681 682 683 684 685 686 687 688 689 690 691
    // apply weighting matrices
    if (weights) {
    if ((xend-xbeg) == (yend-ybeg) && (xend-xbeg) == nx)
    for (unsigned int n=0; n<nx; ++n) {
    FBlas::scal(nx, weights[n], data + n,  nx); // scale rows
    FBlas::scal(nx, weights[n], data + n * nx); // scale cols
    }
    else if ((xend-xbeg) == 1 && (yend-ybeg) == ny)
    for (unsigned int j=0; j<ny; ++j)	data[j] *= weights[j];
    else if ((yend-ybeg) == 1 && (xend-xbeg) == nx)
    for (unsigned int i=0; i<nx; ++i)	data[i] *= weights[i];
    }
    */

692
    }
693 694 695 696 697 698 699 700 701
};





#endif // FINTERPMATRIXKERNEL_HPP

// [--END--]