Repository navigation
Expand file tree
/
Copy pathrecord_adj.cpp
More file actions
508 lines (462 loc) · 19 KB
/
Copy pathrecord_adj.cpp
File metadata and controls
508 lines (462 loc) · 19 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
#include "record_adj.h"
#include "source_base/timer.h"
#include "source_cell/module_neighbor/sltk_grid_driver.h"
#include "source_pw/module_pwdft/global.h"
#include "source_io/module_parameter/parameter.h"
Record_adj::Record_adj()
{
}
Record_adj::~Record_adj()
{
if (info_modified)
{
this->delete_grid();
}
}
void Record_adj::delete_grid()
{
for (int i = 0; i < na_proc; i++)
{
// how many 'numerical orbital' adjacents
// for each atom in this process.
for (int j = 0; j < na_each[i]; j++)
{
delete[] info[i][j];
}
delete[] info[i];
}
delete[] info;
delete[] na_each;
if (iat2ca)
{
delete[] iat2ca;
}
info_modified = false;
}
//--------------------------------------------
// This will record the orbitals according to
// HPSEPS's 2D block division.
// If multi-k, calculate nnr at the same time.
// be called only once in an ion-step.
//--------------------------------------------
void Record_adj::for_2d(const UnitCell& ucell,
const Grid_Driver& grid_d,
Parallel_Orbitals& pv,
bool gamma_only,
const std::vector<double>& orb_cutoff)
{
ModuleBase::TITLE("Record_adj", "for_2d");
ModuleBase::timer::tick("Record_adj", "for_2d");
assert(ucell.nat > 0);
if (!gamma_only)
{
// Record_adj should not modify members of pv, need refactor! mohan add 2025-03-10
delete[] pv.nlocdim;
delete[] pv.nlocstart;
pv.nlocdim = new int[ucell.nat];
pv.nlocstart = new int[ucell.nat];
ModuleBase::GlobalFunc::ZEROS(pv.nlocdim, ucell.nat);
ModuleBase::GlobalFunc::ZEROS(pv.nlocstart, ucell.nat);
pv.nnr = 0;
}
{
// (1) find the adjacent atoms of atom[T1,I1];
ModuleBase::Vector3<double> tau1, tau2, dtau;
ModuleBase::Vector3<double> dtau1, dtau2, tau0;
this->na_proc = ucell.nat;
// number of adjacents for each atom.
this->na_each = new int[na_proc];
ModuleBase::GlobalFunc::ZEROS(na_each, na_proc);
int iat = 0;
for (int T1 = 0; T1 < ucell.ntype; ++T1)
{
Atom* atom1 = &ucell.atoms[T1];
for (int I1 = 0; I1 < atom1->na; ++I1)
{
tau1 = atom1->tau[I1];
// grid_d.Find_atom( tau1 );
grid_d.Find_atom(ucell, tau1, T1, I1);
const int start1 = ucell.itiaiw2iwt(T1, I1, 0);
if (!gamma_only)
{
pv.nlocstart[iat] = pv.nnr;
}
// (2) search among all adjacent atoms.
for (int ad = 0; ad < grid_d.getAdjacentNum() + 1; ++ad)
{
const int T2 = grid_d.getType(ad);
const int I2 = grid_d.getNatom(ad);
const int start2 = ucell.itiaiw2iwt(T2, I2, 0);
tau2 = grid_d.getAdjacentTau(ad);
dtau = tau2 - tau1;
double distance = dtau.norm() * ucell.lat0;
double rcut = orb_cutoff[T1] + orb_cutoff[T2];
bool is_adj = false;
if (distance < rcut)
{
is_adj = true;
// there is another possibility that i and j are adjacent atoms.
// which is that <i|beta> are adjacents while <beta|j> are also
// adjacents, these considerations are only considered in k-point
// algorithm,
}
else if (distance >= rcut)
{
for (int ad0 = 0; ad0 < grid_d.getAdjacentNum() + 1; ++ad0)
{
const int T0 = grid_d.getType(ad0);
// const int I0 = grid_d.getNatom(ad0);
// const int iat0 = ucell.itia2iat(T0, I0);
// const int start0 = ucell.itiaiw2iwt(T0, I0, 0);
tau0 = grid_d.getAdjacentTau(ad0);
dtau1 = tau0 - tau1;
double distance1 = dtau1.norm() * ucell.lat0;
double rcut1 = orb_cutoff[T1] + ucell.infoNL.Beta[T0].get_rcut_max();
dtau2 = tau0 - tau2;
double distance2 = dtau2.norm() * ucell.lat0;
double rcut2 = orb_cutoff[T2] + ucell.infoNL.Beta[T0].get_rcut_max();
if (distance1 < rcut1 && distance2 < rcut2)
{
is_adj = true;
break;
} // dis1, dis2
}
}
if (is_adj)
{
++na_each[iat];
if (!gamma_only)
{
for (int ii = 0; ii < atom1->nw * PARAM.globalv.npol; ++ii)
{
// the index of orbitals in this processor
const int iw1_all = start1 + ii;
const int mu = pv.global2local_row(iw1_all);
if (mu < 0)
{
continue;
}
for (int jj = 0; jj < ucell.atoms[T2].nw * PARAM.globalv.npol; ++jj)
{
const int iw2_all = start2 + jj;
const int nu = pv.global2local_col(iw2_all);
if (nu < 0)
{
continue;
}
pv.nlocdim[iat]++;
++(pv.nnr);
}
}
}
} // end is_adj
} // end ad
++iat;
} // end I1
} // end T1
}
// xiaohui add "OUT_LEVEL", 2015-09-16
if (PARAM.inp.out_level != "m" && !gamma_only)
{
ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "ParaV.nnr", pv.nnr);
}
//------------------------------------------------
// info will identify each atom in each unitcell.
//------------------------------------------------
this->info = new int**[na_proc];
#ifdef _OPENMP
#pragma omp parallel
{
#endif
ModuleBase::Vector3<double> tau1, tau2, dtau;
ModuleBase::Vector3<double> dtau1, dtau2, tau0;
#ifdef _OPENMP
#pragma omp for schedule(dynamic)
#endif
for (int i = 0; i < na_proc; i++)
{
// GlobalV::ofs_running << " atom" << std::setw(5) << i << std::setw(10) << na_each[i] << std::endl;
if (na_each[i] > 0)
{
info[i] = new int*[na_each[i]];
for (int j = 0; j < na_each[i]; j++)
{
// (Rx, Ry, Rz, T, I)
info[i][j] = new int[5];
ModuleBase::GlobalFunc::ZEROS(info[i][j], 5);
}
}
}
#ifdef _OPENMP
#pragma omp for schedule(dynamic)
#endif
for (int iat = 0; iat < ucell.nat; ++iat)
{
const int T1 = ucell.iat2it[iat];
Atom* atom1 = &ucell.atoms[T1];
const int I1 = ucell.iat2ia[iat];
{
tau1 = atom1->tau[I1];
// grid_d.Find_atom( tau1 );
AdjacentAtomInfo adjs;
grid_d.Find_atom(ucell, tau1, T1, I1, &adjs);
// (2) search among all adjacent atoms.
int cb = 0;
for (int ad = 0; ad < adjs.adj_num + 1; ++ad)
{
const int T2 = adjs.ntype[ad];
const int I2 = adjs.natom[ad];
tau2 = adjs.adjacent_tau[ad];
dtau = tau2 - tau1;
double distance = dtau.norm() * ucell.lat0;
double rcut = orb_cutoff[T1] + orb_cutoff[T2];
bool is_adj = false;
if (distance < rcut)
{
is_adj = true;
}
else if (distance >= rcut)
{
for (int ad0 = 0; ad0 < adjs.adj_num + 1; ++ad0)
{
const int T0 = adjs.ntype[ad0];
// const int I0 = grid_d.getNatom(ad0);
// const int iat0 = ucell.itia2iat(T0, I0);
// const int start0 = ucell.itiaiw2iwt(T0, I0, 0);
tau0 = adjs.adjacent_tau[ad0];
dtau1 = tau0 - tau1;
double distance1 = dtau1.norm() * ucell.lat0;
double rcut1 = orb_cutoff[T1] + ucell.infoNL.Beta[T0].get_rcut_max();
dtau2 = tau0 - tau2;
double distance2 = dtau2.norm() * ucell.lat0;
double rcut2 = orb_cutoff[T2] + ucell.infoNL.Beta[T0].get_rcut_max();
if (distance1 < rcut1 && distance2 < rcut2)
{
is_adj = true;
break;
} // dis1, dis2
}
}
if (is_adj)
{
info[iat][cb][0] = adjs.box[ad].x;
info[iat][cb][1] = adjs.box[ad].y;
info[iat][cb][2] = adjs.box[ad].z;
info[iat][cb][3] = T2;
info[iat][cb][4] = I2;
++cb;
}
} // end ad
// GlobalV::ofs_running << " nadj = " << cb << std::endl;
} // end I1
} // end T1
#ifdef _OPENMP
}
#endif
ModuleBase::timer::tick("Record_adj", "for_2d");
info_modified = true;
return;
}
//--------------------------------------------
// This will record the orbitals according to
// grid division (cut along z direction)
//--------------------------------------------
void Record_adj::for_grid(const UnitCell& ucell,
const Grid_Driver& grid_d,
const Grid_Technique& gt,
const std::vector<double>& orb_cutoff)
{
ModuleBase::TITLE("Record_adj", "for_grid");
ModuleBase::timer::tick("Record_adj", "for_grid");
this->na_proc = 0;
this->iat2ca = new int[ucell.nat];
for (int iat = 0; iat < ucell.nat; ++iat)
{
{
if (gt.in_this_processor[iat])
{
iat2ca[iat] = na_proc;
++na_proc;
}
else
{
iat2ca[iat] = -1;
}
}
}
// number of adjacents for each atom.
this->na_each = new int[na_proc];
ModuleBase::GlobalFunc::ZEROS(na_each, na_proc);
this->info = new int**[na_proc];
#ifdef _OPENMP
#pragma omp parallel
{
#endif
ModuleBase::Vector3<double> tau1, tau2, dtau;
ModuleBase::Vector3<double> tau0, dtau1, dtau2;
#ifdef _OPENMP
#pragma omp for schedule(dynamic)
#endif
for (int iat = 0; iat < ucell.nat; ++iat)
{
const int T1 = ucell.iat2it[iat];
Atom* atom1 = &ucell.atoms[T1];
const int I1 = ucell.iat2ia[iat];
{
const int ca = iat2ca[iat];
// key in this function
if (gt.in_this_processor[iat])
{
tau1 = atom1->tau[I1];
// grid_d.Find_atom(tau1);
AdjacentAtomInfo adjs;
grid_d.Find_atom(ucell, tau1, T1, I1, &adjs);
for (int ad = 0; ad < adjs.adj_num + 1; ad++)
{
const int T2 = adjs.ntype[ad];
const int I2 = adjs.natom[ad];
const int iat2 = ucell.itia2iat(T2, I2);
if (gt.in_this_processor[iat2])
{
// Atom* atom2 = &ucell.atoms[T2];
tau2 = adjs.adjacent_tau[ad];
dtau = tau2 - tau1;
double distance = dtau.norm() * ucell.lat0;
double rcut = orb_cutoff[T1] + orb_cutoff[T2];
bool is_adj = false;
if (distance < rcut)
{
is_adj = true;
}
/*
else if(distance >= rcut)
{
for (int ad0 = 0; ad0 < grid_d.getAdjacentNum()+1; ++ad0)
{
const int T0 = grid_d.getType(ad0);
const int I0 = grid_d.getNatom(ad0);
const int iat0 = ucell.itia2iat(T0, I0);
const int start0 = ucell.itiaiw2iwt(T0, I0, 0);
tau0 = grid_d.getAdjacentTau(ad0);
dtau1 = tau0 - tau1;
dtau2 = tau0 - tau2;
double distance1 = dtau1.norm() * ucell.lat0;
double distance2 = dtau2.norm() * ucell.lat0;
double rcut1 = orb_cutoff[T1] + ucell.infoNL.Beta[T0].get_rcut_max();
double rcut2 = orb_cutoff[T2] + ucell.infoNL.Beta[T0].get_rcut_max();
if( distance1 < rcut1 && distance2 < rcut2 )
{
is_adj = true;
break;
} // dis1, dis2
}
}
*/
// check the distance
if (is_adj)
{
++na_each[ca];
}
} // end judge 2
} // end ad
} // end judge 1
} // end I1
} // end T1
#ifdef _OPENMP
#pragma omp for schedule(dynamic)
#endif
for (int i = 0; i < na_proc; i++)
{
assert(na_each[i] > 0);
info[i] = new int*[na_each[i]];
for (int j = 0; j < na_each[i]; j++)
{
// (Rx, Ry, Rz, T, I)
info[i][j] = new int[5];
ModuleBase::GlobalFunc::ZEROS(info[i][j], 5);
}
}
#ifdef _OPENMP
#pragma omp for schedule(dynamic)
#endif
for (int iat = 0; iat < ucell.nat; ++iat)
{
const int T1 = ucell.iat2it[iat];
Atom* atom1 = &ucell.atoms[T1];
const int I1 = ucell.iat2ia[iat];
{
const int ca = iat2ca[iat];
// key of this function
if (gt.in_this_processor[iat])
{
tau1 = atom1->tau[I1];
// grid_d.Find_atom(tau1);
AdjacentAtomInfo adjs;
grid_d.Find_atom(ucell, tau1, T1, I1, &adjs);
int cb = 0;
for (int ad = 0; ad < adjs.adj_num + 1; ad++)
{
const int T2 = adjs.ntype[ad];
const int I2 = adjs.natom[ad];
const int iat2 = ucell.itia2iat(T2, I2);
// key of this function
if (gt.in_this_processor[iat2])
{
// Atom* atom2 = &ucell.atoms[T2];
tau2 = adjs.adjacent_tau[ad];
dtau = tau2 - tau1;
double distance = dtau.norm() * ucell.lat0;
double rcut = orb_cutoff[T1] + orb_cutoff[T2];
// check the distance
if (distance < rcut)
{
info[ca][cb][0] = adjs.box[ad].x;
info[ca][cb][1] = adjs.box[ad].y;
info[ca][cb][2] = adjs.box[ad].z;
info[ca][cb][3] = T2;
info[ca][cb][4] = I2;
++cb;
}
/*
else if(distance >= rcut)
{
for (int ad0 = 0; ad0 < grid_d.getAdjacentNum()+1; ++ad0)
{
const int T0 = grid_d.getType(ad0);
const int I0 = grid_d.getNatom(ad0);
const int iat0 = ucell.itia2iat(T0, I0);
const int start0 = ucell.itiaiw2iwt(T0, I0, 0);
tau0 = grid_d.getAdjacentTau(ad0);
dtau1 = tau0 - tau1;
dtau2 = tau0 - tau2;
double distance1 = dtau1.norm() * ucell.lat0;
double distance2 = dtau2.norm() * ucell.lat0;
double rcut1 = orb_cutoff[T1] + ucell.infoNL.Beta[T0].get_rcut_max();
double rcut2 = orb_cutoff[T2] + ucell.infoNL.Beta[T0].get_rcut_max();
if( distance1 < rcut1 && distance2 < rcut2 )
{
info[ca][cb][0] = grid_d.getBox(ad).x;
info[ca][cb][1] = grid_d.getBox(ad).y;
info[ca][cb][2] = grid_d.getBox(ad).z;
info[ca][cb][3] = T2;
info[ca][cb][4] = I2;
++cb;
break;
} // dis1, dis2
}
}
*/
}
} // end ad
assert(cb == na_each[ca]);
}
}
}
#ifdef _OPENMP
}
#endif
ModuleBase::timer::tick("Record_adj", "for_grid");
info_modified = true;
// std::cout << " after for_grid" << std::endl;
return;
}