From a18f1675e0a8982084e16875a39bc749475f7705 Mon Sep 17 00:00:00 2001 From: Fei Yang <2501213217@stu.pku.edu.cn> Date: Fri, 16 Jan 2026 17:08:08 +0800 Subject: [PATCH 1/3] Optimize install_openmpi.sh script --- toolchain/scripts/stage1/install_openmpi.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/toolchain/scripts/stage1/install_openmpi.sh b/toolchain/scripts/stage1/install_openmpi.sh index 6656fb9a97..6798e8ef17 100755 --- a/toolchain/scripts/stage1/install_openmpi.sh +++ b/toolchain/scripts/stage1/install_openmpi.sh @@ -91,7 +91,12 @@ case "${with_openmpi}" in # else # EXTRA_CONFIGURE_FLAGS="" # fi - ./configure CFLAGS="${CFLAGS}" \ + ./configure \ + CC=gcc \ + CXX=g++ \ + FC=gfortran \ + F77=gfortran \ + CFLAGS="${CFLAGS}" \ --prefix=${pkg_install_dir} \ --libdir="${pkg_install_dir}/lib" \ --with-libevent=internal \ From 311000c5af7ba29eb798ada514c130c5db68cdf1 Mon Sep 17 00:00:00 2001 From: Fei Yang <2501213217@stu.pku.edu.cn> Date: Mon, 23 Mar 2026 15:35:02 +0800 Subject: [PATCH 2/3] fix cg-bfgs problem, add initialization in bfgs --- source/source_relax/bfgs.cpp | 5 +++++ source/source_relax/bfgs.h | 1 + source/source_relax/ions_move_cg.cpp | 3 ++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/source/source_relax/bfgs.cpp b/source/source_relax/bfgs.cpp index d105dc2791..814669bd51 100644 --- a/source/source_relax/bfgs.cpp +++ b/source/source_relax/bfgs.cpp @@ -30,11 +30,16 @@ void BFGS::allocate(const int _size) force0 = std::vector(3*size, 0.0); force = std::vector>(size, ModuleBase::Vector3(0.0, 0.0, 0.0)); steplength = std::vector(size, 0.0); + is_initialized = true; } void BFGS::relax_step(const ModuleBase::matrix& _force,UnitCell& ucell) { + if(!is_initialized) + { + allocate(ucell.nat); + } GetPos(ucell,pos); GetPostaud(ucell,pos_taud); ucell.ionic_position_updated = true; diff --git a/source/source_relax/bfgs.h b/source/source_relax/bfgs.h index b8872c979f..ae83173562 100644 --- a/source/source_relax/bfgs.h +++ b/source/source_relax/bfgs.h @@ -24,6 +24,7 @@ class BFGS double maxstep;//every movement smaller than maxstep double largest_grad; int size;//number of atoms + bool is_initialized=false; std::vector steplength;//the length of atoms displacement std::vector> H;//Hessian matrix diff --git a/source/source_relax/ions_move_cg.cpp b/source/source_relax/ions_move_cg.cpp index eec0009764..05e18c4b7f 100644 --- a/source/source_relax/ions_move_cg.cpp +++ b/source/source_relax/ions_move_cg.cpp @@ -2,6 +2,7 @@ #include "ions_move_basic.h" #include "source_base/global_function.h" #include "source_base/global_variable.h" + using namespace Ions_Move_Basic; double Ions_Move_CG::RELAX_CG_THR = -1.0; // default is 0.5 @@ -169,7 +170,7 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const < RELAX_CG_THR) // cg to bfgs by pengfei 13-8-8 { Ions_Move_Basic::relax_method[0] = "bfgs"; - Ions_Move_Basic::relax_method[1] = "2"; + Ions_Move_Basic::relax_method[1] = "1"; } Ions_Move_Basic::best_xxx = steplength; } From 7f16f00bbdd6db8e6175d6f07ec7570f42001d68 Mon Sep 17 00:00:00 2001 From: Fei Yang <2501213217@stu.pku.edu.cn> Date: Wed, 25 Mar 2026 12:47:15 +0800 Subject: [PATCH 3/3] fix cg-bfgs problem, add initialization in bfgs --- source/source_relax/test/bfgs_test.cpp | 26 +++++++++++++++++++++ toolchain/scripts/stage1/install_openmpi.sh | 8 +++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/source/source_relax/test/bfgs_test.cpp b/source/source_relax/test/bfgs_test.cpp index fb82b90ca0..0f86fee1ea 100644 --- a/source/source_relax/test/bfgs_test.cpp +++ b/source/source_relax/test/bfgs_test.cpp @@ -54,6 +54,32 @@ TEST_F(BFGSTest, TestAllocate) EXPECT_EQ(bfgs.largest_grad,0.0); } +// Test that relax_step will auto-initialize if not already initialized +TEST_F(BFGSTest, RelaxStepAutoInitialize) +{ + bfgs.is_initialized = false; + + UnitCell ucell; + ucell.nat = 2; + ucell.ntype = 1; + ucell.atoms.resize(1); + ucell.atoms[0].na = 2; + ucell.atoms[0].tau.resize(2); + ucell.atoms[0].taud.resize(2); + ucell.atoms[0].mbl.resize(2, {1,1,1}); + ucell.lat0 = 1.0; + + ModuleBase::matrix force(2, 3); + force(0,0) = 0.1; force(0,1) = 0.2; force(0,2) = 0.3; + force(1,0) = 0.4; force(1,1) = 0.5; force(1,2) = 0.6; + + // Before relax_step, is_initialized should be false + EXPECT_FALSE(bfgs.is_initialized); + bfgs.relax_step(force, ucell); + // After relax_step, is_initialized should be true + EXPECT_TRUE(bfgs.is_initialized); +} + // Test if a dimension less than or equal to 0 results in an assertion error TEST_F(BFGSTest, TestAllocateWithZeroDimension) { diff --git a/toolchain/scripts/stage1/install_openmpi.sh b/toolchain/scripts/stage1/install_openmpi.sh index 6798e8ef17..fd9d83654c 100755 --- a/toolchain/scripts/stage1/install_openmpi.sh +++ b/toolchain/scripts/stage1/install_openmpi.sh @@ -92,10 +92,10 @@ case "${with_openmpi}" in # EXTRA_CONFIGURE_FLAGS="" # fi ./configure \ - CC=gcc \ - CXX=g++ \ - FC=gfortran \ - F77=gfortran \ + CC="${CC:-gcc}" \ + CXX="${CXX:-g++}" \ + FC="${FC:-gfortran}" \ + F77="${F77:-gfortran}" \ CFLAGS="${CFLAGS}" \ --prefix=${pkg_install_dir} \ --libdir="${pkg_install_dir}/lib" \