ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
relax_sync.h
Go to the documentation of this file.
1// Wenfei Li, November 2022
2// A new implementation of CG relaxation
3#ifndef RELAX1_H
4#define RELAX1_H
5
6#include "line_search.h"
11
12#include <fstream>
13#include <vector>
14
15class Relax
16{
17 public:
18 Relax() {};
19 ~Relax() {};
20
21 // prepare for relaxation
22 void init_relax(const int nat_in, const Input_para &inp);
23 // perform a single relaxation step
24 bool relax_step(
25 UnitCell &ucell,
26 const ModuleBase::matrix &force,
27 const ModuleBase::matrix &stress,
28 const double etot_in,
29 std::ofstream &ofs_running);
30
31 private:
32 int istep = 0; // count ionic step
33
34 // setup gradient based on force and stress
35 // constraints are considered here
36 // also check if relaxation has converged
37 // based on threshold in force & stress
38 bool setup_gradient(
39 const UnitCell &ucell,
40 const ModuleBase::matrix &force,
41 const ModuleBase::matrix &stress,
42 std::ofstream &ofs_running);
43
44 // Compute the ionic gradient from the force (in eV/Angstrom), honoring
45 // per-atom move flags, and return the largest component. Records the value
46 // in the force history and the running log, prints the optional out_level
47 // diagnostics, and updates force_converged.
48 double setup_ion_gradient(
49 const UnitCell &ucell,
50 const ModuleBase::matrix &force,
51 bool &force_converged,
52 std::ofstream &ofs_running);
53
54 // Compute the cell gradient from the stress, applying the fixed_axes
55 // constraints (shape / volume / per-axis), and update force_converged.
56 // Records the largest stress in the history and the running log. Only runs
57 // when if_cell_moves is true.
59 const UnitCell &ucell,
60 const ModuleBase::matrix &stress,
61 bool &force_converged,
62 std::ofstream &ofs_running);
63
64 // Print the converged / not-converged summary to the running log. On
65 // convergence, reports the method, the converged step (istep + 1, since
66 // setup_gradient runs before istep is incremented), and the per-step
67 // largest force (and stress when the cell moves).
68 void print_gradient_summary(bool force_converged, std::ofstream &ofs_running);
69
70 // check whether previous line search is done
71 bool check_line_search();
72
73 // if line search not done : perform line search
74 void perform_line_search(std::ofstream &ofs_running);
75
76 // if line search done: find new search direction and make a trial move
77 void new_direction(std::ofstream &ofs_running);
78
79 // move ions and lattice vectors
80 void move_cell_ions(UnitCell &ucell, const bool is_new_dir, std::ofstream &ofs_running);
81
82 // Step 1 of move_cell_ions: update latvec along the cell search
83 // direction, honoring per-axis free flags, the volume constraint and
84 // fixed_ibrav. Saves latvec at the start of each CG step. Only runs when
85 // if_cell_moves is true.
86 void update_lattice(UnitCell &ucell, double fac, bool is_new_dir);
87
88 // Steps 2 & 3 of move_cell_ions: compute the ionic displacement along the
89 // ion search direction (Cartesian -> direct via the OLD GT), apply the
90 // per-atom move flags and symmetry, then update taud/tau and print the
91 // structure.
92 void update_ion_positions(UnitCell &ucell, double fac, std::ofstream &ofs_running);
93
94 // Steps 4 & 6 of move_cell_ions: refresh a1/a2/a3, omega and the
95 // reciprocal lattice (G/GT/GGT) from the new latvec, broadcast them under
96 // MPI, and re-setup the cell for the next SCF. Only runs when
97 // if_cell_moves is true.
98 void update_reciprocal_cell(UnitCell &ucell, std::ofstream &ofs_running);
99
100 int nat = 0; // number of atoms
101 bool ltrial = false; // if last step is trial step
102
103 double step_size = 0.0;
104
105 // Gradients; _p means previous step
110
111 // Search directions; _p means previous step
116
117 // Used for applyting constraints
118 bool if_cell_moves = false;
119
120 // Keeps track of how many CG trial steps have been performed,
121 // namely the number of CG directions followed
122 // Note : this should not be confused with number of ionic steps
123 // which includes both trial and line search steps
124 int cg_step = 0;
125
126 // in CG, search_dr = search_dr_p + grad * gamma
127 double gamma = 0.0;
128 void calculate_gamma();
129
130 // Intermediate variables
131 // I put them here because they are used across different subroutines
132 double sr_sr = 0.0;
133 double srp_srp = 0.0; // inner/cross products between search directions
134 double gr_gr = 0.0;
135 double gr_grp = 0.0;
136 double grp_grp = 0.0; // inner/cross products between gradients
137 double gr_sr = 0.0; // cross product between search direction and gradient
138 double e1ord1 = 0.0;
139 double e1ord2 = 0.0;
140 double e2ord = 0.0;
141 double e2ord2 = 0.0;
142 double dmove = 0.0;
143 double dmovel = 0.0;
144 double dmoveh = 0.0;
145 double etot = 0.0;
146 double etot_p = 0.0;
148 double omega_p = 0.0;
149 double force_thr_eva = 0.0;
150
151 bool brent_done = false; // if brent line search is finished
152
153 double fac_force = 0.0;
154 double fac_stress = 0.0;
155
158 const Input_para *inp_ = nullptr;
159
161 std::vector<double> max_force_history_;
163 std::vector<double> max_stress_history_;
164
165 public:
167 const std::vector<double> &get_max_force_history() const { return max_force_history_; }
168 const std::vector<double> &get_max_stress_history() const { return max_stress_history_; }
169};
170
171#endif
Definition line_search.h:7
3x3 matrix and related mathamatical operations
Definition matrix3.h:15
Definition matrix.h:15
ModuleBase::matrix grad_ion_p
Definition relax_sync.h:108
void move_cell_ions(UnitCell &ucell, const bool is_new_dir, std::ofstream &ofs_running)
Definition relax_sync.cpp:526
double etot
Definition relax_sync.h:145
int istep
Definition relax_sync.h:32
bool relax_step(UnitCell &ucell, const ModuleBase::matrix &force, const ModuleBase::matrix &stress, const double etot_in, std::ofstream &ofs_running)
Definition relax_sync.cpp:63
int cg_step
Definition relax_sync.h:124
double step_size
Definition relax_sync.h:103
double omega_p
previous cell volume in Angstrom^3, used to print volume diff during cell-relax
Definition relax_sync.h:148
double fac_stress
Definition relax_sync.h:154
double gr_sr
Definition relax_sync.h:137
ModuleBase::matrix search_dr_cell
Definition relax_sync.h:113
bool brent_done
Definition relax_sync.h:151
double force_thr_eva
Definition relax_sync.h:149
const std::vector< double > & get_max_stress_history() const
Definition relax_sync.h:168
double e2ord
Definition relax_sync.h:140
void print_gradient_summary(bool force_converged, std::ofstream &ofs_running)
Definition relax_sync.cpp:305
double dmoveh
Definition relax_sync.h:144
ModuleBase::matrix grad_cell
Definition relax_sync.h:107
ModuleBase::matrix search_dr_ion_p
Definition relax_sync.h:114
double e2ord2
Definition relax_sync.h:141
ModuleBase::matrix grad_cell_p
Definition relax_sync.h:109
void perform_line_search(std::ofstream &ofs_running)
Definition relax_sync.cpp:401
ModuleBase::matrix grad_ion
Definition relax_sync.h:106
~Relax()
Definition relax_sync.h:19
double gr_gr
Definition relax_sync.h:134
void update_reciprocal_cell(UnitCell &ucell, std::ofstream &ofs_running)
Definition relax_sync.cpp:710
double e1ord1
Definition relax_sync.h:138
ModuleBase::matrix search_dr_ion
Definition relax_sync.h:112
ModuleBase::Matrix3 latvec_save
Definition relax_sync.h:156
double dmovel
Definition relax_sync.h:143
ModuleBase::matrix search_dr_cell_p
Definition relax_sync.h:115
const Input_para * inp_
Definition relax_sync.h:158
bool check_line_search()
Definition relax_sync.cpp:379
void init_relax(const int nat_in, const Input_para &inp)
Definition relax_sync.cpp:14
double gamma
Definition relax_sync.h:127
double gr_grp
Definition relax_sync.h:135
int nat
Definition relax_sync.h:100
Line_Search ls
Definition relax_sync.h:157
std::vector< double > max_stress_history_
Largest stress of each step (kbar), only filled in cell-relax.
Definition relax_sync.h:163
double srp_srp
Definition relax_sync.h:133
bool setup_gradient(const UnitCell &ucell, const ModuleBase::matrix &force, const ModuleBase::matrix &stress, std::ofstream &ofs_running)
Definition relax_sync.cpp:109
double e1ord2
Definition relax_sync.h:139
Relax()
Definition relax_sync.h:18
double fac_force
Definition relax_sync.h:153
double dmove
Definition relax_sync.h:142
double grp_grp
Definition relax_sync.h:136
const std::vector< double > & get_max_force_history() const
Read-only observers of the per-step convergence history, for the final summary.
Definition relax_sync.h:167
void update_lattice(UnitCell &ucell, double fac, bool is_new_dir)
Definition relax_sync.cpp:593
double etot_p
Definition relax_sync.h:146
bool if_cell_moves
Definition relax_sync.h:118
void update_ion_positions(UnitCell &ucell, double fac, std::ofstream &ofs_running)
Definition relax_sync.cpp:663
void setup_cell_gradient(const UnitCell &ucell, const ModuleBase::matrix &stress, bool &force_converged, std::ofstream &ofs_running)
Definition relax_sync.cpp:218
bool ltrial
Definition relax_sync.h:101
double sr_sr
Definition relax_sync.h:132
std::vector< double > max_force_history_
Largest force of each step (eV/Angstrom), includes trial and line-search steps.
Definition relax_sync.h:161
void new_direction(std::ofstream &ofs_running)
Definition relax_sync.cpp:436
void calculate_gamma()
Definition relax_sync.cpp:328
double setup_ion_gradient(const UnitCell &ucell, const ModuleBase::matrix &force, bool &force_converged, std::ofstream &ofs_running)
Definition relax_sync.cpp:146
Provide the basic information about unitcell.
Definition unitcell.h:16
Definition input_parameter.h:13