From 4019fadd6567f4433ba6f62c77d39f6f5b6c4632 Mon Sep 17 00:00:00 2001
From: Ask Hjorth Larsen <asklarsen@gmail.com>
Date: Fri, 4 Feb 2022 14:09:08 +0100
Subject: [PATCH 1/4] prevent computations from running at import time

---
 ase/optimize/test/neb.py     |  86 ++++++++++----------
 ase/optimize/test/systems.py | 147 +++++++++++++++++------------------
 2 files changed, 117 insertions(+), 116 deletions(-)

diff --git a/ase/optimize/test/neb.py b/ase/optimize/test/neb.py
index 100b8c853..d9ad1b976 100644
--- a/ase/optimize/test/neb.py
+++ b/ase/optimize/test/neb.py
@@ -4,43 +4,49 @@ from ase.calculators.emt import EMT
 from ase.neb import NEB
 from ase.build import fcc100, add_adsorbate
 
-# 2x2-Al(001) surface with 3 layers and an
-# Au atom adsorbed in a hollow site:
-slab = fcc100('Al', size=(2, 2, 3))
-add_adsorbate(slab, 'Au', 1.7, 'hollow')
-slab.center(axis=2, vacuum=4.0)
-
-# Fix second and third layers:
-mask = [atom.tag > 1 for atom in slab]
-slab.set_constraint(FixAtoms(mask=mask))
-
-# Use EMT potential:
-slab.calc = EMT()
-
-# Initial state:
-qn = QuasiNewton(slab, logfile=None)
-qn.run(fmax=0.05)
-initial = slab.copy()
-
-# Final state:
-slab[-1].x += slab.get_cell()[0, 0] / 2
-qn = QuasiNewton(slab, logfile=None)
-qn.run(fmax=0.05)
-final = slab.copy()
-
-# Setup a NEB calculation
-constraint = FixAtoms(mask=[atom.tag > 1 for atom in initial])
-
-images = [initial]
-for i in range(3):
-    image = initial.copy()
-    image.set_constraint(constraint)
-    images.append(image)
-
-images.append(final)
-
-neb = NEB(images)
-neb.interpolate()
-
-for image in neb.images[1:-1]:
-    image.calc = EMT()
+
+def main():
+    # 2x2-Al(001) surface with 3 layers and an
+    # Au atom adsorbed in a hollow site:
+    slab = fcc100('Al', size=(2, 2, 3))
+    add_adsorbate(slab, 'Au', 1.7, 'hollow')
+    slab.center(axis=2, vacuum=4.0)
+
+    # Fix second and third layers:
+    mask = [atom.tag > 1 for atom in slab]
+    slab.set_constraint(FixAtoms(mask=mask))
+
+    # Use EMT potential:
+    slab.calc = EMT()
+
+    # Initial state:
+    qn = QuasiNewton(slab, logfile=None)
+    qn.run(fmax=0.05)
+    initial = slab.copy()
+
+    # Final state:
+    slab[-1].x += slab.get_cell()[0, 0] / 2
+    qn = QuasiNewton(slab, logfile=None)
+    qn.run(fmax=0.05)
+    final = slab.copy()
+
+    # Setup a NEB calculation
+    constraint = FixAtoms(mask=[atom.tag > 1 for atom in initial])
+
+    images = [initial]
+    for i in range(3):
+        image = initial.copy()
+        image.set_constraint(constraint)
+        images.append(image)
+
+    images.append(final)
+
+    neb = NEB(images)
+    neb.interpolate()
+
+    for image in neb.images[1:-1]:
+        image.calc = EMT()
+
+
+if __name__ == '__main__':
+    main()
diff --git a/ase/optimize/test/systems.py b/ase/optimize/test/systems.py
index df75355d8..3014efe37 100644
--- a/ase/optimize/test/systems.py
+++ b/ase/optimize/test/systems.py
@@ -7,92 +7,87 @@ from ase.constraints import FixAtoms
 from ase.lattice.cubic import FaceCenteredCubic
 from ase.cluster import wulff_construction
 
-systems = []
 
-cell = (5, 5, 5)
-atoms = Atoms('H2', [(0, 0, 0), (0, 0, 1.4)], cell=cell)
-atoms.center()
-systems.append((atoms, 'Hydrogen molecule'))
+def get_systems():
+    cell = (5, 5, 5)
+    atoms = Atoms('H2', [(0, 0, 0), (0, 0, 1.4)], cell=cell)
+    atoms.center()
+    systems.append((atoms, 'Hydrogen molecule'))
 
-#
-atoms = FaceCenteredCubic(
-    directions=[[1, -1, 0], [1, 1, 0], [0, 0, 1]],
-    size=(2, 2, 2),
-    symbol='Cu',
-    pbc=(1, 1, 1))
-atoms.rattle(stdev=0.1, seed=42)
-systems.append((atoms, 'Shaken bulk copper'))
+    atoms = FaceCenteredCubic(
+        directions=[[1, -1, 0], [1, 1, 0], [0, 0, 1]],
+        size=(2, 2, 2),
+        symbol='Cu',
+        pbc=(1, 1, 1))
+    atoms.rattle(stdev=0.1, seed=42)
+    systems.append((atoms, 'Shaken bulk copper'))
 
-#
-a = 2.70
-c = 1.59 * a
+    a = 2.70
+    c = 1.59 * a
 
-slab = Atoms('2Cu', [(0., 0., 0.), (1 / 3., 1 / 3., -0.5 * c)],
-             tags=(0, 1),
-             pbc=(1, 1, 0))
-slab.set_cell([(a, 0, 0),
-               (a / 2, 3**0.5 * a / 2, 0),
-               (0, 0, 1)])
-slab.center(vacuum=3, axis=2)
-mask = [a.tag == 1 for a in slab]
-slab.set_constraint(FixAtoms(mask=mask))
-systems.append((slab, 'Distorted Cu(111) surface'))
+    slab = Atoms('2Cu', [(0., 0., 0.), (1 / 3., 1 / 3., -0.5 * c)],
+                 tags=(0, 1),
+                 pbc=(1, 1, 0))
+    slab.set_cell([(a, 0, 0),
+                   (a / 2, 3**0.5 * a / 2, 0),
+                   (0, 0, 1)])
+    slab.center(vacuum=3, axis=2)
+    mask = [a.tag == 1 for a in slab]
+    slab.set_constraint(FixAtoms(mask=mask))
+    systems.append((slab, 'Distorted Cu(111) surface'))
 
-#
-zpos = cos(134.3 / 2.0 * pi / 180.0) * 1.197
-xpos = sin(134.3 / 2.0 * pi / 180.0) * 1.19
-co = Atoms('CO', positions=[(-xpos + 1.2, 0, -zpos),
-                            (-xpos + 1.2, -1.1, -zpos)])
-slab = fcc111('Au', size=(2, 2, 2), orthogonal=True)
-add_adsorbate(slab, co, 1.5, 'bridge')
-slab.center(vacuum=6, axis=2)
-slab.set_pbc((True, True, False))
-constraint = FixAtoms(mask=[a.tag == 2 for a in slab])
-slab.set_constraint(constraint)
-systems.append((slab, 'CO on Au(111) surface'))
+    zpos = cos(134.3 / 2.0 * pi / 180.0) * 1.197
+    xpos = sin(134.3 / 2.0 * pi / 180.0) * 1.19
+    co = Atoms('CO', positions=[(-xpos + 1.2, 0, -zpos),
+                                (-xpos + 1.2, -1.1, -zpos)])
+    slab = fcc111('Au', size=(2, 2, 2), orthogonal=True)
+    add_adsorbate(slab, co, 1.5, 'bridge')
+    slab.center(vacuum=6, axis=2)
+    slab.set_pbc((True, True, False))
+    constraint = FixAtoms(mask=[a.tag == 2 for a in slab])
+    slab.set_constraint(constraint)
+    systems.append((slab, 'CO on Au(111) surface'))
 
-#
-atoms = Atoms(symbols='C5H12',
-              cell=[16.83752497, 12.18645905, 11.83462179],
-              positions=[[5.90380523, 5.65545388, 5.91569796],
-                         [7.15617518, 6.52907738, 5.91569796],
-                         [8.41815022, 5.66384716, 5.92196554],
-                         [9.68108996, 6.52891016, 5.91022362],
-                         [10.93006206, 5.65545388, 5.91569796],
-                         [5.00000011, 6.30002353, 5.9163716],
-                         [5.88571848, 5.0122839, 6.82246859],
-                         [5.88625613, 5.01308931, 5.01214155],
-                         [7.14329342, 7.18115393, 6.81640316],
-                         [7.14551332, 7.17200869, 5.00879027],
-                         [8.41609966, 5.00661165, 5.02355167],
-                         [8.41971183, 5.0251482, 6.83462168],
-                         [9.69568096, 7.18645894, 6.8078633],
-                         [9.68914668, 7.16663649, 5.00000011],
-                         [10.95518898, 5.02163182, 6.8289018],
-                         [11.83752486, 6.29836826, 5.90274952],
-                         [10.94464142, 5.00000011, 5.01802495]])
-systems.append((atoms, 'Pentane molecule'))
+    atoms = Atoms(symbols='C5H12',
+                  cell=[16.83752497, 12.18645905, 11.83462179],
+                  positions=[[5.90380523, 5.65545388, 5.91569796],
+                             [7.15617518, 6.52907738, 5.91569796],
+                             [8.41815022, 5.66384716, 5.92196554],
+                             [9.68108996, 6.52891016, 5.91022362],
+                             [10.93006206, 5.65545388, 5.91569796],
+                             [5.00000011, 6.30002353, 5.9163716],
+                             [5.88571848, 5.0122839, 6.82246859],
+                             [5.88625613, 5.01308931, 5.01214155],
+                             [7.14329342, 7.18115393, 6.81640316],
+                             [7.14551332, 7.17200869, 5.00879027],
+                             [8.41609966, 5.00661165, 5.02355167],
+                             [8.41971183, 5.0251482, 6.83462168],
+                             [9.69568096, 7.18645894, 6.8078633],
+                             [9.68914668, 7.16663649, 5.00000011],
+                             [10.95518898, 5.02163182, 6.8289018],
+                             [11.83752486, 6.29836826, 5.90274952],
+                             [10.94464142, 5.00000011, 5.01802495]])
+    systems.append((atoms, 'Pentane molecule'))
 
-#
-slab = fcc100('Cu', size=(2, 2, 2), vacuum=3.5)
-add_adsorbate(slab, 'C', 1.5, 'hollow')
-mask = [a.tag > 1 for a in slab]
-constraint = FixAtoms(mask=mask)
-slab.set_constraint(constraint)
-systems.append((slab, 'C/Cu(100)'))
-
-#
-surfaces = [(1, 0, 0), (1, 1, 0), (1, 1, 1)]
-esurf = [0.9151, 0.9771, 0.7953]  # Surface energies
-size = 10  # number of atoms
-atoms = wulff_construction('Al', surfaces, esurf, size, 'fcc',
-                           rounding='above')
-atoms.center(vacuum=6)
-atoms.rattle(0.2)
-systems.append((atoms, 'Alumninum cluster'))
+    slab = fcc100('Cu', size=(2, 2, 2), vacuum=3.5)
+    add_adsorbate(slab, 'C', 1.5, 'hollow')
+    mask = [a.tag > 1 for a in slab]
+    constraint = FixAtoms(mask=mask)
+    slab.set_constraint(constraint)
+    systems.append((slab, 'C/Cu(100)'))
 
+    surfaces = [(1, 0, 0), (1, 1, 0), (1, 1, 1)]
+    esurf = [0.9151, 0.9771, 0.7953]  # Surface energies
+    size = 10  # number of atoms
+    atoms = wulff_construction('Al', surfaces, esurf, size, 'fcc',
+                               rounding='above')
+    atoms.center(vacuum=6)
+    atoms.rattle(0.2)
+    systems.append((atoms, 'Alumninum cluster'))
+    return systems
 
 def create_database():
+    systems = get_systems()
     db = connect('systems.db', append=False)
     for atoms, description in systems:
         name = atoms.get_chemical_formula()
-- 
GitLab


From c0d155a34dd4115c0aa1c16bb218d808ddfcbbeb Mon Sep 17 00:00:00 2001
From: Ask Hjorth Larsen <asklarsen@gmail.com>
Date: Fri, 4 Feb 2022 14:36:13 +0100
Subject: [PATCH 2/4] update warning handling to appease old and new pytest
 simultaneously

---
 ase/test/test_imports.py | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/ase/test/test_imports.py b/ase/test/test_imports.py
index 5267a0695..93c3bfa91 100644
--- a/ase/test/test_imports.py
+++ b/ase/test/test_imports.py
@@ -1,3 +1,4 @@
+import contextlib
 from pathlib import Path
 from importlib import import_module
 from numpy import VisibleDeprecationWarning
@@ -58,17 +59,18 @@ newpy_only_modules = {
 @pytest.mark.filterwarnings('ignore:Moved to')
 def test_imports():
     for module in all_modules:
-        if module in deprecated_modules:
-            warning = (DeprecationWarning, VisibleDeprecationWarning)
-        else:
-            warning = None
+        with contextlib.ExitStack() as ignored_warnings:
+            if module in deprecated_modules:
+                ignored_warnings.enter_context(
+                    pytest.warns((DeprecationWarning,
+                                  VisibleDeprecationWarning)))
 
-        try:
-            with pytest.warns(warning):
+            try:
                 import_module(module)
-        except SyntaxError:
-            if module not in newpy_only_modules:
-                raise
-        except ImportError as err:
-            if err.name not in ignore_imports and 'deprecated' not in str(err):
-                raise
+            except SyntaxError:
+                if module not in newpy_only_modules:
+                    raise
+            except ImportError as err:
+                ok = err.name in ignore_imports or 'deprecated' in str(err)
+                if not ok:
+                    raise
-- 
GitLab


From 74dba1a8cfe4ee79c1436bdcf0195b79e7c5536e Mon Sep 17 00:00:00 2001
From: Ask Hjorth Larsen <asklarsen@gmail.com>
Date: Fri, 4 Feb 2022 14:41:38 +0100
Subject: [PATCH 3/4] update pytest.warns() call to latest pytest

---
 ase/test/calculator/castep/test_castep_interface.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ase/test/calculator/castep/test_castep_interface.py b/ase/test/calculator/castep/test_castep_interface.py
index 623836b62..08c72c65d 100644
--- a/ase/test/calculator/castep/test_castep_interface.py
+++ b/ase/test/calculator/castep/test_castep_interface.py
@@ -121,7 +121,7 @@ def test_fundamental_params():
 
     # Test special parsers
     mock_cparam.continuation = 'default'
-    with pytest.warns(None):
+    with pytest.warns(UserWarning):
         mock_cparam.reuse = 'default'
     assert mock_cparam.reuse.value is None
 
-- 
GitLab


From ac18ea7145eace58aabe4047b6214faf6cf5703f Mon Sep 17 00:00:00 2001
From: Ask Hjorth Larsen <asklarsen@gmail.com>
Date: Fri, 4 Feb 2022 15:00:43 +0100
Subject: [PATCH 4/4] oops, fix undefined variable

---
 ase/optimize/test/systems.py | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/ase/optimize/test/systems.py b/ase/optimize/test/systems.py
index 3014efe37..9769eaf61 100644
--- a/ase/optimize/test/systems.py
+++ b/ase/optimize/test/systems.py
@@ -9,6 +9,8 @@ from ase.cluster import wulff_construction
 
 
 def get_systems():
+    systems = []
+
     cell = (5, 5, 5)
     atoms = Atoms('H2', [(0, 0, 0), (0, 0, 1.4)], cell=cell)
     atoms.center()
@@ -86,6 +88,7 @@ def get_systems():
     systems.append((atoms, 'Alumninum cluster'))
     return systems
 
+
 def create_database():
     systems = get_systems()
     db = connect('systems.db', append=False)
-- 
GitLab

