Wavefunction.C 21.4 KB
Newer Older
Francois Gygi committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
//
// Wavefunction.C
//
////////////////////////////////////////////////////////////////////////////////
6
// $Id: Wavefunction.C,v 1.29 2008-02-12 05:39:18 fgygi Exp $
Francois Gygi committed
7 8 9

#include "Wavefunction.h"
#include "SlaterDet.h"
10
#include "jacobi.h"
Francois Gygi committed
11 12 13 14 15 16
#include <vector>
#include <iomanip>
#include <sstream>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
17 18
Wavefunction::Wavefunction(const Context& ctxt) : ctxt_(ctxt), nel_(0),
nempty_(0), nspin_(1), deltaspin_(0), ecut_(0.0), nrowmax_(32)
Francois Gygi committed
19 20 21 22 23 24 25
{
  // create a default wavefunction: one k point, k=0
  kpoint_.resize(1);
  kpoint_[0] = D3vector(0,0,0);
  weight_.resize(1);
  weight_[0] = 1.0;
  compute_nst();
26
  create_contexts();
Francois Gygi committed
27 28 29 30
  allocate();
}

////////////////////////////////////////////////////////////////////////////////
31 32 33
Wavefunction::Wavefunction(const Wavefunction& wf) : ctxt_(wf.ctxt_),
nel_(wf.nel_), nempty_(wf.nempty_), nspin_(wf.nspin_),
deltaspin_(wf.deltaspin_), nrowmax_(wf.nrowmax_),
34
cell_(wf.cell_), refcell_(wf.refcell_),
Francois Gygi committed
35 36
ecut_(wf.ecut_), weight_(wf.weight_), kpoint_(wf.kpoint_)
{
37
  // Create a Wavefunction using the dimensions of the argument
38

Francois Gygi committed
39
  compute_nst();
40 41

  // Next lines: do special allocation of contexts to ensure that
Francois Gygi committed
42
  // contexts are same as those of wf
43
  spincontext_ = new Context(*wf.spincontext_);
44 45
  kpcontext_ = new Context(*wf.kpcontext_);
  sdcontext_ = new Context(*wf.sdcontext_);
Francois Gygi committed
46

47
  allocate();
48

49 50
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
51 52 53 54 55 56
}

////////////////////////////////////////////////////////////////////////////////
Wavefunction::~Wavefunction()
{
  deallocate();
57 58 59
  delete spincontext_;
  delete kpcontext_;
  delete sdcontext_;
Francois Gygi committed
60 61 62 63 64
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::allocate(void)
{
65
  // create SlaterDets using kpoint list
Francois Gygi committed
66 67
  const int nkp = kpoint_.size();
  assert(nspin_ == 1);
Francois Gygi committed
68 69 70 71 72
  sd_.resize(nspin_);
  sd_[0].resize(nkp);

  for ( int ikp = 0; ikp < nkp; ikp++ )
    sd_[0][ikp] = new SlaterDet(*sdcontext_,kpoint_[ikp]);
Francois Gygi committed
73 74 75 76 77
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::deallocate(void)
{
78 79
  assert (nspin_==1);
  for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
Francois Gygi committed
80
  {
81
    delete sd_[0][ikp];
Francois Gygi committed
82
  }
83
  sd_[0].resize(0);
Francois Gygi committed
84 85 86 87 88 89 90 91 92
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::clear(void)
{
  for ( int ispin = 0; ispin < nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < nkp(); ikp++ )
    {
Francois Gygi committed
93
      sd(ispin,ikp)->c().clear();
Francois Gygi committed
94 95 96
    }
  }
}
Francois Gygi committed
97

Francois Gygi committed
98
////////////////////////////////////////////////////////////////////////////////
99
int Wavefunction::nkp(void) const { return kpoint_.size(); }
Francois Gygi committed
100 101 102 103 104 105

////////////////////////////////////////////////////////////////////////////////
int Wavefunction::nel() const { return nel_; } // total number of electrons

////////////////////////////////////////////////////////////////////////////////
int Wavefunction::nst() const
106
{
Francois Gygi committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  if ( nspin_ == 1 )
    return nst_[0];
  else
    return nst_[0]+nst_[1];
}

////////////////////////////////////////////////////////////////////////////////
int Wavefunction::nst(int ispin) const
{
  assert(ispin >= 0 && ispin < 2);
  return nst_[ispin];
}

////////////////////////////////////////////////////////////////////////////////
int Wavefunction::nempty() const { return nempty_; } // number of empty states

////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
124
int Wavefunction::nspin() const { return nspin_; } // number of spin components
Francois Gygi committed
125 126

////////////////////////////////////////////////////////////////////////////////
127
double Wavefunction::entropy(void) const
Francois Gygi committed
128
{
Francois Gygi committed
129 130 131 132 133
  double sum = 0.0;
  for ( int ispin = 0; ispin < nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < nkp(); ikp++ )
    {
134
      sum += weight_[ikp] * sd(ispin,ikp)->entropy(nspin_);
Francois Gygi committed
135 136 137
    }
  }
  return sum;
Francois Gygi committed
138
}
Francois Gygi committed
139 140

////////////////////////////////////////////////////////////////////////////////
141
void Wavefunction::resize(const UnitCell& cell, const UnitCell& refcell,
Francois Gygi committed
142 143
  double ecut)
{
144 145 146 147 148 149 150
  try
  {
    // resize all SlaterDets using cell, refcell, ecut and nst_[ispin]
    for ( int ispin = 0; ispin < nspin_; ispin++ )
    {
      for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
      {
Francois Gygi committed
151
        sd_[ispin][ikp]->resize(cell,refcell,ecut,nst_[ispin]);
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
      }
    }
    cell_ = cell;
    refcell_ = refcell;
    ecut_ = ecut;
  }
  catch ( const SlaterDetException& sdex )
  {
    cout << " Wavefunction: SlaterDetException during resize: " << endl
         << sdex.msg << endl;
    // no resize took place
    return;
  }
  catch ( bad_alloc )
  {
    cout << " Wavefunction: insufficient memory for resize operation" << endl;
    return;
  }
170

171
}
172

173 174 175 176 177 178 179 180
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::reset(void)
{
  // reset all SlaterDets
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
Francois Gygi committed
181
      sd_[ispin][ikp]->reset();
182 183
    }
  }
Francois Gygi committed
184
}
185

Francois Gygi committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::compute_nst(void)
{
  // recompute nst from nel_, deltaspin_, nempty_

  nst_.resize(nspin_);
  if ( nspin_ == 1 )
  {
    nst_[0] = ( nel_ + 1 ) / 2 + nempty_;
  }
  else
  {
    // nspin == 2
    nst_[0] = ( nel_ + 1 ) / 2 + deltaspin_ + nempty_;
    nst_[1] = nel_ / 2 - deltaspin_ + nempty_;
  }
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::set_nel(int nel)
{
207
  if ( nel == nel_ ) return;
Francois Gygi committed
208 209 210 211 212
  if ( nel < 0 )
  {
    cout << " Wavefunction::set_nel: nel < 0" << endl;
    return;
  }
213

Francois Gygi committed
214 215
  nel_ = nel;
  compute_nst();
216 217
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
218
}
219

Francois Gygi committed
220 221 222
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::set_nempty(int nempty)
{
223
  if ( nempty == nempty_ ) return;
Francois Gygi committed
224 225 226 227 228 229 230
  if ( nempty < 0 )
  {
    cout << " Wavefunction::set_nempty: negative value" << endl;
    return;
  }
  nempty_ = nempty;
  compute_nst();
231 232
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
233 234 235 236 237 238 239
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::set_nspin(int nspin)
{
  assert(nspin==1 || nspin==2);
  if ( nspin == nspin_ ) return;
240

Francois Gygi committed
241 242
  deallocate();
  cout << " Wavefunction::set_nspin: " << nspin << " deallocate done" << endl;
243

Francois Gygi committed
244
  nspin_ = nspin;
245

246
  compute_nst();
Francois Gygi committed
247 248
  allocate();
  cout << " Wavefunction::set_nspin: " << nspin << " allocate done" << endl;
249 250
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::create_contexts(void)
{
  // determine dimensions of sdcontext
  assert(nrowmax_>0);
  const int size = ctxt_.size();
  int npr = nrowmax_;
  while ( size%npr != 0 ) npr--;
  // npr now divides size
  int npc = size/npr;

  spincontext_ = new Context(npr,npc);
  kpcontext_ = new Context(*spincontext_);
  sdcontext_ = new Context(*kpcontext_);
}

Francois Gygi committed
269 270 271 272 273
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::set_nrowmax(int n)
{
  if ( n > ctxt_.size() )
  {
274 275
    if ( ctxt_.onpe0() )
      cout << " Wavefunction::set_nrowmax: nrowmax > ctxt_.size()" << endl;
Francois Gygi committed
276 277
    return;
  }
278

Francois Gygi committed
279
  deallocate();
280 281 282
  delete spincontext_;
  delete kpcontext_;
  delete sdcontext_;
Francois Gygi committed
283
  nrowmax_ = n;
284
  create_contexts();
285
  compute_nst();
Francois Gygi committed
286
  allocate();
287 288
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
289
}
290

Francois Gygi committed
291 292 293 294 295
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::add_kpoint(D3vector kpoint, double weight)
{
  for ( int i = 0; i < kpoint_.size(); i++ )
  {
Francois Gygi committed
296
    if ( length(kpoint - kpoint_[i]) < 1.e-6 )
Francois Gygi committed
297
    {
Francois Gygi committed
298 299
      if ( ctxt_.onpe0() )
        cout << " Wavefunction::add_kpoint: kpoint already defined"
Francois Gygi committed
300
           << endl;
Francois Gygi committed
301
      return;
Francois Gygi committed
302 303
    }
  }
304

Francois Gygi committed
305 306 307
  deallocate();
  kpoint_.push_back(kpoint);
  weight_.push_back(weight);
308

Francois Gygi committed
309
  allocate();
310 311
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
312
  update_occ(0.0);
Francois Gygi committed
313 314 315 316 317
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::del_kpoint(D3vector kpoint)
{
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
  bool found = false;
  vector<D3vector>::iterator pk = kpoint_.begin();
  vector<double>::iterator pw = weight_.begin();
  while ( !found && pk != kpoint_.end() )
  {
    if ( length(kpoint - *pk) < 1.e-6 )
    {
      found = true;
    }
    else
    {
      pk++;
      pw++;
    }
  }
  if ( !found )
  {
    if ( ctxt_.onpe0() )
      cout << " Wavefunction::del_kpoint: no such kpoint"
         << endl;
    return;
  }
  deallocate();
  kpoint_.erase(pk);
  weight_.erase(pw);
  allocate();
  resize(cell_,refcell_,ecut_);
  reset();
  update_occ(0.0);
Francois Gygi committed
347 348 349 350 351 352 353 354 355
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::randomize(double amplitude)
{
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
Francois Gygi committed
356
      sd_[ispin][ikp]->randomize(amplitude);
Francois Gygi committed
357 358 359 360 361 362 363
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::update_occ(double temp)
{
Francois Gygi committed
364 365
  // update occupation numbers using eigenvalues in SlaterDet
  if ( temp == 0.0 )
Francois Gygi committed
366
  {
Francois Gygi committed
367 368
    // zero temperature
    if ( nspin_ == 1 )
Francois Gygi committed
369
    {
Francois Gygi committed
370
      for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
Francois Gygi committed
371
      {
Francois Gygi committed
372
        sd_[0][ikp]->update_occ(nel_,nspin_);
Francois Gygi committed
373 374
      }
    }
Francois Gygi committed
375
    else if ( nspin_ == 2 )
Francois Gygi committed
376
    {
Francois Gygi committed
377 378 379 380
      const int nocc_up = (nel_+1)/2+deltaspin_;
      const int nocc_dn = nel_/2 - deltaspin_;
      for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
      {
Francois Gygi committed
381 382
        sd_[0][ikp]->update_occ(nocc_up,nspin_);
        sd_[1][ikp]->update_occ(nocc_dn,nspin_);
Francois Gygi committed
383
      }
Francois Gygi committed
384 385 386
    }
    else
    {
Francois Gygi committed
387 388
      // incorrect value of nspin_
      assert(false);
Francois Gygi committed
389
    }
Francois Gygi committed
390 391 392 393 394
  }
  else
  {
    // finite temperature
    const double eVolt = 0.036749023; // 1 eV in Hartree
395
    const int maxiter = 500;
396

Francois Gygi committed
397 398
    // loop to find value of mu
    double mu = 0.0;
399
    double dmu = 2.0 * eVolt;
Francois Gygi committed
400 401 402 403 404
    const double totalcharge = (double) nel_;
    enum direction { up, down };
    direction dir = up;

    double rhosum = 0.0;
Francois Gygi committed
405 406 407 408
    for ( int ispin = 0; ispin < nspin_; ispin++ )
    {
      for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
      {
Francois Gygi committed
409
        sd_[ispin][ikp]->update_occ(nspin_,mu,temp);
410
        rhosum += weight_[ikp] * sd_[ispin][ikp]->total_charge();
Francois Gygi committed
411 412
      }
    }
413

Francois Gygi committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    int niter = 0;
    while ( niter < maxiter && fabs(rhosum - nel_) > 1.e-10 )
    {
      niter++;
      if ( rhosum < totalcharge )
      {
        if ( dir == down ) dmu /= 2.0;
        mu += dmu;
        dir = up;
      }
      else
      {
        if ( dir == up ) dmu /= 2.0;
        mu -= dmu;
        dir = down;
      }
      rhosum = 0.0;
      for ( int ispin = 0; ispin < nspin_; ispin++ )
      {
        for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
        {
Francois Gygi committed
435
          sd_[ispin][ikp]->update_occ(nspin_,mu,temp);
436
          rhosum += weight_[ikp] * sd_[ispin][ikp]->total_charge();
Francois Gygi committed
437 438 439
        }
      }
    }
440

Francois Gygi committed
441 442 443 444 445 446
    if ( niter == maxiter )
    {
      cout << "Wavefunction::update_occ: mu did not converge in "
           << maxiter << " iterations" << endl;
      ctxt_.abort(1);
    }
Francois Gygi committed
447

Francois Gygi committed
448
    if ( ctxt_.onpe0() )
Francois Gygi committed
449
    {
450 451 452 453
      cout << " Wavefunction::update_occ: sum = "
           << rhosum << endl;
      cout << " Wavefunction::update_occ: mu = "
           << setprecision(4) << mu / eVolt << " eV" << endl;
Francois Gygi committed
454 455 456

      cout.setf(ios::right,ios::adjustfield);
      cout.setf(ios::fixed,ios::floatfield);
457

458
      cout << " Wavefunction::update_occ: occupation numbers" << endl;
Francois Gygi committed
459
      for ( int ispin = 0; ispin < nspin_; ispin++ )
Francois Gygi committed
460
      {
Francois Gygi committed
461
        for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
Francois Gygi committed
462
        {
463
          cout << " k = " << kpoint_[ikp] << endl;
Francois Gygi committed
464
          for ( int n = 0; n < sd_[ispin][ikp]->nst(); n++ )
Francois Gygi committed
465
          {
Francois Gygi committed
466 467
            cout << setw(7) << setprecision(4) << sd_[ispin][ikp]->occ(n);
            if ( ( n%10 ) == 9 ) cout << endl;
Francois Gygi committed
468
          }
469 470
          if ( sd_[ispin][ikp]->nst() % 10 != 0 )
            cout << endl;
Francois Gygi committed
471 472 473 474 475 476 477
        }
      }
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
478
void Wavefunction::gram(void)
Francois Gygi committed
479
{
Francois Gygi committed
480
  for ( int ispin = 0; ispin < nspin_; ispin++ )
Francois Gygi committed
481 482 483
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
Francois Gygi committed
484
      sd_[ispin][ikp]->gram();
Francois Gygi committed
485 486
    }
  }
Francois Gygi committed
487 488 489 490 491 492 493
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::riccati(Wavefunction& wf)
{
  assert(wf.context() == ctxt_);
  for ( int ispin = 0; ispin < nspin_; ispin++ )
Francois Gygi committed
494 495 496
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
Francois Gygi committed
497
      sd_[ispin][ikp]->riccati(*wf.sd_[ispin][ikp]);
Francois Gygi committed
498 499 500 501 502
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
503
void Wavefunction::align(Wavefunction& wf)
Francois Gygi committed
504
{
Francois Gygi committed
505
  assert(wf.context() == ctxt_);
Francois Gygi committed
506 507 508 509
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
Francois Gygi committed
510
      sd_[ispin][ikp]->align(*wf.sd_[ispin][ikp]);
Francois Gygi committed
511 512 513 514 515
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
516
complex<double> Wavefunction::dot(const Wavefunction& wf) const
Francois Gygi committed
517 518
{
  assert(wf.context() == ctxt_);
Francois Gygi committed
519
  complex<double> sum = 0.0;
Francois Gygi committed
520 521 522 523
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
Francois Gygi committed
524
      sum += sd_[ispin][ikp]->dot(*wf.sd_[ispin][ikp]);
Francois Gygi committed
525 526 527 528 529 530 531 532 533
    }
  }
  return sum;
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::diag(Wavefunction& dwf, bool eigvec)
{
  // subspace diagonalization of <*this | dwf>
534
  // if eigvec==true, eigenvectors are computed and stored in *this, dwf is
Francois Gygi committed
535 536 537 538 539
  // overwritten
  for ( int ispin = 0; ispin < nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < nkp(); ikp++ )
    {
Francois Gygi committed
540 541
      // compute eigenvalues
      if ( sd(ispin,ikp)->basis().real() )
Francois Gygi committed
542
      {
Francois Gygi committed
543 544 545
        // proxy real matrices c, cp
        DoubleMatrix c(sd(ispin,ikp)->c());
        DoubleMatrix cp(dwf.sd(ispin,ikp)->c());
546

Francois Gygi committed
547
        DoubleMatrix h(c.context(),c.n(),c.n(),c.nb(),c.nb());
548

Francois Gygi committed
549 550 551 552
        // factor 2.0 in next line: G and -G
        h.gemm('t','n',2.0,c,cp,0.0);
        // rank-1 update correction
        h.ger(-1.0,c,0,cp,0);
553

Francois Gygi committed
554 555 556
        // cout << " Hamiltonian at k = " << sd(ispin,ikp)->kpoint()
        //      << endl;
        // cout << h;
557

558
#if 1
Francois Gygi committed
559 560 561 562 563 564 565 566 567 568 569 570 571
        valarray<double> w(h.m());
        if ( eigvec )
        {
          DoubleMatrix z(c.context(),c.n(),c.n(),c.nb(),c.nb());
          h.syev('l',w,z);
          //h.syevx('l',w,z,1.e-6);
          cp = c;
          c.gemm('n','n',1.0,cp,z,0.0);
        }
        else
        {
          h.syev('l',w);
        }
572
#else
Francois Gygi committed
573 574 575 576 577 578 579 580 581
        vector<double> w(h.m());
        DoubleMatrix z(c.context(),c.n(),c.n(),c.nb(),c.nb());
        const int maxsweep = 30;
        int nsweep = jacobi(maxsweep,1.e-6,h,z,w);
        if ( eigvec )
        {
          cp = c;
          c.gemm('n','n',1.0,cp,z,0.0);
        }
582
#endif
Francois Gygi committed
583 584 585 586 587 588 589 590 591
        // set eigenvalues in SlaterDet
        sd(ispin,ikp)->set_eig(w);
      }
      else
      {
        ComplexMatrix& c(sd(ispin,ikp)->c());
        ComplexMatrix& cp(dwf.sd(ispin,ikp)->c());
        ComplexMatrix h(c.context(),c.n(),c.n(),c.nb(),c.nb());
        h.gemm('c','n',1.0,c,cp,0.0);
592 593 594
        // cout << " Hamiltonian at k = "
        //      << sd(ispin,ikp)->kpoint() << endl;
        // cout << h;
Francois Gygi committed
595
        valarray<double> w(h.m());
596
        if ( eigvec )
Francois Gygi committed
597
        {
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
#if DEBUG
          ComplexMatrix hcopy(h);
#endif
          ComplexMatrix z(c.context(),c.n(),c.n(),c.nb(),c.nb());
          h.heev('l',w,z);
          cp = c;
          c.gemm('n','n',1.0,cp,z,0.0);
#if DEBUG
          // check that z contains eigenvectors of h
          // diagonal matrix with eigenvalues on diagonal
          ComplexMatrix d(c.context(),c.n(),c.n(),c.nb(),c.nb());
          // the following test works only on one task
          assert(ctxt_.size()==1);
          for ( int i = 0; i < d.m(); i++ )
            d[i+d.n()*i] = w[i];
          ComplexMatrix dz(c.context(),c.n(),c.n(),c.nb(),c.nb());
          dz.gemm('n','c',1.0,d,z,0.0);
          ComplexMatrix zdz(c.context(),c.n(),c.n(),c.nb(),c.nb());
          zdz.gemm('n','n',1.0,z,dz,0.0);
          // zdz should be equal to hcopy
          zdz -= hcopy;
          cout << " heev: norm of error: " << zdz.nrm2() << endl;
Francois Gygi committed
620
#endif
621 622 623 624 625
        }
        else
        {
          h.heev('l',w);
        }
Francois Gygi committed
626 627
        // set eigenvalues in SlaterDet
        sd(ispin,ikp)->set_eig(w);
Francois Gygi committed
628 629 630 631 632 633
      }
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
634
void Wavefunction::print(ostream& os, string encoding, string tag) const
Francois Gygi committed
635
{
636 637 638 639 640 641
  if ( ctxt_.onpe0() )
  {
    os << "<" << tag << " ecut=\"" << ecut_ << "\""
       << " nspin=\"" << nspin_ << "\""
       << " nel=\"" << nel_ << "\""
       << " nempty=\"" << nempty_ << "\">" << endl;
642
    os << setprecision(10);
643 644 645 646 647 648 649 650
    os << "<domain a=\""
       << cell_.a(0) << "\"\n        b=\""
       << cell_.a(1) << "\"\n        c=\""
       << cell_.a(2) << "\"/>" << endl;
    os << "<grid nx=\"" << sd_[0][0]->basis().np(0) << "\""
       <<      " ny=\"" << sd_[0][0]->basis().np(1) << "\""
       <<      " nz=\"" << sd_[0][0]->basis().np(2) << "\"/>" << endl;
  }
651

Francois Gygi committed
652 653 654
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
Francois Gygi committed
655
      sd_[ispin][ikp]->print(os,encoding,weight_[ikp],ispin,nspin_);
Francois Gygi committed
656
  }
657

658 659
  if ( ctxt_.onpe0() )
    os << "</" << tag << ">" << endl;
Francois Gygi committed
660 661 662
}

////////////////////////////////////////////////////////////////////////////////
663
void Wavefunction::write(MPI_File& fh, string encoding, string tag) const
Francois Gygi committed
664 665 666 667 668 669 670 671
{
  if ( ctxt_.onpe0() )
  {
    ostringstream os;
    os << "<" << tag << " ecut=\"" << ecut_ << "\""
       << " nspin=\"" << nspin_ << "\""
       << " nel=\"" << nel_ << "\""
       << " nempty=\"" << nempty_ << "\">" << endl;
672
    os << setprecision(10);
Francois Gygi committed
673 674 675 676
    os << "<domain a=\""
       << cell_.a(0) << "\"\n        b=\""
       << cell_.a(1) << "\"\n        c=\""
       << cell_.a(2) << "\"/>" << endl;
677 678 679 680 681 682 683
    if ( refcell_.volume() != 0.0 )
    {
      os << "<reference_domain a=\""
         << refcell_.a(0) << "\"\n        b=\""
         << refcell_.a(1) << "\"\n        c=\""
         << refcell_.a(2) << "\"/>" << endl;
    }
Francois Gygi committed
684 685 686 687
    os << "<grid nx=\"" << sd_[0][0]->basis().np(0) << "\""
       <<      " ny=\"" << sd_[0][0]->basis().np(1) << "\""
       <<      " nz=\"" << sd_[0][0]->basis().np(2) << "\"/>" << endl;
    string str(os.str());
688 689 690 691 692
    int len = str.size();
    MPI_Status status;
    int err = MPI_File_write_shared(fh,(void*)str.c_str(),len,MPI_CHAR,&status);
    if ( err != 0 )
      cout << " Wavefunction::write: error in MPI_File_write_shared" << endl;
Francois Gygi committed
693 694
  }

695 696 697 698
  MPI_File_sync(fh);
  ctxt_.barrier();
  MPI_File_sync(fh);

Francois Gygi committed
699 700 701
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
702 703 704
    {
      sd_[ispin][ikp]->write(fh,encoding,weight_[ikp],ispin,nspin_);
    }
Francois Gygi committed
705
  }
706

Francois Gygi committed
707 708 709 710 711
  if ( ctxt_.onpe0() )
  {
    ostringstream os;
    os << "</" << tag << ">" << endl;
    string str(os.str());
712 713 714 715 716
    int len = str.size();
    MPI_Status status;
    int err = MPI_File_write_shared(fh,(void*)str.c_str(),len,MPI_CHAR,&status);
    if ( err != 0 )
      cout << " Wavefunction::write: error in MPI_File_write_shared" << endl;
Francois Gygi committed
717
  }
718 719 720 721 722

  MPI_File_sync(fh);
  ctxt_.barrier();
  MPI_File_sync(fh);

Francois Gygi committed
723 724 725
}

////////////////////////////////////////////////////////////////////////////////
726
void Wavefunction::info(ostream& os, string tag) const
Francois Gygi committed
727
{
728 729 730 731 732 733
  if ( ctxt_.onpe0() )
  {
    os << "<" << tag << " ecut=\"" << ecut_ << "\""
       << " nspin=\"" << nspin_ << "\""
       << " nel=\"" << nel_ << "\""
       << " nempty=\"" << nempty_ << "\">" << endl;
734
    os.setf(ios::fixed,ios::floatfield);
735
    os << "<cell a=\""
736 737
       << setprecision(6) << cell_.a(0) << "\"\n      b=\""
       << cell_.a(1) << "\"\n      c=\""
738
       << cell_.a(2) << "\"/>" << endl;
739
    os << " reciprocal lattice vectors" << endl
Francois Gygi committed
740 741 742
       << setprecision(6)
       << " " << cell_.b(0) << endl
       << " " << cell_.b(1) << endl
743
       << " " << cell_.b(2) << endl;
744
    os << "<refcell a=\""
745 746
       << refcell_.a(0) << "\"\n         b=\""
       << refcell_.a(1) << "\"\n         c=\""
747
       << refcell_.a(2) << "\"/>" << endl;
748 749 750 751
    os << "<grid nx=\"" << sd_[0][0]->basis().np(0) << "\""
       <<      " ny=\"" << sd_[0][0]->basis().np(1) << "\""
       <<      " nz=\"" << sd_[0][0]->basis().np(2) << "\"/>" << endl;
  }
752

Francois Gygi committed
753 754 755
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
Francois Gygi committed
756
    {
757
      if ( ctxt_.onpe0() )
758 759
        cout << " kpoint: " << kpoint_[ikp] << " weight: " << weight_[ikp]
             << endl;
Francois Gygi committed
760
      sd_[ispin][ikp]->info(os);
Francois Gygi committed
761
    }
Francois Gygi committed
762
  }
763

764 765
  if ( ctxt_.onpe0() )
    os << "</" << tag << ">" << endl;
Francois Gygi committed
766 767 768
}

////////////////////////////////////////////////////////////////////////////////
769
ostream& operator<<(ostream& os, const Wavefunction& wf)
Francois Gygi committed
770 771 772 773 774 775 776 777 778 779 780 781 782
{
  wf.print(os,"text","wavefunction");
  return os;
}

////////////////////////////////////////////////////////////////////////////////
Wavefunction& Wavefunction::operator=(const Wavefunction& wf)
{
  if ( this == &wf ) return *this;
  assert(ctxt_ == wf.ctxt_);
  assert(nel_ == wf.nel_);
  assert(nempty_== wf.nempty_);
  assert(nspin_ == wf.nspin_);
783
  assert(nrowmax_ == wf.nrowmax_);
Francois Gygi committed
784 785 786 787
  assert(deltaspin_ == wf.deltaspin_);
  assert(cell_ == wf.cell_);
  assert(refcell_ == wf.refcell_);
  assert(ecut_ == wf.ecut_);
788

Francois Gygi committed
789 790
  weight_ = wf.weight_;
  kpoint_ = wf.kpoint_;
791

792 793 794 795
  assert(*spincontext_ == *wf.spincontext_);
  assert(*kpcontext_   == *wf.kpcontext_);
  assert(*sdcontext_   == *wf.sdcontext_);

Francois Gygi committed
796 797 798 799
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
Francois Gygi committed
800
      *sd_[ispin][ikp] = *wf.sd_[ispin][ikp];
Francois Gygi committed
801 802 803 804
    }
  }
  return *this;
}