Skip to content

Commit dc54ffd

Browse files
committed
Refactor Propagator class implementation into multiple files for better code organization
1 parent e4ab72a commit dc54ffd

File tree

8 files changed

+1208
-1149
lines changed

8 files changed

+1208
-1149
lines changed

source/Makefile.Objects

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,9 @@ OBJS_LCAO=evolve_elec.o\
561561
middle_hamilt.o\
562562
norm_psi.o\
563563
propagator.o\
564+
propagator_cn2.o\
565+
propagator_taylor.o\
566+
propagator_etrs.o\
564567
td_velocity.o\
565568
td_current.o\
566569
snap_psibeta_half_tddft.o\

source/module_hamilt_lcao/module_tddft/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ if(ENABLE_LCAO)
66
middle_hamilt.cpp
77
norm_psi.cpp
88
propagator.cpp
9+
propagator_cn2.cpp
10+
propagator_taylor.cpp
11+
propagator_etrs.cpp
912
upsi.cpp
1013
td_velocity.cpp
1114
td_current.cpp

source/module_hamilt_lcao/module_tddft/propagator.cpp

Lines changed: 0 additions & 1148 deletions
Large diffs are not rendered by default.

source/module_hamilt_lcao/module_tddft/propagator.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,91 @@
1414

1515
namespace module_tddft
1616
{
17+
//------------------------ Utility function ------------------------//
18+
#ifdef __MPI
19+
inline int globalIndex(int localindex, int nblk, int nprocs, int myproc)
20+
{
21+
int iblock, gIndex;
22+
iblock = localindex / nblk;
23+
gIndex = (iblock * nprocs + myproc) * nblk + localindex % nblk;
24+
return gIndex;
25+
}
26+
#endif // __MPI
27+
28+
// Auxiliary function: process non-complex types, return value 1.0
29+
template <typename T>
30+
inline T init_value(typename std::enable_if<!std::is_same<T, std::complex<float>>::value
31+
&& !std::is_same<T, std::complex<double>>::value>::type* = nullptr)
32+
{
33+
return T(1.0);
34+
}
35+
36+
// Auxiliary function: process complex types, return value 1.0 + 0.0i
37+
template <typename T>
38+
inline T init_value(typename std::enable_if<std::is_same<T, std::complex<float>>::value
39+
|| std::is_same<T, std::complex<double>>::value>::type* = nullptr)
40+
{
41+
return T(1.0, 0.0);
42+
}
43+
44+
// Create an identity matrix of size n×n
45+
template <typename T>
46+
ct::Tensor create_identity_matrix(const int n, ct::DeviceType device = ct::DeviceType::CpuDevice)
47+
{
48+
// Choose the data type of the Tensor
49+
ct::DataType data_type;
50+
if (std::is_same<T, float>::value)
51+
{
52+
data_type = ct::DataType::DT_FLOAT;
53+
}
54+
else if (std::is_same<T, double>::value)
55+
{
56+
data_type = ct::DataType::DT_DOUBLE;
57+
}
58+
else if (std::is_same<T, std::complex<float>>::value)
59+
{
60+
data_type = ct::DataType::DT_COMPLEX;
61+
}
62+
else if (std::is_same<T, std::complex<double>>::value)
63+
{
64+
data_type = ct::DataType::DT_COMPLEX_DOUBLE;
65+
}
66+
else
67+
{
68+
static_assert(std::is_same<T, float>::value || std::is_same<T, double>::value
69+
|| std::is_same<T, std::complex<float>>::value
70+
|| std::is_same<T, std::complex<double>>::value,
71+
"Unsupported data type!");
72+
}
73+
74+
ct::Tensor tensor(data_type, device, ct::TensorShape({n, n}));
75+
tensor.zero();
76+
77+
// Set the diagonal elements to 1
78+
if (device == ct::DeviceType::CpuDevice)
79+
{
80+
// For CPU, we can directly access the data
81+
T* data_ptr = tensor.data<T>();
82+
for (int i = 0; i < n; ++i)
83+
{
84+
data_ptr[i * n + i] = init_value<T>();
85+
}
86+
}
87+
else if (device == ct::DeviceType::GpuDevice)
88+
{
89+
// For GPU, we need to use a kernel to set the diagonal elements
90+
T* data_ptr = tensor.data<T>();
91+
for (int i = 0; i < n; ++i)
92+
{
93+
T value = init_value<T>();
94+
ct::kernels::set_memory<T, ct::DEVICE_GPU>()(data_ptr + i * n + i, value, 1);
95+
}
96+
}
97+
98+
return tensor;
99+
}
100+
//------------------------ Utility function ------------------------//
101+
17102
class Propagator
18103
{
19104
public:

0 commit comments

Comments
 (0)