From 2a79ecb7fa17117b47a67213f1cb3aa5a593a74f Mon Sep 17 00:00:00 2001 From: feiyang <2100011095@stu.pku.edu.cn> Date: Mon, 2 Dec 2024 07:39:10 +0000 Subject: [PATCH 1/5] update bfgs method --- docs/advanced/input_files/input-main.md | 1 + docs/advanced/opt.md | 4 ++++ source/module_relax/relax_old/bfgs.cpp | 6 +++--- source/module_relax/relax_old/bfgs.h | 4 ++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/advanced/input_files/input-main.md b/docs/advanced/input_files/input-main.md index ced6c56c54..81d077f2db 100644 --- a/docs/advanced/input_files/input-main.md +++ b/docs/advanced/input_files/input-main.md @@ -1369,6 +1369,7 @@ These variables are used to control the geometry relaxation. - **Description**: The methods to do geometry optimization. - cg: using the conjugate gradient (CG) algorithm. Note that there are two implementations of the conjugate gradient (CG) method, see [relax_new](#relax_new). - bfgs: using the Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm. + - bfgs_trad: using BFGS algorithm consistent with the BFGS algorithm in ASE. - 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). - sd: using the steepest descent (SD) algorithm. - 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. diff --git a/docs/advanced/opt.md b/docs/advanced/opt.md index e27800aa4d..d4f687f275 100644 --- a/docs/advanced/opt.md +++ b/docs/advanced/opt.md @@ -24,6 +24,10 @@ The [BFGS method](https://en.wikipedia.org/wiki/Broyden%E2%80%93Fletcher%E2%80%9 In ABACUS, we implemented the BFGS method for doing fixed-cell structural relaxation. +### BFGS_TRAD method + +The BFGS_TRAD method is an algorithm implemented in ABACUS, referencing the BFGS method from ASE. The previous BFGS method in ABACUS did not perform well for some tests and the BFGS_TRAD method now produces results that are consistant with the BFGS method in ASE. In cases where the previous BFGS method could not converge within a limited number of steps, the BFGS_TRAD method can successfully converge. + ### SD method The [SD (steepest descent) method](https://en.wikipedia.org/wiki/Gradient_descent) is one of the simplest first-order optimization methods, where in each step the motion is along the direction of the gradient, where the function descents the fastest. diff --git a/source/module_relax/relax_old/bfgs.cpp b/source/module_relax/relax_old/bfgs.cpp index 5b8dbc03d5..2915dfa84b 100644 --- a/source/module_relax/relax_old/bfgs.cpp +++ b/source/module_relax/relax_old/bfgs.cpp @@ -6,7 +6,7 @@ void BFGS::allocate(const int _size) // initialize H0、H、pos0、force0、force { - alpha=70; + alpha=70;//default value in ase is 70 maxstep=PARAM.inp.relax_bfgs_rmax; size=_size; sign =true; @@ -24,7 +24,7 @@ void BFGS::allocate(const int _size) // initialize H0、H、pos0、force0、forc steplength = std::vector(size, 0.0); } -void BFGS::relax_step(ModuleBase::matrix _force,UnitCell& ucell) +void BFGS::relax_step(const ModuleBase::matrix _force,UnitCell& ucell) { GetPos(ucell,pos); GetPostaud(ucell,pos_taud); @@ -359,7 +359,7 @@ void BFGS::IsRestrain(std::vector>& dpos) Ions_Move_Basic::converged = Ions_Move_Basic::largest_grad * ModuleBase::Ry_to_eV / 0.529177 grad= std::vector(3*size, 0.0); int iat = 0; diff --git a/source/module_relax/relax_old/bfgs.h b/source/module_relax/relax_old/bfgs.h index 5b7ae415c0..44d806afa5 100644 --- a/source/module_relax/relax_old/bfgs.h +++ b/source/module_relax/relax_old/bfgs.h @@ -49,14 +49,14 @@ class BFGS * @param _size */ void allocate(const int _size);//initialize parameters - void relax_step(ModuleBase::matrix _force,UnitCell& ucell);// + void relax_step(const ModuleBase::matrix _force,UnitCell& ucell);// void PrepareStep(std::vector>& force,std::vector>& pos,std::vector>& H,std::vector& pos0,std::vector& force0,std::vector& steplength,std::vector>& dpos,UnitCell& ucell); void IsRestrain(std::vector>& dpos); private: bool sign; - void CalculateLargestGrad(ModuleBase::matrix& _force,UnitCell& ucell); + void CalculateLargestGrad(const ModuleBase::matrix& _force,UnitCell& ucell); void GetPos(UnitCell& ucell,std::vector>& pos); void GetPostaud(UnitCell& ucell,std::vector>& pos_taud); void Update(std::vector& pos, std::vector& force,std::vector>& H,UnitCell& ucell); From 0b52719c0ce3c983566c8e67879b162f738d74d3 Mon Sep 17 00:00:00 2001 From: feiyang <2100011095@stu.pku.edu.cn> Date: Mon, 2 Dec 2024 09:17:45 +0000 Subject: [PATCH 2/5] update bfgs method and modify the parameter force in relax_step to be passed by reference --- docs/advanced/input_files/input-main.md | 2 +- docs/advanced/opt.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/advanced/input_files/input-main.md b/docs/advanced/input_files/input-main.md index 81d077f2db..4957eea134 100644 --- a/docs/advanced/input_files/input-main.md +++ b/docs/advanced/input_files/input-main.md @@ -1369,7 +1369,7 @@ These variables are used to control the geometry relaxation. - **Description**: The methods to do geometry optimization. - cg: using the conjugate gradient (CG) algorithm. Note that there are two implementations of the conjugate gradient (CG) method, see [relax_new](#relax_new). - bfgs: using the Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm. - - bfgs_trad: using BFGS algorithm consistent with the BFGS algorithm in ASE. + - bfgs_trad: using the standard Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm. - 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). - sd: using the steepest descent (SD) algorithm. - 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. diff --git a/docs/advanced/opt.md b/docs/advanced/opt.md index d4f687f275..e6d605deb2 100644 --- a/docs/advanced/opt.md +++ b/docs/advanced/opt.md @@ -24,9 +24,9 @@ The [BFGS method](https://en.wikipedia.org/wiki/Broyden%E2%80%93Fletcher%E2%80%9 In ABACUS, we implemented the BFGS method for doing fixed-cell structural relaxation. -### BFGS_TRAD method +We have alse implemented an alternative BFGS method, which can be called by using the keyword 'bfgs_trad'. -The BFGS_TRAD method is an algorithm implemented in ABACUS, referencing the BFGS method from ASE. The previous BFGS method in ABACUS did not perform well for some tests and the BFGS_TRAD method now produces results that are consistant with the BFGS method in ASE. In cases where the previous BFGS method could not converge within a limited number of steps, the BFGS_TRAD method can successfully converge. +The bfgs_trad method is a quasi-Newton method that substitute an approximate matrix B for the Hessian matrix. The optimization direction is determined by the inverse of B, therefore, only the inverse of B is iteratively updated and no time-consuming operations such as matrix inversion are involved. ### SD method From f79ab761b146e9e08bebd55f2f8ef98f11bd017e Mon Sep 17 00:00:00 2001 From: feiyang <2100011095@stu.pku.edu.cn> Date: Mon, 2 Dec 2024 09:33:13 +0000 Subject: [PATCH 3/5] update bfgs method and modify the parameter force in relax_step to be passed by reference --- docs/advanced/opt.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/advanced/opt.md b/docs/advanced/opt.md index e6d605deb2..31cad063bd 100644 --- a/docs/advanced/opt.md +++ b/docs/advanced/opt.md @@ -22,11 +22,9 @@ In the nested procedure mentioned above, we used CG method to perform cell relax The [BFGS method](https://en.wikipedia.org/wiki/Broyden%E2%80%93Fletcher%E2%80%93Goldfarb%E2%80%93Shanno_algorithm) is a quasi-Newton method for solving nonlinear optimization problem. It belongs to the class of quasi-Newton method where the Hessian matrix is approximated during the optimization process. If the initial point is not far from the extrema, BFGS tends to work better than gradient-based methods. -In ABACUS, we implemented the BFGS method for doing fixed-cell structural relaxation. - -We have alse implemented an alternative BFGS method, which can be called by using the keyword 'bfgs_trad'. +There is an alternative standard BFGS method, which can be called by using the keyword 'bfgs_trad'. The bfgs_trad method is a quasi-Newton method that substitute an approximate matrix B for the Hessian matrix. The optimization direction is determined by the inverse of B, therefore, only the inverse of B is iteratively updated and no time-consuming operations such as matrix inversion are involved. -The bfgs_trad method is a quasi-Newton method that substitute an approximate matrix B for the Hessian matrix. The optimization direction is determined by the inverse of B, therefore, only the inverse of B is iteratively updated and no time-consuming operations such as matrix inversion are involved. +In ABACUS, we implemented the BFGS method for doing fixed-cell structural relaxation. ### SD method From 808feb5e793517c0a850bfbbb488b8ce953e1c46 Mon Sep 17 00:00:00 2001 From: feiyang <2100011095@stu.pku.edu.cn> Date: Tue, 10 Dec 2024 05:33:54 +0000 Subject: [PATCH 4/5] Introduce the differences between the two BFGS methods --- docs/advanced/input_files/input-main.md | 2 +- docs/advanced/opt.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/advanced/input_files/input-main.md b/docs/advanced/input_files/input-main.md index 4957eea134..68b75bb610 100644 --- a/docs/advanced/input_files/input-main.md +++ b/docs/advanced/input_files/input-main.md @@ -1369,7 +1369,7 @@ These variables are used to control the geometry relaxation. - **Description**: The methods to do geometry optimization. - cg: using the conjugate gradient (CG) algorithm. Note that there are two implementations of the conjugate gradient (CG) method, see [relax_new](#relax_new). - bfgs: using the Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm. - - bfgs_trad: using the standard Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm. + - bfgs_trad: using the traditional Broyden–Fletcher–Goldfarb–Shanno (BFGS) algorithm. - 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). - sd: using the steepest descent (SD) algorithm. - 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. diff --git a/docs/advanced/opt.md b/docs/advanced/opt.md index 31cad063bd..9c2023b655 100644 --- a/docs/advanced/opt.md +++ b/docs/advanced/opt.md @@ -22,9 +22,9 @@ In the nested procedure mentioned above, we used CG method to perform cell relax The [BFGS method](https://en.wikipedia.org/wiki/Broyden%E2%80%93Fletcher%E2%80%93Goldfarb%E2%80%93Shanno_algorithm) is a quasi-Newton method for solving nonlinear optimization problem. It belongs to the class of quasi-Newton method where the Hessian matrix is approximated during the optimization process. If the initial point is not far from the extrema, BFGS tends to work better than gradient-based methods. -There is an alternative standard BFGS method, which can be called by using the keyword 'bfgs_trad'. The bfgs_trad method is a quasi-Newton method that substitute an approximate matrix B for the Hessian matrix. The optimization direction is determined by the inverse of B, therefore, only the inverse of B is iteratively updated and no time-consuming operations such as matrix inversion are involved. +There is an alternative traditional BFGS method, which can be called by using the keyword 'bfgs_trad'. The bfgs_trad method is a quasi-Newton method that substitute an approximate matrix B for the Hessian matrix. The main difference between 'bfgs' and 'bfgs_trad' is that 'bfgs' updates the inverse of matrix B while 'bfgs_trad' updates matrix B and obtains the inverse of B by solving the matrix eigenvalues and taking the reciprocal of the eigenvalues. Both methods are mathematically equivalent, but in some cases, 'bfgs_trad' performs better. -In ABACUS, we implemented the BFGS method for doing fixed-cell structural relaxation. +In ABACUS, we implemented the BFGS method for doing fixed-cell structural relaxation. Users can choose which implementation of BFGS to call by adding the 'bfgs_trad' or 'bfgs' parameter. ### SD method From ebdac5899085b6d42391a23d9ccc2b517d5a2cf8 Mon Sep 17 00:00:00 2001 From: feiyang <2100011095@stu.pku.edu.cn> Date: Wed, 11 Dec 2024 13:06:39 +0000 Subject: [PATCH 5/5] Add & in relax_step input parameters --- source/module_relax/relax_old/bfgs.cpp | 2 +- source/module_relax/relax_old/bfgs.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/module_relax/relax_old/bfgs.cpp b/source/module_relax/relax_old/bfgs.cpp index 2915dfa84b..44f4bf8bf4 100644 --- a/source/module_relax/relax_old/bfgs.cpp +++ b/source/module_relax/relax_old/bfgs.cpp @@ -24,7 +24,7 @@ void BFGS::allocate(const int _size) // initialize H0、H、pos0、force0、forc steplength = std::vector(size, 0.0); } -void BFGS::relax_step(const ModuleBase::matrix _force,UnitCell& ucell) +void BFGS::relax_step(const ModuleBase::matrix& _force,UnitCell& ucell) { GetPos(ucell,pos); GetPostaud(ucell,pos_taud); diff --git a/source/module_relax/relax_old/bfgs.h b/source/module_relax/relax_old/bfgs.h index 44d806afa5..fba422c886 100644 --- a/source/module_relax/relax_old/bfgs.h +++ b/source/module_relax/relax_old/bfgs.h @@ -49,7 +49,7 @@ class BFGS * @param _size */ void allocate(const int _size);//initialize parameters - void relax_step(const ModuleBase::matrix _force,UnitCell& ucell);// + void relax_step(const ModuleBase::matrix& _force,UnitCell& ucell);// void PrepareStep(std::vector>& force,std::vector>& pos,std::vector>& H,std::vector& pos0,std::vector& force0,std::vector& steplength,std::vector>& dpos,UnitCell& ucell); void IsRestrain(std::vector>& dpos);