ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
propagator.h
Go to the documentation of this file.
1
6#ifndef PROPAGATOR_H
7#define PROPAGATOR_H
8
13
14#include <complex>
15
16namespace module_rt
17{
18//--------------------------------- Utility function ---------------------------------//
19#ifdef __MPI
20inline int globalIndex(int localindex, int nblk, int nprocs, int myproc)
21{
22 int iblock, gIndex;
23 iblock = localindex / nblk;
24 gIndex = (iblock * nprocs + myproc) * nblk + localindex % nblk;
25 return gIndex;
26}
27#endif // __MPI
28
29// Auxiliary function: process non-complex types, return value 1.0
30template <typename T>
31inline T init_value(typename std::enable_if<!std::is_same<T, std::complex<float>>::value
32 && !std::is_same<T, std::complex<double>>::value>::type* = nullptr)
33{
34 return T(1.0);
35}
36
37// Auxiliary function: process complex types, return value 1.0 + 0.0i
38template <typename T>
39inline T init_value(typename std::enable_if<std::is_same<T, std::complex<float>>::value
40 || std::is_same<T, std::complex<double>>::value>::type* = nullptr)
41{
42 return T(1.0, 0.0);
43}
44
45// Create an identity matrix of size n×n
46template <typename T>
47ct::Tensor create_identity_matrix(const int n, ct::DeviceType device = ct::DeviceType::CpuDevice)
48{
49 // Choose the data type of the Tensor
50 ct::DataType data_type;
51 if (std::is_same<T, float>::value)
52 {
53 data_type = ct::DataType::DT_FLOAT;
54 }
55 else if (std::is_same<T, double>::value)
56 {
57 data_type = ct::DataType::DT_DOUBLE;
58 }
59 else if (std::is_same<T, std::complex<float>>::value)
60 {
61 data_type = ct::DataType::DT_COMPLEX;
62 }
63 else if (std::is_same<T, std::complex<double>>::value)
64 {
65 data_type = ct::DataType::DT_COMPLEX_DOUBLE;
66 }
67 else
68 {
69 static_assert(std::is_same<T, float>::value || std::is_same<T, double>::value
70 || std::is_same<T, std::complex<float>>::value
71 || std::is_same<T, std::complex<double>>::value,
72 "Unsupported data type!");
73 }
74
75 ct::Tensor tensor(data_type, device, ct::TensorShape({n, n}));
76 tensor.zero();
77
78 // Set the diagonal elements to 1
79 if (device == ct::DeviceType::CpuDevice)
80 {
81 // For CPU, we can directly access the data
82 T* data_ptr = tensor.data<T>();
83 for (int i = 0; i < n; ++i)
84 {
85 data_ptr[i * n + i] = init_value<T>();
86 }
87 }
88#if ((defined __CUDA))
89 else if (device == ct::DeviceType::GpuDevice)
90 {
91 // For GPU, we need to use a kernel to set the diagonal elements
92 T* data_ptr = tensor.data<T>();
93 for (int i = 0; i < n; ++i)
94 {
95 T value = init_value<T>();
96 ct::kernels::set_memory<T, ct::DEVICE_GPU>()(data_ptr + i * n + i, value, 1);
97 }
98 }
99#endif
100
101 return tensor;
102}
103//--------------------------------- Utility function ---------------------------------//
104
106{
107 public:
108 Propagator(const int ptype, const Parallel_Orbitals* pv, const double& dt)
109 {
110 this->ptype = ptype;
111 this->ParaV = pv;
112 this->dt = dt / ModuleBase::AU_to_FS;
113 }
114 ~Propagator();
115
116#ifdef __MPI
127 void compute_propagator(const int nlocal,
128 const std::complex<double>* Stmp,
129 const std::complex<double>* Htmp,
130 const std::complex<double>* H_laststep,
131 std::complex<double>* U_operator,
132 std::ofstream& ofs_running,
133 const int print_matrix) const;
134
135 template <typename Device>
136 void compute_propagator_tensor(const int nlocal,
137 const ct::Tensor& Stmp,
138 const ct::Tensor& Htmp,
139 const ct::Tensor& H_laststep,
140 ct::Tensor& U_operator,
141 std::ofstream& ofs_running,
142 const int print_matrix,
143 const bool use_lapack,
144 CublasMpResources& cublas_res) const;
145#endif // __MPI
146
147 private:
148 int ptype; // type of propagator
149 const Parallel_Orbitals* ParaV = nullptr;
150 double dt; // time step
151
152#ifdef __MPI
153
163 void compute_propagator_cn2(const int nlocal,
164 const std::complex<double>* Stmp,
165 const std::complex<double>* Htmp,
166 std::complex<double>* U_operator,
167 std::ofstream& ofs_running,
168 const int print_matrix) const;
169
170 void compute_propagator_cn2_tensor(const int nlocal,
171 const ct::Tensor& Stmp,
172 const ct::Tensor& Htmp,
173 ct::Tensor& U_operator,
174 std::ofstream& ofs_running,
175 const int print_matrix,
176 CublasMpResources& cublas_res) const;
177
178 template <typename Device>
179 void compute_propagator_cn2_tensor_lapack(const int nlocal,
180 const ct::Tensor& Stmp,
181 const ct::Tensor& Htmp,
182 ct::Tensor& U_operator,
183 std::ofstream& ofs_running,
184 const int print_matrix) const;
185
196 void compute_propagator_taylor(const int nlocal,
197 const std::complex<double>* Stmp,
198 const std::complex<double>* Htmp,
199 std::complex<double>* U_operator,
200 std::ofstream& ofs_running,
201 const int print_matrix,
202 const int tag) const;
203
214 void compute_propagator_etrs(const int nlocal,
215 const std::complex<double>* Stmp,
216 const std::complex<double>* Htmp,
217 const std::complex<double>* H_laststep,
218 std::complex<double>* U_operator,
219 std::ofstream& ofs_running,
220 const int print_matrix) const;
221#endif // __MPI
222};
223} // namespace module_rt
224
225#endif
const std::complex< double > i
Definition cal_pLpR.cpp:46
Definition parallel_orbitals.h:9
A class for representing the shape of a tensor.
Definition tensor_shape.h:13
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
Definition propagator.h:106
void compute_propagator_etrs(const int nlocal, const std::complex< double > *Stmp, const std::complex< double > *Htmp, const std::complex< double > *H_laststep, std::complex< double > *U_operator, std::ofstream &ofs_running, const int print_matrix) const
compute propagator of method ETRS
Definition propagator_etrs.cpp:15
Propagator(const int ptype, const Parallel_Orbitals *pv, const double &dt)
Definition propagator.h:108
void compute_propagator_cn2_tensor_lapack(const int nlocal, const ct::Tensor &Stmp, const ct::Tensor &Htmp, ct::Tensor &U_operator, std::ofstream &ofs_running, const int print_matrix) const
Definition propagator_cn2.cpp:557
void compute_propagator_tensor(const int nlocal, const ct::Tensor &Stmp, const ct::Tensor &Htmp, const ct::Tensor &H_laststep, ct::Tensor &U_operator, std::ofstream &ofs_running, const int print_matrix, const bool use_lapack, CublasMpResources &cublas_res) const
Definition propagator.cpp:50
void compute_propagator_taylor(const int nlocal, const std::complex< double > *Stmp, const std::complex< double > *Htmp, std::complex< double > *U_operator, std::ofstream &ofs_running, const int print_matrix, const int tag) const
compute propagator of method 4th Taylor
Definition propagator_taylor.cpp:17
double dt
Definition propagator.h:150
void compute_propagator_cn2_tensor(const int nlocal, const ct::Tensor &Stmp, const ct::Tensor &Htmp, ct::Tensor &U_operator, std::ofstream &ofs_running, const int print_matrix, CublasMpResources &cublas_res) const
Definition propagator_cn2.cpp:251
void compute_propagator(const int nlocal, const std::complex< double > *Stmp, const std::complex< double > *Htmp, const std::complex< double > *H_laststep, std::complex< double > *U_operator, std::ofstream &ofs_running, const int print_matrix) const
compute propagator
Definition propagator.cpp:19
const Parallel_Orbitals * ParaV
Definition propagator.h:149
int ptype
Definition propagator.h:148
~Propagator()
Definition propagator.cpp:15
void compute_propagator_cn2(const int nlocal, const std::complex< double > *Stmp, const std::complex< double > *Htmp, std::complex< double > *U_operator, std::ofstream &ofs_running, const int print_matrix) const
compute propagator of method Crank-Nicolson
Definition propagator_cn2.cpp:19
void print_matrix(std::ofstream &fp, T *matrix, int &nrow, int &ncol, bool row_first)
Definition diago_lapack_test.cpp:92
#define T
Definition exp.cpp:237
const double AU_to_FS
Definition constants.h:75
DataType
Enumeration of data types for tensors. The DataType enum lists the supported data types for tensors....
Definition tensor_types.h:50
DeviceType
The type of memory used by an allocator.
Definition tensor_types.h:73
Definition band_energy.cpp:15
ct::Tensor create_identity_matrix(const int n, ct::DeviceType device=ct::DeviceType::CpuDevice)
Definition propagator.h:47
int globalIndex(int localindex, int nblk, int nprocs, int myproc)
Definition band_energy.cpp:18
T init_value(typename std::enable_if<!std::is_same< T, std::complex< float > >::value &&!std::is_same< T, std::complex< double > >::value >::type *=nullptr)
Definition propagator.h:31
Definition cublasmp_context.h:38