From 7202e96e0ba707a93a84416a63508eadbf312a11 Mon Sep 17 00:00:00 2001 From: Alexander Schlaich Date: Fri, 24 Nov 2023 11:31:13 +0100 Subject: [PATCH 1/3] Improve steepest descent and enable CI --- .../electrodes/electrodes_part2.ipynb | 58 ++++++++++++++++--- testsuite/scripts/tutorials/CMakeLists.txt | 2 +- 2 files changed, 51 insertions(+), 9 deletions(-) diff --git a/doc/tutorials/electrodes/electrodes_part2.ipynb b/doc/tutorials/electrodes/electrodes_part2.ipynb index 35f7723156..407bf066fd 100644 --- a/doc/tutorials/electrodes/electrodes_part2.ipynb +++ b/doc/tutorials/electrodes/electrodes_part2.ipynb @@ -209,7 +209,7 @@ "import espressomd.shapes\n", "\n", "espressomd.assert_features(['WCA', 'ELECTROSTATICS'])\n", - "rng = np.random.default_rng(42)\n", + "rng = np.random.default_rng(815)\n", "plt.rcParams.update({'font.size': 18})" ] }, @@ -625,18 +625,58 @@ "metadata": {}, "outputs": [], "source": [ - "system.integrator.set_steepest_descent(f_max=10, gamma=50.0,\n", - " max_displacement=0.02)\n", - "system.integrator.run(250)\n", - "system.integrator.set_vv()\n", - "system.thermostat.set_langevin(kT=1.0, gamma=0.1, seed=42)" + "# suitable minimization parameters for this system\n", + "F_TOL = 1e-2\n", + "DAMPING = 30\n", + "MAX_STEPS = 10000\n", + "MAX_DISPLACEMENT = 0.01 * LJ_SIGMA\n", + "EM_STEP = 10" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "# Set up steepest descent integration\n", + "system.integrator.set_steepest_descent(f_max=0, # use a relative convergence criterion only\n", + " gamma=DAMPING,\n", + " max_displacement=MAX_DISPLACEMENT)\n", + "\n", + "# Initialize integrator to obtain initial forces\n", + "system.integrator.run(0)\n", + "old_force = np.max(np.linalg.norm(system.part.all().f, axis=1))\n", + "\n", + "\n", + "while system.time / system.time_step < MAX_STEPS:\n", + " system.integrator.run(EM_STEP)\n", + " force = np.max(np.linalg.norm(system.part.all().f, axis=1))\n", + " rel_force = np.abs((force - old_force) / old_force)\n", + " print(f'rel. force change: {rel_force:.2e}')\n", + " if rel_force < F_TOL:\n", + " break\n", + " old_force = force" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Equilibrate the ion distribution" + "### 2.2 Equilibrate the ion distribution" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Switch to velocity verlet integration using a Langevin thermostat\n", + "system.integrator.set_vv()\n", + "system.thermostat.set_langevin(kT=1.0, gamma=0.1, seed=42)" ] }, { @@ -1015,7 +1055,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(10, 6))\n", diff --git a/testsuite/scripts/tutorials/CMakeLists.txt b/testsuite/scripts/tutorials/CMakeLists.txt index 1779def85d..32cc187d9c 100644 --- a/testsuite/scripts/tutorials/CMakeLists.txt +++ b/testsuite/scripts/tutorials/CMakeLists.txt @@ -62,7 +62,7 @@ tutorial_test(FILE test_ferrofluid_2.py) tutorial_test(FILE test_ferrofluid_3.py) tutorial_test(FILE test_constant_pH__ideal.py) tutorial_test(FILE test_electrodes_1.py) -# tutorial_test(FILE test_electrodes_2.py) # TODO: unstable, see issue #4798 +tutorial_test(FILE test_electrodes_2.py) tutorial_test(FILE test_constant_pH__interactions.py) tutorial_test(FILE test_widom_insertion.py) tutorial_test(FILE test_grand_canonical_monte_carlo.py) From 2068be5d5548bb809096604b5a020630dd9a9d74 Mon Sep 17 00:00:00 2001 From: Alexander Schlaich Date: Fri, 24 Nov 2023 15:03:53 +0100 Subject: [PATCH 2/3] Limit Grahame test to 1 point --- testsuite/scripts/tutorials/test_electrodes_2.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/testsuite/scripts/tutorials/test_electrodes_2.py b/testsuite/scripts/tutorials/test_electrodes_2.py index 5654db1ed8..95a0225277 100644 --- a/testsuite/scripts/tutorials/test_electrodes_2.py +++ b/testsuite/scripts/tutorials/test_electrodes_2.py @@ -23,8 +23,8 @@ from scipy import constants params = {'N_SAMPLES_EQUIL': 25, 'N_SAMPLES_PROD': 5, - 'N_SAMPLES_EQUIL_CAP': 5, 'N_SAMPLES_CAP': 1, - 'MIN_PHI': 1, 'MAX_PHI': 2.5, 'N_PHI': 4} + 'N_SAMPLES_EQUIL_CAP': 0, 'N_SAMPLES_CAP': 5, + 'MIN_PHI': 5, 'MAX_PHI': 5, 'N_PHI': 1} tutorial, skipIfMissingFeatures = importlib_wrapper.configure_and_import( "@TUTORIALS_DIR@/electrodes/electrodes_part2.py", @@ -60,12 +60,13 @@ def test_charge_profile(self): def test_capacitance(self): # For low potentials the capacitance should be in line with Grahame/DH - # equilibration performance limiting + # equilibration performance limiting, just use the system equilibrated + # in the first part grahame = -tutorial.sigma_vs_phi[:, 0] / ( constants.elementary_charge / (constants.Boltzmann * tutorial.TEMPERATURE)) msg = 'The capacitance at low potentials should be in line with Grahame/DH.' np.testing.assert_allclose( - grahame, tutorial.sigma_vs_phi[:, 1], atol=.015, err_msg=msg) + grahame, tutorial.sigma_vs_phi[:, 1], atol=.05, err_msg=msg) if __name__ == "__main__": From 11a92de9ab405121849fd3d6498c16a4733814cf Mon Sep 17 00:00:00 2001 From: Alexander Schlaich Date: Fri, 24 Nov 2023 15:04:19 +0100 Subject: [PATCH 3/3] revert RNG seed --- doc/tutorials/electrodes/electrodes_part2.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorials/electrodes/electrodes_part2.ipynb b/doc/tutorials/electrodes/electrodes_part2.ipynb index 407bf066fd..d723d41474 100644 --- a/doc/tutorials/electrodes/electrodes_part2.ipynb +++ b/doc/tutorials/electrodes/electrodes_part2.ipynb @@ -209,7 +209,7 @@ "import espressomd.shapes\n", "\n", "espressomd.assert_features(['WCA', 'ELECTROSTATICS'])\n", - "rng = np.random.default_rng(815)\n", + "rng = np.random.default_rng(42)\n", "plt.rcParams.update({'font.size': 18})" ] },