-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpotentials_coulomb.cpp
310 lines (281 loc) · 13.2 KB
/
potentials_coulomb.cpp
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
// [class Potentials]
// pseudopotential, Jastrow, Ewald sum
// given in atomic unit (Hartree)
#include "include/header.hpp"
// For divergence correction (Coulomb potential)
void Potentials::set_sum_of_Vaux(const Parallelization ¶llelization,
const Method &method,
const CrystalStructure &crystal_structure,
const Kpoints &kpoints)
{
if (method.calc_method()=="FREE") { return; } // free-electron mode: no interaction
// divergence corrction is considered only for Coulomb interaction.
const bool is_coulomb_only =
(method.calc_method()=="HF" || jastrow.is_A_zero()) ? true : false;
// not used in HF-BAND
if (!(is_coulomb_only && method.calc_mode()=="BAND"))
{
set_sum_of_Vaux_each(parallelization,
crystal_structure,
kpoints,
kpoints.kvectors_scf(),
sum_of_Vaux_scf_);
// used only when (BI)TC && SCF
if (!is_coulomb_only && method.calc_mode()=="SCF")
{
set_sum_of_kVaux_each(parallelization,
crystal_structure,
kpoints,
kpoints.kvectors_scf(),
sum_of_kVaux_scf_);
}
}
if (method.calc_mode()=="BAND")
{
set_sum_of_Vaux_each(parallelization,
crystal_structure,
kpoints,
kpoints.kvectors_band(),
sum_of_Vaux_band_);
if (!is_coulomb_only)
{
// used only when (BI)TC && BAND
set_sum_of_kVaux_each(parallelization,
crystal_structure,
kpoints,
kpoints.kvectors_band(),
sum_of_kVaux_band_);
}
}
}
// private, called in set_sum_of_Vaux
void Potentials::set_sum_of_Vaux_each(const Parallelization ¶llelization,
const CrystalStructure &crystal_structure,
const Kpoints &kpoints,
const std::vector<std::vector<Eigen::Vector3d> > &kvectors,
std::vector<double> &sum_of_Vaux_)
{
const int num_irreducible_kpoints = kvectors.size(); // num_irreducible_kpoints_(scf/band)
const int num_irreducible_kpoints_scf = kpoints.num_irreducible_kpoints_scf();
alpha_Vaux_ = std::cbrt(crystal_structure.unit_cell_volume())/(2.0*PI);
// alpha_Vaux_ = (alpha_Vaux_ * alpha_Vaux_)/2.0; // alpha = 0.5*V^(2/3)/(4*PI^2)
alpha_Vaux_ = (alpha_Vaux_ * alpha_Vaux_); // alpha = V^(2/3)/(4*PI^2)
// set parallelization index (only used here) cf. Parallelization::set_orbital_parallelization()
std::vector<bool> if_match(num_irreducible_kpoints);
int ibegin, iend;
set_parallelization_in_sum_Vaux(parallelization, kpoints, kvectors, if_match, ibegin, iend);
// calculate
bool if_match_kq;
std::vector<double> sum_of_Vaux_local(num_irreducible_kpoints);
for (int ik=0; ik<num_irreducible_kpoints; ik++)
{
sum_of_Vaux_local[ik] = 0.0;
int icount = 0;
for (int iq=0; iq<num_irreducible_kpoints_scf; iq++)
{
for (int isymq=0; isymq<kpoints.kvectors_scf()[iq].size(); isymq++)
{
if (kpoints.kweight_scf()[iq] < 1e-8) { continue; } // zero k-weight (i.e. band k-weight)
Eigen::Vector3d kq = kvectors[ik][0] - kpoints.kvectors_scf()[iq][isymq];
for (int idim=0; idim<3; idim++)
{
while (kq(idim) < -1e-8) { kq(idim) += 1.0; }
while (kq(idim) > 1.0-1e-8) { kq(idim) -= 1.0; }
}
if (kq.squaredNorm()<1e-8)
{
if_match_kq = true;
}
else
{
if_match_kq = false;
}
icount++;
if ((icount-1)<ibegin || (icount-1)>iend) { continue; } // not assigned to this MPI process
if (!if_match_kq) // nshell=0 contribution
{
Eigen::Vector3d kqvect = crystal_structure.reciprocal_vectors().transpose()*kq;
double kq2 = kqvect.squaredNorm();
sum_of_Vaux_local[ik] += std::exp(-alpha_Vaux_*kq2)/kq2;
}
for (int nshell=1; ; nshell++)
{
double sum_on_shell = 0.0;
for (int Gx=-nshell; Gx<=+nshell; Gx++)
{
for (int Gy=-nshell; Gy<=+nshell; Gy++)
{
for (int Gz=-nshell; Gz<=+nshell; Gz++)
{
if (Gx!=nshell && -Gx!=nshell && Gy!=nshell && -Gy!=nshell
&& Gz!=nshell && -Gz!=nshell) { continue; }
Eigen::Vector3i tmpG = {Gx, Gy, Gz};
Eigen::Vector3d kqGvect = crystal_structure.reciprocal_vectors().transpose()
* (kq + tmpG.cast<double>());
double kqG2 = kqGvect.squaredNorm();
sum_on_shell += std::exp(-alpha_Vaux_*kqG2)/kqG2;
} // Gz
} // Gy
} // Gx
sum_of_Vaux_local[ik] += sum_on_shell;
if (sum_on_shell < 1e-20) { break; }
} // int nshell
} // isymq
} // iq
sum_of_Vaux_local[ik] *= FourPI;
} // ik
// allreduce
sum_of_Vaux_.resize(num_irreducible_kpoints);
MPI_Allreduce(&sum_of_Vaux_local[0],
&sum_of_Vaux_[0],
num_irreducible_kpoints,
MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
for (int ik=0; ik<num_irreducible_kpoints; ik++)
{
if (if_match[ik]) // 4pi*exp[-alpha q^2]/q^2 ~ 4pi/q^2 - 4pi*alpha at q=0
// (Note: if_match==true, both nshell=0 contribution in Vaux and 1/0^2 in coulomb are not included)
{
// This is not the correction for \sum_G 4pi*exp[-alpha q^2]/q^2,
// rather the correction in \int 4pi/q^2 ~ \int 4pi*exp[-alpha q^2]/q^2 + 4pi*alpha for \int dq near q=0.
// In calc_hamiltonian_two_body_x, we calculate \int - sum_of_Vaux.
// Thus, "-4pi*alpha" correction for sum_of_Vaux = "+4pi*alpha" correction for \int.
sum_of_Vaux_[ik] -= FourPI*alpha_Vaux_;
}
}
}
void Potentials::set_sum_of_kVaux_each(const Parallelization ¶llelization,
const CrystalStructure &crystal_structure,
const Kpoints &kpoints,
const std::vector<std::vector<Eigen::Vector3d> > &kvectors,
std::vector<Eigen::Vector3d> &sum_of_kVaux_)
{
const int num_irreducible_kpoints = kvectors.size(); // num_irreducible_kpoints_(scf/band)
const int num_irreducible_kpoints_scf = kpoints.num_irreducible_kpoints_scf();
// alpha_Vaux_ is already set in set_sum_of_Vaux_each()
assert(std::abs(alpha_Vaux_) > 1e-8);
// set parallelization index (only used here) cf. Parallelization::set_orbital_parallelization()
std::vector<bool> if_match(num_irreducible_kpoints);
int ibegin, iend;
set_parallelization_in_sum_Vaux(parallelization, kpoints, kvectors, if_match, ibegin, iend);
sum_of_kVaux_.resize(num_irreducible_kpoints);
// calculate
bool if_match_kq;
Eigen::Vector3d sum_of_kVaux_local;
for (int ik=0; ik<num_irreducible_kpoints; ik++)
{
if (if_match[ik]) // if_match=true then sum_of_kVaux = 0
{
sum_of_kVaux_[ik] = {0.0, 0.0, 0.0};
continue;
}
int icount = 0;
sum_of_kVaux_local = {0.0, 0.0, 0.0};
for (int iq=0; iq<num_irreducible_kpoints_scf; iq++)
{
for (int isymq=0; isymq<kpoints.kvectors_scf()[iq].size(); isymq++)
{
if (kpoints.kweight_scf()[iq] < 1e-8) { continue; } // zero k-weight (i.e. band k-weight)
Eigen::Vector3d kq = kvectors[ik][0] - kpoints.kvectors_scf()[iq][isymq];
for (int idim=0; idim<3; idim++)
{
while (kq(idim) < -1e-8) { kq(idim) += 1.0; }
while (kq(idim) > 1.0-1e-8) { kq(idim) -= 1.0; }
}
if (kq.squaredNorm()<1e-8)
{
if_match_kq = true;
}
else
{
if_match_kq = false;
}
icount++;
if ((icount-1)<ibegin || (icount-1)>iend) { continue; } // not assigned to this MPI process
if (!if_match_kq) // nshell=0 contribution
{
Eigen::Vector3d kqvect = crystal_structure.reciprocal_vectors().transpose()*kq;
double kq2 = kqvect.squaredNorm();
sum_of_kVaux_local += (std::exp(-alpha_Vaux_*kq2)/kq2) * kqvect;
}
for (int nshell=1; ; nshell++)
{
Eigen::Vector3d sum_on_shell = {0.0, 0.0, 0.0};
for (int Gx=-nshell; Gx<=+nshell; Gx++)
{
for (int Gy=-nshell; Gy<=+nshell; Gy++)
{
for (int Gz=-nshell; Gz<=+nshell; Gz++)
{
if (Gx!=nshell && -Gx!=nshell && Gy!=nshell && -Gy!=nshell
&& Gz!=nshell && -Gz!=nshell) { continue; }
Eigen::Vector3i tmpG = {Gx, Gy, Gz};
Eigen::Vector3d kqGvect = crystal_structure.reciprocal_vectors().transpose()
* (kq + tmpG.cast<double>());
double kqG2 = kqGvect.squaredNorm();
sum_on_shell += (std::exp(-alpha_Vaux_*kqG2)/kqG2) * kqGvect;
} // Gz
} // Gy
} // Gx
sum_of_kVaux_local += sum_on_shell;
if (sum_on_shell.squaredNorm() < 1e-20) { break; }
} // int nshell
} // isymq
} // iq
sum_of_kVaux_local *= FourPI;
MPI_Allreduce(sum_of_kVaux_local.data(),
sum_of_kVaux_[ik].data(),
3, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
} // ik
}
// set parallelization index (used only in set_sum_of_Vaux_each() and set_sum_of_kVaux_band)
// cf. Parallelization::set_orbital_parallelization()
void Potentials::set_parallelization_in_sum_Vaux(const Parallelization ¶llelization,
const Kpoints &kpoints,
const std::vector<std::vector<Eigen::Vector3d> > &kvectors,
std::vector<bool> &if_match,
int &ibegin, int &iend)
{
const int num_irreducible_kpoints = kvectors.size(); // num_irreducible_kpoints_(scf/band)
const int num_irreducible_kpoints_scf = kpoints.num_irreducible_kpoints_scf();
const int num_mpi_processes = parallelization.num_mpi_processes();
const int my_mpi_rank = parallelization.my_mpi_rank();
for (int ik=0; ik<num_irreducible_kpoints; ik++)
{
if_match[ik] = false;
for (int iq=0; iq<num_irreducible_kpoints_scf; iq++)
{
for (int isymq=0; isymq<kpoints.kvectors_scf()[iq].size(); isymq++)
{
if (kpoints.kweight_scf()[iq] < 1e-8) { continue; } // zero k-weight (i.e. band k-weight)
Eigen::Vector3d kq = kvectors[ik][0] - kpoints.kvectors_scf()[iq][isymq];
for (int idim=0; idim<3; idim++)
{
while (kq(idim) < -1e-8) { kq(idim) += 1.0; }
while (kq(idim) > 1.0-1e-8) { kq(idim) -= 1.0; }
}
if (kq.squaredNorm()<1e-8) { if_match[ik] = true; } // always true for the SCF k-mesh
}
}
}
int icount = 0;
for (int iq=0; iq<num_irreducible_kpoints_scf; iq++)
{
for (int isymq=0; isymq<kpoints.kvectors_scf()[iq].size(); isymq++)
{
if (kpoints.kweight_scf()[iq] < 1e-8) { continue; } // zero k-weight (i.e. band k-weight)
icount++;
}
}
std::vector<int> ncount_each(num_mpi_processes);
std::fill(ncount_each.begin(), ncount_each.end(), icount/num_mpi_processes);
for (int iproc=0; iproc<(icount%num_mpi_processes); iproc++)
{
ncount_each[iproc]++;
}
ibegin = 0;
for (int iproc=0; iproc<my_mpi_rank; iproc++)
{
ibegin += ncount_each[iproc];
}
iend = ibegin + ncount_each[my_mpi_rank] - 1;
}