ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
lr_amp.h
Go to the documentation of this file.
1#ifndef ABACUS_LR_GRADIENT_AMPLITUDES_H
2#define ABACUS_LR_GRADIENT_AMPLITUDES_H
3#include "utils/lr_util.h"
5#include <ostream>
6#include <stdexcept>
7namespace LR
8{
9// Open-shell eigenvectors store all up-spin k blocks before all down-spin k blocks.
10inline int electron_hole_offset(const int state, const int nk, const int up_size,
11 const int down_size, const int ispin, const bool openshell)
12{
13 if (openshell)
14 {
15 return state * nk * (up_size + down_size) + ispin * nk * up_size;
16 }
17 const int channel_size = ispin == 0 ? up_size : down_size;
18 return state * nk * channel_size;
19}
20
21template <typename T>
22void save_mixed_root(const T* Xall, const int n, const std::vector<int>& group,
23 const std::vector<double>& mixing, std::vector<T>& previous)
24{
25 if (group.size() != mixing.size())
26 {
27 throw std::invalid_argument("JT mixing must contain one coefficient per root");
28 }
29 previous.assign(n, T(0));
30 for (size_t k = 0; k < group.size(); ++k)
31 {
32 for (int i = 0; i < n; ++i)
33 {
34 previous[i] += mixing[k] * Xall[group[k] * n + i];
35 }
36 }
37 double norm_squared = 0.0;
38 for (const T& value: previous)
39 {
40 norm_squared += std::real(LR_Util::get_conj(value) * value);
41 }
42 Parallel_Reduce::reduce_all(norm_squared);
43 if (norm_squared <= 0.0)
44 {
45 throw std::runtime_error("JT reference has zero norm");
46 }
47 const double scale = 1.0 / std::sqrt(norm_squared);
48 for (T& value: previous)
49 {
50 value *= scale;
51 }
52}
53
54template <typename T>
55void follow_root(const T* Xall, const int n, const int nstates, const int initial_state,
56 int& target_state, std::vector<T>& previous, std::ostream& ofs)
57{
58
59 if (target_state < 0)
60 {
61 target_state = initial_state;
62 }
63
64 // Ranks with no local pairs still participate in the global overlap collective.
65 int reference_size = previous.size();
66 Parallel_Reduce::reduce_all(reference_size);
67 if (reference_size > 0)
68 {
69 std::vector<T> ov(nstates, T(0));
70 for (int j = 0; j < nstates; ++j)
71 {
72 const T* const Xj = Xall + j * n;
73 T acc = T(0);
74 for (int i = 0; i < n; ++i)
75 {
76 acc += LR_Util::get_conj(previous[i]) * Xj[i];
77 }
78 ov[j] = acc;
79 }
80 // X is distributed over the same 2D grid as the particle-hole pairs, so the inner
81 // product is only complete after summing over that grid. Reduce the accumulators
82 // themselves (real and imaginary parts alike) and take the modulus afterwards --
83 // reducing |partial| would be wrong. `reduce_all` is the guarded wrapper and is a no-op
84 // in a serial build, so no `#ifdef __MPI` is needed around it.
85 Parallel_Reduce::reduce_all(ov.data(), nstates);
86 int best = 0;
87 double best_ov = -1.0;
88 for (int j = 0; j < nstates; ++j)
89 {
90 const double a = std::abs(ov[j]);
91 if (a > best_ov)
92 {
93 best_ov = a;
94 best = j;
95 }
96 }
97
98 if (best != target_state)
99 {
100 ofs << " EXCITED-STATE RELAX: followed root moved from index "
101 << target_state << " to " << best << " (overlap " << best_ov
102 << "); the states crossed and the index no longer names the same state."
103 << std::endl;
104 }
105 // A low best overlap means no current root resembles the one being followed -- the step
106 // was too large, or the state left the solved window. Say so: the relaxation continues
107 // but the surface it follows is no longer guaranteed continuous.
108 if (best_ov < 0.5)
109 {
110 ofs << " WARNING: largest amplitude overlap with the previous step is"
111 " only "
112 << best_ov
113 << ". The followed state may have left the window spanned by"
114 " lr_nstates; consider raising lr_nstates or reducing the ionic step."
115 << std::endl;
116 }
117 target_state = best;
118 }
119
120 if (n > 0)
121 {
122 previous.assign(Xall + target_state * n, Xall + (target_state + 1) * n);
123 }
124 else
125 {
126 previous.clear();
127 }
128}
129
130template <typename T>
131ct::Tensor pad_amplitudes(const std::vector<ct::Tensor>& X, const bool openshell,
132 const std::vector<Parallel_Blkcyclic>& px, const std::vector<Parallel_Blkcyclic>& px_z,
133 const std::vector<int>& nocc, const std::vector<int>& nvirt,
134 const int nk, const int nloc, const int nloc_z,
135 const int ispin, const int istate_begin, const int nst)
136{
137 ct::Tensor Xz = LR_Util::newTensor<T>({ nst, nloc_z });
138 Xz.zero();
139 // Closed shell: one channel, and `ispin` selects the spin COMBINATION (singlet/triplet)
140 // whose X is being widened -- `nocc`/`nvirt`/`paraX_` are the same for both.
141 // Open shell: one eigenvector holding both channels back to back, so both sub-blocks are
142 // widened and the second one is re-based, because widening the first moves where it starts.
143 const std::vector<int> chan = openshell ? std::vector<int>{ 0, 1 }
144 : std::vector<int>{ ispin };
145 const int ix = openshell ? 0 : ispin; // which entry of `X`
146 int soff_ch = 0;
147 int zoff_ch = 0; // channel offsets inside one state's block
148 for (const int is : chan)
149 {
150 const Parallel_Blkcyclic& pxs = px[is];
151 const Parallel_Blkcyclic& pxz = px_z[is];
152 for (int ist = 0;ist < nst;++ist)
153 {
154 const T* const src = X[ix].template data<T>()
155 + (istate_begin + ist) * nloc + soff_ch;
156 T* const dst = Xz.data<T>() + ist * nloc_z + zoff_ch;
157 for (int ik = 0;ik < nk;++ik)
158 {
159 const int soff = ik * pxs.size();
160 const int zoff = ik * pxz.size();
161 // Both windows are block-cyclic with nb2d = 1 on the same process grid and share
162 // the occupied dimension, so a global (virt, occ) element lives on the same
163 // process in both -- only its local index differs. The widening is therefore a
164 // purely local copy.
165 for (int o = 0;o < nocc[is];++o)
166 {
167 for (int v = 0;v < nvirt[is];++v)
168 {
169 if (!pxs.in_this_processor(v, o))
170 {
171 continue;
172 }
173 dst[zoff + pxz.icloc(o) * pxz.nrloc() + pxz.irloc(v)]
174 = src[soff + pxs.icloc(o) * pxs.nrloc() + pxs.irloc(v)];
175 }
176 }
177 }
178 }
179 soff_ch += nk * pxs.size();
180 zoff_ch += nk * pxz.size();
181 }
182 return Xz;
183}
184
185}
186#endif
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
int irloc(const int irglb) const
Definition parallel_blkcyc.h:78
int nrloc() const
Definition parallel_blkcyc.h:35
bool in_this_processor(const int irglb, const int icglb) const
Definition parallel_blkcyc.cpp:58
std::size_t size() const
Definition parallel_blkcyc.h:48
int icloc(const int icglb) const
Definition parallel_blkcyc.h:83
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
void * data() const
Get a pointer to the data buffer of the tensor.
Definition tensor.cpp:73
void zero()
Set all elements in current tensor object to zero.
Definition tensor.cpp:97
#define T
Definition exp.cpp:237
ct::Tensor newTensor(const ct::TensorShape &shape)
Definition lr_util.h:156
double get_conj(const double &x)
Definition lr_util.hpp:70
Definition esolver_lr_lcao_tddft.h:28
ct::Tensor pad_amplitudes(const std::vector< ct::Tensor > &X, const bool openshell, const std::vector< Parallel_Blkcyclic > &px, const std::vector< Parallel_Blkcyclic > &px_z, const std::vector< int > &nocc, const std::vector< int > &nvirt, const int nk, const int nloc, const int nloc_z, const int ispin, const int istate_begin, const int nst)
Definition lr_amp.h:131
void follow_root(const T *Xall, const int n, const int nstates, const int initial_state, int &target_state, std::vector< T > &previous, std::ostream &ofs)
Definition lr_amp.h:55
void save_mixed_root(const T *Xall, const int n, const std::vector< int > &group, const std::vector< double > &mixing, std::vector< T > &previous)
Definition lr_amp.h:22
int electron_hole_offset(const int state, const int nk, const int up_size, const int down_size, const int ispin, const bool openshell)
Definition lr_amp.h:10
void reduce_all(T &object)
reduce in all process
Definition depend_mock.cpp:14