ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
evolve_elec.h
Go to the documentation of this file.
1#ifndef W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_HAMILT_LCAO_MODULE_TDDFT_EVOLVE_ELEC_H
2#define W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_HAMILT_LCAO_MODULE_TDDFT_EVOLVE_ELEC_H
3
7#include "source_base/module_device/device.h" // base_device
8#include "source_base/module_device/memory_op.h" // memory operations
11#include "source_lcao/module_rt/gather_mat.h" // MPI gathering and distributing functions
14#include "source_psi/psi.h"
15
16class Parallel_Blkcyclic; // Defined in source/source_base/parallel_blkcyc.h
17
18namespace ModuleESolver
19{
20template <typename TK, typename TR>
21class ESolver_KS_LCAO;
22template <typename TR, typename Device>
24} // namespace ModuleESolver
25
26//-----------------------------------------------------------
27// mohan add 2021-02-09
28// This class is used to evolve the electronic wave functions
29// in TDDFT in terms of the multiple k points
30// k is the index for the points in the first Brillouin zone
31//-----------------------------------------------------------
32
33//------------------------ Debugging utility function ------------------------//
34
35// Print the shape of a Tensor
36inline void print_tensor_shape(const ct::Tensor& tensor, const std::string& name)
37{
38 GlobalV::ofs_running << "Shape of " << name << ": [";
39 for (int i = 0; i < tensor.shape().ndim(); ++i)
40 {
41 GlobalV::ofs_running << tensor.shape().dim_size(i);
42 if (i < tensor.shape().ndim() - 1)
43 {
45 }
46 }
47 GlobalV::ofs_running << "]" << std::endl;
48}
49
50// Recursive print function
51template <typename T>
52inline void print_single_element(const T& val, double threshold)
53{
54 double clean_val = (std::abs(val) < threshold) ? 0.0 : static_cast<double>(val);
55 GlobalV::ofs_running << std::fixed << std::setprecision(6) << clean_val;
56}
57inline void print_single_element(const std::complex<double>& val, double threshold)
58{
59 double re = (std::abs(val.real()) < threshold) ? 0.0 : val.real();
60 double im = (std::abs(val.imag()) < threshold) ? 0.0 : val.imag();
61 GlobalV::ofs_running << std::fixed << std::setprecision(6) << "(" << re << "," << im << ")";
62}
63
64template <typename T>
65inline void print_tensor_data_recursive(const T* data,
66 const std::vector<int64_t>& shape,
67 const std::vector<int64_t>& strides,
68 int dim,
69 std::vector<int64_t>& indices,
70 const std::string& name,
71 const double threshold = 1e-10)
72{
73 if (dim == shape.size())
74 {
76 for (size_t i = 0; i < indices.size(); ++i)
77 {
78 GlobalV::ofs_running << "[" << indices[i] << "]";
79 }
80 GlobalV::ofs_running << " = ";
81
83
84 GlobalV::ofs_running << std::endl;
85 return;
86 }
87
88 for (int64_t i = 0; i < shape[dim]; ++i)
89 {
90 indices[dim] = i;
91 print_tensor_data_recursive(data + i * strides[dim], shape, strides, dim + 1, indices, name, threshold);
92 }
93}
94
95template <typename T>
96inline void print_tensor_data(const ct::Tensor& tensor, const std::string& name)
97{
98 const ct::Tensor* p_tensor = &tensor;
99 ct::Tensor cpu_tensor_buffer;
100
102 {
103 cpu_tensor_buffer = tensor.to_device<ct::DEVICE_CPU>();
104 p_tensor = &cpu_tensor_buffer;
105 }
106
107 const std::vector<int64_t>& shape = p_tensor->shape().dims();
108 const std::vector<int64_t>& strides = p_tensor->shape().strides();
109
110 const T* data = p_tensor->data<T>();
111
112 std::vector<int64_t> indices(shape.size(), 0);
113 print_tensor_data_recursive(data, shape, strides, 0, indices, name);
114}
115
116template <>
117inline void print_tensor_data<std::complex<double>>(const ct::Tensor& tensor, const std::string& name)
118{
119 const ct::Tensor* p_tensor = &tensor;
120 ct::Tensor cpu_tensor_buffer;
121
122 if (tensor.device_type() != ct::DeviceType::CpuDevice)
123 {
124 cpu_tensor_buffer = tensor.to_device<ct::DEVICE_CPU>();
125 p_tensor = &cpu_tensor_buffer;
126 }
127
128 const std::vector<int64_t>& shape = p_tensor->shape().dims();
129 const std::vector<int64_t>& strides = p_tensor->shape().strides();
130
131 const std::complex<double>* data = p_tensor->data<std::complex<double>>();
132
133 std::vector<int64_t> indices(shape.size(), 0);
134 print_tensor_data_recursive(data, shape, strides, 0, indices, name);
135}
136
137//------------------------ Debugging utility function ------------------------//
138
139namespace module_rt
140{
141template <typename Device = base_device::DEVICE_CPU>
143{
144 friend class ModuleESolver::ESolver_KS_LCAO<std::complex<double>, double>;
145
146 // Template parameter is needed for the friend class declaration
147 friend class ModuleESolver::ESolver_KS_LCAO_TDDFT<double, Device>;
148 friend class ModuleESolver::ESolver_KS_LCAO_TDDFT<std::complex<double>, Device>;
149
150 public:
151 Evolve_elec();
152 ~Evolve_elec();
153
154 private:
155 static void solve_psi(const std::vector<EijOutputInfo>& eij_output,
156 const int nband,
157 const int nlocal,
158 const int& nks,
159 hamilt::Hamilt<std::complex<double>>* phm,
160 const Parallel_Blkcyclic& matrix_layout,
161 const Parallel_Blkcyclic& wfc_layout,
162 const Parallel_Blkcyclic& mo_repr_layout,
163 psi::Psi<std::complex<double>>* psi,
164 psi::Psi<std::complex<double>>* psi_laststep,
165 ct::Tensor& Hk_laststep,
166 ct::Tensor& Sk_laststep,
167 ModuleBase::matrix& ekb,
168 std::ofstream& ofs_running,
169 const int propagator,
170 const bool use_tensor,
171 const bool use_lapack,
172 module_rt::TD_MovingGauge* td_mg,
173 const UnitCell* ucell,
174 const std::vector<ModuleBase::Vector3<double>>& kvec_d,
175 const bool use_td_moving_gauge);
176
177 // ct_device_type = ct::DeviceType::CpuDevice or ct::DeviceType::GpuDevice
179 // ct_Device = ct::DEVICE_CPU or ct::DEVICE_GPU
180 using ct_Device = typename ct::PsiToContainer<Device>::type;
181
182 // Memory operations
183 using syncmem_double_h2d_op = base_device::memory::synchronize_memory_op<double, Device, base_device::DEVICE_CPU>;
184 using syncmem_double_d2h_op = base_device::memory::synchronize_memory_op<double, base_device::DEVICE_CPU, Device>;
185 using syncmem_complex_h2d_op = base_device::memory::synchronize_memory_op<std::complex<double>, Device, base_device::DEVICE_CPU>;
186 using syncmem_complex_d2h_op = base_device::memory::synchronize_memory_op<std::complex<double>, base_device::DEVICE_CPU, Device>;
187};
188} // namespace module_rt
189#endif
compute band energy ekb This file originally belonged to file LCAO_evolve.cpp
Real-time TDDFT energy solver using the LCAO basis.
Definition esolver_ks_lcao_tddft.h:24
Kohn-Sham energy solver using the LCAO basis.
Definition esolver_ks_lcao.h:33
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
int64_t dim_size(int dim) const
Get the size of a dimension in the tensor.
Definition tensor_shape.cpp:31
const std::vector< int64_t > & dims() const
Get all dimension sizes in the tensor.
Definition tensor_shape.cpp:36
unsigned int ndim() const
Get the ndim of the tensor.
Definition tensor_shape.cpp:46
const std::vector< int64_t > & strides() const
Definition tensor_shape.cpp:41
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
Tensor to_device() const
Method to transform data from a given tensor object to the output tensor with a given device type.
Definition tensor.h:231
void * data() const
Get a pointer to the data buffer of the tensor.
Definition tensor.cpp:73
DeviceType device_type() const
Get the data type of the tensor.
Definition tensor.cpp:64
const TensorShape & shape() const
Get the shape of the tensor.
Definition tensor.cpp:67
base_device::memory::synchronize_memory_op< double, base_device::DEVICE_CPU, Device > syncmem_double_d2h_op
Definition evolve_elec.h:184
static ct::DeviceType ct_device_type
Definition evolve_elec.h:178
typename ct::PsiToContainer< Device >::type ct_Device
Definition evolve_elec.h:180
base_device::memory::synchronize_memory_op< double, Device, base_device::DEVICE_CPU > syncmem_double_h2d_op
Definition evolve_elec.h:183
base_device::memory::synchronize_memory_op< std::complex< double >, Device, base_device::DEVICE_CPU > syncmem_complex_h2d_op
Definition evolve_elec.h:185
static void solve_psi(const std::vector< EijOutputInfo > &eij_output, const int nband, const int nlocal, const int &nks, hamilt::Hamilt< std::complex< double > > *phm, const Parallel_Blkcyclic &matrix_layout, const Parallel_Blkcyclic &wfc_layout, const Parallel_Blkcyclic &mo_repr_layout, psi::Psi< std::complex< double > > *psi, psi::Psi< std::complex< double > > *psi_laststep, ct::Tensor &Hk_laststep, ct::Tensor &Sk_laststep, ModuleBase::matrix &ekb, std::ofstream &ofs_running, const int propagator, const bool use_tensor, const bool use_lapack, module_rt::TD_MovingGauge *td_mg, const UnitCell *ucell, const std::vector< ModuleBase::Vector3< double > > &kvec_d, const bool use_td_moving_gauge)
Definition evolve_elec.cpp:20
Evolve_elec()
Definition evolve_elec.cpp:11
base_device::memory::synchronize_memory_op< std::complex< double >, base_device::DEVICE_CPU, Device > syncmem_complex_d2h_op
Definition evolve_elec.h:186
#define complex
Definition elpa_new.h:28
void print_tensor_shape(const ct::Tensor &tensor, const std::string &name)
Definition evolve_elec.h:36
void print_single_element(const T &val, double threshold)
Definition evolve_elec.h:52
void print_tensor_data(const ct::Tensor &tensor, const std::string &name)
Definition evolve_elec.h:96
void print_tensor_data_recursive(const T *data, const std::vector< int64_t > &shape, const std::vector< int64_t > &strides, int dim, std::vector< int64_t > &indices, const std::string &name, const double threshold=1e-10)
Definition evolve_elec.h:65
#define T
Definition exp.cpp:237
std::ofstream ofs_running
Definition global_variable.cpp:38
The First-Principles (FP) Energy Solver Class.
Definition opt_test_tools.cpp:93
DeviceType
Definition tensor_enums.h:28
@ CpuDevice
Definition tensor_enums.h:30
Definition band_energy.cpp:21
#define threshold
Definition sph_bessel_rec_test.cpp:4
Definition tensor_enums.h:25
Definition tensor_types.h:75