Skip to content

Commit a9678d8

Browse files
author
abacus_fixer
committed
refactor(dftu): rename 'locale' to 'occ_mat' in DFT+U tests and input docs
The DFTU tests and input parameter docs used 'locale' as a variable and annotation name for the occupation matrix. This is confusing because 'locale' is also a C++ standard library concept (std::locale, localization). The actual physical quantity is the occupation matrix n_{mm'}, so rename to 'occ_mat' for clarity and consistency with the existing Plus_U_Base member 'occ_mat' and 'get_occ_mat()' accessor. Renames (variable scope limited to tests + comments): * dftu_pw_test.cpp: locale_c / locale_up / locale_dn -> occ_mat_c / occ_mat_up / occ_mat_dn (flattened occupation matrix passed to compute_pot_onsite_scalar); comments updated. * dftu_core_test.cpp: function parameters locale_up / locale_dn -> occ_mat_up / occ_mat_dn (Matrix2D per-atom occupation matrices). * dftu_lcao_op.cpp:192 comment 'Reads locale ...' -> 'Reads occ_mat ...' * input_parameter.h:115 annotation 'mix locale' -> 'mix occ_mat' (parameter name 'mixing_dftu' unchanged, no INPUT behavior change). * read_inp_estruc.cpp:760 annotation 'mix locale' -> 'mix occ_mat' Out of scope (intentionally untouched): * Plus_U_Base::occ_mat member -- already named 'occ_mat', no change. * Non-DFTU 'locale' occurrences (input_parameter.h is touched only for the DFT+U annotation line). Verification: - rg '\blocale\b' over DFTU scope + module_parameter returns no matches. - python3 tools/03_code_analysis/agent_governance_check.py --staged passes with no findings. Test/Doc plan: * Tests: pure rename of test-local variables and comments; no semantic change; existing tests in dftu_pw_test.cpp and dftu_core_test.cpp updated in lock-step. * Docs: no INPUT parameter name change (still 'mixing_dftu'); only the annotation string is reworded for clarity. parameters.yaml and input-main.md do not mention 'locale' so no update required there.
1 parent 36b5ac0 commit a9678d8

5 files changed

Lines changed: 117 additions & 117 deletions

File tree

source/source_io/module_parameter/input_parameter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct Input_para
112112
double mixing_gg0_min = 0.1;
113113
double mixing_angle = -10.0;
114114
bool mixing_tau = false; ///< whether to mix tau in mgga
115-
bool mixing_dftu = false; ///< whether to mix locale in DFT+U
115+
bool mixing_dftu = false; ///< whether to mix occ_mat in DFT+U
116116
bool mixing_dmr = false; ///< whether to mix real space density matrix
117117

118118
bool gamma_only = false; ///< for plane wave.

source/source_io/module_parameter/read_inp_estruc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ This setting takes effect only when the selected exchange-correlation functional
757757
}
758758
{
759759
Input_Item item("mixing_dftu");
760-
item.annotation = "whether to mix locale in DFT+U calculation";
760+
item.annotation = "whether to mix occ_mat in DFT+U calculation";
761761
item.category = "Electronic structure";
762762
item.type = "Boolean";
763763
item.description = R"(Whether to mix the occupation matrices.

source/source_lcao/module_dftu/dftu_lcao_op.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ void hamilt::DFTU<hamilt::OperatorLCAO<TK, TR>>::cal_nlm_all(const Parallel_Orbi
189189
* Case 2: Occ_mat IS initialized (is_occ_mat_initialized, i.e., read from dm_onsite.txt file)
190190
* - First electronic iteration: uses pre-read occ_mat directly without DMR calculation
191191
* * Skips DMR-based occ calculation entirely
192-
* * Reads locale from stored data via get_occ_mat()
192+
* * Reads occ_mat from stored data via get_occ_mat()
193193
* * Different indexing for nspin=4 vs nspin=1/2 (see below)
194194
* - After first iteration: mark_occ_mat_dirty() is called to force recomputation
195195
*

source/source_lcao/module_dftu/test/dftu_core_test.cpp

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -104,81 +104,81 @@ struct Matrix2D {
104104
};
105105

106106
static void copy_occ_mat_to_flat(
107-
const std::vector<Matrix2D>& locale_up,
108-
const std::vector<Matrix2D>& locale_dn,
107+
const std::vector<Matrix2D>& occ_mat_up,
108+
const std::vector<Matrix2D>& occ_mat_dn,
109109
std::vector<double>& uom_save,
110110
const std::vector<int>& pot_uterm_pw_index,
111111
int nspin)
112112
{
113113
if (nspin == 4)
114114
{
115-
for (size_t iat = 0; iat < locale_up.size(); iat++)
115+
for (size_t iat = 0; iat < occ_mat_up.size(); iat++)
116116
{
117-
int size = locale_up[iat].nr * locale_up[iat].nc;
117+
int size = occ_mat_up[iat].nr * occ_mat_up[iat].nc;
118118
for (int mm = 0; mm < size; mm++)
119-
uom_save[pot_uterm_pw_index[iat] + mm] = locale_up[iat].data[mm];
119+
uom_save[pot_uterm_pw_index[iat] + mm] = occ_mat_up[iat].data[mm];
120120
}
121121
}
122122
else if (nspin == 2) // split layout: [up | dn]
123123
{
124124
int half_size = uom_save.size() / 2;
125-
for (size_t iat = 0; iat < locale_up.size(); iat++)
125+
for (size_t iat = 0; iat < occ_mat_up.size(); iat++)
126126
{
127-
int size = locale_up[iat].nr * locale_up[iat].nc;
127+
int size = occ_mat_up[iat].nr * occ_mat_up[iat].nc;
128128
for (int mm = 0; mm < size; mm++)
129129
{
130-
uom_save[pot_uterm_pw_index[iat] + mm] = locale_up[iat].data[mm];
131-
uom_save[half_size + pot_uterm_pw_index[iat] + mm] = locale_dn[iat].data[mm];
130+
uom_save[pot_uterm_pw_index[iat] + mm] = occ_mat_up[iat].data[mm];
131+
uom_save[half_size + pot_uterm_pw_index[iat] + mm] = occ_mat_dn[iat].data[mm];
132132
}
133133
}
134134
}
135135
else // nspin=1: single spin channel
136136
{
137-
for (size_t iat = 0; iat < locale_up.size(); iat++)
137+
for (size_t iat = 0; iat < occ_mat_up.size(); iat++)
138138
{
139-
int size = locale_up[iat].nr * locale_up[iat].nc;
139+
int size = occ_mat_up[iat].nr * occ_mat_up[iat].nc;
140140
for (int mm = 0; mm < size; mm++)
141-
uom_save[pot_uterm_pw_index[iat] + mm] = locale_up[iat].data[mm];
141+
uom_save[pot_uterm_pw_index[iat] + mm] = occ_mat_up[iat].data[mm];
142142
}
143143
}
144144
}
145145

146146
static void set_occ_mat_from_flat(
147147
const std::vector<double>& uom_array,
148-
std::vector<Matrix2D>& locale_up,
149-
std::vector<Matrix2D>& locale_dn,
148+
std::vector<Matrix2D>& occ_mat_up,
149+
std::vector<Matrix2D>& occ_mat_dn,
150150
const std::vector<int>& pot_uterm_pw_index,
151151
int nspin)
152152
{
153153
if (nspin == 4)
154154
{
155-
for (size_t iat = 0; iat < locale_up.size(); iat++)
155+
for (size_t iat = 0; iat < occ_mat_up.size(); iat++)
156156
{
157-
int size = locale_up[iat].nr * locale_up[iat].nc;
157+
int size = occ_mat_up[iat].nr * occ_mat_up[iat].nc;
158158
for (int mm = 0; mm < size; mm++)
159-
locale_up[iat].data[mm] = uom_array[pot_uterm_pw_index[iat] + mm];
159+
occ_mat_up[iat].data[mm] = uom_array[pot_uterm_pw_index[iat] + mm];
160160
}
161161
}
162162
else if (nspin == 2)
163163
{
164164
int half_size = uom_array.size() / 2;
165-
for (size_t iat = 0; iat < locale_up.size(); iat++)
165+
for (size_t iat = 0; iat < occ_mat_up.size(); iat++)
166166
{
167-
int size = locale_up[iat].nr * locale_up[iat].nc;
167+
int size = occ_mat_up[iat].nr * occ_mat_up[iat].nc;
168168
for (int mm = 0; mm < size; mm++)
169169
{
170-
locale_up[iat].data[mm] = uom_array[pot_uterm_pw_index[iat] + mm];
171-
locale_dn[iat].data[mm] = uom_array[half_size + pot_uterm_pw_index[iat] + mm];
170+
occ_mat_up[iat].data[mm] = uom_array[pot_uterm_pw_index[iat] + mm];
171+
occ_mat_dn[iat].data[mm] = uom_array[half_size + pot_uterm_pw_index[iat] + mm];
172172
}
173173
}
174174
}
175175
else // nspin=1
176176
{
177-
for (size_t iat = 0; iat < locale_up.size(); iat++)
177+
for (size_t iat = 0; iat < occ_mat_up.size(); iat++)
178178
{
179-
int size = locale_up[iat].nr * locale_up[iat].nc;
179+
int size = occ_mat_up[iat].nr * occ_mat_up[iat].nc;
180180
for (int mm = 0; mm < size; mm++)
181-
locale_up[iat].data[mm] = uom_array[pot_uterm_pw_index[iat] + mm];
181+
occ_mat_up[iat].data[mm] = uom_array[pot_uterm_pw_index[iat] + mm];
182182
}
183183
}
184184
}
@@ -195,38 +195,38 @@ TEST_F(OccMatRoundtripTest, Nspin1and2_SingleAndSplitLayout)
195195
const int l = 2;
196196
const int size = (2 * l + 1) * (2 * l + 1); // 25
197197

198-
std::vector<Matrix2D> locale_up(1, Matrix2D(2 * l + 1, 2 * l + 1));
199-
std::vector<Matrix2D> locale_dn(1, Matrix2D(2 * l + 1, 2 * l + 1));
198+
std::vector<Matrix2D> occ_mat_up(1, Matrix2D(2 * l + 1, 2 * l + 1));
199+
std::vector<Matrix2D> occ_mat_dn(1, Matrix2D(2 * l + 1, 2 * l + 1));
200200
for (int i = 0; i < size; i++)
201-
locale_up[0].data[i] = static_cast<double>(i + 1);
201+
occ_mat_up[0].data[i] = static_cast<double>(i + 1);
202202

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

210210
// nspin=2: split layout [up | dn] with distinct values
211211
const int total = size * 2;
212212
for (int i = 0; i < size; i++)
213213
{
214-
locale_up[0].data[i] = static_cast<double>(i + 1);
215-
locale_dn[0].data[i] = static_cast<double>(i + 100);
214+
occ_mat_up[0].data[i] = static_cast<double>(i + 1);
215+
occ_mat_dn[0].data[i] = static_cast<double>(i + 100);
216216
}
217217
uom_save.assign(total, 0.0);
218-
copy_occ_mat_to_flat(locale_up, locale_dn, uom_save, pot_uterm_pw_index, 2);
218+
copy_occ_mat_to_flat(occ_mat_up, occ_mat_dn, uom_save, pot_uterm_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_occ_mat_from_flat(uom_save, locale_up, locale_dn, pot_uterm_pw_index, 2);
225+
set_occ_mat_from_flat(uom_save, occ_mat_up, occ_mat_dn, pot_uterm_pw_index, 2);
226226
for (int i = 0; i < size; i++)
227227
{
228-
EXPECT_DOUBLE_EQ(locale_up[0].data[i], static_cast<double>(i + 1));
229-
EXPECT_DOUBLE_EQ(locale_dn[0].data[i], static_cast<double>(i + 100));
228+
EXPECT_DOUBLE_EQ(occ_mat_up[0].data[i], static_cast<double>(i + 1));
229+
EXPECT_DOUBLE_EQ(occ_mat_dn[0].data[i], static_cast<double>(i + 100));
230230
}
231231
}
232232

@@ -253,39 +253,39 @@ TEST_F(OccMatRoundtripTest, Nspin4_PauliBlocks)
253253
offset += sizes[i];
254254
}
255255

256-
std::vector<Matrix2D> locale(specs.size());
256+
std::vector<Matrix2D> occ_mat(specs.size());
257257
for (size_t i = 0; i < specs.size(); i++)
258258
{
259259
int dim = (2 * specs[i].l + 1) * npol;
260-
locale[i] = Matrix2D(dim, dim);
260+
occ_mat[i] = Matrix2D(dim, dim);
261261
for (int j = 0; j < sizes[i]; j++)
262-
locale[i].data[j] = static_cast<double>(i * 1000 + j + 1);
262+
occ_mat[i].data[j] = static_cast<double>(i * 1000 + j + 1);
263263
}
264264

265265
std::vector<double> uom_array(total, 0.0);
266-
std::vector<Matrix2D> locale_dn(specs.size()); // unused for nspin=4
266+
std::vector<Matrix2D> occ_mat_dn(specs.size()); // unused for nspin=4
267267

268-
copy_occ_mat_to_flat(locale, locale_dn, uom_array, pot_uterm_pw_index, 4);
269-
set_occ_mat_from_flat(uom_array, locale, locale_dn, pot_uterm_pw_index, 4);
268+
copy_occ_mat_to_flat(occ_mat, occ_mat_dn, uom_array, pot_uterm_pw_index, 4);
269+
set_occ_mat_from_flat(uom_array, occ_mat, occ_mat_dn, pot_uterm_pw_index, 4);
270270

271271
for (size_t i = 0; i < specs.size(); i++)
272272
for (int j = 0; j < sizes[i]; j++)
273-
EXPECT_DOUBLE_EQ(locale[i].data[j], static_cast<double>(i * 1000 + j + 1));
273+
EXPECT_DOUBLE_EQ(occ_mat[i].data[j], static_cast<double>(i * 1000 + j + 1));
274274
}
275275

276276
// =====================================================================
277277
// 3. pot_onsite effective potential formula (cal_type=3, FLL)
278278
//
279-
// pot_onsite[m0,m1] = U * (0.5*delta(m0,m1) - locale[m0,m1]) (diagonal)
280-
// pot_onsite[m0,m1] = -U * locale[m0,m1] (off-diagonal)
279+
// pot_onsite[m0,m1] = U * (0.5*delta(m0,m1) - occ_mat[m0,m1]) (diagonal)
280+
// pot_onsite[m0,m1] = -U * occ_mat[m0,m1] (off-diagonal)
281281
// =====================================================================
282282

283-
static double compute_pot_onsite(double U_val, int m0, int m1, double locale_val)
283+
static double compute_pot_onsite(double U_val, int m0, int m1, double occ_mat_val)
284284
{
285285
if (m0 == m1)
286-
return U_val * (0.5 - locale_val);
286+
return U_val * (0.5 - occ_mat_val);
287287
else
288-
return -U_val * locale_val;
288+
return -U_val * occ_mat_val;
289289
}
290290

291291
class PotOnsitePotentialTest : public ::testing::Test
@@ -297,25 +297,25 @@ class PotOnsitePotentialTest : public ::testing::Test
297297
TEST_F(PotOnsitePotentialTest, Diagonal_HalfFilled)
298298
{
299299
double U = 4.0;
300-
double locale = 0.5; // half-filled
301-
double pot_onsite = compute_pot_onsite(U, 0, 0, locale);
300+
double occ_mat = 0.5; // half-filled
301+
double pot_onsite = compute_pot_onsite(U, 0, 0, occ_mat);
302302
EXPECT_DOUBLE_EQ(pot_onsite, 0.0); // U * (0.5 - 0.5) = 0
303303
}
304304

305305
TEST_F(PotOnsitePotentialTest, Diagonal_FullyOccupied)
306306
{
307307
double U = 4.0;
308-
double locale = 1.0; // fully occupied
309-
double pot_onsite = compute_pot_onsite(U, 0, 0, locale);
308+
double occ_mat = 1.0; // fully occupied
309+
double pot_onsite = compute_pot_onsite(U, 0, 0, occ_mat);
310310
EXPECT_DOUBLE_EQ(pot_onsite, -2.0); // U * (0.5 - 1.0) = -2.0
311311
}
312312

313313
TEST_F(PotOnsitePotentialTest, OffDiagonal)
314314
{
315315
double U = 5.0;
316-
double locale = 0.3;
317-
double pot_onsite = compute_pot_onsite(U, 0, 1, locale);
318-
EXPECT_DOUBLE_EQ(pot_onsite, -1.5); // -U * locale = -1.5
316+
double occ_mat = 0.3;
317+
double pot_onsite = compute_pot_onsite(U, 0, 1, occ_mat);
318+
EXPECT_DOUBLE_EQ(pot_onsite, -1.5); // -U * occ_mat = -1.5
319319
}
320320

321321
// =====================================================================
@@ -327,14 +327,14 @@ TEST_F(PotOnsitePotentialTest, OffDiagonal)
327327
class EnergyCorrectionTest : public ::testing::Test
328328
{
329329
protected:
330-
static double compute_energy(const std::vector<double>& locale_flat, int m_size, double U)
330+
static double compute_energy(const std::vector<double>& occ_mat_flat, int m_size, double U)
331331
{
332332
double nm_trace = 0.0, nm2_trace = 0.0;
333333
for (int m0 = 0; m0 < m_size; m0++)
334334
{
335-
nm_trace += locale_flat[m0 * m_size + m0];
335+
nm_trace += occ_mat_flat[m0 * m_size + m0];
336336
for (int m1 = 0; m1 < m_size; m1++)
337-
nm2_trace += locale_flat[m0 * m_size + m1] * locale_flat[m1 * m_size + m0];
337+
nm2_trace += occ_mat_flat[m0 * m_size + m1] * occ_mat_flat[m1 * m_size + m0];
338338
}
339339
return 0.5 * U * (nm_trace - nm2_trace);
340340
}
@@ -343,24 +343,24 @@ class EnergyCorrectionTest : public ::testing::Test
343343
TEST_F(EnergyCorrectionTest, HalfFilled_DOrbital)
344344
{
345345
const int m_size = 5;
346-
std::vector<double> locale(m_size * m_size, 0.0);
346+
std::vector<double> occ_mat(m_size * m_size, 0.0);
347347
for (int m = 0; m < m_size; m++)
348-
locale[m * m_size + m] = 0.5;
348+
occ_mat[m * m_size + m] = 0.5;
349349

350-
double energy = compute_energy(locale, m_size, 4.0);
350+
double energy = compute_energy(occ_mat, m_size, 4.0);
351351
// Tr(n) = 2.5, Tr(n^2) = 1.25, E = 0.5 * 4 * 1.25 = 2.5
352352
EXPECT_DOUBLE_EQ(energy, 2.5);
353353
}
354354

355355
TEST_F(EnergyCorrectionTest, OffDiagonal_Contribution)
356356
{
357357
const int m_size = 2;
358-
std::vector<double> locale = {
358+
std::vector<double> occ_mat = {
359359
0.3, 0.1,
360360
0.1, 0.3
361361
};
362362

363-
double energy = compute_energy(locale, m_size, 4.0);
363+
double energy = compute_energy(occ_mat, m_size, 4.0);
364364
// Tr(n) = 0.6, Tr(n^2) = 0.3^2 + 0.1^2 + 0.1^2 + 0.3^2 = 0.20
365365
// E = 0.5 * 4 * (0.6 - 0.20) = 0.8
366366
EXPECT_DOUBLE_EQ(energy, 0.8);
@@ -371,7 +371,7 @@ TEST_F(EnergyCorrectionTest, DoubleCounting_Energy)
371371
// E_dc = sum_{m1,m2,spin} pot_onsite[m1,m2] * n[m2,m1]
372372
const int m_size = 3;
373373
double U = 4.0;
374-
std::vector<double> locale = {
374+
std::vector<double> occ_mat = {
375375
0.5, 0.0, 0.0,
376376
0.0, 0.3, 0.0,
377377
0.0, 0.0, 0.2
@@ -381,9 +381,9 @@ TEST_F(EnergyCorrectionTest, DoubleCounting_Energy)
381381
for (int m1 = 0; m1 < m_size; m1++)
382382
for (int m2 = 0; m2 < m_size; m2++)
383383
{
384-
double pot_onsite = (m1 == m2) ? U * (0.5 - locale[m1 * m_size + m2])
385-
: -U * locale[m1 * m_size + m2];
386-
e_dc += pot_onsite * locale[m2 * m_size + m1];
384+
double pot_onsite = (m1 == m2) ? U * (0.5 - occ_mat[m1 * m_size + m2])
385+
: -U * occ_mat[m1 * m_size + m2];
386+
e_dc += pot_onsite * occ_mat[m2 * m_size + m1];
387387
}
388388

389389
// Only diagonal: m=0: 0*0.5=0, m=1: 0.8*0.3=0.24, m=2: 1.2*0.2=0.24

0 commit comments

Comments
 (0)