forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsi_init_atomic.cpp
More file actions
492 lines (466 loc) · 24.2 KB
/
psi_init_atomic.cpp
File metadata and controls
492 lines (466 loc) · 24.2 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
#include "psi_init_atomic.h"
#include "source_pw/module_pwdft/soc.h"
// numerical algorithm support
#include "source_base/math_integral.h" // for numerical integration
#include "source_base/math_polyint.h" // for polynomial interpolation
#include "source_base/math_ylmreal.h" // for real spherical harmonics
#include "source_base/math_sphbes.h" // for spherical bessel functions
// basic functions support
#include "source_base/tool_quit.h"
#include "source_base/timer.h"
// global variables definition
#include "source_base/global_variable.h"
#include "source_io/module_parameter/parameter.h"
// io support
#include "source_io/module_output/write_pao.h"
// free function, compared with common radial function normalization, it does not multiply r to function
// due to pswfc is already multiplied by r
// template <typename T>
// void normalize(int n_rgrid, std::vector<T>& pswfcr, double* rab)
// {
// std::vector<T> pswfc2r2(pswfcr.size());
// std::transform(pswfcr.begin(), pswfcr.end(), pswfc2r2.begin(), [](T pswfc) { return pswfc * pswfc; });
// T norm = ModuleBase::Integral::simpson(n_rgrid, pswfc2r2.data(), rab);
// norm = sqrt(norm);
// std::transform(pswfcr.begin(), pswfcr.end(), pswfcr.begin(), [norm](T pswfc) { return pswfc / norm; });
// }
template <typename T>
void psi_init_atomic<T>::allocate_ps_table()
{
// find correct dimension for ovlp_flzjlq
int dim1 = this->p_ucell_->ntype;
int dim2 = 0; // dim2 should be the maximum number of pseudo atomic orbitals
for (int it = 0; it < this->p_ucell_->ntype; it++)
{
dim2 = std::max(dim2, this->p_ucell_->atoms[it].ncpp.nchi);
}
if (dim2 == 0)
{
ModuleBase::WARNING_QUIT("psi_init_atomic<T>::allocate_table", "there is not ANY pseudo atomic orbital read in present system, recommand other methods, quit.");
}
int dim3 = PARAM.globalv.nqx;
// allocate memory for ovlp_flzjlq
this->ovlp_pswfcjlq_.create(dim1, dim2, dim3);
this->ovlp_pswfcjlq_.zero_out();
}
template <typename T>
void psi_init_atomic<T>::initialize(const Structure_Factor* sf, //< structure factor
const ModulePW::PW_Basis_K* pw_wfc, //< planewave basis
const UnitCell* p_ucell, //< unit cell
const K_Vectors* p_kv_in,
const int& random_seed, //< random seed
const pseudopot_cell_vnl* p_pspot_nl,
const int& rank)
{
ModuleBase::timer::start("psi_init_atomic", "initialize");
if(p_pspot_nl == nullptr)
{
ModuleBase::WARNING_QUIT("psi_init_atomic<T>::initialize",
"pseudopot_cell_vnl object cannot be nullptr for atomic, quit.");
}
// import
psi_initializer<T>::initialize(sf, pw_wfc, p_ucell, p_kv_in, random_seed, p_pspot_nl, rank);
this->nbands_start_ = std::max(this->p_ucell_->natomwfc, PARAM.inp.nbands);
this->nbands_complem_ = this->nbands_start_ - this->p_ucell_->natomwfc;
// allocate
this->allocate_ps_table();
// then for generate random number to fill in the wavefunction
this->ixy2is_.clear();
this->ixy2is_.resize(this->pw_wfc_->fftnxy);
this->pw_wfc_->getfftixy2is(this->ixy2is_.data());
ModuleBase::timer::end("psi_init_atomic", "initialize");
}
template <typename T>
void psi_init_atomic<T>::tabulate()
{
ModuleBase::timer::start("psi_init_atomic", "tabulate");
GlobalV::ofs_running << "\n Make real space PAO into reciprocal space." << std::endl;
ModuleIO::print_PAOs(*this->p_ucell_);
// Find the type of atom that has most mesh points.
int max_msh = 0;
for (int it=0; it<this->p_ucell_->ntype; it++)
{
max_msh = (this->p_ucell_->atoms[it].ncpp.msh > max_msh) ? this->p_ucell_->atoms[it].ncpp.msh : max_msh;
}
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"max mesh points in Pseudopotential",max_msh);
this->ovlp_pswfcjlq_.zero_out();
const int startq = 0;
const double pref = ModuleBase::FOUR_PI / sqrt(this->p_ucell_->omega);
std::vector<double> aux(max_msh);
std::vector<double> vchi(max_msh);
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"dq(describe PAO in reciprocal space)",PARAM.globalv.dq);
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"max q",PARAM.globalv.nqx);
for (int it=0; it<this->p_ucell_->ntype; it++)
{
Atom* atom = &this->p_ucell_->atoms[it];
GlobalV::ofs_running<<"\n number of pseudo atomic orbitals for "<<atom->label<<" is "<< atom->ncpp.nchi << std::endl;
// QE uses atom->ncpp.mesh
const int n_rgrid = (PARAM.inp.pseudo_mesh) ? atom->ncpp.mesh : atom->ncpp.msh;
std::vector<double> chi2(n_rgrid);
for (int ic = 0; ic < atom->ncpp.nchi ;ic++)
{
// check the unit condition
for(int ir=0; ir<n_rgrid; ir++)
{
double chi = atom->ncpp.chi(ic, ir);
chi2[ir] = chi * chi;
}
double unit = 0.0;
ModuleBase::Integral::Simpson_Integral(n_rgrid, chi2.data(), atom->ncpp.rab.data(), unit);
// liuyu add 2023-10-06
if (unit < 1e-8)
{
// set occupancy to a small negative number so that this wfc
// is not going to be used for starting wavefunctions
atom->ncpp.oc[ic] = -1e-8;
GlobalV::ofs_running << "WARNING: norm of atomic wavefunction # " << ic + 1 << " of atomic type "
<< atom->ncpp.psd << " is zero" << std::endl;
}
// only occupied states are normalized
if (atom->ncpp.oc[ic] < 0)
{
continue;
}
// the US part if needed
if (atom->ncpp.tvanp)
{
int kkbeta = atom->ncpp.kkbeta;
if ((kkbeta % 2 == 0) && kkbeta > 0)
{
kkbeta--;
}
std::vector<double> norm_beta(kkbeta);
std::vector<double> work(atom->ncpp.nbeta);
for (int ib = 0; ib < atom->ncpp.nbeta; ib++)
{
bool match = false;
if (atom->ncpp.lchi[ic] == atom->ncpp.lll[ib])
{
if (atom->ncpp.has_so)
{
if (std::abs(atom->ncpp.jchi[ic] - atom->ncpp.jjj[ib]) < 1e-6)
{
match = true;
}
}
else
{
match = true;
}
}
if (match)
{
for (int ik = 0; ik < kkbeta; ik++)
{
norm_beta[ik] = atom->ncpp.betar(ib, ik) * atom->ncpp.chi(ic, ik);
}
ModuleBase::Integral::Simpson_Integral(kkbeta, norm_beta.data(), atom->ncpp.rab.data(), work[ib]);
}
else
{
work[ib] = 0.0;
}
}
for (int ib1 = 0; ib1 < atom->ncpp.nbeta; ib1++)
{
for (int ib2 = 0; ib2 < atom->ncpp.nbeta; ib2++)
{
unit += atom->ncpp.qqq(ib1, ib2) * work[ib1] * work[ib2];
}
}
} // endif tvanp
//=================================
// normalize radial wave functions
//=================================
unit = std::sqrt(unit);
if (std::abs(unit - 1.0) > 1e-6)
{
GlobalV::ofs_running << "WARNING: norm of atomic wavefunction # " << ic + 1 << " of atomic type "
<< atom->ncpp.psd << " is " << unit << ", renormalized" << std::endl;
for (int ir = 0; ir < n_rgrid; ir++)
{
atom->ncpp.chi(ic, ir) /= unit;
}
}
const int l = atom->ncpp.lchi[ic];
for (int iq = startq; iq < PARAM.globalv.nqx; iq++)
{
const double q = PARAM.globalv.dq * iq;
ModuleBase::Sphbes::Spherical_Bessel(atom->ncpp.msh, atom->ncpp.r.data(), q, l, aux.data());
for (int ir = 0; ir < atom->ncpp.msh; ir++)
{
vchi[ir] = atom->ncpp.chi(ic, ir) * aux[ir] * atom->ncpp.r[ir];
}
double vqint = 0.0;
ModuleBase::Integral::Simpson_Integral(atom->ncpp.msh, vchi.data(), atom->ncpp.rab.data(), vqint);
this->ovlp_pswfcjlq_(it, ic, iq) = vqint * pref;
}
}
}
ModuleBase::timer::end("psi_init_atomic", "tabulate");
}
std::complex<double> phase_factor(double arg, int mode)
{
if(mode == 1) { return std::complex<double>(cos(arg),0); }
else if (mode == -1) { return std::complex<double>(0, sin(arg)); }
else if (mode == 0) { return std::complex<double>(cos(arg), sin(arg)); }
else { return std::complex<double>(1,0); }
}
template <typename T>
void psi_init_atomic<T>::init_psig(T* psig, const int& ik)
{
ModuleBase::timer::start("psi_init_atomic", "init_psig");
const int npw = this->pw_wfc_->npwk[ik];
const int npwk_max = this->pw_wfc_->npwk_max;
int lmax = this->p_ucell_->lmax_ppwf;
const int total_lm = (lmax + 1) * (lmax + 1);
ModuleBase::matrix ylm(total_lm, npw);
ModuleBase::GlobalFunc::ZEROS(psig, PARAM.globalv.npol * this->nbands_start_ * npwk_max);
std::vector<std::complex<double>> aux(npw);
std::vector<double> chiaux(npw);
std::vector<ModuleBase::Vector3<double>> gk(npw);
// I plan to use std::transform to replace the following for loop
// but seems it is not as easy as I thought, the lambda function is not easy to write
for (int ig = 0; ig < npw; ig++)
{
gk[ig] = this->pw_wfc_->getgpluskcar(ik, ig);
}
ModuleBase::YlmReal::Ylm_Real(total_lm, npw, gk.data(), ylm);
int index = 0;
std::vector<double> ovlp_pswfcjlg(npw);
for (int it = 0; it < this->p_ucell_->ntype; it++)
{
for (int ia = 0; ia < this->p_ucell_->atoms[it].na; ia++)
{
/* FOR EVERY ATOM */
// I think it is always a BAD idea to new one pointer in a function, then return it
// it indicates the ownership of the pointer and behind memory is transferred to the caller
// then one must manually delete it, makes new-delete not symmetric
std::complex<double> *sk = this->sf_->get_sk(ik, it, ia, this->pw_wfc_);
for (int ipswfc = 0; ipswfc < this->p_ucell_->atoms[it].ncpp.nchi; ipswfc++)
{
/* FOR EVERY PSWFC OF ATOM */
if (this->p_ucell_->atoms[it].ncpp.oc[ipswfc] >= 0.0)
{
/* IF IS OCCUPIED, GET L */
const int l = this->p_ucell_->atoms[it].ncpp.lchi[ipswfc];
std::complex<double> lphase = pow(ModuleBase::NEG_IMAG_UNIT, l);
for (int ig=0; ig<npw; ig++)
{
ovlp_pswfcjlg[ig] = ModuleBase::PolyInt::Polynomial_Interpolation(
this->ovlp_pswfcjlq_, it, ipswfc,
PARAM.globalv.nqx, PARAM.globalv.dq, gk[ig].norm() * this->p_ucell_->tpiba );
}
/* NSPIN == 4 */
if(PARAM.inp.nspin == 4)
{
if(this->p_ucell_->atoms[it].ncpp.has_so)
{
Soc soc; soc.rot_ylm(l + 1);
const double j = this->p_ucell_->atoms[it].ncpp.jchi[ipswfc];
/* NOT NONCOLINEAR CASE, rotation matrix become identity */
if (!(PARAM.globalv.domag||PARAM.globalv.domag_z))
{
double cg_coeffs[2];
for(int m = -l-1; m < l+1; m++)
{
cg_coeffs[0] = soc.spinor(l, j, m, 0);
cg_coeffs[1] = soc.spinor(l, j, m, 1);
if (fabs(cg_coeffs[0]) > 1e-8 || fabs(cg_coeffs[1]) > 1e-8)
{
for(int is = 0; is < 2; is++)
{
if(fabs(cg_coeffs[is]) > 1e-8)
{
/* GET COMPLEX SPHERICAL HARMONIC FUNCTION */
const int ind = this->p_pspot_nl_->lmaxkb + soc.sph_ind(l,j,m,is); // ind can be l+m, l+m+1, l+m-1
std::fill(aux.begin(), aux.end(), std::complex<double>(0.0, 0.0));
for(int n1 = 0; n1 < 2*l+1; n1++)
{
const int lm = l*l +n1;
std::complex<double> umM = soc.rotylm(n1, ind);
if(std::abs(umM) > 1e-8)
{
for(int ig = 0; ig < npw; ig++)
{
aux[ig] += umM * ylm(lm, ig);
}
}
}
for(int ig = 0; ig < npw; ig++)
{
psig[(2 * index + is) * npwk_max + ig] = this->template cast_to_T<T>(
lphase * cg_coeffs[is] * sk[ig] * aux[ig] * ovlp_pswfcjlg[ig]);
}
}
else
{
for (int ig = 0; ig < npw; ig++)
{
psig[(2 * index + is) * npwk_max + ig]
= this->template cast_to_T<T>(std::complex<double>(0.0, 0.0));
}
}
}
index++;
}
}
}
else
{
/* NONCONLINEAR CASE, will use [[cos(a/2)*exp(-ib/2), sin(a/2)*exp(ib/2)], [-sin(a/2)*exp(-ib/2), cos(a/2)*exp(ib/2)]] to rotate */
int ipswfc_noncolin_soc=0;
/* J = L - 1/2 -> continue */
/* J = L + 1/2 */
if(fabs(j - l + 0.5) < 1e-4)
{
continue;
}
chiaux.clear();
chiaux.resize(npw);
/* L == 0 */
if(l == 0)
{
std::memcpy(chiaux.data(), ovlp_pswfcjlg.data(), npw * sizeof(double));
}
else
{
/* L != 0, scan pswfcs that have the same L and satisfy J(pswfc) = L - 0.5 */
for(int jpsiwfc = 0; jpsiwfc < this->p_ucell_->atoms[it].ncpp.nchi; jpsiwfc++)
{
if(
(this->p_ucell_->atoms[it].ncpp.lchi[jpsiwfc] == l)
&&(fabs(this->p_ucell_->atoms[it].ncpp.jchi[jpsiwfc] - l + 0.5) < 1e-4))
{
ipswfc_noncolin_soc = jpsiwfc;
break;
}
}
for(int ig=0;ig<npw;ig++)
{
/* average <pswfc_a|jl(q)> and <pswfc_b(j=l-1/2)|jl(q)>, a and b seem not necessarily to be equal */
chiaux[ig] = l *
ModuleBase::PolyInt::Polynomial_Interpolation(
this->ovlp_pswfcjlq_, it, ipswfc_noncolin_soc,
PARAM.globalv.nqx, PARAM.globalv.dq, gk[ig].norm() * this->p_ucell_->tpiba);
chiaux[ig] += ovlp_pswfcjlg[ig] * (l + 1.0) ;
chiaux[ig] *= 1/(2.0*l+1.0);
}
}
/* ROTATE ACCORDING TO NONCOLINEAR */
double alpha = this->p_ucell_->atoms[it].angle1[ia];
double gamma = -1 * this->p_ucell_->atoms[it].angle2[ia] + 0.5 * ModuleBase::PI;
std::complex<double> fup, fdw;
for(int m = 0; m < 2*l+1; m++)
{
const int lm = l*l +m;
if(index+2*l+1 > this->p_ucell_->natomwfc)
{
std::cout<<__FILE__<<__LINE__<<" "<<index<<" "<<this->p_ucell_->natomwfc<<std::endl;
//ModuleBase::WARNING_QUIT("psi_init_atomic<T>::init_psig()","error: too many wfcs");
}
for(int ig = 0;ig<npw;ig++)
{
aux[ig] = sk[ig] * ylm(lm,ig) * chiaux[ig];
}
//rotate wfc as needed
//first rotation with angle alpha around (OX)
for(int ig = 0;ig<npw;ig++)
{
fup = phase_factor(0.5*alpha, 1)*aux[ig];
fdw = phase_factor(0.5*alpha, -1)*aux[ig];
//build the orthogonal wfc
//first rotation with angle (alpha + ModuleBase::PI) around (OX)
psig[index * 2 * npwk_max + ig]
= this->template cast_to_T<T>(phase_factor(0.5 * gamma, 0) * fup);
psig[(index * 2 + 1) * npwk_max + ig]
= this->template cast_to_T<T>(phase_factor(-0.5 * gamma, 0) * fdw);
//second rotation with angle gamma around(OZ)
fup = phase_factor(0.5*(alpha + ModuleBase::PI), 1)*aux[ig];
fdw = phase_factor(0.5*(alpha + ModuleBase::PI), -1)*aux[ig];
psig[(index + 2 * l + 1) * 2 * npwk_max + ig]
= this->template cast_to_T<T>(phase_factor(0.5 * gamma, 0) * fup);
psig[((index + 2 * l + 1) * 2 + 1) * npwk_max + ig]
= this->template cast_to_T<T>(phase_factor(-0.5 * gamma, 0) * fdw);
}
index++;
}
index += 2*l +1;
}
}
else
{//atomic_wfc_nc
double alpha=0.0;
double gamman=0.0;
std::complex<double> fup, fdown;
//alpha = this->p_ucell_->magnet.angle1_[it];
//gamman = -this->p_ucell_->magnet.angle2_[it] + 0.5*ModuleBase::PI;
alpha = this->p_ucell_->atoms[it].angle1[ia];
gamman = -1 * this->p_ucell_->atoms[it].angle2[ia] + 0.5 * ModuleBase::PI;
for(int m = 0; m < 2*l+1; m++)
{
const int lm = l*l +m;
if(index+2*l+1 > this->p_ucell_->natomwfc)
{
std::cout<<__FILE__<<__LINE__<<" "<<index<<" "<<this->p_ucell_->natomwfc<<std::endl;
//ModuleBase::WARNING_QUIT("psi_init_atomic<T>::init_psig()","error: too many wfcs");
}
for(int ig = 0;ig<npw;ig++)
{
aux[ig] = sk[ig] * ylm(lm,ig) * ovlp_pswfcjlg[ig];
}
//rotate function
//first, rotation with angle alpha around(OX)
for(int ig = 0; ig<npw; ig++)
{
fup = cos(0.5 * alpha) * aux[ig];
fdown = ModuleBase::IMAG_UNIT * sin(0.5 * alpha) * aux[ig];
// build the orthogonal wfc
// first rotation with angle(alpha+ModuleBase::PI) around(OX)
psig[index * 2 * npwk_max + ig] = this->template cast_to_T<T>(
(cos(0.5 * gamman) + ModuleBase::IMAG_UNIT * sin(0.5 * gamman)) * fup);
psig[(index * 2 + 1) * npwk_max + ig] = this->template cast_to_T<T>(
(cos(0.5 * gamman) - ModuleBase::IMAG_UNIT * sin(0.5 * gamman)) * fdown);
// second rotation with angle gamma around(OZ)
fup = cos(0.5 * (alpha + ModuleBase::PI)) * aux[ig];
fdown = ModuleBase::IMAG_UNIT * sin(0.5 * (alpha + ModuleBase::PI)) * aux[ig];
psig[(index + 2 * l + 1) * 2 * npwk_max + ig] = this->template cast_to_T<T>(
(cos(0.5 * gamman) + ModuleBase::IMAG_UNIT * sin(0.5 * gamman)) * fup);
psig[((index + 2 * l + 1) * 2 + 1) * npwk_max + ig] = this->template cast_to_T<T>(
(cos(0.5 * gamman) - ModuleBase::IMAG_UNIT * sin(0.5 * gamman)) * fdown);
}
index++;
}
index += 2*l+1;
}
}
else
{
for (int m = 0; m < 2*l+1; m++)
{
const int lm = l * l + m;
for (int ig = 0; ig < npw; ig++)
{
psig[index * npwk_max + ig]
= this->template cast_to_T<T>(lphase * sk[ig] * ylm(lm, ig) * ovlp_pswfcjlg[ig]);
}
index++;
}
}
}
}
delete [] sk;
}
}
/* complement the rest of bands if there are */
if(this->nbands_complem() > 0)
{
this->random_t(psig, index, this->nbands_start_, ik);
}
ModuleBase::timer::end("psi_init_atomic", "init_psig");
}
template class psi_init_atomic<std::complex<double>>;
template class psi_init_atomic<std::complex<float>>;
// gamma point calculation
template class psi_init_atomic<double>;
template class psi_init_atomic<float>;