2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2019, 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.
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.
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.
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.
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.
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.
35 """Test gmx.fileio submodule"""
41 from gmxapi.simulation.fileio import TprFile
42 from gmxapi.simulation.fileio import read_tpr
43 from gmxapi.exceptions import UsageError
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):
53 tprfile = TprFile(tpr_filename, 'r')
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
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
75 @pytest.mark.usefixtures('cleandir')
76 def test_core_tprcopy_alt(spc_water_box):
77 """Test gmx.core.copy_tprfile() for update of end_time.
79 Set a new end time that is 5000 steps later than the original. Read dt
80 from file to avoid floating point round-off errors.
82 Transitively test gmx.fileio.read_tpr()
84 tpr_filename = spc_water_box
85 additional_steps = 5000
86 sim_input = read_tpr(tpr_filename)
87 params = sim_input.parameters.extract()
89 nsteps = params['nsteps']
90 init_step = params['init-step']
91 initial_endtime = (init_step + nsteps) * dt
92 new_endtime = initial_endtime + additional_steps*dt
93 _, temp_filename = tempfile.mkstemp(suffix='.tpr')
94 gmxapi._gmxapi.rewrite_tprfile(source=tpr_filename, destination=temp_filename, end_time=new_endtime)
95 tprfile = TprFile(temp_filename, 'r')
97 params = read_tpr(fh).parameters.extract()
99 nsteps = params['nsteps']
100 init_step = params['init-step']
101 assert (init_step + nsteps) * dt == new_endtime
103 os.unlink(temp_filename)
106 @pytest.mark.usefixtures('cleandir')
107 def test_write_tpr_file(spc_water_box):
108 """Test gmx.fileio.write_tpr_file() using gmx.core API.
110 tpr_filename = spc_water_box
111 additional_steps = 5000
112 sim_input = read_tpr(tpr_filename)
113 params = sim_input.parameters.extract()
114 nsteps = params['nsteps']
115 init_step = params['init-step']
116 # Choose a new nsteps to check integer parameter setting.
117 new_nsteps = init_step + additional_steps
118 # Choose a new dt to check floating point parameter setting
119 new_dt = params['dt'] * 2.
121 sim_input.parameters.set('nsteps', new_nsteps)
122 sim_input.parameters.set('dt', new_dt)
124 _, temp_filename = tempfile.mkstemp(suffix='.tpr')
125 gmxapi.simulation.fileio.write_tpr_file(temp_filename, input=sim_input)
126 tprfile = TprFile(temp_filename, 'r')
128 params = read_tpr(fh).parameters.extract()
129 # Note that we have chosen an exactly representable dt for spc_water_box.
130 # Otherwise, we would have to use pytest.approx with a suitable tolerance.
131 assert params['dt'] == new_dt
132 assert params['nsteps'] != nsteps
133 assert params['nsteps'] == new_nsteps
135 os.unlink(temp_filename)