forked from abacusmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubgrid_oper.cpp
More file actions
376 lines (322 loc) · 8.99 KB
/
subgrid_oper.cpp
File metadata and controls
376 lines (322 loc) · 8.99 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
#include "subgrid_oper.h"
#include "../module_base/global_function.h"
#include "../module_base/global_variable.h"
#include "../src_parallel/parallel_reduce.h"
#include "../src_pw/global.h"
SubGrid_oper::SubGrid_oper()
{
trace_lo_tot = nullptr;
lgd=0;
allocate_totwfc = false;
}
SubGrid_oper::~SubGrid_oper()
{
delete[] trace_lo_tot;
}
//--------------------------------------------------
// because only the DIAG_WORLD processors
// have augmented wave functions,
// others will not have, we use this
// augmented wave functions to calculate
// the force, not related to the grid integration
// Only DIAG_WORLD use density matrix (2D) only.
//--------------------------------------------------
void SubGrid_oper::cal_totwfc()
{
ModuleBase::TITLE("SubGrid_oper","cal_totwfc");
//-----------------------------------------
// combine the wave functions index in the
// 'Grid group'
//-----------------------------------------
int *occupy = new int[GlobalV::NLOCAL];
ModuleBase::GlobalFunc::ZEROS(occupy, GlobalV::NLOCAL);
for(int iw=0; iw<GlobalV::NLOCAL; ++iw)
{
if(GlobalC::GridT.trace_lo[iw] >= 0)
{
occupy[iw] = 1;
}
}
// reduce occupy and get the full occupations.
#ifdef __MPI
// mohan 2012-02-23
Parallel_Reduce::reduce_int_grid(occupy, GlobalV::NLOCAL);
#endif
/*
for(int i=0; i<GlobalV::NLOCAL; ++i)
{
GlobalV::ofs_running << " i=" << i << " occupy=" << occupy[i] << std::endl;
}
*/
delete[] trace_lo_tot;
trace_lo_tot = new int[GlobalV::NLOCAL];
ModuleBase::GlobalFunc::ZEROS(trace_lo_tot, GlobalV::NLOCAL);
int count = 0;
for(int iw=0; iw<GlobalV::NLOCAL; ++iw)
{
// mohan fix bug 2012-02-23, shoud > 0,
// not >= 0.
if(occupy[iw]>0)
{
trace_lo_tot[iw]=count;
++count;
}
else
{
trace_lo_tot[iw]=-1;
}
}
//-----------------------------------------
// calculate the total lgd.
//-----------------------------------------
this->lgd = count;
//------------
// for test
//------------
/*
GlobalV::ofs_running << " trace_lo_tot" << std::endl;
for(int iw=0; iw<GlobalV::NLOCAL; ++iw)
{
GlobalV::ofs_running << " iw=" << iw << " trace_lo_tot=" << trace_lo_tot[iw]
<< " trace_lo=" << GlobalC::GridT.trace_lo[iw]
<< " occupy=" << occupy[iw]
<< std::endl;
}
*/
if(GlobalV::GRANK==0)
{
//xiaohui add 'GlobalV::OUT_LEVEL', 2015-09-16
if(GlobalV::OUT_LEVEL != "m") ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"GlobalC::SGO.lgd",lgd);
if(lgd==0)
{
// need to allocate the pointer,
// because it would be some array's
// entrance parameters.
// mohan update 2021-02-12
this->totwfc = new double**[1];
for(int is=0; is<1; ++is)
{
this->totwfc[is] = new double*[GlobalV::NBANDS];
}
this->allocate_totwfc=false;
return;
}
assert(this->allocate_totwfc==false);
static bool first_time=true;
if(first_time)
{
// GlobalC::LOWF.init_Cij(1);
first_time=false;
}
this->totwfc = new double**[1];
for(int is=0; is<1; ++is)
{
this->totwfc[is] = new double*[GlobalV::NBANDS];
for(int ib=0; ib<GlobalV::NBANDS; ++ib)
{
this->totwfc[is][ib] = new double[lgd];
ModuleBase::GlobalFunc::ZEROS(totwfc[is][ib], lgd);
// mohan update 2012-02-10
//if(GlobalV::DIAGO_TYPE!="cg") xiaohui modify 2013-09-02
if(GlobalV::KS_SOLVER!="cg") //xiaohui add 2013-09-02
{
for(int i=0; i<GlobalV::NLOCAL; ++i)
{
if(occupy[i])
{
if(ib==i)
{
totwfc[is][ib][trace_lo_tot[i]] = 1.0;
}
}
}
}
else //use cg method
{
// bug: dimension of WFC_GAMMA is smaller than totwfc
for(int i=0; i<lgd; ++i)
{
// mohan comment out 2021-02-09
// this->totwfc[is][ib][i] = GlobalC::LOWF.WFC_GAMMA[GlobalV::CURRENT_SPIN][ib][i]; //mohan update 2012-02-07
}
}
}
}
allocate_totwfc = true;
}
delete[] occupy; //mohan fix bug 2012-03-25
return;
}
void SubGrid_oper::dis_subwfc()
{
ModuleBase::TITLE("SubGrid_oper","dis_subwfc");
#ifdef __MPI
// std::cout << " distribute the wave functions " << std::endl;
//------------------------------------------
// bcast the eigenvalues
//------------------------------------------
for(int ik=0; ik<GlobalC::kv.nks; ++ik)
{
MPI_Bcast(GlobalC::wf.ekb[ik], GlobalV::NBANDS, MPI_DOUBLE, 0, GRID_WORLD);
}
MPI_Status status;
for(int i=0; i<GlobalV::GSIZE; ++i)
{
if(GlobalV::GRANK==0)
{
if(i==0)
{
//---------------------------------------------
// Transfer the data from totwfc to WFC_GAMMA.
//---------------------------------------------
for(int iw=0; iw<GlobalV::NLOCAL; ++iw)
{
const int mu1 = GlobalC::GridT.trace_lo[iw];
if(mu1 >= 0)
{
const int mu2 = this->trace_lo_tot[iw];
for(int ib=0; ib<GlobalV::NBANDS; ++ib)
{
// mohan comment out 2021-02-09
// GlobalC::LOWF.WFC_GAMMA[GlobalV::CURRENT_SPIN][ib][mu1] = this->totwfc[0][ib][mu2];
}//ib
}//mu1>=0
}//iw
}//i
else
{
int tag;
// receive trace_lo2
tag = i * 10;
int* trace_lo2 = new int[GlobalV::NLOCAL];
MPI_Recv(trace_lo2, GlobalV::NLOCAL, MPI_INT, i, tag, GRID_WORLD, &status);
/*
GlobalV::ofs_running << " Proc " << i << std::endl;
for(int i=0; i<GlobalV::NLOCAL; ++i)
{
GlobalV::ofs_running << std::setw(5) << i << std::setw(10) << trace_lo2[i] << std::endl;
}
*/
// receive lgd2
int lgd2 = 0;
tag = i * 10 + 1;
MPI_Recv(&lgd2, 1, MPI_INT, i, tag, GRID_WORLD, &status);
// GlobalV::ofs_running << " receive=" << tag << std::endl;
// send csend
double* csend = new double[GlobalV::NBANDS*lgd2];
ModuleBase::GlobalFunc::ZEROS(csend, GlobalV::NBANDS*lgd2);
for (int iw=0; iw<GlobalV::NLOCAL; iw++)
{
const int mu1 = trace_lo2[iw];
if (mu1>=0)
{
const int mu2 = this->trace_lo_tot[iw];
if(mu2<0)
{
GlobalV::ofs_running << " iw = " << iw << std::endl;
GlobalV::ofs_running << " GlobalC::GridT.trace_lo=" << mu1 << std::endl;
GlobalV::ofs_running << " trace_lo_tot=" << mu2 << std::endl;
assert(mu2>=0);
}
for (int ib=0; ib<GlobalV::NBANDS; ib++)
{
csend[mu1*GlobalV::NBANDS+ib] = this->totwfc[0][ib][mu2];
}
}
}
tag = i * 10 + 2;
// GlobalV::ofs_running << " send=" << tag << std::endl;
MPI_Send(csend,GlobalV::NBANDS*lgd2,MPI_DOUBLE,i,tag,GRID_WORLD);
// GlobalV::ofs_running << " send done." << std::endl;
delete[] csend;
delete[] trace_lo2;
}
}//GlobalV::GRANK=0
else if(i==GlobalV::GRANK)
{
int tag;
// send trace_lo
tag = GlobalV::GRANK * 10;
MPI_Send(GlobalC::GridT.trace_lo, GlobalV::NLOCAL, MPI_INT, 0, tag, GRID_WORLD);
//GlobalV::ofs_running << " send1." << std::endl;
// send GlobalC::GridT.lgd
tag = GlobalV::GRANK * 10 + 1;
MPI_Send(&GlobalC::GridT.lgd, 1, MPI_INT, 0, tag, GRID_WORLD);
//GlobalV::ofs_running << " send2." << std::endl;
// receive c
double* crecv = new double[GlobalV::NBANDS*GlobalC::GridT.lgd];
ModuleBase::GlobalFunc::ZEROS(crecv, GlobalV::NBANDS*GlobalC::GridT.lgd);
tag = GlobalV::GRANK * 10 + 2;
// GlobalV::ofs_running << " receive=" << tag << std::endl;
MPI_Recv(crecv, GlobalV::NBANDS*GlobalC::GridT.lgd, MPI_DOUBLE, 0, tag, GRID_WORLD, &status);
// GlobalV::ofs_running << " receive done." << std::endl;
for(int ib=0; ib<GlobalV::NBANDS; ++ib)
{
for (int mu=0; mu<GlobalC::GridT.lgd; ++mu)
{
// mohan comment out 2021-02-09
// GlobalC::LOWF.WFC_GAMMA[GlobalV::CURRENT_SPIN][ib][mu] = crecv[mu*GlobalV::NBANDS+ib];
}
}
delete[] crecv;
}
MPI_Barrier(GRID_WORLD);
}//end i
//-------------------
// Test
//-------------------
/*
GlobalV::ofs_running << " WFC " << " CURRENT_SPIN=" << GlobalV::CURRENT_SPIN << std::endl;
for(int i=0; i<GlobalV::NBANDS; ++i)
{
for(int j=0; j<GlobalV::NLOCAL; ++j)
{
const int mu = GlobalC::GridT.trace_lo[j];
if(mu>=0)
{
// if( abs(GlobalC::LOWF.WFC_GAMMA[0][i][mu] > 1.0e-8) )
GlobalV::ofs_running << std::setw(5) << i+1 << std::setw(8) << j+1
<< std::setw(15) << GlobalC::LOWF.WFC_GAMMA[GlobalV::CURRENT_SPIN][i][mu] << std::endl;
}
}
}
*/
#else
// this is for serial version
//---------------------------------------------
// Transfer the data from totwfc to WFC_GAMMA.
//---------------------------------------------
for(int iw=0; iw<GlobalV::NLOCAL; ++iw)
{
const int mu1 = GlobalC::GridT.trace_lo[iw];
if(mu1 >= 0)
{
const int mu2 = this->trace_lo_tot[iw];
for(int ib=0; ib<GlobalV::NBANDS; ++ib)
{
// mohan comment out 2021-02-09
// GlobalC::LOWF.WFC_GAMMA[GlobalV::CURRENT_SPIN][ib][mu1] = this->totwfc[0][ib][mu2];
}//ib
}//mu1>=0
}//iw
#endif //mohan fix bug 2012-02-04
if(GlobalV::GRANK==0)
{
// 1 spin because it can not be calculated two spin
// at the same time.
for(int is=0; is<1; ++is)
{
for(int ib=0; ib<GlobalV::NBANDS; ++ib)
{
if(allocate_totwfc)
{
delete[] this->totwfc[is][ib];
}
}
delete[] this->totwfc[is];
}
delete[] this->totwfc;
this->allocate_totwfc = false;
}
return;
}