Skip to content

Commit 33593b1

Browse files
authored
fix(lmp): clear DPLR PPPM force cache (#5744)
## Summary - add a small helper to resize and zero the DPLR PPPM fele cache - clear fele before returning from PPPMDPLR::compute when qsqsum == 0 - reuse the same helper in init and fieldforce paths This addresses #5647 as a defensive consistency fix. In normal DeePMD DPLR usage, atom charges are fixed and are not changed from nonzero to all-zero during a run, so this stale-cache path should not be triggered by standard DeePMD workflows. It would only matter for unusual external charge mutation or reinitialization paths where qsqsum is refreshed to zero after a previous nonzero-charge PPPM/DPLR step. ## Tests - git diff --check - /tmp/deepmd-check-venv/bin/ruff check . - /tmp/deepmd-check-venv/bin/ruff format --check . <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved consistency in per-atom field/force handling by clearing the accumulator before early exits and during setup. * Reduced the chance of stale values carrying over between calculations. * **Refactor** * Consolidated repeated zeroing and resizing steps into a shared internal routine, simplifying the calculation flow and making the code easier to maintain. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 7b0050f commit 33593b1

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

source/lmp/pppm_dplr.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ PPPMDPLR::PPPMDPLR(LAMMPS* lmp)
4848

4949
/* ---------------------------------------------------------------------- */
5050

51+
void PPPMDPLR::clear_fele() {
52+
int nlocal = atom->nlocal;
53+
fele.resize(static_cast<size_t>(nlocal) * 3);
54+
fill(fele.begin(), fele.end(), 0.0);
55+
}
56+
57+
/* ---------------------------------------------------------------------- */
58+
5159
void PPPMDPLR::init() {
5260
// DPLR PPPM requires newton on, b/c it computes forces on ghost atoms
5361

@@ -57,10 +65,8 @@ void PPPMDPLR::init() {
5765

5866
PPPM::init();
5967

60-
int nlocal = atom->nlocal;
6168
// cout << " ninit pppm/dplr ---------------------- " << nlocal << endl;
62-
fele.resize(static_cast<size_t>(nlocal) * 3);
63-
fill(fele.begin(), fele.end(), 0.0);
69+
clear_fele();
6470
}
6571

6672
/* ----------------------------------------------------------------------
@@ -89,6 +95,7 @@ void PPPMDPLR::compute(int eflag, int vflag) {
8995
// return if there are no charges
9096

9197
if (qsqsum == 0.0) {
98+
clear_fele();
9299
return;
93100
}
94101

@@ -296,8 +303,7 @@ void PPPMDPLR::fieldforce_ik() {
296303
int nghost = atom->nghost;
297304
int nall = nlocal + nghost;
298305

299-
fele.resize(static_cast<size_t>(nlocal) * 3);
300-
fill(fele.begin(), fele.end(), 0.0);
306+
clear_fele();
301307

302308
for (i = 0; i < nlocal; i++) {
303309
nx = part2grid[i][0];
@@ -372,8 +378,7 @@ void PPPMDPLR::fieldforce_ad() {
372378
int nghost = atom->nghost;
373379
int nall = nlocal + nghost;
374380

375-
fele.resize(static_cast<size_t>(nlocal) * 3);
376-
fill(fele.begin(), fele.end(), 0.0);
381+
clear_fele();
377382

378383
for (i = 0; i < nlocal; i++) {
379384
nx = part2grid[i][0];

source/lmp/pppm_dplr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class PPPMDPLR : public PPPM {
3636
void fieldforce_ad() override;
3737

3838
private:
39+
void clear_fele();
3940
std::vector<double> fele;
4041
};
4142

0 commit comments

Comments
 (0)