Skip to content

Commit 22659fc

Browse files
author
abacus_fixer
committed
refactor(dftu): cleanup comments, tests, parameter names (Step 5/5)
Final cleanup pass to align naming after the locale -> occ_mat rename: * dftu_lcao.cpp (~40 lines): - Replace identifier-style 'locale' / 'Locale' references in the contributeHR() doc block and inline comments with 'occ_mat' / 'Occ_mat', including stale 'via get_locale()' / 'get_locale uses' mentions that no longer match the API. - Rename local bool flag locale_not_init -> occ_mat_not_init. * dftu_pw.cpp (4 lines): Fix the 4 remaining comments that still referred to 'locale matrix' / 'reduce locale' / 'locale reduced' in the cal_occ_pw implementation notes. * dftu_occup.cpp (2 lines): Update the top-of-file function inventory comment: set_locale(ucell) -> set_occ_mat(ucell); get/set_locale_flat -> get/set_occ_mat_flat. * dftu_tools.cpp (12 lines): Rename bool parameter 'newlocale' -> 'new_occ_mat' and its 3 internal uses in cal_VU_pot_mat_{complex,real} and get_onebody_eff_pot signatures/bodies, so the parameter name matches the occ_mat naming convention. * dftu_core_test.cpp (28 lines): - copy_locale_to_flat -> copy_occ_mat_to_flat (def + 3 call sites) - set_locale_from_flat -> set_occ_mat_from_flat (def + 3 call sites) - LocaleRoundtripTest -> OccMatRoundtripTest (test fixture + 2 TEST_F) - Related header comments: 'copy_locale <-> set_locale roundtrip', 'nested locale matrix' updated accordingly. * dftu_pw_test.cpp (6 lines): - 'Locale accumulation from becp' -> 'Occupation matrix accumulation from becp' in the test description; - 'set_locale is tested via integration tests.' -> 'set_occ_mat is tested via integration tests.' Not touched: pure semantic/mathematical local variable names inside the test helpers (locale_up, locale_dn, locale_c, compute_vu(locale_val), compute_energy(locale_flat), etc.) - these are local symbolic placeholders independent of the Plus_U_Base member naming. 6 files changed, 49 insertions(+), 49 deletions(-).
1 parent e805788 commit 22659fc

6 files changed

Lines changed: 49 additions & 49 deletions

File tree

source/source_lcao/module_dftu/dftu_occup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#endif
77
#include "source_base/module_external/scalapack_connector.h"
88

9-
// copy_occ_mat(), zero_occ_mat(), mix_occ_mat(), set_locale(ucell),
10-
// get_locale_flat(), set_locale_flat()
9+
// copy_occ_mat(), zero_occ_mat(), mix_occ_mat(), set_occ_mat(ucell),
10+
// get_occ_mat_flat(), set_occ_mat_flat()
1111
// are now implemented in dftu_base.cpp as Plus_U_Base methods (inherited by Plus_U).
1212

1313
#ifdef __LCAO

source/source_lcao/module_dftu/dftu_tools.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "source_io/module_parameter/parameter.h"
44

55
#ifdef __LCAO
6-
void Plus_U::cal_VU_pot_mat_complex(const int spin, const bool newlocale, std::complex<double>* VU, const int npol)
6+
void Plus_U::cal_VU_pot_mat_complex(const int spin, const bool new_occ_mat, std::complex<double>* VU, const int npol)
77
{
88
ModuleBase::TITLE("Plus_U", "cal_VU_pot_mat_complex");
99
ModuleBase::GlobalFunc::ZEROS(VU, this->paraV->nloc);
@@ -53,7 +53,7 @@ void Plus_U::cal_VU_pot_mat_complex(const int spin, const bool newlocale, std::c
5353
}
5454
int m1_all = m1 + (2 * L + 1) * ipol1;
5555
int m2_all = m2 + (2 * L + 1) * ipol2;
56-
double val = get_onebody_eff_pot(it, iat, L, n, spin, m1_all, m2_all, newlocale);
56+
double val = get_onebody_eff_pot(it, iat, L, n, spin, m1_all, m2_all, new_occ_mat);
5757
VU[nu * this->paraV->nrow + mu] = std::complex<double>(val, 0.0);
5858
} // ipol2
5959
} // m2
@@ -67,7 +67,7 @@ void Plus_U::cal_VU_pot_mat_complex(const int spin, const bool newlocale, std::c
6767
return;
6868
}
6969

70-
void Plus_U::cal_VU_pot_mat_real(const int spin, const bool newlocale, double* VU, const int npol)
70+
void Plus_U::cal_VU_pot_mat_real(const int spin, const bool new_occ_mat, double* VU, const int npol)
7171
{
7272
ModuleBase::TITLE("Plus_U", "cal_VU_pot_mat_real");
7373
ModuleBase::GlobalFunc::ZEROS(VU, this->paraV->nloc);
@@ -118,7 +118,7 @@ void Plus_U::cal_VU_pot_mat_real(const int spin, const bool newlocale, double* V
118118
int m2_all = m2 + (2 * L + 1) * ipol2;
119119

120120
VU[nu * this->paraV->nrow + mu]
121-
= this->get_onebody_eff_pot(it, iat, L, n, spin, m1_all, m2_all, newlocale);
121+
= this->get_onebody_eff_pot(it, iat, L, n, spin, m1_all, m2_all, new_occ_mat);
122122

123123
} // ipol2
124124
} // m2
@@ -139,7 +139,7 @@ double Plus_U::get_onebody_eff_pot(const int T,
139139
const int spin,
140140
const int m0,
141141
const int m1,
142-
const bool newlocale)
142+
const bool new_occ_mat)
143143
{
144144
ModuleBase::TITLE("Plus_U", "get_onebody_eff_pot");
145145

@@ -156,7 +156,7 @@ double Plus_U::get_onebody_eff_pot(const int T,
156156
break;
157157

158158
case 3: // simplified formalism and FLL double counting
159-
if (newlocale)
159+
if (new_occ_mat)
160160
{
161161
if (Yukawa)
162162
{

source/source_lcao/module_dftu/test/dftu_core_test.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
* These tests target the most complex and bug-prone logic:
1212
* 1. eff_pot_pw_index calculation for mixed atom types and nspin modes
13-
* 2. copy_locale <-> set_locale roundtrip (3 data layouts)
13+
* 2. copy_occ_mat <-> set_occ_mat roundtrip (3 data layouts)
1414
* 3. VU effective potential formula (cal_type=3, FLL)
1515
* 4. Energy correction and double-counting terms
1616
***********************************************************************/
@@ -88,9 +88,9 @@ TEST_F(EffPotIndexTest, Nspin2and4_SplitAndPauli)
8888
}
8989

9090
// =====================================================================
91-
// 2. copy_locale <-> set_locale roundtrip
91+
// 2. copy_occ_mat <-> set_occ_mat roundtrip
9292
//
93-
// Tests the bidirectional conversion between nested locale matrix
93+
// Tests the bidirectional conversion between nested occ_mat matrix
9494
// and flat uom_array/uom_save arrays for all 3 nspin modes.
9595
// =====================================================================
9696

@@ -103,7 +103,7 @@ struct Matrix2D {
103103
const double& operator()(int i, int j) const { return data[i * nc + j]; }
104104
};
105105

106-
static void copy_locale_to_flat(
106+
static void copy_occ_mat_to_flat(
107107
const std::vector<Matrix2D>& locale_up,
108108
const std::vector<Matrix2D>& locale_dn,
109109
std::vector<double>& uom_save,
@@ -143,7 +143,7 @@ static void copy_locale_to_flat(
143143
}
144144
}
145145

146-
static void set_locale_from_flat(
146+
static void set_occ_mat_from_flat(
147147
const std::vector<double>& uom_array,
148148
std::vector<Matrix2D>& locale_up,
149149
std::vector<Matrix2D>& locale_dn,
@@ -183,13 +183,13 @@ static void set_locale_from_flat(
183183
}
184184
}
185185

186-
class LocaleRoundtripTest : public ::testing::Test
186+
class OccMatRoundtripTest : public ::testing::Test
187187
{
188188
protected:
189189
void SetUp() override {}
190190
};
191191

192-
TEST_F(LocaleRoundtripTest, Nspin1and2_SingleAndSplitLayout)
192+
TEST_F(OccMatRoundtripTest, Nspin1and2_SingleAndSplitLayout)
193193
{
194194
// nspin=1: single atom d-orbital roundtrip
195195
const int l = 2;
@@ -202,8 +202,8 @@ TEST_F(LocaleRoundtripTest, Nspin1and2_SingleAndSplitLayout)
202202

203203
std::vector<int> eff_pot_pw_index = {0};
204204
std::vector<double> uom_save(size, 0.0);
205-
copy_locale_to_flat(locale_up, locale_dn, uom_save, eff_pot_pw_index, 1);
206-
set_locale_from_flat(uom_save, locale_up, locale_dn, eff_pot_pw_index, 1);
205+
copy_occ_mat_to_flat(locale_up, locale_dn, uom_save, eff_pot_pw_index, 1);
206+
set_occ_mat_from_flat(uom_save, locale_up, locale_dn, eff_pot_pw_index, 1);
207207
for (int i = 0; i < size; i++)
208208
EXPECT_DOUBLE_EQ(locale_up[0].data[i], static_cast<double>(i + 1));
209209

@@ -215,22 +215,22 @@ TEST_F(LocaleRoundtripTest, Nspin1and2_SingleAndSplitLayout)
215215
locale_dn[0].data[i] = static_cast<double>(i + 100);
216216
}
217217
uom_save.assign(total, 0.0);
218-
copy_locale_to_flat(locale_up, locale_dn, uom_save, eff_pot_pw_index, 2);
218+
copy_occ_mat_to_flat(locale_up, locale_dn, uom_save, eff_pot_pw_index, 2);
219219
// Verify split layout
220220
for (int i = 0; i < size; i++)
221221
{
222222
EXPECT_DOUBLE_EQ(uom_save[i], static_cast<double>(i + 1));
223223
EXPECT_DOUBLE_EQ(uom_save[size + i], static_cast<double>(i + 100));
224224
}
225-
set_locale_from_flat(uom_save, locale_up, locale_dn, eff_pot_pw_index, 2);
225+
set_occ_mat_from_flat(uom_save, locale_up, locale_dn, eff_pot_pw_index, 2);
226226
for (int i = 0; i < size; i++)
227227
{
228228
EXPECT_DOUBLE_EQ(locale_up[0].data[i], static_cast<double>(i + 1));
229229
EXPECT_DOUBLE_EQ(locale_dn[0].data[i], static_cast<double>(i + 100));
230230
}
231231
}
232232

233-
TEST_F(LocaleRoundtripTest, Nspin4_PauliBlocks)
233+
TEST_F(OccMatRoundtripTest, Nspin4_PauliBlocks)
234234
{
235235
// 2 atoms: d(l=2), p(l=1)
236236
struct AtomSpec { int l; };
@@ -265,8 +265,8 @@ TEST_F(LocaleRoundtripTest, Nspin4_PauliBlocks)
265265
std::vector<double> uom_array(total, 0.0);
266266
std::vector<Matrix2D> locale_dn(specs.size()); // unused for nspin=4
267267

268-
copy_locale_to_flat(locale, locale_dn, uom_array, eff_pot_pw_index, 4);
269-
set_locale_from_flat(uom_array, locale, locale_dn, eff_pot_pw_index, 4);
268+
copy_occ_mat_to_flat(locale, locale_dn, uom_array, eff_pot_pw_index, 4);
269+
set_occ_mat_from_flat(uom_array, locale, locale_dn, eff_pot_pw_index, 4);
270270

271271
for (size_t i = 0; i < specs.size(); i++)
272272
for (int j = 0; j < sizes[i]; j++)

source/source_lcao/module_dftu/test/dftu_pw_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
* 2. Becp index logic: different index formulas for nspin=1/2 vs nspin=4
1313
* 3. VU effective potential: cal_occ_pw VU calculation for all nspin modes
1414
* 4. Energy calculation: E_U accumulation with correct weights
15-
* 5. Locale accumulation from becp: the core loop of cal_occ_pw
15+
* 5. Occupation matrix accumulation from becp: the core loop of cal_occ_pw
1616
* 6. Multi-atom split layout: [all_up | all_dn] layout for nspin=2
1717
* 7. OnsitePsOp kernel: vu application to ps for npol=1
1818
*
1919
* Strategy: test energy weights and becp index logic as pure
2020
* arithmetic — no need to link against full ABACUS libraries.
21-
* set_locale is tested via integration tests.
21+
* set_occ_mat is tested via integration tests.
2222
***********************************************************************/
2323

2424
class DftuPwTest : public ::testing::Test
@@ -215,7 +215,7 @@ TEST_F(DftuPwTest, EnergyNspin4_WithOffDiagonal)
215215
}
216216

217217
// =====================================================================
218-
// Locale accumulation from becp (cal_occ_pw core loop)
218+
// Occupation matrix accumulation from becp (cal_occ_pw core loop)
219219
// =====================================================================
220220

221221
TEST_F(DftuPwTest, LocaleAccumNspin12)

source/source_lcao/module_operator_lcao/dftu_lcao.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,23 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::cal_nlm_all(const Parallel_Orbi
177177
* @brief Contribute DFT+U Hamiltonian to real-space HR matrix
178178
*
179179
* @details This function handles different scenarios based on:
180-
* 1. Whether locale (occupation matrix) is read from file (is_occ_mat_initialized)
180+
* 1. Whether occ_mat (occupation matrix) is read from file (is_occ_mat_initialized)
181181
* 2. Spin configuration (nspin=1, 2, or 4)
182182
* 3. SCF iteration stage (first vs subsequent iterations)
183183
*
184-
* Case 1: Locale NOT initialized (!is_occ_mat_initialized)
184+
* Case 1: Occ_mat NOT initialized (!is_occ_mat_initialized)
185185
* - First electronic iteration: calculates occupation matrix from density matrix (DMR)
186186
* * Uses get_dmr(current_spin) to get real-space density matrix
187187
* * Accumulates contributions from all atom pairs via cal_occ()
188188
* * Performs MPI reduction to sum occ across processes
189189
* * Stores result via set_occ_mat_flat() for use in VU calculation
190190
* * For nspin=1: occ is scaled by 0.5 (since only one spin channel computed)
191-
* - Subsequent iterations: locale is computed fresh each iteration from updated DMR
191+
* - Subsequent iterations: occ_mat is computed fresh each iteration from updated DMR
192192
*
193-
* Case 2: Locale IS initialized (is_occ_mat_initialized, i.e., read from dm_onsite.txt file)
194-
* - First electronic iteration: uses pre-read locale directly without DMR calculation
193+
* Case 2: Occ_mat IS initialized (is_occ_mat_initialized, i.e., read from dm_onsite.txt file)
194+
* - First electronic iteration: uses pre-read occ_mat directly without DMR calculation
195195
* * Skips DMR-based occ calculation entirely
196-
* * Reads locale from stored data via get_locale()
196+
* * Reads locale from stored data via get_occ_mat()
197197
* * Different indexing for nspin=4 vs nspin=1/2 (see below)
198198
* - After first iteration: mark_occ_mat_dirty() is called to force recomputation
199199
*
@@ -212,7 +212,7 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::cal_nlm_all(const Parallel_Orbi
212212
* nspin=4 (non-collinear/SOC):
213213
* - Single 4x4 Pauli matrix representation per atom
214214
* - occ has 4*(2l+1)^2 elements (spin_fold=4)
215-
* - get_locale uses spin=0, ipol indices for Pauli blocks
215+
* - get_occ_mat uses spin=0, ipol indices for Pauli blocks
216216
* - mark_occ_mat_dirty() always called (current_spin check always true)
217217
* - No current_spin toggling (all spins handled simultaneously)
218218
*
@@ -228,12 +228,12 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::contributeHR()
228228
ModuleBase::TITLE("DFTU", "contributeHR");
229229
// Early exit conditions:
230230
// - get_dmr(0) == nullptr: DMR not available (typical in first iteration without file input)
231-
// - !is_occ_mat_initialized(): locale not read from file AND not yet computed from DMR
231+
// - !is_occ_mat_initialized(): occ_mat not read from file AND not yet computed from DMR
232232
// When both true, skip DFT+U contribution entirely (first iteration, no file input)
233233
const bool dmr_null = (this->dftu->get_dmr(0) == nullptr);
234-
const bool locale_not_init = !this->dftu->is_occ_mat_initialized();
234+
const bool occ_mat_not_init = !this->dftu->is_occ_mat_initialized();
235235

236-
if (dmr_null && locale_not_init)
236+
if (dmr_null && occ_mat_not_init)
237237
{
238238
return;
239239
}
@@ -277,7 +277,7 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::contributeHR()
277277
std::vector<double> occ(tlp1 * tlp1 * spin_fold, 0.0);
278278

279279
// ============================================================
280-
// BRANCH 1: Locale NOT initialized (compute from DMR)
280+
// BRANCH 1: Occ_mat NOT initialized (compute from DMR)
281281
// ============================================================
282282
// This branch is taken when:
283283
// - is_occ_mat_initialized() == false (no file read or omc != 0)
@@ -331,32 +331,32 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::contributeHR()
331331
this->dftu->set_occ_mat_flat(iat0, target_L, this->current_spin, occ);
332332
}
333333
// ============================================================
334-
// BRANCH 2: Locale IS initialized (use pre-read data)
334+
// BRANCH 2: Occ_mat IS initialized (use pre-read data)
335335
// ============================================================
336336
// This branch is taken when:
337-
// - is_occ_mat_initialized() == true (locale read from dm_onsite.txt file)
337+
// - is_occ_mat_initialized() == true (occ_mat read from dm_onsite.txt file)
338338
// - OR omc != 0 (occupation matrix control with dm_onsite_ini.txt)
339339
// Typical scenario: first SCF iteration with file input, or restart calculation
340340
else
341341
{
342342
// nspin=4: Non-collinear case with Pauli matrix representation
343-
// Locale stored as single 4x4 block per atom, with spin indices embedded
343+
// Occ_mat stored as single 4x4 block per atom, with spin indices embedded
344344
// in the matrix indices (ipol0, ipol1 for Pauli block indices)
345345
if (this->nspin == 4)
346346
{
347-
// For nspin=4, locale is stored as 4 stacked tlp1^2 blocks
347+
// For nspin=4, occ_mat is stored as 4 stacked tlp1^2 blocks
348348
// at offsets 0, tlp1^2, 2*tlp1^2, 3*tlp1^2 for the 4 Pauli channels.
349349
// Use get_occ_mat_flat to read the stacked blocks directly
350350
this->dftu->get_occ_mat_flat(iat0, target_L, occ);
351351
}
352352
// nspin=1 or nspin=2: Collinear spin case
353-
// Locale stored separately for each spin channel
353+
// Occ_mat stored separately for each spin channel
354354
else
355355
{
356356
for (int i = 0; i < static_cast<int>(occ.size()); i++)
357357
{
358358
// TODO: UNSAFE - current_spin must be correct for nspin=2.
359-
// If current_spin is not toggled properly, wrong spin channel's locale is read.
359+
// If current_spin is not toggled properly, wrong spin channel's occ_mat is read.
360360
// This can happen if contributeHR() is called out of expected order.
361361
occ[i] = this->dftu->get_occ_mat(iat0, target_L, 0, this->current_spin,
362362
i / (2 * target_L + 1), i % (2 * target_L + 1));
@@ -428,21 +428,21 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::contributeHR()
428428
ModuleBase::timer::end("DFTU", "cal_vu");
429429
}
430430

431-
// 6. Post-processing: Energy correction and locale state management
431+
// 6. Post-processing: Energy correction and occ_mat state management
432432
// For nspin=1: DFT+U energy computed for single spin channel, but should count both spins
433433
// set_double_energy() doubles the energy to account for degenerate spin-up/down
434434
if (this->nspin == 1)
435435
{
436436
this->dftu->set_double_energy();
437437
}
438438

439-
// 7. Mark locale as dirty to force recomputation in next iteration
439+
// 7. Mark occ_mat as dirty to force recomputation in next iteration
440440
// This is called when:
441441
// - nspin=4: Always (all spins handled simultaneously, current_spin==0==nspin-1)
442442
// - nspin=2: When current_spin==1 (after spin-down calculation, last spin channel)
443443
// - nspin=1: When current_spin==0==nspin-1 (always called)
444444
//
445-
// Purpose: Ensure locale is recomputed from updated DMR in next SCF iteration,
445+
// Purpose: Ensure occ_mat is recomputed from updated DMR in next SCF iteration,
446446
// rather than using stale pre-read data from file.
447447
// TODO: This logic is confusing. Consider explicit variable like `is_last_spin_channel`.
448448
if (this->current_spin == this->nspin - 1 || this->nspin == 4)

source/source_pw/module_pwdft/dftu_pw.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/// spin channel selected by `isk[ik]` (not ik >= nk/2, which fails for kpar>1);
1717
///
1818
/// nspin=4 (npol=2): spinor calculation;
19-
/// locale has a single matrix of size (2*tlp1) x (2*tlp1) per atom
19+
/// occ_mat has a single matrix of size (2*tlp1) x (2*tlp1) per atom
2020
/// storing all 4 Pauli blocks contiguously.
2121
void Plus_U_Base::cal_occ_pw(const int iter,
2222
const void* psi_in,
@@ -188,7 +188,7 @@ void Plus_U_Base::cal_occ_pw(const int iter,
188188
}
189189
#endif
190190

191-
// reduce locale from all k-pools
191+
// reduce occ_mat from all k-pools
192192
for(int iat = 0; iat < cell.nat; iat++)
193193
{
194194
const int it = cell.iat2it[iat];
@@ -221,7 +221,7 @@ void Plus_U_Base::cal_occ_pw(const int iter,
221221
size * 4);
222222
}
223223

224-
// save locale matrix for this iat to uom_array
224+
// save occ_mat matrix for this iat to uom_array
225225
if(this->uom_array.size() != 0)
226226
{
227227
for(int mm=0;mm<size;mm++)
@@ -249,7 +249,7 @@ void Plus_U_Base::cal_occ_pw(const int iter,
249249
Plus_U_Base::energy_u = 0.0;
250250
const double weight_eu = (Plus_U_Base::nspin == 1) ? 1.0 : (Plus_U_Base::nspin == 2) ? 0.5 : 0.25;
251251
const double diag_coeff = (Plus_U_Base::nspin == 4) ? 1.0 : 0.5;
252-
// calculate VU and energy (locale already reduced above)
252+
// calculate VU and energy (occ_mat already reduced above)
253253
for(int iat = 0; iat < cell.nat; iat++)
254254
{
255255
const int it = cell.iat2it[iat];

0 commit comments

Comments
 (0)