Skip to content

Commit 424c7c0

Browse files
A-006Fisherd99
authored andcommitted
Refactor:Remove GlobalC::ucell in the module_surchem (deepmodeling#5641)
* update the cal_psedudo.cpp * change the cal_vel.cpp * change the cal_vcav.cpp * change the sol_force.cpp * update the test file in module_surchem * update the const ucell in the cal_pseduo.cpp * remove const_cast
1 parent b299d64 commit 424c7c0

File tree

13 files changed

+63
-54
lines changed

13 files changed

+63
-54
lines changed

source/module_elecstate/test/charge_extra_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Structure_Factor::Structure_Factor()
8787
Structure_Factor::~Structure_Factor()
8888
{
8989
}
90-
void Structure_Factor::setup_structure_factor(UnitCell* Ucell, const ModulePW::PW_Basis* rho_basis)
90+
void Structure_Factor::setup_structure_factor(const UnitCell* Ucell, const ModulePW::PW_Basis* rho_basis)
9191
{
9292
}
9393

source/module_hamilt_general/module_surchem/cal_pseudo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void surchem::gauss_charge(const UnitCell& cell,
88
complex<double>* N,
99
Structure_Factor* sf)
1010
{
11-
sf->setup_structure_factor(&GlobalC::ucell, rho_basis); // call strucFac(ntype,ngmc)
11+
sf->setup_structure_factor(&cell, rho_basis); // call strucFac(ntype,ngmc)
1212
for (int it = 0; it < cell.ntype; it++)
1313
{
1414
double RCS = GetAtom.atom_RCS[cell.atoms[it].ncpp.psd];

source/module_hamilt_general/module_surchem/cal_vcav.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
#include "module_hamilt_general/module_xc/xc_functional.h"
33
#include "surchem.h"
44

5-
void lapl_rho(const std::complex<double>* rhog, double* lapn, const ModulePW::PW_Basis* rho_basis)
5+
void lapl_rho(const double& tpiba2,
6+
const std::complex<double>* rhog,
7+
double* lapn,
8+
const ModulePW::PW_Basis* rho_basis)
69
{
710
std::complex<double> *gdrtmpg = new std::complex<double>[rho_basis->npw];
811
ModuleBase::GlobalFunc::ZEROS(lapn, rho_basis->nrxx);
@@ -22,9 +25,9 @@ void lapl_rho(const std::complex<double>* rhog, double* lapn, const ModulePW::PW
2225
// bring the gdr from G --> R
2326
rho_basis->recip2real(aux, aux);
2427
// remember to multily 2pi/a0, which belongs to G vectors.
25-
for (int ir = 0; ir < rho_basis->nrxx; ir++) {
26-
lapn[ir] -= aux[ir].real() * GlobalC::ucell.tpiba2;
27-
}
28+
for (int ir = 0; ir < rho_basis->nrxx; ir++)
29+
lapn[ir] -= aux[ir].real() * tpiba2;
30+
2831
}
2932

3033
delete[] gdrtmpg;
@@ -70,7 +73,7 @@ void surchem::createcavity(const UnitCell& ucell,
7073
ModuleBase::GlobalFunc::ZEROS(lapn, rho_basis->nrxx);
7174

7275
// nabla n
73-
XC_Functional::grad_rho(PS_TOTN, nablan, rho_basis, GlobalC::ucell.tpiba);
76+
XC_Functional::grad_rho(PS_TOTN, nablan, rho_basis, ucell.tpiba);
7477

7578
// |\nabla n |^2 = nablan_2
7679
for (int ir = 0; ir < rho_basis->nrxx; ir++)
@@ -79,7 +82,7 @@ void surchem::createcavity(const UnitCell& ucell,
7982
}
8083

8184
// Laplacian of n
82-
lapl_rho(PS_TOTN, lapn, rho_basis);
85+
lapl_rho(ucell.tpiba2,PS_TOTN, lapn, rho_basis);
8386

8487
//-------------------------------------------------------------
8588
// add -Lap(n)/|\nabla n| to vwork and copy \sqrt(|\nabla n|^2)
@@ -131,7 +134,7 @@ void surchem::createcavity(const UnitCell& ucell,
131134

132135
// \nabla(1 / |\nabla n|), ggn in real space
133136
ModuleBase::Vector3<double> *ggn = new ModuleBase::Vector3<double>[rho_basis->nrxx];
134-
XC_Functional::grad_rho(inv_gn, ggn, rho_basis, GlobalC::ucell.tpiba);
137+
XC_Functional::grad_rho(inv_gn, ggn, rho_basis, ucell.tpiba);
135138

136139
//-------------------------------------------------------------
137140
// add -(\nabla n . \nabla(1/ |\nabla n|)) to Vcav in real space

source/module_hamilt_general/module_surchem/cal_vel.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ void shape_gradn(const double* PS_TOTN_real, const ModulePW::PW_Basis* rho_basis
1717
}
1818

1919
void eps_pot(const double* PS_TOTN_real,
20+
const double& tpiba,
2021
const complex<double>* phi,
2122
const ModulePW::PW_Basis* rho_basis,
2223
double* d_eps,
@@ -36,7 +37,7 @@ void eps_pot(const double* PS_TOTN_real,
3637
double *phisq = new double[rho_basis->nrxx];
3738

3839
// nabla phi
39-
XC_Functional::grad_rho(phi, nabla_phi, rho_basis, GlobalC::ucell.tpiba);
40+
XC_Functional::grad_rho(phi, nabla_phi, rho_basis, tpiba);
4041

4142
for (int ir = 0; ir < rho_basis->nrxx; ir++)
4243
{
@@ -118,7 +119,7 @@ ModuleBase::matrix surchem::cal_vel(const UnitCell& cell,
118119
this->Ael *= cell.omega / rho_basis->nxyz;
119120

120121
// the 2nd item of tmp_Vel
121-
eps_pot(PS_TOTN_real, Sol_phi, rho_basis, epsilon, epspot);
122+
eps_pot(PS_TOTN_real, cell.tpiba, Sol_phi, rho_basis, epsilon, epspot);
122123

123124
for (int i = 0; i < rho_basis->nrxx; i++)
124125
{

source/module_hamilt_general/module_surchem/sol_force.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void force_cor_one(const UnitCell& cell,
3232
vloc_at[ig] = vloc(it, rho_basis->ig2igg[ig]) * phase;
3333
if(rho_basis->ig_gge0 == ig)
3434
{
35-
N[ig] = GlobalC::ucell.atoms[it].ncpp.zv / GlobalC::ucell.omega;
35+
N[ig] = cell.atoms[it].ncpp.zv / cell.omega;
3636
}
3737
else
3838
{
@@ -48,9 +48,9 @@ void force_cor_one(const UnitCell& cell,
4848
forcesol(iat, 2) += rho_basis->gcar[ig][2] * imag(conj(delta_phi_g[ig]) * N[ig]);
4949
}
5050

51-
forcesol(iat, 0) *= (GlobalC::ucell.tpiba * GlobalC::ucell.omega);
52-
forcesol(iat, 1) *= (GlobalC::ucell.tpiba * GlobalC::ucell.omega);
53-
forcesol(iat, 2) *= (GlobalC::ucell.tpiba * GlobalC::ucell.omega);
51+
forcesol(iat, 0) *= (cell.tpiba * cell.omega);
52+
forcesol(iat, 1) *= (cell.tpiba * cell.omega);
53+
forcesol(iat, 2) *= (cell.tpiba * cell.omega);
5454
//unit Ry/Bohr
5555
forcesol(iat, 0) *= 2 ;
5656
forcesol(iat, 1) *= 2 ;
@@ -128,9 +128,9 @@ void force_cor_two(const UnitCell& cell, const ModulePW::PW_Basis* rho_basis, Mo
128128
forcesol(iat, 2) -= rho_basis->gcar[ig][2] * imag(conj(Vcav_g[ig]+Vel_g[ig]) * n_pseudo[ig]);
129129
}
130130

131-
forcesol(iat, 0) *= (GlobalC::ucell.tpiba * GlobalC::ucell.omega);
132-
forcesol(iat, 1) *= (GlobalC::ucell.tpiba * GlobalC::ucell.omega);
133-
forcesol(iat, 2) *= (GlobalC::ucell.tpiba * GlobalC::ucell.omega);
131+
forcesol(iat, 0) *= (cell.tpiba * cell.omega);
132+
forcesol(iat, 1) *= (cell.tpiba * cell.omega);
133+
forcesol(iat, 2) *= (cell.tpiba * cell.omega);
134134
//eV/Ang
135135
forcesol(iat, 0) *= 2 ;
136136
forcesol(iat, 1) *= 2 ;
@@ -157,17 +157,17 @@ void surchem::cal_force_sol(const UnitCell& cell,
157157
ModuleBase::TITLE("surchem", "cal_force_sol");
158158
ModuleBase::timer::tick("surchem", "cal_force_sol");
159159

160-
int nat = GlobalC::ucell.nat;
160+
int nat = cell.nat;
161161
ModuleBase::matrix force1(nat, 3);
162162
ModuleBase::matrix force2(nat, 3);
163163

164164
force_cor_one(cell, rho_basis, vloc, force1);
165165
force_cor_two(cell, rho_basis,force2);
166166

167167
int iat = 0;
168-
for (int it = 0;it < GlobalC::ucell.ntype;it++)
168+
for (int it = 0;it < cell.ntype;it++)
169169
{
170-
for (int ia = 0;ia < GlobalC::ucell.atoms[it].na;ia++)
170+
for (int ia = 0;ia < cell.atoms[it].na;ia++)
171171
{
172172
for(int ipol = 0; ipol < 3; ipol++)
173173
{

source/module_hamilt_general/module_surchem/test/cal_pseudo_test.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class cal_pseudo_test : public testing::Test
2626
{
2727
protected:
2828
surchem solvent_model;
29+
UnitCell ucell;
2930
};
3031

3132
TEST_F(cal_pseudo_test, gauss_charge)
@@ -41,7 +42,7 @@ TEST_F(cal_pseudo_test, gauss_charge)
4142
double wfcecut;
4243
bool gamma_only;
4344

44-
Setcell::setupcell(GlobalC::ucell);
45+
Setcell::setupcell(ucell);
4546

4647
wfcecut = 80;
4748
gamma_only = false;
@@ -60,7 +61,7 @@ TEST_F(cal_pseudo_test, gauss_charge)
6061
#endif
6162
// pwtest.initgrids(lat0,latvec,wfcecut);
6263

63-
GlobalC::rhopw->initgrids(GlobalC::ucell.lat0, GlobalC::ucell.latvec, wfcecut);
64+
GlobalC::rhopw->initgrids(ucell.lat0, ucell.latvec, wfcecut);
6465

6566
GlobalC::rhopw->initparameters(gamma_only, wfcecut, distribution_type, xprime);
6667
GlobalC::rhopw->setuptransform();
@@ -75,7 +76,7 @@ TEST_F(cal_pseudo_test, gauss_charge)
7576
Structure_Factor sf;
7677
sf.nbspline = -1;
7778

78-
solvent_model.gauss_charge(GlobalC::ucell, GlobalC::rhopw, N, &sf);
79+
solvent_model.gauss_charge(ucell, GlobalC::rhopw, N, &sf);
7980

8081
EXPECT_NEAR(N[14].real(), 0.002, 1e-9);
8182
EXPECT_NEAR(N[16].real(), -0.001573534, 1e-9);
@@ -88,7 +89,7 @@ TEST_F(cal_pseudo_test, cal_pseudo)
8889
std::string precision_flag, device_flag;
8990
precision_flag = "double";
9091
device_flag = "cpu";
91-
92+
Setcell::setupcell(ucell);
9293
ModulePW::PW_Basis pwtest(device_flag, precision_flag);
9394
GlobalC::rhopw = &pwtest;
9495
ModuleBase::Matrix3 latvec;
@@ -115,7 +116,7 @@ TEST_F(cal_pseudo_test, cal_pseudo)
115116
#endif
116117
// pwtest.initgrids(lat0,latvec,wfcecut);
117118

118-
GlobalC::rhopw->initgrids(GlobalC::ucell.lat0, GlobalC::ucell.latvec, wfcecut);
119+
GlobalC::rhopw->initgrids(ucell.lat0, ucell.latvec, wfcecut);
119120
GlobalC::rhopw->initparameters(gamma_only, wfcecut, distribution_type, xprime);
120121
GlobalC::rhopw->setuptransform();
121122
GlobalC::rhopw->collect_local_pw();
@@ -135,7 +136,7 @@ TEST_F(cal_pseudo_test, cal_pseudo)
135136
}
136137

137138
complex<double>* PS_TOTN = new complex<double>[npw];
138-
solvent_model.cal_pseudo(GlobalC::ucell, GlobalC::rhopw, Porter_g, PS_TOTN, &sf);
139+
solvent_model.cal_pseudo(ucell, GlobalC::rhopw, Porter_g, PS_TOTN, &sf);
139140

140141
EXPECT_NEAR(PS_TOTN[16].real(), 0.098426466, 1e-9);
141142
EXPECT_NEAR(PS_TOTN[14].real(), 0.102, 1e-9);

source/module_hamilt_general/module_surchem/test/cal_totn_test.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class cal_totn_test : public testing::Test
2525
{
2626
protected:
2727
surchem solvent_model;
28+
UnitCell ucell;
2829
};
2930

3031
TEST_F(cal_totn_test, cal_totn)
@@ -40,7 +41,7 @@ TEST_F(cal_totn_test, cal_totn)
4041
double wfcecut;
4142
bool gamma_only;
4243

43-
Setcell::setupcell(GlobalC::ucell);
44+
Setcell::setupcell(ucell);
4445

4546
wfcecut = 80;
4647
gamma_only = false;
@@ -59,7 +60,7 @@ TEST_F(cal_totn_test, cal_totn)
5960
#endif
6061
// pwtest.initgrids(lat0,latvec,wfcecut);
6162

62-
GlobalC::rhopw->initgrids(GlobalC::ucell.lat0, GlobalC::ucell.latvec, wfcecut);
63+
GlobalC::rhopw->initgrids(ucell.lat0, ucell.latvec, wfcecut);
6364

6465
GlobalC::rhopw->initparameters(gamma_only, wfcecut, distribution_type, xprime);
6566
GlobalC::rhopw->setuptransform();
@@ -87,7 +88,7 @@ TEST_F(cal_totn_test, cal_totn)
8788
vloc[i] = 0.1;
8889
}
8990

90-
solvent_model.cal_totn(GlobalC::ucell, GlobalC::rhopw, Porter_g, N, TOTN, vloc);
91+
solvent_model.cal_totn(ucell, GlobalC::rhopw, Porter_g, N, TOTN, vloc);
9192

9293
EXPECT_NEAR(TOTN[0].real(), -0.0999496256, 1e-10);
9394
EXPECT_NEAR(TOTN[0].imag(), -1.299621928166352e-7, 1e-10);
@@ -103,7 +104,8 @@ TEST_F(cal_totn_test, induced_charge)
103104
std::string precision_flag, device_flag;
104105
precision_flag = "double";
105106
device_flag = "cpu";
106-
107+
Setcell::setupcell(ucell);
108+
107109
ModulePW::PW_Basis pwtest(device_flag, precision_flag);
108110
GlobalC::rhopw = &pwtest;
109111
ModuleBase::Matrix3 latvec;
@@ -128,15 +130,15 @@ TEST_F(cal_totn_test, induced_charge)
128130
#endif
129131
// pwtest.initgrids(lat0,latvec,wfcecut);
130132

131-
GlobalC::rhopw->initgrids(GlobalC::ucell.lat0, GlobalC::ucell.latvec, wfcecut);
133+
GlobalC::rhopw->initgrids(ucell.lat0, ucell.latvec, wfcecut);
132134

133135
GlobalC::rhopw->initparameters(gamma_only, wfcecut, distribution_type, xprime);
134136
GlobalC::rhopw->setuptransform();
135137
GlobalC::rhopw->collect_local_pw();
136138
GlobalC::rhopw->collect_uniqgg();
137139

138140
double fac;
139-
fac = ModuleBase::e2 * ModuleBase::FOUR_PI / (GlobalC::ucell.tpiba2 * GlobalC::rhopw->gg[0]);
141+
fac = ModuleBase::e2 * ModuleBase::FOUR_PI / (ucell.tpiba2 * GlobalC::rhopw->gg[0]);
140142
complex<double> delta_phi{-2.0347933860e-05, 4.5900395826e-07};
141143
complex<double> induced_charge;
142144
induced_charge = -delta_phi / fac;

source/module_hamilt_general/module_surchem/test/cal_vcav_test.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ class cal_vcav_test : public testing::Test
3030
{
3131
protected:
3232
surchem solvent_model;
33+
UnitCell ucell;
3334
};
3435
TEST_F(cal_vcav_test, lapl_rho)
3536
{
36-
Setcell::setupcell(GlobalC::ucell);
37+
Setcell::setupcell(ucell);
3738

3839
std::string precision_flag, device_flag;
3940
precision_flag = "double";
@@ -61,7 +62,7 @@ TEST_F(cal_vcav_test, lapl_rho)
6162
#ifdef __MPI
6263
GlobalC::rhopw->initmpi(1, 0, POOL_WORLD);
6364
#endif
64-
GlobalC::rhopw->initgrids(GlobalC::ucell.lat0, GlobalC::ucell.latvec, wfcecut);
65+
GlobalC::rhopw->initgrids(ucell.lat0, ucell.latvec, wfcecut);
6566

6667
GlobalC::rhopw->initparameters(gamma_only, wfcecut, distribution_type, xprime);
6768
GlobalC::rhopw->setuptransform();
@@ -93,7 +94,7 @@ TEST_F(cal_vcav_test, lapl_rho)
9394
GlobalC::rhopw->recip2real(aux, aux);
9495
for (int ir = 0; ir < nrxx; ir++)
9596
{
96-
lapn[ir] -= aux[ir].real() * GlobalC::ucell.tpiba2;
97+
lapn[ir] -= aux[ir].real() * ucell.tpiba2;
9798
}
9899
}
99100

@@ -140,7 +141,7 @@ TEST_F(cal_vcav_test, shape_gradn)
140141

141142
TEST_F(cal_vcav_test, createcavity)
142143
{
143-
Setcell::setupcell(GlobalC::ucell);
144+
Setcell::setupcell(ucell);
144145

145146
std::string precision_flag, device_flag;
146147
precision_flag = "double";
@@ -168,7 +169,7 @@ TEST_F(cal_vcav_test, createcavity)
168169
#ifdef __MPI
169170
GlobalC::rhopw->initmpi(1, 0, POOL_WORLD);
170171
#endif
171-
GlobalC::rhopw->initgrids(GlobalC::ucell.lat0, GlobalC::ucell.latvec, wfcecut);
172+
GlobalC::rhopw->initgrids(ucell.lat0, ucell.latvec, wfcecut);
172173

173174
GlobalC::rhopw->initparameters(gamma_only, wfcecut, distribution_type, xprime);
174175
GlobalC::rhopw->setuptransform();
@@ -190,7 +191,7 @@ TEST_F(cal_vcav_test, createcavity)
190191
PS_TOTN[ig] = 1e-7;
191192
}
192193

193-
solvent_model.createcavity(GlobalC::ucell, GlobalC::rhopw, PS_TOTN, vwork);
194+
solvent_model.createcavity(ucell, GlobalC::rhopw, PS_TOTN, vwork);
194195

195196
EXPECT_NEAR(vwork[0], 4.8556305312, 1e-10);
196197
EXPECT_NEAR(vwork[1], -2.1006480538, 1e-10);
@@ -201,7 +202,7 @@ TEST_F(cal_vcav_test, createcavity)
201202

202203
TEST_F(cal_vcav_test, cal_vcav)
203204
{
204-
Setcell::setupcell(GlobalC::ucell);
205+
Setcell::setupcell(ucell);
205206

206207
std::string precision_flag, device_flag;
207208
precision_flag = "double";
@@ -229,7 +230,7 @@ TEST_F(cal_vcav_test, cal_vcav)
229230
#ifdef __MPI
230231
GlobalC::rhopw->initmpi(1, 0, POOL_WORLD);
231232
#endif
232-
GlobalC::rhopw->initgrids(GlobalC::ucell.lat0, GlobalC::ucell.latvec, wfcecut);
233+
GlobalC::rhopw->initgrids(ucell.lat0, ucell.latvec, wfcecut);
233234

234235
GlobalC::rhopw->initparameters(gamma_only, wfcecut, distribution_type, xprime);
235236
GlobalC::rhopw->setuptransform();
@@ -252,7 +253,7 @@ TEST_F(cal_vcav_test, cal_vcav)
252253
int nspin = 2;
253254
solvent_model.Vcav.create(nspin, nrxx);
254255

255-
solvent_model.cal_vcav(GlobalC::ucell, GlobalC::rhopw, PS_TOTN, nspin);
256+
solvent_model.cal_vcav(ucell, GlobalC::rhopw, PS_TOTN, nspin);
256257

257258
EXPECT_NEAR(solvent_model.Vcav(0, 0), 4.8556305312, 1e-10);
258259
EXPECT_NEAR(solvent_model.Vcav(0, 1), -2.1006480538, 1e-10);

0 commit comments

Comments
 (0)