Skip to content

Commit

Permalink
Stop after max trials in electric potential calc.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mlund committed Jun 29, 2023
1 parent e4f94be commit 46fe2af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2263,13 +2263,26 @@ void ElectricPotential::setPolicy(const json& j) {
stride = j.at("stride").get<double>();
output_information["stride"] = stride;
applyPolicy = [&, stride] {
unsigned int cnt = 0;
auto origin = targets.begin();
do {
spc.geometry.randompos(origin->position, random);
cnt++;
if (cnt > max_overlap_trials) {
faunus_logger->warn("{}: Max number of overlaps reached. Using overlapping point", name);
break;
}
} while (overlapWithParticles(origin->position));

cnt = 0;
std::for_each(std::next(origin), targets.end(), [&](Target& target) {
do {
target.position = origin->position + stride * randomUnitVector(random);
cnt++;
if (cnt > max_overlap_trials) {
faunus_logger->warn("{}: Max number of overlaps reached. Using overlapping point", name);
break;
}
} while (overlapWithParticles(target.position));
std::advance(origin, 1);
});
Expand Down
1 change: 1 addition & 0 deletions src/analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class ElectricPotential : public Analysis {
enum class Policies { FIXED, RANDOM_WALK, RANDOM_WALK_NO_OVERLAP, INVALID };

private:
unsigned int max_overlap_trials = 100; //!< Maximum number of overlap checks before bailing out
double histogram_resolution = 0.01; //!< Potential resolution
unsigned int calculations_per_sample_event = 1;
struct Target {
Expand Down

0 comments on commit 46fe2af

Please sign in to comment.