forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcal_ldos.cpp
More file actions
368 lines (321 loc) · 12.7 KB
/
Copy pathcal_ldos.cpp
File metadata and controls
368 lines (321 loc) · 12.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
#include "cal_ldos.h"
#include "cal_dos.h"
#include "cube_io.h"
#include "source_estate/module_dm/cal_dm_psi.h"
#include "source_lcao/module_gint/temp_gint/gint_interface.h"
#include <type_traits>
namespace ModuleIO
{
#ifdef __LCAO
template <typename T>
void Cal_ldos<T>::cal_ldos_lcao(const elecstate::ElecStateLCAO<T>* pelec,
const psi::Psi<T>& psi,
const Parallel_Grid& pgrid,
const UnitCell& ucell)
{
for (int ie = 0; ie < PARAM.inp.stm_bias[2]; ie++)
{
// energy range for ldos (efermi as reference)
const double en = PARAM.inp.stm_bias[0] + ie * PARAM.inp.stm_bias[1];
const double emin = en < 0 ? en : 0;
const double emax = en > 0 ? en : 0;
// calculate weight (for bands not in the range, weight is zero)
ModuleBase::matrix weight(pelec->ekb.nr, pelec->ekb.nc);
for (int ik = 0; ik < pelec->ekb.nr; ++ik)
{
const double efermi = pelec->eferm.get_efval(pelec->klist->isk[ik]);
for (int ib = 0; ib < pelec->ekb.nc; ib++)
{
const double eigenval = (pelec->ekb(ik, ib) - efermi) * ModuleBase::Ry_to_eV;
if (eigenval >= emin && eigenval <= emax)
{
weight(ik, ib) = en > 0 ? pelec->klist->wk[ik] - pelec->wg(ik, ib) : pelec->wg(ik, ib);
}
}
}
// calculate dm-like for ldos
const int nspin_dm = PARAM.inp.nspin == 2 ? 2 : 1;
elecstate::DensityMatrix<T, double> dm_ldos(pelec->DM->get_paraV_pointer(),
nspin_dm,
pelec->klist->kvec_d,
pelec->klist->get_nks() / nspin_dm);
elecstate::cal_dm_psi(pelec->DM->get_paraV_pointer(), weight, psi, dm_ldos);
dm_ldos.init_DMR(*(pelec->DM->get_DMR_pointer(1)));
dm_ldos.cal_DMR();
// allocate ldos space
std::vector<double> ldos_space(PARAM.inp.nspin * pelec->charge->nrxx);
double** ldos = new double*[PARAM.inp.nspin];
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
ldos[is] = &ldos_space[is * pelec->charge->nrxx];
}
// calculate ldos
#ifdef __OLD_GINT
ModuleBase::WARNING_QUIT("Cal_ldos::dm2ldos",
"do not support old grid integral, please recompile with __NEW_GINT");
#else
ModuleGint::cal_gint_rho(dm_ldos.get_DMR_vector(), PARAM.inp.nspin, ldos);
#endif
// I'm not sure whether ldos should be output for each spin or not
// ldos[0] += ldos[1] for nspin_dm == 2
if (nspin_dm == 2)
{
BlasConnector::axpy(pelec->charge->nrxx, 1.0, ldos[1], 1, ldos[0], 1);
}
// write ldos to cube file
std::stringstream fn;
fn << PARAM.globalv.global_out_dir << "LDOS_" << en << "eV"
<< ".cube";
const int precision = PARAM.inp.out_ldos[1];
ModuleIO::write_vdata_palgrid(pgrid,
ldos_space.data(),
0,
PARAM.inp.nspin,
0,
fn.str(),
0,
&ucell,
precision,
0);
// free memory
delete[] ldos;
}
}
#endif
template class Cal_ldos<double>; // Gamma_only case
template class Cal_ldos<std::complex<double>>; // multi-k case
// pw case
void cal_ldos_pw(const elecstate::ElecStatePW<std::complex<double>>* pelec,
const psi::Psi<std::complex<double>>& psi,
const Parallel_Grid& pgrid,
const UnitCell& ucell)
{
if (PARAM.inp.out_ldos[0] == 1 || PARAM.inp.out_ldos[0] == 3)
{
ModuleIO::stm_mode_pw(pelec, psi, pgrid, ucell);
}
if (PARAM.inp.out_ldos[0] == 2 || PARAM.inp.out_ldos[0] == 3)
{
ModuleIO::ldos_mode_pw(pelec, psi, pgrid, ucell);
}
}
void stm_mode_pw(const elecstate::ElecStatePW<std::complex<double>>* pelec,
const psi::Psi<std::complex<double>>& psi,
const Parallel_Grid& pgrid,
const UnitCell& ucell)
{
for (int ie = 0; ie < PARAM.inp.stm_bias[2]; ie++)
{
// energy range for ldos (efermi as reference)
const double en = PARAM.inp.stm_bias[0] + ie * PARAM.inp.stm_bias[1];
const double emin = en < 0 ? en : 0;
const double emax = en > 0 ? en : 0;
std::vector<double> ldos(pelec->charge->nrxx);
std::vector<std::complex<double>> wfcr(pelec->basis->nrxx);
for (int ik = 0; ik < pelec->klist->get_nks(); ++ik)
{
psi.fix_k(ik);
const double efermi = pelec->eferm.get_efval(pelec->klist->isk[ik]);
const int nbands = psi.get_nbands();
for (int ib = 0; ib < nbands; ib++)
{
pelec->basis->recip_to_real<std::complex<double>,base_device::DEVICE_CPU>(&psi(ib, 0), wfcr.data(), ik);
const double eigenval = (pelec->ekb(ik, ib) - efermi) * ModuleBase::Ry_to_eV;
double weight = en > 0 ? pelec->klist->wk[ik] - pelec->wg(ik, ib) : pelec->wg(ik, ib);
weight /= ucell.omega;
if (eigenval >= emin && eigenval <= emax)
{
for (int ir = 0; ir < pelec->basis->nrxx; ir++)
{
ldos[ir] += weight * norm(wfcr[ir]);
}
}
}
}
std::stringstream fn;
fn << PARAM.globalv.global_out_dir << "LDOS_" << en << "eV"
<< ".cube";
const int precision = PARAM.inp.out_ldos[1];
ModuleIO::write_vdata_palgrid(pgrid, ldos.data(), 0, PARAM.inp.nspin, 0, fn.str(), 0, &ucell, precision, 0);
}
}
void ldos_mode_pw(const elecstate::ElecStatePW<std::complex<double>>* pelec,
const psi::Psi<std::complex<double>>& psi,
const Parallel_Grid& pgrid,
const UnitCell& ucell)
{
double emax = 0.0;
double emin = 0.0;
prepare_dos(GlobalV::ofs_running,
pelec->eferm,
pelec->ekb,
pelec->klist->get_nks(),
PARAM.inp.nbands,
PARAM.inp.dos_edelta_ev,
PARAM.inp.dos_scale,
emax,
emin);
const int ndata = static_cast<int>((emax - emin) / PARAM.inp.dos_edelta_ev) + 1;
const double sigma = sqrt(2.0) * PARAM.inp.dos_sigma;
const double sigma2 = sigma * sigma;
const double sigma_PI = sqrt(ModuleBase::PI) * sigma;
std::vector<double> start = {PARAM.inp.ldos_line[0], PARAM.inp.ldos_line[1], PARAM.inp.ldos_line[2]};
std::vector<double> end = {PARAM.inp.ldos_line[3], PARAM.inp.ldos_line[4], PARAM.inp.ldos_line[5]};
const int npoints = PARAM.inp.ldos_line[6];
// calculate grid points
std::vector<std::vector<int>> points(npoints, std::vector<int>(3, 0));
std::vector<std::vector<double>> shifts(npoints, std::vector<double>(3, 0));
get_grid_points(start, end, npoints, pgrid.nx, pgrid.ny, pgrid.nz, points, shifts);
std::vector<std::vector<double>> ldos(npoints, std::vector<double>(ndata, 0));
// calculate ldos
std::vector<double> tmp(pelec->charge->nrxx);
std::vector<std::complex<double>> wfcr(pelec->basis->nrxx);
for (int ik = 0; ik < pelec->klist->get_nks(); ++ik)
{
psi.fix_k(ik);
const double efermi = pelec->eferm.get_efval(pelec->klist->isk[ik]);
const int nbands = psi.get_nbands();
for (int ib = 0; ib < nbands; ib++)
{
pelec->basis->recip_to_real<std::complex<double>,base_device::DEVICE_CPU>(&psi(ib, 0), wfcr.data(), ik);
const double weight = pelec->klist->wk[ik] / ucell.omega;
for (int ir = 0; ir < pelec->basis->nrxx; ir++)
{
tmp[ir] += weight * norm(wfcr[ir]);
}
std::vector<double> results(npoints, 0);
trilinear_interpolate(points, shifts, pgrid, tmp, results);
const double eigenval = pelec->ekb(ik, ib) * ModuleBase::Ry_to_eV;
for (int ie = 0; ie < ndata; ++ie)
{
const double en = emin + ie * PARAM.inp.dos_edelta_ev;
const double de = en - eigenval;
const double de2 = de * de;
const double gauss = exp(-de2 / sigma2) / sigma_PI;
for (int ip = 0; ip < npoints; ++ip)
{
ldos[ip][ie] += results[ip] * gauss;
}
}
}
}
std::ofstream ofs_ldos;
std::stringstream fn;
fn << PARAM.globalv.global_out_dir << "LDOS.txt";
if (GlobalV::MY_RANK == 0)
{
ofs_ldos.open(fn.str().c_str());
for (int ip = 0; ip < npoints; ++ip)
{
for (int ie = 0; ie < ndata; ++ie)
{
ofs_ldos << ldos[ip][ie] << " ";
}
ofs_ldos << std::endl;
}
ofs_ldos.close();
}
}
void get_grid_points(const std::vector<double>& start,
const std::vector<double>& end,
const int& npoints,
const int& nx,
const int& ny,
const int& nz,
std::vector<std::vector<int>>& points,
std::vector<std::vector<double>>& shifts)
{
std::vector<int> ndim = {nx, ny, nz};
auto grid_points = [](const std::vector<double>& coor,
const std::vector<int>& ndim,
std::vector<int>& points,
std::vector<double>& shift) {
for (int i = 0; i < 3; i++)
{
shift[i] = coor[i] * ndim[i];
while (shift[i] >= ndim[i])
{
shift[i] -= ndim[i];
}
while (shift[i] < 0)
{
shift[i] += ndim[i];
}
points[i] = static_cast<int>(shift[i]);
shift[i] -= points[i];
}
};
if (npoints == 1)
{
grid_points(start, ndim, points[0], shifts[0]);
}
else
{
std::vector<double> delta = {end[0] - start[0], end[1] - start[1], end[2] - start[2]};
for (int i = 0; i < npoints; i++)
{
const double ratio = static_cast<double>(i) / (npoints - 1);
std::vector<double> current = {0, 0, 0};
for (int j = 0; j < 3; j++)
{
current[j] = start[j] + ratio * delta[j];
}
grid_points(current, ndim, points[i], shifts[i]);
}
}
}
void trilinear_interpolate(const std::vector<std::vector<int>>& points,
const std::vector<std::vector<double>>& shifts,
const Parallel_Grid& pgrid,
const std::vector<double>& data,
std::vector<double>& results)
{
const int nx = pgrid.nx;
const int ny = pgrid.ny;
const int nz = pgrid.nz;
const int nyz = ny * nz;
const int nxyz = nx * ny * nz;
// reduce
std::vector<double> data_full(nxyz);
#ifdef __MPI
if (GlobalV::MY_POOL == 0 && GlobalV::MY_BNDGROUP == 0)
{
pgrid.reduce(data_full.data(), data.data(), false);
}
MPI_Barrier(MPI_COMM_WORLD);
#else
std::memcpy(data_full.data(), data.data(), nxyz * sizeof(double));
#endif
auto grid_points = [&data_full, &nyz, &nz](const int& ix, const int& iy, const int& iz) {
return data_full[ix * nyz + iy * nz + iz];
};
// trilinear interpolation
const int npoints = points.size();
results.resize(npoints, 0.0);
if (GlobalV::MY_RANK == 0)
{
for (int l = 0; l < npoints; ++l)
{
for (int i = 0; i < 2; ++i)
{
double weight = (i * shifts[l][0] + (1 - i) * (1 - shifts[l][0]));
for (int j = 0; j < 2; ++j)
{
weight *= (j * shifts[l][1] + (1 - j) * (1 - shifts[l][1]));
for (int k = 0; k < 2; ++k)
{
weight *= (k * shifts[l][2] + (1 - k) * (1 - shifts[l][2]));
const int ix = points[l][0] + i;
const int iy = points[l][1] + j;
const int iz = points[l][2] + k;
results[l] += weight * grid_points(ix, iy, iz);
}
}
}
}
}
#ifdef __MPI
MPI_Bcast(results.data(), npoints, MPI_DOUBLE, 0, MPI_COMM_WORLD);
#endif
}
} // namespace ModuleIO