Replace gmxapi `erroroutput` with `stderr` and `stdout`
[alexxy/gromacs.git] / python_packaging / sample_restraint / tests / 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
41 import pytest
42
43 pytest_plugins = ('gmxapi.testsupport',)
44
45
46 @pytest.fixture(scope='class')
47 def spc_water_box(gmxcli, remove_tempdir):
48     """Provide a TPR input file for a simple simulation.
49
50     Prepare the MD input in a freshly created working directory.
51     """
52     import gmxapi as gmx
53     # TODO: Remove this import when the the spc_water_box fixture is migrated to gmxapi.testsupport
54     from gmxapi.testsupport import _cleandir
55
56     # TODO: (#2896) Fetch MD input from package / library data.
57     # Example:
58     #     import pkg_resources
59     #     # Note: importing pkg_resources means setuptools is required for running this test.
60     #     # Get or build TPR file from data bundled via setup(package_data=...)
61     #     # Ref https://setuptools.readthedocs.io/en/latest/setuptools.html#including-data-files
62     #     from gmx.data import tprfilename
63
64     with _cleandir(remove_tempdir) as tempdir:
65
66         testdir = os.path.dirname(__file__)
67         with open(os.path.join(testdir, 'testdata.json'), 'r') as fh:
68             testdata = json.load(fh)
69
70         # TODO: (#2756) Don't rely on so many automagical behaviors (as described in comments below)
71
72         structurefile = os.path.join(tempdir, 'structure.gro')
73         # We let `gmx solvate` use the default solvent. Otherwise, we would do
74         #     gro_input = testdata['solvent_structure']
75         #     with open(structurefile, 'w') as fh:
76         #         fh.write('\n'.join(gro_input))
77         #         fh.write('\n')
78
79         topfile = os.path.join(tempdir, 'topology.top')
80         top_input = testdata['solvent_topology']
81         # `gmx solvate` will append a line to the provided file with the molecule count,
82         # so we strip the last line from the input topology.
83         with open(topfile, 'w') as fh:
84             fh.write('\n'.join(top_input[:-1]))
85             fh.write('\n')
86
87         assert os.path.exists(topfile)
88         solvate = gmx.commandline_operation(gmxcli,
89                                             arguments=['solvate', '-box', '5', '5', '5'],
90                                             # We use the default solvent instead of specifying one.
91                                             # input_files={'-cs': structurefile},
92                                             output_files={'-p': topfile,
93                                                           '-o': structurefile,
94                                                           }
95                                             )
96         assert os.path.exists(topfile)
97
98         if solvate.output.returncode.result() != 0:
99             logging.debug(solvate.output.stderr.result())
100             raise RuntimeError('solvate failed in spc_water_box testing fixture.')
101
102         # Choose an exactly representable dt of 2^-9 ps (approximately 0.002)
103         dt = 2.**-9.
104         mdp_input = [('integrator', 'md'),
105                      ('dt', dt),
106                      ('cutoff-scheme', 'Verlet'),
107                      ('nsteps', 2),
108                      ('nstxout', 1),
109                      ('nstvout', 1),
110                      ('nstfout', 1),
111                      ('tcoupl', 'v-rescale'),
112                      ('tc-grps', 'System'),
113                      ('tau-t', 1),
114                      ('ref-t', 298)]
115         mdp_input = '\n'.join([' = '.join([str(item) for item in kvpair]) for kvpair in mdp_input])
116         mdpfile = os.path.join(tempdir, 'md.mdp')
117         with open(mdpfile, 'w') as fh:
118             fh.write(mdp_input)
119             fh.write('\n')
120         tprfile = os.path.join(tempdir, 'topol.tpr')
121         # We don't use mdout_mdp, but if we don't specify it to grompp,
122         # it will be created in the current working directory.
123         mdout_mdp = os.path.join(tempdir, 'mdout.mdp')
124
125         grompp = gmx.commandline_operation(gmxcli, 'grompp',
126                                            input_files={
127                                                '-f': mdpfile,
128                                                '-p': solvate.output.file['-p'],
129                                                '-c': solvate.output.file['-o'],
130                                                '-po': mdout_mdp,
131                                            },
132                                            output_files={'-o': tprfile})
133         tprfilename = grompp.output.file['-o'].result()
134         if grompp.output.returncode.result() != 0:
135             logging.debug(grompp.output.stderr.result())
136             raise RuntimeError('grompp failed in spc_water_box testing fixture.')
137
138         # TODO: more inspection of grompp errors...
139         assert os.path.exists(tprfilename)
140         yield tprfilename