From a010ae90811c411405f0e15d82734142458bc182 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Wed, 24 Jul 2024 11:01:06 -0400 Subject: [PATCH] fix(lmp): call model deviation interface without atomic properties when they are not requested (#4012) ## Summary by CodeRabbit - **New Features** - Enhanced computation flexibility by introducing conditional logic for handling different sets of parameters based on specific flags. - **Bug Fixes** - Improved robustness by ensuring computations do not attempt to access uninitialized elements, preventing potential errors. - **Refactor** - Modified control flow to accommodate various computational scenarios, optimizing the function's performance. Signed-off-by: Jinzhe Zeng --- source/lmp/pair_deepmd.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/source/lmp/pair_deepmd.cpp b/source/lmp/pair_deepmd.cpp index d4fbdd3363..2cb6cfacd4 100644 --- a/source/lmp/pair_deepmd.cpp +++ b/source/lmp/pair_deepmd.cpp @@ -662,12 +662,23 @@ void PairDeepMD::compute(int eflag, int vflag) { vector all_energy; vector> all_atom_energy; vector> all_atom_virial; - try { - deep_pot_model_devi.compute( - all_energy, all_force, all_virial, all_atom_energy, all_atom_virial, - dcoord, dtype, dbox, nghost, lmp_list, ago, fparam, daparam); - } catch (deepmd_compat::deepmd_exception &e) { - error->one(FLERR, e.what()); + if (!(eflag_atom || cvflag_atom)) { + try { + deep_pot_model_devi.compute(all_energy, all_force, all_virial, dcoord, + dtype, dbox, nghost, lmp_list, ago, + fparam, daparam); + } catch (deepmd_compat::deepmd_exception &e) { + error->one(FLERR, e.what()); + } + } else { + try { + deep_pot_model_devi.compute(all_energy, all_force, all_virial, + all_atom_energy, all_atom_virial, dcoord, + dtype, dbox, nghost, lmp_list, ago, + fparam, daparam); + } catch (deepmd_compat::deepmd_exception &e) { + error->one(FLERR, e.what()); + } } // deep_pot_model_devi.compute_avg (dener, all_energy); // deep_pot_model_devi.compute_avg (dforce, all_force); @@ -677,9 +688,8 @@ void PairDeepMD::compute(int eflag, int vflag) { dener = all_energy[0]; dforce = all_force[0]; dvirial = all_virial[0]; - deatom = all_atom_energy[0]; - dvatom = all_atom_virial[0]; if (eflag_atom) { + deatom = all_atom_energy[0]; for (int ii = 0; ii < nlocal; ++ii) { eatom[ii] += scale[1][1] * deatom[ii] * ener_unit_cvt_factor; } @@ -688,6 +698,7 @@ void PairDeepMD::compute(int eflag, int vflag) { // interface the atomic virial computed by DeepMD // with the one used in centroid atoms if (cvflag_atom) { + dvatom = all_atom_virial[0]; for (int ii = 0; ii < nall; ++ii) { // vatom[ii][0] += 1.0 * dvatom[9*ii+0]; // vatom[ii][1] += 1.0 * dvatom[9*ii+4];