2 * This file is part of the GROMACS molecular simulation package.
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.
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.
37 * Tests for reading/writing different structure file formats.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_fileio
44 #include "gromacs/fileio/confio.h"
50 #include <gtest/gtest.h>
52 #include "gromacs/fileio/filenm.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/math/vectypes.h"
55 #include "gromacs/topology/atoms.h"
56 #include "gromacs/topology/symtab.h"
57 #include "gromacs/topology/topology.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/utility/smalloc.h"
61 #include "testutils/stringtest.h"
62 #include "testutils/testfilemanager.h"
64 // TODO: These should really appear somewhere centralized.
66 * Google Test formatter for GromacsFileType values.
68 void PrintTo(const GromacsFileType &ftp, std::ostream *os)
70 *os << "'" << ftp2ext(ftp) << "'";
76 class StructureIORoundtripTest : public gmx::test::StringTestBase,
77 public ::testing::WithParamInterface<GromacsFileType>
80 StructureIORoundtripTest()
82 generateReferenceTopology();
83 generateReferenceCoordinates();
88 fileManager_.getTemporaryFilePath(getFileSuffix("ref"));
90 fileManager_.getTemporaryFilePath(getFileSuffix("test"));
92 ~StructureIORoundtripTest()
96 free_t_atoms(&testTop_->atoms, TRUE);
105 void writeReferenceFile()
107 write_sto_conf(referenceFilename_.c_str(), *refTop_->name,
108 &refTop_->atoms, as_rvec_array(&refX_[0]), NULL, -1,
112 void readReferenceFileStx()
115 get_stx_coordnum(referenceFilename_.c_str(), &natoms);
116 ASSERT_EQ(refTop_->atoms.nr, natoms)
117 << "get_stx_coordnum() returned unexpected number of atoms";
120 init_t_atoms(&testTop_->atoms, natoms, GetParam() == efPDB);
121 snew(testX_, natoms);
122 read_stx_conf(referenceFilename_.c_str(), title, &testTop_->atoms,
123 testX_, NULL, NULL, testBox_);
124 testTop_->name = put_symtab(&testTop_->symtab, title);
127 void readReferenceFileTps()
132 read_tps_conf(referenceFilename_.c_str(), title, testTop_,
133 &ePBC, &testX_, NULL, testBox_, FALSE);
134 testTop_->name = put_symtab(&testTop_->symtab, title);
137 void testTopologies()
139 // TODO: Compare the topologies.
142 void writeTestFileAndTest()
144 write_sto_conf(testFilename_.c_str(), *testTop_->name,
145 &testTop_->atoms, testX_, NULL, -1, testBox_);
146 testFilesEqual(referenceFilename_, testFilename_);
150 std::string getFileSuffix(const char *type)
152 return std::string(type) + "." + ftp2ext(GetParam());
155 void generateReferenceTopology()
158 open_symtab(&refTop_->symtab);
159 if (GetParam() == efESP)
161 // Titles cannot be read from an .esp file...
162 refTop_->name = put_symtab(&refTop_->symtab, "");
166 refTop_->name = put_symtab(&refTop_->symtab, "Test title");
168 const int atomCount = 10;
169 init_t_atoms(&refTop_->atoms, atomCount, FALSE);
170 for (int i = 0; i < atomCount; ++i)
176 refTop_->atoms.atomname[i] = put_symtab(&refTop_->symtab, name);
177 refTop_->atoms.atom[i].resind = i/3;
182 resname[1] = 'A' + i/3;
184 t_atoms_set_resinfo(&refTop_->atoms, i, &refTop_->symtab,
185 resname, i/3 + 1, ' ', 0, ' ');
188 refTop_->atoms.nres = 4;
189 close_symtab(&refTop_->symtab);
192 void generateReferenceCoordinates()
198 const int atomCount = refTop_->atoms.nr;
199 refX_.reserve(atomCount);
200 for (int i = 0; i < atomCount; ++i)
202 refX_.push_back(gmx::RVec(i%4, i/4, (i/2)%3));
206 gmx::test::TestFileManager fileManager_;
207 std::string referenceFilename_;
208 std::string testFilename_;
210 std::vector<gmx::RVec> refX_;
212 t_topology *testTop_;
217 TEST_P(StructureIORoundtripTest, ReadWriteStxConf)
219 writeReferenceFile();
220 readReferenceFileStx();
222 writeTestFileAndTest();
225 TEST_P(StructureIORoundtripTest, ReadWriteTpsConf)
227 writeReferenceFile();
228 readReferenceFileTps();
230 writeTestFileAndTest();
233 INSTANTIATE_TEST_CASE_P(WithDifferentFormats,
234 StructureIORoundtripTest,
235 ::testing::Values(efGRO, efG96, efPDB, efESP));