Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions source/source_relax/bfgs_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,110 @@ class BFGS_Basic
BFGS_Basic();
~BFGS_Basic() = default;

//====================================================================
// Test seam.
//
// The BFGS state below is protected and the update machinery private,
// because only Ions_Move_BFGS drives them. The unit tests seed that state
// and step the algorithm one stage at a time, so each piece they touch is
// reachable through the accessors and wrappers here rather than by
// reinterpreting the access specifiers.
//
// Production code must keep using the protected/private names directly;
// nothing outside the tests should call the *_for_testing() wrappers.
//====================================================================

/// @brief 3N coordinates of the system ( x )
std::vector<double>& get_pos()
{
return pos;
}
/// @brief 3N components of ( grad( V(x) ) )
std::vector<double>& get_grad()
{
return grad;
}
/// @brief the step taken, pos = pos_p + move
std::vector<double>& get_move()
{
return move;
}
/// @brief coordinates of the previous step
std::vector<double>& get_pos_p()
{
return pos_p;
}
/// @brief gradient of the previous step
std::vector<double>& get_grad_p()
{
return grad_p;
}
/// @brief step taken at the previous step
std::vector<double>& get_move_p()
{
return move_p;
}
/// @brief whether a bfgs state has been saved
bool& get_save_flag()
{
return save_flag;
}
/// @brief whether the trust radius already hit its minimum last step
bool& get_tr_min_hit()
{
return tr_min_hit;
}
/// @brief whether the Wolfe conditions were satisfied
bool& get_wolfe_flag()
{
return wolfe_flag;
}
/// @brief the inverse Hessian of the BFGS update
ModuleBase::matrix& get_inv_hess()
{
return inv_hess;
}
/// @brief number of previous steps kept by the BFGS update
int& get_bfgs_ndim()
{
return bfgs_ndim;
}

void allocate_basic_for_testing()
{
allocate_basic();
}
void new_step_for_testing(const double& lat0,
int& update_iter,
std::ofstream& ofs,
std::vector<double>& etot_info,
const int test_relax_method)
{
new_step(lat0, update_iter, ofs, etot_info, test_relax_method);
}
void reset_hessian_for_testing()
{
reset_hessian();
}
void save_bfgs_for_testing()
{
save_bfgs();
}
void update_inverse_hessian_for_testing(const double& lat0, std::ofstream& ofs)
{
update_inverse_hessian(lat0, ofs);
}
void check_wolfe_conditions_for_testing(std::ofstream& ofs, std::vector<double>& etot_info)
{
check_wolfe_conditions(ofs, etot_info);
}
void compute_trust_radius_for_testing(std::ofstream& ofs,
std::vector<double>& etot_info,
const int test_relax_method)
{
compute_trust_radius(ofs, etot_info, test_relax_method);
}

protected:
void allocate_basic(void);
void new_step(const double& lat0, int& update_iter, std::ofstream& ofs, std::vector<double>& etot_info, const int test_relax_method);
Expand Down
34 changes: 34 additions & 0 deletions source/source_relax/ions_move_bfgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ class Ions_Move_BFGS : public BFGS_Basic
void reset(void);
bool start(UnitCell& ucell, const ModuleBase::matrix& force, const double& energy_in, const int istep, int& update_iter, std::ofstream& ofs, std::vector<double>& etot_info, const Relax_Criteria& criteria);

//====================================================================
// Test seam; see the equivalent block in BFGS_Basic. Production code
// must keep using the private names directly.
//====================================================================

/// @brief whether allocate() has already run
bool& get_init_done()
{
return init_done;
}
/// @brief whether this is the first step of the relaxation
bool& get_first_step()
{
return first_step;
}

void bfgs_routine_for_testing(const double& lat0,
const int istep,
int& update_iter,
std::ofstream& ofs,
std::vector<double>& etot_info,
const std::string& out_level,
const int test_relax_method)
{
bfgs_routine(lat0, istep, update_iter, ofs, etot_info, out_level, test_relax_method);
}
void restart_bfgs_for_testing(const double& lat0,
int& update_iter,
std::ofstream& ofs,
const int test_relax_method)
{
restart_bfgs(lat0, update_iter, ofs, test_relax_method);
}

private:
bool init_done;
void bfgs_routine(const double& lat0, const int istep, int& update_iter, std::ofstream& ofs, std::vector<double>& etot_info, const std::string& out_level, const int test_relax_method);
Expand Down
Loading
Loading