MDWavefunctionStepper.C 9.29 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 18 19 20 21 22 23 24 25 26
// MDWavefunctionStepper.C
//
////////////////////////////////////////////////////////////////////////////////

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

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

////////////////////////////////////////////////////////////////////////////////
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++ )
    {
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 68 69 70 71
      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;
72

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

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

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

96 97
  // Compute cm using c and wavefunction velocity
  // cm = c - dt * v - 0.5 * dt2/m * hpsi
98 99 100 101 102 103
  // 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++ )
    {
104
      SlaterDet* sd = wf_.sd(ispin,ikp);
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
      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();
133 134
    }
  }
135 136 137 138
  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
139
  // Note: *wfv_ now contains wf(t-dt)
140 141 142
}

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

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

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

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

160 161 162 163 164 165
      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());
166

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

169 170 171 172
        // 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);
173

174 175 176 177 178 179 180 181 182 183 184 185
        // 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);
      }
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
      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];
209

210 211 212 213 214 215 216
          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
217 218 219 220 221 222 223 224 225 226
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
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;
227
  // assume that wfv contains wfm
228 229 230 231
  for ( int ispin = 0; ispin < wf_.nspin(); ispin++ )
  {
    for ( int ikp = 0; ikp < wf_.nkp(); ikp++ )
    {
232 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
      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];
263

264 265 266 267 268 269 270 271 272
          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;
      }
273 274 275 276 277 278
    }
  }
  wf_.context().dsum(1,1,&ekin_e,1);
  tmap_["ekin_e"].stop();
  return ekin_e;
}