ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
chg_rho_detail.h
Go to the documentation of this file.
1#ifndef CHG_RHO_DETAIL_H
2#define CHG_RHO_DETAIL_H
3
4// Internal helpers for charge density mixing (mix_rho_recip/mix_rho_real).
5// Not part of the public module_charge API: only chg_mix.cpp
6// and the charge mixing unit test are expected to include this header.
7
8#include <functional>
9#include <complex>
10
11#include "charge.h"
12#include "chg_mix_cfg.h"
16
17namespace module_charge
18{
19namespace detail
20{
21
34template <typename T>
35std::function<void(T*, const T*, const T*)> make_twobeta_mix(
36 const int total, const int nunit,
37 const double mixing_beta, const double mixing_beta_mag)
38{
39 return [total, nunit, mixing_beta, mixing_beta_mag](T* out, const T* in, const T* sres)
40 {
41#ifdef _OPENMP
42#pragma omp parallel for schedule(static, 256)
43#endif
44 for (int i = 0; i < nunit; ++i)
45 {
46 out[i] = in[i] + mixing_beta * sres[i];
47 }
48#ifdef _OPENMP
49#pragma omp parallel for schedule(static, 256)
50#endif
51 for (int i = nunit; i < total; ++i)
52 {
53 out[i] = in[i] + mixing_beta_mag * sres[i];
54 }
55 };
56}
57
68template <typename T>
69void pack_rho_mag(T* out, const T* d0, const T* d1, const int n)
70{
71 if (n < 0)
72 {
73 ModuleBase::WARNING_QUIT("pack_rho_mag", "n must be >= 0");
74 }
75 // A rank may own zero real-space grid points (n == 0) when the grid is
76 // decomposed across more processes than it has slabs. Its buffers are
77 // legitimately null in that case and the loop below performs no access;
78 // only a null buffer with a positive n is a genuine bug.
79 if (n > 0 && (out == nullptr || d0 == nullptr || d1 == nullptr))
80 {
81 ModuleBase::WARNING_QUIT("pack_rho_mag", "pointer is null");
82 }
83#ifdef _OPENMP
84#pragma omp parallel for schedule(static, 512)
85#endif
86 for (int i = 0; i < n; ++i)
87 {
88 out[i] = d0[i] + d1[i];
89 out[i + n] = d0[i] - d1[i];
90 }
91}
92
103template <typename T>
104void unpack_rho_mag(T* d0, T* d1, const T* in, const int n)
105{
106 if (n < 0)
107 {
108 ModuleBase::WARNING_QUIT("unpack_rho_mag", "n must be >= 0");
109 }
110 // A rank may own zero real-space grid points (n == 0); see pack_rho_mag.
111 if (n > 0 && (d0 == nullptr || d1 == nullptr || in == nullptr))
112 {
113 ModuleBase::WARNING_QUIT("unpack_rho_mag", "pointer is null");
114 }
115#ifdef _OPENMP
116#pragma omp parallel for schedule(static, 512)
117#endif
118 for (int i = 0; i < n; ++i)
119 {
120 d0[i] = 0.5 * (in[i] + in[i + n]);
121 d1[i] = 0.5 * (in[i] - in[i + n]);
122 }
123}
124
125} // namespace detail
126} // namespace module_charge
127
128#endif // CHG_RHO_DETAIL_H
#define T
Definition exp.cpp:237
void WARNING_QUIT(const std::string &, const std::string &)
Combine the functions of WARNING and QUIT.
Definition test_delley.cpp:15
Definition chg_atomic_detail.h:19
void pack_rho_mag(T *out, const T *d0, const T *d1, const int n)
Pack charge and magnetism into interleaved layout: out[0..n] = d0 + d1 (charge channel) out[n....
Definition chg_rho_detail.h:69
std::function< void(T *, const T *, const T *)> make_twobeta_mix(const int total, const int nunit, const double mixing_beta, const double mixing_beta_mag)
Create a two-beta mixing functor: mix the first nunit elements with mixing_beta and the rest (nunit....
Definition chg_rho_detail.h:35
void unpack_rho_mag(T *d0, T *d1, const T *in, const int n)
Unpack interleaved layout back to charge and magnetism components: d0[i] = 0.5 * (in[i] + in[i+n]) d1...
Definition chg_rho_detail.h:104
Charge-density symmetrization free functions.
Definition charge.h:18