Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove infinite loop in linear polymer #3491

Merged
merged 3 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/core/polymer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ draw_polymer_positions(PartCfg &partCfg, int const n_polymers,
// create remaining monomers' positions by backtracking.
for (int p = 0; p < n_polymers; ++p) {
for (int attempts_poly = 0; attempts_poly < max_tries; attempts_poly++) {
int rejections = 0;
while (positions[p].size() < beads_per_chain) {
auto pos = draw_valid_monomer_position(p, positions[p].size());

Expand All @@ -186,6 +187,11 @@ draw_polymer_positions(PartCfg &partCfg, int const n_polymers,
} else if (not positions[p].empty()) {
/* Go back one position and try again */
positions[p].pop_back();
rejections++;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain why this is needed? Couldn't you just increase attempts_poly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, when the first one has fixed position. But there has to be a more elegant solution than this...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this while loop doesn't increment attempts_poly, therefore in high density systems, you can get stuck in an infinite cycle of deleting the previous bead and adding a new one at the same position

if (rejections > max_tries) {
/* Give up for this try. */
break;
}
} else {
/* Give up for this try. */
break;
Expand Down
5 changes: 2 additions & 3 deletions testsuite/python/polymer_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest as ut
import numpy as np
import random
import espressomd
from espressomd import polymer
import espressomd.shapes
Expand All @@ -33,10 +34,9 @@ class LinearPolymerPositions(ut.TestCase):
"""

box_l = 15
seed = 23
seed = random.randint(0, 1000)

system = espressomd.System(box_l=[box_l, box_l, box_l])
np.random.seed(1234)

def assertShape(self, positions, n_poly, n_mono):
"""
Expand Down Expand Up @@ -185,7 +185,6 @@ def test_respect_constraints_wall(self):

z_components = positions[:, :, 2][0]
for z in z_components:
print(z)
self.assertGreaterEqual(z, 0.5 * self.box_l)

# assert that illegal start position raises error
Expand Down