98f278c38f793b07aa8a66409454d0532572275f
[alexxy/gromacs.git] / src / gromacs / coordinateio / tests / coordinate_test.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  * Helper classes for coordinatefile and coordinateio tests
39  *
40  * \author Paul Bauer <paul.bauer.q@gmail.com>
41  * \inlibraryapi
42  * \ingroup module_coordinateio
43  */
44
45 #ifndef GMX_COORDINATEIO_TESTS_COORDINATEIO_H
46 #define GMX_COORDINATEIO_TESTS_COORDINATEIO_H
47
48 #include <utility>
49
50 #include <gtest/gtest.h>
51
52 #include "gromacs/coordinateio/coordinatefile.h"
53 #include "gromacs/coordinateio/ioutputadapter.h"
54 #include "gromacs/coordinateio/requirements.h"
55 #include "gromacs/options/options.h"
56 #include "gromacs/options/optionsassigner.h"
57 #include "gromacs/selection/selectioncollection.h"
58 #include "gromacs/selection/selectionoption.h"
59 #include "gromacs/selection/selectionoptionmanager.h"
60 #include "gromacs/trajectoryanalysis/topologyinformation.h"
61 #include "gromacs/utility/exceptions.h"
62 #include "gromacs/utility/smalloc.h"
63
64 #include "testutils/cmdlinetest.h"
65 #include "testutils/testasserts.h"
66 #include "testutils/testfilemanager.h"
67
68 namespace gmx
69 {
70
71 namespace test
72 {
73
74 /*!\brief
75  * Create minimal TrajectoryFrameWriter using the provided builder.
76  *
77  * \param[in] filename      Name of file to create object for.
78  * \param[in] topology      Reference to input top.
79  * \param[in] selection     Reference to input selection.
80  * \param[in] requirements  Requirements for constructing OutputManagar.
81  * \throws InconsistentInputError When builder can not create the CoordinateFile.
82  * \returns unique_ptr to new CoordinateFile object.
83  */
84 inline TrajectoryFrameWriterPointer createMinimalTrajectoryFrameWriter(const std::string& filename,
85                                                                        const TopologyInformation& topology,
86                                                                        const Selection& selection,
87                                                                        OutputRequirements requirements)
88 {
89     return createTrajectoryFrameWriter(topology.mtop(), selection, filename,
90                                        topology.hasTopology() ? topology.copyAtoms() : nullptr,
91                                        requirements);
92 }
93 /*! \libinternal \brief
94  * Helper class for tests that need an initialized selection.
95  */
96 class ModuleSelection
97 {
98 public:
99     ModuleSelection() : manager_(&sc_)
100     {
101         options_.addManager(&manager_);
102         sc_.setReferencePosType("atom");
103         sc_.setOutputPosType("atom");
104         top_.fillFromInputFile(TestFileManager::getInputFilePath("lysozyme.pdb"));
105         sc_.setTopology(top_.mtop(), 0);
106     }
107
108     /*! \brief
109      * Method to add a valid selection option to the Module, or an invalid
110      * one for testing.
111      *
112      * \param[in] sel Selection to add option to.
113      * \param[in] useValid Set if the added selection should be valid for the module.
114      */
115     void addOptionForSelection(Selection* sel, bool useValid);
116
117     /*! \brief
118      * Set the actual values for the selection.
119      *
120      * \param[in] options Option to set values for.
121      * \param[in] sel Selection to use.
122      * \param[in] useValid Set if the added selection should be valid for the module.
123      */
124     void setSelectionOptionValues(Options* options, Selection* sel, bool useValid);
125
126     /*! \brief
127      * Get pointer to options to set values.
128      *
129      * \returns Pointer to options.
130      */
131     Options* getOption() { return &options_; }
132
133 private:
134     //! Selection collection used for handling test selection.
135     SelectionCollection sc_;
136     //! Selection manager for test selection.
137     SelectionOptionManager manager_;
138     //! Options manager for test selection input.
139     Options options_;
140     //! Topology information needed for test selection atoms.
141     TopologyInformation top_;
142 };
143
144 inline void ModuleSelection::addOptionForSelection(Selection* sel, bool useValid)
145 {
146     if (useValid)
147     {
148         options_.addOption(SelectionOption("sel").store(sel).onlyAtoms());
149     }
150     else
151     {
152         options_.addOption(SelectionOption("sel").store(sel).dynamicMask());
153     }
154 }
155
156 inline void ModuleSelection::setSelectionOptionValues(Options* options, Selection* sel, bool useValid)
157 {
158     OptionsAssigner assigner(options);
159     assigner.start();
160     assigner.startOption("sel");
161     if (useValid)
162     {
163         assigner.appendValue("all");
164     }
165     else
166     {
167         assigner.appendValue("res_cog of all");
168     }
169     assigner.finishOption();
170     assigner.finish();
171
172     ASSERT_TRUE(sel->isValid());
173     EXPECT_NO_THROW(sc_.compile());
174 }
175
176 /*! \libinternal \brief
177  * Test fixture to test matching file types for modules.
178  */
179 class ModuleTest : public gmx::test::CommandLineTestBase, public ::testing::WithParamInterface<const char*>
180 {
181 public:
182     /*! \brief
183      * Run the builder to create an TrajectoryFrameWriter during tests.
184      *
185      * \param[in] filename Name for output file, to determine filetype during construction.
186      * \param[in] requirements Requirements for adding to the object.
187      * \returns The newly created object.
188      */
189     TrajectoryFrameWriterPointer runTest(const char* filename, const OutputRequirements& requirements)
190     {
191         return createMinimalTrajectoryFrameWriter(filename, dummyTopology_, dummySelection_, requirements);
192     }
193     //! Add topology information to test if needed.
194     void addTopology()
195     {
196         dummyTopology_.fillFromInputFile(TestFileManager::getInputFilePath("lysozyme.pdb"));
197     }
198     //! Dummy topology to use to create CoordinateFile.
199     TopologyInformation dummyTopology_;
200     //! Dummy selection.
201     Selection dummySelection_;
202 };
203
204 } // namespace test
205
206 } // namespace gmx
207
208 #endif