Skip to content

Commit 28ce5d6

Browse files
committed
add setup_estate_pw in source_estate
1 parent 6f1657e commit 28ce5d6

File tree

6 files changed

+216
-70
lines changed

6 files changed

+216
-70
lines changed

source/Makefile.Objects

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ OBJS_ELECSTAT=elecstate.o\
238238
gatefield.o\
239239
potential_new.o\
240240
potential_types.o\
241+
pot_sep.o\
241242
pot_local.o\
242243
pot_local_paw.o\
243244
H_Hartree_pw.o\
@@ -248,7 +249,7 @@ OBJS_ELECSTAT=elecstate.o\
248249
cal_nelec_nband.o\
249250
read_pseudo.o\
250251
cal_wfc.o\
251-
pot_sep.o\
252+
setup_estate_pw.cpp\
252253

253254
OBJS_ELECSTAT_LCAO=elecstate_lcao.o\
254255
elecstate_lcao_cal_tau.o\

source/source_esolver/esolver_ks_pw.cpp

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <chrono>
3333

3434
#include "source_pw/module_pwdft/setup_pot.h" // mohan add 20250929
35+
#include "source_estate/setup_estate_pw.h" // mohan add 20251005
3536
#include "source_io/ctrl_output_pw.h" // mohan add 20250927
3637

3738
namespace ModuleESolver
@@ -51,16 +52,6 @@ ESolver_KS_PW<T, Device>::~ESolver_KS_PW()
5152
// delete Hamilt
5253
this->deallocate_hamilt();
5354

54-
if (this->vsep_cell != nullptr)
55-
{
56-
delete this->vsep_cell;
57-
}
58-
59-
if (this->pelec != nullptr)
60-
{
61-
delete reinterpret_cast<elecstate::ElecStatePW<T, Device>*>(this->pelec);
62-
this->pelec = nullptr;
63-
}
6455

6556
if (PARAM.inp.device == "gpu" || PARAM.inp.precision == "single")
6657
{
@@ -97,53 +88,10 @@ void ESolver_KS_PW<T, Device>::before_all_runners(UnitCell& ucell, const Input_p
9788
//! Call before_all_runners() of ESolver_KS
9889
ESolver_KS<T, Device>::before_all_runners(ucell, inp);
9990

100-
//! Initialize ElecState, set pelec pointer
101-
if (this->pelec == nullptr)
102-
{
103-
if (inp.esolver_type == "sdft")
104-
{
105-
//! SDFT only supports double precision currently
106-
this->pelec = new elecstate::ElecStatePW_SDFT<std::complex<double>, Device>(this->pw_wfc,
107-
&(this->chr), &(this->kv), &ucell, &(this->ppcell),
108-
this->pw_rhod, this->pw_rho, this->pw_big);
109-
}
110-
else
111-
{
112-
this->pelec = new elecstate::ElecStatePW<T, Device>(this->pw_wfc,
113-
&(this->chr), &(this->kv), &ucell, &this->ppcell,
114-
this->pw_rhod, this->pw_rho, this->pw_big);
115-
}
116-
}
117-
118-
//! Set the cell volume variable in pelec
119-
this->pelec->omega = ucell.omega;
120-
121-
//! Inititlize the charge density.
122-
this->chr.allocate(inp.nspin);
123-
124-
//! Initialize DFT-1/2
125-
if (PARAM.inp.dfthalf_type > 0)
126-
{
127-
this->vsep_cell = new VSep;
128-
this->vsep_cell->init_vsep(*this->pw_rhod, ucell.sep_cell);
129-
}
130-
131-
//! Initialize the potential.
132-
if (this->pelec->pot == nullptr)
133-
{
134-
this->pelec->pot = new elecstate::Potential(this->pw_rhod,
135-
this->pw_rho, &ucell, &this->locpp.vloc, &(this->sf),
136-
&(this->solvent), &(this->pelec->f_en.etxc), &(this->pelec->f_en.vtxc), this->vsep_cell);
137-
}
138-
139-
//! Initalize local pseudopotential
140-
this->locpp.init_vloc(ucell, this->pw_rhod);
141-
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL");
142-
143-
//! Initalize non-local pseudopotential
144-
this->ppcell.init(ucell, &this->sf, this->pw_wfc);
145-
this->ppcell.init_vnl(ucell, this->pw_rhod);
146-
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL");
91+
//! setup and allocation for pelec, charge density, potentials, etc.
92+
elecstate::setup_estate_pw(ucell, this->kv, this->sf, this->pelec, this->chr,
93+
this->locpp, this->ppcell, this->vsep_cell, this->pw_wfc, this->pw_rho,
94+
this->pw_rhod, this->pw_big, this->solvent, inp);
14795

14896
//! Allocate and initialize psi
14997
this->p_psi_init = new psi::PSIInit<T, Device>(inp.init_wfc,
@@ -160,17 +108,6 @@ void ESolver_KS_PW<T, Device>::before_all_runners(UnitCell& ucell, const Input_p
160108

161109
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT BASIS");
162110

163-
//! Setup occupations
164-
if (inp.ocp)
165-
{
166-
elecstate::fixed_weights(inp.ocp_kb,
167-
inp.nbands,
168-
inp.nelec,
169-
this->pelec->klist,
170-
this->pelec->wg,
171-
this->pelec->skip_weights);
172-
}
173-
174111
//! Initialize exx pw
175112
if (inp.calculation == "scf" || inp.calculation == "relax" || inp.calculation == "cell-relax"
176113
|| inp.calculation == "md")
@@ -604,7 +541,8 @@ void ESolver_KS_PW<T, Device>::after_all_runners(UnitCell& ucell)
604541
this->pw_rho, this->pw_rhod, this->chr, this->kv, this->psi,
605542
this->kspw_psi, this->__kspw_psi, this->sf,
606543
this->ppcell, this->solvent, this->ctx, this->Pgrid, PARAM.inp);
607-
544+
545+
elecstate::teardown_estate_pw(this->pelec, this->vsep_cell);
608546
}
609547

610548
template class ESolver_KS_PW<std::complex<float>, base_device::DEVICE_CPU>;

source/source_estate/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ list(APPEND objects
3737
cal_nelec_nband.cpp
3838
read_pseudo.cpp
3939
cal_wfc.cpp
40+
setup_estate_pw.cpp
4041
)
4142

4243
if(ENABLE_LCAO)
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#include "source_estate/setup_estate_pw.h"
2+
3+
template <typename T, typename Device>
4+
void elecstate::setup_estate_pw(UnitCell& ucell, // unitcell
5+
const K_Vectors &kv, // kpoints
6+
Structure_Factor &sf, // structure factors
7+
elecstate::ElecState *pelec, // pointer of electrons
8+
const Charge &chr, // charge density
9+
pseudopot_cell_vl &locpp, // local pseudopotentials
10+
pseudopot_cell_vnl &ppcell, // non-local pseudopotentials
11+
VSep* vsep_cell, // U-1/2 method
12+
ModulePW::PW_Basis_K *pw_wfc, // pw for wfc
13+
const ModulePW::PW_Basis *pw_rho, // pw for rho
14+
const ModulePW::PW_Basis *pw_rhod, // pw for rhod
15+
const ModulePW::PW_Basis_Big* pw_big, // pw for big grid
16+
surchem &solvent, // solvent
17+
const Input_para& inp) // input parameters
18+
{
19+
ModuleBase::TITLE("elecstate", "setup_estate_pw");
20+
21+
//! Initialize ElecState, set pelec pointer
22+
if (pelec == nullptr)
23+
{
24+
if (inp.esolver_type == "sdft")
25+
{
26+
//! SDFT only supports double precision currently
27+
pelec = new elecstate::ElecStatePW_SDFT<std::complex<double>, Device>(pw_wfc,
28+
&chr, &kv, &ucell, &ppcell,
29+
pw_rhod, pw_rho, pw_big);
30+
}
31+
else
32+
{
33+
pelec = new elecstate::ElecStatePW<T, Device>(pw_wfc,
34+
&chr, &kv, &ucell, &ppcell,
35+
pw_rhod, pw_rho, pw_big);
36+
}
37+
}
38+
39+
//! Set the cell volume variable in pelec
40+
pelec->omega = ucell.omega;
41+
42+
//! Inititlize the charge density.
43+
chr.allocate(inp.nspin);
44+
45+
//! Initialize DFT-1/2
46+
if (PARAM.inp.dfthalf_type > 0)
47+
{
48+
vsep_cell = new VSep;
49+
vsep_cell->init_vsep(*pw_rhod, ucell.sep_cell);
50+
}
51+
52+
//! Initialize the potential.
53+
if (pelec->pot == nullptr)
54+
{
55+
pelec->pot = new elecstate::Potential(pw_rhod,
56+
pw_rho, &ucell, &locpp.vloc, &sf,
57+
&solvent, &(pelec->f_en.etxc), &(pelec->f_en.vtxc), vsep_cell);
58+
}
59+
60+
//! Initalize local pseudopotential
61+
locpp.init_vloc(ucell, pw_rhod);
62+
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "LOCAL POTENTIAL");
63+
64+
//! Initalize non-local pseudopotential
65+
ppcell.init(ucell, &sf, pw_wfc);
66+
ppcell.init_vnl(ucell, pw_rhod);
67+
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "NON-LOCAL POTENTIAL");
68+
69+
//! Setup occupations
70+
if (inp.ocp)
71+
{
72+
elecstate::fixed_weights(inp.ocp_kb,
73+
inp.nbands,
74+
inp.nelec,
75+
pelec->klist,
76+
pelec->wg,
77+
pelec->skip_weights);
78+
}
79+
80+
return;
81+
}
82+
83+
84+
void elecstate::teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell)
85+
{
86+
ModuleBase::TITLE("elecstate", "teardown_estate_pw");
87+
88+
if (vsep_cell != nullptr)
89+
{
90+
delete vsep_cell;
91+
}
92+
93+
if (pelec != nullptr)
94+
{
95+
delete reinterpret_cast<elecstate::ElecStatePW<T, Device>*>(pelec);
96+
pelec = nullptr;
97+
}
98+
}
99+
100+
101+
template void elecstate::setup_estate_pw<std::complex<float>, base_device::DEVICE_CPU>(
102+
UnitCell& ucell, // unitcell
103+
const K_Vectors &kv, // kpoints
104+
Structure_Factor &sf, // structure factors
105+
elecstate::ElecState *pelec, // pointer of electrons
106+
const Charge &chr, // charge density
107+
pseudopot_cell_vl &locpp, // local pseudopotentials
108+
pseudopot_cell_vnl &ppcell, // non-local pseudopotentials
109+
VSep* vsep_cell, // U-1/2 method
110+
ModulePW::PW_Basis_K *pw_wfc, // pw for wfc
111+
const ModulePW::PW_Basis *pw_rho, // pw for rho
112+
const ModulePW::PW_Basis *pw_rhod, // pw for rhod
113+
const ModulePW::PW_Basis_Big* pw_big, // pw for big grid
114+
surchem &solvent, // solvent
115+
const Input_para& inp); // input parameters
116+
117+
template void elecstate::setup_estate_pw<std::complex<double>, base_device::DEVICE_CPU>(
118+
UnitCell& ucell, // unitcell
119+
const K_Vectors &kv, // kpoints
120+
Structure_Factor &sf, // structure factors
121+
elecstate::ElecState *pelec, // pointer of electrons
122+
const Charge &chr, // charge density
123+
pseudopot_cell_vl &locpp, // local pseudopotentials
124+
pseudopot_cell_vnl &ppcell, // non-local pseudopotentials
125+
VSep* vsep_cell, // U-1/2 method
126+
ModulePW::PW_Basis_K *pw_wfc, // pw for wfc
127+
const ModulePW::PW_Basis *pw_rho, // pw for rho
128+
const ModulePW::PW_Basis *pw_rhod, // pw for rhod
129+
const ModulePW::PW_Basis_Big* pw_big, // pw for big grid
130+
surchem &solvent, // solvent
131+
const Input_para& inp); // input parameters
132+
133+
134+
135+
#if ((defined __CUDA) || (defined __ROCM))
136+
137+
template void elecstate::setup_estate_pw<std::complex<float>, base_device::DEVICE_GPU>(
138+
UnitCell& ucell, // unitcell
139+
const K_Vectors &kv, // kpoints
140+
Structure_Factor &sf, // structure factors
141+
elecstate::ElecState *pelec, // pointer of electrons
142+
const Charge &chr, // charge density
143+
pseudopot_cell_vl &locpp, // local pseudopotentials
144+
pseudopot_cell_vnl &ppcell, // non-local pseudopotentials
145+
VSep* vsep_cell, // U-1/2 method
146+
ModulePW::PW_Basis_K *pw_wfc, // pw for wfc
147+
const ModulePW::PW_Basis *pw_rho, // pw for rho
148+
const ModulePW::PW_Basis *pw_rhod, // pw for rhod
149+
const ModulePW::PW_Basis_Big* pw_big, // pw for big grid
150+
surchem &solvent, // solvent
151+
const Input_para& inp); // input parameters
152+
153+
template void elecstate::setup_estate_pw<std::complex<double>, base_device::DEVICE_GPU>(
154+
UnitCell& ucell, // unitcell
155+
const K_Vectors &kv, // kpoints
156+
Structure_Factor &sf, // structure factors
157+
elecstate::ElecState *pelec, // pointer of electrons
158+
const Charge &chr, // charge density
159+
pseudopot_cell_vl &locpp, // local pseudopotentials
160+
pseudopot_cell_vnl &ppcell, // non-local pseudopotentials
161+
VSep* vsep_cell, // U-1/2 method
162+
ModulePW::PW_Basis_K *pw_wfc, // pw for wfc
163+
const ModulePW::PW_Basis *pw_rho, // pw for rho
164+
const ModulePW::PW_Basis *pw_rhod, // pw for rhod
165+
const ModulePW::PW_Basis_Big* pw_big, // pw for big grid
166+
surchem &solvent, // solvent
167+
const Input_para& inp); // input parameters
168+
169+
#endif
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef SETUP_ESTATE_PW_H
2+
#define SETUP_ESTATE_PW_H
3+
4+
#include "source_base/module_device/device.h" // use Device
5+
#include "source_cell/unitcell.h"
6+
#include "source_cell/klist.h"
7+
#include "source_pw/module_pwdft/structure_factor.h"
8+
#include "source_estate/elecstate.h"
9+
#include "source_pw/module_pwdft/VL_in_pw.h"
10+
#include "source_pw/module_pwdft/VSep_in_pw.h"
11+
12+
namespace elecstate
13+
{
14+
15+
template <typename T, typename Device>
16+
void setup_estate_pw(UnitCell& ucell, // unitcell
17+
const K_Vectors &kv, // kpoints
18+
Structure_Factor &sf, // structure factors
19+
elecstate::ElecState *pelec, // pointer of electrons
20+
const Charge &chr, // charge density
21+
pseudopot_cell_vl &locpp, // local pseudopotentials
22+
pseudopot_cell_vnl &ppcell, // non-local pseudopotentials
23+
VSep* vsep_cell, // U-1/2 method
24+
ModulePW::PW_Basis_K *pw_wfc, // pw for wfc
25+
const ModulePW::PW_Basis *pw_rho, // pw for rho
26+
const ModulePW::PW_Basis *pw_rhod, // pw for rhod
27+
const ModulePW::PW_Basis_Big* pw_big, // pw for big grid
28+
surchem &solvent, // solvent
29+
const Input_para& inp); // input parameters
30+
31+
void teardown_estate_pw(elecstate::ElecState *pelec, VSep* vsep_cell);
32+
33+
}
34+
35+
36+
#endif

source/source_pw/module_pwdft/setup_pot.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void pw::setup_pot(const int istep,
2323
const ModulePW::PW_Basis *pw_rhod, // pw for rhod
2424
const Input_para& inp) // input parameters
2525
{
26+
ModuleBase::TITLE("pw", "setup_pot");
2627
//----------------------------------------------------------
2728
//! 1) Renew local pseudopotential
2829
//----------------------------------------------------------

0 commit comments

Comments
 (0)