Repository navigation
Expand file tree
/
Copy pathesolver_of_tool.cpp
More file actions
519 lines (485 loc) · 19.1 KB
/
Copy pathesolver_of_tool.cpp
File metadata and controls
519 lines (485 loc) · 19.1 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
#include "esolver_of.h"
#include "source_base/formatter.h"
#include "source_base/memory.h"
#include "source_estate/module_pot/efield.h"
#include "source_estate/module_pot/gatefield.h"
#include "source_pw/module_pwdft/global.h"
#include "source_io/module_parameter/parameter.h"
#include "source_estate/cal_ux.h"
namespace ModuleESolver
{
/**
* @brief Initialize this->pelec, as well as this->pelec->pot
*
* @param ucell
*/
void ESolver_OF::init_elecstate(UnitCell& ucell)
{
if (this->pelec == nullptr)
{
this->pelec = new elecstate::ElecState((Charge*)(&chr), this->pw_rho, pw_big);
this->chr.allocate(PARAM.inp.nspin);
}
this->pelec->omega = ucell.omega;
delete this->pelec->pot;
this->pelec->pot = new elecstate::Potential(this->pw_rhod,
this->pw_rho,
&ucell,
&(this->locpp.vloc),
&(this->sf),
&(this->solvent),
&(this->pelec->f_en.etxc),
&(this->pelec->f_en.vtxc));
// There is no Operator in ESolver_OF, register Potentials here!
std::vector<std::string> pot_register_in;
if (PARAM.inp.vion_in_h)
{
pot_register_in.push_back("local");
}
if (PARAM.inp.vh_in_h)
{
pot_register_in.push_back("hartree");
}
// no variable can choose xc, maybe it is necessary
pot_register_in.push_back("xc");
if (PARAM.inp.imp_sol)
{
pot_register_in.push_back("surchem");
}
if (PARAM.inp.efield_flag)
{
pot_register_in.push_back("efield");
}
if (PARAM.inp.gate_flag)
{
pot_register_in.push_back("gatefield");
}
// only Potential is not empty, Veff and Meta are available
if (pot_register_in.size() > 0)
{
// register Potential by gathered operator
this->pelec->pot->pot_register(pot_register_in);
}
}
/**
* @brief Allocate the arrays, as well as this->psi_ and this->ptemp_rho_.
*/
void ESolver_OF::allocate_array()
{
// Initialize the "wavefunction", which is sqrt(rho)
this->psi_ = new psi::Psi<double>(1,
PARAM.inp.nspin,
this->pw_rho->nrxx,
this->pw_rho->nrxx,
true);
ModuleBase::Memory::record("OFDFT::Psi", sizeof(double) * PARAM.inp.nspin * this->pw_rho->nrxx);
this->pphi_ = new double*[PARAM.inp.nspin];
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
this->pphi_[is] = this->psi_->get_pointer(is);
}
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT PHI");
// initialize chemical potential, step length, ...
delete this->ptemp_rho_;
this->ptemp_rho_ = new Charge();
this->ptemp_rho_->set_rhopw(this->pw_rho);
this->ptemp_rho_->allocate(PARAM.inp.nspin);
this->theta_ = new double[PARAM.inp.nspin];
this->pdLdphi_ = new double*[PARAM.inp.nspin];
this->pdEdphi_ = new double*[PARAM.inp.nspin];
this->pdirect_ = new double*[PARAM.inp.nspin];
this->precip_dir_ = new std::complex<double>*[PARAM.inp.nspin];
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
this->pdLdphi_[is] = new double[this->pw_rho->nrxx];
this->pdEdphi_[is] = new double[this->pw_rho->nrxx];
this->pdirect_[is] = new double[this->pw_rho->nrxx];
this->precip_dir_[is] = new std::complex<double>[pw_rho->npw];
}
ModuleBase::Memory::record("OFDFT::pdLdphi_", sizeof(double) * PARAM.inp.nspin * this->pw_rho->nrxx);
ModuleBase::Memory::record("OFDFT::pdEdphi_", sizeof(double) * PARAM.inp.nspin * this->pw_rho->nrxx);
ModuleBase::Memory::record("OFDFT::pdirect_", sizeof(double) * PARAM.inp.nspin * this->pw_rho->nrxx);
ModuleBase::Memory::record("OFDFT::precip_dir_", sizeof(std::complex<double>) * PARAM.inp.nspin * this->pw_rho->npw);
}
/**
* @brief Get dL/dphi = dL/drho * drho/dphi = (dE/drho - mu) * 2 * ptemp_phi and
* store it in rdLdphi
*
* @param [in] ptemp_phi phi
* @param [out] rdLdphi dL/dphi
*/
void ESolver_OF::cal_potential(double* ptemp_phi, double* rdLdphi, UnitCell& ucell)
{
double** dEdtemp_phi = new double*[PARAM.inp.nspin];
double** temp_phi = new double*[PARAM.inp.nspin];
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
dEdtemp_phi[is] = new double[this->pw_rho->nrxx];
if (is == this->tn_spin_flag_)
{
temp_phi[is] = ptemp_phi;
}
else
{
temp_phi[is] = this->pphi_[is];
}
for (int ir = 0; ir < this->pw_rho->nrxx; ++ir)
{
this->ptemp_rho_->rho[is][ir] = temp_phi[is][ir] * temp_phi[is][ir];
}
}
elecstate::cal_ux(ucell);
this->pelec->pot->update_from_charge(this->ptemp_rho_, &ucell);
ModuleBase::matrix& vr_eff = this->pelec->pot->get_effective_v();
this->kedf_manager_->get_potential(this->ptemp_rho_->rho,
temp_phi,
this->pw_rho,
vr_eff); // KEDF potential
for (int i = 0; i < this->pw_rho->nrxx; ++i)
{
dEdtemp_phi[this->tn_spin_flag_][i] = vr_eff(this->tn_spin_flag_, i);
}
double temp_mu = this->cal_mu(ptemp_phi, dEdtemp_phi[this->tn_spin_flag_], this->nelec_[this->tn_spin_flag_]);
for (int i = 0; i < this->pw_rho->nrxx; ++i)
{
rdLdphi[i] = dEdtemp_phi[this->tn_spin_flag_][i] - 2. * temp_mu * ptemp_phi[i];
}
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
delete[] dEdtemp_phi[is];
}
delete[] dEdtemp_phi;
delete[] temp_phi;
}
/**
* @brief Calculate dE/dTheta and store it in rdEdtheta.
* dE/dTheta = <dE / dtemp_phi | dtemp_phi / dTheta>
* = <dE / dtemp_phi | - sin(theta) * phi + cos(theta) * direction>
*
* @param [in] ptemp_phi
* @param [in] temp_rho
* @param [in] ucell
* @param [in] ptheta
* @param [out] rdEdtheta dE/dTheta
*/
void ESolver_OF::cal_dEdtheta(double** ptemp_phi, Charge* temp_rho, UnitCell& ucell, double* ptheta, double* rdEdtheta)
{
double* dphi_dtheta = new double[this->pw_rho->nrxx];
elecstate::cal_ux(ucell);
this->pelec->pot->update_from_charge(temp_rho, &ucell);
ModuleBase::matrix& vr_eff = this->pelec->pot->get_effective_v();
this->kedf_manager_->get_potential(temp_rho->rho,
ptemp_phi,
this->pw_rho,
vr_eff); // KEDF potential
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
for (int ir = 0; ir < this->pw_rho->nrxx; ++ir)
{
this->pdEdphi_[is][ir] = vr_eff(is, ir);
dphi_dtheta[ir] = -this->pphi_[is][ir] * sin(ptheta[is]) + this->pdirect_[is][ir] * cos(ptheta[is]);
}
rdEdtheta[is] = this->inner_product(this->pdEdphi_[is], dphi_dtheta, this->pw_rho->nrxx, this->dV_);
Parallel_Reduce::reduce_all(rdEdtheta[is]);
}
delete[] dphi_dtheta;
}
/**
* @brief Calculate the chemical potential mu.
* mu = <dE/dphi|phi> / (2 * nelec)
*
* @param pphi
* @param pdEdphi
* @param nelec
* @return mu
*/
double ESolver_OF::cal_mu(double* pphi, double* pdEdphi, double nelec)
{
double mu = this->inner_product(pphi, pdEdphi, this->pw_rho->nrxx, this->dV_);
Parallel_Reduce::reduce_all(mu);
mu = mu / (2.0 * nelec);
return mu;
}
/**
* @brief Rotate and renormalize the direction |d>,
* make it orthogonal to phi (<d|phi> = 0), and <d|d> = nelec
*/
void ESolver_OF::adjust_direction()
{
// filter the high frequency term in direction if of_full_pw = false
if (!PARAM.inp.of_full_pw)
{
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
pw_rho->real2recip(this->pdirect_[is], this->precip_dir_[is]);
pw_rho->recip2real(this->precip_dir_[is], this->pdirect_[is]);
}
}
if (PARAM.inp.nspin == 1)
{
double temp_theta = 0; // temp_theta = |d'|/|d0 + phi|, theta = min(theta, temp_theta)
// (1) make direction orthogonal to phi
// |d'> = |d0> - |phi><phi|d0>/nelec
double inner_phi_direction
= this->inner_product(this->pphi_[0], this->pdirect_[0], this->pw_rho->nrxx, this->dV_);
Parallel_Reduce::reduce_all(inner_phi_direction);
for (int i = 0; i < this->pw_rho->nrxx; ++i)
{
temp_theta += pow(this->pdirect_[0][i] + this->pphi_[0][i], 2);
this->pdirect_[0][i] = this->pdirect_[0][i] - this->pphi_[0][i] * inner_phi_direction / this->nelec_[0];
}
Parallel_Reduce::reduce_all(temp_theta);
temp_theta = std::sqrt(temp_theta);
// (2) renormalize direction
// |d> = |d'> * \sqrt(nelec) / <d'|d'>
double norm_direction
= this->inner_product(this->pdirect_[0], this->pdirect_[0], this->pw_rho->nrxx, this->dV_);
Parallel_Reduce::reduce_all(norm_direction);
norm_direction = std::sqrt(norm_direction);
for (int i = 0; i < this->pw_rho->nrxx; ++i)
{
this->pdirect_[0][i] = std::sqrt(this->nelec_[0]) * this->pdirect_[0][i] / norm_direction;
}
temp_theta = norm_direction / temp_theta;
this->theta_[0] = std::min(this->theta_[0], temp_theta);
}
else if (PARAM.inp.nspin == 2) // theta = 0
{
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
// (1) make direction orthogonal to phi
// |d'> = |d0> - |phi><phi|d0>/nelec
double inner_phi_direction
= this->inner_product(this->pphi_[is], this->pdirect_[is], this->pw_rho->nrxx, this->dV_);
Parallel_Reduce::reduce_all(inner_phi_direction);
for (int i = 0; i < this->pw_rho->nrxx; ++i)
{
this->pdirect_[is][i]
= this->pdirect_[is][i] - this->pphi_[is][i] * inner_phi_direction / this->nelec_[is];
}
// (2) renormalize direction
// |d> = |d'> * \sqrt(nelec) / <d'|d'>
double norm_direction
= this->inner_product(this->pdirect_[is], this->pdirect_[is], this->pw_rho->nrxx, this->dV_);
Parallel_Reduce::reduce_all(norm_direction);
norm_direction = std::sqrt(norm_direction);
for (int i = 0; i < this->pw_rho->nrxx; ++i)
{
this->pdirect_[is][i] = std::sqrt(this->nelec_[is]) * this->pdirect_[is][i] / norm_direction;
}
this->theta_[is] = 0.;
}
}
}
/**
* @brief Make sure that dEdtheta<0 at theta = 0,
* preparing to call the line search
*
* @param dEdtheta
* @param ptemp_phi
* @param ucell
*/
void ESolver_OF::check_direction(double* dEdtheta, double** ptemp_phi, UnitCell& ucell)
{
assert(PARAM.inp.nspin > 0);
double* temp_theta = new double[PARAM.inp.nspin];
ModuleBase::GlobalFunc::ZEROS(temp_theta, PARAM.inp.nspin);
double max_dEdtheta = 1e5; // threshould of dEdtheta, avoid the unstable optimization
this->cal_dEdtheta(ptemp_phi, this->ptemp_rho_, ucell, temp_theta, dEdtheta);
// Assert dEdtheta(theta = 0) < 0, otherwise line search will not work.
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
if (dEdtheta[is] > max_dEdtheta)
{
std::cout << "dEdtheta " << dEdtheta[is] << std::endl;
ModuleBase::WARNING_QUIT("esolver_of.cpp", "dE/dtheta is too large.");
}
else if (dEdtheta[is] > 0)
{
GlobalV::ofs_warning << "ESolver_OF: WARNING "
<< "dEdphi > 0, replace direct with steepest descent method." << std::endl;
for (int ir = 0; ir < this->pw_rho->nrxx; ++ir)
{
this->pdirect_[is][ir] = -this->pdLdphi_[is][ir];
}
this->adjust_direction();
this->cal_dEdtheta(ptemp_phi, this->ptemp_rho_, ucell, temp_theta, dEdtheta);
if (dEdtheta[is] > max_dEdtheta)
{
std::cout << "dEdtheta " << dEdtheta[is] << std::endl;
ModuleBase::WARNING_QUIT("esolver_of.cpp", "dE/dtheta is too large.");
}
else if (dEdtheta[is] > 0)
{
GlobalV::ofs_warning << "ESolver_OF: WARNING "
<< "when use steepest dencent method, "
"dEdphi > 0, so we might get minimum."
<< std::endl;
}
}
}
delete[] temp_theta;
}
/**
* @brief ONLY used for test.
* Check the validity of KEDF
*
* @param dEdtheta
* @param ptemp_phi
* @param ucell
*/
void ESolver_OF::test_direction(double* dEdtheta, double** ptemp_phi, UnitCell& ucell)
{
double temp_energy = 0.;
if (this->iter_ == 0)
{
for (int i = -100; i < 100; ++i)
{
this->theta_[0] = 0.001 * i;
for (int ir = 0; ir < this->pw_rho->nrxx; ++ir)
{
ptemp_phi[0][ir]
= this->pphi_[0][ir] * cos(this->theta_[0]) + this->pdirect_[0][ir] * sin(this->theta_[0]);
ptemp_rho_->rho[0][ir] = ptemp_phi[0][ir] * ptemp_phi[0][ir];
}
this->cal_dEdtheta(ptemp_phi, ptemp_rho_, ucell, this->theta_, dEdtheta);
this->pelec->cal_energies(2);
temp_energy = this->pelec->f_en.etot;
double kinetic_energy = 0.;
double pseudopot_energy = 0.;
kinetic_energy = this->kedf_manager_->get_energy();
pseudopot_energy = this->inner_product(this->pelec->pot->get_fixed_v(),
this->ptemp_rho_->rho[0],
this->pw_rho->nrxx,
this->dV_);
Parallel_Reduce::reduce_all(pseudopot_energy);
temp_energy += kinetic_energy + pseudopot_energy;
GlobalV::ofs_warning << i << " " << dEdtheta[0] << " " << temp_energy << std::endl;
if (this->theta_[0] == 0) {
std::cout << "dEdtheta " << dEdtheta[0] << std::endl;
}
}
exit(0);
}
}
/**
* @brief Print nessecary information to the screen,
* and write the components of the total energy into running_log.
*/
void ESolver_OF::print_info(const bool conv_esolver)
{
if (this->iter_ == 0)
{
std::cout << " ============================= Running OFDFT "
"=============================="
<< std::endl;
std::cout << " ITER ETOT/eV EDIFF/eV EFERMI/eV POTNORM TIME/s"
<< std::endl;
}
std::map<std::string, std::string> prefix_map = {
{"cg1", "CG"},
{"cg2", "CG"},
{"tn", "TN"}
};
std::string iteration = prefix_map[PARAM.inp.of_method] + std::to_string(this->iter_);
#ifdef __MPI
double duration = (double)(MPI_Wtime() - this->iter_time);
#else
double duration
= (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - this->iter_time)).count()
/ static_cast<double>(1e6);
#endif
std::cout << " " << std::setw(8) << iteration
<< std::setw(18) << std::scientific << std::setprecision(8) << this->energy_current_ * ModuleBase::Ry_to_eV
<< std::setw(18) << (this->energy_current_ - this->energy_last_) * ModuleBase::Ry_to_eV
<< std::setw(13) << std::setprecision(4) << this->pelec->eferm.get_efval(0) * ModuleBase::Ry_to_eV
<< std::setw(13) << std::setprecision(4) << this->normdLdphi_
<< std::setw(6) << std::fixed << std::setprecision(2) << duration << std::endl;
GlobalV::ofs_running << std::setprecision(12);
GlobalV::ofs_running << std::setiosflags(std::ios::right);
GlobalV::ofs_running << "\nIter" << this->iter_ << ": the norm of potential is " << this->normdLdphi_ << std::endl;
std::vector<std::string> titles;
std::vector<double> energies_Ry;
std::vector<double> energies_eV;
if ((PARAM.inp.out_band[0] > 0 &&
((this->iter_ + 1) % PARAM.inp.out_band[0] == 0 ||
conv_esolver ||
this->iter_ == PARAM.inp.scf_nmax)) ||
PARAM.inp.init_chg == "file")
{
titles.push_back("E_Total");
energies_Ry.push_back(this->pelec->f_en.etot);
titles.push_back("E_Kinetic");
energies_Ry.push_back(this->pelec->f_en.ekinetic);
titles.push_back("E_Hartree");
energies_Ry.push_back(this->pelec->f_en.hartree_energy);
titles.push_back("E_xc");
energies_Ry.push_back(this->pelec->f_en.etxc - this->pelec->f_en.etxcc);
titles.push_back("E_LocalPP");
energies_Ry.push_back(this->pelec->f_en.e_local_pp);
titles.push_back("E_Ewald");
energies_Ry.push_back(this->pelec->f_en.ewald_energy);
this->kedf_manager_->record_energy(titles, energies_Ry);
std::string vdw_method = PARAM.inp.vdw_method;
if (vdw_method == "d2") // Peize Lin add 2014-04, update 2021-03-09
{
titles.push_back("E_vdwD2");
energies_Ry.push_back(this->pelec->f_en.evdw);
}
else if (vdw_method == "d3_0" || vdw_method == "d3_bj") // jiyy add 2019-05, update 2021-05-02
{
titles.push_back("E_vdwD3");
energies_Ry.push_back(this->pelec->f_en.evdw);
}
if (PARAM.inp.imp_sol)
{
titles.push_back("E_sol_el");
energies_Ry.push_back(this->pelec->f_en.esol_el);
titles.push_back("E_sol_cav");
energies_Ry.push_back(this->pelec->f_en.esol_cav);
}
if (PARAM.inp.efield_flag)
{
titles.push_back("E_efield");
energies_Ry.push_back(elecstate::Efield::etotefield);
}
if (PARAM.inp.gate_flag)
{
titles.push_back("E_gatefield");
energies_Ry.push_back(elecstate::Gatefield::etotgatefield);
}
}
else
{
titles.push_back("E_Total");
energies_Ry.push_back(this->pelec->f_en.etot);
}
if (PARAM.globalv.two_fermi)
{
titles.push_back("E_Fermi_up");
energies_Ry.push_back(this->pelec->eferm.get_efval(0));
titles.push_back("E_Fermi_dw");
energies_Ry.push_back(this->pelec->eferm.get_efval(1));
}
else
{
titles.push_back("E_Fermi");
energies_Ry.push_back(this->pelec->eferm.get_efval(0));
}
energies_eV.resize(energies_Ry.size());
std::transform(energies_Ry.begin(), energies_Ry.end(), energies_eV.begin(), [](double energy) {
return energy * ModuleBase::Ry_to_eV;
});
FmtTable table(/*titles=*/{"Energy", "Rydberg", "eV"},
/*nrows=*/titles.size(),
/*formats=*/{"%20s", "%20.12f", "%20.12f"}, 0);
table << titles << energies_Ry << energies_eV;
GlobalV::ofs_running << table.str() << std::endl;
// reset the iter_time for the next iteration
#ifdef __MPI
this->iter_time = MPI_Wtime();
#else
this->iter_time = std::chrono::system_clock::now();
#endif
}
} // namespace ModuleESolver