Clean up some gmxapi sources and tests.
[alexxy/gromacs.git] / python_packaging / src / test / test_fileio.py
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2019,2021, 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 """Test gmx.fileio submodule"""
36 import os
37 import tempfile
38
39 import gmxapi
40 import pytest
41 from gmxapi.simulation.fileio import TprFile
42 from gmxapi.simulation.fileio import read_tpr
43 from gmxapi.exceptions import UsageError
44
45
46 @pytest.mark.usefixtures('cleandir')
47 def test_tprfile_read_old(spc_water_box):
48     tpr_filename = spc_water_box
49     with pytest.raises(UsageError):
50         TprFile(tpr_filename, 'x')
51     with pytest.raises(UsageError):
52         TprFile()
53     tprfile = TprFile(tpr_filename, 'r')
54     with tprfile as fh:
55         cpp_object = fh._tprFileHandle
56         assert cpp_object is not None
57         params = cpp_object.params().extract()
58         assert "nsteps" in params
59         assert "foo" not in params
60
61
62 @pytest.mark.usefixtures('cleandir')
63 def test_read_tpr(spc_water_box):
64     tpr_filename = spc_water_box
65     tpr_source = gmxapi.read_tpr(tpr_filename)
66     assert 'parameters' in tpr_source.output
67     assert hasattr(tpr_source.output, 'parameters')
68     parameters = tpr_source.output.parameters.result()
69     assert 'nsteps' in parameters
70     assert 'foo' not in parameters
71     assert parameters['nsteps'] == 2
72     assert tpr_source.output.parameters['nsteps'].result() == 2
73
74
75 @pytest.mark.usefixtures('cleandir')
76 def test_write_tpr_file(spc_water_box):
77     """Test gmx.fileio.write_tpr_file() using gmx.core API.
78     """
79     tpr_filename = spc_water_box
80     additional_steps = 5000
81     sim_input = read_tpr(tpr_filename)
82     params = sim_input.parameters.extract()
83     nsteps = params['nsteps']
84     init_step = params['init-step']
85     # Choose a new nsteps to check integer parameter setting.
86     new_nsteps = init_step + additional_steps
87     # Choose a new dt to check floating point parameter setting
88     new_dt = params['dt'] * 2.
89
90     sim_input.parameters.set('nsteps', new_nsteps)
91     sim_input.parameters.set('dt', new_dt)
92
93     _, temp_filename = tempfile.mkstemp(suffix='.tpr')
94     gmxapi.simulation.fileio.write_tpr_file(temp_filename, input=sim_input)
95     tprfile = TprFile(temp_filename, 'r')
96     with tprfile as fh:
97         params = read_tpr(fh).parameters.extract()
98         # Note that we have chosen an exactly representable dt for spc_water_box.
99         # Otherwise, we would have to use pytest.approx with a suitable tolerance.
100         assert params['dt'] == new_dt
101         assert params['nsteps'] != nsteps
102         assert params['nsteps'] == new_nsteps
103
104     os.unlink(temp_filename)