Skip to content

Commit 8d926f7

Browse files
committed
bug may be in random_t function.
1 parent 20ee880 commit 8d926f7

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

source/module_psi/psi_initializer.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,56 @@ psi::Psi<std::complex<double>>* PsiInitializer<T, Device>::allocate(const bool o
129129
}
130130

131131
template<typename T, typename Device>
132-
void PsiInitializer<T, Device>::random_t(T* psi, const int iw_start, const int iw_end, const int ik)
132+
void PsiInitializer<T, Device>::_random_t_serial_impl(T* psi,
133+
const int iw_start,
134+
const int iw_end,
135+
const int ik)
136+
{
137+
assert(iw_start >= 0);
138+
const int ng = this->pw_wfc_->npwk[ik];
139+
if (PARAM.inp.pw_seed > 0) // qianrui add 2021-8-13
140+
{
141+
srand(unsigned(PARAM.inp.pw_seed + ik));
142+
}
143+
for (int iw = iw_start; iw < iw_end; iw++)
144+
{
145+
T* psi_slice = &(psi[iw * this->pw_wfc_->npwk_max * PARAM.globalv.npol]);
146+
for (int ig = 0; ig < ng; ig++)
147+
{
148+
const double rr = std::rand() / double(RAND_MAX); // qianrui add RAND_MAX
149+
const double arg = ModuleBase::TWO_PI * std::rand() / double(RAND_MAX);
150+
const double gk2 = this->pw_wfc_->getgk2(ik, ig);
151+
psi_slice[ig] = this->template cast_to_T<T>(
152+
std::complex<double>(rr*cos(arg)/(gk2 + 1.0), rr*sin(arg)/(gk2 + 1.0)));
153+
}
154+
if (PARAM.globalv.npol == 2) // if npol = 2, then fill the second polarization
155+
{
156+
for (int ig = this->pw_wfc_->npwk_max; ig < this->pw_wfc_->npwk_max + ng; ig++)
157+
{
158+
const double rr = std::rand() / double(RAND_MAX);
159+
const double arg = ModuleBase::TWO_PI * std::rand() / double(RAND_MAX);
160+
const double gk2 = this->pw_wfc_->getgk2(ik, ig - this->pw_wfc_->npwk_max);
161+
psi_slice[ig] = this->template cast_to_T<T>(
162+
std::complex<double>(rr*cos(arg)/(gk2 + 1.0), rr*sin(arg)/(gk2 + 1.0)));
163+
}
164+
}
165+
}
166+
}
167+
168+
template<typename T, typename Device>
169+
void PsiInitializer<T, Device>::_random_t_pal_impl(T* psi,
170+
const int iw_start,
171+
const int iw_end,
172+
const int ik)
173+
{
174+
175+
}
176+
177+
template<typename T, typename Device>
178+
void PsiInitializer<T, Device>::random_t(T* psi,
179+
const int iw_start,
180+
const int iw_end,
181+
const int ik)
133182
{
134183
ModuleBase::timer::tick("PsiInitializer", "random_t");
135184
assert(iw_start >= 0);

source/module_psi/psi_initializer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,8 @@ class PsiInitializer
207207
std::string method_ = "none";
208208
int nbands_complem_ = 0;
209209
double random_mix_ = 0;
210+
211+
void _random_t_serial_impl(T* psi, const int iw_start, const int iw_end, const int ik);
212+
void _random_t_pal_impl(T* psi, const int iw_start, const int iw_end, const int ik);
210213
};
211214
#endif

source/module_psi/psi_initializer_random.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
#ifdef __MPI
1111
template <typename T, typename Device>
1212
void PsiInitializerRandom<T, Device>::initialize(Structure_Factor* sf,
13-
ModulePW::PW_Basis_K* pw_wfc,
14-
UnitCell* p_ucell,
15-
Parallel_Kpoints* p_parakpts,
16-
const int& random_seed,
17-
pseudopot_cell_vnl* p_pspot_nl,
18-
const int& rank)
13+
ModulePW::PW_Basis_K* pw_wfc,
14+
UnitCell* p_ucell,
15+
Parallel_Kpoints* p_parakpts,
16+
const int& random_seed,
17+
pseudopot_cell_vnl* p_pspot_nl,
18+
const int& rank)
1919
{
2020
this->pw_wfc_ = pw_wfc;
2121
this->p_ucell_ = p_ucell;

0 commit comments

Comments
 (0)