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
12#include "source_lcao/module_rt/gather_mat.h" // MPI gathering and distributing functions
15#include "source_psi/psi.h"
16
17//-----------------------------------------------------------
18// mohan add 2021-02-09
19// This class is used to evolve the electronic wave functions
20// in TDDFT in terms of the multiple k points
21// k is the index for the points in the first Brillouin zone
22//-----------------------------------------------------------
23
24//------------------------ Debugging utility function ------------------------//
25
26// Print the shape of a Tensor
27inline void print_tensor_shape(const ct::Tensor& tensor, const std::string& name)
28{
29 GlobalV::ofs_running << "Shape of " << name << ": [";
30 for (int i = 0; i < tensor.shape().ndim(); ++i)
31 {
33 if (i < tensor.shape().ndim() - 1)
34 {
36 }
37 }
38 GlobalV::ofs_running << "]" << std::endl;
39}
40
41// Recursive print function
42template <typename T>
43inline void print_single_element(const T& val, double threshold)
44{
45 double clean_val = (std::abs(val) < threshold) ? 0.0 : static_cast<double>(val);
46 GlobalV::ofs_running << std::fixed << std::setprecision(6) << clean_val;
47}
48inline void print_single_element(const std::complex<double>& val, double threshold)
49{
50 double re = (std::abs(val.real()) < threshold) ? 0.0 : val.real();
51 double im = (std::abs(val.imag()) < threshold) ? 0.0 : val.imag();
52 GlobalV::ofs_running << std::fixed << std::setprecision(6) << "(" << re << "," << im << ")";
53}
54
55template <typename T>
56inline void print_tensor_data_recursive(const T* data,
57 const std::vector<int64_t>& shape,
58 const std::vector<int64_t>& strides,
59 int dim,
60 std::vector<int64_t>& indices,
61 const std::string& name,
62 const double threshold = 1e-10)
63{
64 if (dim == shape.size())
65 {
67 for (size_t i = 0; i < indices.size(); ++i)
68 {
69 GlobalV::ofs_running << "[" << indices[i] << "]";
70 }
71 GlobalV::ofs_running << " = ";
72
74
75 GlobalV::ofs_running << std::endl;
76 return;
77 }
78
79 for (int64_t i = 0; i < shape[dim]; ++i)
80 {
81 indices[dim] = i;
82 print_tensor_data_recursive(data + i * strides[dim], shape, strides, dim + 1, indices, name, threshold);
83 }
84}
85
86template <typename T>
87inline void print_tensor_data(const ct::Tensor& tensor, const std::string& name)
88{
89 const ct::Tensor* p_tensor = &tensor;
90 ct::Tensor cpu_tensor_buffer;
91
92 if (tensor.device_type() != ct::DeviceType::CpuDevice)
93 {
94 cpu_tensor_buffer = tensor.to_device<ct::DEVICE_CPU>();
95 p_tensor = &cpu_tensor_buffer;
96 }
97
98 const std::vector<int64_t>& shape = p_tensor->shape().dims();
99 const std::vector<int64_t>& strides = p_tensor->shape().strides();
100
101 const T* data = p_tensor->data<T>();
102
103 std::vector<int64_t> indices(shape.size(), 0);
104 print_tensor_data_recursive(data, shape, strides, 0, indices, name);
105}
106
107template <>
108inline void print_tensor_data<std::complex<double>>(const ct::Tensor& tensor, const std::string& name)
109{
110 const ct::Tensor* p_tensor = &tensor;
111 ct::Tensor cpu_tensor_buffer;
112
113 if (tensor.device_type() != ct::DeviceType::CpuDevice)
114 {
115 cpu_tensor_buffer = tensor.to_device<ct::DEVICE_CPU>();
116 p_tensor = &cpu_tensor_buffer;
117 }
118
119 const std::vector<int64_t>& shape = p_tensor->shape().dims();
120 const std::vector<int64_t>& strides = p_tensor->shape().strides();
121
122 const std::complex<double>* data = p_tensor->data<std::complex<double>>();
123
124 std::vector<int64_t> indices(shape.size(), 0);
125 print_tensor_data_recursive(data, shape, strides, 0, indices, name);
126}
127
128//------------------------ Debugging utility function ------------------------//
129
130namespace module_rt
131{
132template <typename Device = base_device::DEVICE_CPU>
134{
135 friend class ModuleESolver::ESolver_KS_LCAO<std::complex<double>, double>;
136
137 // Template parameter is needed for the friend class declaration
138 friend class ModuleESolver::ESolver_KS_LCAO_TDDFT<double, Device>;
139 friend class ModuleESolver::ESolver_KS_LCAO_TDDFT<std::complex<double>, Device>;
140
141 public:
142 Evolve_elec();
143 ~Evolve_elec();
144
145 private:
146 static void solve_psi(const int& istep,
147 const int nband,
148 const int nlocal,
149 const int& nks,
150 hamilt::Hamilt<std::complex<double>>* phm,
151 Parallel_Orbitals& para_orb,
152 psi::Psi<std::complex<double>>* psi,
153 psi::Psi<std::complex<double>>* psi_laststep,
154 ct::Tensor& Hk_laststep,
155 ct::Tensor& Sk_laststep,
156 ModuleBase::matrix& ekb,
157 std::ofstream& ofs_running,
158 const int propagator,
159 const bool use_tensor,
160 const bool use_lapack,
161 module_rt::TD_MovingGauge* td_mg,
162 const UnitCell* ucell,
163 const std::vector<ModuleBase::Vector3<double>>& kvec_d,
164 const bool use_td_moving_gauge);
165
166 // ct_device_type = ct::DeviceType::CpuDevice or ct::DeviceType::GpuDevice
168 // ct_Device = ct::DEVICE_CPU or ct::DEVICE_GPU
169 using ct_Device = typename ct::PsiToContainer<Device>::type;
170
171 // Memory operations
172 using syncmem_double_h2d_op = base_device::memory::synchronize_memory_op<double, Device, base_device::DEVICE_CPU>;
173 using syncmem_double_d2h_op = base_device::memory::synchronize_memory_op<double, base_device::DEVICE_CPU, Device>;
175 = base_device::memory::synchronize_memory_op<std::complex<double>, Device, base_device::DEVICE_CPU>;
177 = base_device::memory::synchronize_memory_op<std::complex<double>, base_device::DEVICE_CPU, Device>;
178};
179} // namespace module_rt
180#endif
const std::complex< double > i
Definition cal_pLpR.cpp:46
Definition esolver_ks_lcao_tddft.h:16
Definition esolver_ks_lcao.h:33
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
Definition evolve_elec.h:134
static ct::DeviceType ct_device_type
Definition evolve_elec.h:167
typename ct::PsiToContainer< Device >::type ct_Device
Definition evolve_elec.h:169
static void solve_psi(const int &istep, const int nband, const int nlocal, const int &nks, hamilt::Hamilt< std::complex< double > > *phm, Parallel_Orbitals &para_orb, 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:22
std::complex< double > complex
Definition diago_cusolver.cpp:15
void print_tensor_shape(const ct::Tensor &tensor, const std::string &name)
Definition evolve_elec.h:27
void print_single_element(const T &val, double threshold)
Definition evolve_elec.h:43
void print_tensor_data(const ct::Tensor &tensor, const std::string &name)
Definition evolve_elec.h:87
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:56
#define T
Definition exp.cpp:237
std::ofstream ofs_running
Definition global_variable.cpp:38
DeviceType
The type of memory used by an allocator.
Definition tensor_types.h:73
Definition band_energy.cpp:15
#define threshold
Definition sph_bessel_recursive_test.cpp:4
A tag type for identifying CPU and GPU devices.
Definition tensor_types.h:68
Definition tensor_types.h:113