ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
operator_fs_utils.hpp
Go to the documentation of this file.
1#ifndef OPERATOR_FS_UTILS_HPP
2#define OPERATOR_FS_UTILS_HPP
3
4#include "operator_fs_utils.h"
8#include "source_base/timer.h"
11
12class Parallel_Blkcyclic; // Defined in source/source_base/parallel_blkcyc.h
13
14namespace OperatorForceStress {
15
41template <typename TK, typename TR, typename IntegralFunc, int ForceSign, int StressSign>
43 const bool cal_force,
44 const bool cal_stress,
46 const UnitCell* ucell,
47 const Grid_Driver* gridD,
48 const std::vector<double>& orb_cutoff,
49 const BasisFlattenedIndexing& basis,
50 const Parallel_Blkcyclic& layout,
51 IntegralFunc& integral_calculator,
52 ModuleBase::matrix& force,
53 ModuleBase::matrix& stress)
54{
55 const int npol = ucell->get_npol();
56 std::vector<double> stress_tmp(6, 0);
57
58 if (cal_force)
59 {
60 force.zero_out();
61 }
62
63 // Loop over all atom pairs and calculate force/stress contributions
64 #pragma omp parallel
65 {
66 std::vector<double> stress_local(6, 0);
67 ModuleBase::matrix force_local(force.nr, force.nc);
68
69 #pragma omp for schedule(dynamic)
70 for (int iat1 = 0; iat1 < ucell->nat; iat1++)
71 {
72 auto tau1 = ucell->get_tau(iat1);
73 int T1 = 0, I1 = 0;
74 ucell->iat2iait(iat1, &I1, &T1);
75 Atom& atom1 = ucell->atoms[T1];
76 const AtomBasisView rows = AtomBasisView::rows(basis, layout, iat1);
77 if (rows.empty())
78 {
79 continue;
80 }
81
82 // Find adjacent atoms
84 gridD->Find_atom(*ucell, tau1, T1, I1, &adjs);
85
86 for (int ad = 0; ad < adjs.adj_num + 1; ++ad)
87 {
88 const int T2 = adjs.ntype[ad];
89 const int I2 = adjs.natom[ad];
90 const int iat2 = ucell->itia2iat(T2, I2);
91 const ModuleBase::Vector3<int>& R_index = adjs.box[ad];
92
93 // Check cutoff
94 ModuleBase::Vector3<double> dtau = ucell->cal_dtau(iat1, iat2, R_index);
95 if (dtau.norm() * ucell->lat0 >= orb_cutoff[T1] + orb_cutoff[T2])
96 {
97 continue;
98 }
99
100 // Find density matrix for this atom pair
101 const hamilt::BaseMatrix<double>* dm_matrix = dmR->find_matrix(iat1, iat2, R_index[0], R_index[1], R_index[2]);
102 if (dm_matrix == nullptr)
103 {
104 continue;
105 }
106
107 // Calculate force and stress for this atom pair
108 double* force_tmp1 = (cal_force) ? &force_local(iat1, 0) : nullptr;
109 double* force_tmp2 = (cal_force) ? &force_local(iat2, 0) : nullptr;
110
111 Atom& atom2 = ucell->atoms[T2];
112 const AtomBasisView cols = AtomBasisView::cols(basis, layout, iat2);
113
114 if (cols.empty())
115 {
116 continue;
117 }
118
119 const double* dm_pointer = dm_matrix->get_pointer();
120 double olm[4] = {0, 0, 0, 0}; // value, dx, dy, dz
121
122 // step_trace = 0 for npol=1; ={0, 1, ncol, ncol+1} for npol=2
123 const int ncol = cols.nloc();
124 const int ncol_stride = ncol + 1;
125 std::vector<int> step_trace(npol * npol, 0);
126 if (npol == 2)
127 {
128 step_trace[1] = 1;
129 step_trace[2] = ncol;
130 step_trace[3] = ncol_stride;
131 }
132
133 // Loop over orbital pairs
134 for (int iw1l = 0; iw1l < rows.nloc(); iw1l += npol)
135 {
136 const int iw1 = rows[iw1l] / npol;
137 const int L1 = atom1.iw2l[iw1];
138 const int N1 = atom1.iw2n[iw1];
139 const int m1 = atom1.iw2m[iw1];
140 const int M1 = (m1 % 2 == 0) ? -m1 / 2 : (m1 + 1) / 2;
141
142 for (int iw2l = 0; iw2l < ncol; iw2l += npol)
143 {
144 const int iw2 = cols[iw2l] / npol;
145 const int L2 = atom2.iw2l[iw2];
146 const int N2 = atom2.iw2n[iw2];
147 const int m2 = atom2.iw2m[iw2];
148 const int M2 = (m2 % 2 == 0) ? -m2 / 2 : (m2 + 1) / 2;
149
150 // Calculate integral and its gradient using provided functor
151 integral_calculator(T1, L1, N1, M1, T2, L2, N2, M2, dtau, olm);
152
153 // only charge should be considered
154 const double dm_current = dm_pointer[0];
155
156 // Calculate force contribution with compile-time sign
157 if (cal_force)
158 {
159 // Factor of 2 for Hermitian matrix will be applied later
160 for (int i = 0; i < 3; i++)
161 {
162 force_tmp1[i] += ForceSign * dm_current * olm[i + 1];
163 force_tmp2[i] -= ForceSign * dm_current * olm[i + 1];
164 }
165 }
166
167 // Calculate stress contribution with compile-time sign
168 if (cal_stress)
169 {
170 stress_local[0] += StressSign * dm_current * olm[1] * dtau.x; // xx
171 stress_local[1] += StressSign * dm_current * olm[1] * dtau.y; // xy
172 stress_local[2] += StressSign * dm_current * olm[1] * dtau.z; // xz
173 stress_local[3] += StressSign * dm_current * olm[2] * dtau.y; // yy
174 stress_local[4] += StressSign * dm_current * olm[2] * dtau.z; // yz
175 stress_local[5] += StressSign * dm_current * olm[3] * dtau.z; // zz
176 }
177
178 dm_pointer += npol;
179 }
180 dm_pointer += (npol - 1) * ncol;
181 }
182 }
183 }
184
185 #pragma omp critical
186 {
187 if (cal_force)
188 {
189 force += force_local;
190 }
191 if (cal_stress)
192 {
193 for (int i = 0; i < 6; i++)
194 {
195 stress_tmp[i] += stress_local[i];
196 }
197 }
198 }
199 }
200
201 // Finalize with MPI reduction and post-processing
202 finalize_force_stress(cal_force, cal_stress, ucell, stress_tmp, force, stress, 1.0, 1.0);
203}
204
205} // namespace OperatorForceStress
206
207#endif // OPERATOR_FS_UTILS_HPP
Struct of array for packing the Adjacent atom information.
Definition sltk_grid_driver.h:21
int adj_num
number of adjacent atoms
Definition sltk_grid_driver.h:30
std::vector< ModuleBase::Vector3< int > > box
box indices of adjacent atoms
Definition sltk_grid_driver.h:34
std::vector< int > ntype
types of adjacent atoms
Definition sltk_grid_driver.h:31
std::vector< int > natom
indices of adjacent atoms
Definition sltk_grid_driver.h:32
Atom-local basis entries derived from basis indexing and distribution.
Definition atom_basis_view.h:16
bool empty() const
Definition atom_basis_view.h:30
int nloc() const
Definition atom_basis_view.h:25
static AtomBasisView cols(const BasisFlattenedIndexing &basis, const Parallel_Blkcyclic &layout, int iat)
Definition atom_basis_view.cpp:72
static AtomBasisView rows(const BasisFlattenedIndexing &basis, const Parallel_Blkcyclic &layout, int iat)
Definition atom_basis_view.cpp:65
Atom class for storing atom information.
Definition atom_spec.h:14
std::vector< int > iw2m
use iw to find m
Definition atom_spec.h:31
std::vector< int > iw2l
use iw to find L
Definition atom_spec.h:33
std::vector< int > iw2n
use iw to find n
Definition atom_spec.h:32
Canonical atom-major flattening of the LCAO basis hierarchy.
Definition basis_flattened_indexing.h:40
Grid_Driver class for neighbor search interface.
Definition sltk_grid_driver.h:63
void Find_atom(const UnitCell &ucell, const int ntype, const int nnumber, AdjacentAtomInfo *adjs=nullptr) const
Find adjacent atoms for a given atom.
Definition sltk_grid_driver.cpp:24
3 elements vector
Definition vector3.h:21
T x
Definition vector3.h:23
T norm(void) const
Get the norm of a Vector3.
Definition vector3.h:213
T y
Definition vector3.h:24
T z
Definition vector3.h:25
Definition matrix.h:15
void zero_out(void)
Definition matrix.cpp:312
int nr
Definition matrix.h:19
int nc
Definition matrix.h:20
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
Provide the basic information about unitcell.
Definition unitcell.h:16
Atom * atoms
Definition unitcell.h:165
const int & get_npol() const
Get npol.
Definition unitcell.h:66
const ModuleBase::Vector3< double > & get_tau(const int &iat) const
Get tau for atom iat.
Definition unitcell.h:104
double & lat0
Definition unitcell.h:135
ModuleBase::IntArray itia2iat
(it, ia)==>iat, the index in nat (add 2009-3-2 by mohan)
Definition unitcell.h:160
int nat
total number of atoms of all species in unitcell
Definition unitcell.h:155
const ModuleBase::Vector3< double > cal_dtau(const int &iat1, const int &iat2, const ModuleBase::Vector3< int > &R) const
Calculate vector between two atoms with R cell.
Definition unitcell.h:110
bool iat2iait(const Tiat iat, Tiait *ia, Tiait *it) const
Indexing tools for ia and it.
Definition unitcell.h:79
Definition base_matrix.h:20
T * get_pointer() const
get pointer of value from a submatrix
Definition base_matrix.h:86
Definition hcontainer.h:147
BaseMatrix< T > * find_matrix(int i, int j, int rx, int ry, int rz)
find BaseMatrix with atom index atom_i and atom_j and R index (rx, ry, rz) This interface can be used...
Definition hcontainer.cpp:426
Definition operator_fs_utils.cpp:4
void cal_force_stress_2center(const bool cal_force, const bool cal_stress, const hamilt::HContainer< double > *dmR, const UnitCell *ucell, const Grid_Driver *gridD, const std::vector< double > &orb_cutoff, const BasisFlattenedIndexing &basis, const Parallel_Blkcyclic &layout, IntegralFunc &integral_calculator, ModuleBase::matrix &force, ModuleBase::matrix &stress)
Template function for calculating force and stress from 2-center integrals.
Definition operator_fs_utils.hpp:42
void finalize_force_stress(bool cal_force, bool cal_stress, const UnitCell *ucell, const std::vector< double > &stress_tmp, ModuleBase::matrix &force, ModuleBase::matrix &stress, double force_factor, double stress_factor)
Finalize force and stress calculations with MPI reduction and post-processing.
Definition operator_fs_utils.cpp:6
Grid_Driver class for neighbor search interface.