From 9e3b5967ab163993f385cc0f7d240b85c0a335aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl=20Grad?= Date: Sun, 16 Feb 2020 18:45:56 +0100 Subject: [PATCH 1/3] Break infinite loop Cap the number of retries for monomer placement to `max_tries`, as explained in the function documentation. --- src/core/polymer.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/polymer.cpp b/src/core/polymer.cpp index c6884d59ed4..b59967e3f25 100644 --- a/src/core/polymer.cpp +++ b/src/core/polymer.cpp @@ -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()); @@ -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; From 0e0d8e86eeec147d450e43d0a6eda1029d622a91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl=20Grad?= Date: Sun, 16 Feb 2020 18:47:34 +0100 Subject: [PATCH 2/3] Remove debug print statement --- testsuite/python/polymer_linear.py | 1 - 1 file changed, 1 deletion(-) diff --git a/testsuite/python/polymer_linear.py b/testsuite/python/polymer_linear.py index e8c293dbda3..e93c266a592 100644 --- a/testsuite/python/polymer_linear.py +++ b/testsuite/python/polymer_linear.py @@ -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 From ae2c1f80b286cabdb2e462123ef9f54c2bac14ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-No=C3=ABl=20Grad?= Date: Mon, 17 Feb 2020 10:12:13 +0100 Subject: [PATCH 3/3] Remove fixed seed The `linear_polymer_positions()` function already has a retry mechanism with a default value of max_tries=1000. Running the test 10000 times with a random Mersenne Twister seed resulted in no failure. A single run required 3 retries in one test function, all the other runs placed a polymer on the first try in every tests. 6 runs took 1 min instead of 250 ms to execute. --- testsuite/python/polymer_linear.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testsuite/python/polymer_linear.py b/testsuite/python/polymer_linear.py index e93c266a592..7c9ca72b2c3 100644 --- a/testsuite/python/polymer_linear.py +++ b/testsuite/python/polymer_linear.py @@ -16,6 +16,7 @@ # along with this program. If not, see . import unittest as ut import numpy as np +import random import espressomd from espressomd import polymer import espressomd.shapes @@ -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): """