Skip to content

Commit

Permalink
Test: UnitTest for module_tddft (deepmodeling#2580)
Browse files Browse the repository at this point in the history
* Merge develop branch to TDDFT branch (deepmodeling#2249)

* Add the dipole and spectrum post-processing script. (deepmodeling#1928)

* Add the Dipole and post-processing script necessary to calculate the spectrum using TDDFT.

* fix td_val test bug.

* Fix: bug in parsing strings mixed with " ", "\t" (deepmodeling#1951)

* Use template to reconstruct parse_expression

* Feature: output R matrix at each MD step

* Modify'matrix_HS' to 'matrix' for R matrix output

* Merge branches 'develop' and 'develop' of https://github.com/1041176461/abacus-develop into develop

* Fix: modify index in parse_expression

* Fix: bug in parsing strings mixed with " ", "\t"

---------

Co-authored-by: jiyuang <[email protected]>

* solve conflicts

* solve conflicts

* solve conflicts

* solve conflicts

* solve conflicts

* Fix : bug of multi-kpoint for tddft (deepmodeling#2023)

* fix bug of electric field in tddft

* fix bug of dipole and electric field

* fix bug of multi-kpoint for tddft

* unit conversion of out_efield

* delete useless paramters for tddft (td_val_elec*)

* delete useless parameters for tddft

* pack all changes in tddft

* solve conflicts

* delete td_val

---------

Co-authored-by: HeFuxiang94 <[email protected]>
Co-authored-by: jiyuyang <[email protected]>
Co-authored-by: jiyuang <[email protected]>

* New propagator for better Convergence (deepmodeling#2253)

* New propagator for better Convergence

* Add files via upload

* Add files via upload

* Modified the code's implementation approach.

I have modified the implementation approach of the code, and this one involves less changes to the original code. It also includes handling for multiple k-points.

* Delete module_esolver directory

* Delete module_hamilt_general directory

* Delete module_hamilt_lcao directory

* Add files via upload

* Update dipole.py (deepmodeling#2270)

* Simplify matrix multiplication and add new propagator method (deepmodeling#2272)

* Add the dipole and spectrum post-processing script. (deepmodeling#1928)

* Add the Dipole and post-processing script necessary to calculate the spectrum using TDDFT.

* fix td_val test bug.

* Fix: bug in parsing strings mixed with " ", "\t" (deepmodeling#1951)

* Use template to reconstruct parse_expression

* Feature: output R matrix at each MD step

* Modify'matrix_HS' to 'matrix' for R matrix output

* Merge branches 'develop' and 'develop' of https://github.com/1041176461/abacus-develop into develop

* Fix: modify index in parse_expression

* Fix: bug in parsing strings mixed with " ", "\t"

---------

Co-authored-by: jiyuang <[email protected]>

* solve conflicts

* solve conflicts

* solve conflicts

* solve conflicts

* solve conflicts

* Fix : bug of multi-kpoint for tddft (deepmodeling#2023)

* fix bug of electric field in tddft

* fix bug of dipole and electric field

* fix bug of multi-kpoint for tddft

* unit conversion of out_efield

* delete useless paramters for tddft (td_val_elec*)

* delete useless parameters for tddft

* pack all changes in tddft

* solve conflicts

* delete td_val

* simplify matrix multiplication and update new propagator for tddft

* add autotests for new propagator

* fix bug of ekb in tddft

* delete temp output

* update ref of new propagator

* update CASES

---------

Co-authored-by: HeFuxiang94 <[email protected]>
Co-authored-by: jiyuyang <[email protected]>
Co-authored-by: jiyuang <[email protected]>
Co-authored-by: Qianrui <[email protected]>

* delete td_htype and update ref of tddft autotests

* delete td_htype

* refactor tddft code

* move read_paramter of electric field to input_conv.cpp for tddft

* sve conflicts

* move definition of tag

* convert read_parameter

* add namespace module_tddft

* remove tmp file

* move read_paramters of electric field in tddft to input_conv.cpp

* add force of tddft efield

* add annotations

* move read_parameters from H_TDDFT_pw.cpp to input_conv.cpp

* add ifdef __LCAO

* remove input parameter of read_td_efield

* remove input parameter of read_td_efield

* fix bug for input UTs

* change name of parameters of compute_force in H_TDDFT_pw.h

* improve annotation

* solve conflicts

* solve conflicts

* add UT for read_td_efield

* add ifdef __LCAO

* add ifdef __LCAO

* mistake for TEST and TEST_F

* fix ReadTdEfieldTest

* fix ReadTdEfieldTest

* add doublenear() for EXPECT_EQ in ReadEfieldTest

* add nampspace for doublenear

* add nampspace for doublenear

* mistake

* replace doublenear with EXPECT_NEAR

* TEST_F

* delete nband in propagator

* add UT for module tddft

* comment add_test

* fix bug of prepare() in H_TDDFT_pw.cpp

* split tddft_test

* fix bug

* fix bug of UT for tddft

* add tddft prefix

* add UT for cblacs_gridinit

* fix bug of UT for tddft

* fix bug of UT for tddft

---------

Co-authored-by: HeFuxiang94 <[email protected]>
Co-authored-by: jiyuyang <[email protected]>
Co-authored-by: jiyuang <[email protected]>
Co-authored-by: HTZhao <[email protected]>
Co-authored-by: Omega <[email protected]>
Co-authored-by: Qianrui <[email protected]>
  • Loading branch information
7 people authored and jinzx10 committed Jun 10, 2023
1 parent c0cc66c commit f4335f8
Show file tree
Hide file tree
Showing 21 changed files with 1,208 additions and 90 deletions.
5 changes: 3 additions & 2 deletions source/module_base/blacs_connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ extern "C"
void Cblacs_gridmap(int* icontxt, int *usermap, int ldumap, int nprow, int npcol);
// Informational and Miscellaneous
void Cblacs_gridinfo(int icontxt, int* nprow, int *npcol, int *myprow, int *mypcol);
int Cblacs_pnum(int icontxt, int prow, int pcol);
void Cblacs_pcoord(int icontxt, int pnum, int *prow, int *pcol);
void Cblacs_gridinit(int* icontxt, char* layout, int nprow, int npcol);
int Cblacs_pnum(int icontxt, int prow, int pcol);
void Cblacs_pcoord(int icontxt, int pnum, int *prow, int *pcol);
void Cblacs_exit(int icontxt);
}
5 changes: 5 additions & 0 deletions source/module_base/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ AddTest(
LIBS ${math_libs}
SOURCES blas_connector_test.cpp
)
AddTest(
TARGET base_blacs_connector
LIBS ${math_libs}
SOURCES blacs_connector_test.cpp
)
AddTest(
TARGET base_timer
SOURCES timer_test.cpp ../timer.cpp
Expand Down
53 changes: 53 additions & 0 deletions source/module_base/test/blacs_connector_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "../blacs_connector.h"

#include <mpi.h>

#include "gtest/gtest.h"

/************************************************
* unit test of functions in blacs_connector.h
***********************************************/

/**
* - Tested Function
* - Cblacs_gridinit
* - Initializes a grid of processors with a given number of rows and columns.
* The function creates a cartesian topology of all the processors initialized
* by the BLS library. In this topology, each processor is identified by its
* coordinates (row, col) in the grid.
*/

TEST(blacs_connector, Cblacs_gridinit)
{
int icontxt;
char layout[] = "ROW";
int nprow = 1;
int npcol = 1;

int myid, nprocs;
Cblacs_pinfo(&myid, &nprocs);
Cblacs_get(-1, 0, &icontxt);

// Call the Cblacs_gridinit() function
Cblacs_gridinit(&icontxt, layout, nprow, npcol);

// Check if the grid context handle is created successfully
EXPECT_EQ(icontxt, 0);
}

int main(int argc, char** argv)
{
int myrank;
int mysize;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &mysize);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

testing::InitGoogleTest(&argc, argv);

int result = 0;
result = RUN_ALL_TESTS();
MPI_Finalize();
return 0;
}
6 changes: 3 additions & 3 deletions source/module_elecstate/potentials/H_TDDFT_pw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,19 +312,19 @@ double H_TDDFT_pw::cal_v_time_heaviside()

void H_TDDFT_pw::prepare(const UnitCell& cell, int& dir)
{
if (dir == 0)
if (dir == 1)
{
bvec[0] = cell.G.e11;
bvec[1] = cell.G.e12;
bvec[2] = cell.G.e13;
}
else if (dir == 1)
else if (dir == 2)
{
bvec[0] = cell.G.e21;
bvec[1] = cell.G.e22;
bvec[2] = cell.G.e23;
}
else if (dir == 2)
else if (dir == 3)
{
bvec[0] = cell.G.e31;
bvec[1] = cell.G.e32;
Expand Down
122 changes: 63 additions & 59 deletions source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,32 @@
double Force_Stress_LCAO::force_invalid_threshold_ev = 0.00;
double Force_Stress_LCAO::output_acc = 1.0e-8;

Force_Stress_LCAO::Force_Stress_LCAO(Record_adj& ra, const int nat_in) :
RA(&ra), f_pw(nat_in), nat(nat_in){}
Force_Stress_LCAO::~Force_Stress_LCAO() {}

void Force_Stress_LCAO::getForceStress(
const bool isforce,
const bool isstress,
const bool istestf,
const bool istests,
Local_Orbital_Charge& loc,
const elecstate::ElecState* pelec,
const psi::Psi<double>* psid,
const psi::Psi<std::complex<double>>* psi,
LCAO_Hamilt &uhm,
ModuleBase::matrix& fcs,
ModuleBase::matrix & scs,
const Structure_Factor& sf,
const K_Vectors& kv,
ModulePW::PW_Basis* rhopw,
Force_Stress_LCAO::Force_Stress_LCAO(Record_adj& ra, const int nat_in) : RA(&ra), f_pw(nat_in), nat(nat_in)
{
}
Force_Stress_LCAO::~Force_Stress_LCAO()
{
}

void Force_Stress_LCAO::getForceStress(const bool isforce,
const bool isstress,
const bool istestf,
const bool istests,
Local_Orbital_Charge& loc,
const elecstate::ElecState* pelec,
const psi::Psi<double>* psid,
const psi::Psi<std::complex<double>>* psi,
LCAO_Hamilt& uhm,
ModuleBase::matrix& fcs,
ModuleBase::matrix& scs,
const Structure_Factor& sf,
const K_Vectors& kv,
ModulePW::PW_Basis* rhopw,
#ifdef __EXX
Exx_LRI<double>& exx_lri_double,
Exx_LRI<std::complex<double>>& exx_lri_complex,
Exx_LRI<double>& exx_lri_double,
Exx_LRI<std::complex<double>>& exx_lri_complex,
#endif
ModuleSymmetry::Symmetry* symm)
ModuleSymmetry::Symmetry* symm)
{
ModuleBase::TITLE("Force_Stress_LCAO", "getForceStress");
ModuleBase::timer::tick("Force_Stress_LCAO", "getForceStress");
Expand Down Expand Up @@ -154,13 +156,13 @@ void Force_Stress_LCAO::getForceStress(
svl_dphi,
#endif
uhm,
kv);
//implement vdw force or stress here
// Peize Lin add 2014-04-04, update 2021-03-09
// jiyy add 2019-05-18, update 2021-05-02
ModuleBase::matrix force_vdw;
ModuleBase::matrix stress_vdw;
kv);
// implement vdw force or stress here
// Peize Lin add 2014-04-04, update 2021-03-09
// jiyy add 2019-05-18, update 2021-05-02
ModuleBase::matrix force_vdw;
ModuleBase::matrix stress_vdw;

auto vdw_solver = vdw::make_vdw(GlobalC::ucell, INPUT);
if (vdw_solver != nullptr)
{
Expand Down Expand Up @@ -219,13 +221,13 @@ void Force_Stress_LCAO::getForceStress(
if (isforce)
{
force_dftu.create(nat, 3);
}
if(isstress)
{
stress_dftu.create(3, 3);
}
GlobalC::dftu.force_stress(loc.dm_gamma, loc.dm_k, *uhm.LM, force_dftu, stress_dftu, kv);
}
}
if (isstress)
{
stress_dftu.create(3, 3);
}
GlobalC::dftu.force_stress(loc.dm_gamma, loc.dm_k, *uhm.LM, force_dftu, stress_dftu, kv);
}

if (!GlobalV::GAMMA_ONLY_LOCAL)
this->flk.finish_k();
Expand Down Expand Up @@ -366,35 +368,37 @@ void Force_Stress_LCAO::getForceStress(
GlobalC::ld.save_npy_f(fcs, "f_tot.npy", GlobalC::ucell.nat); // Ty/Bohr, F_tot
if (GlobalV::deepks_scf)
{
GlobalC::ld.save_npy_f(fcs - GlobalC::ld.F_delta, "f_base.npy", GlobalC::ucell.nat); //Ry/Bohr, F_base
GlobalC::ld.save_npy_f(fcs - GlobalC::ld.F_delta, "f_base.npy", GlobalC::ucell.nat); // Ry/Bohr, F_base

if(GlobalV::GAMMA_ONLY_LOCAL)
if (GlobalV::GAMMA_ONLY_LOCAL)
{
GlobalC::ld.cal_gdmx(loc.dm_gamma[0],
GlobalC::ucell,
GlobalC::ORB,
GlobalC::GridD,
pv->trace_loc_row,
pv->trace_loc_col,
isstress);
GlobalC::ucell,
GlobalC::ORB,
GlobalC::GridD,
pv->trace_loc_row,
pv->trace_loc_col,
isstress);
}
else
{
GlobalC::ld.cal_gdmx_k(loc.dm_k,
GlobalC::ucell,
GlobalC::ORB,
GlobalC::GridD,
pv->trace_loc_row,
pv->trace_loc_col,
kv.nks,
kv.kvec_d,
isstress);
{
GlobalC::ld.cal_gdmx_k(loc.dm_k,
GlobalC::ucell,
GlobalC::ORB,
GlobalC::GridD,
pv->trace_loc_row,
pv->trace_loc_col,
kv.nks,
kv.kvec_d,
isstress);
}
if(GlobalV::deepks_out_unittest) GlobalC::ld.check_gdmx(GlobalC::ucell.nat);
if (GlobalV::deepks_out_unittest)
GlobalC::ld.check_gdmx(GlobalC::ucell.nat);
GlobalC::ld.cal_gvx(GlobalC::ucell.nat);

if(GlobalV::deepks_out_unittest) GlobalC::ld.check_gvx(GlobalC::ucell.nat);
GlobalC::ld.save_npy_gvx(GlobalC::ucell.nat);// /Bohr, grad_vx
if (GlobalV::deepks_out_unittest)
GlobalC::ld.check_gvx(GlobalC::ucell.nat);
GlobalC::ld.save_npy_gvx(GlobalC::ucell.nat); // /Bohr, grad_vx
}
else
{
Expand Down Expand Up @@ -712,7 +716,7 @@ void Force_Stress_LCAO::calForceStressIntegralPart(const bool isGammaOnly,
#else
ModuleBase::matrix& svl_dphi,
#endif
LCAO_Hamilt &uhm,
LCAO_Hamilt& uhm,
const K_Vectors& kv)
{
if (isGammaOnly)
Expand Down Expand Up @@ -759,7 +763,7 @@ void Force_Stress_LCAO::calForceStressIntegralPart(const bool isGammaOnly,
svl_dphi,
#endif
uhm,
kv);
kv);
}
return;
}
Expand Down
4 changes: 4 additions & 0 deletions source/module_hamilt_lcao/module_tddft/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ if(ENABLE_LCAO)
add_coverage(tddft)
endif()

IF (BUILD_TESTING)
add_subdirectory(test)
endif()

endif()
2 changes: 1 addition & 1 deletion source/module_hamilt_lcao/module_tddft/evolve_psi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void evolve_psi(const int nband,
/// @input Stmp, Htmp, print_matrix
/// @output U_operator
Propagator prop(propagator, pv);
prop.compute_propagator(nband, nlocal, Stmp, Htmp, H_laststep, U_operator, print_matrix);
prop.compute_propagator(nlocal, Stmp, Htmp, H_laststep, U_operator, print_matrix);

// (3)->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Expand Down
22 changes: 9 additions & 13 deletions source/module_hamilt_lcao/module_tddft/propagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ inline int globalIndex(int localindex, int nblk, int nprocs, int myproc)
return gIndex;
}

void Propagator::compute_propagator(const int nband,
const int nlocal,
void Propagator::compute_propagator(const int nlocal,
const std::complex<double>* Stmp,
const std::complex<double>* Htmp,
const std::complex<double>* H_laststep,
Expand All @@ -35,16 +34,16 @@ void Propagator::compute_propagator(const int nband,
switch (ptype)
{
case 0:
compute_propagator_cn2(nband, nlocal, Stmp, Htmp, U_operator, print_matrix);
compute_propagator_cn2(nlocal, Stmp, Htmp, U_operator, print_matrix);
break;

case 1:
tag = 1;
compute_propagator_taylor(nband, nlocal, Stmp, Htmp, U_operator, print_matrix, tag);
compute_propagator_taylor(nlocal, Stmp, Htmp, U_operator, print_matrix, tag);
break;

case 2:
compute_propagator_etrs(nband, nlocal, Stmp, Htmp, H_laststep, U_operator, print_matrix);
compute_propagator_etrs(nlocal, Stmp, Htmp, H_laststep, U_operator, print_matrix);

break;

Expand All @@ -54,8 +53,7 @@ void Propagator::compute_propagator(const int nband,
}
}

void Propagator::compute_propagator_cn2(const int nband,
const int nlocal,
void Propagator::compute_propagator_cn2(const int nlocal,
const std::complex<double>* Stmp,
const std::complex<double>* Htmp,
std::complex<double>* U_operator,
Expand Down Expand Up @@ -259,8 +257,7 @@ void Propagator::compute_propagator_cn2(const int nband,
delete[] ipiv;
}

void Propagator::compute_propagator_taylor(const int nband,
const int nlocal,
void Propagator::compute_propagator_taylor(const int nlocal,
const std::complex<double>* Stmp,
const std::complex<double>* Htmp,
std::complex<double>* U_operator,
Expand Down Expand Up @@ -580,8 +577,7 @@ void Propagator::compute_propagator_taylor(const int nband,
delete[] ipiv;
}

void Propagator::compute_propagator_etrs(const int nband,
const int nlocal,
void Propagator::compute_propagator_etrs(const int nlocal,
const std::complex<double>* Stmp,
const std::complex<double>* Htmp,
const std::complex<double>* H_laststep,
Expand All @@ -593,8 +589,8 @@ void Propagator::compute_propagator_etrs(const int nband,
ModuleBase::GlobalFunc::ZEROS(U1, this->ParaV->nloc);
ModuleBase::GlobalFunc::ZEROS(U2, this->ParaV->nloc);
int tag = 2;
compute_propagator_taylor(nband, nlocal, Stmp, Htmp, U1, print_matrix, tag);
compute_propagator_taylor(nband, nlocal, Stmp, H_laststep, U2, print_matrix, tag);
compute_propagator_taylor(nlocal, Stmp, Htmp, U1, print_matrix, tag);
compute_propagator_taylor(nlocal, Stmp, H_laststep, U2, print_matrix, tag);
ScalapackConnector::gemm('N',
'N',
nlocal,
Expand Down
Loading

0 comments on commit f4335f8

Please sign in to comment.