testSlaterDet.C 5.08 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License 
// as published by the Free Software Foundation, either version 2 of 
// 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
// testSlaterDet.C
//
////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
18
// $Id: testSlaterDet.C,v 1.10 2008-08-13 06:39:43 fgygi Exp $
Francois Gygi committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

#include "Context.h"
#include "SlaterDet.h"
#include "FourierTransform.h"
#include "Timer.h"

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cassert>
using namespace std;

#ifdef USE_MPI
#include <mpi.h>
#endif

int main(int argc, char **argv)
{
#if USE_MPI
  MPI_Init(&argc,&argv);
#endif
  {
    // use: testSlaterDet a0 a1 a2 b0 b1 b2 c0 c1 c2 ecut nst kx ky kz npr npc
    double err;
    assert(argc==17);
    D3vector a(atof(argv[1]),atof(argv[2]),atof(argv[3]));
    D3vector b(atof(argv[4]),atof(argv[5]),atof(argv[6]));
    D3vector c(atof(argv[7]),atof(argv[8]),atof(argv[9]));
    UnitCell cell(a,b,c);
    double ecut = atof(argv[10]);
    int nst = atoi(argv[11]);
    D3vector kpoint(atof(argv[12]),atof(argv[13]),atof(argv[14]));
51

Francois Gygi committed
52 53
    int npr = atoi(argv[15]);
    int npc = atoi(argv[16]);
54

Francois Gygi committed
55
    Timer tm;
56

57
    Context ctxt(npr,npc);
58

Francois Gygi committed
59 60
    SlaterDet sd(ctxt,kpoint);
    cout << sd.context();
61

Francois Gygi committed
62 63 64 65 66 67 68
    sd.resize(cell,cell,ecut,nst);
    if ( ctxt.myproc() == 0 )
    {
      cout << " kpoint: " << sd.basis().kpoint() << endl;
      cout << " ngw:    " << sd.basis().size() << endl;
      cout << " nst:    " << sd.nst() << endl;
    }
69

Francois Gygi committed
70 71 72
    err = sd.ortho_error();
    if ( ctxt.myproc() == 0 )
      cout << " Initial orthogonality error before rand " << err << endl;
73

Francois Gygi committed
74 75 76 77 78 79
    sd.randomize(0.2/sd.basis().size());
    if ( ctxt.myproc() == 0 )
      cout << " sd.randomize: done" << endl;
    err = sd.ortho_error();
    if ( ctxt.myproc() == 0 )
      cout << " Orthogonality error after rand " << err << endl;
80

Francois Gygi committed
81 82 83 84 85 86 87 88 89 90 91
    tm.reset();
    tm.start();
    sd.gram();
    tm.stop();
    cout << " Gram: CPU/Real: " << tm.cpu() << " / " << tm.real() << endl;
    err = sd.ortho_error();
    if ( ctxt.myproc() == 0 )
      cout << " Gram orthogonality error " << err << endl;

    SlaterDet sdm(ctxt,kpoint);
    sdm.resize(cell,cell,ecut,nst);
92

Francois Gygi committed
93 94
    sdm.c() = sd.c();
    sd.randomize(0.1/sd.basis().size());
95

Francois Gygi committed
96 97 98 99 100 101 102 103
    tm.reset();
    tm.start();
    sd.riccati(sdm);
    tm.stop();
    cout << " riccati: CPU/Real: " << tm.cpu() << " / " << tm.real() << endl;
    err = sd.ortho_error();
    if ( ctxt.myproc() == 0 )
      cout << " Riccati orthogonality error " << err << endl;
104

Francois Gygi committed
105 106 107 108 109 110 111 112 113 114
    sd.riccati(sdm);
    err = sd.ortho_error();
    if ( ctxt.myproc() == 0 )
    {
      cout << " Riccati orthogonality error " << err << endl;
      cout << " sd.total size (MB): " << setprecision(4)
           << sd.memsize() / 1048576.0 << endl;
      cout << " sd.local size (MB): " << setprecision(4)
           << sd.localmemsize() / 1048576.0 << endl;
    }
115

116
    //cout << " ekin: " << setprecision(8) << sd.ekin(occ) << endl;
117

Francois Gygi committed
118 119 120
    // compute charge density in real space
    FourierTransform ft(sd.basis(),
      2*sd.basis().np(0), 2*sd.basis().np(1), 2*sd.basis().np(2));
121

Francois Gygi committed
122 123
    vector<complex<double> > f(ft.np012loc());
    vector<double> rho(ft.np012loc());
124

Francois Gygi committed
125 126 127
    Timer tmrho;
    tmrho.reset();
    tmrho.start();
128

Francois Gygi committed
129 130
    cout << " compute_density..." << endl;
    double weight = 1.0;
131
    sd.compute_density(ft,weight,&rho[0]);
132

Francois Gygi committed
133
    tmrho.stop();
134
    cout << " compute_density: CPU/Real: "
Francois Gygi committed
135
         << tmrho.cpu() << " / " << tmrho.real() << endl;
136

Francois Gygi committed
137 138 139 140 141 142 143 144 145 146
    // integral of rho in r space
    double sum = 0.0;
    for ( int i = 0; i < rho.size(); i++ )
      sum += rho[i];
#if USE_MPI
    double tsum;
    int mycol = sd.context().mycol();
    MPI_Allreduce(&sum,&tsum,1,MPI_DOUBLE,MPI_SUM,sd.context().comm());
    sum = tsum;
#endif
147
    cout << ctxt.mype() << ": "
Francois Gygi committed
148
         << " rho: " << sum * cell.volume() / ft.np012() << endl;
149

Francois Gygi committed
150 151 152 153 154 155 156 157 158
    SlaterDet sdp(sd);
    //SlaterDet sdp(ctxt,kpoint);
    //sdp.resize(cell,cell,ecut,nst);
    //sdp = sd;
    sdp.randomize(0.001);
    sdp.gram();
    err = sdp.ortho_error();
    if ( ctxt.myproc() == 0 )
      cout << " Gram orthogonality error " << err << endl;
159

Francois Gygi committed
160 161 162 163 164
    Timer tmv;
    tmv.reset();
    tmv.start();
    sd.rs_mul_add(ft,&rho[0],sdp);
    tmv.stop();
165
    cout << " rs_mul_add: CPU/Real: "
Francois Gygi committed
166
         << tmv.cpu() << " / " << tmv.real() << endl;
167

Francois Gygi committed
168 169 170 171 172 173
#if 0
    ofstream outfile("sd.dat");
    tm.reset();
    tm.start();
    outfile << sd;
    tm.stop();
174
    cout << " write: CPU/Real: "
Francois Gygi committed
175 176
         << tm.cpu() << " / " << tm.real() << endl;
#endif
177

Francois Gygi committed
178 179
    for ( TimerMap::iterator i = sd.tmap.begin(); i != sd.tmap.end(); i++ )
      cout << ctxt.mype() << ": "
180
           << setw(15) << (*i).first
Francois Gygi committed
181 182 183 184 185 186
           << " : " << setprecision(3) << (*i).second.real() << endl;
  }
#if USE_MPI
  MPI_Finalize();
#endif
}