11 const bool cal_stress,
16 if (this->dftu->get_dmr(0) ==
nullptr)
23 dmR_tmp[0] = this->dftu->get_dmr(0);
27 dmR_tmp[1] = this->dftu->get_dmr(1);
29 if (dmR_tmp[0]->size_atom_pairs() == 0)
38 const int npol = this->ucell->get_npol();
39 std::vector<double> stress_tmp(6, 0);
46 std::vector<int> atom_index_all(this->ucell->nat, -1);
47 for (
int iat0 = 0; iat0 < this->ucell->nat; iat0++)
51 ucell->iat2iait(iat0, &I0, &T0);
56 atom_index_all[iat0] = atom_index;
64 std::vector<double> stress_local(6, 0);
66 #pragma omp for schedule(dynamic)
67 for (
int iat0 = 0; iat0 < this->ucell->nat; iat0++)
70 auto tau0 = ucell->get_tau(iat0);
73 ucell->iat2iait(iat0, &I0, &T0);
79 const int tlp1 = 2 * target_L + 1;
82 std::vector<std::unordered_map<int, std::vector<double>>> nlm_tot;
83 nlm_tot.resize(adjs.
adj_num + 1);
85 for (
int ad = 0; ad < adjs.
adj_num + 1; ++ad)
87 const int T1 = adjs.
ntype[ad];
88 const int I1 = adjs.
natom[ad];
89 const int iat1 = ucell->itia2iat(T1, I1);
91 const Atom* atom1 = &ucell->atoms[T1];
96 all_indexes.insert(all_indexes.end(), col_indexes.begin(), col_indexes.end());
97 std::sort(all_indexes.begin(), all_indexes.end());
98 all_indexes.erase(std::unique(all_indexes.begin(), all_indexes.end()), all_indexes.end());
99 for (
int iw1l = 0; iw1l < all_indexes.size(); iw1l += npol)
101 const int iw1 = all_indexes[iw1l] / npol;
102 std::vector<std::vector<double>> nlm;
107 int L1 = atom1->
iw2l[iw1];
108 int N1 = atom1->
iw2n[iw1];
109 int m1 = atom1->
iw2m[iw1];
112 int M1 = (m1 % 2 == 0) ? -m1 / 2 : (m1 + 1) / 2;
115 intor_->snap(T1, L1, N1, M1, T0, dtau * this->ucell->lat0, 1 , nlm);
118 std::vector<double> nlm_target(tlp1 * 4);
119 for (
int iw = 0; iw < this->ucell->atoms[T0].nw; iw++)
121 const int L0 = this->ucell->atoms[T0].iw2l[iw];
124 for (
int m = 0; m < tlp1; m++)
126 for (
int n = 0; n < 4; n++)
128 nlm_target[m + n * tlp1] = nlm[n][iw + m];
137 nlm_tot[ad].insert({all_indexes[iw1l], nlm_target});
141 std::vector<double> occ(tlp1 * tlp1 * this->nspin, 0);
144 for (
int i = 0; i < occ.size(); i++)
146 const int is = i / (tlp1 * tlp1);
147 const int ii = i % (tlp1 * tlp1);
148 occ[i] = this->dftu->
locale[iat0][target_L][0][is].c[ii];
153 for (
int i = 0; i < occ.size(); i++)
155 occ[i] = this->dftu->
locale[iat0][target_L][0][0].c[i];
160 const double u_value = this->dftu->
U[T0];
161 std::vector<double> VU(occ.size());
163 this->cal_v_of_u(occ, tlp1, u_value, &VU[0], eu_tmp);
166 for (
int i = 0; i < VU.size(); i++)
181 for (
int ad1 = 0; ad1 < adjs.
adj_num + 1; ++ad1)
183 const int T1 = adjs.
ntype[ad1];
184 const int I1 = adjs.
natom[ad1];
185 const int iat1 = ucell->itia2iat(T1, I1);
186 double* force_tmp1 = (cal_force) ? &force_local(iat1, 0) :
nullptr;
187 double* force_tmp2 = (cal_force) ? &force_local(iat0, 0) :
nullptr;
190 for (
int ad2 = 0; ad2 < adjs.
adj_num + 1; ++ad2)
192 const int T2 = adjs.
ntype[ad2];
193 const int I2 = adjs.
natom[ad2];
194 const int iat2 = ucell->itia2iat(T2, I2);
198 R_index2[1] - R_index1[1],
199 R_index2[2] - R_index1[2]);
201 tmp[0] = dmR_tmp[0]->
find_matrix(iat1, iat2, R_vector[0], R_vector[1], R_vector[2]);
202 if (this->nspin == 2)
204 tmp[1] = dmR_tmp[1]->
find_matrix(iat1, iat2, R_vector[0], R_vector[1], R_vector[2]);
207 if (tmp[0] !=
nullptr)
211 this->cal_force_IJR(iat1,
225 this->cal_stress_IJR(iat1,
235 stress_local.data());
245 force += force_local;
249 for(
int i = 0; i < 6; i++)
251 stress_tmp[i] += stress_local[i];
263 for (
int i = 0; i < force.nr * force.nc; i++)
276 const double weight = this->ucell->lat0 / this->ucell->omega;
277 for (
int i = 0; i < 6; i++)
279 stress.
c[i] = stress_tmp[i] * weight;
281 stress.
c[8] = stress.
c[5];
282 stress.
c[7] = stress.
c[4];
283 stress.
c[6] = stress.
c[2];
284 stress.
c[5] = stress.
c[4];
285 stress.
c[4] = stress.
c[3];
286 stress.
c[3] = stress.
c[1];
297 const std::unordered_map<
int, std::vector<double>>& nlm1_all,
298 const std::unordered_map<
int, std::vector<double>>& nlm2_all,
299 const std::vector<double>& vu_in,
308 const int npol = this->ucell->get_npol();
314 const int m_size = int(sqrt(vu_in.size() / nspin));
315 const int m_size2 = m_size * m_size;
318 std::vector<int> step_trace(npol * npol, 0);
323 step_trace[2] = col_indexes.size();
324 step_trace[3] = col_indexes.size() + 1;
329 for (
int is = 0; is < nspin; is++)
331 const int is0 = nspin==2 ? is : 0;
332 const int step_is = nspin==4 ? is : 0;
333 const double* dm_pointer = dmR_pointer[is0]->
get_pointer();
334 for (
int iw1l = 0; iw1l < row_indexes.size(); iw1l += npol)
336 const std::vector<double>& nlm1 = nlm1_all.find(row_indexes[iw1l])->second;
337 for (
int iw2l = 0; iw2l < col_indexes.size(); iw2l += npol)
339 const std::vector<double>& nlm2 = nlm2_all.find(col_indexes[iw2l])->second;
341 assert(nlm1.size() == nlm2.size());
343 for (
int m1 = 0; m1 < m_size; m1++)
345 for (
int m2 = 0; m2 < m_size; m2++)
347 tmp[0] = vu_in[m1 * m_size + m2 + is * m_size2] * nlm1[m1 + m_size]
348 * nlm2[m2] * dm_pointer[step_trace[step_is]];
349 tmp[1] = vu_in[m1 * m_size + m2 + is * m_size2] * nlm1[m1 + m_size * 2]
350 * nlm2[m2] * dm_pointer[step_trace[step_is]];
351 tmp[2] = vu_in[m1 * m_size + m2 + is * m_size2] * nlm1[m1 + m_size * 3]
352 * nlm2[m2] * dm_pointer[step_trace[step_is]];
365 dm_pointer += (npol - 1) * col_indexes.size();
374 const std::unordered_map<
int, std::vector<double>>& nlm1_all,
375 const std::unordered_map<
int, std::vector<double>>& nlm2_all,
376 const std::vector<double>& vu_in,
386 const int npol = this->ucell->get_npol();
392 const int m_size = int(sqrt(vu_in.size() / nspin));
393 const int m_size2 = m_size * m_size;
396 std::vector<int> step_trace(npol * npol, 0);
401 step_trace[2] = col_indexes.size();
402 step_trace[3] = col_indexes.size() + 1;
406 for (
int is = 0; is < nspin; is++)
408 const int is0 = nspin==2 ? is : 0;
409 const int step_is = nspin==4 ? is : 0;
410 const double* dm_pointer = dmR_pointer[is0]->
get_pointer();
411 for (
int iw1l = 0; iw1l < row_indexes.size(); iw1l += npol)
413 const std::vector<double>& nlm1 = nlm1_all.find(row_indexes[iw1l])->second;
414 for (
int iw2l = 0; iw2l < col_indexes.size(); iw2l += npol)
416 const std::vector<double>& nlm2 = nlm2_all.find(col_indexes[iw2l])->second;
418 assert(nlm1.size() == nlm2.size());
420 for (
int m1 = 0; m1 < m_size; m1++)
422 for (
int m2 = 0; m2 < m_size; m2++)
424 double tmp = vu_in[m1 * m_size + m2 + is * m_size2] * dm_pointer[step_trace[step_is]];
427 stress[0] += tmp * (nlm1[m1 + m_size] * dis1.
x * nlm2[m2]
428 + nlm1[m1] * nlm2[m2 + m_size] * dis2.
x);
429 stress[1] += tmp * (nlm1[m1 + m_size] * dis1.
y * nlm2[m2]
430 + nlm1[m1] * nlm2[m2 + m_size] * dis2.
y);
431 stress[2] += tmp * (nlm1[m1 + m_size] * dis1.
z * nlm2[m2]
432 + nlm1[m1] * nlm2[m2 + m_size] * dis2.
z);
434 stress[3] += tmp * (nlm1[m1 + m_size * 2] * dis1.
y * nlm2[m2]
435 + nlm1[m1] * nlm2[m2 + m_size * 2] * dis2.
y);
436 stress[4] += tmp * (nlm1[m1 + m_size * 2] * dis1.
z * nlm2[m2]
437 + nlm1[m1] * nlm2[m2 + m_size * 2] * dis2.
z);
438 stress[5] += tmp * (nlm1[m1 + m_size * 3] * dis1.
z * nlm2[m2]
439 + nlm1[m1] * nlm2[m2 + m_size * 3] * dis2.
z);
444 dm_pointer += (npol - 1) * col_indexes.size();