Repository navigation
Expand file tree
/
Copy pathoccupy.cpp
More file actions
615 lines (563 loc) · 19 KB
/
Copy pathoccupy.cpp
File metadata and controls
615 lines (563 loc) · 19 KB
1
2
3
4
5
6
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
34
35
36
37
38
39
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
180
181
182
183
184
185
186
187
188
189
190
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
220
221
222
223
224
225
226
227
228
229
230
231
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
#include "occupy.h"
#include "source_base/constants.h"
#include "source_base/mymath.h"
#include "source_base/parallel_reduce.h"
#include "source_io/module_parameter/parameter.h"
Occupy::Occupy()
{
}
Occupy::~Occupy()
{
}
//===========================================================
// Four smearing methods:
// (1) do nothing
// (2) gaussian_broadening method (need 'smearing_sigma' value).
// (3) tetrahedron method
// (4) fixed_occupations
//===========================================================
bool Occupy::use_gaussian_broadening = false;
int Occupy::gaussian_type = 0;
double Occupy::gaussian_parameter = 0.01;
bool Occupy::fixed_occupations = false;
void Occupy::decision(const std::string& name, const std::string& smearing_method, const double& smearing_sigma)
{
ModuleBase::TITLE("Occupy", "decision");
use_gaussian_broadening = false;
fixed_occupations = false;
gaussian_type = 0;
gaussian_parameter = smearing_sigma;
if (name == "fixed")
{
if (gaussian_parameter != 0.0)
{
ModuleBase::WARNING("smearing_decision", "Fixed occupations,gauss broadening ignored");
ModuleBase::GlobalFunc::AUTO_SET("gaussian_parameter", 0.0);
gaussian_parameter = 0.0;
}
}
else if (name == "smearing" && smearing_method == "fixed")
{
if (gaussian_parameter != 0.0)
{
ModuleBase::WARNING("smearing_decision", "Fixed occupations,gauss broadening ignored");
ModuleBase::GlobalFunc::AUTO_SET("gaussian_parameter", 0.0);
gaussian_parameter = 0.0;
}
}
// there are four types of smearing methods:
// (1) gaussian
// (2) methfessel-paxton
// (3) Marzari-Vanderbilt
// (4) Fermi-Dirac
else if (name == "smearing")
{
use_gaussian_broadening = true;
if (gaussian_parameter == 0.0)
{
ModuleBase::WARNING_QUIT(
"smearing_decision",
"Smearing requires gaussian broadening,but gaussian_parameter = 0(default value = 0.01)");
}
if (smearing_method == "gaussian" || smearing_method == "gauss")
{
gaussian_type = 0; // 0: gaussian
}
else if (smearing_method == "methfessel-paxton" || smearing_method == "mp")
{
gaussian_type = 1; // >0 Methfessel-Paxton method.
}
else if (smearing_method == "mp2")
{
gaussian_type = 2; // 2nd Methfessel-Paxton method.
}
else if (smearing_method == "mp3")
{
// acually any order Methfessel-Paxton method can be supported in Occupy::w1gauss()
// however the parameter is string instead of int
ModuleBase::WARNING_QUIT(
"occupy",
"Some refactor of smearing shoule be done before supporting any order of Methfessel-Paxton method!");
}
else if (smearing_method == "marzari-vanderbilt" || smearing_method == "cold" || smearing_method == "mv")
{
gaussian_type = -1;
}
else if (smearing_method == "fermi-dirac" || smearing_method == "fd")
{
gaussian_type = -99;
}
}
else if (name == "tetrahedra")
{
ModuleBase::WARNING_QUIT("occupy", "not implemented yet!");
}
else if (name == "from_input")
{
fixed_occupations = true;
}
else
{
ModuleBase::WARNING_QUIT("occupy_decision", "occupations, not implemented");
}
return;
}
/**
* @brief calculates weights and fermi energy for semiconductors and insulators
*
* @note It is only applicable to semiconductors and insulators that have an energy gap.
* Here, bands are either empty or filled, and there is no electronic transfer among different k-points.
*
* @param nks number of k points.
* @param wk weight of each k point (consider symmetry).
* @param nbands number of bands.
* @param nelec number of electrons for this spin direction.
* @param ekb the array save the band energy.
* @param ef output: the highest occupied Kohn-Sham level.
* @param wg output: weight for each k, each band.
* @param is the spin index now.
* @param isk distinguish k point belong to which spin.
*/
void Occupy::iweights(
const int nks,
const std::vector<double>& wk,
const int nbands,
const double& nelec,
const ModuleBase::matrix& ekb,
double& ef,
ModuleBase::matrix& wg,
const int& is, //<- is should be -1, 0, or 1. -1 means set all spins, and 0 means spin up, 1 means spin down.
const std::vector<int>& isk)
{
assert(is < 2);
double degspin = 2.0;
if (PARAM.inp.nspin == 4) {
degspin = 1.0;
}
if (is != -1) {
degspin = 1.0;
}
double ib_mind = nelec / degspin;
int ib_min = std::ceil(ib_mind);
if (ib_min != int(ib_mind))
{
ModuleBase::WARNING_QUIT("iweights",
"It is not a semiconductor or insulator. Please do not set 'smearing_method=fixed', "
"and try other options.");
}
ef = -1e+10;
for (int ik = 0; ik < nks; ++ik)
{
// when NSPIN=2, only calculate spin up or spin down with TWO_FERMI mode(nupdown != 0)
if (PARAM.inp.nspin == 2 && isk[ik] != is && is != -1)
{
continue;
}
for (int ib = 0; ib < nbands; ++ib)
{
if (ib < ib_min)
{
wg(ik, ib) = wk[ik];
ef = std::max(ef, ekb(ik, ib));
}
else
{
wg(ik, ib) = 0.0;
}
}
}
#ifdef __MPI
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, ef);
#endif
return;
}
/**
* @brief calculates weights with the gaussian spreading technique
*
* @param nks number of k points.
* @param wk weight of each k point(symmetry considered).
* @param nband number of bands.
* @param nelec number of electrons.
* @param smearing_sigma parameter input by user.
* @param ngauss which type of smearing.
* @param ekb band energy.
* @param ef output: fermi level
* @param demet output: energy correction for metal
* @param wg output: weight of each band at each k point
* @param is spin
* @param isk array to point out each k belong to which spin
*/
void Occupy::gweights(const int nks,
const std::vector<double>& wk,
const int nband,
const double& nelec,
const double& smearing_sigma,
const int ngauss,
const ModuleBase::matrix& ekb,
double& ef,
double& demet,
ModuleBase::matrix& wg,
const int& is,
const std::vector<int>& isk)
{
// ModuleBase::TITLE("Occupy","gweights");
//===============================
// Calculate the Fermi energy ef
//===============================
// call efermig
Occupy::efermig(ekb, nband, nks, nelec, wk, smearing_sigma, ngauss, ef, is, isk);
demet = 0.0;
for (int ik = 0; ik < nks; ik++)
{
// mohan add 2011-04-03
if (is != -1 && is != isk[ik]) {
continue;
}
for (int ib = 0; ib < PARAM.globalv.nbands_l; ib++)
{
//================================
// Calculate the gaussian weights
//================================
// call wgauss
wg(ik, ib) = wk[ik] * Occupy::wgauss((ef - ekb(ik, ib)) / smearing_sigma, ngauss);
//====================================================================
// The correct form of the band energy is \int e n(e) de for e<ef
// demet is the correction to add to the sum of eigenvalues
//====================================================================
// Mohan fix bug 2010-1-9
// call w1gauss
demet += wk[ik] * smearing_sigma * Occupy::w1gauss((ef - ekb(ik, ib)) / smearing_sigma, ngauss);
}
}
return;
} // end subroutine gweights
/**
* @brief Finds the Fermi energy
*
* @param ekb band energy.
* @param nband number of bands.
* @param nks nks number of k points.
* @param nelec number of electrons.
* @param wk wk weight of each k point(symmetry considered).
* @param smearing_sigma parameter input by user.
* @param ngauss which type of smearing.
* @param ef output: fermi level
* @param is spin
* @param isk array to point out each k belong to which spin
*/
void Occupy::efermig(const ModuleBase::matrix& ekb,
const int nband,
const int nks,
const double& nelec,
const std::vector<double>& wk,
const double& smearing_sigma,
const int ngauss,
double& ef,
const int& is,
const std::vector<int>& isk)
{
// ModuleBase::TITLE("Occupy","efermig");
//==================================================================
// Finds the Fermi energy - Gaussian Broadening (Methfessel-Paxton)
//==================================================================
// find bounds for the Fermi energy. Very safe choice!
const int maxiter = 300;
const double eps = 1.0e-10;
/*
for(int ik=0; ik<nks; ik++)
{
for(int i=0; i<nband; i++)
{
std::cout << " ib=" << i << " ekb=" << ekb[ik][i] << std::endl;
}
}
*/
// the first 0 stands for the first k point.
double elw = ekb(0, 0);
double eup = ekb(0, nband - 1);
for (int ik = 1; ik < nks; ik++) // do ik = 2, nks
{
elw = std::min(elw, ekb(ik, 0));
eup = std::max(eup, ekb(ik, nband - 1));
}
eup += 2 * smearing_sigma;
elw -= 2 * smearing_sigma;
#ifdef __MPI
// find min and max across pools
Parallel_Reduce::gather_max_double_all(GlobalV::NPROC, eup);
Parallel_Reduce::gather_min_double_all(GlobalV::NPROC, elw);
#endif
//=================
// Bisection method
//=================
// call sumkg
int changetime = 0;
sumkg:
const double sumkup = Occupy::sumkg(ekb, nband, nks, wk, smearing_sigma, ngauss, eup, is, isk);
const double sumklw = Occupy::sumkg(ekb, nband, nks, wk, smearing_sigma, ngauss, elw, is, isk);
if (changetime > 1000)
{
std::cout << " SOMETHING WRONG: " << std::endl;
std::cout << " is = " << is << std::endl;
std::cout << " eup = " << eup << std::endl;
std::cout << " elw = " << elw << std::endl;
std::cout << " nband = " << nband << std::endl;
std::cout << " nelec = " << nelec << std::endl;
std::cout << " sumkup = " << sumkup << std::endl;
std::cout << " sumklw = " << sumklw << std::endl;
std::cout << " sumkup - nelec = " << sumkup - nelec << std::endl;
std::cout << " sumklw - nelec = " << sumklw - nelec << std::endl;
ModuleBase::WARNING_QUIT("Occupy::efermig", "ERROS in SMEARING");
}
else if ((sumkup - nelec) < -eps)
{
eup += 2 * smearing_sigma;
++changetime;
goto sumkg;
}
else if ((sumklw - nelec) > eps)
{
elw -= 2 * smearing_sigma;
++changetime;
goto sumkg;
}
for (int i = 0; i < maxiter; i++)
{
//======================
// change ef value
//======================
ef = (eup + elw) / 2.0;
const double sumkmid = sumkg(ekb, nband, nks, wk, smearing_sigma, ngauss, ef, is, isk);
if (std::abs(sumkmid - nelec) < eps)
{
return;
}
else if ((sumkmid - nelec) < -eps)
{
elw = ef;
}
else
{
eup = ef;
}
}
return;
}
/**
* @brief This function computes the number of states under a given energy e
*
* @param ekb band energy.
* @param nband number of bands.
* @param nks number of k points.
* @param wk weight of each k point(symmetry considered).
* @param smearing_sigma parameter input by user
* @param ngauss which type of smearing.
* @param e a givern energy
* @param is spin
* @param isk array to point out each k belong to which spin
* @return (double) the number of states
*/
double Occupy::sumkg(const ModuleBase::matrix& ekb,
const int nband,
const int nks,
const std::vector<double>& wk,
const double& smearing_sigma,
const int ngauss,
const double& e,
const int& is,
const std::vector<int>& isk)
{
// ModuleBase::TITLE("Occupy","sumkg");
double sum2 = 0.0;
for (int ik = 0; ik < nks; ik++)
{
if (is != -1 && is != isk[ik]) {
continue;
}
double sum1 = 0.0;
for (int ib = 0; ib < nband; ib++)
{
//===========================
// call wgauss
//===========================
sum1 += Occupy::wgauss((e - ekb(ik, ib)) / smearing_sigma, ngauss);
}
sum2 += wk[ik] * sum1;
}
// GlobalV::ofs_running << "\n sum2 before reduce = " << sum2 << std::endl;
#ifdef __MPI
const int npool = GlobalV::KPAR * PARAM.inp.bndpar;
Parallel_Reduce::reduce_double_allpool(npool, GlobalV::NPROC_IN_POOL, sum2);
#endif
// GlobalV::ofs_running << "\n sum2 after reduce = " << sum2 << std::endl;
return sum2;
}
double Occupy::wgauss(const double& x, const int n)
{
// ModuleBase::TITLE("Occupy","wgauss");
//=====================================================================
// This function computes the approximate theta function for the
// iven order n, at the point x.
// --> (n>=0) : Methfessel-Paxton case. See PRB 40, 3616 (1989).
// --> (n=-1 ): Cold smearing
// (Marzari-Vanderbilt). See PRL 82, 3296 (1999)
// 1/2*erf(x-1/sqrt(2)) + 1/sqrt(2*pi)*exp(-(x-1/sqrt(2))**2) + 1/2
// --> (n=-99): Fermi-Dirac case: 1.0/(1.0+exp(-x)).
//=====================================================================
//============================
// The value of this function
//============================
double wga = 0.0;
const double maxarg = 200.0;
//===========================
// Fermi-Dirac(fd) smearing
//===========================
if (n == -99)
{
if (x < -maxarg)
{
wga = 0.0;
}
else if (x > maxarg)
{
wga = 1.0;
}
else
{
wga = 1.00 / (1.0 + std::exp(-x));
}
return wga;
}
//===================
// Cold smearing(mv)
//===================
if (n == -1)
{
const double xp = x - 1.00 / ModuleBase::SQRT2;
const double arg = std::min(maxarg, xp * xp);
wga = 0.50 * erf(xp) + 1.00 / sqrt(ModuleBase::TWO_PI) * std::exp(-arg) + 0.50;
return wga;
}
//====================
// Methfessel-Paxton //pengfei 2014-10-13
//====================
wga = 0.5 * (1 - erf(-x));
// wga = gauss_freq(x * ModuleBase::SQRT2);
// std::cout<<"\n x="<<x<<" wga="<<wga;
if (n == 0)
{
return wga;
}
// double hd = 0.0;
int ni = 0;
const double arg = std::min(maxarg, x * x);
double hp = std::exp(-arg);
double h0 = 1.00;
double h1 = -2.00 * x;
double a = 1.0 / sqrt(ModuleBase::PI);
for (int i = 0; i < n; i++)
{
a = -a / (static_cast<double>(i + 1) * 4.00);
wga = wga + a * h1 * hp;
++ni;
// std::cout << " wga = " <<wga<<std::endl;
h0 = 2.00 * (-x) * h1 - 2.00 * static_cast<double>(ni) * h0;
++ni;
h1 = 2.00 * (-x) * h0 - 2.00 * static_cast<double>(ni) * h1;
}
return wga;
} // end function wgauss
double Occupy::w1gauss(const double& x, const int n)
{
//========================================================================
// w1gauss(x,n) = \int_{-\infty}^x y delta(y) dy
// where delta(x) is the current approximation for the delta function,
// as obtained from w0gauss(x,n)
//
// --> (n>=0) : Methfessel-Paxton case
//
// --> (n=-1): Cold smearing (Marzari-Vanderbilt)
// w1gauss = 1/sqrt(2*pi)*(x-1/sqrt(2))*exp(-(x-1/sqrt(2))**2)
//
// --> (n=-99): Fermi-Dirac case. In this case w1gauss corresponds
// to the negative of the electronic entropy.
//========================================================================
double w1 = 0.0;
//=======================
// Fermi-Dirac smearing
//=======================
if (n == -99)
{
if (std::abs(x) <= 36.0)
{
const double f = 1.00 / (1.00 + exp(-x));
const double onemf = 1.00 - f;
w1 = f * log(f) + onemf * log(onemf);
//==================================================
// in order to avoid problems for large values of x
//==================================================
}
else
{
//=============================================
// neglect w1gauss when abs(w1gauss) < 1.0d-14
//=============================================
w1 = 0.00;
}
return w1;
}
//===============
// Cold smearing
//===============
if (n == -1)
{
const double xp = x - 1.00 / ModuleBase::SQRT2;
const double arg = std::min(200.0, xp * xp);
w1 = 1.00 / sqrt(ModuleBase::TWO_PI) * xp * std::exp(-arg);
return w1;
}
//====================
// Methfessel-Paxton
//====================
const double arg = std::min(200.0, x * x);
w1 = -0.50 * std::exp(-arg) / sqrt(ModuleBase::PI);
// std::cout << "\n x=" << x << " w1=" << w1;
if (n == 0) // specific case : gaussian smearing.
{
return w1;
}
/* double hd = 0.0;
double hp = exp(- arg);
int ni = 0;
double a = 1.0 / sqrt(ModuleBase::PI);
for (int i = 0;i < n;i++)
{
hd = 2.00 * x * hp - 2.00 * static_cast<double>(ni) * hd;//dble(ni)
ni ++;
const double hpm1 = hp;
hp = 2.00 * x * hd - 2.00 * static_cast<double>(ni) * hp;//dble(ni)
ni ++;
// mohan fixed bug 2010-1-10
// description of bug: i should not be 0.
a = - a / ( static_cast<double>(i+1) * 4.00);//dble(i)
std::cout << " w1 == "<<w1<<std::endl;
w1 -= a * (0.50 * hp + static_cast<double>(ni) * hpm1);//dble(ni)
std::cout << " w1 == "<<w1<<std::endl;
}*/
int ni = 0;
double hp = std::exp(-arg);
double h0 = 1.00;
double h1 = 2.00 * x;
double a = 1.0 / sqrt(ModuleBase::PI);
for (int i = 0; i < n; i++)
{
a = -a / (static_cast<double>(i + 1) * 4.00);
ni++;
const double h0m1 = h0;
h0 = 2.00 * x * h1 - 2.00 * static_cast<double>(ni) * h0;
ni++;
h1 = 2.00 * x * h0 - 2.00 * static_cast<double>(ni) * h1;
w1 = w1 - a * (0.50 * h0 + static_cast<double>(ni) * h0m1) * hp;
}
return w1;
} // end function w1gauss