forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctrl_output_pw.cpp
More file actions
484 lines (445 loc) · 25.7 KB
/
Copy pathctrl_output_pw.cpp
File metadata and controls
484 lines (445 loc) · 25.7 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
#include "ctrl_output_pw.h"
#include "../module_bessel/numerical_basis.h"
#include "../module_bessel/numerical_descriptor.h"
#include "../module_chgpot/get_pchg_pw.h"
#include "../module_dos/cal_ldos.h"
#include "../module_dos/write_dos_pw.h" // use write_dos_pw
#include "../module_unk/berryphase.h"
#include "../module_wannier/to_w90_pw.h" // wannier90 interface
#include "../module_wf/get_wf_pw.h"
#include "../module_wf/write_wfc_pw.h" // use write_wfc_pw
#include "source_base/formatter.h"
#include "source_lcao/module_deltaspin/lambda_loop_helper.h"
#include "source_lcao/module_deltaspin/deltaspin_pw_mi.h"
#include "source_lcao/module_deltaspin/spin_constrain.h"
#include "source_pw/module_pwdft/elecond.h"
#include "source_pw/module_pwdft/onsite_proj.h" // use projector
#ifdef __MLALGO
#include "../module_ml/write_mlkedf_desc.h"
#endif
void ModuleIO::ctrl_iter_pw(const int istep,
const int iter,
const double& conv_esolver,
psi::Psi<std::complex<double>, base_device::DEVICE_CPU>* psi,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* pw_wfc,
const Input_para& inp)
{
ModuleBase::TITLE("ModuleIO", "ctrl_iter_pw");
ModuleBase::timer::start("ModuleIO", "ctrl_iter_pw");
//----------------------------------------------------------
// 3) Print out electronic wavefunctions in pw basis
// we only print information every few ionic steps
//----------------------------------------------------------
// if istep_in = -1, istep will not appear in file name
// if iter_in = -1, iter will not appear in file name
int istep_in = -1;
int iter_in = -1;
bool out_wfc_flag = false;
if (inp.out_freq_ion > 0) // default value of out_freq_ion is 0
{
if (istep % inp.out_freq_ion == 0)
{
if (iter % inp.out_freq_elec == 0 || iter == inp.scf_nmax || conv_esolver)
{
istep_in = istep;
iter_in = iter;
out_wfc_flag = true;
}
}
}
else if (iter == inp.scf_nmax || conv_esolver)
{
out_wfc_flag = true;
}
if (out_wfc_flag)
{
ModuleIO::write_wfc_pw(istep_in,
iter_in,
GlobalV::KPAR,
GlobalV::MY_POOL,
GlobalV::MY_RANK,
inp.nbands,
inp.nspin,
PARAM.globalv.npol,
GlobalV::RANK_IN_POOL,
GlobalV::NPROC_IN_POOL,
inp.out_wfc_pw,
inp.ecutwfc,
PARAM.globalv.global_out_dir,
psi[0],
kv,
pw_wfc,
GlobalV::ofs_running);
}
ModuleBase::timer::end("ModuleIO", "ctrl_iter_pw");
return;
}
template <typename T, typename Device>
void ModuleIO::ctrl_scf_pw(const int istep,
UnitCell& ucell,
elecstate::ElecState* pelec,
const Charge& chr,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* pw_wfc,
const ModulePW::PW_Basis* pw_rho,
const ModulePW::PW_Basis* pw_rhod,
const ModulePW::PW_Basis_Big* pw_big,
Setup_Psi_pw& stp,
const Parallel_Grid& para_grid,
const Input_para& inp)
{
ModuleBase::TITLE("ModuleIO", "ctrl_scf_pw");
ModuleBase::timer::start("ModuleIO", "ctrl_scf_pw");
// Transfer data from device (GPU) to host (CPU) in pw basis
stp.copy_d2h();
//----------------------------------------------------------
//! 4) Compute density of states (DOS)
//----------------------------------------------------------
if (inp.out_dos)
{
bool out_dos_tmp = false;
int istep_in = -1;
// default value of out_freq_ion is 0
if (inp.out_freq_ion == 0)
{
out_dos_tmp = true;
}
else if (inp.out_freq_ion > 0)
{
if (istep % inp.out_freq_ion == 0)
{
out_dos_tmp = true;
istep_in = istep;
}
else
{
out_dos_tmp = false;
}
}
else
{
out_dos_tmp = false;
}
// the above is only valid for KSDFT, not SDFT
// Needs update in the near future
if (inp.esolver_type == "sdft")
{
out_dos_tmp = false;
}
if (out_dos_tmp)
{
ModuleIO::write_dos_pw(ucell,
pelec->ekb,
pelec->wg,
kv,
inp.nbands,
istep_in,
pelec->eferm,
inp.dos_edelta_ev,
inp.dos_scale,
inp.dos_sigma,
GlobalV::ofs_running);
}
}
//------------------------------------------------------------------
// 5) calculate band-decomposed (partial) charge density in pw basis
//------------------------------------------------------------------
if (inp.out_pchg.size() > 0)
{
// update psi_d
stp.update_psi_d();
ModuleIO::get_pchg_pw(inp.out_pchg,
inp.nspin,
inp.nbands,
&ucell,
stp.template get_psi_d<T, Device>(),
pw_rho,
pw_rhod,
pw_wfc,
para_grid,
PARAM.globalv.global_out_dir,
inp.if_separate_k,
inp.noncolin,
kv);
}
//------------------------------------------------------------------
//! 6) calculate Wannier functions in pw basis
//------------------------------------------------------------------
if (inp.calculation == "nscf" && inp.towannier90)
{
std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Wannier functions calculation");
toW90_PW wan(inp.out_wannier_mmn,
inp.out_wannier_amn,
inp.out_wannier_unk,
inp.out_wannier_eig,
inp.out_wannier_wvfn_formatted,
inp.nnkpfile,
inp.wannier_spin,
inp.nspin,
PARAM.inp.nbands,
PARAM.globalv.nqx,
PARAM.globalv.dq,
PARAM.globalv.npol);
wan.set_tpiba_omega(ucell.tpiba, ucell.omega);
wan.calculate(ucell, pelec->ekb, pw_wfc, pw_big, kv, stp.psi_cpu);
std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Wannier functions calculation");
}
//------------------------------------------------------------------
//! 7) calculate Berry phase polarization in pw basis
//------------------------------------------------------------------
if (inp.calculation == "nscf" && berryphase::berry_phase_flag && ModuleSymmetry::Symmetry::symm_flag != 1)
{
std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "Berry phase polarization");
berryphase bp;
bp.Macroscopic_polarization(ucell, pw_wfc->npwk_max, stp.psi_cpu, pw_rho, pw_wfc, kv);
std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "Berry phase polarization");
}
//------------------------------------------------------------------
// 8) write spin constrian results in pw basis
// spin constrain calculations, write atomic magnetization and magnetic force.
//------------------------------------------------------------------
if (inp.sc_mag_switch)
{
spinconstrain::SpinConstrain<std::complex<double>>& sc = spinconstrain::SpinConstrain<std::complex<double>>::getScInstance();
spinconstrain::pw::cal_mi_pw(sc.state_, sc.psi, sc.pelec);
spinconstrain::print_Mag_Force(sc, GlobalV::ofs_running);
}
//------------------------------------------------------------------
// 9) write onsite occupations for charge and magnetizations
//------------------------------------------------------------------
if (inp.onsite_radius > 0)
{ // float type has not been implemented
auto* onsite_p = projectors::OnsiteProjector<double, Device>::get_instance();
onsite_p->cal_occupations(reinterpret_cast<psi::Psi<std::complex<double>, Device>*>(stp.template get_psi_t<T, Device>()),
pelec->wg);
}
ModuleBase::timer::end("ModuleIO", "ctrl_scf_pw");
return;
}
template <typename T, typename Device>
void ModuleIO::ctrl_runner_pw(UnitCell& ucell,
elecstate::ElecState* pelec,
ModulePW::PW_Basis_K* pw_wfc,
ModulePW::PW_Basis* pw_rho,
ModulePW::PW_Basis* pw_rhod,
Charge& chr,
K_Vectors& kv,
Setup_Psi_pw& stp,
Structure_Factor& sf,
pseudopot_cell_vnl& ppcell,
surchem& solvent,
Parallel_Grid& para_grid,
const Input_para& inp)
{
ModuleBase::TITLE("ModuleIO", "ctrl_runner_pw");
ModuleBase::timer::start("ModuleIO", "ctrl_runner_pw");
// Create local ctx for device type deduction
Device* ctx = nullptr;
//----------------------------------------------------------
//! 1) Compute LDOS
//----------------------------------------------------------
if (inp.out_ldos[0])
{
stp.update_psi_d();
ModuleIO::cal_ldos_pw(reinterpret_cast<elecstate::ElecStatePW<std::complex<double>>*>(pelec), *stp.template get_psi_d<T, Device>(), ctx, para_grid, ucell);
}
//----------------------------------------------------------
//! 2) Calculate the spillage value,
//! which are used to generate numerical atomic orbitals
//----------------------------------------------------------
if (inp.basis_type == "pw" && inp.out_spillage)
{
// ! Print out overlap matrices
if (inp.out_spillage <= 2)
{
for (int i = 0; i < inp.bessel_nao_rcuts.size(); i++)
{
if (GlobalV::MY_RANK == 0)
{
std::cout << "update value: bessel_nao_rcut <- " << std::fixed << inp.bessel_nao_rcuts[i] << " a.u." << std::endl;
}
Numerical_Basis numerical_basis;
numerical_basis.output_overlap(stp.psi_cpu[0], sf, kv, pw_wfc, ucell, i);
}
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "BASIS OVERLAP (Q and S) GENERATION.");
}
}
//----------------------------------------------------------
//! 3) Print out electronic wave functions in real space
//----------------------------------------------------------
if (inp.out_wfc_norm.size() > 0 || inp.out_wfc_re_im.size() > 0)
{
stp.update_psi_d();
ModuleIO::get_wf_pw(inp.out_wfc_norm,
inp.out_wfc_re_im,
inp.nspin,
inp.nbands,
&ucell,
stp.template get_psi_d<T, Device>(),
pw_wfc,
pw_rho,
pw_rhod,
para_grid,
PARAM.globalv.global_out_dir,
kv);
}
//----------------------------------------------------------
//! 4) Use Kubo-Greenwood method to compute conductivities
//----------------------------------------------------------
if (inp.cal_cond)
{
using Real = typename GetTypeReal<T>::type;
EleCond<Real, Device> elec_cond(&ucell, &kv, pelec, pw_wfc, stp.template get_psi_t<T, Device>(), &ppcell);
elec_cond
.KG(inp.cond_smear, inp.cond_fwhm, inp.cond_wcut, inp.cond_dw, inp.cond_dt, inp.cond_nonlocal, inp.cond_mgga_vel, pelec->wg);
}
#ifdef __MLALGO
//----------------------------------------------------------
//! 7) generate training data for ML-KEDF
//----------------------------------------------------------
if (inp.of_ml_gene_data == 1)
{
pelec->pot->update_from_charge(&chr, &ucell);
ModuleIO::Write_MLKEDF_Descriptors write_mlkedf_desc;
write_mlkedf_desc.cal_tool->set_para(chr.nrxx,
inp.nelec,
inp.of_tf_weight,
inp.of_vw_weight,
inp.of_ml_chi_p,
inp.of_ml_chi_q,
inp.of_ml_chi_xi,
inp.of_ml_chi_pnl,
inp.of_ml_chi_qnl,
inp.of_ml_nkernel,
inp.of_ml_kernel,
inp.of_ml_kernel_scaling,
inp.of_ml_yukawa_alpha,
inp.of_ml_kernel_file,
ucell.omega,
pw_rho,
GlobalV::ofs_running);
write_mlkedf_desc.generateTrainData_KS(PARAM.globalv.global_mlkedf_descriptor_dir,
stp.template get_psi_t<T, Device>(),
pelec,
pw_wfc,
pw_rho,
ucell,
pelec->pot->get_eff_v(0),
chr.nrxx);
}
#endif
ModuleBase::timer::end("ModuleIO", "ctrl_runner_pw");
}
// complex<float> + CPU
template void ModuleIO::ctrl_scf_pw<std::complex<float>, base_device::DEVICE_CPU>(const int nstep,
UnitCell& ucell,
elecstate::ElecState* pelec,
const Charge& chr,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* pw_wfc,
const ModulePW::PW_Basis* pw_rho,
const ModulePW::PW_Basis* pw_rhod,
const ModulePW::PW_Basis_Big* pw_big,
Setup_Psi_pw& stp,
const Parallel_Grid& para_grid,
const Input_para& inp);
// complex<double> + CPU
template void ModuleIO::ctrl_scf_pw<std::complex<double>, base_device::DEVICE_CPU>(const int nstep,
UnitCell& ucell,
elecstate::ElecState* pelec,
const Charge& chr,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* pw_wfc,
const ModulePW::PW_Basis* pw_rho,
const ModulePW::PW_Basis* pw_rhod,
const ModulePW::PW_Basis_Big* pw_big,
Setup_Psi_pw& stp,
const Parallel_Grid& para_grid,
const Input_para& inp);
#if ((defined __CUDA) || (defined __ROCM))
// complex<float> + GPU
template void ModuleIO::ctrl_scf_pw<std::complex<float>, base_device::DEVICE_GPU>(const int nstep,
UnitCell& ucell,
elecstate::ElecState* pelec,
const Charge& chr,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* pw_wfc,
const ModulePW::PW_Basis* pw_rho,
const ModulePW::PW_Basis* pw_rhod,
const ModulePW::PW_Basis_Big* pw_big,
Setup_Psi_pw& stp,
const Parallel_Grid& para_grid,
const Input_para& inp);
// complex<double> + GPU
template void ModuleIO::ctrl_scf_pw<std::complex<double>, base_device::DEVICE_GPU>(const int nstep,
UnitCell& ucell,
elecstate::ElecState* pelec,
const Charge& chr,
const K_Vectors& kv,
const ModulePW::PW_Basis_K* pw_wfc,
const ModulePW::PW_Basis* pw_rho,
const ModulePW::PW_Basis* pw_rhod,
const ModulePW::PW_Basis_Big* pw_big,
Setup_Psi_pw& stp,
const Parallel_Grid& para_grid,
const Input_para& inp);
#endif
// complex<float> + CPU
template void ModuleIO::ctrl_runner_pw<std::complex<float>, base_device::DEVICE_CPU>(UnitCell& ucell,
elecstate::ElecState* pelec,
ModulePW::PW_Basis_K* pw_wfc,
ModulePW::PW_Basis* pw_rho,
ModulePW::PW_Basis* pw_rhod,
Charge& chr,
K_Vectors& kv,
Setup_Psi_pw& stp,
Structure_Factor& sf,
pseudopot_cell_vnl& ppcell,
surchem& solvent,
Parallel_Grid& para_grid,
const Input_para& inp);
// complex<double> + CPU
template void ModuleIO::ctrl_runner_pw<std::complex<double>, base_device::DEVICE_CPU>(UnitCell& ucell,
elecstate::ElecState* pelec,
ModulePW::PW_Basis_K* pw_wfc,
ModulePW::PW_Basis* pw_rho,
ModulePW::PW_Basis* pw_rhod,
Charge& chr,
K_Vectors& kv,
Setup_Psi_pw& stp,
Structure_Factor& sf,
pseudopot_cell_vnl& ppcell,
surchem& solvent,
Parallel_Grid& para_grid,
const Input_para& inp);
#if ((defined __CUDA) || (defined __ROCM))
// complex<float> + GPU
template void ModuleIO::ctrl_runner_pw<std::complex<float>, base_device::DEVICE_GPU>(UnitCell& ucell,
elecstate::ElecState* pelec,
ModulePW::PW_Basis_K* pw_wfc,
ModulePW::PW_Basis* pw_rho,
ModulePW::PW_Basis* pw_rhod,
Charge& chr,
K_Vectors& kv,
Setup_Psi_pw& stp,
Structure_Factor& sf,
pseudopot_cell_vnl& ppcell,
surchem& solvent,
Parallel_Grid& para_grid,
const Input_para& inp);
// complex<double> + GPU
template void ModuleIO::ctrl_runner_pw<std::complex<double>, base_device::DEVICE_GPU>(UnitCell& ucell,
elecstate::ElecState* pelec,
ModulePW::PW_Basis_K* pw_wfc,
ModulePW::PW_Basis* pw_rho,
ModulePW::PW_Basis* pw_rhod,
Charge& chr,
K_Vectors& kv,
Setup_Psi_pw& stp,
Structure_Factor& sf,
pseudopot_cell_vnl& ppcell,
surchem& solvent,
Parallel_Grid& para_grid,
const Input_para& inp);
#endif