forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstress_func_nl.cpp
More file actions
591 lines (516 loc) · 14.8 KB
/
stress_func_nl.cpp
File metadata and controls
591 lines (516 loc) · 14.8 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
#include "stress_func.h"
#include "../module_base/math_polyint.h"
#include "../module_base/math_ylmreal.h"
#include "../module_base/timer.h"
#include "global.h"
//calculate the nonlocal pseudopotential stress in PW
void Stress_Func::stress_nl(ModuleBase::matrix& sigma, const psi::Psi<complex<double>>* psi_in)
{
ModuleBase::TITLE("Stress_Func","stres_nl");
ModuleBase::timer::tick("Stress_Func","stres_nl");
const int nkb = GlobalC::ppcell.nkb;
if(nkb == 0)
{
ModuleBase::timer::tick("Stress_Func","stres_nl");
return;
}
double sigmanlc[3][3];
for(int l=0;l<3;l++)
{
for(int m=0;m<3;m++)
{
sigmanlc[l][m]=0.0;
}
}
// dbecp: conj( -iG * <Beta(nkb,npw)|psi(nbnd,npw)> )
ModuleBase::ComplexMatrix dbecp( GlobalV::NBANDS, nkb );
ModuleBase::ComplexMatrix becp( GlobalV::NBANDS, nkb );
// vkb1: |Beta(nkb,npw)><Beta(nkb,npw)|psi(nbnd,npw)>
ModuleBase::ComplexMatrix vkb1( nkb, GlobalC::wf.npwx );
ModuleBase::ComplexMatrix vkb0[3];
for(int i=0;i<3;i++){
vkb0[i].create(nkb, GlobalC::wf.npwx);
}
ModuleBase::ComplexMatrix vkb2( nkb, GlobalC::wf.npwx );
for (int ik = 0;ik < GlobalC::kv.nks;ik++)
{
if (GlobalV::NSPIN==2) GlobalV::CURRENT_SPIN = GlobalC::kv.isk[ik];
const int npw = GlobalC::kv.ngk[ik];
// generate vkb
if (GlobalC::ppcell.nkb > 0)
{
GlobalC::ppcell.getvnl(ik, GlobalC::ppcell.vkb);
}
// get becp according to wave functions and vkb
// important here ! becp must set zero!!
// vkb: Beta(nkb,npw)
// becp(nkb,nbnd): <Beta(nkb,npw)|psi(nbnd,npw)>
becp.zero_out();
const std::complex<double>* ppsi=nullptr;
if(psi_in!=nullptr)
{
ppsi = &(psi_in[0](ik, 0, 0));
}
else
{
ppsi = &(GlobalC::wf.evc[ik](0, 0));
}
char transa = 'C';
char transb = 'N';
///
///only occupied band should be calculated.
///
int nbands_occ = GlobalV::NBANDS;
while(GlobalC::wf.wg(ik, nbands_occ-1) < ModuleBase::threshold_wg)
{
nbands_occ--;
}
int npm = GlobalV::NPOL * nbands_occ;
zgemm_(&transa,
&transb,
&nkb,
&npm,
&npw,
&ModuleBase::ONE,
GlobalC::ppcell.vkb.c,
&GlobalC::wf.npwx,
ppsi,
&GlobalC::wf.npwx,
&ModuleBase::ZERO,
becp.c,
&nkb);
//becp calculate is over , now we should broadcast this data.
Parallel_Reduce::reduce_complex_double_pool( becp.c, becp.size);
for (int i = 0; i < 3; i++)
{
get_dvnl1(vkb0[i], ik, i);
}
get_dvnl2(vkb2, ik);
ModuleBase::Vector3<double> qvec;
double* qvec0[3];
qvec0[0] = &(qvec.x);
qvec0[1] = &(qvec.y);
qvec0[2] = &(qvec.z);
for (int ipol = 0; ipol < 3; ipol++)
{
for (int jpol = 0; jpol < ipol + 1; jpol++)
{
dbecp.zero_out();
vkb1.zero_out();
for (int i = 0; i < nkb; i++)
{
std::complex<double>* pvkb0i = &vkb0[ipol](i, 0);
std::complex<double>* pvkb0j = &vkb0[jpol](i, 0);
std::complex<double>* pvkb1 = &vkb1(i, 0);
// third term of dbecp_noevc
//std::complex<double>* pvkb = &vkb2(i,0);
//std::complex<double>* pdbecp_noevc = &dbecp_noevc(i, 0);
for (int ig = 0; ig < npw; ig++)
{
qvec = GlobalC::wfcpw->getgpluskcar(ik, ig);
pvkb1[ig] += 0.5 * qvec0[ipol][0] * pvkb0j[ig] +
0.5 * qvec0[jpol][0] * pvkb0i[ig];
} // end ig
}//end nkb
ModuleBase::ComplexMatrix dbecp_noevc(nkb, GlobalC::wf.npwx, true);
for (int i = 0; i < nkb; i++)
{
std::complex<double>* pdbecp_noevc = &dbecp_noevc(i, 0);
std::complex<double>* pvkb = &vkb1(i, 0);
// first term
for (int ig = 0; ig < npw;ig++)
{
pdbecp_noevc[ig] -= 2.0 * pvkb[ig];
}
// second termi
if (ipol == jpol)
{
pvkb = &GlobalC::ppcell.vkb(i, 0);
for (int ig = 0; ig < npw;ig++)
{
pdbecp_noevc[ig] -= pvkb[ig];
}
}
// third term
pvkb = &vkb2(i,0);
for (int ig = 0; ig < npw;ig++)
{
qvec = GlobalC::wfcpw->getgpluskcar(ik, ig);
double qm1;
if(qvec.norm2() > 1e-16) qm1 = 1.0 / qvec.norm();
else qm1 = 0;
pdbecp_noevc[ig] -= 2.0 * pvkb[ig] * qvec0[ipol][0] *
qvec0[jpol][0] * qm1 * GlobalC::ucell.tpiba;
} // end ig
} // end i
zgemm_(&transa,
&transb,
&nkb,
&npm,
&npw,
&ModuleBase::ONE,
dbecp_noevc.c,
&GlobalC::wf.npwx,
ppsi,
&GlobalC::wf.npwx,
&ModuleBase::ZERO,
dbecp.c,
&nkb);
// don't need to reduce here, keep
// dbecp different in each
// processor, and at last sum up
// all the forces.
// Parallel_Reduce::reduce_complex_double_pool(
// dbecp.ptr, dbecp.ndata);
// double *cf = new
// double[GlobalC::ucell.nat*3];
// ModuleBase::GlobalFunc::ZEROS(cf,
// GlobalC::ucell.nat);
for (int ib=0; ib<nbands_occ; ib++)
{
double fac = GlobalC::wf.wg(ik, ib) * 1.0;
int iat = 0;
int sum = 0;
for (int it=0; it<GlobalC::ucell.ntype; it++)
{
const int Nprojs = GlobalC::ucell.atoms[it].nh;
for (int ia=0; ia<GlobalC::ucell.atoms[it].na; ia++)
{
for (int ip1=0; ip1<Nprojs; ip1++)
{
for(int ip2=0; ip2<Nprojs; ip2++)
{
if(!GlobalC::ppcell.multi_proj && ip1 != ip2)
{
continue;
}
double ps = GlobalC::ppcell.deeq(GlobalV::CURRENT_SPIN, iat, ip1, ip2) ;
const int inkb1 = sum + ip1;
const int inkb2 = sum + ip2;
//out<<"\n ps = "<<ps;
const double dbb = ( conj( dbecp( ib, inkb1) ) * becp( ib, inkb2) ).real();
sigmanlc[ipol][ jpol] -= ps * fac * dbb;
}
}//end ip
++iat;
sum+=Nprojs;
}//ia
} //end it
} //end band
}//end jpol
}//end ipol
}// end ik
// sum up forcenl from all processors
for(int l=0;l<3;l++)
{
for(int m=0;m<3;m++)
{
if(m>l)
{
sigmanlc[l][m] = sigmanlc[m][l];
}
Parallel_Reduce::reduce_double_all( sigmanlc[l][m] ); //qianrui fix a bug for kpar > 1
}
}
// Parallel_Reduce::reduce_double_all(sigmanl.c, sigmanl.nr * sigmanl.nc);
for (int ipol = 0; ipol<3; ipol++)
{
for(int jpol = 0; jpol < 3; jpol++)
{
sigmanlc[ipol][jpol] *= 1.0 / GlobalC::ucell.omega;
}
}
for (int ipol = 0; ipol<3; ipol++)
{
for(int jpol = 0; jpol < 3; jpol++)
{
sigma(ipol,jpol) = sigmanlc[ipol][jpol] ;
}
}
//do symmetry
if(ModuleSymmetry::Symmetry::symm_flag)
{
GlobalC::symm.stress_symmetry(sigma, GlobalC::ucell);
}//end symmetry
// this->print(GlobalV::ofs_running, "nonlocal stress", stresnl);
ModuleBase::timer::tick("Stress_Func","stres_nl");
return;
}
void Stress_Func::get_dvnl1
(
ModuleBase::ComplexMatrix &vkb,
const int ik,
const int ipol
)
{
if(GlobalV::test_pp) ModuleBase::TITLE("Stress_Func","get_dvnl1");
const int lmaxkb = GlobalC::ppcell.lmaxkb;
if(lmaxkb < 0)
{
return;
}
const int npw = GlobalC::kv.ngk[ik];
const int nhm = GlobalC::ppcell.nhm;
int ig, ia, nb, ih;
ModuleBase::matrix vkb1(nhm, npw);
vkb1.zero_out();
double *vq = new double[npw];
const int x1= (lmaxkb + 1)*(lmaxkb + 1);
ModuleBase::matrix dylm(x1, npw);
ModuleBase::Vector3<double> *gk = new ModuleBase::Vector3<double>[npw];
for (ig = 0;ig < npw;ig++)
{
gk[ig] = GlobalC::wf.get_1qvec_cartesian(ik, ig);
}
dylmr2(x1, npw, gk, dylm, ipol);
int jkb = 0;
for(int it = 0;it < GlobalC::ucell.ntype;it++)
{
if(GlobalV::test_pp>1) ModuleBase::GlobalFunc::OUT("it",it);
// calculate beta in G-space using an interpolation table
const int nbeta = GlobalC::ucell.atoms[it].nbeta;
const int nh = GlobalC::ucell.atoms[it].nh;
if(GlobalV::test_pp>1) ModuleBase::GlobalFunc::OUT("nbeta",nbeta);
for (nb = 0;nb < nbeta;nb++)
{
if(GlobalV::test_pp>1) ModuleBase::GlobalFunc::OUT("ib",nb);
for (ig = 0;ig < npw;ig++)
{
const double gnorm = gk[ig].norm() * GlobalC::ucell.tpiba;
//cout << "\n gk[ig] = " << gk[ig].x << " " << gk[ig].y << " " << gk[ig].z;
//cout << "\n gk.norm = " << gnorm;
vq [ig] = ModuleBase::PolyInt::Polynomial_Interpolation(
GlobalC::ppcell.tab, it, nb, GlobalV::NQX, GlobalV::DQ, gnorm );
} // enddo
// add spherical harmonic part
for (ih = 0;ih < nh;ih++)
{
if (nb == GlobalC::ppcell.indv(it, ih))
{
const int lm = static_cast<int>( GlobalC::ppcell.nhtolm(it, ih) );
for (ig = 0;ig < npw;ig++)
{
vkb1(ih, ig) = dylm(lm, ig) * vq [ig];
}
}
} // end ih
} // end nbeta
// vkb1 contains all betas including angular part for type nt
// now add the structure factor and factor (-i)^l
for (ia=0; ia<GlobalC::ucell.atoms[it].na; ia++)
{
std::complex<double> *sk = GlobalC::wf.get_sk(ik, it, ia,GlobalC::wfcpw);
for (ih = 0;ih < nh;ih++)
{
std::complex<double> pref = pow( ModuleBase::NEG_IMAG_UNIT, GlobalC::ppcell.nhtol(it, ih)); //?
for (ig = 0;ig < npw;ig++)
{
vkb(jkb, ig) = vkb1(ih, ig) * sk [ig] * pref;
}
++jkb;
} // end ih
delete [] sk;
} // end ia
} // enddo
delete [] gk;
delete [] vq;
return;
}//end get_dvnl1
void Stress_Func::get_dvnl2(ModuleBase::ComplexMatrix &vkb,
const int ik)
{
if(GlobalV::test_pp) ModuleBase::TITLE("Stress","get_dvnl2");
// ModuleBase::timer::tick("Stress","get_dvnl2");
const int lmaxkb = GlobalC::ppcell.lmaxkb;
if(lmaxkb < 0)
{
return;
}
const int npw = GlobalC::kv.ngk[ik];
const int nhm = GlobalC::ppcell.nhm;
int ig, ia, nb, ih;
ModuleBase::matrix vkb1(nhm, npw);
double *vq = new double[npw];
const int x1= (lmaxkb + 1)*(lmaxkb + 1);
ModuleBase::matrix ylm(x1, npw);
ModuleBase::Vector3<double> *gk = new ModuleBase::Vector3<double>[npw];
for (ig = 0;ig < npw;ig++)
{
gk[ig] = GlobalC::wf.get_1qvec_cartesian(ik, ig);
}
ModuleBase::YlmReal::Ylm_Real(x1, npw, gk, ylm);
int jkb = 0;
for(int it = 0;it < GlobalC::ucell.ntype;it++)
{
if(GlobalV::test_pp>1) ModuleBase::GlobalFunc::OUT("it",it);
// calculate beta in G-space using an interpolation table
const int nbeta = GlobalC::ucell.atoms[it].nbeta;
const int nh = GlobalC::ucell.atoms[it].nh;
if(GlobalV::test_pp>1) ModuleBase::GlobalFunc::OUT("nbeta",nbeta);
for (nb = 0;nb < nbeta;nb++)
{
if(GlobalV::test_pp>1) ModuleBase::GlobalFunc::OUT("ib",nb);
for (ig = 0;ig < npw;ig++)
{
const double gnorm = gk[ig].norm() * GlobalC::ucell.tpiba;
//cout << "\n gk[ig] = " << gk[ig].x << " " << gk[ig].y << " " << gk[ig].z;
//cout << "\n gk.norm = " << gnorm;
vq [ig] = Polynomial_Interpolation_nl(
GlobalC::ppcell.tab, it, nb, GlobalV::DQ, gnorm );
} // enddo
// add spherical harmonic part
for (ih = 0;ih < nh;ih++)
{
if (nb == GlobalC::ppcell.indv(it, ih))
{
const int lm = static_cast<int>( GlobalC::ppcell.nhtolm(it, ih) );
for (ig = 0;ig < npw;ig++)
{
vkb1(ih, ig) = ylm(lm, ig) * vq [ig];
}
}
} // end ih
} // end nbeta
// vkb1 contains all betas including angular part for type nt
// now add the structure factor and factor (-i)^l
for (ia=0; ia<GlobalC::ucell.atoms[it].na; ia++)
{
std::complex<double> *sk = GlobalC::wf.get_sk(ik, it, ia,GlobalC::wfcpw);
for (ih = 0;ih < nh;ih++)
{
std::complex<double> pref = pow( ModuleBase::NEG_IMAG_UNIT, GlobalC::ppcell.nhtol(it, ih)); //?
for (ig = 0;ig < npw;ig++)
{
vkb(jkb, ig) = vkb1(ih, ig) * sk [ig] * pref;
}
++jkb;
} // end ih
delete [] sk;
} // end ia
} // enddo
delete [] gk;
delete [] vq;
// ModuleBase::timer::tick("Stress","get_dvnl2");
return;
}
double Stress_Func::Polynomial_Interpolation_nl
(
const ModuleBase::realArray &table,
const int &dim1,
const int &dim2,
const double &table_interval,
const double &x // input value
)
{
assert(table_interval>0.0);
const double position = x / table_interval;
const int iq = static_cast<int>(position);
const double x0 = position - static_cast<double>(iq);
const double x1 = 1.0 - x0;
const double x2 = 2.0 - x0;
const double x3 = 3.0 - x0;
const double y=
( table(dim1, dim2, iq) * (-x2*x3-x1*x3-x1*x2) / 6.0 +
table(dim1, dim2, iq+1) * (+x2*x3-x0*x3-x0*x2) / 2.0 -
table(dim1, dim2, iq+2) * (+x1*x3-x0*x3-x0*x1) / 2.0 +
table(dim1, dim2, iq+3) * (+x1*x2-x0*x2-x0*x1) / 6.0 )/table_interval ;
return y;
}
void Stress_Func::dylmr2 (
const int nylm,
const int ngy,
ModuleBase::Vector3<double> *gk,
ModuleBase::matrix &dylm,
const int ipol)
{
//-----------------------------------------------------------------------
//
// compute \partial Y_lm(G) \over \partial (G)_ipol
// using simple numerical derivation (SdG)
// The spherical harmonics are calculated in ylmr2
//
//int nylm, ngy, ipol;
// number of spherical harmonics
// the number of g vectors to compute
// desired polarization
//double g (3, ngy), gg (ngy), dylm (ngy, nylm)
// the coordinates of g vectors
// the moduli of g vectors
// the spherical harmonics derivatives
//
int ig, lm;
// counter on g vectors
// counter on l,m component
const double delta = 1e-6;
double *dg, *dgi;
ModuleBase::matrix ylmaux;
// dg is the finite increment for numerical derivation:
// dg = delta |G| = delta * sqrt(gg)
// dgi= 1 /(delta * sqrt(gg))
// gx = g +/- dg
ModuleBase::Vector3<double> *gx = new ModuleBase::Vector3<double> [ngy];
dg = new double [ngy];
dgi = new double [ngy];
ylmaux.create (nylm, ngy);
dylm.zero_out();
ylmaux.zero_out();
for( ig = 0;ig< ngy;ig++){
gx[ig] = gk[ig];
}
//$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(ig)
for( ig = 0;ig< ngy;ig++){
dg [ig] = delta * gx[ig].norm() ;
if (gx[ig].norm2() > 1e-9) {
dgi [ig] = 1.0 / dg [ig];
}
else{
dgi [ig] = 0.0;
}
}
//$OMP END PARALLEL DO
//$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(ig)
for( ig = 0;ig< ngy;ig++)
{
if(ipol==0)
gx [ig].x = gk[ ig].x + dg [ig];
else if(ipol==1)
gx [ig].y = gk [ ig].y + dg [ig];
else if(ipol==2)
gx [ig].z = gk [ ig].z + dg [ig];
}
//$OMP END PARALLEL DO
ModuleBase::YlmReal::Ylm_Real(nylm, ngy, gx, dylm);
//$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(ig)
for(ig = 0;ig< ngy;ig++)
{
if(ipol==0)
gx [ig].x = gk [ ig].x - dg [ig];
else if(ipol==1)
gx [ig].y = gk [ ig].y - dg [ig];
else if(ipol==2)
gx [ig].z = gk [ ig].z - dg [ig];
}
//$OMP END PARALLEL DO
ModuleBase::YlmReal::Ylm_Real(nylm, ngy, gx, ylmaux);
// zaxpy ( - 1.0, ylmaux, 1, dylm, 1);
for( lm = 0;lm< nylm;lm++)
{
for(ig = 0;ig< ngy;ig++)
{
dylm (lm,ig) = dylm(lm,ig) - ylmaux(lm,ig);
}
}
for( lm = 0;lm< nylm;lm++)
{
//$OMP PARALLEL DO DEFAULT(SHARED) PRIVATE(ig)
for(ig = 0;ig< ngy;ig++)
{
dylm (lm,ig) = dylm(lm,ig) * 0.5 * dgi [ig];
}
//$OMP END PARALLEL DO
}
delete[] gx;
delete[] dg;
delete[] dgi;
return;
}