From 2946087b4534d005979182298129ca9b4a6a7a64 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sat, 22 Jun 2024 04:19:23 -0400 Subject: [PATCH] avoid access to uninitialized step_respa pointer in Nose-Hoover fixes --- src/INTEL/fix_nh_intel.cpp | 8 +++++++- src/RIGID/fix_shake.cpp | 30 +++++++++++++++++++----------- src/fix_nh.cpp | 22 +++++++++++++--------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/INTEL/fix_nh_intel.cpp b/src/INTEL/fix_nh_intel.cpp index 2c05c3f6fa..c551e31aab 100644 --- a/src/INTEL/fix_nh_intel.cpp +++ b/src/INTEL/fix_nh_intel.cpp @@ -26,6 +26,7 @@ #include "memory.h" #include "modify.h" #include "neighbor.h" +#include "respa.h" #include "update.h" #include @@ -339,8 +340,13 @@ void FixNHIntel::reset_dt() // If using respa, then remap is performed in innermost level - if (utils::strmatch(update->integrate_style,"^respa")) + if (utils::strmatch(update->integrate_style,"^respa")) { + auto respa_ptr = dynamic_cast(update->integrate); + if (!respa_ptr) error->all(FLERR, "Failure to access Respa style {}", update->integrate_style); + nlevels_respa = respa_ptr->nlevels; + step_respa = respa_ptr->step; dto = 0.5*step_respa[0]; + } if (pstat_flag) pdrag_factor = 1.0 - (update->dt * p_freq_max * drag / nc_pchain); diff --git a/src/RIGID/fix_shake.cpp b/src/RIGID/fix_shake.cpp index 15bd5d207f..633b8dc154 100644 --- a/src/RIGID/fix_shake.cpp +++ b/src/RIGID/fix_shake.cpp @@ -371,7 +371,9 @@ void FixShake::init() // if rRESPA, find associated fix that must exist // could have changed locations in fix list since created // set ptrs to rRESPA variables + // set respa to 0 if verlet is used and to 1 otherwise + respa = 0; fix_respa = nullptr; if (utils::strmatch(update->integrate_style,"^respa")) { if (update->whichflag > 0) { @@ -379,10 +381,12 @@ void FixShake::init() if (fixes.size() > 0) fix_respa = dynamic_cast(fixes.front()); else error->all(FLERR,"Run style respa did not create fix RESPA"); } - auto respa_style = dynamic_cast(update->integrate); - nlevels_respa = respa_style->nlevels; - loop_respa = respa_style->loop; - step_respa = respa_style->step; + auto respa_ptr = dynamic_cast(update->integrate); + if (!respa_ptr) error->all(FLERR, "Failure to access Respa style {}", update->integrate_style); + respa = 1; + nlevels_respa = respa_ptr->nlevels; + loop_respa = respa_ptr->loop; + step_respa = respa_ptr->step; } // set equilibrium bond distances @@ -473,18 +477,22 @@ void FixShake::setup(int vflag) next_output = (ntimestep/output_every)*output_every + output_every; } else next_output = -1; - // set respa to 0 if verlet is used and to 1 otherwise - - if (utils::strmatch(update->integrate_style,"^verlet")) - respa = 0; - else - respa = 1; - if (!respa) { dtv = update->dt; dtfsq = 0.5 * update->dt * update->dt * force->ftm2v; if (!rattle) dtfsq = update->dt * update->dt * force->ftm2v; } else { + auto respa_ptr = dynamic_cast(update->integrate); + if (!respa_ptr) error->all(FLERR, "Failure to access Respa style {}", update->integrate_style); + if (update->whichflag > 0) { + auto fixes = modify->get_fix_by_style("^RESPA"); + if (fixes.size() > 0) fix_respa = dynamic_cast(fixes.front()); + else error->all(FLERR,"Run style respa did not create fix RESPA"); + } + respa = 1; + nlevels_respa = respa_ptr->nlevels; + loop_respa = respa_ptr->loop; + step_respa = respa_ptr->step; dtv = step_respa[0]; dtf_innerhalf = 0.5 * step_respa[0] * force->ftm2v; dtf_inner = dtf_innerhalf; diff --git a/src/fix_nh.cpp b/src/fix_nh.cpp index 12272c3123..8d5017d0f3 100644 --- a/src/fix_nh.cpp +++ b/src/fix_nh.cpp @@ -53,12 +53,9 @@ enum{ISO,ANISO,TRICLINIC}; ---------------------------------------------------------------------- */ FixNH::FixNH(LAMMPS *lmp, int narg, char **arg) : - Fix(lmp, narg, arg), - rfix(nullptr), id_dilate(nullptr), irregular(nullptr), - id_temp(nullptr), id_press(nullptr), - eta(nullptr), eta_dot(nullptr), eta_dotdot(nullptr), - eta_mass(nullptr), etap(nullptr), etap_dot(nullptr), etap_dotdot(nullptr), - etap_mass(nullptr) + Fix(lmp, narg, arg), rfix(nullptr), id_dilate(nullptr), irregular(nullptr),step_respa(nullptr), + id_temp(nullptr), id_press(nullptr), eta(nullptr), eta_dot(nullptr), eta_dotdot(nullptr), + eta_mass(nullptr), etap(nullptr), etap_dot(nullptr), etap_dotdot(nullptr), etap_mass(nullptr) { if (narg < 4) utils::missing_cmd_args(FLERR, std::string("fix ") + style, error); @@ -712,8 +709,10 @@ void FixNH::init() else kspace_flag = 0; if (utils::strmatch(update->integrate_style,"^respa")) { - nlevels_respa = (dynamic_cast(update->integrate))->nlevels; - step_respa = (dynamic_cast(update->integrate))->step; + auto respa_ptr = dynamic_cast(update->integrate); + if (!respa_ptr) error->all(FLERR, "Failure to access Respa style {}", update->integrate_style); + nlevels_respa = respa_ptr->nlevels; + step_respa = respa_ptr->step; dto = 0.5*step_respa[0]; } @@ -1706,8 +1705,13 @@ void FixNH::reset_dt() // If using respa, then remap is performed in innermost level - if (utils::strmatch(update->integrate_style,"^respa")) + if (utils::strmatch(update->integrate_style,"^respa")) { + auto respa_ptr = dynamic_cast(update->integrate); + if (!respa_ptr) error->all(FLERR, "Failure to access Respa style {}", update->integrate_style); + nlevels_respa = respa_ptr->nlevels; + step_respa = respa_ptr->step; dto = 0.5*step_respa[0]; + } if (pstat_flag) pdrag_factor = 1.0 - (update->dt * p_freq_max * drag / nc_pchain);