Skip to content

Commit 46fe2af

Browse files
committed
Stop after max trials in electric potential calc.
Sampling in dense environments could cause an infinite loop of no free space is found. This break after a certain (hardcoded) number of attempts and gives a warning.
1 parent e4f94be commit 46fe2af

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/analysis.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,13 +2263,26 @@ void ElectricPotential::setPolicy(const json& j) {
22632263
stride = j.at("stride").get<double>();
22642264
output_information["stride"] = stride;
22652265
applyPolicy = [&, stride] {
2266+
unsigned int cnt = 0;
22662267
auto origin = targets.begin();
22672268
do {
22682269
spc.geometry.randompos(origin->position, random);
2270+
cnt++;
2271+
if (cnt > max_overlap_trials) {
2272+
faunus_logger->warn("{}: Max number of overlaps reached. Using overlapping point", name);
2273+
break;
2274+
}
22692275
} while (overlapWithParticles(origin->position));
2276+
2277+
cnt = 0;
22702278
std::for_each(std::next(origin), targets.end(), [&](Target& target) {
22712279
do {
22722280
target.position = origin->position + stride * randomUnitVector(random);
2281+
cnt++;
2282+
if (cnt > max_overlap_trials) {
2283+
faunus_logger->warn("{}: Max number of overlaps reached. Using overlapping point", name);
2284+
break;
2285+
}
22732286
} while (overlapWithParticles(target.position));
22742287
std::advance(origin, 1);
22752288
});

src/analysis.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ class ElectricPotential : public Analysis {
228228
enum class Policies { FIXED, RANDOM_WALK, RANDOM_WALK_NO_OVERLAP, INVALID };
229229

230230
private:
231+
unsigned int max_overlap_trials = 100; //!< Maximum number of overlap checks before bailing out
231232
double histogram_resolution = 0.01; //!< Potential resolution
232233
unsigned int calculations_per_sample_event = 1;
233234
struct Target {

0 commit comments

Comments
 (0)