Skip to content

Commit

Permalink
Remove infinite loop in linear polymer (#3491)
Browse files Browse the repository at this point in the history
Description of changes:
- fix infinite loop in `draw_polymer_positions()` (regression from #3484)
- remove fixed seed in the `polymer_linear.py` test that used to hide a bug in the previous implementation of `draw_polymer_positions()`
  • Loading branch information
kodiakhq[bot] authored Feb 17, 2020
2 parents cec08a2 + ae2c1f8 commit f9ae0e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
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++;
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

0 comments on commit f9ae0e8

Please sign in to comment.