00f971c7d02092523bbb2256809ed5e1f17adf14
[alexxy/gromacs.git] / src / gromacs / fileio / tests / confio.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2015, 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 \file
36  * \brief
37  * Tests for reading/writing different structure file formats.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_fileio
41  */
42 #include "gmxpre.h"
43
44 #include "gromacs/fileio/confio.h"
45
46 #include <iostream>
47 #include <string>
48 #include <vector>
49
50 #include <gtest/gtest.h>
51
52 #include "gromacs/fileio/filenm.h"
53 #include "gromacs/fileio/tpxio.h"
54 #include "gromacs/math/vec.h"
55 #include "gromacs/math/vectypes.h"
56 #include "gromacs/topology/atoms.h"
57 #include "gromacs/topology/symtab.h"
58 #include "gromacs/topology/topology.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/smalloc.h"
61
62 #include "testutils/stringtest.h"
63 #include "testutils/testfilemanager.h"
64
65 // TODO: These should really appear somewhere centralized.
66 /*! \brief
67  * Google Test formatter for GromacsFileType values.
68  */
69 void PrintTo(const GromacsFileType &ftp, std::ostream *os)
70 {
71     *os << "'" << ftp2ext(ftp) << "'";
72 }
73
74 namespace
75 {
76
77 class StructureIORoundtripTest : public gmx::test::StringTestBase,
78                                  public ::testing::WithParamInterface<GromacsFileType>
79 {
80     public:
81         StructureIORoundtripTest()
82         {
83             generateReferenceTopology();
84             generateReferenceCoordinates();
85             testTop_ = NULL;
86             testX_   = NULL;
87             clear_mat(testBox_);
88             referenceFilename_ =
89                 fileManager_.getTemporaryFilePath(getFileSuffix("ref"));
90             testFilename_ =
91                 fileManager_.getTemporaryFilePath(getFileSuffix("test"));
92         }
93         ~StructureIORoundtripTest()
94         {
95             if (testTop_ != NULL)
96             {
97                 free_t_atoms(&testTop_->atoms, TRUE);
98                 done_top(testTop_);
99                 sfree(testTop_);
100             }
101             sfree(testX_);
102             done_top(refTop_);
103             sfree(refTop_);
104         }
105
106         void writeReferenceFile()
107         {
108             write_sto_conf(referenceFilename_.c_str(), *refTop_->name,
109                            &refTop_->atoms, as_rvec_array(&refX_[0]), NULL, -1,
110                            refBox_);
111         }
112
113         void readReferenceFileStx()
114         {
115             int natoms = -1;
116             get_stx_coordnum(referenceFilename_.c_str(), &natoms);
117             ASSERT_EQ(refTop_->atoms.nr, natoms)
118             << "get_stx_coordnum() returned unexpected number of atoms";
119             char title[STRLEN];
120             snew(testTop_, 1);
121             init_t_atoms(&testTop_->atoms, natoms, GetParam() == efPDB);
122             snew(testX_, natoms);
123             read_stx_conf(referenceFilename_.c_str(), title, &testTop_->atoms,
124                           testX_, NULL, NULL, testBox_);
125             testTop_->name = put_symtab(&testTop_->symtab, title);
126         }
127
128         void readReferenceFileTps()
129         {
130             snew(testTop_, 1);
131             int  ePBC = -2;
132             char title[STRLEN];
133             read_tps_conf(referenceFilename_.c_str(), title, testTop_,
134                           &ePBC, &testX_, NULL, testBox_, FALSE);
135             testTop_->name = put_symtab(&testTop_->symtab, title);
136         }
137
138         void testTopologies()
139         {
140             // TODO: Compare the topologies.
141         }
142
143         void writeTestFileAndTest()
144         {
145             write_sto_conf(testFilename_.c_str(), *testTop_->name,
146                            &testTop_->atoms, testX_, NULL, -1, testBox_);
147             testFilesEqual(referenceFilename_, testFilename_);
148         }
149
150     private:
151         std::string getFileSuffix(const char *type)
152         {
153             return std::string(type) + "." + ftp2ext(GetParam());
154         }
155
156         void generateReferenceTopology()
157         {
158             snew(refTop_, 1);
159             open_symtab(&refTop_->symtab);
160             if (GetParam() == efESP)
161             {
162                 // Titles cannot be read from an .esp file...
163                 refTop_->name = put_symtab(&refTop_->symtab, "");
164             }
165             else
166             {
167                 refTop_->name = put_symtab(&refTop_->symtab, "Test title");
168             }
169             const int atomCount = 10;
170             init_t_atoms(&refTop_->atoms, atomCount, FALSE);
171             for (int i = 0; i < atomCount; ++i)
172             {
173                 char name[3];
174                 name[0]                       = 'A';
175                 name[1]                       = 'A' + i%3;
176                 name[2]                       = '\0';
177                 refTop_->atoms.atomname[i]    = put_symtab(&refTop_->symtab, name);
178                 refTop_->atoms.atom[i].resind = i/3;
179                 if (i%3 == 0)
180                 {
181                     char resname[3];
182                     resname[0] = 'R';
183                     resname[1] = 'A' + i/3;
184                     resname[2] = '\0';
185                     t_atoms_set_resinfo(&refTop_->atoms, i, &refTop_->symtab,
186                                         resname, i/3 + 1, ' ', 0, ' ');
187                 }
188             }
189             refTop_->atoms.nres = 4;
190             close_symtab(&refTop_->symtab);
191         }
192
193         void generateReferenceCoordinates()
194         {
195             clear_mat(refBox_);
196             refBox_[XX][XX] = 1;
197             refBox_[YY][YY] = 2;
198             refBox_[ZZ][ZZ] = 3;
199             const int atomCount = refTop_->atoms.nr;
200             refX_.reserve(atomCount);
201             for (int i = 0; i < atomCount; ++i)
202             {
203                 refX_.push_back(gmx::RVec(i%4, i/4, (i/2)%3));
204             }
205         }
206
207         gmx::test::TestFileManager      fileManager_;
208         std::string                     referenceFilename_;
209         std::string                     testFilename_;
210         t_topology                     *refTop_;
211         std::vector<gmx::RVec>          refX_;
212         matrix                          refBox_;
213         t_topology                     *testTop_;
214         rvec                           *testX_;
215         matrix                          testBox_;
216 };
217
218 TEST_P(StructureIORoundtripTest, ReadWriteStxConf)
219 {
220     writeReferenceFile();
221     readReferenceFileStx();
222     testTopologies();
223     writeTestFileAndTest();
224 }
225
226 TEST_P(StructureIORoundtripTest, ReadWriteTpsConf)
227 {
228     writeReferenceFile();
229     readReferenceFileTps();
230     testTopologies();
231     writeTestFileAndTest();
232 }
233
234 INSTANTIATE_TEST_CASE_P(WithDifferentFormats,
235                         StructureIORoundtripTest,
236                             ::testing::Values(efGRO, efG96, efPDB, efESP));
237
238 } // namespace