@@ -12,7 +12,6 @@ void BFGS::allocate(const int _size)
12
12
maxstep=PARAM.inp .relax_bfgs_rmax ;
13
13
size=_size;
14
14
sign =true ;
15
-
16
15
H = std::vector<std::vector<double >>(3 *size, std::vector<double >(3 *size, 0.0 ));
17
16
18
17
for (int i = 0 ; i < 3 *size; ++i)
@@ -32,7 +31,6 @@ void BFGS::allocate(const int _size)
32
31
33
32
34
33
void BFGS::relax_step (const ModuleBase::matrix& _force,UnitCell& ucell)
35
-
36
34
{
37
35
GetPos (ucell,pos);
38
36
GetPostaud (ucell,pos_taud);
@@ -44,7 +42,6 @@ void BFGS::relax_step(const ModuleBase::matrix& _force,UnitCell& ucell)
44
42
force[i][j]=_force (i,j)*ModuleBase::Ry_to_eV/ModuleBase::BOHR_TO_A;
45
43
}
46
44
}
47
-
48
45
int k=0 ;
49
46
for (int i=0 ;i<ucell.ntype ;i++)
50
47
{
@@ -68,35 +65,6 @@ void BFGS::relax_step(const ModuleBase::matrix& _force,UnitCell& ucell)
68
65
69
66
this ->PrepareStep (force,pos,H,pos0,force0,steplength,dpos,ucell);
70
67
this ->DetermineStep (steplength,dpos,maxstep);
71
-
72
- /* std::cout<<"force"<<std::endl;
73
- for(int i=0;i<size;i++)
74
- {
75
- for(int j=0;j<3;j++)
76
- {
77
- std::cout<<force[i][j]<<' ';
78
- }
79
- std::cout<<std::endl;
80
- }
81
- std::cout<<"dpos"<<std::endl;
82
- for(int i=0;i<size;i++)
83
- {
84
- for(int j=0;j<3;j++)
85
- {
86
- std::cout<<dpos[i][j]<<' ';
87
- }
88
- std::cout<<std::endl;
89
- }
90
- std::cout<<"pos"<<std::endl;
91
- for(int i=0;i<size;i++)
92
- {
93
- for(int j=0;j<3;j++)
94
- {
95
- std::cout<<pos[i][j]<<' ';
96
- }
97
- std::cout<<std::endl;
98
- }*/
99
-
100
68
this ->UpdatePos (ucell);
101
69
this ->CalculateLargestGrad (_force,ucell);
102
70
this ->IsRestrain (dpos);
@@ -142,8 +110,8 @@ void BFGS::PrepareStep(std::vector<std::vector<double>>& force,
142
110
std::vector<std::vector<double >>& dpos,
143
111
UnitCell& ucell)
144
112
{
145
- std::vector<double > changedforce = this -> ReshapeMToV (force);
146
- std::vector<double > changedpos = this -> ReshapeMToV (pos);
113
+ std::vector<double > changedforce = ReshapeMToV (force);
114
+ std::vector<double > changedpos = ReshapeMToV (pos);
147
115
this ->Update (changedpos, changedforce,H,ucell);
148
116
149
117
// ! call dysev
@@ -169,22 +137,13 @@ void BFGS::PrepareStep(std::vector<std::vector<double>>& force,
169
137
V[j][i] = H_flat[3 *size*i + j];
170
138
}
171
139
}
172
- std::vector<double > a=this -> DotInMAndV2 (V, changedforce);
140
+ std::vector<double > a=DotInMAndV2 (V, changedforce);
173
141
for (int i = 0 ; i < a.size (); i++)
174
142
{
175
143
a[i]/=std::abs (omega[i]);
176
144
}
177
- std::vector<double > tmpdpos = this ->DotInMAndV1 (V, a);
178
- dpos = this ->ReshapeVToM (tmpdpos);
179
- /* std::cout<<"dpos0"<<std::endl;
180
- for(int i=0;i<size;i++)
181
- {
182
- for(int j=0;j<3;j++)
183
- {
184
- std::cout<<dpos[i][j]<<' ';
185
- }
186
- std::cout<<std::endl;
187
- }*/
145
+ std::vector<double > tmpdpos = DotInMAndV1 (V, a);
146
+ dpos = ReshapeVToM (tmpdpos);
188
147
for (int i = 0 ; i < size; i++)
189
148
{
190
149
double k = 0 ;
@@ -194,9 +153,9 @@ void BFGS::PrepareStep(std::vector<std::vector<double>>& force,
194
153
}
195
154
steplength[i] = sqrt (k);
196
155
}
197
- pos0 = this -> ReshapeMToV (pos);
198
- pos_taud0=this -> ReshapeMToV (pos_taud);
199
- force0 = this -> ReshapeMToV (force);
156
+ pos0 = ReshapeMToV (pos);
157
+ pos_taud0=ReshapeMToV (pos_taud);
158
+ force0 = ReshapeMToV (force);
200
159
}
201
160
202
161
void BFGS::Update (std::vector<double >& pos,
@@ -210,8 +169,8 @@ void BFGS::Update(std::vector<double>& pos,
210
169
return ;
211
170
}
212
171
// std::vector<double> dpos=this->VSubV(pos,pos0);
213
- auto term=this -> ReshapeMToV (pos_taud);
214
- std::vector<double > dpos = this -> VSubV (term, pos_taud0);
172
+ std::vector< double > term=ReshapeMToV (pos_taud);
173
+ std::vector<double > dpos = VSubV (term, pos_taud0);
215
174
for (int i=0 ;i<3 *size;i++)
216
175
{
217
176
double shortest_move = dpos[i];
@@ -258,56 +217,28 @@ void BFGS::Update(std::vector<double>& pos,
258
217
dpos[iat * 3 + 2 ] = move_ion_dr.z ;
259
218
}
260
219
}
261
- /* std::cout<<"Printpos"<<std::endl;
262
- for(int i=0;i<3*size;i++)
263
- {
264
- std::cout<<pos[i]<<' ';
265
- }
266
- std::cout<<std::endl;
267
- std::cout<<"Printpos0"<<std::endl;
268
- for(int i=0;i<3*size;i++)
269
- {
270
- std::cout<<pos0[i]<<' ';
271
- }
272
- std::cout<<std::endl;*/
273
- /* std::cout<<"PrintDpos"<<std::endl;
274
- for(int i=0;i<3*size;i++)
275
- {
276
- std::cout<<dpos[i]<<' ';
277
- }
278
- std::cout<<std::endl;*/
279
220
if (*max_element (dpos.begin (), dpos.end ()) < 1e-7 )
280
221
{
281
222
return ;
282
- }
283
-
284
- std::vector<double > dforce = this ->VSubV (force, force0);
285
- double a = this ->DotInVAndV (dpos, dforce);
286
- std::vector<double > dg = this ->DotInMAndV1 (H, dpos);
287
- double b = this ->DotInVAndV (dpos, dg);
288
-
289
- /* std::cout<<"a"<<std::endl;
290
- std::cout<<a<<std::endl;
291
- std::cout<<"b"<<std::endl;
292
- std::cout<<b<<std::endl;*/
293
- auto term1=this ->OuterVAndV (dforce, dforce);
294
- auto term2=this ->OuterVAndV (dg, dg);
295
- auto term3=this ->MPlus (term1, a);
296
- auto term4=this ->MPlus (term2, b);
297
- H = this ->MSubM (H, term3);
298
- H = this ->MSubM (H, term4);
223
+ }
224
+ std::vector<double > dforce = VSubV (force, force0);
225
+ double a = DotInVAndV (dpos, dforce);
226
+ std::vector<double > dg = DotInMAndV1 (H, dpos);
227
+ double b = DotInVAndV (dpos, dg);
228
+ std::vector<std::vector<double >> term1=OuterVAndV (dforce, dforce);
229
+ std::vector<std::vector<double >> term2=OuterVAndV (dg, dg);
230
+ std::vector<std::vector<double >> term3=MPlus (term1, a);
231
+ std::vector<std::vector<double >> term4=MPlus (term2, b);
232
+ H = MSubM (H, term3);
233
+ H = MSubM (H, term4);
299
234
}
300
235
301
236
void BFGS::DetermineStep (std::vector<double >& steplength,
302
237
std::vector<std::vector<double >>& dpos,
303
238
double & maxstep)
304
239
{
305
- auto maxsteplength = max_element (steplength.begin (), steplength.end ());
240
+ std::vector< double >::iterator maxsteplength = max_element (steplength.begin (), steplength.end ());
306
241
double a = *maxsteplength;
307
- /* std::cout<<"maxstep"<<std::endl;
308
- std::cout<<maxstep<<std::endl;
309
- std::cout<<"maxsteplength"<<std::endl;
310
- std::cout<<a<<std::endl;*/
311
242
if (a >= maxstep)
312
243
{
313
244
double scale = maxstep / a;
@@ -332,8 +263,6 @@ void BFGS::UpdatePos(UnitCell& ucell)
332
263
a[i*3 +j]/=ModuleBase::BOHR_TO_A;
333
264
}
334
265
}
335
- std::cout<<std::endl;
336
- int k=0 ;
337
266
unitcell::update_pos_tau (ucell.lat ,a,ucell.ntype ,ucell.nat ,ucell.atoms );
338
267
/* double move_ion[3*size];
339
268
ModuleBase::zeros(move_ion, size*3);
@@ -418,126 +347,3 @@ void BFGS::CalculateLargestGrad(const ModuleBase::matrix& _force,UnitCell& ucell
418
347
}
419
348
420
349
}
421
- // matrix methods
422
-
423
- std::vector<double > BFGS::ReshapeMToV (std::vector<std::vector<double >>& matrix)
424
- {
425
- int size = matrix.size ();
426
- std::vector<double > result;
427
- result.reserve (3 *size);
428
- for (const auto & row : matrix) {
429
- result.insert (result.end (), row.begin (), row.end ());
430
- }
431
- return result;
432
- }
433
-
434
- std::vector<std::vector<double >> BFGS::MAddM (std::vector<std::vector<double >>& a,
435
- std::vector<std::vector<double >>& b)
436
- {
437
- std::vector<std::vector<double >> result = std::vector<std::vector<double >>(a.size (), std::vector<double >(a[0 ].size (), 0.0 ));
438
- for (int i = 0 ; i < a.size (); i++)
439
- {
440
- for (int j = 0 ; j < a[0 ].size (); j++)
441
- {
442
- result[i][j] = a[i][j] + b[i][j];
443
- }
444
- }
445
- return result;
446
- }
447
-
448
- std::vector<double > BFGS::VSubV (std::vector<double >& a, std::vector<double >& b)
449
- {
450
- std::vector<double > result = std::vector<double >(a.size (), 0.0 );
451
- for (int i = 0 ; i < a.size (); i++)
452
- {
453
- result[i] = a[i] - b[i];
454
- }
455
- return result;
456
- }
457
-
458
- std::vector<std::vector<double >> BFGS::ReshapeVToM (std::vector<double >& matrix)
459
- {
460
- std::vector<std::vector<double >> result = std::vector<std::vector<double >>(matrix.size () / 3 , std::vector<double >(3 ));
461
- for (int i = 0 ; i < result.size (); i++)
462
- {
463
- for (int j = 0 ; j < 3 ; j++)
464
- {
465
- result[i][j] = matrix[i*3 + j];
466
- }
467
- }
468
- return result;
469
- }
470
-
471
- std::vector<double > BFGS::DotInMAndV1 (std::vector<std::vector<double >>& matrix, std::vector<double >& vec)
472
- {
473
- std::vector<double > result (matrix.size (), 0.0 );
474
- for (int i = 0 ; i < result.size (); i++)
475
- {
476
- for (int j = 0 ; j < vec.size (); j++)
477
- {
478
- result[i] += matrix[i][j] * vec[j];
479
- }
480
- }
481
- return result;
482
- }
483
- std::vector<double > BFGS::DotInMAndV2 (std::vector<std::vector<double >>& matrix, std::vector<double >& vec)
484
- {
485
- std::vector<double > result (matrix.size (), 0.0 );
486
- for (int i = 0 ; i < result.size (); i++)
487
- {
488
- for (int j = 0 ; j < vec.size (); j++)
489
- {
490
- result[i] += matrix[j][i] * vec[j];
491
- }
492
- }
493
- return result;
494
- }
495
-
496
- double BFGS::DotInVAndV (std::vector<double >& vec1, std::vector<double >& vec2)
497
- {
498
- double result = 0.0 ;
499
- for (int i = 0 ; i < vec1.size (); i++)
500
- {
501
- result += vec1[i] * vec2[i];
502
- }
503
- return result;
504
- }
505
-
506
- std::vector<std::vector<double >> BFGS::OuterVAndV (std::vector<double >& a, std::vector<double >& b)
507
- {
508
- std::vector<std::vector<double >> result = std::vector<std::vector<double >>(a.size (), std::vector<double >(b.size (), 0.0 ));
509
- for (int i = 0 ; i < a.size (); i++)
510
- {
511
- for (int j = 0 ; j < b.size (); j++)
512
- {
513
- result[i][j] = a[i] * b[j];
514
- }
515
- }
516
- return result;
517
- }
518
-
519
- std::vector<std::vector<double >> BFGS::MPlus (std::vector<std::vector<double >>& a, double & b)
520
- {
521
- std::vector<std::vector<double >> result = std::vector<std::vector<double >>(a.size (), std::vector<double >(a[0 ].size (), 0.0 ));
522
- for (int i = 0 ; i < a.size (); i++)
523
- {
524
- for (int j = 0 ; j < a[0 ].size (); j++)
525
- {
526
- result[i][j] = a[i][j] / b;
527
- }
528
- }
529
- return result;
530
- }
531
-
532
- std::vector<std::vector<double >> BFGS::MSubM (std::vector<std::vector<double >>& a, std::vector<std::vector<double >>& b)
533
- {
534
- std::vector<std::vector<double >> result = std::vector<std::vector<double >>(a.size (), std::vector<double >(a[0 ].size (), 0.0 ));
535
- for (int i = 0 ; i < a.size (); i++)
536
- {
537
- for (int j = 0 ; j < a[0 ].size (); j++)
538
- {
539
- result[i][j] = a[i][j] - b[i][j];
540
- }
541
- }
542
- return result;
543
- }
0 commit comments