79ac2ad7b27fb501cf3d562f9274f7854ca8cbaa
[alexxy/gromacs.git] / src / gromacs / coordinateio / tests / setatoms.cpp
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 /*!\internal
36  * \file
37  * \brief
38  * Tests for gmx::SetAtoms
39  *
40  * \author Paul Bauer <paul.bauer.q@gmail.com>
41  * \ingroup module_coordinateio
42  */
43
44
45 #include "gmxpre.h"
46
47 #include <utility>
48
49 #include "gromacs/coordinateio/outputadapters/setatoms.h"
50 #include "gromacs/trajectory/trajectoryframe.h"
51 #include "gromacs/trajectoryanalysis/topologyinformation.h"
52
53 #include "gromacs/coordinateio/tests/coordinate_test.h"
54 #include "testutils/testfilemanager.h"
55
56 namespace gmx
57 {
58
59 namespace test
60 {
61
62 /*!\brief
63  * Test fixture to prepare a setatoms object to pass data through.
64  */
65 class SetAtomsTest : public gmx::test::CommandLineTestBase
66 {
67 public:
68     //! Prepare test fixture topology to have atoms available.
69     SetAtomsTest()
70     {
71         topology_.fillFromInputFile(TestFileManager::getInputFilePath("lysozyme.pdb"));
72     }
73
74     /*! \brief
75      * Get access to the method for changing atom information.
76      *
77      * \param[in] type Type to use for setting up method.
78      * \param[in] haveAtomsAvailable Decide if method shouöd have atoms stored or not.
79      */
80     SetAtoms* setAtoms(ChangeAtomsType type, bool haveAtomsAvailable)
81     {
82         if (!setAtoms_)
83         {
84             if (haveAtomsAvailable)
85             {
86                 setAtoms_ = std::make_unique<SetAtoms>(type, topology_.copyAtoms());
87             }
88             else
89             {
90                 setAtoms_ = std::make_unique<SetAtoms>(type, nullptr);
91             }
92         }
93         return setAtoms_.get();
94     }
95     //! Get access to a t_atoms struct to use in the dummy t_trxframe.
96     t_atoms* getAtomsCopy() { return const_cast<t_atoms*>(topology_.atoms()); }
97
98 private:
99     //! Object to use for tests
100     SetAtomsPointer setAtoms_;
101     //! Local topology to get atoms from
102     TopologyInformation topology_;
103 };
104
105 TEST_F(SetAtomsTest, RemovesExistingAtoms)
106 {
107     t_trxframe frame;
108     clear_trxframe(&frame, true);
109
110     frame.bAtoms = true;
111     frame.atoms  = getAtomsCopy();
112
113     EXPECT_NE(frame.atoms, nullptr);
114
115     SetAtoms* method = setAtoms(ChangeAtomsType::Never, true);
116     EXPECT_NO_THROW(method->processFrame(0, &frame));
117
118     EXPECT_FALSE(frame.bAtoms);
119     EXPECT_EQ(frame.atoms, nullptr);
120 }
121
122 TEST_F(SetAtomsTest, AddsNewAtoms)
123 {
124     t_trxframe frame;
125     clear_trxframe(&frame, true);
126
127     frame.bAtoms = false;
128     frame.atoms  = nullptr;
129
130     SetAtoms* method = setAtoms(ChangeAtomsType::AlwaysFromStructure, true);
131     EXPECT_NO_THROW(method->processFrame(0, &frame));
132
133     EXPECT_TRUE(frame.bAtoms);
134     EXPECT_NE(frame.atoms, nullptr);
135 }
136
137 TEST_F(SetAtomsTest, ThrowsOnRequiredAtomsNotAvailable)
138 {
139     t_trxframe frame;
140     clear_trxframe(&frame, true);
141
142     frame.bAtoms = false;
143     frame.atoms  = nullptr;
144
145     SetAtoms* method = setAtoms(ChangeAtomsType::Always, false);
146     EXPECT_THROW(method->processFrame(0, &frame), InconsistentInputError);
147 }
148
149 TEST_F(SetAtomsTest, WillUseOldAtomsWhenNoNewAvailable)
150 {
151     t_trxframe frame;
152     clear_trxframe(&frame, true);
153
154     frame.bAtoms = true;
155     frame.atoms  = getAtomsCopy();
156
157     EXPECT_NE(frame.atoms, nullptr);
158
159     SetAtoms* method = setAtoms(ChangeAtomsType::Always, false);
160     EXPECT_NO_THROW(method->processFrame(0, &frame));
161 }
162
163 TEST_F(SetAtomsTest, ThrowsWhenUserAtomReplacementNotPossible)
164 {
165     t_trxframe frame;
166     clear_trxframe(&frame, true);
167
168     frame.bAtoms = true;
169     frame.atoms  = getAtomsCopy();
170
171     EXPECT_NE(frame.atoms, nullptr);
172
173     SetAtoms* method = setAtoms(ChangeAtomsType::AlwaysFromStructure, false);
174     EXPECT_THROW(method->processFrame(0, &frame), InconsistentInputError);
175 }
176
177 } // namespace test
178
179 } // namespace gmx