FFTPoissonSolver.cpp 25.2 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// -*- C++ -*-
/***************************************************************************
 *
 *
 * FFTPoissonSolver.cc
 *
 *
 *
 *
 *
 *
 *
 ***************************************************************************/

// include files
kraus's avatar
kraus committed
16
#include "Solvers/FFTPoissonSolver.h"
gsell's avatar
gsell committed
17 18 19 20 21 22 23 24 25 26
#include "Algorithms/PartBunch.h"
#include "Physics/Physics.h"
#include <fstream>
//////////////////////////////////////////////////////////////////////////////
// a little helper class to specialize the action of the Green's function
// calculation.  This should be specialized for each dimension
// to the proper action for computing the Green's function.  The first
// template parameter is the full type of the Field to compute, and the second
// is the dimension of the data, which should be specialized.

27 28 29 30
#ifdef OPAL_NOCPLUSPLUS11_NULLPTR
#define nullptr NULL
#endif

gsell's avatar
gsell committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
template<unsigned int Dim>
struct SpecializedGreensFunction { };

template<>
struct SpecializedGreensFunction<3> {
    template<class T, class FT, class FT2>
    static void calculate(Vektor<T, 3> &hrsq, FT &grn, FT2 *grnI) {
        grn = grnI[0] * hrsq[0] + grnI[1] * hrsq[1] + grnI[2] * hrsq[2];
        grn = 1.0 / sqrt(grn);
        grn[0][0][0] = grn[0][0][1];
    }
};

////////////////////////////////////////////////////////////////////////////

// constructor


49
FFTPoissonSolver::FFTPoissonSolver(Mesh_t *mesh, FieldLayout_t *fl, std::string greensFunction, std::string bcz):
gsell's avatar
gsell committed
50 51
    mesh_m(mesh),
    layout_m(fl),
52 53 54 55
    mesh2_m(nullptr),
    layout2_m(nullptr),
    mesh3_m(nullptr),
    layout3_m(nullptr),
56 57
    mesh4_m(nullptr),
    layout4_m(nullptr),
gsell's avatar
gsell committed
58 59
    greensFunction_m(greensFunction) {
    int i;
kraus's avatar
kraus committed
60

adelmann's avatar
adelmann committed
61
    bcz_m = (bcz==std::string("PERIODIC"));   // for DC beams, the z direction has periodic boundary conditions
kraus's avatar
kraus committed
62

gsell's avatar
gsell committed
63 64 65 66 67 68 69 70 71 72 73
    domain_m = layout_m->getDomain();

    // For efficiency in the FFT's, we can use a parallel decomposition
    // which can be serial in the first dimension.
    e_dim_tag decomp[3];
    e_dim_tag decomp2[3];
    for(int d = 0; d < 3; ++d) {
        decomp[d] = layout_m->getRequestedDistribution(d);
        decomp2[d] = layout_m->getRequestedDistribution(d);
    }

adelmann's avatar
adelmann committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    if (bcz_m) {
      // The FFT's require double-sized field sizes in order to
      // simulate an isolated system.  The FFT of the charge density field, rho,
      // would otherwise mimic periodic boundary conditions, i.e. as if there were
      // several beams set a periodic distance apart.  The double-sized fields in x and
      // alleviate this problem, in z we have periodic BC's
      for(i = 0; i < 2; i++) {
        hr_m[i] = mesh_m->get_meshSpacing(i);
        nr_m[i] = domain_m[i].length();
        domain2_m[i] = Index(2 * nr_m[i] + 1);
      }

      hr_m[2] = mesh_m->get_meshSpacing(2);
      nr_m[2] = domain_m[2].length();
      domain2_m[2] = Index(2 * nr_m[2] + 1);

      for(i = 0; i < 2 * 3; ++i) {
        bc_m[i] = new ZeroFace<double, 3, Mesh_t, Center_t>(i);
        vbc_m[i] = new ZeroFace<Vector_t, 3, Mesh_t, Center_t>(i);
kraus's avatar
kraus committed
93
      }
adelmann's avatar
adelmann committed
94 95 96 97 98
      // z-direction
      bc_m[4] = new ParallelPeriodicFace<double,3,Mesh_t,Center_t>(4);
      bc_m[5] = new ParallelPeriodicFace<double,3,Mesh_t,Center_t>(5);
      vbc_m[4] = new ZeroFace<Vector_t, 3, Mesh_t, Center_t>(4);
      vbc_m[5] = new ZeroFace<Vector_t, 3, Mesh_t, Center_t>(5);
kraus's avatar
kraus committed
99
    }
adelmann's avatar
adelmann committed
100 101 102 103 104 105 106
    else {
      // The FFT's require double-sized field sizes in order to
      // simulate an isolated system.  The FFT of the charge density field, rho,
      // would otherwise mimic periodic boundary conditions, i.e. as if there were
      // several beams set a periodic distance apart.  The double-sized fields
      // alleviate this problem.
      for(i = 0; i < 3; i++) {
gsell's avatar
gsell committed
107 108 109
        hr_m[i] = mesh_m->get_meshSpacing(i);
        nr_m[i] = domain_m[i].length();
        domain2_m[i] = Index(2 * nr_m[i] + 1);
adelmann's avatar
adelmann committed
110 111 112 113 114
      }

      for(i = 0; i < 2 * 3; ++i) {
        bc_m[i] = new ZeroFace<double, 3, Mesh_t, Center_t>(i);
        vbc_m[i] = new ZeroFace<Vector_t, 3, Mesh_t, Center_t>(i);
kraus's avatar
kraus committed
115
      }
gsell's avatar
gsell committed
116 117 118
    }

    // create double sized mesh and layout objects for the use in the FFT's
119 120
    mesh2_m = std::unique_ptr<Mesh_t>(new Mesh_t(domain2_m));
    layout2_m = std::unique_ptr<FieldLayout_t>(new FieldLayout_t(*mesh2_m, decomp));
gsell's avatar
gsell committed
121 122
    rho2_m.initialize(*mesh2_m, *layout2_m);

adelmann's avatar
adelmann committed
123

gsell's avatar
gsell committed
124 125 126 127 128 129 130 131 132 133 134 135
    NDIndex<3> tmpdomain;
    // Create the domain for the transformed (complex) fields.  Do this by
    // taking the domain from the doubled mesh, permuting it to the right, and
    // setting the 2nd dimension to have n/2 + 1 elements.
    domain3_m[0] = Index(2 * nr_m[3-1] + 1);
    domain3_m[1] = Index(nr_m[0] + 2);

    for(i = 2; i < 3; ++i)
        domain3_m[i] = Index(2 * nr_m[i-1] + 1);

    // create mesh and layout for the new real-to-complex FFT's, for the
    // complex transformed fields
136 137
    mesh3_m = std::unique_ptr<Mesh_t>(new Mesh_t(domain3_m));
    layout3_m = std::unique_ptr<FieldLayout_t>(new FieldLayout_t(*mesh3_m, decomp2));
kraus's avatar
kraus committed
138

gsell's avatar
gsell committed
139 140 141 142 143 144 145
    rho2tr_m.initialize(*mesh3_m, *layout3_m);
    imgrho2tr_m.initialize(*mesh3_m, *layout3_m);
    grntr_m.initialize(*mesh3_m, *layout3_m);

    // helper field for sin
    greentr_m.initialize(*mesh3_m, *layout3_m);

146 147 148 149 150 151 152 153
    for(i = 0; i < 3; i++) {
        domain4_m[i] = Index(nr_m[i] + 2);
    }
    mesh4_m = std::unique_ptr<Mesh_t>(new Mesh_t(domain4_m));
    layout4_m = std::unique_ptr<FieldLayout_t>(new FieldLayout_t(*mesh4_m, decomp));

    tmpgreen.initialize(*mesh4_m, *layout4_m);

gsell's avatar
gsell committed
154 155 156 157 158 159 160 161
    // create a domain used to indicate to the FFT's how to construct it's
    // temporary fields.  This is the same as the complex field's domain,
    // but permuted back to the left.
    tmpdomain = layout3_m->getDomain();
    for(i = 0; i < 3; ++i)
        domainFFTConstruct_m[i] = tmpdomain[(i+1) % 3];

    // create the FFT object
162
    fft_m = std::unique_ptr<FFT_t>(new FFT_t(layout2_m->getDomain(), domainFFTConstruct_m));
gsell's avatar
gsell committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

    // these are fields that are used for calculating the Green's function.
    // they eliminate some calculation at each time-step.
    for(i = 0; i < 3; ++i) {
        grnIField_m[i].initialize(*mesh2_m, *layout2_m);
        grnIField_m[i][domain2_m] = where(lt(domain2_m[i], nr_m[i]),
                                          domain2_m[i] * domain2_m[i],
                                          (2 * nr_m[i] - domain2_m[i]) *
                                          (2 * nr_m[i] - domain2_m[i]));
    }

    GreensFunctionTimer_m = IpplTimings::getTimer("GreensFTotal");

    IntGreensFunctionTimer1_m = IpplTimings::getTimer("IntGreenF1");
    IntGreensFunctionTimer2_m = IpplTimings::getTimer("IntGreenF2");
    IntGreensFunctionTimer3_m = IpplTimings::getTimer("IntGreenF3");
179
    IntGreensMirrorTimer1_m = IpplTimings::getTimer("MirrorRho1");
gsell's avatar
gsell committed
180 181 182 183 184

    ShIntGreensFunctionTimer1_m = IpplTimings::getTimer("ShIntGreenF1");
    ShIntGreensFunctionTimer2_m = IpplTimings::getTimer("ShIntGreenF2");
    ShIntGreensFunctionTimer3_m = IpplTimings::getTimer("ShIntGreenF3");
    ShIntGreensFunctionTimer4_m = IpplTimings::getTimer("ShIntGreenF4");
185
    IntGreensMirrorTimer2_m = IpplTimings::getTimer("MirrorRho2");
gsell's avatar
gsell committed
186 187 188 189 190 191 192 193 194 195 196

    GreensFunctionTimer1_m = IpplTimings::getTimer("GreenF1");
    GreensFunctionTimer2_m = IpplTimings::getTimer("GreenF2");
    GreensFunctionTimer3_m = IpplTimings::getTimer("GreenF3");
    GreensFunctionTimer4_m = IpplTimings::getTimer("GreenF4");
}


FFTPoissonSolver::FFTPoissonSolver(PartBunch &beam, std::string greensFunction):
    mesh_m(&beam.getMesh()),
    layout_m(&beam.getFieldLayout()),
197 198 199 200
    mesh2_m(nullptr),
    layout2_m(nullptr),
    mesh3_m(nullptr),
    layout3_m(nullptr),
201 202
    mesh4_m(nullptr),
    layout4_m(nullptr),
gsell's avatar
gsell committed
203 204 205 206
    greensFunction_m(greensFunction) {
    int i;
    domain_m = layout_m->getDomain();

adelmann's avatar
adelmann committed
207 208 209 210
    INFOMSG("FFTPoissonSolver(PartBunch &beam ..." << endl;);



gsell's avatar
gsell committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    // For efficiency in the FFT's, we can use a parallel decomposition
    // which can be serial in the first dimension.
    e_dim_tag decomp[3];
    e_dim_tag decomp2[3];
    for(int d = 0; d < 3; ++d) {
        decomp[d] = layout_m->getRequestedDistribution(d);
        decomp2[d] = layout_m->getRequestedDistribution(d);
    }

    // The FFT's require double-sized field sizes in order to (more closely
    // do not understand this ...)
    // simulate an isolated system.  The FFT of the charge density field, rho,
    // would otherwise mimic periodic boundary conditions, i.e. as if there were
    // several beams set a periodic distance apart.  The double-sized fields
    // alleviate this problem.
    for(i = 0; i < 3; i++) {
        hr_m[i] = mesh_m->get_meshSpacing(i);
        nr_m[i] = domain_m[i].length();
        domain2_m[i] = Index(2 * nr_m[i] + 1);
    }
    // create double sized mesh and layout objects for the use in the FFT's
232 233
    mesh2_m = std::unique_ptr<Mesh_t>(new Mesh_t(domain2_m));
    layout2_m = std::unique_ptr<FieldLayout_t>(new FieldLayout_t(*mesh2_m, decomp));
gsell's avatar
gsell committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247
    rho2_m.initialize(*mesh2_m, *layout2_m);

    NDIndex<3> tmpdomain;
    // Create the domain for the transformed (complex) fields.  Do this by
    // taking the domain from the doubled mesh, permuting it to the right, and
    // setting the 2nd dimension to have n/2 + 1 elements.
    domain3_m[0] = Index(2 * nr_m[3-1] + 1);
    domain3_m[1] = Index(nr_m[0] + 2);

    for(i = 2; i < 3; ++i)
        domain3_m[i] = Index(2 * nr_m[i-1] + 1);

    // create mesh and layout for the new real-to-complex FFT's, for the
    // complex transformed fields
248 249
    mesh3_m = std::unique_ptr<Mesh_t>(new Mesh_t(domain3_m));
    layout3_m = std::unique_ptr<FieldLayout_t>(new FieldLayout_t(*mesh3_m, decomp2));
gsell's avatar
gsell committed
250 251 252 253 254 255 256
    rho2tr_m.initialize(*mesh3_m, *layout3_m);
    imgrho2tr_m.initialize(*mesh3_m, *layout3_m);
    grntr_m.initialize(*mesh3_m, *layout3_m);

    // helper field for sin
    greentr_m.initialize(*mesh3_m, *layout3_m);

257 258 259 260 261 262 263 264
    for(i = 0; i < 3; i++) {
        domain4_m[i] = Index(nr_m[i] + 2);
    }
    mesh4_m = std::unique_ptr<Mesh_t>(new Mesh_t(domain4_m));
    layout4_m = std::unique_ptr<FieldLayout_t>(new FieldLayout_t(*mesh4_m, decomp));

    tmpgreen.initialize(*mesh4_m, *layout4_m);

gsell's avatar
gsell committed
265 266 267 268 269 270 271 272
    // create a domain used to indicate to the FFT's how to construct it's
    // temporary fields.  This is the same as the complex field's domain,
    // but permuted back to the left.
    tmpdomain = layout3_m->getDomain();
    for(i = 0; i < 3; ++i)
        domainFFTConstruct_m[i] = tmpdomain[(i+1) % 3];

    // create the FFT object
273
    fft_m = std::unique_ptr<FFT_t>(new FFT_t(layout2_m->getDomain(), domainFFTConstruct_m));
gsell's avatar
gsell committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

    // these are fields that are used for calculating the Green's function.
    // they eliminate some calculation at each time-step.
    for(i = 0; i < 3; ++i) {
        grnIField_m[i].initialize(*mesh2_m, *layout2_m);
        grnIField_m[i][domain2_m] = where(lt(domain2_m[i], nr_m[i]),
                                          domain2_m[i] * domain2_m[i],
                                          (2 * nr_m[i] - domain2_m[i]) *
                                          (2 * nr_m[i] - domain2_m[i]));
    }
}

////////////////////////////////////////////////////////////////////////////
// destructor
FFTPoissonSolver::~FFTPoissonSolver() {
    // delete the FFT object
290
    //~ delete fft_m;
gsell's avatar
gsell committed
291 292

    // delete the mesh and layout objects
293 294 295 296
    //~ if(mesh2_m != 0)     delete mesh2_m;
    //~ if(layout2_m != 0)   delete layout2_m;
    //~ if(mesh3_m != 0)     delete mesh3_m;
    //~ if(layout3_m != 0)   delete layout3_m;
gsell's avatar
gsell committed
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 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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
}

////////////////////////////////////////////////////////////////////////////
// given a charge-density field rho and a set of mesh spacings hr,
// compute the electric potential from the image charge by solving
// the Poisson's equation

void FFTPoissonSolver::computePotential(Field_t &rho, Vector_t hr, double zshift) {
    // use grid of complex doubled in both dimensions
    // and store rho in lower left quadrant of doubled grid
    rho2_m = 0.0;

    rho2_m[domain_m] = rho[domain_m];

    // needed in greens function
    hr_m = hr;
    // FFT double-sized charge density
    // we do a backward transformation so that we dont have to account for the normalization factor
    // that is used in the forward transformation of the IPPL FFT
    fft_m->transform(-1, rho2_m, rho2tr_m);

    // must be called if the mesh size has changed
    // have to check if we can do G with h = (1,1,1)
    // and rescale later

    // Do image charge.
    // The minus sign is due to image charge.
    // Convolute transformed charge density with shifted green's function.
    IpplTimings::startTimer(GreensFunctionTimer_m);
    shiftedIntGreensFunction(zshift);
    IpplTimings::stopTimer(GreensFunctionTimer_m);

    // Multiply transformed charge density and
    // transformed Green's function. Don't divide
    // by (2*nx_m)*(2*ny_m), as Ryne does; this
    // normalization is done in POOMA's fft routine.
    imgrho2tr_m = - rho2tr_m * grntr_m;

    // Inverse FFT to find image charge potential, rho2_m equals the electrostatic potential.
    fft_m->transform(+1, imgrho2tr_m, rho2_m);

    // Re-use rho to store image potential. Flip z coordinate since this is a mirror image.
    Index I = nr_m[0];
    Index J = nr_m[1];
    Index K = nr_m[2];
    rho[I][J][K] = rho2_m[I][J][nr_m[2] - K - 1];
}


////////////////////////////////////////////////////////////////////////////
// given a charge-density field rho and a set of mesh spacings hr,
// compute the electric field and put in eg by solving the Poisson's equation

void FFTPoissonSolver::computePotential(Field_t &rho, Vector_t hr) {
    // use grid of complex doubled in both dimensions
    // and store rho in lower left quadrant of doubled grid
    rho2_m = 0.0;

    rho2_m[domain_m] = rho[domain_m];

    // needed in greens function
    hr_m = hr;
    // FFT double-sized charge density
    // we do a backward transformation so that we dont have to account for the normalization factor
    // that is used in the forward transformation of the IPPL FFT
    fft_m->transform(-1, rho2_m, rho2tr_m);

    // must be called if the mesh size has changed
    // have to check if we can do G with h = (1,1,1)
    // and rescale later
    IpplTimings::startTimer(GreensFunctionTimer_m);
    if(greensFunction_m == std::string("INTEGRATED"))
        integratedGreensFunction();
    else
        greensFunction();
    IpplTimings::stopTimer(GreensFunctionTimer_m);
    // multiply transformed charge density
    // and transformed Green function
    // Don't divide by (2*nx_m)*(2*ny_m), as Ryne does;
    // this normalization is done in POOMA's fft routine.
    rho2tr_m *= grntr_m;

    // inverse FFT, rho2_m equals to the electrostatic potential
    fft_m->transform(+1, rho2tr_m, rho2_m);
    // end convolution

    // back to physical grid
    // reuse the charge density field to store the electrostatic potential
    rho[domain_m] = rho2_m[domain_m];
}
///////////////////////////////////////////////////////////////////////////
// calculate the FFT of the Green's function for the given field
void FFTPoissonSolver::greensFunction() {

    //hr_m[0]=hr_m[1]=hr_m[2]=1;

    Vector_t hrsq(hr_m * hr_m);
    IpplTimings::startTimer(GreensFunctionTimer1_m);
    SpecializedGreensFunction<3>::calculate(hrsq, rho2_m, grnIField_m);
    IpplTimings::stopTimer(GreensFunctionTimer1_m);
    // Green's function calculation complete at this point.
    // The next step is to FFT it.
    // FFT of Green's function

    //  ofstream fstr;
    //    fstr.precision(9);
    //    fstr.open("green",ios::out);

    //    for (int i=0;i<domain2_m[0].length();i++)
    //      fstr << i << " " << rho2_m[0][0][i] << endl;
    //    fstr.close();

    //IFF: +1 is forward
    // we do a backward transformation so that we dont have to account for the normalization factor
    // that is used in the forward transformation of the IPPL FFT
    IpplTimings::startTimer(GreensFunctionTimer4_m);
    fft_m->transform(-1, rho2_m, grntr_m);
    IpplTimings::stopTimer(GreensFunctionTimer4_m);

    //    ofstream fstr2;
    //    fstr2.precision(9);
    //    fstr2.open("green_fft",ios::out);
    //    for (int i=0;i<domain2_m[0].length();i++)
    //      fstr2 << i << " " << grntr_m[0][0][i] << endl;
    //    fstr2.close();
}

/** If the beam has a longitudinal size >> transverse size the
  * direct Green function at each mesh point is not efficient
  * (needs a lot of mesh points along the transverse size to
  * get a good resolution)
  *
  * If the charge density function is uniform within each cell
  * the following Green's function can be defined:
  *
  * \f[ \overline{G}(x_i - x_{i'}, y_j - y_{j'}, z_k - z_{k'}  cout << I << endl;
  cout << J << endl;
  cout << K << endl;
  cout << IE << endl;
  cout << JE << endl;
  cout << KE << endl;

) = \int_{x_{i'} - h_x/2}^{x_{i'} + h_x/2} dx' \int_{y_{j'} - h_y/2}^{y_{j'} + h_y/2} dy' \int_{z_{k'} - h_z/2}^{z_{k'} + h_z/2} dz' G(x_i - x_{i'}, y_j - y_{j'}, z_k - z_{k'}).
  * \f]
  */
void FFTPoissonSolver::integratedGreensFunction() {

444 445
    NDIndex<3> idx =  layout4_m->getLocalNDIndex();
    double cellVolume = hr_m[0] * hr_m[1] * hr_m[2];
gsell's avatar
gsell committed
446 447 448 449 450 451 452
    tmpgreen = 0.0;

    IpplTimings::startTimer(IntGreensFunctionTimer1_m);

    /**
      * This integral can be calculated analytically in a closed from:
      */
453 454 455
    for(int k = idx[2].first(); k <= idx[2].last() + 1; k++) {
        for(int j = idx[1].first(); j <=  idx[1].last() + 1; j++) {
            for(int i = idx[0].first(); i <= idx[0].last() + 1; i++) {
gsell's avatar
gsell committed
456 457 458 459 460 461

                Vector_t vv = Vector_t(0.0);
                vv(0) = i * hr_m[0] - hr_m[0] / 2;
                vv(1) = j * hr_m[1] - hr_m[1] / 2;
                vv(2) = k * hr_m[2] - hr_m[2] / 2;

462 463
                double r = sqrt(vv(0) * vv(0) + vv(1) * vv(1) + vv(2) * vv(2));
                double tmpgrn  = -vv(2) * vv(2) * atan(vv(0) * vv(1) / (vv(2) * r)) / 2;
gsell's avatar
gsell committed
464 465 466 467 468 469
                tmpgrn += -vv(1) * vv(1) * atan(vv(0) * vv(2) / (vv(1) * r)) / 2;
                tmpgrn += -vv(0) * vv(0) * atan(vv(1) * vv(2) / (vv(0) * r)) / 2;
                tmpgrn += vv(1) * vv(2) * log(vv(0) + r);
                tmpgrn += vv(0) * vv(2) * log(vv(1) + r);
                tmpgrn += vv(0) * vv(1) * log(vv(2) + r);

470
                tmpgreen[i][j][k] = tmpgrn / cellVolume;
gsell's avatar
gsell committed
471 472 473 474 475 476 477 478

            }
        }
    }
    IpplTimings::stopTimer(IntGreensFunctionTimer1_m);

    IpplTimings::startTimer(IntGreensFunctionTimer2_m);

479 480 481 482 483 484
    //assign seems to have problems when we need values that are on another CPU, i.e. [I+1]
    /*assign(rho2_m[I][J][K] ,
           tmpgreen[I+1][J+1][K+1] - tmpgreen[I][J+1][K+1] -
           tmpgreen[I+1][J][K+1] + tmpgreen[I][J][K+1] -
           tmpgreen[I+1][J+1][K] + tmpgreen[I][J+1][K] +
           tmpgreen[I+1][J][K] - tmpgreen[I][J][K]);*/
gsell's avatar
gsell committed
485 486 487 488 489 490

    Index I = nr_m[0] + 1;
    Index J = nr_m[1] + 1;
    Index K = nr_m[2] + 1;

    // the actual integration
491
    rho2_m = 0.0;
gsell's avatar
gsell committed
492 493 494 495 496 497 498 499 500 501 502
    rho2_m[I][J][K]  = tmpgreen[I+1][J+1][K+1];
    rho2_m[I][J][K] += tmpgreen[I+1][J][K];
    rho2_m[I][J][K] += tmpgreen[I][J+1][K];
    rho2_m[I][J][K] += tmpgreen[I][J][K+1];
    rho2_m[I][J][K] -= tmpgreen[I+1][J+1][K];
    rho2_m[I][J][K] -= tmpgreen[I+1][J][K+1];
    rho2_m[I][J][K] -= tmpgreen[I][J+1][K+1];
    rho2_m[I][J][K] -= tmpgreen[I][J][K];

    IpplTimings::stopTimer(IntGreensFunctionTimer2_m);

503
    mirrorRhoField();
gsell's avatar
gsell committed
504

505
    IpplTimings::startTimer(IntGreensFunctionTimer3_m);
gsell's avatar
gsell committed
506
    fft_m->transform(-1, rho2_m, grntr_m);
507
    IpplTimings::stopTimer(IntGreensFunctionTimer3_m);
gsell's avatar
gsell committed
508 509 510 511 512
}

void FFTPoissonSolver::shiftedIntGreensFunction(double zshift) {

    tmpgreen = 0.0;
513
    Field_t grn2(*mesh4_m, *layout4_m);
gsell's avatar
gsell committed
514
    grn2 = 0.0;
515 516
    NDIndex<3> idx =  layout4_m->getLocalNDIndex();
    double cellVolume = hr_m[0] * hr_m[1] * hr_m[2];
gsell's avatar
gsell committed
517
    IpplTimings::startTimer(ShIntGreensFunctionTimer1_m);
518 519 520
    for(int k = idx[2].first(); k <= idx[2].last(); k++) {
        for(int j = idx[1].first(); j <= idx[1].last(); j++) {
            for(int i = idx[0].first(); i <= idx[0].last(); i++) {
gsell's avatar
gsell committed
521 522 523 524 525 526

                Vector_t vv = Vector_t(0.0);
                vv(0) = i * hr_m[0] - hr_m[0] / 2;
                vv(1) = j * hr_m[1] - hr_m[1] / 2;
                vv(2) = k * hr_m[2] - hr_m[2] / 2 + zshift;

527 528
                double r = sqrt(vv(0) * vv(0) + vv(1) * vv(1) + vv(2) * vv(2));
                double tmpgrn  = -vv(2) * vv(2) * atan(vv(0) * vv(1) / (vv(2) * r)) / 2;
gsell's avatar
gsell committed
529 530 531 532 533 534
                tmpgrn += -vv(1) * vv(1) * atan(vv(0) * vv(2) / (vv(1) * r)) / 2;
                tmpgrn += -vv(0) * vv(0) * atan(vv(1) * vv(2) / (vv(0) * r)) / 2;
                tmpgrn += vv(1) * vv(2) * log(vv(0) + r);
                tmpgrn += vv(0) * vv(2) * log(vv(1) + r);
                tmpgrn += vv(0) * vv(1) * log(vv(2) + r);

535
                tmpgreen[i][j][k] = tmpgrn / cellVolume;
gsell's avatar
gsell committed
536 537 538 539 540 541 542

            }
        }
    }
    IpplTimings::stopTimer(ShIntGreensFunctionTimer1_m);

    IpplTimings::startTimer(ShIntGreensFunctionTimer2_m);
543 544 545
    for(int k = idx[2].first(); k <= idx[2].last(); k++) {
        for(int j = idx[1].first(); j <= idx[1].last(); j++) {
            for(int i = idx[0].first(); i <= idx[0].last(); i++) {
gsell's avatar
gsell committed
546 547 548 549

                Vector_t vv = Vector_t(0.0);
                vv(0) = i * hr_m[0] - hr_m[0] / 2;
                vv(1) = j * hr_m[1] - hr_m[1] / 2;
550
                vv(2) = k * hr_m[2] - hr_m[2] / 2 + zshift - nr_m[2] * hr_m[2];
gsell's avatar
gsell committed
551

552 553
                double r = sqrt(vv(0) * vv(0) + vv(1) * vv(1) + vv(2) * vv(2));
                double tmpgrn  = -vv(2) * vv(2) * atan(vv(0) * vv(1) / (vv(2) * r)) / 2;
gsell's avatar
gsell committed
554 555 556 557 558 559
                tmpgrn += -vv(1) * vv(1) * atan(vv(0) * vv(2) / (vv(1) * r)) / 2;
                tmpgrn += -vv(0) * vv(0) * atan(vv(1) * vv(2) / (vv(0) * r)) / 2;
                tmpgrn += vv(1) * vv(2) * log(vv(0) + r);
                tmpgrn += vv(0) * vv(2) * log(vv(1) + r);
                tmpgrn += vv(0) * vv(1) * log(vv(2) + r);

560
                grn2[i][j][k] = tmpgrn / cellVolume;
gsell's avatar
gsell committed
561 562 563 564 565 566

            }
        }
    }
    IpplTimings::stopTimer(ShIntGreensFunctionTimer2_m);

567 568 569 570 571 572 573 574 575 576 577
    /**
     ** (x[0:nr_m[0]-1]^2 + y[0:nr_m[1]-1]^2 + (z_c + z[0:nr_m[2]-1])^2)^{-0.5}
     ** (x[nr_m[0]:1]^2   + y[0:nr_m[1]-1]^2 + (z_c + z[0:nr_m[2]-1])^2)^{-0.5}
     ** (x[0:nr_m[0]-1]^2 + y[nr_m[1]:1]^2   + (z_c + z[0:nr_m[2]-1])^2)^{-0.5}
     ** (x[nr_m[0]:1]^2   + y[nr_m[1]:1]^2   + (z_c + z[0:nr_m[2]-1])^2)^{-0.5}
     **
     ** (x[0:nr_m[0]-1]^2 + y[0:nr_m[1]-1]^2 + (z_c - z[nr_m[2]:1])^2)^{-0.5}
     ** (x[nr_m[0]:1]^2   + y[0:nr_m[1]-1]^2 + (z_c - z[nr_m[2]:1])^2)^{-0.5}
     ** (x[0:nr_m[0]-1]^2 + y[nr_m[1]:1]^2   + (z_c - z[nr_m[2]:1])^2)^{-0.5}
     ** (x[nr_m[0]:1]^2   + y[nr_m[1]:1]^2   + (z_c - z[nr_m[2]:1])^2)^{-0.5}
     */
gsell's avatar
gsell committed
578

579
    IpplTimings::startTimer(ShIntGreensFunctionTimer3_m);
gsell's avatar
gsell committed
580 581 582 583 584 585

    Index I = nr_m[0] + 1;
    Index J = nr_m[1] + 1;
    Index K = nr_m[2] + 1;

    // the actual integration
586
    rho2_m = 0.0;
gsell's avatar
gsell committed
587 588 589 590 591 592 593 594
    rho2_m[I][J][K]  = tmpgreen[I+1][J+1][K+1];
    rho2_m[I][J][K] += tmpgreen[I+1][J][K];
    rho2_m[I][J][K] += tmpgreen[I][J+1][K];
    rho2_m[I][J][K] += tmpgreen[I][J][K+1];
    rho2_m[I][J][K] -= tmpgreen[I+1][J+1][K];
    rho2_m[I][J][K] -= tmpgreen[I+1][J][K+1];
    rho2_m[I][J][K] -= tmpgreen[I][J+1][K+1];
    rho2_m[I][J][K] -= tmpgreen[I][J][K];
595

596 597 598 599 600 601 602 603 604
    tmpgreen = 0.0;
    tmpgreen[I][J][K]  = grn2[I+1][J+1][K+1];
    tmpgreen[I][J][K] += grn2[I+1][J][K];
    tmpgreen[I][J][K] += grn2[I][J+1][K];
    tmpgreen[I][J][K] += grn2[I][J][K+1];
    tmpgreen[I][J][K] -= grn2[I+1][J+1][K];
    tmpgreen[I][J][K] -= grn2[I+1][J][K+1];
    tmpgreen[I][J][K] -= grn2[I][J+1][K+1];
    tmpgreen[I][J][K] -= grn2[I][J][K];
gsell's avatar
gsell committed
605 606
    IpplTimings::stopTimer(ShIntGreensFunctionTimer3_m);

607 608
    mirrorRhoField(tmpgreen);

gsell's avatar
gsell committed
609 610 611 612 613 614
    IpplTimings::startTimer(ShIntGreensFunctionTimer4_m);
    fft_m->transform(-1, rho2_m, grntr_m);
    IpplTimings::stopTimer(ShIntGreensFunctionTimer4_m);

}

615 616
void FFTPoissonSolver::mirrorRhoField() {

617 618 619
    IpplTimings::startTimer(IntGreensMirrorTimer1_m);
    Index aI(0, 2 * nr_m[0]);
    Index aJ(0, 2 * nr_m[1]);
620

621 622
    Index J(0, nr_m[1]);
    Index K(0, nr_m[2]);
623

624 625 626 627 628 629 630
    Index IE(nr_m[0] + 1, 2 * nr_m[0]);
    Index JE(nr_m[1] + 1, 2 * nr_m[1]);
    Index KE(nr_m[2] + 1, 2 * nr_m[2]);

    Index mirroredIE = 2 * nr_m[0] - IE;
    Index mirroredJE = 2 * nr_m[1] - JE;
    Index mirroredKE = 2 * nr_m[2] - KE;
631 632 633 634

    rho2_m[0][0][0] = rho2_m[0][0][1];

    rho2_m[IE][J ][K ] = rho2_m[mirroredIE][J         ][K         ];
635 636 637 638
    rho2_m[aI][JE][K ] = rho2_m[aI        ][mirroredJE][K         ];
    rho2_m[aI][aJ][KE] = rho2_m[aI        ][aJ        ][mirroredKE];

    IpplTimings::stopTimer(IntGreensMirrorTimer1_m);
639 640 641 642
}

void FFTPoissonSolver::mirrorRhoField(Field_t & ggrn2) {

643 644 645 646 647 648
    IpplTimings::startTimer(IntGreensMirrorTimer2_m);
    Index aI(0, 2 * nr_m[0]);
    Index aK(0, 2 * nr_m[2]);

    Index I(0, nr_m[0]);
    Index J(0, nr_m[1]);
649

650 651 652
    Index IE(nr_m[0] + 1, 2 * nr_m[0]);
    Index JE(nr_m[1] + 1, 2 * nr_m[1]);
    Index KE(nr_m[2] + 1, 2 * nr_m[2]);
653 654 655

    Index mirroredIE = 2*nr_m[0] - IE;
    Index mirroredJE = 2*nr_m[1] - JE;
656
    Index shiftedKE  = KE - nr_m[2];
657

658 659 660
    rho2_m[I ][J ][KE] = ggrn2[I          ][J         ][shiftedKE];
    rho2_m[IE][J ][aK] = rho2_m[mirroredIE][J         ][aK       ];
    rho2_m[aI][JE][aK] = rho2_m[aI        ][mirroredJE][aK       ];
661

662
    IpplTimings::stopTimer(IntGreensMirrorTimer2_m);
663 664
}

gsell's avatar
gsell committed
665 666 667 668 669 670 671 672 673 674 675
Inform &FFTPoissonSolver::print(Inform &os) const {
    os << "* ************* F F T P o i s s o n S o l v e r ************************************ " << endl;
    os << "* h " << hr_m << '\n';
    os << "* ********************************************************************************** " << endl;
    return os;
}

/***************************************************************************
 * $RCSfile: FFTPoissonSolver.cc,v $   $Author: adelmann $
 * $Revision: 1.6 $   $Date: 2001/08/16 09:36:08 $
 ***************************************************************************/