Wavefunction.C 22.1 KB
Newer Older
Francois Gygi committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
//
// Wavefunction.C
//
////////////////////////////////////////////////////////////////////////////////
6
// $Id: Wavefunction.C,v 1.18 2004-11-10 22:33:05 fgygi Exp $
Francois Gygi committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

#include "Wavefunction.h"
#include "SlaterDet.h"
#include <vector>
#include <iomanip>
#if USE_CSTDIO_LFS
#include <sstream>
#include <cstdio>
#endif
using namespace std;

////////////////////////////////////////////////////////////////////////////////
Wavefunction::Wavefunction(const Context& ctxt) : ctxt_(ctxt), nel_(0), 
nempty_(0), nspin_(1), deltaspin_(0), ecut_(0.0), nrowmax_(32) 
{
  // 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();
  allocate();
}

////////////////////////////////////////////////////////////////////////////////
Wavefunction::Wavefunction(const Wavefunction& wf) : ctxt_(wf.ctxt_), 
nel_(wf.nel_), nempty_(wf.nempty_), nspin_(wf.nspin_), 
34 35
deltaspin_(wf.deltaspin_), nrowmax_(wf.nrowmax_), 
cell_(wf.cell_), refcell_(wf.refcell_),
Francois Gygi committed
36 37
ecut_(wf.ecut_), weight_(wf.weight_), kpoint_(wf.kpoint_)
{
38 39
  // Create a Wavefunction using the dimensions of the argument
  
Francois Gygi committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  compute_nst();
  
  // Next lines: do special allocation of contexts to ensure that 
  // contexts are same as those of wf
  
  // create sd contexts and SlaterDets
  const int nkp = kpoint_.size();
  
  assert(ctxt_.active());
  
  assert(nspin_ == 1);
  assert(nkp == 1);
  
  spincontext_.resize(1);
  sdcontext_.resize(1);
  sdcontext_[0].resize(1);
  sd_.resize(1);
  sd_[0].resize(1);
  
  spincontext_[0] = new Context(*wf.spincontext_[0]);
  sdcontext_[0][0] = 0;
  sd_[0][0] = 0;
  if ( spincontext_[0]->active() )
  {
    sdcontext_[0][0] = new Context(*wf.spincontext_[0]);
    sd_[0][0] = new SlaterDet(*sdcontext_[0][0],kpoint_[0]);
  }
  
68 69
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
}

////////////////////////////////////////////////////////////////////////////////
Wavefunction::~Wavefunction()
{
  deallocate();
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::allocate(void)
{
  // create sd contexts and SlaterDets
  const int nkp = kpoint_.size();
  
  assert(ctxt_.active());
  
  assert(nspin_ == 1);
  assert(nkp == 1);
  
89 90 91 92 93 94 95 96
  // 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;

Francois Gygi committed
97 98 99 100 101 102
  spincontext_.resize(1);
  sdcontext_.resize(1);
  sdcontext_[0].resize(1);
  sd_.resize(1);
  sd_[0].resize(1);
  
103 104
  spincontext_[0] = new Context(npr,npc);
  //cout << *spincontext_[0];
Francois Gygi committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
  sdcontext_[0][0] = 0;
  sd_[0][0] = 0;
  if ( spincontext_[0]->active() )
  {
    sdcontext_[0][0] = new Context(*spincontext_[0]);
    sd_[0][0] = new SlaterDet(*sdcontext_[0][0],kpoint_[0]);
  }
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::deallocate(void)
{
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    if ( spincontext_[ispin] != 0 )
    {
      for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
      {
        if ( sdcontext_[ispin][ikp] != 0 )
        {
          delete sd_[ispin][ikp];
          delete sdcontext_[ispin][ikp];
        }
      }
      delete spincontext_[ispin];
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::clear(void)
{
  for ( int ispin = 0; ispin < nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < nkp(); ikp++ )
    {
      if ( sd(ispin,ikp) != 0 )
      {
        if ( sdcontext(ispin,ikp)->active() )
        {
          sd(ispin,ikp)->c().clear();
        }
      }
    }
  }
}
////////////////////////////////////////////////////////////////////////////////
int Wavefunction::nkp(void) const { return kpoint_.size(); } 

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

////////////////////////////////////////////////////////////////////////////////
int Wavefunction::nst() const
{ 
  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

////////////////////////////////////////////////////////////////////////////////
int Wavefunction::nspin() const { return nspin_; } // number of empty states

////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
180 181 182 183 184 185
double Wavefunction::entropy(void) const 
{
  assert(nspin_==1);
  assert(kpoint_.size()==1);
  return sd(0,0)->entropy(nspin_);
}
Francois Gygi committed
186 187 188 189 190

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::resize(const UnitCell& cell, const UnitCell& refcell, 
  double ecut)
{
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  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++ )
      {
        if ( sdcontext_[ispin][ikp]!= 0 && sdcontext_[ispin][ikp]->active() )
        {
          sd_[ispin][ikp]->resize(cell,refcell,ecut,nst_[ispin]);
        }
      }
    }
    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;
  }
Francois Gygi committed
220
  
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
}
  
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::reset(void)
{
  // reset all SlaterDets
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
      if ( sdcontext_[ispin][ikp]!= 0 && sdcontext_[ispin][ikp]->active() )
      {
        sd_[ispin][ikp]->reset();
      }
    }
  }
Francois Gygi committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
}
  
////////////////////////////////////////////////////////////////////////////////
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)
{
260
  if ( nel == nel_ ) return;
Francois Gygi committed
261 262 263 264 265 266 267 268
  if ( nel < 0 )
  {
    cout << " Wavefunction::set_nel: nel < 0" << endl;
    return;
  }
  
  nel_ = nel;
  compute_nst();
269 270
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
271 272 273 274 275
}
  
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::set_nempty(int nempty)
{
276
  if ( nempty == nempty_ ) return;
Francois Gygi committed
277 278 279 280 281 282 283
  if ( nempty < 0 )
  {
    cout << " Wavefunction::set_nempty: negative value" << endl;
    return;
  }
  nempty_ = nempty;
  compute_nst();
284 285
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
286 287 288 289 290 291 292 293 294 295 296 297 298
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::set_nspin(int nspin)
{
  assert(nspin==1 || nspin==2);
  if ( nspin == nspin_ ) return;
  
  deallocate();
  cout << " Wavefunction::set_nspin: " << nspin << " deallocate done" << endl;
  
  nspin_ = nspin;
  
299
  compute_nst();
Francois Gygi committed
300 301
  allocate();
  cout << " Wavefunction::set_nspin: " << nspin << " allocate done" << endl;
302 303
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
304 305 306 307 308 309 310 311 312 313 314 315 316
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::set_nrowmax(int n)
{
  if ( n > ctxt_.size() )
  {
    cout << " Wavefunction::set_nrowmax: nrowmax > ctxt_.size()" << endl;
    return;
  }
  
  deallocate();
  nrowmax_ = n;
317
  compute_nst();
Francois Gygi committed
318
  allocate();
319 320
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
}
  
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::add_kpoint(D3vector kpoint, double weight)
{
  for ( int i = 0; i < kpoint_.size(); i++ )
  {
    if ( kpoint == kpoint_[i] )
    {
      cout << " Wavefunction::add_kpoint: warning: kpoint already defined" 
           << endl;
      //!! return;
    }
  }
  
  deallocate();
  cout << " Wavefunction::add_kpoint: " << kpoint << " deallocate done" << endl;
  
  kpoint_.push_back(kpoint);
  weight_.push_back(weight);
  
  allocate();
  cout << " Wavefunction::add_kpoint: " << kpoint << " allocate done" << endl;
344 345
  resize(cell_,refcell_,ecut_);
  reset();
Francois Gygi committed
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
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::del_kpoint(D3vector kpoint)
{
  cout << " Wavefunction::del_kpoint: not implemented" << endl;
  assert(false);
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::randomize(double amplitude)
{
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
      if ( sd_[ispin][ikp] != 0 && sdcontext_[ispin][ikp]->active() )
      {
        sd_[ispin][ikp]->randomize(amplitude);
      }
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::update_occ(double temp)
{
Francois Gygi committed
373 374
  // update occupation numbers using eigenvalues in SlaterDet
  if ( temp == 0.0 )
Francois Gygi committed
375
  {
Francois Gygi committed
376 377
    // zero temperature
    if ( nspin_ == 1 )
Francois Gygi committed
378
    {
Francois Gygi committed
379
      for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
Francois Gygi committed
380
      {
Francois Gygi committed
381 382 383 384
        if ( sd_[0][ikp] != 0 && sdcontext_[0][ikp]->active() )
        {
          sd_[0][ikp]->update_occ(nel_,nspin_);
        }
Francois Gygi committed
385 386
      }
    }
Francois Gygi committed
387
    else if ( nspin_ == 2 )
Francois Gygi committed
388
    {
Francois Gygi committed
389 390 391 392 393 394 395 396 397
      const int nocc_up = (nel_+1)/2+deltaspin_;
      const int nocc_dn = nel_/2 - deltaspin_;
      for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
      {
        if ( sd_[0][ikp] != 0 && sdcontext_[0][ikp]->active() )
          sd_[0][ikp]->update_occ(nocc_up,nspin_);
        if ( sd_[1][ikp] != 0 && sdcontext_[1][ikp]->active() )
          sd_[1][ikp]->update_occ(nocc_dn,nspin_);
      }
Francois Gygi committed
398 399 400
    }
    else
    {
Francois Gygi committed
401 402
      // incorrect value of nspin_
      assert(false);
Francois Gygi committed
403
    }
Francois Gygi committed
404 405 406 407 408
  }
  else
  {
    // finite temperature
    const double eVolt = 0.036749023; // 1 eV in Hartree
409
    const int maxiter = 500;
Francois Gygi committed
410 411 412
 
    // loop to find value of mu
    double mu = 0.0;
413
    double dmu = 2.0 * eVolt;
Francois Gygi committed
414 415 416 417 418 419
    //!! double totalcharge = (double) ( nel_ + netcharge_ );
    const double totalcharge = (double) nel_;
    enum direction { up, down };
    direction dir = up;

    double rhosum = 0.0;
Francois Gygi committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    for ( int ispin = 0; ispin < nspin_; ispin++ )
    {
      for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
      {
        if ( sd_[ispin][ikp] != 0 && sdcontext_[ispin][ikp]->active() )
        {
          sd_[ispin][ikp]->update_occ(nspin_,mu,temp);
          rhosum += sd_[ispin][ikp]->total_charge();
        }
        //!! rhosum must be reduced on pe 0 of each sdcontext only
        //!! without reduction, works only if nspin_==1 and nkp_==1
        assert(nspin_==1);
        assert(kpoint_.size()==1);
      }
    }
Francois Gygi committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
 
    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++ )
        {
          if ( sd_[ispin][ikp] != 0 && sdcontext_[ispin][ikp]->active() )
          {
            sd_[ispin][ikp]->update_occ(nspin_,mu,temp);
            rhosum += sd_[ispin][ikp]->total_charge();
          }
          //!! rhosum must be reduced on pe 0 of each sdcontext only
          //!! without reduction, works only if nspin_==1 and nkp_==1
          assert(nspin_==1);
          assert(kpoint_.size()==1);
        }
      }
    }
 
    if ( niter == maxiter )
    {
      cout << "Wavefunction::update_occ: mu did not converge in "
           << maxiter << " iterations" << endl;
      ctxt_.abort(1);
    }
Francois Gygi committed
476

Francois Gygi committed
477
    if ( ctxt_.onpe0() )
Francois Gygi committed
478
    {
Francois Gygi committed
479 480 481 482 483 484 485 486 487 488 489
      //!! print on one process only
      cout << " <!-- Wavefunction::update_occ: sum = "
           << rhosum << " -->" << endl;
      cout << " <!-- Wavefunction::update_occ: mu = "
           << setprecision(4) << mu / eVolt << " eV" << " -->" << endl;

      cout.setf(ios::right,ios::adjustfield);
      cout.setf(ios::fixed,ios::floatfield);
 
      cout << " <!-- Wavefunction::update_occ: occupation numbers" << endl;
      for ( int ispin = 0; ispin < nspin_; ispin++ )
Francois Gygi committed
490
      {
Francois Gygi committed
491
        for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
Francois Gygi committed
492
        {
Francois Gygi committed
493 494 495 496 497 498 499 500
          if ( sd_[ispin][ikp] != 0 && sdcontext_[ispin][ikp]->active() )
          {
            for ( int n = 0; n < sd_[ispin][ikp]->nst(); n++ )
            {
              cout << setw(7) << setprecision(4) << sd_[ispin][ikp]->occ(n);
              if ( ( n%10 ) == 9 ) cout << endl;
            }
          }
Francois Gygi committed
501
        }
Francois Gygi committed
502
        cout << "  -->" << endl;
Francois Gygi committed
503 504 505 506 507 508
      }
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
509
void Wavefunction::gram(void)
Francois Gygi committed
510
{
Francois Gygi committed
511
  for ( int ispin = 0; ispin < nspin_; ispin++ )
Francois Gygi committed
512 513 514
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
Francois Gygi committed
515
      if ( sd_[ispin][ikp] != 0 && sdcontext_[ispin][ikp]->active() )
Francois Gygi committed
516
      {
Francois Gygi committed
517
        sd_[ispin][ikp]->gram();
Francois Gygi committed
518 519 520
      }
    }
  }
Francois Gygi committed
521 522 523 524 525 526 527
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::riccati(Wavefunction& wf)
{
  assert(wf.context() == ctxt_);
  for ( int ispin = 0; ispin < nspin_; ispin++ )
Francois Gygi committed
528 529 530
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
Francois Gygi committed
531 532 533 534
      if ( sd_[ispin][ikp] != 0 && sdcontext_[ispin][ikp]->active() )
      {
        sd_[ispin][ikp]->riccati(*wf.sd_[ispin][ikp]);
      }
Francois Gygi committed
535 536 537 538 539
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
540
void Wavefunction::align(Wavefunction& wf)
Francois Gygi committed
541
{
Francois Gygi committed
542
  assert(wf.context() == ctxt_);
Francois Gygi committed
543 544 545 546 547 548
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
      if ( sd_[ispin][ikp] != 0 && sdcontext_[ispin][ikp]->active() )
      {
Francois Gygi committed
549
        sd_[ispin][ikp]->align(*wf.sd_[ispin][ikp]);
Francois Gygi committed
550 551 552 553 554 555
      }
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
556
double Wavefunction::dot(const Wavefunction& wf) const
Francois Gygi committed
557 558
{
  assert(wf.context() == ctxt_);
Francois Gygi committed
559
  double sum = 0.0;
Francois Gygi committed
560 561 562 563 564 565
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
      if ( sd_[ispin][ikp] != 0 && sdcontext_[ispin][ikp]->active() )
      {
Francois Gygi committed
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
        sum += sd_[ispin][ikp]->dot(*wf.sd_[ispin][ikp]);
      }
    }
  }
  return sum;
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::diag(Wavefunction& dwf, bool eigvec)
{
  // subspace diagonalization of <*this | dwf>
  // if eigvec==true, eigenvectors are computed and stored in *this, dwf is 
  // overwritten
  for ( int ispin = 0; ispin < nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < nkp(); ikp++ )
    {
      if ( sd(ispin,ikp) != 0 )
      {
        if ( sdcontext(ispin,ikp)->active() )
        {
          // compute eigenvalues
          if ( sd(ispin,ikp)->basis().real() )
          {
            // proxy real matrices c, cp
            DoubleMatrix c(sd(ispin,ikp)->c());
            DoubleMatrix cp(dwf.sd(ispin,ikp)->c());
 
            DoubleMatrix h(c.context(),c.n(),c.n(),c.nb(),c.nb());
            valarray<double> w(h.m());
 
            // 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);
 
            // cout << " Hamiltonian at k = " << sd(ispin,ikp)->kpoint()
            //      << endl;
            // cout << h;
 
            if ( eigvec )
            {
              DoubleMatrix z(c.context(),c.n(),c.n(),c.nb(),c.nb());
              h.syev('l',w,z);
              cp = c;
              c.gemm('n','n',1.0,cp,z,0.0);
            }
            else
            {
              h.syev('l',w);
            }
            if ( ctxt_.onpe0() )
            {
              const double eVolt = 2.0 * 13.6058;
              cout <<    "  <eigenvalues spin=\"" << ispin
                   << "\" kpoint=\"" << sd(ispin,ikp)->kpoint()
                   << "\" n=\"" << h.m() << "\">" << endl;
              for ( int i = 0; i < h.m(); i++ )
              {
                cout << setw(10) << setprecision(5) << w[i]*eVolt;
                if ( i%5 == 4 ) cout << endl;
              }
              if ( h.m()%5 != 0 ) cout << endl;
              cout << "  </eigenvalues>" << endl;
            }
            // set eigenvalues in SlaterDet
            sd(ispin,ikp)->set_eig(w);
          }
          else
          {
            // complex case not implemented
            assert(false);
            #if 0
            ComplexMatrix& c(wf.sd[ikp]->c());
            ComplexMatrix& cp(dwf.sd[ikp]->c());
 
            ComplexMatrix h(c.context(),c.n(),c.n(),c.nb(),c.nb());
 
            h.gemm('c','n',1.0,c,cp,0.0);
 
            //cout << " Hamiltonian at k = " << sd[ikp]->kpoint() << endl;
            //cout << h;
 
            valarray<double> w(h.m());

            h.heev('l',w);
            cout << " Eigenvalues at k = " << sd[ikp]->kpoint() << endl;
            const double eVolt = 2.0 * 13.6058;
            for ( int i = 0; i < h.m(); i++ )
            {
              cout << "%" << setw(3) << ikp
                   << setw(10) << setprecision(5) << w[i]*eVolt << endl;;
            }
            #endif
          }
        }
Francois Gygi committed
662 663 664 665 666 667 668 669
      }
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::print(ostream& os, string encoding, string tag)
{
670 671 672 673 674 675
  if ( ctxt_.onpe0() )
  {
    os << "<" << tag << " ecut=\"" << ecut_ << "\""
       << " nspin=\"" << nspin_ << "\""
       << " nel=\"" << nel_ << "\""
       << " nempty=\"" << nempty_ << "\">" << endl;
676
    os << setprecision(10);
677 678 679 680 681 682 683 684
    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;
  }
Francois Gygi committed
685 686 687 688 689 690
     
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
      sd_[ispin][ikp]->print(os,encoding);
  }
691 692 693
  
  if ( ctxt_.onpe0() )
    os << "</" << tag << ">" << endl;
Francois Gygi committed
694 695 696 697 698 699 700 701 702 703 704 705 706
}

#if USE_CSTDIO_LFS
////////////////////////////////////////////////////////////////////////////////
void Wavefunction::write(FILE* outfile, string encoding, string tag)
{
  if ( ctxt_.onpe0() )
  {
    ostringstream os;
    os << "<" << tag << " ecut=\"" << ecut_ << "\""
       << " nspin=\"" << nspin_ << "\""
       << " nel=\"" << nel_ << "\""
       << " nempty=\"" << nempty_ << "\">" << endl;
707
    os << setprecision(10);
Francois Gygi committed
708 709 710 711
    os << "<domain a=\""
       << cell_.a(0) << "\"\n        b=\""
       << cell_.a(1) << "\"\n        c=\""
       << cell_.a(2) << "\"/>" << endl;
712 713 714 715 716 717 718
    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
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
    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());
    off_t len = str.length();
    fwrite(str.c_str(),sizeof(char),len,outfile);
  }

  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
      sd_[ispin][ikp]->write(outfile,encoding);
  }
  
  if ( ctxt_.onpe0() )
  {
    ostringstream os;
    os << "</" << tag << ">" << endl;
    string str(os.str());
    off_t len = str.length();
    fwrite(str.c_str(),sizeof(char),len,outfile);
  }
}
#endif

////////////////////////////////////////////////////////////////////////////////
void Wavefunction::info(ostream& os, string tag)
{
747 748 749 750 751 752
  if ( ctxt_.onpe0() )
  {
    os << "<" << tag << " ecut=\"" << ecut_ << "\""
       << " nspin=\"" << nspin_ << "\""
       << " nel=\"" << nel_ << "\""
       << " nempty=\"" << nempty_ << "\">" << endl;
753
    os.setf(ios::fixed,ios::floatfield);
754
    os << "<cell a=\""
755 756
       << setprecision(6) << cell_.a(0) << "\"\n      b=\""
       << cell_.a(1) << "\"\n      c=\""
757
       << cell_.a(2) << "\"/>" << endl;
758
    os << "<refcell a=\""
759 760
       << refcell_.a(0) << "\"\n         b=\""
       << refcell_.a(1) << "\"\n         c=\""
761
       << refcell_.a(2) << "\"/>" << endl;
762 763 764 765
    os << "<grid nx=\"" << sd_[0][0]->basis().np(0) << "\""
       <<      " ny=\"" << sd_[0][0]->basis().np(1) << "\""
       <<      " nz=\"" << sd_[0][0]->basis().np(2) << "\"/>" << endl;
  }
Francois Gygi committed
766 767 768 769 770 771
     
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
      sd_[ispin][ikp]->info(os);
  }
772 773 774
  
  if ( ctxt_.onpe0() )
    os << "</" << tag << ">" << endl;
Francois Gygi committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
}

////////////////////////////////////////////////////////////////////////////////
ostream& operator<<(ostream& os, Wavefunction& wf)
{
  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_);
792
  assert(nrowmax_ == wf.nrowmax_);
Francois Gygi committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
  assert(deltaspin_ == wf.deltaspin_);
  assert(cell_ == wf.cell_);
  assert(refcell_ == wf.refcell_);
  assert(ecut_ == wf.ecut_);
  
  weight_ = wf.weight_;
  kpoint_ = wf.kpoint_;
  
  for ( int ispin = 0; ispin < nspin_; ispin++ )
  {
    for ( int ikp = 0; ikp < kpoint_.size(); ikp++ )
    {
      if ( sd_[ispin][ikp] != 0 && sdcontext_[ispin][ikp]->active() )
      {
        *sd_[ispin][ikp] = *wf.sd_[ispin][ikp];
      }
    }
  }
  return *this;
}