Species.C 42.9 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Species.C:
//
////////////////////////////////////////////////////////////////////////////////

#include "Species.h"
#include "spline.h"
#include "sinft.h"
#include <cmath>
#include <cassert>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
29 30
#include <algorithm>

Francois Gygi committed
31 32 33 34 35 36 37 38 39 40 41
using namespace std;

////////////////////////////////////////////////////////////////////////////////
static double simpsn ( int n, double *t )
{
  const double c0 =  17.0/48.0, c1 = 59.0/48.0,
               c2 =  43.0/48.0, c3 = 49.0/48.0;
  double sum = c0 * ( t[0] + t[n-1] ) +
               c1 * ( t[1] + t[n-2] ) +
               c2 * ( t[2] + t[n-3] ) +
               c3 * ( t[3] + t[n-4] );
42

Francois Gygi committed
43 44 45 46 47 48 49 50
  for ( int i = 4; i < n-4; i++ )
  {
    sum += t[i];
  }
  return sum;
}

////////////////////////////////////////////////////////////////////////////////
51
Species::Species(string name) : name_(name),
52
zval_(-1), mass_(0.0), lmax_(-1), deltar_(0.0), atomic_number_(0),
Francois Gygi committed
53 54 55
llocal_(-1), nquad_(-1), rquad_(0.0),
rcps_(0.0), uri_(""), description_("undefined"), symbol_("Undef")
{}
56

Francois Gygi committed
57 58 59 60
////////////////////////////////////////////////////////////////////////////////
bool Species::initialize(double rcpsval)
{
  rcps_ = rcpsval;
61 62 63 64 65 66 67 68
  // select appropriate initialization method
  if ( type_ == NCPP )
    return initialize_ncpp();
  else if ( type_ == SLPP )
    return initialize_slpp();
  else
    throw SpeciesInitException("potential type unknown");
}
69

70 71 72
////////////////////////////////////////////////////////////////////////////////
bool Species::initialize_ncpp()
{
Francois Gygi committed
73
  assert(description_ != "undefined");
74

Francois Gygi committed
75 76 77 78 79 80
  const double fpi = 4.0 * M_PI;

  const int np = vps_[0].size();
  if (zval_ < 0) throw SpeciesInitException("zval_ < 0");
  if (rcps_ < 0.0) throw SpeciesInitException("rcps_ < 0");
  if (mass_ < 0.0) throw SpeciesInitException("mass_ < 0");
81
  if (lmax_ < 0 || lmax_ > 3) throw SpeciesInitException("lmax_<0 or lmax_>3");
82

Francois Gygi committed
83 84
  if (vps_.size() < lmax_+1) throw SpeciesInitException("vps_.size < lmax_+1");

85
  if (llocal_ < 0 || llocal_ > lmax_)
Francois Gygi committed
86
      throw SpeciesInitException("llocal_ < 0 || llocal_ > lmax_");
87

Francois Gygi committed
88 89 90 91 92 93
  if ( nquad_ == 0 ) // KB potential
  {
    for ( int l = 0; l <= lmax_; l++ )
      if ( l != llocal_ && phi_[l].size() == 0 )
        throw SpeciesInitException("phi_[l] undefined for non-local projector");
  }
94

Francois Gygi committed
95
  if ( nquad_ < 0 ) throw SpeciesInitException("nquad < 0");
96
  if ( nquad_ > 0 && rquad_ <= 0.0 )
Francois Gygi committed
97
    throw SpeciesInitException("semilocal with rquad_ <= 0");
98

Francois Gygi committed
99 100
  // compute number of non-local projectors nlm_
  nlm_ = 0;
101
  nop_ = lmax_ + 1;
Francois Gygi committed
102 103 104 105 106 107
  for ( int l = 0; l <= lmax_; l++ )
  {
    if ( l != llocal_ )
    {
       nlm_ += 2 * l + 1;
    }
108
    lmap_.push_back(l);
Francois Gygi committed
109
  }
110

Francois Gygi committed
111
  // compute ndft_: size of radial FFT array
112 113 114
  // ndft_ is a power of 2 larger than ( rdftmin / deltar_ )
  // minimum outer bound in (a.u.)
  const double rdftmin = 40.0;
115
  assert(deltar_ > 0.0);
Francois Gygi committed
116
  ndft_ = 1;
117
  while ( ndft_ * deltar_ < rdftmin )
Francois Gygi committed
118
    ndft_ *= 2;
119

120
  rps_spl_.resize(ndft_);
Francois Gygi committed
121
  for ( int i = 0; i < ndft_; i++ )
122
    rps_spl_[i] = i * deltar_;
123

Francois Gygi committed
124
  vps_spl_.resize(lmax_+1);
125
  vps_spl2_.resize(lmax_+1);
Francois Gygi committed
126
  phi_spl_.resize(lmax_+1);
127
  phi_spl2_.resize(lmax_+1);
128

Francois Gygi committed
129 130 131
  for ( int l = 0; l <= lmax_; l++ )
  {
    vps_spl_[l].resize(ndft_);
132
    vps_spl2_[l].resize(ndft_);
133 134 135 136 137
    if ( phi_[l].size() > 0 )
    {
      phi_spl_[l].resize(ndft_);
      phi_spl2_[l].resize(ndft_);
    }
Francois Gygi committed
138
  }
139

Francois Gygi committed
140
  // extend rps and vps_ to full mesh (up to i==ndft_-1)
141

Francois Gygi committed
142
  vector<double> fint(ndft_);
143

Francois Gygi committed
144 145
  wsg_.resize(lmax_+1);
  gspl_.resize(ndft_);
146 147
  vlocg_spl_.resize(ndft_);
  vlocg_spl2_.resize(ndft_);
148

149 150
  vnlg_spl_.resize(lmax_+1);
  vnlg_spl2_.resize(lmax_+1);
151

Francois Gygi committed
152 153
  vector<double> vlocr(ndft_);
  vector<vector<double> > vnlr(lmax_+1);
154

Francois Gygi committed
155 156 157
  for ( int l = 0; l <= lmax_; l++ )
  {
    vnlr[l].resize(ndft_);
158 159
    vnlg_spl_[l].resize(ndft_+1);
    vnlg_spl2_[l].resize(ndft_+1);
Francois Gygi committed
160
  }
161

Francois Gygi committed
162
  // Extend vps_[l][i] up to ndft_ using -zv/r
163

Francois Gygi committed
164 165
  for ( int l = 0; l <= lmax_; l++ )
  {
166 167
    for ( int i = 0; i < np; i++ )
      vps_spl_[l][i] = vps_[l][i];
Francois Gygi committed
168
    for ( int i = np; i < ndft_; i++ )
169
      vps_spl_[l][i] = - zval_ / rps_spl_[i];
170 171 172 173 174 175
    if ( phi_[l].size() > 0 )
    {
      for ( int i = 0; i < np; i++ )
        phi_spl_[l][i] = phi_[l][i];
      for ( int i = np; i < ndft_; i++ )
        phi_spl_[l][i] = 0.0;
Francois Gygi committed
176 177 178
    }
  }

179
  // compute spline coefficients of vps_spl_ and phi_spl_
Francois Gygi committed
180 181
  for ( int l = 0; l <= lmax_; l++ )
  {
182 183
    const double dvdr = zval_ / (rps_spl_[ndft_-1]*rps_spl_[ndft_-1]);
    spline(ndft_,&rps_spl_[0],&vps_spl_[l][0],0.0,dvdr,0,0,&vps_spl2_[l][0]);
Francois Gygi committed
184 185 186
  }
  for ( int l = 0; l <= lmax_; l++ )
  {
187
    if ( l != llocal_ && phi_[l].size() > 0 )
Francois Gygi committed
188
    {
189
      spline(ndft_,&rps_spl_[0],&phi_spl_[l][0],0.0,0.0,0,1,&phi_spl2_[l][0]);
Francois Gygi committed
190
    }
Francois Gygi committed
191
  }
192

193 194 195 196
  // nonlinear core correction
  if ( has_nlcc() ) initialize_nlcc();
  ztot_ = zval_;

Francois Gygi committed
197 198 199
  // local potential: subtract the long range part due to the smeared charge
  // Next line: constant is 2/sqrt(pi)
  // math.h: # define M_2_SQRTPI     1.12837916709551257390  /* 2/sqrt(pi) */
200
  vlocr[0] = vps_spl_[llocal_][0] + (ztot_/rcps_) * M_2_SQRTPI;
Francois Gygi committed
201 202
  for ( int i = 1; i < ndft_; i++ )
  {
203
    vlocr[i] = vps_spl_[llocal_][i] + (ztot_/rps_spl_[i]) *
204
               erf( rps_spl_[i]/rcps_ );
Francois Gygi committed
205 206 207 208 209 210 211 212 213 214 215 216
  }

  //  Prepare the function vlocr to be used later in the Bessel transforms:
  //
  //  local potential: v(G) = 4 pi \int r^2 vloc(r) sin(Gr)/(Gr) dr
  //  -> store 4 pi r dr vps_(lmax_) in vlocr(i,is)
  //
  //  the Bessel transform is then:
  //  v(G) = 1/G \sum_r sin(Gr) vlocr

  for ( int i = 0; i < ndft_; i++ )
  {
217
    vlocr[i] *= fpi * rps_spl_[i] * deltar_;
Francois Gygi committed
218 219
  }
  //  Local potential
Francois Gygi committed
220
  //  Compute Fourier coefficients of the local potential
Francois Gygi committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234
  //  vlocr[i] contains 4 pi r dr vpsr(lmax_)
  //  v(G) = 4 pi \int r^2 vpsr(r) sin(Gr)/(Gr) dr
  //       = 1/G \sum_r sin(Gr) vlocr
  //
  //  v(G=0) by simpson integration
  //  v(G) = 4 pi \int r^2 vpsr(r) dr
  //       = \sum_r r vlocr
  //
  //  N.B. vlocr[i] contains 4 pi r dr (vps_(lmax_)-v_pseudocharge(r))
  //  Simpson integration up to ndft_ (V is zero beyond that point)
  //  Use fint as temporary array for integration

  for ( int i = 0; i < ndft_; i++ )
  {
235
    fint[i] = vlocr[i] * rps_spl_[i];
Francois Gygi committed
236
  }
Francois Gygi committed
237 238 239 240

  const double v0 = simpsn(ndft_,&fint[0]);
  // next line: include correction for v(g=0) to be independent of rcps
  // const double v0 = simpsn(ndft_,&fint[0]) - M_PI*zval_*rcps_*rcps_;
Francois Gygi committed
241 242 243

  for ( int i = 0; i < ndft_; i++ )
  {
244
    vlocg_spl_[i] = vlocr[i];
Francois Gygi committed
245 246
  }

247
  sinft(ndft_,&vlocg_spl_[0]);
248
  if ( has_nlcc() ) sinft(ndft_,&nlccg_spl_[0]);
Francois Gygi committed
249 250 251

  //  Divide by g's
  gspl_[0] = 0.0;
252
  vlocg_spl_[0] = v0;
253
  if ( has_nlcc() ) nlccg_spl_[0] = zcore_;
Francois Gygi committed
254 255 256 257
  double fac = M_PI/(ndft_*deltar_);
  for ( int i = 1; i < ndft_; i++ )
  {
    gspl_[i] = i * fac;
258
    vlocg_spl_[i] /= gspl_[i];
259
    if ( has_nlcc() ) nlccg_spl_[i] /= gspl_[i];
Francois Gygi committed
260 261 262 263 264
  }

  //  Initialize cubic spline interpolation for local potential Vloc(G)
  //  Use zero first derivative at G=0 and natural (y"=0) at Gmax

265
  spline(ndft_,&gspl_[0],&vlocg_spl_[0],0.0,0.0,0,1,&vlocg_spl2_[0]);
266 267 268
  if ( has_nlcc() )
    spline(ndft_,&gspl_[0],&nlccg_spl_[0],0.0,0.0,0,1,&nlccg_spl2_[0]);

Francois Gygi committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282

  // Non-local KB projectors
  if ( nquad_ == 0 && lmax_ > 0)
  {
    for ( int l = 0; l <= lmax_; l++ )
    {
      wsg_[l] = 0.0;

      if ( l != llocal_ )
      {
        // for KB potentials, compute weights wsg[l]
        //  Compute weight wsg_[l] by integration on the linear mesh
        for ( int i = 0; i < ndft_; i++ )
        {
283 284
          double tmp = phi_spl_[l][i] * rps_spl_[i];
          fint[i] = ( vps_spl_[l][i] - vps_spl_[llocal_][i] ) * tmp * tmp;
Francois Gygi committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
        }
        double tmp = simpsn(ndft_,&fint[0]);
        assert(tmp != 0.0);
        // Next lines: store 1/<phi|delta_v| phi > in wsg[is][l]
        wsg_[l] = 1.0 / ( deltar_ * tmp );

        //   compute non-local projectors:
        //   w(G) = Ylm(G) i^l 4 pi \int r^2 phi_l(r) j_l(Gr) v_l(r) dr
        //   -> store 4 pi v_l(r) phi_l(r) dr in vnlr[l][i]
        //   the Bessel transform is then done by
        //   l=0: j_0(Gr) = sin(Gr)/(Gr)
        //        w_0(G) = Ylm(G)  1/G \sum_r sin(Gr) r vnlr
        //   l=1: j_1(Gr) = sin(Gr)/(Gr)^2 - cos(Gr)/Gr
        //        w_1(G) = Ylm(G) i^-1 1/G^2 \sum_r sin(Gr) vnlr -
        //                 Ylm(G) i^-1 1/G   \sum_r cos(Gr) r vnlr
        //   l=2: j_2(Gr) = sin(Gr)*(3/(Gr)^3 -1/(Gr)) - 3*cos(Gr)/(Gr)^2
        //        w_2(G) = Ylm(G) i^-2 (  3/G^3 \sum_r sin(Gr)/r  vnlr -
        //                                1/G   \sum_r sin(Gr)*r  vnlr -
        //                                3/G^2 \sum_r cos(Gr)    vnlr )
304

Francois Gygi committed
305 306 307
        for ( int i = 0; i < ndft_; i++ )
        {
          vnlr[l][i] = fpi * deltar_ *
308
            ( vps_spl_[l][i] - vps_spl_[llocal_][i] ) * phi_spl_[l][i];
Francois Gygi committed
309 310 311 312 313
        }

      }
    }

Francois Gygi committed
314
    //  compute radial Fourier transforms of vnlr
315

316
    //  Next line: vnlg_spl_ dimensioned ndft_+1 since it is passed to cosft1
317

Francois Gygi committed
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
    // Non-local potentials
    //
    // w(G) = Ylm(G) i^l 4 pi \int r^2 phi_l(r) j_l(Gr) v_l(r) dr
    // -> store 4 pi v_l(r) phi_l(r) dr in vnlr[l][i]
    // the Bessel transform is then done by
    // s: j_0(Gr) = sin(Gr)/(Gr)
    //    w_0(G) = Ylm(G)  1/G \sum_r sin(Gr) r vnlr
    // p: j_1(Gr) = sin(Gr)/(Gr)^2 - cos(Gr)/Gr
    //    w_1(G) = Ylm(G) i^-1 1/G^2 \sum_r sin(Gr) vnlr -
    //             Ylm(G) i^-1 1/G   \sum_r cos(Gr) r vnlr
    // d: j_2(Gr) = sin(Gr)*(3/(Gr)^3 -1/(Gr)) - 3*cos(Gr)/(Gr)^2
    //    w_2(G) = Ylm(G) i^-2 (  3/G^3 \sum_r sin(Gr)/r  vnlr -
    //                            1/G   \sum_r sin(Gr)*r  vnlr -
    //                            3/G^2 \sum_r cos(Gr)    vnlr )
    // vnlr[l][i] contains 4 pi dr phi_l(r) v_l(r)

    for ( int l = 0; l <= lmax_; l++ )
    {
      if ( l != llocal_ )
      {
        if ( l == 0 )
        {

          // s projector
          // w_0(G) = Ylm(G)  1/G \sum_r sin(Gr) r vnlr
          //
          // G=0: Simpson integration up to ndft_
          // Use fint as temporary array for integration

          for ( int i = 0; i < ndft_; i++ )
          {
349
            fint[i] = vnlr[l][i] * rps_spl_[i] * rps_spl_[i];
Francois Gygi committed
350
          }
Francois Gygi committed
351
          const double v0 = simpsn(ndft_, &fint[0]);
Francois Gygi committed
352 353 354

          for ( int i = 0; i < ndft_; i++ )
          {
355
            vnlg_spl_[l][i] = vnlr[l][i] * rps_spl_[i];
Francois Gygi committed
356
          }
357

358
          sinft(ndft_,&vnlg_spl_[l][0]);
Francois Gygi committed
359

360
          vnlg_spl_[l][0] = v0;
Francois Gygi committed
361 362 363
          // Divide by g
          for ( int i = 1; i < ndft_; i++ )
          {
364
            vnlg_spl_[l][i] /= gspl_[i];
Francois Gygi committed
365 366 367 368 369
          }

          //  Initialize cubic spline interpolation
          //  Use zero first derivative at G=0 and natural (y"=0) at Gmax

370 371
          spline(ndft_,&gspl_[0],&vnlg_spl_[l][0],0.0,0.0,0,1,
                 &vnlg_spl2_[l][0]);
Francois Gygi committed
372 373 374 375 376 377 378 379 380 381

        }
        else if ( l == 1 )
        {
          //  p projectors
          //  w_1(G) = Ylm(G) i 1/G^2 \sum_r sin(Gr) vnlr -
          //           Ylm(G) i 1/G   \sum_r cos(Gr) r vnlr
          //  vnlr(i,is,l) contains 4 pi dr phi_l(r) v_l(r)

          //  Next line: v(G=0) is zero (j_1(Gr) -> 0 as G->0 )
Francois Gygi committed
382
          const double v0 = 0.0;
Francois Gygi committed
383 384 385 386

          //  First part: 1/G^2 \sum_r sin(Gr) vnlr
          for ( int i = 0; i < ndft_; i++ )
          {
387
            vnlg_spl_[l][i] = vnlr[l][i];
Francois Gygi committed
388 389
          }

390
          sinft(ndft_,&vnlg_spl_[l][0]);
Francois Gygi committed
391 392 393 394 395

          // Divide by g**2 and store in fint */
          fint[0] = v0;
          for ( int i = 1; i < ndft_; i++ )
          {
396
            fint[i] = vnlg_spl_[l][i] / ( gspl_[i] * gspl_[i] );
Francois Gygi committed
397 398 399 400 401 402
          }

          //  Second part: cosine transform: 1/G   \sum_r cos(Gr) r vnlr

          for ( int i = 0; i < ndft_; i++ )
          {
403
            vnlg_spl_[l][i] = vnlr[l][i] * rps_spl_[i];
Francois Gygi committed
404 405 406 407 408
          }

          //  N.B. Next line: Initialize also vnlg_[l][ndft_] to zero
          //  since it is used and modified by cosft1
          //  vnlg_ was dimensioned ndft_[is]+1
409

410 411
          vnlg_spl_[l][ndft_] = 0.0;
          cosft1(ndft_,&vnlg_spl_[l][0]);
Francois Gygi committed
412 413

          // Divide by g and add to fint to get vnlg_
414
          vnlg_spl_[l][0] = v0;
Francois Gygi committed
415 416
          for ( int i = 1; i < ndft_; i++ )
          {
417
            vnlg_spl_[l][i] = fint[i] - vnlg_spl_[l][i]/gspl_[i];
Francois Gygi committed
418 419 420
          }

          // Initialize spline interpolation
421
          // Use natural bc (y"=0) at G=0 and natural bc (y"=0) at Gmax
Francois Gygi committed
422

423 424
          spline(ndft_,&gspl_[0],&vnlg_spl_[l][0],0.0,0.0,1,1,
                 &vnlg_spl2_[l][0]);
Francois Gygi committed
425 426 427 428 429 430 431 432 433 434 435
        }
        else if ( l == 2 )
        {
          // d projectors
          // d: j_2(Gr) = sin(Gr)*(3/(Gr)^3 -1/(Gr)) - 3*cos(Gr)/(Gr)^2
          //    w_2(G) = Ylm(G) i^-2 (  3/G^3 \sum_r sin(Gr)/r  vnlr -
          //                            1/G   \sum_r sin(Gr)*r  vnlr -
          //                            3/G^2 \sum_r cos(Gr)    vnlr )
          // vnlr[l][i] contains 4 pi dr phi_l(r) v_l(r)

          //  Next line: v(G=0) is zero (j_2(Gr) -> 0 as G->0 )
Francois Gygi committed
436
          const double v0 = 0.0;
Francois Gygi committed
437 438 439

          // First part: sine transform 3/G^3 \sum_r sin(Gr)/r vnlr
          // Note: the integrand is linear near r=0 since vnlr(r) ~ r^2
440
          vnlg_spl_[l][0] = 0.0;
Francois Gygi committed
441 442
          for ( int i = 1; i < ndft_; i++ )
          {
443
            vnlg_spl_[l][i] = vnlr[l][i] / rps_spl_[i];
Francois Gygi committed
444 445
          }

446
          sinft(ndft_,&vnlg_spl_[l][0]);
Francois Gygi committed
447 448 449 450 451

          // multiply by 3/G^3 and store in fint */
          fint[0] = v0;
          for ( int i = 1; i < ndft_; i++ )
          {
452
            fint[i] = 3.0 * vnlg_spl_[l][i] / ( gspl_[i]*gspl_[i]*gspl_[i] );
Francois Gygi committed
453 454 455 456 457
          }

          // Second part: sine transform -1/G \sum_r sin(Gr)*r vnlr
          for ( int i = 0; i < ndft_; i++ )
          {
458
            vnlg_spl_[l][i] = vnlr[l][i] * rps_spl_[i];
Francois Gygi committed
459 460
          }

461
          sinft(ndft_,&vnlg_spl_[l][0]);
Francois Gygi committed
462 463 464 465 466

          // multiply by -1/G and accumulate in fint */
          fint[0] += v0;
          for ( int i = 1; i < ndft_; i++ )
          {
467
            fint[i] += - vnlg_spl_[l][i] / gspl_[i];
Francois Gygi committed
468 469 470 471 472 473
          }

          // Third part: cosine transform: -3/G^2 \sum_r cos(Gr) vnlr

          for ( int i = 0; i < ndft_; i++ )
          {
474
            vnlg_spl_[l][i] = vnlr[l][i];
Francois Gygi committed
475 476 477 478 479
          }

          //  N.B. Next line: Initialize also vnlg_[l][ndft_] to zero
          //  since it is used and modified by cosft1
          //  vnlg_ was dimensioned ndft_[is]+1
480

481 482
          vnlg_spl_[l][ndft_] = 0.0;
          cosft1(ndft_,&vnlg_spl_[l][0]);
Francois Gygi committed
483 484 485 486 487

          // Multiply by -3/G^2 and add to fint
          fint[0] += v0;
          for ( int i = 1; i < ndft_; i++ )
          {
488
            fint[i] += - 3.0 * vnlg_spl_[l][i] / (gspl_[i] * gspl_[i]);
Francois Gygi committed
489
          }
490

491
          vnlg_spl_[l][0] = v0;
Francois Gygi committed
492 493
          for ( int i = 1; i < ndft_; i++ )
          {
494
            vnlg_spl_[l][i] = fint[i];
Francois Gygi committed
495 496 497 498 499
          }

          // Initialize spline interpolation
          // Use zero first derivative at G=0 and natural (y"=0) at Gmax

500 501
          spline(ndft_,&gspl_[0],&vnlg_spl_[l][0],0.0,0.0,0,1,
                 &vnlg_spl2_[l][0]);
Francois Gygi committed
502 503 504 505 506 507 508
        }
      } // l != llocal_
    } // l
  } // nquad_ == 0 && lmax_ > 0 (KB projectors)
  return true;
}

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 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 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 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 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 688 689 690 691 692 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 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
////////////////////////////////////////////////////////////////////////////////
bool Species::initialize_slpp()
{
  assert(description_ != "undefined");

  const double fpi = 4.0 * M_PI;

  const int np = vlocal_.size();
  nquad_ = 0;

  // number of projectors
  nop_ = 0;
  for ( int l = 0; l < proj_.size(); l++ )
    for ( int n = 0; n < proj_[l].size(); n++ )
    {
      lmap_.push_back(l);
      nop_++;
    }

  // check sanity of input
  if (zval_ < 0) throw SpeciesInitException("zval_ < 0");
  if (rcps_ < 0.0) throw SpeciesInitException("rcps_ < 0");
  if (mass_ < 0.0) throw SpeciesInitException("mass_ < 0");

  // check consistency of input
  // a) for each angular momentum l there has to be a D matrix of
  //    the size n x n, where n are the number of projectors with
  //    this angular momentum l
  if ( proj_.size() != d_.size() )
    throw SpeciesInitException("projector and dmatrix inconsistent");
  for ( int l = 0; l < proj_.size(); l++ )
  {
    // trivial case only one projector (n = 1)
    // if D matrix is not present, create a trivial one D = 1
    // otherwise leave D matrix unchanged
    if ( proj_[l].size() == 1 )
    {
      if ( d_[l].size() > 1 )
        throw SpeciesInitException("dmatrix has wrong dimension");
      d_[l].resize(1);
      if ( d_[l][0].size() > 1)
        throw SpeciesInitException("dmatrix has wrong dimension");
      d_[l][0].resize(1,1);
    }
    // general case dim( d[l] ) = n
    else
    {
      const int nmax = proj_[l].size();
      if ( d_[l].size() != nmax )
        throw SpeciesInitException("dmatrix has wrong dimension");
      for ( int n = 0; n < nmax; n++ )
      {
        if ( d_[l][n].size() != nmax )
          throw SpeciesInitException("dmatrix has wrong dimension");
      }
    }
  }

  // compute number of non-local projectors nlm_
  nlm_ = 0;
  for ( int l = 0; l < proj_.size(); l++ )
  {
    nlm_ += (2 * l + 1) * proj_[l].size();
  }

  // compute ndft_: size of radial FFT array
  // ndft_ is a power of 2 larger than ( rdftmin / deltar_ )
  // minimum outer bound in (a.u.)
  const double rdftmin = 40.0;
  // next line: limit small mesh sizes
  assert(deltar_ > 0.0001);
  ndft_ = 1;
  while ( ndft_ * deltar_ < rdftmin )
    ndft_ *= 2;

  rps_spl_.resize(ndft_);
  for ( int i = 0; i < ndft_; i++ )
    rps_spl_[i] = i * deltar_;

  phi_spl_.resize(nop_);
  phi_spl2_.resize(nop_);

  for ( int iop = 0; iop < nop_; iop++ )
  {
    phi_spl_[iop].resize(ndft_);
    phi_spl2_[iop].resize(ndft_);
  }


  // extend rps and vps_ to full mesh (up to i==ndft_-1)

  vector<double> fint(ndft_);

  gspl_.resize(ndft_);
  vlocg_spl_.resize(ndft_);
  vlocg_spl2_.resize(ndft_);

  vnlg_spl_.resize(nop_);
  vnlg_spl2_.resize(nop_);

  vector<double> vlocr(ndft_);
  vector<vector<double> > vnlr(nop_);

  for ( int iop = 0; iop < nop_; iop++ )
  {
    vnlr[iop].resize(ndft_);
    vnlg_spl_[iop].resize(ndft_ + 1);
    vnlg_spl2_[iop].resize(ndft_ + 1);
  }

  // Extend vps_[l][i] up to ndft_ using -zv/r

  // local potential
  for ( int i = 0; i < np; i++ )
    vlocr[i] = vlocal_[i];
  for ( int i = np; i < ndft_; i++ )
    vlocr[i] = -zval_ / rps_spl_[i];

  for ( int iop = 0; iop < nop_; iop++ )
  {
    // counter for projectors of same l
    int n = 0;
    if ( iop == 0 || lmap_[iop] != lmap_[iop - 1] ) n = 0;
    else n++;
    for ( int i = 0; i < np; i++ )
      phi_spl_[iop][i] = proj_[lmap_[iop]][n][i];
    for ( int i = np; i < ndft_; i++ )
      phi_spl_[iop][i] = 0.0;
  }

  for ( int iop = 0; iop < nop_; iop++ )
  {
    spline(ndft_,&rps_spl_[0],&phi_spl_[iop][0],0.0,0.0,0,1,&phi_spl2_[iop][0]);
  }

  // nonlinear core correction
  if ( has_nlcc() ) initialize_nlcc();
  ztot_ = zval_;

  // local potential: subtract the long range part due to the smeared charge
  // Next line: constant is 2/sqrt(pi)
  // math.h: # define M_2_SQRTPI     1.12837916709551257390  /* 2/sqrt(pi) */
  vlocr[0] += ( ztot_ / rcps_ ) * M_2_SQRTPI;
  for ( int i = 1; i < ndft_; i++ )
  {
    vlocr[i] += ( ztot_ / rps_spl_[i] ) * erf(rps_spl_[i] / rcps_);
  }

  //  Prepare the function vlocr to be used later in the Bessel transforms:
  //
  //  local potential: v(G) = 4 pi \int r^2 vloc(r) sin(Gr)/(Gr) dr
  //  -> store 4 pi r dr vps_(lmax_) in vlocr(i,is)
  //
  //  the Bessel transform is then:
  //  v(G) = 1/G \sum_r sin(Gr) vlocr

  for ( int i = 0; i < ndft_; i++ )
  {
    vlocr[i] *= fpi * rps_spl_[i] * deltar_;
  }
  //  Local potential
  //  Compute Fourier coefficients of the local potential
  //  vlocr[i] contains 4 pi r dr vpsr(lmax_)
  //  v(G) = 4 pi \int r^2 vpsr(r) sin(Gr)/(Gr) dr
  //       = 1/G \sum_r sin(Gr) vlocr
  //
  //  v(G=0) by simpson integration
  //  v(G) = 4 pi \int r^2 vpsr(r) dr
  //       = \sum_r r vlocr
  //
  //  N.B. vlocr[i] contains 4 pi r dr (vps_(lmax_)-v_pseudocharge(r))
  //  Simpson integration up to ndft_ (V is zero beyond that point)
  //  Use fint as temporary array for integration

  for ( int i = 0; i < ndft_; i++ )
  {
    fint[i] = vlocr[i] * rps_spl_[i];
  }

  const double v0 = simpsn(ndft_,&fint[0]);
  // next line: include correction for v(g=0) to be independent of rcps
  // const double v0 = simpsn(ndft_,&fint[0]) - M_PI*zval_*rcps_*rcps_;

  for ( int i = 0; i < ndft_; i++ )
  {
    vlocg_spl_[i] = vlocr[i];
  }

  sinft(ndft_,&vlocg_spl_[0]);
  if ( has_nlcc() ) sinft(ndft_,&nlccg_spl_[0]);

  //  Divide by g's
  gspl_[0] = 0.0;
  vlocg_spl_[0] = v0;
  if ( has_nlcc() ) nlccg_spl_[0] = zcore_;
  double fac = M_PI/(ndft_*deltar_);
  for ( int i = 1; i < ndft_; i++ )
  {
    gspl_[i] = i * fac;
    vlocg_spl_[i] /= gspl_[i];
    if ( has_nlcc() ) nlccg_spl_[i] /= gspl_[i];
  }

  //  Initialize cubic spline interpolation for local potential Vloc(G)
  //  Use zero first derivative at G=0 and natural (y"=0) at Gmax

  spline(ndft_,&gspl_[0],&vlocg_spl_[0],0.0,0.0,0,1,&vlocg_spl2_[0]);
  if ( has_nlcc() )
    spline(ndft_,&gspl_[0],&nlccg_spl_[0],0.0,0.0,0,1,&nlccg_spl2_[0]);


  // Non-local projectors
  //                                          inf
  //   /                                        /
  //  |  3   i G r                     l       |     2
  //  | d r e      f (r) Y (r) = 4 pi i  Y (G) | dr r  j (Gr) f (r)
  //  |             l     lm              lm   |        l      l
  // /                                        /
  //                                          0
  // where f_l(r) = phi_l(r)

  // multiply projectors with weight
  // w_l = 1 / < phi_l | V_l | phi_l >
  // as there is no V_l all weights are 1
  wsg_.resize(nop_,1.0);
  for ( int iop = 0; iop < nop_; iop++ )
  {
    //   compute non-local projectors:
    //   w(G) = Ylm(G) i^l 4 pi \int r^2 j_l(Gr) f_l(r) dr
    //   -> store 4 pi f_l(r) dr in vnlr[l][i]
    //   the Bessel transform is then done by
    //   l=0: j_0(Gr) = sin(Gr)/(Gr)
    //        w_0(G) = Ylm(G)  1/G \sum_r sin(Gr) r vnlr
    //   l=1: j_1(Gr) = sin(Gr)/(Gr)^2 - cos(Gr)/Gr
    //        w_1(G) = Ylm(G) i^-1 1/G^2 \sum_r sin(Gr) vnlr -
    //                 Ylm(G) i^-1 1/G   \sum_r cos(Gr) r vnlr
    //   l=2: j_2(Gr) = sin(Gr)*(3/(Gr)^3 -1/(Gr)) - 3*cos(Gr)/(Gr)^2
    //        w_2(G) = Ylm(G) i^-2 (  3/G^3 \sum_r sin(Gr)/r  vnlr -
    //                                1/G   \sum_r sin(Gr)*r  vnlr -
    //                                3/G^2 \sum_r cos(Gr)    vnlr )

    for ( int i = 0; i < ndft_; i++ )
    {
      vnlr[iop][i] = fpi * deltar_ * phi_spl_[iop][i];
    }

  } // loop over projector

  //  compute radial Fourier transforms of vnlr

  //  Next line: vnlg_spl_ dimensioned ndft_+1 since it is passed to cosft1

  // Non-local potentials
  //
  // w(G) = Ylm(G) i^l 4 pi \int r^2 j_l(Gr) f_l(r) dr
  // -> store 4 pi f_l(r) dr in vnlr[l][i]
  // the Bessel transform is then done by
  // s: j_0(Gr) = sin(Gr)/(Gr)
  //    w_0(G) = Ylm(G)  1/G \sum_r sin(Gr) r vnlr
  // p: j_1(Gr) = sin(Gr)/(Gr)^2 - cos(Gr)/Gr
  //    w_1(G) = Ylm(G) i^-1 1/G^2 \sum_r sin(Gr) vnlr -
  //             Ylm(G) i^-1 1/G   \sum_r cos(Gr) r vnlr
  // d: j_2(Gr) = sin(Gr)*(3/(Gr)^3 -1/(Gr)) - 3*cos(Gr)/(Gr)^2
  //    w_2(G) = Ylm(G) i^-2 (  3/G^3 \sum_r sin(Gr)/r  vnlr -
  //                            1/G   \sum_r sin(Gr)*r  vnlr -
  //                            3/G^2 \sum_r cos(Gr)    vnlr )
775 776 777 778 779 780
  // f: j_3(Gr) = sin(Gr)*(15/(Gr)^4-6/(Gr)^2)) +
  //              cos(Gr)*(1/(Gr)-15/(Gr)^3)
  //    w_3(G) = Ylm(G) i^-3 (  15/G^4 \sum_r sin(Gr)/r^2  vnlr
  //                          - 6/G^2  \sum_r sin(Gr)      vnlr
  //                          + 1/G    \sum_r cos(Gr)*r    vnlr
  //                          - 15/G^3 \sum_r cos(Gr)/r    vnlr )
781 782 783 784 785 786 787 788 789 790 791 792 793 794

  for ( int iop = 0; iop < nop_; iop++ )
  {

    if ( lmap_[iop] == 0 )
    {
      // s projector
      // w_0(G) = Ylm(G)  1/G \sum_r sin(Gr) r vnlr
      //
      // G=0: Simpson integration up to ndft_
      // Use fint as temporary array for integration

      for ( int i = 0; i < ndft_; i++ )
      {
795 796
        const double r = rps_spl_[i];
        fint[i] = vnlr[iop][i] * r * r;
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
      }
      const double v0 = simpsn(ndft_,&fint[0]);

      for ( int i = 0; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = vnlr[iop][i] * rps_spl_[i];
      }

      sinft(ndft_,&vnlg_spl_[iop][0]);

      vnlg_spl_[iop][0] = v0;
      // Divide by g
      for ( int i = 1; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] /= gspl_[i];
      }

      //  Initialize cubic spline interpolation
      //  Use zero first derivative at G=0 and natural (y"=0) at Gmax

      spline(ndft_,&gspl_[0],&vnlg_spl_[iop][0],
             0.0,0.0,0,1,&vnlg_spl2_[iop][0]);

    }
    else if ( lmap_[iop] == 1 )
    {
      //  p projectors
      //  w_1(G) = Ylm(G) i 1/G^2 \sum_r sin(Gr) vnlr -
      //           Ylm(G) i 1/G   \sum_r cos(Gr) r vnlr

      //  Next line: v(G=0) is zero (j_1(Gr) -> 0 as G->0 )
      const double v0 = 0.0;

      //  First part: 1/G^2 \sum_r sin(Gr) vnlr
      for ( int i = 0; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = vnlr[iop][i];
      }

      sinft(ndft_,&vnlg_spl_[iop][0]);

      // Divide by g**2 and store in fint */
      fint[0] = v0;
      for ( int i = 1; i < ndft_; i++ )
      {
842 843
        const double g = gspl_[i];
        fint[i] = vnlg_spl_[iop][i] / ( g * g );
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
      }

      //  Second part: cosine transform: 1/G   \sum_r cos(Gr) r vnlr

      for ( int i = 0; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = vnlr[iop][i] * rps_spl_[i];
      }

      //  N.B. Next line: Initialize also vnlg_[l][ndft_] to zero
      //  since it is used and modified by cosft1
      //  vnlg_ was dimensioned ndft_[is]+1

      vnlg_spl_[iop][ndft_] = 0.0;
      cosft1(ndft_,&vnlg_spl_[iop][0]);

      // Divide by g and add to fint to get vnlg_
      vnlg_spl_[iop][0] = v0;
      for ( int i = 1; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = fint[i] - vnlg_spl_[iop][i] / gspl_[i];
      }

      // Initialize spline interpolation
      // Use natural bc (y"=0) at G=0 and natural bc (y"=0) at Gmax

      spline(ndft_,&gspl_[0],&vnlg_spl_[iop][0],
             0.0,0.0,1,1,&vnlg_spl2_[iop][0]);
    }
    else if ( lmap_[iop] == 2 )
    {
      // d projectors
      // d: j_2(Gr) = sin(Gr)*(3/(Gr)^3 -1/(Gr)) - 3*cos(Gr)/(Gr)^2
      //    w_2(G) = Ylm(G) i^-2 (  3/G^3 \sum_r sin(Gr)/r  vnlr -
      //                            1/G   \sum_r sin(Gr)*r  vnlr -
      //                            3/G^2 \sum_r cos(Gr)    vnlr )

      //  Next line: v(G=0) is zero (j_2(Gr) -> 0 as G->0 )
      const double v0 = 0.0;

      // First part: sine transform 3/G^3 \sum_r sin(Gr)/r vnlr
      // Note: the integrand is linear near r=0 since vnlr(r) ~ r^2
      vnlg_spl_[iop][0] = 0.0;
      for ( int i = 1; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = vnlr[iop][i] / rps_spl_[i];
      }

      sinft(ndft_,&vnlg_spl_[iop][0]);

      // multiply by 3/G^3 and store in fint */
      fint[0] = v0;
      for ( int i = 1; i < ndft_; i++ )
      {
898 899
        const double g = gspl_[i];
        fint[i] = 3.0 * vnlg_spl_[iop][i] / ( g * g * g );
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
      }

      // Second part: sine transform -1/G \sum_r sin(Gr)*r vnlr
      for ( int i = 0; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = vnlr[iop][i] * rps_spl_[i];
      }

      sinft(ndft_,&vnlg_spl_[iop][0]);

      // multiply by -1/G and accumulate in fint */
      for ( int i = 1; i < ndft_; i++ )
      {
        fint[i] += -vnlg_spl_[iop][i] / gspl_[i];
      }

      // Third part: cosine transform: -3/G^2 \sum_r cos(Gr) vnlr
      for ( int i = 0; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = vnlr[iop][i];
      }

      //  N.B. Next line: Initialize also vnlg_[l][ndft_] to zero
      //  since it is used and modified by cosft1
      //  vnlg_ was dimensioned ndft_[is]+1

      vnlg_spl_[iop][ndft_] = 0.0;
      cosft1(ndft_,&vnlg_spl_[iop][0]);

      // Multiply by -3/G^2 and add to fint
      for ( int i = 1; i < ndft_; i++ )
      {
932 933
        const double g = gspl_[i];
        fint[i] += -3.0 * vnlg_spl_[iop][i] / ( g * g );
934 935 936 937 938 939 940 941 942 943 944
      }

      vnlg_spl_[iop][0] = v0;
      for ( int i = 1; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = fint[i];
      }

      // Initialize spline interpolation
      // Use zero first derivative at G=0 and natural (y"=0) at Gmax

945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
      spline(ndft_,&gspl_[0],&vnlg_spl_[iop][0],
             0.0,0.0,0,1,&vnlg_spl2_[iop][0]);
    }
    else if ( lmap_[iop] == 3 )
    {
      // f projectors
      // f: j_3(Gr) = sin(Gr)*(15/(Gr)^4-6/(Gr)^2)) +
      //              cos(Gr)*(1/(Gr)-15/(Gr)^3)
      //    w_3(G) = Ylm(G) i^-3 (  15/G^4 \sum_r sin(Gr)/r^2  vnlr
      //                          - 6/G^2  \sum_r sin(Gr)      vnlr
      //                          + 1/G    \sum_r cos(Gr)*r    vnlr
      //                          - 15/G^3 \sum_r cos(Gr)/r    vnlr )

      //  Next line: v(G=0) is zero (j_3(Gr) -> 0 as G->0 )
      const double v0 = 0.0;

      // First part: sine transform 15/G^4 \sum_r sin(Gr)/r^2 vnlr
      // Note: the integrand is linear near r=0 since vnlr(r) ~ r^3
      vnlg_spl_[iop][0] = 0.0;
      for ( int i = 1; i < ndft_; i++ )
      {
        const double r = rps_spl_[i];
        vnlg_spl_[iop][i] = vnlr[iop][i] / (r*r);
      }

      sinft(ndft_,&vnlg_spl_[iop][0]);

      // multiply by 15/G^4 and store in fint */
      fint[0] = v0;
      for ( int i = 1; i < ndft_; i++ )
      {
        const double g = gspl_[i];
        fint[i] = 15.0 * vnlg_spl_[iop][i] / (g*g*g*g);
      }

      // Second part: sine transform -6/G^2 \sum_r sin(Gr) vnlr
      for ( int i = 0; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = vnlr[iop][i];
      }

      sinft(ndft_,&vnlg_spl_[iop][0]);

      // multiply by -6/G^2 and accumulate in fint */
      for ( int i = 1; i < ndft_; i++ )
      {
        const double g = gspl_[i];
        fint[i] += -6.0 * vnlg_spl_[iop][i] / (g*g);
      }

      // Third part: cosine transform:  1/G \sum_r cos(Gr)*r vnlr
      for ( int i = 0; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = vnlr[iop][i] * rps_spl_[i];
      }

      //  N.B. Next line: Initialize also vnlg_[l][ndft_] to zero
      //  since it is used and modified by cosft1
      //  vnlg_ was dimensioned ndft_[is]+1

      vnlg_spl_[iop][ndft_] = 0.0;
      cosft1(ndft_,&vnlg_spl_[iop][0]);

      // Multiply by 1/G and add to fint
      for ( int i = 1; i < ndft_; i++ )
      {
        fint[i] += vnlg_spl_[iop][i] / gspl_[i];
      }

      // Fourth part: cosine transform:  -15/G^3 \sum_r cos(Gr)/r vnlr
      vnlg_spl_[iop][0] = 0.0;
      for ( int i = 1; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = vnlr[iop][i] / rps_spl_[i];
      }

      //  N.B. Next line: Initialize also vnlg_[l][ndft_] to zero
      //  since it is used and modified by cosft1
      //  vnlg_ was dimensioned ndft_[is]+1

      vnlg_spl_[iop][ndft_] = 0.0;
      cosft1(ndft_,&vnlg_spl_[iop][0]);

      // Multiply by -15/G^3 and add to fint
      for ( int i = 1; i < ndft_; i++ )
      {
        const double g = gspl_[i];
        fint[i] += -15.0 * vnlg_spl_[iop][i] / (g*g*g);
      }

      vnlg_spl_[iop][0] = v0;
      for ( int i = 1; i < ndft_; i++ )
      {
        vnlg_spl_[iop][i] = fint[i];
      }

      // Initialize spline interpolation
      // Use zero first derivative at G=0 and natural (y"=0) at Gmax

      spline(ndft_,&gspl_[0],&vnlg_spl_[iop][0],
             0.0,0.0,0,1,&vnlg_spl2_[iop][0]);
    }
    else
    {
      throw SpeciesInitException("Species::initialize_slpp: l < 0 or l > 3");
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    }
  } // loop over projector
  return true;
}

////////////////////////////////////////////////////////////////////////////////
void Species::initialize_nlcc()
{
  const double fpi = 4.0 * M_PI;

  // allocate array
  nlcc_spl_.resize(ndft_);
  nlcc_spl2_.resize(ndft_);
  nlccg_spl_.resize(ndft_);
  nlccg_spl2_.resize(ndft_);

  // expand nlcc to large mesh
  const int np = nlcc_.size();
  copy( nlcc_.begin(), nlcc_.end(), nlcc_spl_.begin() );
  // if function is not decaying, keep it at constant value
  if ( nlcc_[np-2] <= nlcc_[np-1] )
  {
    fill( nlcc_spl_.begin()+np, nlcc_spl_.end(), nlcc_[np-1] );
  }
  // otherwise expand nlcc to large mesh using a
  // exponential decay exp(-alpha r)
  else
  {
    const double factor = nlcc_[np-1] / nlcc_[np-2];
    for ( int i = np; i < ndft_; i++ )
    {
      nlcc_spl_[i] = nlcc_spl_[i-1] * factor;
    }
  }

  // compute spline coefficients
  spline(ndft_,&rps_spl_[0],&nlcc_spl_[0],0.0,0.0,0,1,&nlcc_spl2_[0]);

  // prepare the function nlccr to be used later in the Bessel transforms
  //
  //  local density: nlcc(G) = 4 pi \int r^2 nlcc(r) sin(Gr)/(Gr) dr
  //  -> store 4 pi r dr nlcc_spl_ in nlccr(i)
  //
  //  the Bessel transform is then:
  //  nlcc(G) = 1/G \sum_r sin(Gr) nlccr

  for ( int i = 0; i < ndft_; i++ )
  {
    nlccg_spl_[i] = fpi * rps_spl_[i] * deltar_ * nlcc_spl_[i];
  }

  // integrate core correction density
  //   /         2
  //  | dr 4 pi r n(r)
  // /
  // nlccr already contains factor 4 pi r
  // use fint for integration
  vector<double> fint(ndft_);
  for ( int i = 0; i < ndft_; i++ )
  {
    fint[i] = nlccg_spl_[i] * rps_spl_[i];
  }
  zcore_ = simpsn(ndft_,&fint[0]);

}

Francois Gygi committed
1116 1117
void Species::phi(int l, double r, double &val)
{
1118 1119 1120 1121
  val = 0.0;
  if ( phi_[l].size() == 0 || l > lmax_ || r > rps_spl_[ndft_-1] )
    return;
  splint(ndft_,&rps_spl_[0],&phi_spl_[l][0],&phi_spl2_[l][0],r,&val);
Francois Gygi committed
1122 1123
}

Francois Gygi committed
1124 1125
void Species::vpsr(int l, double r, double &v)
{
1126
  if ( l > lmax_ || r > rps_spl_[ndft_-1] )
Francois Gygi committed
1127 1128 1129 1130 1131
  {
    v = 0.0;
  }
  else
  {
1132
    splint(ndft_,&rps_spl_[0],&vps_spl_[l][0],&vps_spl2_[l][0],r,&v);
Francois Gygi committed
1133 1134 1135 1136 1137
  }
}

void Species::dvpsr(int l, double r, double &v, double &dv)
{
1138
  if ( l > lmax_ || r > rps_spl_[ndft_-1] )
Francois Gygi committed
1139 1140 1141 1142 1143 1144
  {
    v = 0.0;
    dv = 0.0;
  }
  else
  {
1145
    splintd(ndft_,&rps_spl_[0],&vps_spl_[l][0],&vps_spl2_[l][0],r,&v,&dv);
Francois Gygi committed
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
  }
}

void Species::vlocg(double g, double &v)
{
  if ( g > gspl_[ndft_-1] )
  {
    v = 0.0;
  }
  else
  {
1157
    splint(ndft_,&gspl_[0],&vlocg_spl_[0],&vlocg_spl2_[0],g,&v);
Francois Gygi committed
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
  }
}

void Species::dvlocg(double g, double &v, double &dv)
{
  if ( g > gspl_[ndft_-1] )
  {
    v = 0.0;
    dv = 0.0;
  }
  else
  {
1170
    splintd(ndft_,&gspl_[0],&vlocg_spl_[0],&vlocg_spl2_[0],g,&v,&dv);
Francois Gygi committed
1171 1172 1173
  }
}

1174
void Species::vnlg(int iop, double g, double &v)
Francois Gygi committed
1175
{
1176 1177
  assert ( iop >= 0 && iop < nop_ );
  int l = lmap_[iop];
Francois Gygi committed
1178 1179 1180 1181 1182 1183
  if ( l == llocal_ || g > gspl_[ndft_-1] )
  {
    v = 0.0;
  }
  else
  {
1184
    splint(ndft_,&gspl_[0],&vnlg_spl_[iop][0],&vnlg_spl2_[iop][0],g,&v);
Francois Gygi committed
1185 1186 1187
  }
}

1188
void Species::dvnlg(int iop, double g, double &v, double &dv)
Francois Gygi committed
1189
{
1190 1191
  assert ( iop >= 0 && iop < nop_ );
  int l = lmap_[iop];
Francois Gygi committed
1192 1193 1194 1195 1196 1197 1198
  if ( l == llocal_ || g > gspl_[ndft_-1] )
  {
    v = 0.0;
    dv = 0.0;
  }
  else
  {
1199
    splintd(ndft_,&gspl_[0],&vnlg_spl_[iop][0],&vnlg_spl2_[iop][0],g,&v,&dv);
Francois Gygi committed
1200 1201 1202 1203 1204 1205
  }
}

double Species::rhopsg( double g )
{
  double arg = 0.25 * rcps_ * rcps_ * g * g;
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
  return -ztot_ * exp( -arg );
}

// core density for nonlinear core correction in reciprocal space
void Species::rhocoreg(double g, double &rho)
{
  if ( has_nlcc() && g <= gspl_[ndft_-1] )
  {
    // spline interpolation
    splint(ndft_,&gspl_[0],&nlccg_spl_[0],&nlccg_spl2_[0],g,&rho);
  }
  else
  {
    rho = 0.0;
  }
}

// core density for nonlinear core correction in reciprocal space
void Species::drhocoreg(double g, double &rho, double &drho)
{
  if ( has_nlcc() && g <= gspl_[ndft_ - 1] )
  {
    // spline interpolation
    splintd(ndft_,&gspl_[0],&nlccg_spl_[0],&nlccg_spl2_[0],g,&rho,&drho);
  }
  else
  {
    rho = 0.0;
    drho = 0.0;
  }
Francois Gygi committed
1236 1237 1238
}

ostream& operator << ( ostream &os, Species &s )
1239 1240 1241 1242 1243 1244
{
  s.print(os, false);
  return os;
}

void Species::print(ostream &os, bool expanded_form)
Francois Gygi committed
1245 1246
{
  // XML output of species
1247 1248 1249
  // print in compact form if the uri is known
  // and if expanded_form==false
  if ( !expanded_form && uri() != "" )
Francois Gygi committed
1250
  {
1251 1252
    os <<"<species name=\"" << name()
       << "\" href=\"" << uri() << "\"/>" << endl;
Francois Gygi committed
1253 1254 1255
  }
  else
  {
1256 1257
    os.setf(ios::scientific,ios::floatfield);
    os << setprecision(12);
1258 1259 1260 1261 1262
    os <<"<species name=\"" << name() << "\">" << endl;
    os << "<description>" << description() << "</description>" << endl;
    os << "<symbol>" << symbol() << "</symbol>" << endl;
    os << "<atomic_number>" << atomic_number() << "</atomic_number>" << endl;
    os << "<mass>" << mass() << "</mass>" << endl;
1263
    if ( type_ == NCPP )
Francois Gygi committed
1264
    {
1265 1266 1267 1268 1269 1270 1271 1272 1273
      os << "<norm_conserving_pseudopotential>" << endl;
      os << "<valence_charge>" << zval() << "</valence_charge>" << endl;
      os << "<lmax>" << lmax() << "</lmax>" << endl;
      os << "<llocal>" << llocal() << "</llocal>" << endl;
      os << "<nquad>" << nquad() << "</nquad>" << endl;
      os << "<rquad>" << rquad() << "</rquad>" << endl;
      os << "<mesh_spacing>" << deltar() << "</mesh_spacing>" << endl;
      if ( nlcc_.size() > 0 ) print_nlcc(os);
      for ( int l = 0; l <= lmax(); l++ )
Francois Gygi committed
1274
      {
1275 1276 1277 1278
        const int size = vps_[l].size();
        os << "<projector l=\"" << l << "\" size=\"" << size
           << "\">" << endl;
        os << "<radial_potential>\n";
Francois Gygi committed
1279
        for ( int i = 0; i < size; i++ )
1280 1281 1282 1283 1284
          os << vps_[l][i] << "\n";
        os << "</radial_potential>\n";
        os << "<radial_function>\n";
        if ( l < phi_.size() && phi_[l].size() == size )
          print_radial_function(os,phi_[l]);
Francois Gygi committed
1285
        os << "</radial_function>\n";
1286
        os << "</projector>" << endl;
Francois Gygi committed
1287
      }
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
      os << "</norm_conserving_pseudopotential>" << endl;
    }
    else if ( type_ == SLPP )
    {
      os << "<norm_conserving_semilocal_pseudopotential>" << endl;
      os << "<valence_charge>" << zval() << "</valence_charge>" << endl;
      os << "<mesh_spacing>" << deltar() << "</mesh_spacing>" << endl;
      if ( nlcc_.size() > 0 ) print_nlcc(os);
      os << "<local_potential size=\"" << vlocal_.size() << "\">" << endl;
      print_radial_function(os,vlocal_);
      os << "</local_potential>" << endl;
      // print projector
      for ( int l = 0; l < proj_.size(); l++ )
      {
        for ( int n = 0; n < proj_[l].size(); n++ )
        {
          os << "<projector l=\"" << l << "\" i=\"" << n + 1 << "\" size=\""
            << proj_[l][n].size() << "\">" << endl;
          // print radial function
          print_radial_function(os,proj_[l][n]);
          os << "</projector>" << endl;
        }
      }
      // print D matrix
      for ( int l = 0; l < d_.size(); l++ )
      {
        for ( int n = 0; n < d_[l].size(); n++ )
          for ( int m = 0; m < d_[l][n].size(); m++ )
            os << "<d_ij l=\"" << l << "\" i=\"" << n + 1 << "\" j=\"" << m + 1
              << "\">" << d_[l][n][m] << "</d_ij>" << endl;
      }
      os << "</norm_conserving_semilocal_pseudopotential>" << endl;
Francois Gygi committed
1320 1321 1322 1323 1324
    }
    os << "</species>" << endl;
  }
}

1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
// print core density of nonlinear core correction
void Species::print_nlcc(std::ostream& os)
{
  os << "<core_density size=\"" << nlcc_.size() << "\">" << endl;
  print_radial_function(os,nlcc_);
  os << "</core_density>" << endl;
}

// print any radial function
void Species::print_radial_function(std::ostream& os,
  const std::vector<double>& rad_func)
{
  for ( int i = 0; i < rad_func.size(); i++ )
    os << rad_func[i] << endl;
}

Francois Gygi committed
1341 1342 1343 1344
void Species::info(ostream &os)
{
  os.setf(ios::left,ios::adjustfield);

Francois Gygi committed
1345 1346 1347 1348 1349 1350 1351 1352 1353
  if ( uri() != "" )
  {
    os <<"<species name=\"" << name()
       << "\" href=\"" << uri() << "\">" << endl;
  }
  else
  {
    os <<"<species name=\"" << name() << "\">" << endl;
  }
1354
  os << " <description>" << description() << " </description>" << endl;
Francois Gygi committed
1355 1356 1357
  os << " <symbol>" << symbol() << "</symbol>" << endl;
  os << " <atomic_number>" << atomic_number() << "</atomic_number>" << endl;
  os << " <mass>" << mass() << "</mass>" << endl;
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
  if ( type_ == NCPP )
  {
    os << " <norm_conserving_pseudopotential>" << endl;
    os << " <valence_charge>" << zval() << "</valence_charge>" << endl;
    os << " <lmax>" << lmax() << "</lmax>" << endl;
    os << " <llocal>" << llocal() << "</llocal>" << endl;
    os << " <nquad>" << nquad() << "</nquad>" << endl;
    os << " <rquad>" << rquad() << "</rquad>" << endl;
    os << " <mesh_spacing>" << deltar() << "</mesh_spacing>" << endl;
    os << " </norm_conserving_pseudopotential>" << endl;
  }
  else
  {
    os << " <norm_conserving_semilocal_pseudopotential>" << endl;
    os << " <valence_charge>" << zval() << "</valence_charge>" << endl;
    os << " <mesh_spacing>" << deltar() << "</mesh_spacing>" << endl;
    os << " </norm_conserving_semilocal_pseudopotential>" << endl;
  }
Francois Gygi committed
1376 1377
  os << "</species>" << endl;

Francois Gygi committed
1378
  // describe type of potential
1379
  if ( type_ == NCPP )
Francois Gygi committed
1380
  {
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
    if ( nquad() == 0 )
    {
      if ( lmax() == 0 )
        os << " local potential" << endl;
      else
        os << " Kleinman-Bylander potential" << endl;
    }
    else if ( nquad() > 0 )
    {
      os << " NCPP potential with " << nquad()
         << " quadrature points in [0.0, " << rquad() << "]" << endl;
      os << " local (within 1.e-6) beyond r = " << rcut_loc(1.e-6) << endl;
      os << " local (within 1.e-5) beyond r = " << rcut_loc(1.e-5) << endl;
      os << " local (within 1.e-4) beyond r = " << rcut_loc(1.e-4) << endl;
      os << " local (within 1.e-3) beyond r = " << rcut_loc(1.e-3) << endl;
    }
Francois Gygi committed
1397
  }
1398
  else if ( type_ == SLPP )
Francois Gygi committed
1399
  {
1400
    os << " SLPP semilocal potential" << endl;
Francois Gygi committed
1401
  }
1402
  os << " rcps_ =   " << rcps() << endl;
Francois Gygi committed
1403
  os.setf(ios::right,ios::adjustfield);
Francois Gygi committed
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
}

////////////////////////////////////////////////////////////////////////////////
double Species::rcut_loc(double epsilon)
{
  // find radius at which the largest deviation delta_vnl(r) is < epsilon
  double delta = 0.0;
  int i = ndft_-1;
  while ( ( delta < epsilon ) && i > 0 )
  {
    i--;
    for ( int l = 0; l <= lmax_; l++ )
    {
      if ( l != llocal_ )
      {
        // compute deviation vps_[l][i]
1420
        double dv = fabs( vps_spl_[l][i] - vps_spl_[llocal_][i] );
Francois Gygi committed
1421 1422 1423 1424 1425 1426
        delta = dv > delta ? dv : delta;
      }
    }
  }
  // adjust i so that delta_v[i] < epsilon
  if ( i < ndft_-1 ) i++;
1427

1428
  return rps_spl_[i];
Francois Gygi committed
1429
}
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480

////////////////////////////////////////////////////////////////////////////////
// helper function that extract l and n from projector index
Species::ProjectorData Species::get_proj_data(int ipr)
{
  ProjectorData res;
  res.l = -1;
  res.n = 0;
  int jpr = 0;
  for ( int iop = 0; iop < nop_; iop++ )
  {
    int l = lmap_[iop];
    // n labels differentiates between projectors of same l
    if ( res.l == l ) res.n++;
    // reset n if new angular momentum is found
    else
    {
      res.l = l;
      res.n = 0;
    }
    // there is one projector Y_lm
    for ( res.m = -l; res.m <= l; res.m++ )
    {
      if ( ipr == jpr ) return res;
      jpr++;
    }
  }
  // projector out of bounds
  assert(false);
  res.l = -1;
  res.n = 0;
  return res;
}

////////////////////////////////////////////////////////////////////////////////
// extract D matrix
double Species::dmatrix(int ipr, int jpr)
{
  // extract angular momentum of projectors
  const ProjectorData iem = get_proj_data(ipr);
  const ProjectorData jem = get_proj_data(jpr);

  // projectors have only nonzero overlap if diagonal in l and m
  if ( iem.l != jem.l || iem.m != jem.m ) return 0;

  // if dmatrix is not present identity matrix is assumed
  if ( d_.size() <= iem.l || d_[iem.l].size() == 0 ) return 1;

  // return dmatrix
  return d_[iem.l][iem.n][jem.n];
}