Skip to content

Commit d74c70b

Browse files
author
abacus_fixer
committed
Refactor: remove legacy DFT+U occupation-matrix members
All readers and writers now go through occmat_ (OccupationMatrix), so the legacy Plus_U_Base members occ_mat, occ_mat_save and iatlnmipol2iwt are deleted together with their allocation block in init_base (the pot_uterm_pw_index / num_locale bookkeeping is kept). The IO free functions read_occup_m / local_occup_bcast now take an OccupationMatrix& instead of the raw nested vector, and write into it through the set()/mat() interface. write_occup_m / output already used the public get_occ_mat() accessors and are unchanged. Behavior is unchanged: make -j 30 and OMP_NUM_THREADS=1 ctest -R dftu (4/4) pass.
1 parent 43c8af0 commit d74c70b

4 files changed

Lines changed: 16 additions & 76 deletions

File tree

source/source_pw/module_pwdft/dftu_base.cpp

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,16 @@ void Plus_U_Base::init_base(UnitCell& cell,
7272

7373
this->occmat_.init(cell, orbital_corr, nspin, npol);
7474

75-
this->occ_mat.resize(cell.nat);
76-
this->occ_mat_save.resize(cell.nat);
7775
this->pot_uterm_pw_index.resize(cell.nat);
7876
int pot_index = 0;
7977

80-
this->iatlnmipol2iwt.resize(cell.nat);
81-
8278
int num_locale = 0;
8379
for (int it = 0; it < cell.ntype; ++it)
8480
{
8581
for (int ia = 0; ia < cell.atoms[it].na; ia++)
8682
{
8783
const int iat = cell.itia2iat(it, ia);
8884

89-
occ_mat[iat].resize(cell.atoms[it].nwl + 1);
90-
occ_mat_save[iat].resize(cell.atoms[it].nwl + 1);
91-
92-
this->iatlnmipol2iwt[iat].resize(cell.atoms[it].nwl + 1);
93-
9485
if(!has_correlated_orbital(it))
9586
{
9687
continue;
@@ -114,62 +105,18 @@ void Plus_U_Base::init_base(UnitCell& cell,
114105
{
115106
const int N = cell.atoms[it].l_nchi[l];
116107

117-
occ_mat[iat][l].resize(N);
118-
occ_mat_save[iat][l].resize(N);
119-
120108
for (int n = 0; n < N; n++)
121109
{
122110
if (nspin == 1 || nspin == 2)
123111
{
124-
occ_mat[iat][l][n].resize(2);
125-
occ_mat_save[iat][l][n].resize(2);
126-
127-
occ_mat[iat][l][n][0].create(2 * l + 1, 2 * l + 1);
128-
occ_mat[iat][l][n][1].create(2 * l + 1, 2 * l + 1);
129-
130-
occ_mat_save[iat][l][n][0].create(2 * l + 1, 2 * l + 1);
131-
occ_mat_save[iat][l][n][1].create(2 * l + 1, 2 * l + 1);
132112
num_locale += (2 * l + 1) * (2 * l + 1) * 2;
133113
}
134114
else if (nspin == 4)
135115
{
136-
occ_mat[iat][l][n].resize(1);
137-
occ_mat_save[iat][l][n].resize(1);
138-
139-
occ_mat[iat][l][n][0].create((2 * l + 1) * npol, (2 * l + 1) * npol);
140-
occ_mat_save[iat][l][n][0].create((2 * l + 1) * npol, (2 * l + 1) * npol);
141116
num_locale += (2 * l + 1) * (2 * l + 1) * npol * npol;
142117
}
143118
}
144119
}
145-
146-
this->iatlnmipol2iwt[iat].resize(cell.atoms[it].nwl + 1);
147-
for (int L = 0; L <= cell.atoms[it].nwl; L++)
148-
{
149-
this->iatlnmipol2iwt[iat][L].resize(cell.atoms[it].l_nchi[L]);
150-
151-
for (int n = 0; n < cell.atoms[it].l_nchi[L]; n++)
152-
{
153-
this->iatlnmipol2iwt[iat][L][n].resize(2 * L + 1);
154-
155-
for (int m = 0; m < 2 * L + 1; m++)
156-
{
157-
this->iatlnmipol2iwt[iat][L][n][m].resize(npol);
158-
}
159-
}
160-
}
161-
162-
for (int iw = 0; iw < cell.atoms[it].nw * npol; iw++)
163-
{
164-
int iw0 = iw / npol;
165-
int ipol = iw % npol;
166-
int iwt = cell.itiaiw2iwt(it, ia, iw);
167-
int l = cell.atoms[it].iw2l[iw0];
168-
int n = cell.atoms[it].iw2n[iw0];
169-
int m = cell.atoms[it].iw2m[iw0];
170-
171-
this->iatlnmipol2iwt[iat][l][n][m][ipol] = iwt;
172-
}
173120
}
174121
}
175122

@@ -189,10 +136,10 @@ void Plus_U_Base::init_base(UnitCell& cell,
189136
{
190137
std::stringstream sst;
191138
sst << global_readin_dir << "dm_onsite_ini.txt";
192-
DFTU_BASE::read_occup_m(cell, this->occmat_.data(), this->orbital_corr, this->occ_mat_ctrl,
139+
DFTU_BASE::read_occup_m(cell, this->occmat_, this->orbital_corr, this->occ_mat_ctrl,
193140
sst.str(), init_chg, nspin, npol);
194141
#ifdef __MPI
195-
DFTU_BASE::local_occup_bcast(cell, this->occmat_.data(), this->orbital_corr, nspin, npol);
142+
DFTU_BASE::local_occup_bcast(cell, this->occmat_, this->orbital_corr, nspin, npol);
196143
#endif
197144

198145
mark_occ_mat_initialized();
@@ -204,10 +151,10 @@ void Plus_U_Base::init_base(UnitCell& cell,
204151
{
205152
std::stringstream sst;
206153
sst << global_readin_dir << "dm_onsite.txt";
207-
DFTU_BASE::read_occup_m(cell, this->occmat_.data(), this->orbital_corr, this->occ_mat_ctrl,
154+
DFTU_BASE::read_occup_m(cell, this->occmat_, this->orbital_corr, this->occ_mat_ctrl,
208155
sst.str(), init_chg, nspin, npol);
209156
#ifdef __MPI
210-
DFTU_BASE::local_occup_bcast(cell, this->occmat_.data(), this->orbital_corr, nspin, npol);
157+
DFTU_BASE::local_occup_bcast(cell, this->occmat_, this->orbital_corr, nspin, npol);
211158
#endif
212159
mark_occ_mat_initialized();
213160
}

source/source_pw/module_pwdft/dftu_base.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ class Plus_U_Base
171171

172172
// --- Occupation matrices ---
173173
OccupationMatrix occmat_;
174-
// legacy arrays; initialized alongside occmat_ and removed once all
175-
// internal writers go through occmat_
176-
std::vector<std::vector<std::vector<std::vector<ModuleBase::matrix>>>> occ_mat;
177-
std::vector<std::vector<std::vector<std::vector<ModuleBase::matrix>>>> occ_mat_save;
178174

179175
// --- Internal state ---
180176
double energy_u = 0.0;
@@ -183,10 +179,6 @@ class Plus_U_Base
183179
std::string device;
184180
int kpar = 1;
185181

186-
// transform between iwt index and it, ia, L, N and m index
187-
std::vector<std::vector<std::vector<std::vector<std::vector<int>>>>>
188-
iatlnmipol2iwt;
189-
190182
void copy_occ_mat(const UnitCell& ucell);
191183
void zero_occ_mat(const UnitCell& ucell);
192184
void set_occ_mat(const UnitCell& ucell);

source/source_pw/module_pwdft/dftu_base_io.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ namespace DFTU_BASE
9393
{
9494

9595
void read_occup_m(const UnitCell& ucell,
96-
OccMatData& occ_mat,
96+
OccupationMatrix& occ,
9797
const std::vector<int>& orbital_corr,
9898
const int occ_mat_ctrl,
9999
const std::string& fn,
@@ -194,7 +194,7 @@ void read_occup_m(const UnitCell& ucell,
194194
for (int m1 = 0; m1 < 2 * L + 1; m1++)
195195
{
196196
ifdftu >> value;
197-
occ_mat[iat][L][zeta][spin](m0, m1) = value;
197+
occ.set(iat, L, zeta, spin, m0, m1, value);
198198
}
199199
ifdftu.ignore(150, '\n');
200200
}
@@ -220,7 +220,7 @@ void read_occup_m(const UnitCell& ucell,
220220
{
221221
int m1_all = m1 + (2 * L + 1) * ipol1;
222222
ifdftu >> value;
223-
occ_mat[iat][L][zeta][0](m0_all, m1_all) = value;
223+
occ.set(iat, L, zeta, 0, m0_all, m1_all, value);
224224
}
225225
}
226226
ifdftu.ignore(150, '\n');
@@ -252,7 +252,7 @@ void read_occup_m(const UnitCell& ucell,
252252
/// (matrix::c stores nr * nc consecutive doubles) instead of element by
253253
/// element.
254254
void local_occup_bcast(const UnitCell& ucell,
255-
OccMatData& occ_mat,
255+
OccupationMatrix& occ,
256256
const std::vector<int>& orbital_corr,
257257
int nspin,
258258
int npol)
@@ -289,14 +289,14 @@ void local_occup_bcast(const UnitCell& ucell,
289289
{
290290
for (int spin = 0; spin < 2; spin++)
291291
{
292-
Parallel_Common::bcast_double(occ_mat[iat][l][n][spin].c,
293-
occ_mat[iat][l][n][spin].nr * occ_mat[iat][l][n][spin].nc);
292+
Parallel_Common::bcast_double(occ.mat(iat, l, n, spin).c,
293+
occ.mat(iat, l, n, spin).nr * occ.mat(iat, l, n, spin).nc);
294294
}
295295
}
296296
else if (nspin == 4) // SOC
297297
{
298-
Parallel_Common::bcast_double(occ_mat[iat][l][n][0].c,
299-
occ_mat[iat][l][n][0].nr * occ_mat[iat][l][n][0].nc);
298+
Parallel_Common::bcast_double(occ.mat(iat, l, n, 0).c,
299+
occ.mat(iat, l, n, 0).nr * occ.mat(iat, l, n, 0).nc);
300300
}
301301
}
302302
}

source/source_pw/module_pwdft/dftu_base_io.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define DFTU_BASE_IO_H
33

44
#include "source_base/matrix.h"
5+
#include "source_pw/module_pwdft/occ_matrix.h"
56

67
#include <iosfwd>
78
#include <string>
@@ -16,13 +17,13 @@ namespace DFTU_BASE
1617
/// nested occupation-matrix type used by DFT+U: occ_mat[iat][l][n][spin](m0, m1)
1718
using OccMatData = std::vector<std::vector<std::vector<std::vector<ModuleBase::matrix>>>>;
1819

19-
/// Read the local occupation number matrix from file into occ_mat (rank 0 only).
20+
/// Read the local occupation number matrix from file (rank 0 only).
2021
///
2122
/// The file format matches the output of write_occup_m(). When the file can
2223
/// not be opened, the run quits with an error message that depends on
2324
/// occ_mat_ctrl and init_chg.
2425
void read_occup_m(const UnitCell& ucell,
25-
OccMatData& occ_mat,
26+
OccupationMatrix& occ,
2627
const std::vector<int>& orbital_corr,
2728
const int occ_mat_ctrl,
2829
const std::string& fn,
@@ -34,7 +35,7 @@ void read_occup_m(const UnitCell& ucell,
3435
///
3536
/// Implemented in dftu_base_io.cpp (only available in MPI builds).
3637
void local_occup_bcast(const UnitCell& ucell,
37-
OccMatData& occ_mat,
38+
OccupationMatrix& occ,
3839
const std::vector<int>& orbital_corr,
3940
int nspin,
4041
int npol);

0 commit comments

Comments
 (0)