b80c21d4392ecc6044903236b1997e44a91f458f
[alexxy/gromacs.git] / python_packaging / src / test / conftest.py
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2019,2020, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 """Configuration and fixtures for pytest."""
36
37 import json
38 import logging
39 import os
40 import pytest
41
42 pytest_plugins = ('gmxapi.testsupport',)
43
44 @pytest.fixture(scope='class')
45 def spc_water_box(gmxcli, remove_tempdir):
46     """Provide a TPR input file for a simple simulation.
47
48     Prepare the MD input in a freshly created working directory.
49     """
50     import gmxapi as gmx
51     # TODO: Remove this import when the the spc_water_box fixture is migrated to gmxapi.testsupport
52     from gmxapi.testsupport import _cleandir
53
54     # TODO: (#2896) Fetch MD input from package / library data.
55     # Example:
56     #     import pkg_resources
57     #     # Note: importing pkg_resources means setuptools is required for running this test.
58     #     # Get or build TPR file from data bundled via setup(package_data=...)
59     #     # Ref https://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files
60     #     from gmx.data import tprfilename
61
62     with _cleandir(remove_tempdir) as tempdir:
63
64         testdir = os.path.dirname(__file__)
65         with open(os.path.join(testdir, 'testdata.json'), 'r') as fh:
66             testdata = json.load(fh)
67
68         # TODO: (#2756) Don't rely on so many automagical behaviors (as described in comments below)
69
70         structurefile = os.path.join(tempdir, 'structure.gro')
71         # We let `gmx solvate` use the default solvent. Otherwise, we would do
72         #     gro_input = testdata['solvent_structure']
73         #     with open(structurefile, 'w') as fh:
74         #         fh.write('\n'.join(gro_input))
75         #         fh.write('\n')
76
77         topfile = os.path.join(tempdir, 'topology.top')
78         top_input = testdata['solvent_topology']
79         # `gmx solvate` will append a line to the provided file with the molecule count,
80         # so we strip the last line from the input topology.
81         with open(topfile, 'w') as fh:
82             fh.write('\n'.join(top_input[:-1]))
83             fh.write('\n')
84
85         assert os.path.exists(topfile)
86         solvate = gmx.commandline_operation(gmxcli,
87                                             arguments=['solvate', '-box', '5', '5', '5'],
88                                             # We use the default solvent instead of specifying one.
89                                             # input_files={'-cs': structurefile},
90                                             output_files={'-p': topfile,
91                                                           '-o': structurefile,
92                                                           }
93                                             )
94         assert os.path.exists(topfile)
95
96         if solvate.output.returncode.result() != 0:
97             logging.debug(solvate.output.stderr.result())
98             raise RuntimeError('solvate failed in spc_water_box testing fixture.')
99
100         # Choose an exactly representable dt of 2^-9 ps (approximately 0.002)
101         dt = 2.**-9.
102         mdp_input = [('integrator', 'md'),
103                      ('dt', dt),
104                      ('cutoff-scheme', 'Verlet'),
105                      ('nsteps', 2),
106                      ('nstxout', 1),
107                      ('nstvout', 1),
108                      ('nstfout', 1),
109                      ('tcoupl', 'v-rescale'),
110                      ('tc-grps', 'System'),
111                      ('tau-t', 1),
112                      ('ref-t', 298)]
113         mdp_input = '\n'.join([' = '.join([str(item) for item in kvpair]) for kvpair in mdp_input])
114         mdpfile = os.path.join(tempdir, 'md.mdp')
115         with open(mdpfile, 'w') as fh:
116             fh.write(mdp_input)
117             fh.write('\n')
118         tprfile = os.path.join(tempdir, 'topol.tpr')
119         # We don't use mdout_mdp, but if we don't specify it to grompp,
120         # it will be created in the current working directory.
121         mdout_mdp = os.path.join(tempdir, 'mdout.mdp')
122
123         grompp = gmx.commandline_operation(gmxcli, 'grompp',
124                                            input_files={
125                                                '-f': mdpfile,
126                                                '-p': solvate.output.file['-p'],
127                                                '-c': solvate.output.file['-o'],
128                                                '-po': mdout_mdp,
129                                            },
130                                            output_files={'-o': tprfile})
131         tprfilename = grompp.output.file['-o'].result()
132         if grompp.output.returncode.result() != 0:
133             logging.debug(grompp.output.stderr.result())
134             raise RuntimeError('grompp failed in spc_water_box testing fixture.')
135
136         # TODO: more inspection of grompp errors...
137         assert os.path.exists(tprfilename)
138         yield tprfilename