Skip to content

Commit 0dc5e8b

Browse files
authored
Test: Add unit tests and integrate tests for new features of DeePKS. (#6140)
* Add integrate test for v_delta(k) label in DeePKS multi-k calculations. * Add orbital related unit test for DeePKS. * Add vdpre related unit test for DeePKS. * Add check for complex numbers. * Change DeePKS multik UT to minimize file size. * Update gedm_ref.dat. * Rearrange and modify tests in DeePKS. * Fix nscf bug caused by different filename. * Update DeePKS multi-k UT. * Rename jd as README. * Update result.ref. * Update result.ref. * Adjust integrate tests in DeePKS for acceleration. * Remove useless files in DeePKS integrate tests. * Add nscf and relax integrate tests for DeePKS. * Add input check. * Add missing files for DeePKS test. * Temporarily close the check for gedm in DeePKS UT.
1 parent 80ab641 commit 0dc5e8b

File tree

360 files changed

+50080
-12097
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

360 files changed

+50080
-12097
lines changed

source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp

+25-28
Original file line numberDiff line numberDiff line change
@@ -690,42 +690,39 @@ void Force_Stress_LCAO<T>::getForceStress(UnitCell& ucell,
690690
} // end symmetry
691691

692692
#ifdef __DEEPKS
693-
if (PARAM.inp.deepks_out_labels) // not parallelized yet
693+
if (PARAM.inp.deepks_out_labels == 1)
694694
{
695-
if (PARAM.inp.deepks_out_labels == 1)
695+
const std::string file_stot = PARAM.globalv.global_out_dir + "deepks_stot.npy";
696+
LCAO_deepks_io::save_matrix2npy(file_stot,
697+
scs,
698+
GlobalV::MY_RANK,
699+
ucell.omega,
700+
'U'); // change to energy unit Ry when printing, S_tot;
701+
702+
const std::string file_sbase = PARAM.globalv.global_out_dir + "deepks_sbase.npy";
703+
if (PARAM.inp.deepks_scf)
696704
{
697-
const std::string file_stot = PARAM.globalv.global_out_dir + "deepks_stot.npy";
698-
LCAO_deepks_io::save_matrix2npy(file_stot,
699-
scs,
705+
LCAO_deepks_io::save_matrix2npy(file_sbase,
706+
scs - svnl_dalpha,
700707
GlobalV::MY_RANK,
701708
ucell.omega,
702-
'U'); // change to energy unit Ry when printing, S_tot;
703-
704-
const std::string file_sbase = PARAM.globalv.global_out_dir + "deepks_sbase.npy";
705-
if (PARAM.inp.deepks_scf)
706-
{
707-
LCAO_deepks_io::save_matrix2npy(file_sbase,
708-
scs - svnl_dalpha,
709-
GlobalV::MY_RANK,
710-
ucell.omega,
711-
'U'); // change to energy unit Ry when printing, S_base;
712-
}
713-
else
714-
{
715-
LCAO_deepks_io::save_matrix2npy(file_sbase,
716-
scs,
717-
GlobalV::MY_RANK,
718-
ucell.omega,
719-
'U'); // sbase = stot
720-
}
709+
'U'); // change to energy unit Ry when printing, S_base;
721710
}
722-
else if (PARAM.inp.deepks_out_labels == 2)
711+
else
723712
{
724-
const std::string file_stot = PARAM.globalv.global_out_dir + "deepks_stress.npy";
725-
LCAO_deepks_io::save_matrix2npy(file_stot, scs, GlobalV::MY_RANK, ucell.omega,
726-
'F'); // flat mode
713+
LCAO_deepks_io::save_matrix2npy(file_sbase,
714+
scs,
715+
GlobalV::MY_RANK,
716+
ucell.omega,
717+
'U'); // sbase = stot
727718
}
728719
}
720+
else if (PARAM.inp.deepks_out_labels == 2)
721+
{
722+
const std::string file_stot = PARAM.globalv.global_out_dir + "deepks_stress.npy";
723+
LCAO_deepks_io::save_matrix2npy(file_stot, scs, GlobalV::MY_RANK, ucell.omega,
724+
'F'); // flat mode
725+
}
729726
#endif
730727

731728
// print Rydberg stress or not

source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/deepks_lcao.cpp

-14
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ void hamilt::DeePKS<hamilt::OperatorLCAO<TK, TR>>::calculate_HR()
233233
const Parallel_Orbitals* paraV = this->V_delta_R->get_paraV();
234234
const int npol = this->ucell->get_npol();
235235

236-
// 1. calculate <phi|alpha> for each pair of atoms
237236
#pragma omp parallel for schedule(dynamic)
238237
for (int iat0 = 0; iat0 < this->ucell->nat; iat0++)
239238
{
@@ -290,9 +289,7 @@ void hamilt::DeePKS<hamilt::OperatorLCAO<TK, TR>>::calculate_HR()
290289
}
291290
}
292291
const int trace_alpha_size = trace_alpha_row.size();
293-
//--------------------------------------------------
294292

295-
// 2. calculate <phi_I|beta>D<beta|phi_{J,R}> for each pair of <IJR> atoms
296293
for (int ad1 = 0; ad1 < adjs.adj_num + 1; ++ad1)
297294
{
298295
const int T1 = adjs.ntype[ad1];
@@ -363,17 +360,6 @@ void hamilt::DeePKS<hamilt::OperatorLCAO<TK, TR>>::calculate_HR()
363360
ps2t[i] = col_ptr[trace_alpha_col[i]];
364361
}
365362
}
366-
/*for(int irow = 0;irow<row_size;irow++)
367-
{
368-
for(int icol=0;icol<col_size;icol++)
369-
{
370-
for(int ialpha=0;ialpha<trace_alpha_size;ialpha++)
371-
{
372-
tmp->get_pointer()[irow*col_size+icol] +=
373-
s_1t[irow*trace_alpha_size+ialpha] * s_2t[icol*trace_alpha_size+ialpha];
374-
}
375-
}
376-
}*/
377363
// dgemm for s_2t and s_1t to get HR_12
378364
constexpr char transa = 'T', transb = 'N';
379365
const double gemm_alpha = 1.0, gemm_beta = 1.0;

source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ void LCAO_Deepks_Interface<TK, TR>::out_deepks_labels(const double& etot,
5858
const int nspin = PARAM.inp.nspin;
5959
const int nk = nks / nspin;
6060

61+
// Update DMR in any case of deepks_out_labels/deepks_scf
62+
DeePKS_domain::update_dmr(kvec_d, dm->get_DMK_vector(), ucell, orb, *ParaV, GridD, dmr);
63+
6164
// Note : update PDM and all other quantities with the current dm
6265
// DeePKS PDM and descriptor
6366
if (PARAM.inp.deepks_out_labels == 1 || PARAM.inp.deepks_scf)
6467
{
6568
// this part is for integrated test of deepks
6669
// so it is printed no matter even if deepks_out_labels is not used
67-
DeePKS_domain::update_dmr(kvec_d, dm->get_DMK_vector(), ucell, orb, *ParaV, GridD, dmr);
68-
6970
DeePKS_domain::cal_pdm<
7071
TK>(init_pdm, inlmax, lmaxd, inl2l, inl_index, kvec_d, dmr, phialpha, ucell, orb, GridD, *ParaV, pdm);
7172

source/module_hamilt_lcao/module_deepks/deepks_orbital.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ void DeePKS_domain::cal_o_delta(const std::vector<TH>& dm_hl,
5656
return;
5757
}
5858

59+
void DeePKS_domain::check_o_delta(ModuleBase::matrix& o_delta)
60+
{
61+
std::ofstream ofs("o_delta.dat");
62+
ofs << std::setprecision(10);
63+
for (int ik = 0; ik < o_delta.nr; ik++)
64+
{
65+
ofs << o_delta(ik, 0) << std::endl;
66+
}
67+
}
68+
5969
template void DeePKS_domain::cal_o_delta<double, ModuleBase::matrix>(const std::vector<ModuleBase::matrix>& dm_hl,
6070
const std::vector<std::vector<double>>& h_delta,
6171
// std::vector<double>& o_delta,

source/module_hamilt_lcao/module_deepks/deepks_orbital.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ void cal_o_delta(const std::vector<TH>& dm_hl,
3131
const Parallel_Orbitals& pv,
3232
const int nks,
3333
const int nspin);
34+
35+
void check_o_delta(ModuleBase::matrix& o_delta);
3436
} // namespace DeePKS_domain
3537

3638
#endif

source/module_hamilt_lcao/module_deepks/deepks_orbpre.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,25 @@ void DeePKS_domain::cal_orbital_precalc(const std::vector<TH>& dm_hl,
295295
return;
296296
}
297297

298+
void DeePKS_domain::check_orbpre(const torch::Tensor& orbpre)
299+
{
300+
auto sizes = orbpre.sizes();
301+
auto accessor = orbpre.accessor<double, 3>();
302+
std::ofstream ofs("orbital_precalc.dat");
303+
for (int iknb = 0; iknb < sizes[0]; iknb++)
304+
{
305+
for (int iat = 0; iat < sizes[1]; iat++)
306+
{
307+
for (int m = 0; m < sizes[2]; m++)
308+
{
309+
ofs << accessor[iknb][iat][m] << " ";
310+
}
311+
ofs << std::endl;
312+
}
313+
ofs << std::endl;
314+
}
315+
}
316+
298317
template void DeePKS_domain::cal_orbital_precalc<double, ModuleBase::matrix>(
299318
const std::vector<ModuleBase::matrix>& dm_hl,
300319
const int lmaxd,

source/module_hamilt_lcao/module_deepks/deepks_orbpre.h

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void cal_orbital_precalc(const std::vector<TH>& dm_hl,
4141
const Parallel_Orbitals& pv,
4242
const Grid_Driver& GridD,
4343
torch::Tensor& orbital_precalc);
44+
45+
void check_orbpre(const torch::Tensor& orbpre);
4446
} // namespace DeePKS_domain
4547
#endif
4648
#endif

source/module_hamilt_lcao/module_deepks/deepks_pdm.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ void DeePKS_domain::cal_pdm(bool& init_pdm,
458458

459459
void DeePKS_domain::check_pdm(const int inlmax, const std::vector<int>& inl2l, const std::vector<torch::Tensor>& pdm)
460460
{
461-
const std::string file_projdm = PARAM.globalv.global_out_dir + "pdm.dat";
461+
const std::string file_projdm = PARAM.globalv.global_out_dir + "deepks_projdm.dat";
462462
std::ofstream ofs(file_projdm.c_str());
463463

464464
ofs << std::setprecision(10);

source/module_hamilt_lcao/module_deepks/deepks_vdpre.cpp

+9-20
Original file line numberDiff line numberDiff line change
@@ -172,28 +172,25 @@ void DeePKS_domain::cal_v_delta_precalc(const int nlocal,
172172
}
173173

174174
template <typename TK>
175-
void DeePKS_domain::check_v_delta_precalc(const int nat,
176-
const int nks,
177-
const int nlocal,
178-
const int des_per_atom,
179-
const torch::Tensor& v_delta_precalc)
175+
void DeePKS_domain::check_v_delta_precalc(const torch::Tensor& v_delta_precalc)
180176
{
181177
using TK_tensor =
182178
typename std::conditional<std::is_same<TK, std::complex<double>>::value, c10::complex<double>, TK>::type;
179+
auto sizes = v_delta_precalc.sizes();
183180
std::ofstream ofs("v_delta_precalc.dat");
184181
ofs << std::setprecision(10);
185182
auto accessor
186183
= v_delta_precalc
187184
.accessor<std::conditional_t<std::is_same<TK, double>::value, double, c10::complex<double>>, 5>();
188-
for (int iks = 0; iks < nks; ++iks)
185+
for (int iks = 0; iks < sizes[0]; ++iks)
189186
{
190-
for (int mu = 0; mu < nlocal; ++mu)
187+
for (int mu = 0; mu < sizes[1]; ++mu)
191188
{
192-
for (int nu = 0; nu < nlocal; ++nu)
189+
for (int nu = 0; nu < sizes[2]; ++nu)
193190
{
194-
for (int iat = 0; iat < nat; ++iat)
191+
for (int iat = 0; iat < sizes[3]; ++iat)
195192
{
196-
for (int p = 0; p < des_per_atom; ++p)
193+
for (int p = 0; p < sizes[4]; ++p)
197194
{
198195
TK_tensor tmp = accessor[iks][mu][nu][iat][p];
199196
TK* tmp_ptr = reinterpret_cast<TK*>(&tmp);
@@ -432,16 +429,8 @@ template void DeePKS_domain::cal_v_delta_precalc<std::complex<double>>(
432429
const Grid_Driver& GridD,
433430
torch::Tensor& v_delta_precalc);
434431

435-
template void DeePKS_domain::check_v_delta_precalc<double>(const int nat,
436-
const int nks,
437-
const int nlocal,
438-
const int des_per_atom,
439-
const torch::Tensor& v_delta_precalc);
440-
template void DeePKS_domain::check_v_delta_precalc<std::complex<double>>(const int nat,
441-
const int nks,
442-
const int nlocal,
443-
const int des_per_atom,
444-
const torch::Tensor& v_delta_precalc);
432+
template void DeePKS_domain::check_v_delta_precalc<double>(const torch::Tensor& v_delta_precalc);
433+
template void DeePKS_domain::check_v_delta_precalc<std::complex<double>>(const torch::Tensor& v_delta_precalc);
445434

446435
template void DeePKS_domain::prepare_phialpha<double>(const int nlocal,
447436
const int lmaxd,

source/module_hamilt_lcao/module_deepks/deepks_vdpre.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ void cal_v_delta_precalc(const int nlocal,
5151
torch::Tensor& v_delta_precalc);
5252

5353
template <typename TK>
54-
void check_v_delta_precalc(const int nat,
55-
const int nks,
56-
const int nlocal,
57-
const int des_per_atom,
58-
const torch::Tensor& v_delta_precalc);
54+
void check_v_delta_precalc(const torch::Tensor& v_delta_precalc);
5955

6056
// for deepks_v_delta = 2
6157
// prepare phialpha for outputting npy file

0 commit comments

Comments
 (0)