From d35c27167742bd457abc2a36a2fa95ca6b96312f Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 27 Oct 2023 19:10:55 -0400 Subject: [PATCH] limit the maximum number of iterations so the LAMMPS simulation will not stall --- src/compute_cluster_atom.cpp | 9 +++++++-- src/compute_fragment_atom.cpp | 10 ++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/compute_cluster_atom.cpp b/src/compute_cluster_atom.cpp index ae44fbcd37..0d60b8b993 100644 --- a/src/compute_cluster_atom.cpp +++ b/src/compute_cluster_atom.cpp @@ -29,6 +29,7 @@ using namespace LAMMPS_NS; +static constexpr int MAXLOOP = 100; /* ---------------------------------------------------------------------- */ ComputeClusterAtom::ComputeClusterAtom(LAMMPS *lmp, int narg, char **arg) : @@ -136,9 +137,11 @@ void ComputeClusterAtom::compute_peratom() int change, done, anychange; - while (true) { + int counter = 0; + // stop after MAXLOOP iterations + while (counter < MAXLOOP) { comm->forward_comm(this); - + ++counter; change = 0; while (true) { done = 1; @@ -177,6 +180,8 @@ void ComputeClusterAtom::compute_peratom() MPI_Allreduce(&change, &anychange, 1, MPI_INT, MPI_MAX, world); if (!anychange) break; } + if ((comm->me == 0) && (counter >= MAXLOOP)) + error->warning(FLERR, "Compute cluster/atom did not converge after {} iterations", MAXLOOP); } /* ---------------------------------------------------------------------- */ diff --git a/src/compute_fragment_atom.cpp b/src/compute_fragment_atom.cpp index 035f554c8d..7e3f3437b6 100644 --- a/src/compute_fragment_atom.cpp +++ b/src/compute_fragment_atom.cpp @@ -31,7 +31,8 @@ using namespace LAMMPS_NS; -#define BIG 1.0e20 +static constexpr double BIG = 1.0e20; +static constexpr int MAXLOOP = 100; /* ---------------------------------------------------------------------- */ @@ -145,8 +146,11 @@ void ComputeFragmentAtom::compute_peratom() commflag = 1; - while (true) { + int counter = 0; + // stop after MAXLOOP iterations + while (counter < MAXLOOP) { comm->forward_comm(this); + ++counter; done = 1; // set markflag = 0 for all owned atoms, for new iteration @@ -223,6 +227,8 @@ void ComputeFragmentAtom::compute_peratom() MPI_Allreduce(&done,&alldone,1,MPI_INT,MPI_MIN,world); if (alldone) break; } + if ((comm->me == 0) && (counter >= MAXLOOP)) + error->warning(FLERR, "Compute fragment/atom did not converge after {} iterations", MAXLOOP); } /* ---------------------------------------------------------------------- */