Skip to content

Commit bf4ce74

Browse files
19helloFei Yang
andauthored
change BFGS name and make lattice_change_cg and ions_move_cg shorter (#6517)
* change BFGS name and make lattice_change_cg and ions_move_cg shorter * change input parameters * change BFGS name and make lattice_change_cg and ions_move_cg shorter * change input parameters * change input parameters * change input parameters * change input parameters * change input parameters * fix INPUT problem * fix INPUT problem * fix INPUT problem * fix INPUT problem * fix INPUT problem * fix INPUT problem * fix INPUT problem * fix INPUT problem * fix INPUT problem --------- Co-authored-by: Fei Yang <[email protected]>
1 parent 2f10362 commit bf4ce74

File tree

20 files changed

+136
-162
lines changed

20 files changed

+136
-162
lines changed

docs/advanced/input_files/input-main.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,15 +1487,20 @@ These variables are used to control the geometry relaxation.
14871487

14881488
### relax_method
14891489

1490-
- **Type**: String
1490+
- **Type**: Vector of string
14911491
- **Description**: The methods to do geometry optimization.
1492+
the first element:
14921493
- cg: using the conjugate gradient (CG) algorithm. Note that there are two implementations of the conjugate gradient (CG) method, see [relax_new](#relax_new).
1493-
- bfgs: using the Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm.
1494-
- bfgs_trad: using the traditional Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm.
1494+
- bfgs : using the Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm.
1495+
- lbfgs: using the Limited-memory Broyden–Fletcher–Goldfarb–Shanno (LBFGS) algorithm.
14951496
- cg_bfgs: using the CG method for the initial steps, and switching to BFGS method when the force convergence is smaller than [relax_cg_thr](#relax_cg_thr).
14961497
- sd: using the steepest descent (SD) algorithm.
14971498
- fire: the Fast Inertial Relaxation Engine method (FIRE), a kind of molecular-dynamics-based relaxation algorithm, is implemented in the molecular dynamics (MD) module. The algorithm can be used by setting [calculation](#calculation) to `md` and [md_type](#md_type) to `fire`. Also ionic velocities should be set in this case. See [fire](../md.md#fire) for more details.
1498-
- **Default**: cg
1499+
1500+
the second element:
1501+
when the first element is bfgs, if the second parameter is 1, it indicates the use of the new BFGS algorithm; if the second parameter is not 1, it indicates the use of the old BFGS algorithm.
1502+
- **Default**: cg 1
1503+
- **Note**:In the 3.10-LTS version, the type of this parameter is std::string. It can be set to "cg","bfgs","cg_bfgs","bfgs_trad","lbfgs","sd","fire".
14991504

15001505
### relax_new
15011506

source/source_io/module_parameter/input_parameter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string>
77
#include <vector>
88

9+
910
// It stores all input parameters both defined in INPUT file and not defined in
1011
// INPUT file
1112
struct Input_para
@@ -150,7 +151,7 @@ struct Input_para
150151
// int bessel_nao_lmax; ///< lmax used in descriptor
151152

152153
// ============== #Parameters (4.Relaxation) ===========================
153-
std::string relax_method = "cg"; ///< methods to move_ion: sd, bfgs, cg...
154+
std::vector<std::string> relax_method = {"cg","1"}; ///< methods to move_ion: sd, bfgs, cg...
154155
bool relax_new = true;
155156
bool relax = false; ///< allow relaxation along the specific direction
156157
double relax_scale_force = 0.5;

source/source_io/read_input_item_relax.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,53 @@
55

66
namespace ModuleIO
77
{
8+
9+
810
void ReadInput::item_relax()
911
{
1012
{
1113
Input_Item item("relax_method");
1214
item.annotation = "cg; bfgs; sd; cg; cg_bfgs;";
13-
read_sync_string(input.relax_method);
14-
item.check_value = [](const Input_Item& item, const Parameter& para) {
15-
const std::vector<std::string> relax_methods = {"cg", "bfgs", "sd", "cg_bfgs","bfgs_trad","lbfgs"};
16-
if (std::find(relax_methods.begin(),relax_methods.end(), para.input.relax_method)==relax_methods.end())
17-
{
18-
const std::string warningstr = nofound_str(relax_methods, "relax_method");
19-
ModuleBase::WARNING_QUIT("ReadInput", warningstr);
20-
}
15+
item.read_value = [](const Input_Item& item, Parameter& para) {
16+
if(item.get_size()==1)
17+
{
18+
para.input.relax_method[0] = item.str_values[0];
19+
para.input.relax_method[1] = "1";
20+
}
21+
else if(item.get_size()>=2)
22+
{
23+
para.input.relax_method[0] = item.str_values[0];
24+
para.input.relax_method[1] = item.str_values[1];
25+
}
26+
};
27+
item.check_value = [](const Input_Item& item, const Parameter& para) {
28+
const std::vector<std::string> relax_methods = {"cg", "sd", "cg_bfgs","lbfgs","bfgs"};
29+
if (std::find(relax_methods.begin(), relax_methods.end(), para.input.relax_method[0]) == relax_methods.end()) {
30+
const std::string warningstr = nofound_str(relax_methods, "relax_method");
31+
ModuleBase::WARNING_QUIT("ReadInput", warningstr);
32+
}
2133
};
2234
this->add_item(item);
35+
36+
// Input_Item item("relax_method");
37+
// item.annotation = "cg; bfgs; sd; cg; cg_bfgs;";
38+
// read_sync_string(input.relax_method);
39+
// item.check_value = [](const Input_Item& item, const Parameter& para) {
40+
// const std::vector<std::string> relax_methods = {"cg", "bfgs_old", "sd", "cg_bfgs","bfgs","lbfgs"};
41+
// if (std::find(relax_methods.begin(),relax_methods.end(), para.input.relax_method)==relax_methods.end())
42+
// {
43+
// const std::string warningstr = nofound_str(relax_methods, "relax_method");
44+
// ModuleBase::WARNING_QUIT("ReadInput", warningstr);
45+
// }
46+
// };
47+
// this->add_item(item);
2348
}
2449
{
2550
Input_Item item("relax_new");
2651
item.annotation = "whether to use the new relaxation method";
2752
read_sync_bool(input.relax_new);
2853
item.reset_value = [](const Input_Item& item, Parameter& para) {
29-
if (para.input.relax_new && para.input.relax_method != "cg")
54+
if (para.input.relax_new && para.input.relax_method[0] != "cg")
3055
{
3156
para.input.relax_new = false;
3257
}

source/source_io/test/read_input_ptest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ TEST_F(InputParaTest, ParaRead)
114114
EXPECT_EQ(param.inp.fixed_axes, "None");
115115
EXPECT_FALSE(param.inp.fixed_ibrav);
116116
EXPECT_FALSE(param.inp.fixed_atoms);
117-
EXPECT_EQ(param.inp.relax_method, "cg");
117+
EXPECT_EQ(param.inp.relax_method[0], "cg");
118118
EXPECT_DOUBLE_EQ(param.inp.relax_cg_thr, 0.5);
119119
EXPECT_EQ(param.inp.out_level, "ie");
120120
EXPECT_TRUE(param.globalv.out_md_control);

source/source_io/test_serial/read_input_item_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ TEST_F(InputTest, Item_test)
766766
}
767767
{ // relax_method
768768
auto it = find_label("relax_method", readinput.input_lists);
769-
param.input.relax_method = "none";
769+
param.input.relax_method[0] = "none";
770770
testing::internal::CaptureStdout();
771771
EXPECT_EXIT(it->second.check_value(it->second, param), ::testing::ExitedWithCode(1), "");
772772
output = testing::internal::GetCapturedStdout();
@@ -775,12 +775,12 @@ TEST_F(InputTest, Item_test)
775775
{ //relax_new
776776
auto it = find_label("relax_new", readinput.input_lists);
777777
param.input.relax_new = true;
778-
param.input.relax_method = "cg";
778+
param.input.relax_method[0] = "cg";
779779
it->second.reset_value(it->second, param);
780780
EXPECT_EQ(param.input.relax_new, true);
781781

782782
param.input.relax_new = true;
783-
param.input.relax_method = "none";
783+
param.input.relax_method[0] = "none";
784784
it->second.reset_value(it->second, param);
785785
EXPECT_EQ(param.input.relax_new, false);
786786
}

source/source_relax/ions_move_basic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ double Ions_Move_Basic::relax_bfgs_init = -1.0; // default is 0.5
2323
double Ions_Move_Basic::best_xxx = 1.0;
2424

2525
int Ions_Move_Basic::out_stru = 0;
26-
std::string Ions_Move_Basic::relax_method = "bfgs";
26+
std::vector<std::string> Ions_Move_Basic::relax_method = {"bfgs","2"};
2727

2828
void Ions_Move_Basic::setup_gradient(const UnitCell &ucell, const ModuleBase::matrix &force, double *pos, double *grad)
2929
{

source/source_relax/ions_move_basic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern double relax_bfgs_rmax; // max value of trust radius,
2121
extern double relax_bfgs_rmin; // min value of trust radius,
2222
extern double relax_bfgs_init; // initial value of trust radius,
2323
extern double best_xxx; // the last step length of cg , we use it as bfgs`s initial step length
24-
extern std::string relax_method; // relaxation method,
24+
extern std::vector<std::string> relax_method; // relaxation method,
2525
extern int out_stru; // output the structure or not
2626
// funny way to pass this parameter, but nevertheless
2727

source/source_relax/ions_move_cg.cpp

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "ions_move_cg.h"
2-
32
#include "ions_move_basic.h"
43
#include "source_base/global_function.h"
54
#include "source_base/global_variable.h"
@@ -77,20 +76,16 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
7776
// ncggrad is a parameter to control the cg method , every ten cg directions,
7877
// we change the direction back to the steepest descent method
7978
static int ncggrad = 0;
80-
8179
static double fa = 0.0;
8280
static double fb = 0.0;
8381
static double fc = 0.0;
84-
8582
static double xa = 0.0;
8683
static double xb = 0.0;
8784
static double xc = 0.0;
8885
static double xpt = 0.0;
8986
static double steplength = 0.0;
9087
static double fmax = 0.0;
91-
9288
static int nbrent = 0;
93-
9489

9590
// some arrays
9691
double *pos = new double[dim];
@@ -100,7 +95,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
10095
double *cg_grad = new double[dim];
10196
double best_x = 0.0;
10297
double fmin = 0.0;
103-
10498
int flag = 0;
10599

106100
ModuleBase::GlobalFunc::ZEROS(pos, dim);
@@ -137,7 +131,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
137131
{
138132
Ions_Move_Basic::check_converged(ucell, grad);
139133
}
140-
141134
if (Ions_Move_Basic::converged)
142135
{
143136
Ions_Move_Basic::terminate(ucell);
@@ -155,21 +148,7 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
155148
flag); // we use the last direction ,the last grad and the grad now to get the direction now
156149
ncggrad++;
157150

158-
double norm = 0.0;
159-
for (int i = 0; i < dim; ++i)
160-
{
161-
norm += pow(cg_grad[i], 2);
162-
}
163-
norm = sqrt(norm);
164-
165-
if (norm != 0.0)
166-
{
167-
for (int i = 0; i < dim; ++i)
168-
{
169-
cg_gradn[i] = cg_grad[i] / norm;
170-
}
171-
}
172-
151+
normalize(cg_gradn, cg_grad, dim);
173152
setup_move(move0, cg_gradn, steplength); // move the atom position
174153
Ions_Move_Basic::move_atoms(ucell, move0, pos);
175154

@@ -181,16 +160,16 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
181160

182161
f_cal(move0, move0, dim, xb); // xb = trial steplength
183162
f_cal(move0, grad, dim, fa); // fa is the projection force in this direction
184-
185163
fmax = fa;
186164
sd = false;
187165

188-
if (Ions_Move_Basic::relax_method == "cg_bfgs")
166+
if (Ions_Move_Basic::relax_method[0] == "cg_bfgs")
189167
{
190168
if (Ions_Move_Basic::largest_grad * ModuleBase::Ry_to_eV / 0.529177
191169
< RELAX_CG_THR) // cg to bfgs by pengfei 13-8-8
192170
{
193-
Ions_Move_Basic::relax_method = "bfgs";
171+
Ions_Move_Basic::relax_method[0] = "bfgs";
172+
Ions_Move_Basic::relax_method[1] = "2";
194173
}
195174
Ions_Move_Basic::best_xxx = steplength;
196175
}
@@ -204,7 +183,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
204183
double e1 = etot_in;
205184
f_cal(move0, grad, dim, fb);
206185
f_cal(move0, move0, dim, xb);
207-
208186
if ((std::abs(fb) < std::abs((fa) / 10.0)))
209187
{
210188
sd = true;
@@ -214,21 +192,7 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
214192
goto CG_begin;
215193
}
216194

217-
double norm = 0.0;
218-
for (int i = 0; i < dim; ++i)
219-
{
220-
norm += pow(cg_grad0[i], 2);
221-
}
222-
norm = sqrt(norm);
223-
224-
if (norm != 0.0)
225-
{
226-
for (int i = 0; i < dim; ++i)
227-
{
228-
cg_gradn[i] = cg_grad0[i] / norm;
229-
}
230-
}
231-
195+
normalize(cg_gradn, cg_grad0, dim);
232196
third_order(e0, e1, fa, fb, xb, best_x); // cubic interpolation
233197

234198
if (best_x > 6 * xb || best_x < (-xb))
@@ -238,7 +202,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
238202

239203
setup_move(move, cg_gradn, best_x);
240204
Ions_Move_Basic::move_atoms(ucell, move, pos);
241-
242205
trial = false;
243206
xa = 0;
244207
f_cal(move0, move, dim, xc);
@@ -250,7 +213,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
250213
{
251214
double xtemp, ftemp;
252215
f_cal(move0, grad, dim, fc);
253-
254216
fmin = std::abs(fc);
255217
nbrent++;
256218

@@ -275,24 +237,9 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
275237
goto CG_begin;
276238
}
277239

278-
double norm = 0.0;
279-
for (int i = 0; i < dim; ++i)
280-
{
281-
norm += pow(cg_grad0[i], 2);
282-
}
283-
norm = sqrt(norm);
284-
285-
if (norm != 0.0)
286-
{
287-
for (int i = 0; i < dim; ++i)
288-
{
289-
cg_gradn[i] = cg_grad0[i] / norm;
290-
}
291-
}
292-
240+
normalize(cg_gradn, cg_grad0, dim);
293241
setup_move(move, cg_gradn, best_x);
294242
Ions_Move_Basic::move_atoms(ucell, move, pos);
295-
296243
Ions_Move_Basic::relax_bfgs_init = xc;
297244
}
298245
}
@@ -342,7 +289,6 @@ void Ions_Move_CG::setup_cg_grad(double *grad,
342289
cgp_gp += cg_grad0[i] * grad0[i];
343290
cgp_g += cg_grad0[i] * grad[i];
344291
}
345-
346292
assert(g_gp != 0.0);
347293
const double gamma1 = gg / gp_gp; // FR
348294
// const double gamma2 = -(gg - g_gp)/(cgp_g - cgp_gp); //CW
@@ -508,3 +454,22 @@ void Ions_Move_CG::setup_move(double *move, double *cg_gradn, const double &trus
508454
}
509455
return;
510456
}
457+
458+
void Ions_Move_CG::normalize(double *cg_gradn, const double *cg_grad, int dim)
459+
{
460+
double norm = 0.0;
461+
for (int i = 0; i < dim; ++i)
462+
{
463+
norm += pow(cg_grad[i], 2);
464+
}
465+
norm = sqrt(norm);
466+
467+
if (norm != 0.0)
468+
{
469+
for (int i = 0; i < dim; ++i)
470+
{
471+
cg_gradn[i] = cg_grad[i] / norm;
472+
}
473+
}
474+
return;
475+
}

source/source_relax/ions_move_cg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Ions_Move_CG
3838
const double &fb,
3939
const double x,
4040
double &best_x);
41+
void normalize(double *cg_gradn, const double *cg_grad, int dim);
4142
};
4243

4344
#endif

0 commit comments

Comments
 (0)