ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
exx_lri.hpp
Go to the documentation of this file.
1//=======================
2// AUTHOR : Peize Lin
3// DATE : 2022-08-17
4//=======================
5
6#ifndef EXX_LRI_HPP
7#define EXX_LRI_HPP
8
9#include "lri_cv_tools.h"
10#include "exx_lri.h"
11#include "hybrid_image_domain.h"
15#include "ri_2d_comm.h"
16#include "ri_util.h"
17#include "exx_lri_detail.h"
22#include "source_base/timer.h"
26
27#include <RI/distribute/Distribute_Equally.h>
28#include <RI/global/Map_Operator-3.h>
29
30#include <fstream>
31#include <stdexcept>
32#include <string>
33
34#if defined(__GLIBC__)
35#include <malloc.h>
36#endif
37
38template<typename Tdata>
39void Exx_LRI<Tdata>::init(const MPI_Comm &mpi_comm_in,
40 const UnitCell &ucell,
41 const K_Vectors &kv_in,
42 const LCAO_Orbitals& orb,
43 const std::vector<std::vector<std::vector<Numerical_Orbital_Lm>>>& abfs_in)
44{
45 this->init(
46 mpi_comm_in,
47 ucell,
48 kv_in,
49 orb,
50 abfs_in,
51 false);
52}
53
54template <typename Tdata>
56 const MPI_Comm &mpi_comm_in,
57 const UnitCell &ucell,
58 const K_Vectors &kv_in,
59 const LCAO_Orbitals &orb,
60 const std::vector<std::vector<std::vector<Numerical_Orbital_Lm>>> &abfs_in,
61 const bool use_gpu)
62{
63 ModuleBase::TITLE("Exx_LRI","init");
64 ModuleBase::timer::start("Exx_LRI", "init");
65
66 this->mpi_comm = mpi_comm_in;
67 this->p_kv = &kv_in;
68 this->orb_cutoff_ = orb.cutoffs();
69
70 this->lcaos = Exx_Abfs::Construct_Orbs::change_orbs( orb, this->info.kmesh_times );
72
73 if(abfs_in.empty())
74 {
75 const std::vector<std::vector<std::vector<Numerical_Orbital_Lm>>>
76 abfs_same_atom = Exx_Abfs::Construct_Orbs::abfs_same_atom(ucell, orb, this->lcaos, this->info.kmesh_times, this->info.pca_threshold );
77 if(this->info.files_abfs.empty())
78 { this->abfs = abfs_same_atom;}
79 else
80 { this->abfs = Exx_Abfs::IO::construct_abfs( abfs_same_atom, orb, this->info.files_abfs, this->info.kmesh_times ); }
81 }
82 else
83 {
84 this->abfs = abfs_in;
85 }
88
89 for( size_t T=0; T!=this->abfs.size(); ++T )
90 {
91 this->abfs_Lmax_ = std::max(this->abfs_Lmax_, static_cast<int>(this->abfs[T].size())-1);
92 }
93
94 this->exx_objs.clear();
95 const auto settings = RI_Util::update_coulomb_settings(this->info.coulomb_param, ucell, this->p_kv);
96 this->MGT = std::make_shared<ORB_gaunt_table>();
97 this->refresh_coulomb(
98 ucell,
99 orb,
100 settings,
101 use_gpu);
102
103 // The radial interpolation tables now own the integrals used by EXX.
104 // Retain the Bessel objects for existing consumers, but release their cache.
105 std::size_t workspace_bytes = 0;
107 {
108 workspace_bytes += table.release_workspace();
109 }
110 std::cout << " Bessel table workspace released (bytes): " << workspace_bytes << std::endl;
111
112 ModuleBase::timer::end("Exx_LRI", "init");
113}
114
115template <typename Tdata>
117 const UnitCell &ucell,
118 const LCAO_Orbitals &orb,
119 const CoulombSettings &settings,
120 const bool use_gpu)
121{
122 ModuleBase::TITLE("Exx_LRI", "refresh_coulomb");
123 ModuleBase::timer::start("Exx_LRI", "refresh_coulomb");
124
125 for (auto object = this->exx_objs.begin(); object != this->exx_objs.end();)
126 {
127 if (settings.count(object->first) == 0)
128 {
129 object = this->exx_objs.erase(object);
130 }
131 else
132 {
133 ++object;
134 }
135 }
136 for (const auto& settings_list : settings)
137 {
138 const auto previous = this->coulomb_settings.find(settings_list.first);
139 const bool unchanged = previous != this->coulomb_settings.end()
140 && previous->second == settings_list.second;
141 if (unchanged && this->exx_objs.count(settings_list.first) != 0)
142 {
143 continue;
144 }
145
146 // Discard interpolation and point-value caches belonging to the old kernel.
147 // In particular, reusing Matrix_Orbs maps would retain old entries inserted by init().
148 this->exx_objs.erase(settings_list.first);
149 auto& object = this->exx_objs[settings_list.first];
150 object.abfs_ccp = Conv_Coulomb_Pot_K::cal_orbs_ccp(
151 this->abfs, settings_list.second.second, this->info.ccp_rmesh_times);
152 object.cv.set_orbitals(
153 ucell,
154 orb,
155 this->lcaos,
156 this->abfs,
157 object.abfs_ccp,
158 this->info.kmesh_times,
159 this->MGT,
160 settings_list.second.first,
161 use_gpu);
162 object.cv.set_info_ri(&this->info);
163 if (settings_list.first == Conv_Coulomb_Pot_K::Coulomb_Method::Ewald)
164 {
165 const int evq_abfs_Lmax = this->abfs_Lmax_;
166 object.evq.init(ucell, orb, this->mpi_comm, this->p_kv, this->lcaos, this->abfs,
167 settings_list.second.second, this->MGT, this->info.ccp_rmesh_times,
168 this->info.kmesh_times, evq_abfs_Lmax);
169 }
170 }
171 this->coulomb_settings = settings;
172
173 ModuleBase::timer::end("Exx_LRI", "refresh_coulomb");
174}
175
176template <typename Tdata>
178 const MPI_Comm& mpi_comm_in,
179 const UnitCell& ucell,
180 const K_Vectors& kv_in,
181 const LCAO_Orbitals& orb,
182 const std::vector<std::vector<std::vector<Numerical_Orbital_Lm>>>& abfs_in)
183{
184 ModuleBase::TITLE("Exx_LRI", "init_spencer");
185 ModuleBase::timer::start("Exx_LRI", "init_spencer");
186
187 this->mpi_comm = mpi_comm_in;
188 this->p_kv = &kv_in;
189 this->orb_cutoff_ = orb.cutoffs();
190
191 this->lcaos = Exx_Abfs::Construct_Orbs::change_orbs(orb, this->info.kmesh_times);
193
194 if (abfs_in.empty())
195 {
196 const std::vector<std::vector<std::vector<Numerical_Orbital_Lm>>> abfs_same_atom
198 ucell, orb, this->lcaos, this->info.kmesh_times, this->info.pca_threshold);
199 if (this->info.files_abfs.empty())
200 { this->abfs = abfs_same_atom; }
201 else
202 { this->abfs = Exx_Abfs::IO::construct_abfs(abfs_same_atom, orb, this->info.files_abfs, this->info.kmesh_times); }
203 }
204 else
205 {
206 this->abfs = abfs_in;
207 }
210
211 for (size_t T = 0; T != this->abfs.size(); ++T)
212 {
213 this->abfs_Lmax_ = std::max(this->abfs_Lmax_, static_cast<int>(this->abfs[T].size()) - 1);
214 }
215
216 this->exx_objs.clear();
217 this->coulomb_settings.clear();
219 true, ExxLriDetail::build_center2_cut_coulomb_param(this->info.coulomb_param, ucell, kv_in));
220
221 this->MGT = std::make_shared<ORB_gaunt_table>();
222 const auto center2_settings = this->coulomb_settings.find(Conv_Coulomb_Pot_K::Coulomb_Method::Center2);
223 if (center2_settings == this->coulomb_settings.end())
224 { throw std::invalid_argument("Exx_LRI::init_spencer failed to prepare Center2 settings."); }
225
227 this->abfs,
228 center2_settings->second.second,
229 this->info.ccp_rmesh_times);
231 ucell,
232 orb,
233 this->lcaos,
234 this->abfs,
236 this->info.kmesh_times,
237 this->MGT,
238 center2_settings->second.first,
239 false);
240 this->exx_objs[Conv_Coulomb_Pot_K::Coulomb_Method::Center2].cv.set_info_ri(&this->info);
241
242 ModuleBase::timer::end("Exx_LRI", "init_spencer");
243}
244
245
246template<typename Tdata>
248 const bool write_cv)
249{
250 ModuleBase::TITLE("Exx_LRI","cal_exx_ions");
251 ModuleBase::timer::start("Exx_LRI", "cal_exx_ions");
252
253 // init_radial_table_ions( cal_atom_centres_core(atom_pairs_core_origin), atom_pairs_core_origin );
254
255 // this->m_abfsabfs.init_radial_table(Rradial);
256 // this->m_abfslcaos_lcaos.init_radial_table(Rradial);
257
258 std::vector<TA> atoms(ucell.nat);
259 for(int iat=0; iat<ucell.nat; ++iat)
260 { atoms[iat] = iat; }
261 std::map<TA,TatomR> atoms_pos;
262 for(int iat=0; iat<ucell.nat; ++iat)
263 { atoms_pos[iat] = RI_Util::Vector3_to_array3( ucell.atoms[ ucell.iat2it[iat] ].tau[ ucell.iat2ia[iat] ] ); }
264 const std::array<TatomR,Ndim> latvec
268 const std::array<Tcell,Ndim> period = {this->p_kv->nmp[0], this->p_kv->nmp[1], this->p_kv->nmp[2]};
269
270 this->exx_lri.set_parallel(this->mpi_comm, atoms_pos, latvec, period);
271
272 // std::max(3) for gamma_only, list_A2 should contain cell {-1,0,1}. In the future distribute will be neighbour.
273 const std::array<Tcell,Ndim> period_Vs = LRI_CV_Tools::cal_latvec_range<Tcell>(1+this->info.ccp_rmesh_times, ucell, orb_cutoff_);
274 const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>>
275 list_As_Vs = RI::Distribute_Equally::distribute_atoms_periods(this->mpi_comm, atoms, period_Vs, 2, false);
276
277 std::map<TA,std::map<TAC,RI::Tensor<Tdata>>> Vs;
278 std::map<TA, std::map<TAC, std::array<RI::Tensor<Tdata>, Ndim>>> dVs;
279 const bool calculate_stress = PARAM.inp.cal_stress;
280 const bool cal_dCV = (PARAM.inp.cal_force || calculate_stress) && !this->info.unfold_hybrid_images;
281 if (this->info.unfold_hybrid_images && calculate_stress)
282 {
283 throw std::invalid_argument("Unfolded hybrid RI stress is not yet implemented");
284 }
285 for(const auto &settings_list : this->coulomb_settings)
286 {
287 std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>
288 Vs_temp = this->exx_objs[settings_list.first].cv.cal_Vs(ucell,
289 list_As_Vs.first, list_As_Vs.second[0],
290 {{"writable_Vws",true}});
291 this->exx_objs[settings_list.first].cv.Vws = LRI_CV_Tools::get_CVws(ucell,Vs_temp);
292 if (settings_list.first == Conv_Coulomb_Pot_K::Coulomb_Method::Ewald)
293 {
294 this->exx_objs[settings_list.first].evq.init_ions(ucell, period_Vs);
295 const auto &coulomb_param = settings_list.second.second;
296 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>> Vs_ewald;
297 for(const auto &param_list : coulomb_param)
298 {
299 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>> Vs_ewald_temp;
300 switch(param_list.first)
301 {
303 {
304 double chi = this->exx_objs[settings_list.first].evq.get_singular_chi(ucell, param_list.second, 2.0);
305 Vs_ewald_temp = this->exx_objs[settings_list.first].evq.cal_Vs(ucell, chi, Vs_temp);
306 break;
307 }
308 default:
309 {
310 throw std::invalid_argument( std::string(__FILE__) + " line " + std::to_string(__LINE__) );
311 }
312 }
313 // Vs_temp cannot be covered here
314 Vs_ewald = Vs_ewald.empty() ? Vs_ewald_temp : LRI_CV_Tools::add(Vs_ewald, Vs_ewald_temp);
315 }
316 Vs_temp = Vs_ewald;
317 }
318 Vs = Vs.empty() ? Vs_temp : LRI_CV_Tools::add(Vs, Vs_temp);
319
320 if (cal_dCV)
321 {
322 std::map<TA, std::map<TAC, std::array<RI::Tensor<Tdata>, Ndim>>>
323 dVs_temp = this->exx_objs[settings_list.first].cv.cal_dVs(ucell,
324 list_As_Vs.first, list_As_Vs.second[0],
325 {{"writable_dVws",true}});
326 this->exx_objs[settings_list.first].cv.dVws = LRI_CV_Tools::get_dCVws(ucell,dVs_temp);
327 dVs = dVs.empty() ? dVs_temp : LRI_CV_Tools::add(dVs, dVs_temp);
328 }
329 }
330 if (write_cv && GlobalV::MY_RANK == 0)
331 { LRI_CV_Tools::write_Vs_abf(Vs, PARAM.globalv.global_out_dir + "Vs"); }
332 if (!this->info.unfold_hybrid_images)
333 {
334 this->exx_lri.set_Vs(std::move(Vs), this->info.V_threshold);
335 }
336
337 if (cal_dCV)
338 {
339 std::array<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>, Ndim>
340 dVs_order = LRI_CV_Tools::change_order(std::move(dVs));
341 this->exx_lri.set_dVs(std::move(dVs_order), this->info.V_grad_threshold);
342 if(calculate_stress)
343 {
344 std::array<std::array<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>,3>,3> dVRs = LRI_CV_Tools::cal_dMRs(ucell,dVs_order);
345 this->exx_lri.set_dVRs(std::move(dVRs), this->info.V_grad_R_threshold);
346 }
347 }
348
349 const std::array<Tcell,Ndim> period_Cs = LRI_CV_Tools::cal_latvec_range<Tcell>(2, ucell,orb_cutoff_);
350 const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>>
351 list_As_Cs = RI::Distribute_Equally::distribute_atoms_periods(this->mpi_comm, atoms, period_Cs, 2, false);
352
353 std::map<TA,std::map<TAC,RI::Tensor<Tdata>>> Cs;
354 std::map<TA, std::map<TAC, std::array<RI::Tensor<Tdata>, 3>>> dCs;
355 for(const auto &settings_list : this->coulomb_settings)
356 {
357 if(settings_list.second.first)
358 {
359 std::pair<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>,
360 std::map<TA, std::map<TAC, std::array<RI::Tensor<Tdata>, 3>>>>
361 Cs_dCs = this->exx_objs[settings_list.first].cv.cal_Cs_dCs(
362 ucell,
363 list_As_Cs.first, list_As_Cs.second[0],
364 { {"cal_dC",cal_dCV},
365 {"writable_Cws",true}, {"writable_dCws",true}, {"writable_Vws",false}, {"writable_dVws",false}});
366 std::map<TA,std::map<TAC,RI::Tensor<Tdata>>> &Cs_temp = std::get<0>(Cs_dCs);
367 this->exx_objs[settings_list.first].cv.Cws = LRI_CV_Tools::get_CVws(ucell,Cs_temp);
368 Cs = Cs.empty() ? Cs_temp : LRI_CV_Tools::add(Cs, Cs_temp);
369
370 if (cal_dCV)
371 {
372 std::map<TA, std::map<TAC, std::array<RI::Tensor<Tdata>, 3>>> &dCs_temp = std::get<1>(Cs_dCs);
373 this->exx_objs[settings_list.first].cv.dCws = LRI_CV_Tools::get_dCVws(ucell,dCs_temp);
374 dCs = dCs.empty() ? dCs_temp : LRI_CV_Tools::add(dCs, dCs_temp);
375 }
376 }
377 }
378 if (write_cv && GlobalV::MY_RANK == 0)
379 { LRI_CV_Tools::write_Cs_ao(Cs, PARAM.globalv.global_out_dir + "Cs"); }
380 if (this->info.unfold_hybrid_images)
381 {
382 RI_2D_Comm::ImageCell c_extent = RI_2D_Comm::retained_image_extent(Cs, this->info.C_threshold);
383 RI_2D_Comm::ImageCell v_extent = RI_2D_Comm::retained_image_extent(Vs, this->info.V_threshold);
384 for (int d = 0; d < 3; ++d)
385 {
386 Parallel_Reduce::reduce_max(c_extent[d]);
387 Parallel_Reduce::reduce_max(v_extent[d]);
388 }
389 const RI_2D_Comm::ImageCell image_period = RI_2D_Comm::hybrid_image_period(c_extent, v_extent);
390 if (image_period != this->hybrid_images_period_
391 && RI::MPI_Wrapper::mpi_get_rank(this->mpi_comm) == 0)
392 {
393 std::cout << "RI_HYBRID_IMAGE_PERIOD " << image_period[0] << " " << image_period[1]
394 << " " << image_period[2] << " physical_mesh " << period[0] << " " << period[1]
395 << " " << period[2] << std::endl;
396 }
397 this->hybrid_images_period_ = image_period;
398 this->exx_lri.set_parallel(this->mpi_comm, atoms_pos, latvec, this->hybrid_images_period_);
399 this->exx_lri.set_Vs(Vs, this->info.V_threshold);
400 }
401 this->exx_lri.set_Cs(std::move(Cs), this->info.C_threshold);
402
403 if (cal_dCV)
404 {
405 std::array<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>, Ndim>
406 dCs_order = LRI_CV_Tools::change_order(std::move(dCs));
407 this->exx_lri.set_dCs(std::move(dCs_order), this->info.C_grad_threshold);
408 if(calculate_stress)
409 {
410 std::array<std::array<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>,3>,3> dCRs = LRI_CV_Tools::cal_dMRs(ucell,dCs_order);
411 this->exx_lri.set_dCRs(std::move(dCRs), this->info.C_grad_R_threshold);
412 }
413 }
414 // Electronic, force and stress contractions use LibRI's distributed tensors.
415 // The original integral lookup caches are no longer needed for this ionic step.
416 for (std::pair<const Conv_Coulomb_Pot_K::Coulomb_Method, Exx_Obj<Tdata>>& object : this->exx_objs)
417 {
418 object.second.cv.clear_cache();
419 }
420 ModuleBase::timer::end("Exx_LRI", "cal_exx_ions");
421}
422
423 #if 0
424 template <typename Tdata>
426 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>& Vs_cut,
427 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>& Cs,
428 const UnitCell& ucell,
429 const bool write_cv)
430{
431 ModuleBase::TITLE("Exx_LRI", "cal_cut_coulomb_cs");
432 ModuleBase::timer::start("Exx_LRI", "cal_cut_coulomb_cs");
433
434 std::vector<TA> atoms(ucell.nat);
435 for(int iat=0; iat<ucell.nat; ++iat)
436 atoms[iat] = iat;
437 std::map<TA,TatomR> atoms_pos;
438 for(int iat=0; iat<ucell.nat; ++iat)
439 atoms_pos[iat] = RI_Util::Vector3_to_array3( ucell.atoms[ucell.iat2it[iat]].tau[ucell.iat2ia[iat]] );
440 const std::array<TatomR,Ndim> latvec
444 const std::array<Tcell,Ndim> period = {this->p_kv->nmp[0], this->p_kv->nmp[1], this->p_kv->nmp[2]};
445
446 this->exx_lri.set_parallel(this->mpi_comm, atoms_pos, latvec, period);
447
448 // std::max(3) for gamma_only, list_A2 should contain cell {-1,0,1}. In the future distribute will be neighbour.
449 const std::array<Tcell, Ndim> period_Vs
450 = LRI_CV_Tools::cal_latvec_range<Tcell>(1 + this->info.ccp_rmesh_times, ucell, orb_cutoff_);
451 const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>>
452 list_As_Vs = RI::Distribute_Equally::distribute_atoms_periods(this->mpi_comm, atoms, period_Vs, 2, false);
453
454 std::map<TA, std::map<TAC, std::array<RI::Tensor<Tdata>, Ndim>>> dVs;
455 for(const auto &settings_list : this->coulomb_settings)
456 {
457 if(!settings_list.second.first) continue;
458 std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>
459 Vs_temp = this->exx_objs[settings_list.first].cv.cal_Vs(ucell,
460 list_As_Vs.first, list_As_Vs.second[0],
461 {{"writable_Vws",true}});
462 this->exx_objs[settings_list.first].cv.Vws = LRI_CV_Tools::get_CVws(ucell,Vs_temp);
463
464 // ======rotate ABFs begin======
465 int flag = 0;
466 for (const auto& IJRc: this->exx_objs[settings_list.first].cv.Vws)
467 {
468 const TA& I = IJRc.first;
469 const auto& JRc = IJRc.second;
470 for (const auto& JRc_tensor: JRc)
471 {
472 const TA& J = JRc_tensor.first;
473 const auto Rc = JRc_tensor.second;
474 for (const auto& Rc_tensor: Rc)
475 {
476 const auto& R = Rc_tensor.first;
477 flag += 1;
478 }
479 }
480 }
481 std::cout << "Coulomb: number of atom-pairs inside atomic overlap is " << flag << ". " << std::endl;
482 if (this->info.coul_moment == true)
483 {
484 double hf_Rcut = std::pow(0.75 * this->p_kv->get_nkstot_nospin() * ucell.omega / (ModuleBase::PI), 1.0 / 3.0);
485 // To cal Cs, we still cal all Vs(R) in r space
486 // moment_abfs->cal_VR(ucell,
487 // this->abfs,
488 // list_As_Vs,
489 // orb_cutoff_,
490 // hf_Rcut,
491 // this->exx_objs[settings_list.first].cv,
492 // Vs_cut);
493 delete moment_abfs;
494 moment_abfs = nullptr;
495 malloc_trim(0);
496 }
497
498 flag = 0;
499 for (const auto& IJRc: this->exx_objs[settings_list.first].cv.Vws)
500 {
501 const auto& JRc = IJRc.second;
502 for (const auto& JRc_tensor: JRc)
503 {
504 const auto Rc = JRc_tensor.second;
505 for (const auto& Rc_tensor: Rc)
506 {
507 flag += 1;
508 }
509 }
510 }
511 std::cout << "Coulomb: number of all atom-pairs is " << flag << ". " << std::endl;
512 // ======rotate ABFs end======
513
514 Vs_cut = Vs.empty() ? Vs_temp : LRI_CV_Tools::add(Vs_cut, Vs_temp);
515
516 if(PARAM.inp.cal_force || PARAM.inp.cal_stress)
517 {
518 std::map<TA, std::map<TAC, std::array<RI::Tensor<Tdata>, Ndim>>>
519 dVs_temp = this->exx_objs[settings_list.first].cv.cal_dVs(ucell,
520 list_As_Vs.first, list_As_Vs.second[0],
521 {{"writable_dVws",true}});
522 this->exx_objs[settings_list.first].cv.dVws = LRI_CV_Tools::get_dCVws(ucell,dVs_temp);
523 dVs = dVs.empty() ? dVs_temp : LRI_CV_Tools::add(dVs, dVs_temp);
524 }
525 }
526
527 if (write_cv && GlobalV::MY_RANK == 0)
528 {
529 LRI_CV_Tools::write_Vs_abf(Vs_cut, PARAM.globalv.global_out_dir + "Vs_cut");
530 }
531 this->exx_lri.set_Vs(std::move(Vs_cut), this->info.V_threshold);
532
533 if(PARAM.inp.cal_force || PARAM.inp.cal_stress)
534 {
535 std::map<TA,std::map<TAC,std::array<RI::Tensor<Tdata>,Ndim>>> dVs
536 = this->exx_objs[coulomb_method].cv.cal_dVs(ucell,
537 list_As_Vs.first, list_As_Vs.second[0],
538 {{"writable_dVws",true}});
539 this->exx_objs[coulomb_method].cv.dVws = LRI_CV_Tools::get_dCVws(ucell,dVs);
540
541 std::array<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>,Ndim> dVs_order
542 = LRI_CV_Tools::change_order(std::move(dVs));
543 this->exx_lri.set_dVs(std::move(dVs_order), this->info.V_grad_threshold);
544 if(PARAM.inp.cal_stress)
545 {
546 std::array<std::array<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>,3>,3> dVRs
547 = LRI_CV_Tools::cal_dMRs(ucell,dVs_order);
548 this->exx_lri.set_dVRs(std::move(dVRs), this->info.V_grad_R_threshold);
549 }
550 }
551
552 const std::array<Tcell,Ndim> period_Cs = LRI_CV_Tools::cal_latvec_range<Tcell>(2, ucell,orb_cutoff_);
553 const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>>
554 list_As_Cs = RI::Distribute_Equally::distribute_atoms_periods(this->mpi_comm, atoms, period_Cs, 2, false);
555 std::pair<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>,
556 std::map<TA,std::map<TAC,std::array<RI::Tensor<Tdata>,3>>>>
557 Cs_dCs = this->exx_objs[coulomb_method].cv.cal_Cs_dCs(ucell,
558 list_As_Cs.first, list_As_Cs.second[0],
559 {{"cal_dC",PARAM.inp.cal_force||PARAM.inp.cal_stress},
560 {"writable_Cws",true},
561 {"writable_dCws",true},
562 {"writable_Vws",false},
563 {"writable_dVws",false}});
564 Cs = std::get<0>(Cs_dCs);
565 this->exx_objs[coulomb_method].cv.Cws = LRI_CV_Tools::get_CVws(ucell,Cs);
566 if(write_cv && GlobalV::MY_RANK==0)
567 {
568 LRI_CV_Tools::write_Cs_ao(Cs, PARAM.globalv.global_out_dir + "Cs");
569 }
570 this->exx_lri.set_Cs(Cs, this->info.C_threshold);
571
572 if(PARAM.inp.cal_force || PARAM.inp.cal_stress)
573 {
574 std::map<TA,std::map<TAC,std::array<RI::Tensor<Tdata>,3>>>& dCs = std::get<1>(Cs_dCs);
575 this->exx_objs[coulomb_method].cv.dCws = LRI_CV_Tools::get_dCVws(ucell,dCs);
576 std::array<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>,Ndim> dCs_order
577 = LRI_CV_Tools::change_order(std::move(dCs));
578 this->exx_lri.set_dCs(std::move(dCs_order), this->info.C_grad_threshold);
579 if(PARAM.inp.cal_stress)
580 {
581 std::array<std::array<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>,3>,3> dCRs
582 = LRI_CV_Tools::cal_dMRs(ucell,dCs_order);
583 this->exx_lri.set_dCRs(std::move(dCRs), this->info.C_grad_R_threshold);
584 }
585 }
586 ModuleBase::timer::end("Exx_LRI", "cal_cut_coulomb_cs");
587}
588
589template <typename Tdata>
591 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>& Vs_full,
592 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>& Cs,
593 const UnitCell& ucell,
594 const bool write_cv)
595{
596 ModuleBase::TITLE("Exx_LRI", "cal_ewald_coulomb");
597 ModuleBase::timer::start("Exx_LRI", "cal_ewald_coulomb");
598
599 std::vector<TA> atoms(ucell.nat);
600 for (int iat = 0; iat < ucell.nat; ++iat)
601 atoms[iat] = iat;
602 std::map<TA, TatomR> atoms_pos;
603 for (int iat = 0; iat < ucell.nat; ++iat)
604 atoms_pos[iat] = RI_Util::Vector3_to_array3(ucell.atoms[ucell.iat2it[iat]].tau[ucell.iat2ia[iat]]);
605 const std::array<TatomR, Ndim> latvec = {
609 const std::array<Tcell, Ndim> period = {this->p_kv->nmp[0], this->p_kv->nmp[1], this->p_kv->nmp[2]};
610
611 this->exx_lri.set_parallel(this->mpi_comm, atoms_pos, latvec, period);
612
613 // std::max(3) for gamma_only, list_A2 should contain cell {-1,0,1}. In the future distribute will be neighbour.
614 const std::array<Tcell, Ndim> period_Vs
615 = LRI_CV_Tools::cal_latvec_range<Tcell>(1 + this->info.ccp_rmesh_times, ucell, orb_cutoff_);
616 const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA, std::array<Tcell, Ndim>>>>> list_As_Vs
617 = RI::Distribute_Equally::distribute_atoms_periods(this->mpi_comm, atoms, period_Vs, 2, false);
618
619 std::map<TA, std::map<TAC, std::array<RI::Tensor<Tdata>, Ndim>>> dVs;
620 for (const auto& settings_list: this->coulomb_settings)
621 {
622 if (!settings_list.second.first)
623 continue;
624 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>> Vs_temp
625 = this->exx_objs[settings_list.first].cv.cal_Vs(
626 ucell,
627 list_As_Vs.first,
628 list_As_Vs.second[0],
629 {{"writable_Vws", true}});
630 this->exx_objs[settings_list.first].cv.Vws = LRI_CV_Tools::get_CVws(ucell, Vs_temp);
631
632 // ======rotate ABFs begin======
633 int flag = 0;
634 for (const auto& IJRc: this->exx_objs[settings_list.first].cv.Vws)
635 {
636 const TA& I = IJRc.first;
637 const auto& JRc = IJRc.second;
638 for (const auto& JRc_tensor: JRc)
639 {
640 const TA& J = JRc_tensor.first;
641 const auto Rc = JRc_tensor.second;
642 for (const auto& Rc_tensor: Rc)
643 {
644 const auto& R = Rc_tensor.first;
645 flag += 1;
646 }
647 }
648 }
649 std::cout << "Coulomb: number of atom-pairs inside atomic overlap is " << flag << ". " << std::endl;
650 if (this->info.coul_moment == true)
651 {
652 double hf_Rcut = std::pow(0.75 * this->p_kv->get_nkstot_nospin() * ucell.omega / (ModuleBase::PI), 1.0 / 3.0);
653 // To cal Cs, we still cal all Vs(R) in r space
654 // moment_abfs->cal_VR(ucell,
655 // this->abfs,
656 // list_As_Vs,
657 // orb_cutoff_,
658 // hf_Rcut,
659 // this->exx_objs[settings_list.first].cv,
660 // Vs_full);
661 delete moment_abfs;
662 moment_abfs = nullptr;
663 malloc_trim(0);
664 }
665
666 flag = 0;
667 for (const auto& IJRc: this->exx_objs[settings_list.first].cv.Vws)
668 {
669 const auto& JRc = IJRc.second;
670 for (const auto& JRc_tensor: JRc)
671 {
672 const auto Rc = JRc_tensor.second;
673 for (const auto& Rc_tensor: Rc)
674 {
675 flag += 1;
676 }
677 }
678 }
679 std::cout << "Coulomb: number of all atom-pairs is " << flag << ". " << std::endl;
680 // ======rotate ABFs end======
681
682 if (settings_list.first == Conv_Coulomb_Pot_K::Coulomb_Method::Ewald)
683 {
684 this->exx_objs[settings_list.first].evq.init_ions(ucell, period_Vs);
685 const auto& coulomb_param = settings_list.second.second;
686 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>> Vs_ewald;
687 for (const auto& param_list: coulomb_param)
688 {
689 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>> Vs_ewald_temp;
690 switch (param_list.first)
691 {
693 double chi
694 = this->exx_objs[settings_list.first].evq.get_singular_chi(ucell, param_list.second, 2.0);
695 Vs_ewald_temp = this->exx_objs[settings_list.first].evq.cal_Vs(ucell, chi, Vs_temp);
696 break;
697 }
698 default: {
699 throw std::invalid_argument(std::string(__FILE__) + " line " + std::to_string(__LINE__));
700 }
701 }
702 // Vs_temp cannot be covered here
703 Vs_ewald = Vs_ewald.empty() ? Vs_ewald_temp : LRI_CV_Tools::add(Vs_ewald, Vs_ewald_temp);
704 }
705 Vs_temp = Vs_ewald;
706 }
707
708 Vs_full = Vs.empty() ? Vs_temp : LRI_CV_Tools::add(Vs_full, Vs_temp);
709 }
710
711 if (write_cv && GlobalV::MY_RANK == 0)
712 {
713 LRI_CV_Tools::write_Vs_abf(Vs_full, PARAM.globalv.global_out_dir + "Vs_full");
714 }
715 // this->exx_lri.set_Vs(std::move(Vs_full), this->info.V_threshold);
716
717 // const std::array<Tcell,Ndim> period_Cs = LRI_CV_Tools::cal_latvec_range<Tcell>(2, ucell,orb_cutoff_);
718 // const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA,std::array<Tcell,Ndim>>>>>
719 // list_As_Cs = RI::Distribute_Equally::distribute_atoms_periods(this->mpi_comm, atoms, period_Cs, 2, false);
720 // std::pair<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>,
721 // std::map<TA,std::map<TAC,std::array<RI::Tensor<Tdata>,3>>>>
722 // Cs_dCs = this->exx_objs[coulomb_method].cv.cal_Cs_dCs(ucell,
723 // list_As_Cs.first, list_As_Cs.second[0],
724 // {{"cal_dC",PARAM.inp.cal_force||PARAM.inp.cal_stress},
725 // {"writable_Cws",true},
726 // {"writable_dCws",true},
727 // {"writable_Vws",false},
728 // {"writable_dVws",false}});
729 // Cs = std::get<0>(Cs_dCs);
730 // this->exx_objs[coulomb_method].cv.Cws = LRI_CV_Tools::get_CVws(ucell,Cs);
731 // if(write_cv && GlobalV::MY_RANK==0)
732 // {
733 // LRI_CV_Tools::write_Cs_ao(Cs, PARAM.globalv.global_out_dir + "Cs");
734 // }
735 // this->exx_lri.set_Cs(Cs, this->info.C_threshold);
736 ModuleBase::timer::end("Exx_LRI", "cal_ewald_coulomb");
737}
738 #endif
739
740template <typename Tdata>
742 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>& Vs_cut,
743 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>& Cs,
744 const UnitCell& ucell,
745 const bool write_cv)
746{
747 ModuleBase::TITLE("Exx_LRI", "cal_cut_coulomb_cs");
748 ModuleBase::timer::start("Exx_LRI", "cal_cut_coulomb_cs");
749
750 std::vector<TA> atoms(ucell.nat);
751 for (int iat = 0; iat < ucell.nat; ++iat)
752 {
753 atoms[iat] = iat;
754 }
755 std::map<TA, TatomR> atoms_pos;
756 for (int iat = 0; iat < ucell.nat; ++iat)
757 {
758 atoms_pos[iat] = RI_Util::Vector3_to_array3(ucell.atoms[ucell.iat2it[iat]].tau[ucell.iat2ia[iat]]);
759 }
760 const std::array<TatomR, Ndim> latvec = {
764 const std::array<Tcell, Ndim> period = {this->p_kv->nmp[0], this->p_kv->nmp[1], this->p_kv->nmp[2]};
765
766 this->exx_lri.set_parallel(this->mpi_comm, atoms_pos, latvec, period);
767
768 const auto center2_method = Conv_Coulomb_Pot_K::Coulomb_Method::Center2;
769 auto center2_obj_it = this->exx_objs.find(center2_method);
770 if (center2_obj_it == this->exx_objs.end())
771 {
772 throw std::invalid_argument(std::string(__FILE__) + " line " + std::to_string(__LINE__));
773 }
774
775 const std::array<Tcell, Ndim> period_Vs
776 = LRI_CV_Tools::cal_latvec_range<Tcell>(1 + this->info.ccp_rmesh_times, ucell, orb_cutoff_);
777 const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA, std::array<Tcell, Ndim>>>>> list_As_Vs
778 = RI::Distribute_Equally::distribute_atoms_periods(this->mpi_comm, atoms, period_Vs, 2, false);
779
780 Vs_cut = center2_obj_it->second.cv.cal_Vs(
781 ucell,
782 list_As_Vs.first,
783 list_As_Vs.second[0],
784 {{"writable_Vws", true}});
785 center2_obj_it->second.cv.Vws = LRI_CV_Tools::get_CVws(ucell, Vs_cut);
786 if (write_cv && GlobalV::MY_RANK == 0)
787 {
788 LRI_CV_Tools::write_Vs_abf(Vs_cut, PARAM.globalv.global_out_dir + "Vs_cut");
789 }
790 this->exx_lri.set_Vs(Vs_cut, this->info.V_threshold);
791
792 const std::array<Tcell, Ndim> period_Cs = LRI_CV_Tools::cal_latvec_range<Tcell>(2, ucell, orb_cutoff_);
793 const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA, std::array<Tcell, Ndim>>>>> list_As_Cs
794 = RI::Distribute_Equally::distribute_atoms_periods(this->mpi_comm, atoms, period_Cs, 2, false);
795 std::pair<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>,
796 std::map<TA, std::map<TAC, std::array<RI::Tensor<Tdata>, 3>>>>
797 Cs_dCs = center2_obj_it->second.cv.cal_Cs_dCs(
798 ucell,
799 list_As_Cs.first,
800 list_As_Cs.second[0],
801 {{"cal_dC", false},
802 {"writable_Cws", true},
803 {"writable_dCws", true},
804 {"writable_Vws", false},
805 {"writable_dVws", false}});
806 Cs = std::get<0>(Cs_dCs);
807 center2_obj_it->second.cv.Cws = LRI_CV_Tools::get_CVws(ucell, Cs);
808 if (write_cv && GlobalV::MY_RANK == 0)
809 {
810 LRI_CV_Tools::write_Cs_ao(Cs, PARAM.globalv.global_out_dir + "Cs");
811 }
812 this->exx_lri.set_Cs(Cs, this->info.C_threshold);
813
814 ModuleBase::timer::end("Exx_LRI", "cal_cut_coulomb_cs");
815}
816
817template <typename Tdata>
819 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>& Vs_full,
820 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>& Cs,
821 const UnitCell& ucell,
822 const bool write_cv)
823{
824 ModuleBase::TITLE("Exx_LRI", "cal_ewald_coulomb");
825 ModuleBase::timer::start("Exx_LRI", "cal_ewald_coulomb");
826
827 std::vector<TA> atoms(ucell.nat);
828 for (int iat = 0; iat < ucell.nat; ++iat)
829 {
830 atoms[iat] = iat;
831 }
832 std::map<TA, TatomR> atoms_pos;
833 for (int iat = 0; iat < ucell.nat; ++iat)
834 {
835 atoms_pos[iat] = RI_Util::Vector3_to_array3(ucell.atoms[ucell.iat2it[iat]].tau[ucell.iat2ia[iat]]);
836 }
837 const std::array<TatomR, Ndim> latvec = {
841 const std::array<Tcell, Ndim> period = {this->p_kv->nmp[0], this->p_kv->nmp[1], this->p_kv->nmp[2]};
842
843 this->exx_lri.set_parallel(this->mpi_comm, atoms_pos, latvec, period);
844
845 const std::array<Tcell, Ndim> period_Vs
846 = LRI_CV_Tools::cal_latvec_range<Tcell>(1 + this->info.ccp_rmesh_times, ucell, orb_cutoff_);
847 const std::pair<std::vector<TA>, std::vector<std::vector<std::pair<TA, std::array<Tcell, Ndim>>>>> list_As_Vs
848 = RI::Distribute_Equally::distribute_atoms_periods(this->mpi_comm, atoms, period_Vs, 2, false);
849
850 for (const auto& settings_list : this->coulomb_settings)
851 {
852 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>> Vs_temp
853 = this->exx_objs[settings_list.first].cv.cal_Vs(
854 ucell,
855 list_As_Vs.first,
856 list_As_Vs.second[0],
857 {{"writable_Vws", true}});
858 this->exx_objs[settings_list.first].cv.Vws = LRI_CV_Tools::get_CVws(ucell, Vs_temp);
859
860 if (settings_list.first == Conv_Coulomb_Pot_K::Coulomb_Method::Ewald)
861 {
862 this->exx_objs[settings_list.first].evq.init_ions(ucell, period_Vs);
863 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>> Vs_ewald;
864 for (const auto& param_list : settings_list.second.second)
865 {
866 std::map<TA, std::map<TAC, RI::Tensor<Tdata>>> Vs_ewald_temp;
867 switch (param_list.first)
868 {
870 {
871 double chi = this->exx_objs[settings_list.first].evq.get_singular_chi(ucell, param_list.second, 2.0);
872 Vs_ewald_temp = this->exx_objs[settings_list.first].evq.cal_Vs(ucell, chi, Vs_temp);
873 break;
874 }
875 default:
876 {
877 throw std::invalid_argument(std::string(__FILE__) + " line " + std::to_string(__LINE__));
878 }
879 }
880 Vs_ewald = Vs_ewald.empty() ? Vs_ewald_temp : LRI_CV_Tools::add(Vs_ewald, Vs_ewald_temp);
881 }
882 Vs_temp = Vs_ewald;
883 }
884
885 Vs_full = Vs_full.empty() ? Vs_temp : LRI_CV_Tools::add(Vs_full, Vs_temp);
886 }
887
888 if (write_cv && GlobalV::MY_RANK == 0)
889 {
890 LRI_CV_Tools::write_Vs_abf(Vs_full, PARAM.globalv.global_out_dir + "Vs_full");
891 }
892
893 Cs.clear();
894 ModuleBase::timer::end("Exx_LRI", "cal_ewald_coulomb");
895}
896
897template<typename Tdata>
898void Exx_LRI<Tdata>::cal_exx_elec(const std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>& Ds,
899 const UnitCell& ucell,
900 const Parallel_Orbitals& pv,
901 const ModuleSymmetry::Symmetry_rotation* p_symrot)
902{
903 ModuleBase::TITLE("Exx_LRI","cal_exx_elec");
904 ModuleBase::timer::start("Exx_LRI", "cal_exx_elec");
905
906 const std::vector<std::tuple<std::set<TA>, std::set<TA>>> judge
907 = RI_2D_Comm::get_2D_judge(ucell, pv.para_2d());
908
909 if(p_symrot)
910 { this->exx_lri.set_symmetry(true, p_symrot->get_irred_sector()); }
911 else
912 { this->exx_lri.set_symmetry(false, {}); }
913
914 // (nspin=4) the spinor H(R) rotation mixes the 4 spin channels, so they cannot be rotated
915 // independently in the per-spin loop below; hand off to the SOC implementation.
916 if (p_symrot && PARAM.inp.nspin == 4)
917 {
918 this->cal_exx_elec_soc(Ds, ucell, judge, p_symrot);
919 this->exx_lri.set_symmetry(false, {});
920 ModuleBase::timer::end("Exx_LRI", "cal_exx_elec");
921 return;
922 }
923
924 this->Hexxs.resize(PARAM.inp.nspin);
925 this->Eexx = 0;
926 for(int is=0; is<PARAM.inp.nspin; ++is)
927 {
928 const std::string suffix = ((PARAM.inp.cal_force || PARAM.inp.cal_stress) ? std::to_string(is) : "");
929
930 this->exx_lri.set_Ds(Ds[is], this->info.dm_threshold, suffix);
931 this->exx_lri.cal_Hs({ "","",suffix });
932
933 if (this->info.unfold_hybrid_images)
934 {
935 RI_2D_Comm::decode_hybrid_images(this->exx_lri.Hs, this->hybrid_images_period_);
936 }
937
938 if (!p_symrot)
939 {
940 this->Hexxs[is] = RI::Communicate_Tensors_Map_Judge::comm_map2_first(
941 this->mpi_comm, std::move(this->exx_lri.Hs), std::get<0>(judge[is]), std::get<1>(judge[is]));
942 }
943 else
944 {
945 // reduce but not repeat
946 auto Hs_a2D = this->exx_lri.post_2D.set_tensors_map2(this->exx_lri.Hs);
947 // rotate locally without repeat
948 Hs_a2D = p_symrot->restore_HR(ucell.symm, ucell.atoms, ucell.iat2it, 'H', Hs_a2D);
949 // cal energy using full Hs without repeat
950 this->exx_lri.energy = this->exx_lri.post_2D.cal_energy(
951 this->exx_lri.post_2D.saves["Ds_" + suffix],
952 this->exx_lri.post_2D.set_tensors_map2(Hs_a2D));
953 // get repeated full Hs for abacus
954 this->Hexxs[is] = RI::Communicate_Tensors_Map_Judge::comm_map2_first(
955 this->mpi_comm, std::move(Hs_a2D), std::get<0>(judge[is]), std::get<1>(judge[is]));
956 }
957 this->Eexx += std::real(this->exx_lri.energy);
958 post_process_Hexx(this->Hexxs[is]);
959 }
960 this->Eexx = post_process_Eexx(this->Eexx);
961 this->exx_lri.set_symmetry(false, {});
962 ModuleBase::timer::end("Exx_LRI", "cal_exx_elec");
963}
964
965template<typename Tdata>
967 const std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>& Ds,
968 const UnitCell& ucell,
969 const std::vector<std::tuple<std::set<TA>, std::set<TA>>>& judge,
970 const ModuleSymmetry::Symmetry_rotation* p_symrot)
971{
972 ModuleBase::TITLE("Exx_LRI", "cal_exx_elec_soc");
973 this->Hexxs.resize(PARAM.inp.nspin); // nspin==4
974 this->Eexx = 0;
975
976 // pass 1: compute the irreducible-sector Hs of all 4 spin channels.
977 // distinct suffix per channel keeps all 4 "Ds_*" saves alive for the energy in pass 3.
978 std::array<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>, 4> Hs_irr;
979 std::array<std::string, 4> suffix;
980 for (int is = 0; is < 4; ++is)
981 {
982 suffix[is] = std::to_string(is);
983 this->exx_lri.set_Ds(Ds[is], this->info.dm_threshold, suffix[is]);
984 this->exx_lri.cal_Hs({ "","",suffix[is] });
985 Hs_irr[is] = this->exx_lri.post_2D.set_tensors_map2(this->exx_lri.Hs);
986 }
987
988 // pass 2: spinor-coupled rotation of the 4 channels from the irreducible sector to the full BZ
989 std::array<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>, 4> Hs_full =
990 p_symrot->restore_HR_nspin4(ucell.symm, ucell.atoms, ucell.iat2it, 'H', Hs_irr);
991
992 // pass 3: per-channel energy (full Hs, no repeat), then gather the repeated full Hs for abacus
993 for (int is = 0; is < 4; ++is)
994 {
995 this->exx_lri.energy = this->exx_lri.post_2D.cal_energy(
996 this->exx_lri.post_2D.saves["Ds_" + suffix[is]],
997 this->exx_lri.post_2D.set_tensors_map2(Hs_full[is]));
998 this->Hexxs[is] = RI::Communicate_Tensors_Map_Judge::comm_map2_first(
999 this->mpi_comm, std::move(Hs_full[is]), std::get<0>(judge[is]), std::get<1>(judge[is]));
1000 this->Eexx += std::real(this->exx_lri.energy);
1001 post_process_Hexx(this->Hexxs[is]);
1002 }
1003 this->Eexx = post_process_Eexx(this->Eexx);
1004}
1005
1006template<typename Tdata>
1007void Exx_LRI<Tdata>::post_process_Hexx( std::map<TA, std::map<TAC, RI::Tensor<Tdata>>> &Hexxs_io ) const
1008{
1009 ModuleBase::TITLE("Exx_LRI","post_process_Hexx");
1010 constexpr Tdata frac = -1 * 2; // why? Hartree to Ry?
1011 const std::function<void(RI::Tensor<Tdata>&)>
1012 multiply_frac = [&frac](RI::Tensor<Tdata> &t)
1013 { t = t*frac; };
1014 RI::Map_Operator::for_each( Hexxs_io, multiply_frac );
1015}
1016
1017template<typename Tdata>
1018double Exx_LRI<Tdata>::post_process_Eexx(const double& Eexx_in) const
1019{
1020 ModuleBase::TITLE("Exx_LRI","post_process_Eexx");
1021 const double SPIN_multiple = std::map<int, double>{ {1,2}, {2,1}, {4,1} }.at(PARAM.inp.nspin); // why?
1022 const double frac = -SPIN_multiple;
1023 return frac * Eexx_in;
1024}
1025
1026/*
1027post_process_old
1028{
1029 // D
1030 const std::map<int,double> SPIN_multiple = {{1,0.5}, {2,1}, {4,1}}; // ???
1031 DR *= SPIN_multiple.at(NSPIN);
1032
1033 // H
1034 HR *= -2;
1035
1036 // E
1037 const std::map<int,double> SPIN_multiple = {{1,2}, {2,1}, {4,1}}; // ???
1038 energy *= SPIN_multiple.at(PARAM.inp.nspin); // ?
1039 energy /= 2; // /2 for Ry
1040}
1041*/
1042
1043template<typename Tdata>
1045{
1046 ModuleBase::TITLE("Exx_LRI","cal_exx_force");
1047 ModuleBase::timer::start("Exx_LRI", "cal_exx_force");
1048
1049 this->force_exx.create(nat, Ndim);
1050 const int& nspin = PARAM.inp.nspin;
1051 for(int is=0; is<nspin; ++is)
1052 {
1053 this->exx_lri.cal_force({"","",std::to_string(is),"",""});
1054 for(std::size_t idim=0; idim<Ndim; ++idim) {
1055 for(const auto &force_item : this->exx_lri.force[idim]) {
1056 this->force_exx(force_item.first, idim) += std::real(force_item.second);
1057 } }
1058 }
1059 // SPIN_multiple cancels the one in `split_m2D_ktoR`, which are 0.5*0.5 at nspin=1.
1060 // but only u-u and d-d pairs of Ds has contribution, so here's 2 instead of 4.
1061 // And -2 is the same as post_process_Hexx (which didn't act on Hs)
1062 const double SPIN_multiple = std::map<int,double>{{1,2}, {2,1}, {4,1}}.at(nspin);
1063 const double frac = -2 * SPIN_multiple;
1064 this->force_exx *= frac;
1065 ModuleBase::timer::end("Exx_LRI", "cal_exx_force");
1066}
1067
1068
1069template<typename Tdata>
1070void Exx_LRI<Tdata>::cal_exx_stress(const double& omega, const double& lat0)
1071{
1072 ModuleBase::TITLE("Exx_LRI","cal_exx_stress");
1073 ModuleBase::timer::start("Exx_LRI", "cal_exx_stress");
1074
1075 this->stress_exx.create(Ndim, Ndim);
1076 for(int is=0; is<PARAM.inp.nspin; ++is)
1077 {
1078 this->exx_lri.cal_stress({"","",std::to_string(is),"",""});
1079 for(std::size_t idim0=0; idim0<Ndim; ++idim0) {
1080 for(std::size_t idim1=0; idim1<Ndim; ++idim1) {
1081 this->stress_exx(idim0,idim1) += std::real(this->exx_lri.stress(idim0,idim1));
1082 } }
1083 }
1084
1085 const double SPIN_multiple = std::map<int,double>{{1,2}, {2,1}, {4,1}}.at(PARAM.inp.nspin); // why?
1086 const double frac = 2 * SPIN_multiple / omega * lat0; // why?
1087 this->stress_exx *= frac;
1088
1089 ModuleBase::timer::end("Exx_LRI", "cal_exx_stress");
1090}
1091
1092
1093/*
1094template<typename Tdata>
1095std::vector<std::vector<int>> Exx_LRI<Tdata>::get_abfs_nchis() const
1096{
1097 std::vector<std::vector<int>> abfs_nchis;
1098 for (const auto& abfs_T : this->abfs)
1099 {
1100 std::vector<int> abfs_nchi_T;
1101 for (const auto& abfs_L : abfs_T)
1102 { abfs_nchi_T.push_back(abfs_L.size()); }
1103 abfs_nchis.push_back(abfs_nchi_T);
1104 }
1105 return abfs_nchis;
1106}
1107*/
1108
1109#endif
std::vector< ModuleBase::Vector3< double > > tau
Cartesian coordinates of each atom in this type, in unit of lat0.
Definition atom_spec.h:52
static void print_orbs_size(const UnitCell &ucell, const std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > &orbs, std::ostream &os)
Definition exx_abfs_ctor_orbs.cpp:475
static void filter_empty_orbs(std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > &orbs)
Definition exx_abfs_ctor_orbs.cpp:545
static std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > abfs_same_atom(const UnitCell &ucell, const LCAO_Orbitals &orb, const std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > &lcaos, const double kmesh_times_mot, const double times_threshold=0)
Definition exx_abfs_ctor_orbs.cpp:80
static std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > change_orbs(const LCAO_Orbitals &orb_in, const double kmesh_times)
Definition exx_abfs_ctor_orbs.cpp:10
static std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > construct_abfs(const LCAO_Orbitals &orbs, const std::vector< std::string > &files_abfs, const double kmesh_times=1)
Definition exx_abfs_io.cpp:12
ModuleBase::matrix stress_exx
Definition exx_lri.h:148
void post_process_Hexx(std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > &Hexxs_io) const
Definition exx_lri.hpp:1007
std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > lcaos
Definition exx_lri.h:166
int abfs_Lmax_
Definition exx_lri.h:160
CoulombSettings coulomb_settings
Definition exx_lri.h:172
std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > abfs
Definition exx_lri.h:167
void init(const MPI_Comm &mpi_comm_in, const UnitCell &ucell, const K_Vectors &kv_in, const LCAO_Orbitals &orb, const std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > &abfs_in={})
Definition exx_lri.hpp:39
void cal_cut_coulomb_cs(std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > &Vs_cut_IJR, std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > &Cs, const UnitCell &ucell, const bool write_cv=false)
Definition exx_lri.hpp:741
static constexpr std::size_t Ndim
Definition exx_lri.h:54
std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > Hexxs
Definition exx_lri.h:143
Exx_Info_RI info
Definition exx_lri.h:158
ModuleBase::matrix force_exx
Definition exx_lri.h:147
void cal_exx_elec(const std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > &Ds, const UnitCell &ucell, const Parallel_Orbitals &pv, const ModuleSymmetry::Symmetry_rotation *p_symrot=nullptr)
Definition exx_lri.hpp:898
void cal_exx_elec_soc(const std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > &Ds, const UnitCell &ucell, const std::vector< std::tuple< std::set< TA >, std::set< TA > > > &judge, const ModuleSymmetry::Symmetry_rotation *p_symrot)
Definition exx_lri.hpp:966
std::shared_ptr< ORB_gaunt_table > MGT
Definition exx_lri.h:163
double post_process_Eexx(const double &Eexx_in) const
Definition exx_lri.hpp:1018
std::pair< TA, TC > TAC
Definition exx_lri.h:56
std::map< Conv_Coulomb_Pot_K::Coulomb_Method, std::pair< bool, std::map< Conv_Coulomb_Pot_K::Coulomb_Type, std::vector< std::map< std::string, std::string > > > > > CoulombSettings
Definition exx_lri.h:60
int TA
Definition exx_lri.h:52
void refresh_coulomb(const UnitCell &ucell, const LCAO_Orbitals &orb, const CoulombSettings &settings, const bool use_gpu)
Refresh changed Coulomb kernels while retaining the initialized basis and Gaunt table.
Definition exx_lri.hpp:116
double Eexx
Definition exx_lri.h:144
std::map< Conv_Coulomb_Pot_K::Coulomb_Method, Exx_Obj< Tdata > > exx_objs
Definition exx_lri.h:169
void cal_exx_ions(const UnitCell &ucell, const bool write_cv=false)
Definition exx_lri.hpp:247
MPI_Comm mpi_comm
Definition exx_lri.h:161
void cal_exx_force(const int &nat)
Definition exx_lri.hpp:1044
void init_spencer(const MPI_Comm &mpi_comm_in, const UnitCell &ucell, const K_Vectors &kv_in, const LCAO_Orbitals &orb, const std::vector< std::vector< std::vector< Numerical_Orbital_Lm > > > &abfs_in={})
Definition exx_lri.hpp:177
RI::Exx< TA, Tcell, Ndim, Tdata > exx_lri
Definition exx_lri.h:171
void cal_ewald_coulomb(std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > &Vs_full_IJR, std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > &Cs, const UnitCell &ucell, const bool write_cv=false)
Definition exx_lri.hpp:818
void cal_exx_stress(const double &omega, const double &lat0)
Definition exx_lri.hpp:1070
const K_Vectors * p_kv
Definition exx_lri.h:162
std::vector< double > orb_cutoff_
Definition exx_lri.h:164
Definition exx_lri.h:40
Class for k-points management.
Definition klist.h:20
Definition orb_read.h:18
std::vector< double > cutoffs() const
Definition orb_read.cpp:41
Definition sph_bessel_recursive.h:52
static std::vector< Sph_Bessel_Recursive::D2 > sb_pool
Definition sph_bessel_recursive.h:94
static void start(void)
Start total time calculation.
Definition timer.cpp:44
static void end(const std::string &class_name_in, const std::string &name_in)
Definition timer.cpp:109
const std::map< Tap, std::set< TC > > & get_irred_sector() const
Definition symm_rotation_k.h:32
Definition symm_rotation.h:20
std::array< std::map< int, std::map< std::pair< int, TC >, RI::Tensor< Tdata > > >, 4 > restore_HR_nspin4(const Symmetry &symm, const Atom *atoms, const std::vector< int > &iat2it, const char mode, const std::array< std::map< int, std::map< std::pair< int, TC >, RI::Tensor< Tdata > > >, 4 > &HR_irreducible_soc) const
Definition symm_rotation_r.hpp:173
std::map< int, std::map< std::pair< int, TC >, RI::Tensor< Tdata > > > restore_HR(const Symmetry &symm, const Atom *atoms, const std::vector< int > &iat2it, const char mode, const std::map< int, std::map< std::pair< int, TC >, RI::Tensor< Tdata > > > &HR_irreduceble) const
Definition symm_rotation_r.hpp:79
Definition parallel_orbitals.h:16
Parallel_Blkcyclic & para_2d()
Definition parallel_orbitals.h:24
Definition rpa_lri_coeff_v1.h:11
Provide the basic information about unitcell.
Definition unitcell.h:16
Atom * atoms
Definition unitcell.h:165
std::vector< int > iat2ia
iat==>ia
Definition unitcell.h:157
ModuleSymmetry::Symmetry symm
Definition unitcell.h:177
double & omega
Definition unitcell.h:139
ModuleBase::Vector3< double > & a2
Definition unitcell.h:143
ModuleBase::Vector3< double > & a3
Definition unitcell.h:143
int nat
total number of atoms of all species in unitcell
Definition unitcell.h:155
ModuleBase::Vector3< double > & a1
Definition unitcell.h:143
std::vector< int > iat2it
iat==>it, distinguish a atom belong to which type
Definition unitcell.h:156
#define T
Definition exp.cpp:237
int TA
================ RI ==================
Definition bse_util.h:36
@ Fock
Definition coulomb_config.h:12
std::enable_if<!is_std_vector< T >::value, T >::type cal_orbs_ccp(const T &orbs, const CoulombParam &coulomb_param, const double rmesh_times)
Coulomb_Method
Definition coulomb_config.h:23
@ Ewald
Definition coulomb_config.h:25
@ Center2
Definition coulomb_config.h:24
std::enable_if<!is_std_vector< T >::value, T >::type cal_orbs_ccp_spencer(const T &orbs, const CoulombParam &coulomb_param, const double rmesh_times)
CoulombParam build_center2_cut_coulomb_param(const CoulombParam &coulomb_param, const UnitCell &ucell, const K_Vectors &kv, bool *synthesized_rcut)
Definition exx_lri_detail.cpp:25
int MY_RANK
global index of process
Definition global_variable.cpp:21
std::ofstream ofs_running
Definition global_variable.cpp:38
std::vector< std::array< T, N > > add(const std::vector< std::array< T, N > > &v1, const std::vector< std::array< T, N > > &v2)
Definition lri_cv_tools.hpp:222
std::map< int, std::map< int, std::map< Abfs::Vector3_Order< double >, RI::Tensor< Tdata > > > > get_CVws(const UnitCell &ucell, const std::map< TA, std::map< std::pair< TA, std::array< Tcell, 3 > >, RI::Tensor< Tdata > > > &CVs)
Definition lri_cv_tools.hpp:385
std::array< std::vector< T >, N > change_order(std::vector< std::array< T, N > > &&ds_in)
Definition lri_cv_tools.hpp:305
std::map< int, std::map< int, std::map< Abfs::Vector3_Order< double >, std::array< RI::Tensor< Tdata >, 3 > > > > get_dCVws(const UnitCell &ucell, const std::map< TA, std::map< std::pair< TA, std::array< Tcell, 3 > >, std::array< RI::Tensor< Tdata >, 3 > > > &dCVs)
Definition lri_cv_tools.hpp:412
std::array< Tcell, 3 > cal_latvec_range(const double &rcut_times, const UnitCell &ucell, const std::vector< double > &orb_cutoff)
Definition lri_cv_tools.hpp:367
void write_Cs_ao(const TLRI< T > &Vs, const std::string &file_path)
Definition write_ri_cv.hpp:59
std::array< std::array< std::map< TA, std::map< std::pair< TA, TC >, RI::Tensor< Tdata > > >, 3 >, 3 > cal_dMRs(const UnitCell &ucell, const std::array< std::map< TA, std::map< std::pair< TA, TC >, RI::Tensor< Tdata > > >, 3 > &dMs)
Definition lri_cv_tools.hpp:523
void write_Vs_abf(const TLRI< T > &Vs, const std::string &file_path)
Definition write_ri_cv.hpp:161
const double PI
Definition constants.h:19
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
void reduce_max(T &v)
Definition parallel_reduce.cpp:57
std::array< int, 3 > ImageCell
Definition hybrid_image_domain.h:12
void decode_hybrid_images(std::map< int, std::map< std::pair< int, ImageCell >, RI::Tensor< T > > > &blocks, const ImageCell &period)
Definition hybrid_image_domain.cpp:139
ImageCell hybrid_image_period(const ImageCell &c_extent, const ImageCell &v_extent)
Definition hybrid_image_domain.cpp:62
std::vector< std::tuple< std::set< TA >, std::set< TA > > > get_2D_judge(const UnitCell &ucell, const Parallel_Blkcyclic &pv)
ImageCell retained_image_extent(std::map< int, std::map< std::pair< int, ImageCell >, RI::Tensor< T > > > &blocks, const double threshold)
Definition hybrid_image_domain.cpp:27
std::array< Tcell, 3 > Vector3_to_array3(const ModuleBase::Vector3< Tcell > &v)
Definition ri_util.h:26
std::map< Conv_Coulomb_Pot_K::Coulomb_Method, std::pair< bool, std::map< Conv_Coulomb_Pot_K::Coulomb_Type, std::vector< std::map< std::string, std::string > > > > > update_coulomb_settings(const std::map< Conv_Coulomb_Pot_K::Coulomb_Type, std::vector< std::map< std::string, std::string > > > &coulomb_param, const UnitCell &ucell, const K_Vectors *p_kv)
Definition ri_util.cpp:53
Parameter PARAM
Definition parameter.cpp:3
std::pair< int, TC > TAC
Definition test_write_ri_cv.cpp:10