11#include " for_test.h"
22#include " gmock/gmock.h"
33#include " gtest/gtest.h"
4+ #include < algorithm>
5+ #include < cstdio>
6+ #include < fstream>
7+ #include < iterator>
8+ #include < string>
9+ #include < vector>
410#define private public
11+ #define protected public
512#include " source_relax/ions_move_methods.h"
13+ #undef protected
614#undef private
715/* ***********************************************
816 * unit tests of class Ions_Move_Methods
@@ -62,6 +70,10 @@ namespace unitcell
6270 }
6371 }
6472
73+ void update_pos_tau (const Lattice&, const double *, const int , const int , Atom*)
74+ {
75+ }
76+
6577 // Helper function to reset mock state
6678 void reset_remake_cell_mock ()
6779 {
@@ -130,7 +142,9 @@ TEST_F(IonsMoveMethodsTest, CalMovement)
130142{
131143 const int istep = 0 ;
132144 const int force_step = 1 ;
133- const ModuleBase::matrix f (3 , 3 );
145+ ModuleBase::matrix f (natom, 3 );
146+ f (0 , 0 ) = 0.1 ;
147+ f (1 , 1 ) = -0.1 ;
134148 const double etot = 0.0 ;
135149 UnitCell ucell;
136150 std::ofstream ofs;
@@ -205,7 +219,74 @@ TEST_F(IonsMoveMethodsTest, GetTrustRadius)
205219// Test the get_update_iter() function
206220TEST_F (IonsMoveMethodsTest, GetUpdateIter)
207221{
208- Ions_Move_Basic::update_iter = 4 ;
222+ imm. update_iter_ = 4 ;
209223
210224 EXPECT_EQ (imm.get_update_iter (), 4 );
211- }
225+ }
226+
227+ TEST_F (IonsMoveMethodsTest, ResetAfterCellChange)
228+ {
229+ const std::string log_file = " reset_after_cell_change.log" ;
230+ std::ofstream ofs (log_file);
231+
232+ imm.allocate (natom, " bfgs" , " 2" );
233+ imm.converged_ = true ;
234+ imm.update_iter_ = 4 ;
235+ imm.etot_info_ = {-1.0 , -2.0 };
236+ imm.bfgs .first_step = false ;
237+ imm.bfgs .save_flag = true ;
238+ imm.bfgs .tr_min_hit = true ;
239+ std::fill (imm.bfgs .pos .begin (), imm.bfgs .pos .end (), 1.0 );
240+ std::fill (imm.bfgs .pos_p .begin (), imm.bfgs .pos_p .end (), 2.0 );
241+ std::fill (imm.bfgs .grad .begin (), imm.bfgs .grad .end (), 3.0 );
242+ std::fill (imm.bfgs .grad_p .begin (), imm.bfgs .grad_p .end (), 4.0 );
243+ std::fill (imm.bfgs .move .begin (), imm.bfgs .move .end (), 5.0 );
244+ std::fill (imm.bfgs .move_p .begin (), imm.bfgs .move_p .end (), 6.0 );
245+ Ions_Move_Basic::trust_radius = 0.3 ;
246+ Ions_Move_Basic::trust_radius_old = 0.2 ;
247+
248+ imm.reset_after_cell_change ({" bfgs" , " 2" }, ofs);
249+
250+ EXPECT_FALSE (imm.converged_ );
251+ EXPECT_EQ (imm.update_iter_ , 0 );
252+ EXPECT_THAT (imm.etot_info_ , testing::Each (0.0 ));
253+ EXPECT_TRUE (imm.bfgs .first_step );
254+ EXPECT_FALSE (imm.bfgs .save_flag );
255+ EXPECT_FALSE (imm.bfgs .tr_min_hit );
256+ EXPECT_THAT (imm.bfgs .pos , testing::Each (0.0 ));
257+ EXPECT_THAT (imm.bfgs .pos_p , testing::Each (0.0 ));
258+ EXPECT_THAT (imm.bfgs .grad , testing::Each (0.0 ));
259+ EXPECT_THAT (imm.bfgs .grad_p , testing::Each (0.0 ));
260+ EXPECT_THAT (imm.bfgs .move , testing::Each (0.0 ));
261+ EXPECT_THAT (imm.bfgs .move_p , testing::Each (0.0 ));
262+ for (int i = 0 ; i < Ions_Move_Basic::dim; ++i)
263+ {
264+ for (int j = 0 ; j < Ions_Move_Basic::dim; ++j)
265+ {
266+ EXPECT_DOUBLE_EQ (imm.bfgs .inv_hess (i, j), i == j ? 1.0 : 0.0 );
267+ }
268+ }
269+ EXPECT_DOUBLE_EQ (Ions_Move_Basic::trust_radius, 0.0 );
270+ EXPECT_DOUBLE_EQ (Ions_Move_Basic::trust_radius_old, 0.0 );
271+
272+ imm.allocate (natom, " bfgs" , " 1" );
273+ imm.converged_ = true ;
274+ imm.update_iter_ = 3 ;
275+ imm.etot_info_ = {-3.0 , -4.0 };
276+ ASSERT_TRUE (imm.bfgs_trad .is_initialized );
277+
278+ imm.reset_after_cell_change ({" bfgs" , " 1" }, ofs);
279+ ofs.close ();
280+
281+ EXPECT_FALSE (imm.converged_ );
282+ EXPECT_EQ (imm.update_iter_ , 0 );
283+ EXPECT_THAT (imm.etot_info_ , testing::Each (0.0 ));
284+ EXPECT_FALSE (imm.bfgs_trad .is_initialized );
285+
286+ std::ifstream ifs (log_file);
287+ const std::string output ((std::istreambuf_iterator<char >(ifs)), std::istreambuf_iterator<char >());
288+ EXPECT_THAT (output, testing::HasSubstr (" Reset ionic BFGS history after cell change." ));
289+ EXPECT_THAT (output, testing::HasSubstr (" Reset traditional ionic BFGS history after cell change." ));
290+ ifs.close ();
291+ std::remove (log_file.c_str ());
292+ }
0 commit comments