MDWavefunctionStepper.C 9.36 KB
Newer Older
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/>.
//
////////////////////////////////////////////////////////////////////////////////
//
15 16 17
// MDWavefunctionStepper.C
//
////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
18
// $Id: MDWavefunctionStepper.C,v 1.12 2008-09-08 15:56:18 fgygi Exp $
19 20 21 22 23 24 25 26 27

#include "MDWavefunctionStepper.h"
#include "Wavefunction.h"
#include "SlaterDet.h"
#include "Sample.h"
#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
28 29
MDWavefunctionStepper::MDWavefunctionStepper(Wavefunction& wf,
  Wavefunction *wfv, double dt, double dt2bye, TimerMap& tmap) :
30
  wfv_(wfv), dt_(dt), dt2bye_(dt2bye), WavefunctionStepper(wf,tmap)
31
{
32
  assert(wfv!=0);
33 34 35 36 37 38 39 40 41 42
}

////////////////////////////////////////////////////////////////////////////////
void MDWavefunctionStepper::update(Wavefunction& dwf)
{
  // Verlet update of wf using force dwf and wfm stored in *wfv
  for ( int ispin = 0; ispin < wf_.nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < wf_.nkp(); ikp++ )
    {
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 68 69 70 71 72
      tmap_["md_update_wf"].start();
      // Verlet update of wf
      // cp = c + (c - cm) - dt2/m * hpsi
      // This is implemented (for each coefficient) as:
      // cp = 2*c - cm - dt2bye * hpsi
      // cm = c
      // c = cp
      SlaterDet* sd = wf_.sd(ispin,ikp);
      double* cptr = (double*) sd->c().valptr();
      double* cptrm = (double*) wfv_->sd(ispin,ikp)->c().valptr();
      const double* dcptr =
        (const double*) dwf.sd(ispin,ikp)->c().cvalptr();
      const int mloc = sd->c().mloc();
      const int nloc = sd->c().nloc();
      for ( int n = 0; n < nloc; n++ )
      {
        // note: double mloc length for complex<double> indices
        double* c = &cptr[2*mloc*n];
        double* cm = &cptrm[2*mloc*n];
        const double* dc = &dcptr[2*mloc*n];
        for ( int i = 0; i < mloc; i++ )
        {
          const double ctmp = c[2*i];
          const double ctmp1 = c[2*i+1];
          const double cmtmp = cm[2*i];
          const double cmtmp1 = cm[2*i+1];
          const double dctmp = dc[2*i];
          const double dctmp1 = dc[2*i+1];
          const double cptmp = 2.0*ctmp -  cmtmp -  dt2bye_ * dctmp;
          const double cptmp1= 2.0*ctmp1 - cmtmp1 - dt2bye_ * dctmp1;
73

74 75 76 77 78 79 80
          c[2*i]    = cptmp;
          c[2*i+1]  = cptmp1;
          cm[2*i]   = ctmp;
          cm[2*i+1] = ctmp1;
        }
      }
      tmap_["md_update_wf"].stop();
81

82 83 84
      tmap_["riccati"].start();
      wf_.sd(ispin,ikp)->riccati(*(wfv_->sd(ispin,ikp)));
      tmap_["riccati"].stop();
85 86 87 88 89 90 91
    }
  }
  ekin_em_ = ekin_ep_;
  ekin_ep_ = ekin_eh();
}

////////////////////////////////////////////////////////////////////////////////
92
void MDWavefunctionStepper::compute_wfm(Wavefunction& dwf)
93
{
94 95
  // Compute wfm for first MD step using wf, wfv and dwf (= Hpsi)
  // Replace then wfv by wfm
96

97 98
  // Compute cm using c and wavefunction velocity
  // cm = c - dt * v - 0.5 * dt2/m * hpsi
99 100 101 102 103 104
  // replace wfv by wfm
  const double half_dt2bye = 0.5 * dt2bye_;
  for ( int ispin = 0; ispin < wf_.nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < wf_.nkp(); ikp++ )
    {
105
      SlaterDet* sd = wf_.sd(ispin,ikp);
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
      double* cptr = (double*) sd->c().valptr();
      double* cptrv = (double*) wfv_->sd(ispin,ikp)->c().valptr();
      const double* dcptr =
        (const double*) dwf.sd(ispin,ikp)->c().cvalptr();
      const int mloc = sd->c().mloc();
      const int nloc = sd->c().nloc();
      for ( int n = 0; n < nloc; n++ )
      {
        // note: double mloc length for complex<double> indices
        double* c = &cptr[2*mloc*n];
        double* cv = &cptrv[2*mloc*n];
        const double* dc = &dcptr[2*mloc*n];
        for ( int i = 0; i < mloc; i++ )
        {
          const double ctmp = c[2*i];
          const double ctmp1 = c[2*i+1];
          const double cvtmp = cv[2*i];
          const double cvtmp1 = cv[2*i+1];
          const double dctmp = dc[2*i];
          const double dctmp1 = dc[2*i+1];
          cv[2*i]    = ctmp  - dt_ * cvtmp  - half_dt2bye * dctmp;
          cv[2*i+1]  = ctmp1 - dt_ * cvtmp1 - half_dt2bye * dctmp1;
        }
      }
      tmap_["riccati"].start();
      wfv_->sd(ispin,ikp)->riccati(*wf_.sd(ispin,ikp));
      tmap_["riccati"].stop();
134 135
    }
  }
136 137 138 139
  ekin_em_ = 0.0;
  ekin_ep_ = ekin_eh();
  // Note: ekin_ep is a first-order approximation of ekin_e using wf and wfm
  // only. This makes ekin_e consistent with the following update() call
140
  // Note: *wfv_ now contains wf(t-dt)
141 142 143
}

////////////////////////////////////////////////////////////////////////////////
144
void MDWavefunctionStepper::compute_wfv(Wavefunction& dwf)
145
{
146
  // Compute wfv = (wf - wfm)/dt - 0.5*dtbye*dwf
147

148 149 150 151 152
  assert(dt_!=0.0);
  for ( int ispin = 0; ispin < wf_.nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < wf_.nkp(); ikp++ )
    {
153 154
      // compute final velocity wfv
      // v = ( c - cm ) / dt - 0.5 * dt/m * hpsi
155

156
      // Note: At this point, *wfv_ contains wf(t-dt)
157

158 159
      // hpsi must be orthogonal to the subspace spanned by c
      // compute descent direction H psi - psi (psi^T H psi)
160

161 162 163 164 165 166
      SlaterDet* sd = wf_.sd(ispin,ikp);
      if ( sd->basis().real() )
      {
        // proxy real matrices c, cp
        DoubleMatrix c(sd->c());
        DoubleMatrix cp(dwf.sd(ispin,ikp)->c());
167

168
        DoubleMatrix a(c.context(),c.n(),c.n(),c.nb(),c.nb());
169

170 171 172 173
        // factor 2.0 in next line: G and -G
        a.gemm('t','n',2.0,c,cp,0.0);
        // rank-1 update correction
        a.ger(-1.0,c,0,cp,0);
174

175 176 177 178 179 180 181 182 183 184 185 186
        // cp = cp - c * a
        cp.gemm('n','n',-1.0,c,a,1.0);
      }
      else
      {
        ComplexMatrix& c = sd->c();
        ComplexMatrix& cp = dwf.sd(ispin,ikp)->c();
        ComplexMatrix a(c.context(),c.n(),c.n(),c.nb(),c.nb());
        a.gemm('c','n',1.0,c,cp,0.0);
        // cp = cp - c * a
        cp.gemm('n','n',-1.0,c,a,1.0);
      }
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
      const double dt_inv = 1.0/dt_;
      const double half_dtbye = 0.5 * dt2bye_ / dt_;
      double* cptr = (double*) sd->c().valptr();
      double* cptrv = (double*) wfv_->sd(ispin,ikp)->c().valptr();
      const double* dcptr =
        (const double*) dwf.sd(ispin,ikp)->c().cvalptr();
      const int mloc = sd->c().mloc();
      const int nloc = sd->c().nloc();
      for ( int n = 0; n < nloc; n++ )
      {
        // note: double mloc length for complex<double> indices
        double* c = &cptr[2*mloc*n];
        double* cv = &cptrv[2*mloc*n];
        const double* dc = &dcptr[2*mloc*n];
        for ( int i = 0; i < mloc; i++ )
        {
          const double ctmp = c[2*i];
          const double ctmp1 = c[2*i+1];
          const double cmtmp = cv[2*i];
          const double cmtmp1 = cv[2*i+1];
          const double dctmp = dc[2*i];
          const double dctmp1 = dc[2*i+1];
210

211 212 213 214 215 216 217
          cv[2*i]   = ( ctmp  - cmtmp  ) * dt_inv
                      - half_dtbye * dctmp;
          cv[2*i+1] = ( ctmp1 - cmtmp1 ) * dt_inv
                      - half_dtbye * dctmp1;
        }
      }
      // Note: *wfv_ now contains the wavefunction velocity
218 219 220 221 222 223 224 225 226 227
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
double MDWavefunctionStepper::ekin_eh(void)
{
  // compute ekin at time t - 0.5*dt using wf and wfm
  tmap_["ekin_e"].start();
  double ekin_e = 0.0;
228
  // assume that wfv contains wfm
229 230 231 232
  for ( int ispin = 0; ispin < wf_.nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < wf_.nkp(); ikp++ )
    {
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
      const double weight = wf_.weight(ikp);
      SlaterDet* sd = wf_.sd(ispin,ikp);
      double* cptr = (double*) sd->c().valptr();
      double* cptrm = (double*) wfv_->sd(ispin,ikp)->c().valptr();
      const int mloc = sd->c().mloc();
      const int nloc = sd->c().nloc();
      // compute electronic kinetic energy at time t-1/2
      const bool onrow0 = ( wf_.context().myrow() == 0 );
      const vector<double>& occ = sd->occ();
      for ( int n = 0; n < nloc; n++ )
      {
        const int nglobal = sd->c().j(0,n);
        const double occn = occ[nglobal];
        // note: double mloc length for complex<double> indices
        double* c = &cptr[2*mloc*n];
        double* cm = &cptrm[2*mloc*n];
        double tmpsum = 0.0;
        if ( sd->basis().real() && onrow0 )
        {
          // correct for double counting of G=0 element
          // i=0 coefficient is real, count only real part
          const double ctmp = c[0];
          const double cmtmp = cm[0];
          tmpsum -= 0.5 * (ctmp - cmtmp)*(ctmp - cmtmp);
        }
        for ( int i = 0; i < mloc; i++ )
        {
          const double ctmp = c[2*i];
          const double ctmp1 = c[2*i+1];
          const double cmtmp = cm[2*i];
          const double cmtmp1 = cm[2*i+1];
264

265 266 267 268 269 270 271 272 273
          tmpsum += (ctmp -cmtmp )*(ctmp -cmtmp ) +
                    (ctmp1-cmtmp1)*(ctmp1-cmtmp1);
        }
        if ( sd->basis().real() )
          // Note: 2 in next line: from (G,-G)
          ekin_e += weight * ( 2.0 * occn / dt2bye_ ) * tmpsum;
        else
          ekin_e += weight * ( occn / dt2bye_ ) * tmpsum;
      }
274 275 276 277 278 279
    }
  }
  wf_.context().dsum(1,1,&ekin_e,1);
  tmap_["ekin_e"].stop();
  return ekin_e;
}