Skip to content
Open
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
9 changes: 7 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Language: Cpp
BasedOnStyle: Microsoft
BasedOnStyle: LLVM

AlwaysBreakTemplateDeclarations: Yes

Expand All @@ -9,7 +9,7 @@ AllowAllParametersOfDeclarationOnNextLine: false
BinPackArguments: false
BinPackParameters: false

BreakBeforeBinaryOperators: All
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: true

Cpp11BracedListStyle: true
Expand All @@ -25,6 +25,11 @@ IncludeIsMainSourceRegex: ''
IndentWidth: 4
IndentWrappedFunctionNames: true

ColumnLimit: 120

# Avoid awkward line breaks before member access operators (e.g., obj.method()).
PenaltyBreakBeforeMemberAccess: 1000

# About Point
DerivePointerAlignment: false
PointerAlignment: Left
Expand Down
130 changes: 42 additions & 88 deletions source/source_md/fire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ FIRE::FIRE(const MD_para& mdp_in,
const int my_rank_in,
const double force_thr_in,
MDCell& mdcell_in)
: MD_base(mdp_in, cal_stress_in, init_vel, my_rank_in, mdcell_in)
{
: MD_base(mdp_in, cal_stress_in, init_vel, my_rank_in, mdcell_in) {
force_thr = force_thr_in;
dt_max = -1.0;
alpha_start = 0.10;
Expand All @@ -32,12 +31,9 @@ FIRE::FIRE(const MD_para& mdp_in,
// force_thr = 1e-3;
}

FIRE::~FIRE()
{
}
FIRE::~FIRE() {}

void FIRE::setup(ModuleESolver::ESolver* p_esolver, const std::string& global_readin_dir, DomainDecomposition& decomp)
{
void FIRE::setup(ModuleESolver::ESolver* p_esolver, const std::string& global_readin_dir, DomainDecomposition& decomp) {
ModuleBase::TITLE("FIRE", "setup");
ModuleBase::timer::start("FIRE", "setup");

Expand All @@ -50,8 +46,7 @@ void FIRE::setup(ModuleESolver::ESolver* p_esolver, const std::string& global_re
return;
}

void FIRE::first_half(std::ofstream& ofs)
{
void FIRE::first_half(std::ofstream& ofs) {
ModuleBase::TITLE("FIRE", "first_half");
ModuleBase::timer::start("FIRE", "first_half");

Expand All @@ -66,9 +61,7 @@ void FIRE::first_half(std::ofstream& ofs)
return;
}


void FIRE::second_half(void)
{
void FIRE::second_half(void) {
ModuleBase::TITLE("FIRE", "second_half");
ModuleBase::timer::start("FIRE", "second_half");

Expand All @@ -81,9 +74,7 @@ void FIRE::second_half(void)
return;
}


void FIRE::print_md(std::ofstream& ofs, const bool& cal_stress)
{
void FIRE::print_md(std::ofstream& ofs, const bool& cal_stress) {
MD_base::print_md(ofs, cal_stress);

const double max_force = max * ModuleBase::Hartree_to_eV * ModuleBase::ANGSTROM_AU;
Expand All @@ -94,11 +85,8 @@ void FIRE::print_md(std::ofstream& ofs, const bool& cal_stress)
return;
}


void FIRE::write_restart(const std::string& global_out_dir)
{
if (!my_rank)
{
void FIRE::write_restart(const std::string& global_out_dir) {
if (!my_rank) {
std::stringstream ssc;
ssc << global_out_dir << "Restart_md.txt";
std::ofstream file(ssc.str().c_str());
Expand All @@ -118,29 +106,23 @@ void FIRE::write_restart(const std::string& global_out_dir)
return;
}


void FIRE::restart(const std::string& global_readin_dir)
{
void FIRE::restart(const std::string& global_readin_dir) {
bool ok = true;

if (!my_rank)
{
if (!my_rank) {
std::stringstream ssc;
ssc << global_readin_dir << "Restart_md.txt";
std::ifstream file(ssc.str().c_str());

if (!file)
{
if (!file) {
ok = false;
}

if (ok)
{
if (ok) {
file >> step_rst_ >> md_tfirst >> alpha >> negative_count >> dt_max >> md_dt;
if(!file)
{
if (!file) {
ok = false;
}
}
file.close();
}
}
Expand All @@ -149,8 +131,7 @@ void FIRE::restart(const std::string& global_readin_dir)
MPI_Bcast(&ok, 1, MPI_C_BOOL, 0, mdcell.communicator());
#endif

if (!ok)
{
if (!ok) {
ModuleBase::WARNING_QUIT("mdrun", "no Restart_md.txt !");
}

Expand All @@ -166,40 +147,34 @@ void FIRE::restart(const std::string& global_readin_dir)
return;
}

void FIRE::check_force(void)
{
void FIRE::check_force(void) {
max = 0.0;

std::int64_t movable_dof = 0;

for (const LocalAtom& atom : mdcell.owned_atoms())
{
for (int j = 0; j < 3; ++j)
{
for (const LocalAtom& atom: mdcell.owned_atoms()) {
for (int j = 0; j < 3; ++j) {
// Only movable degrees of freedom should be used
// in the FIRE convergence criterion.
//
// For example:
// m 1 1 1 -> x/y/z are included.
// m 1 0 1 -> y is excluded.
// m 0 0 0 -> this atom contributes no DOF to convergence.
if (!atom.mbl[j])
{
if (!atom.mbl[j]) {
continue;
}

++movable_dof;

if (max < std::abs(atom.force[j]))
{
if (max < std::abs(atom.force[j])) {
max = std::abs(atom.force[j]);
}
}
}

#ifdef __MPI
if (mdcell.mpi_size() > 1)
{
#ifdef __MPI
if (mdcell.mpi_size() > 1) {
double global_max = 0.0;
std::int64_t global_movable_dof = 0;
MPI_Allreduce(&max, &global_max, 1, MPI_DOUBLE, MPI_MAX, mdcell.communicator());
Expand All @@ -210,30 +185,25 @@ void FIRE::check_force(void)
#endif

// If there are no movable degrees of freedom, there is nothing to optimize.
if (movable_dof == 0)
{
if (movable_dof == 0) {
stop = true;
return;
}

if (2.0 * max < force_thr)
{
if (2.0 * max < force_thr) {
stop = true;
}

return;
}


void FIRE::check_fire(void)
{
void FIRE::check_fire(void) {
double P = 0.0;
double sumforce = 0.0;
double normvel = 0.0;

/// initial dt_max
if (dt_max < 0)
{
if (dt_max < 0) {
dt_max = 2.5 * md_dt;
}

Expand All @@ -242,12 +212,9 @@ void FIRE::check_fire(void)
// Compute P, |F| and |v| only on movable degrees of freedom.
// Fixed atoms/directions may have non-zero raw forces, but they should not
// affect the FIRE velocity projection or adaptive time-step control.
for (LocalAtom& atom : mdcell.owned_atoms())
{
for (int j = 0; j < 3; ++j)
{
if (!atom.mbl[j])
{
for (LocalAtom& atom: mdcell.owned_atoms()) {
for (int j = 0; j < 3; ++j) {
if (!atom.mbl[j]) {
// Keep frozen components clean.
atom.vel[j] = 0.0;
continue;
Expand All @@ -261,9 +228,8 @@ void FIRE::check_fire(void)
}
}

#ifdef __MPI
if (mdcell.mpi_size() > 1)
{
#ifdef __MPI
if (mdcell.mpi_size() > 1) {
double local_values[3] = {P, sumforce, normvel};
double global_values[3] = {0.0, 0.0, 0.0};
std::int64_t global_movable_dof = 0;
Expand All @@ -277,8 +243,7 @@ void FIRE::check_fire(void)
#endif

// No movable degrees of freedom: nothing to update.
if (movable_dof == 0)
{
if (movable_dof == 0) {
return;
}

Expand All @@ -287,42 +252,31 @@ void FIRE::check_fire(void)

// If force or velocity norm is zero, the velocity projection is undefined.
// Avoid 0/0. In a truly converged case check_force() should stop the run.
if (sumforce > 0.0 && normvel > 0.0)
{
for (LocalAtom& atom : mdcell.owned_atoms())
{
for (int j = 0; j < 3; ++j)
{
if (!atom.mbl[j])
{
if (sumforce > 0.0 && normvel > 0.0) {
for (LocalAtom& atom: mdcell.owned_atoms()) {
for (int j = 0; j < 3; ++j) {
if (!atom.mbl[j]) {
atom.vel[j] = 0.0;
continue;
}

atom.vel[j] = (1.0 - alpha) * atom.vel[j]
+ alpha * atom.force[j] / sumforce * normvel;
atom.vel[j] = (1.0 - alpha) * atom.vel[j] + alpha * atom.force[j] / sumforce * normvel;
}
}
}

if (P > 0)
{
if (P > 0) {
negative_count++;
if (negative_count >= n_min)
{
if (negative_count >= n_min) {
md_dt = std::min(md_dt * finc, dt_max);
alpha *= f_alpha;
}
}
else
{
} else {
md_dt *= fdec;
negative_count = 0;

for (LocalAtom& atom : mdcell.owned_atoms())
{
for (int j = 0; j < 3; ++j)
{
for (LocalAtom& atom: mdcell.owned_atoms()) {
for (int j = 0; j < 3; ++j) {
atom.vel[j] = 0;
}
}
Expand Down
19 changes: 4 additions & 15 deletions source/source_md/fire.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
* It is based on conventional molecular dynamics with additional velocity modifications and adaptive time steps.
* The MD trajectory will descend to an energy-minimum.
*/
class FIRE : public MD_base
{
class FIRE : public MD_base {
public:
/// @param force_thr_in force convergence threshold used by the FIRE descent
FIRE(const MD_para& mdp_in,
Expand All @@ -24,25 +23,15 @@ class FIRE : public MD_base
~FIRE();

/// @brief mixing coefficient of the FIRE velocity update
double get_alpha() const
{
return alpha;
}
double get_alpha() const { return alpha; }

/// @brief largest time step the adaptive scheme is allowed to reach
double get_dt_max() const
{
return dt_max;
}
double get_dt_max() const { return dt_max; }

/// @brief number of consecutive steps with negative power
int get_negative_count() const
{
return negative_count;
}
int get_negative_count() const { return negative_count; }

private:

void setup(ModuleESolver::ESolver* p_esolver, const std::string& global_readin_dir, DomainDecomposition& decomp);

void first_half(std::ofstream& ofs);
Expand Down
Loading
Loading