5e0437da9ec360392d4eb4c688d66e9399d2fbd9
[alexxy/gromacs.git] / src / gromacs / coordinateio / tests / outputadapters.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
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 /*!\file
36  * \libinternal
37  * \brief
38  * Helpers and data for outputadapter module tests.
39  *
40  * \author Paul Bauer <paul.bauer.q@gmail.com>
41  * \inlibraryapi
42  * \ingroup module_coordinateio
43  */
44
45 #ifndef GMX_COORDINATEIO_TESTS_MODULE_H
46 #define GMX_COORDINATEIO_TESTS_MODULE_H
47
48 #include "gmxpre.h"
49
50 #include "config.h"
51
52 #include <gtest/gtest.h>
53
54 #include "gromacs/coordinateio/outputadapters/outputselector.h"
55 #include "gromacs/coordinateio/outputadapters/setatoms.h"
56 #include "gromacs/coordinateio/outputadapters/setbox.h"
57 #include "gromacs/coordinateio/outputadapters/setforces.h"
58 #include "gromacs/coordinateio/outputadapters/setprecision.h"
59 #include "gromacs/coordinateio/outputadapters/setstarttime.h"
60 #include "gromacs/coordinateio/outputadapters/settimestep.h"
61 #include "gromacs/coordinateio/outputadapters/setvelocities.h"
62
63 #include "gromacs/coordinateio/tests/coordinate_test.h"
64
65 namespace gmx
66 {
67
68 namespace test
69 {
70
71 /*!\libinternal \brief  Helper to test supported file names. */
72 class SetAtomsSupportedFiles : public ModuleTest
73 {
74 public:
75     void prepareTest(const char* filename)
76     {
77         addTopology();
78         //! Storage for requirements.
79         OutputRequirements requirements;
80
81         requirements.atoms = ChangeAtomsType::AlwaysFromStructure;
82
83         EXPECT_NO_THROW(runTest(filename, requirements));
84     }
85 };
86
87 /*!\libinternal \brief  Helper to test supported file names. */
88 class SetAtomsUnSupportedFiles : public ModuleTest
89 {
90 public:
91     void prepareTest(const char* filename)
92     {
93         //! Storage for requirements.
94         OutputRequirements requirements;
95
96         requirements.atoms = ChangeAtomsType::AlwaysFromStructure;
97
98         EXPECT_THROW(runTest(filename, requirements), InconsistentInputError);
99     }
100 };
101
102 /*!\libinternal \brief  Helper to test supported file names. */
103 class AnyOutputSupportedFiles : public ModuleTest, public ModuleSelection
104 {
105 public:
106     void prepareTest(const char* filename)
107     {
108         addTopology();
109         //! Storage for requirements.
110         OutputRequirements requirements;
111         //! Local atoms
112         Selection sel;
113         //! Local box
114         matrix box;
115
116         clear_mat(box);
117
118         addOptionForSelection(&dummySelection_, true);
119         setSelectionOptionValues(getOption(), &dummySelection_, true);
120
121         copy_mat(requirements.newBox, box);
122         requirements.box       = ChangeFrameInfoType::Always;
123         requirements.frameTime = ChangeFrameTimeType::Both;
124
125         EXPECT_NO_THROW(runTest(filename, requirements));
126     }
127 };
128
129 /*!\libinternal \brief  Helper to test support for disabling all output options. */
130 class NoOptionalOutput : public ModuleTest
131 {
132 public:
133     void prepareTest(const char* filename)
134     {
135         addTopology();
136         //! Storage for requirements.
137         OutputRequirements requirements;
138
139         requirements.atoms     = ChangeAtomsType::Never;
140         requirements.velocity  = ChangeSettingType::Never;
141         requirements.force     = ChangeSettingType::Never;
142         requirements.precision = ChangeFrameInfoType::Always;
143         requirements.prec      = 3;
144
145         EXPECT_NO_THROW(runTest(filename, requirements));
146     }
147 };
148
149 /*!\libinternal \brief  Helper to test that invalid selection is rejected */
150 class OutputSelectorDeathTest : public ModuleTest, public ModuleSelection
151 {
152 public:
153     void prepareTest()
154     {
155         //! Storage for frameadapters.
156         OutputAdapterContainer adapters(CoordinateFileFlags::Base);
157         //! Local atoms
158         Selection sel;
159
160         addOptionForSelection(&sel, false);
161         setSelectionOptionValues(getOption(), &sel, false);
162
163         ASSERT_DEATH_IF_SUPPORTED(adapters.addAdapter(std::make_unique<OutputSelector>(sel),
164                                                       CoordinateFileFlags::RequireCoordinateSelection),
165                                   "Need a valid selection out of simple atom indices");
166     }
167 };
168
169 /*!\libinternal \brief  Helper to test supported file names. */
170 class SetVelocitySupportedFiles : public ModuleTest
171 {
172 public:
173     void prepareTest(const char* filename)
174     {
175         addTopology();
176         //! Storage for requirements.
177         OutputRequirements requirements;
178
179         requirements.velocity = ChangeSettingType::Always;
180
181         EXPECT_NO_THROW(runTest(filename, requirements));
182     }
183 };
184
185 /*!\libinternal \brief  Helper to test supported file names. */
186 class SetVelocityUnSupportedFiles : public ModuleTest
187 {
188 public:
189     void prepareTest(const char* filename)
190     {
191         //! Storage for requirements.
192         OutputRequirements requirements;
193
194         requirements.velocity = ChangeSettingType::Always;
195
196         EXPECT_THROW(runTest(filename, requirements), InconsistentInputError);
197     }
198 };
199
200 /*!\libinternal \brief  Helper to test supported file names. */
201 class SetForceSupportedFiles : public ModuleTest
202 {
203 public:
204     void prepareTest(const char* filename)
205     {
206         addTopology();
207         //! Storage for requirements.
208         OutputRequirements requirements;
209
210         requirements.force = ChangeSettingType::Always;
211
212         EXPECT_NO_THROW(runTest(filename, requirements));
213     }
214 };
215
216 /*!\libinternal \brief  Helper to test supported file names. */
217 class SetForceUnSupportedFiles : public ModuleTest
218 {
219 public:
220     void prepareTest(const char* filename)
221     {
222         //! Storage for requirements.
223         OutputRequirements requirements;
224
225         requirements.force = ChangeSettingType::Always;
226
227         EXPECT_THROW(runTest(filename, requirements), InconsistentInputError);
228     }
229 };
230
231 /*!\libinternal \brief  Helper to test supported file names. */
232 class SetPrecisionSupportedFiles : public ModuleTest
233 {
234 public:
235     /*! \brief Set up the test case before running.
236      *
237      * \param[in] filename Name of the outputfile used to specify file type.
238      */
239     void prepareTest(const char* filename)
240     {
241         addTopology();
242         OutputRequirements requirements;
243
244         requirements.precision = ChangeFrameInfoType::Always;
245         requirements.prec      = 5;
246
247         EXPECT_NO_THROW(runTest(filename, requirements));
248     }
249 };
250
251 /*!\libinternal \brief  Helper to test supported file names. */
252 class SetPrecisionUnSupportedFiles : public ModuleTest
253 {
254 public:
255     /*! \brief Set up the test case before running.
256      *
257      * \param[in] filename Name of the outputfile used to specify file type.
258      */
259     void prepareTest(const char* filename)
260     {
261         OutputRequirements requirements;
262
263         requirements.precision = ChangeFrameInfoType::Always;
264         requirements.prec      = 5;
265
266         EXPECT_THROW(runTest(filename, requirements), InconsistentInputError);
267     }
268 };
269
270 //! Names here work for setAtoms module
271 const char* const setAtomsSupported[] = {
272 #if GMX_USE_TNG
273     "spc2-traj.tng",
274 #endif
275     "spc2-traj.gro",
276     "spc2-traj.pdb",
277 };
278
279 //! Names here don't work for setAtoms module
280 const char* const setAtomsUnSupported[] = { "spc2-traj.trr", "spc2-traj.xtc", "spc2-traj.g96" };
281
282 /*! \brief
283  *  Names here work for stuff that has no specific requirements.
284  *
285  *  PDB and GRO format are not tested here because they also require atoms information
286  *  that is incompatible with the other output formats.
287  */
288 const char* const anySupported[] = { "spc2-traj.trr",
289 #if GMX_USE_TNG
290                                      "spc2-traj.tng",
291 #endif
292                                      "spc2-traj.xtc", "spc2-traj.g96" };
293
294 //! Names here work for setVelocity module
295 const char* const setVelocitySupported[] = {
296 #if GMX_USE_TNG
297     "spc2-traj.tng",
298 #endif
299     "spc2-traj.gro",
300     "spc2-traj.trr",
301 };
302
303 //! Names here don't work for setVelocity module
304 const char* const setVelocityUnSupported[] = { "spc2-traj.xtc", "spc2-traj.pdb", "spc2-traj.g96" };
305
306 //! Names here work for setForce module
307 const char* const setForceSupported[] = {
308 #if GMX_USE_TNG
309     "spc2-traj.tng",
310 #endif
311     "spc2-traj.trr",
312 };
313
314 //! Names here don't work for setForce module
315 const char* const setForceUnSupported[] = { "spc2-traj.xtc", "spc2-traj.pdb", "spc2-traj.gro",
316                                             "spc2-traj.g96" };
317
318 //! Names here work for setPrecision module
319 const char* const setPrecisionSupported[] = {
320 #if GMX_USE_TNG
321     "spc2-traj.tng",
322 #endif
323     "spc2-traj.xtc",
324 };
325
326 //! Names here don't work for setPrecision module
327 const char* const setPrecisionUnSupported[] = { "spc2-traj.trr", "spc2-traj.pdb", "spc2-traj.gro",
328                                                 "spc2-traj.g96" };
329
330
331 } // namespace test
332
333 } // namespace gmx
334
335 #endif