SYCL: Avoid using no_init read accessor in rocFFT
[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,2016,2017,2018,2019,2020,2021, 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/filetypes.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/math/vectypes.h"
55 #include "gromacs/pbcutil/pbc.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 static void PrintTo(const GromacsFileType& ftp, std::ostream* os)
70 {
71     *os << "'" << ftp2ext(ftp) << "'";
72 }
73
74 namespace
75 {
76
77 class StructureIORoundtripTest :
78     public gmx::test::StringTestBase,
79     public ::testing::WithParamInterface<GromacsFileType>
80 {
81 public:
82     StructureIORoundtripTest()
83     {
84         generateReferenceTopology();
85         generateReferenceCoordinates();
86         testTop_ = nullptr;
87         testX_   = nullptr;
88         clear_mat(testBox_);
89         referenceFilename_ = fileManager_.getTemporaryFilePath(getFileSuffix("ref"));
90         testFilename_      = fileManager_.getTemporaryFilePath(getFileSuffix("test"));
91     }
92     ~StructureIORoundtripTest() override
93     {
94         if (testTop_ != nullptr)
95         {
96             done_top(testTop_);
97             sfree(testTop_);
98         }
99         sfree(testX_);
100         done_top(refTop_);
101         sfree(refTop_);
102     }
103
104     void writeReferenceFile()
105     {
106         write_sto_conf(referenceFilename_.c_str(),
107                        *refTop_->name,
108                        &refTop_->atoms,
109                        as_rvec_array(refX_.data()),
110                        nullptr,
111                        PbcType::Unset,
112                        refBox_);
113     }
114
115     void readReferenceFileTps()
116     {
117         snew(testTop_, 1);
118         PbcType pbcType = PbcType::Unset;
119         read_tps_conf(referenceFilename_.c_str(), testTop_, &pbcType, &testX_, nullptr, testBox_, FALSE);
120     }
121
122     void testTopologies()
123     {
124         // TODO: Compare the topologies.
125     }
126
127     void writeTestFileAndTest()
128     {
129         write_sto_conf(
130                 testFilename_.c_str(), *testTop_->name, &testTop_->atoms, testX_, nullptr, PbcType::Unset, testBox_);
131         testFilesEqual(referenceFilename_, testFilename_);
132     }
133
134 private:
135     static std::string getFileSuffix(const char* type)
136     {
137         return std::string(type) + "." + ftp2ext(GetParam());
138     }
139
140     void generateReferenceTopology()
141     {
142         snew(refTop_, 1);
143         open_symtab(&refTop_->symtab);
144         if (GetParam() == efESP)
145         {
146             // Titles cannot be read from an .esp file...
147             refTop_->name = put_symtab(&refTop_->symtab, "");
148         }
149         else
150         {
151             refTop_->name = put_symtab(&refTop_->symtab, "Test title");
152         }
153         const int atomCount = 10;
154         init_t_atoms(&refTop_->atoms, atomCount, FALSE);
155         for (int i = 0; i < atomCount; ++i)
156         {
157             char name[3];
158             name[0]                       = 'A';
159             name[1]                       = 'A' + i % 3;
160             name[2]                       = '\0';
161             refTop_->atoms.atomname[i]    = put_symtab(&refTop_->symtab, name);
162             refTop_->atoms.atom[i].resind = i / 3;
163             if (i % 3 == 0)
164             {
165                 char resname[3];
166                 resname[0] = 'R';
167                 resname[1] = 'A' + i / 3;
168                 resname[2] = '\0';
169                 t_atoms_set_resinfo(&refTop_->atoms, i, &refTop_->symtab, resname, i / 3 + 1, ' ', 0, ' ');
170             }
171         }
172         refTop_->atoms.nres = 4;
173         close_symtab(&refTop_->symtab);
174     }
175
176     void generateReferenceCoordinates()
177     {
178         clear_mat(refBox_);
179         refBox_[XX][XX]     = 1;
180         refBox_[YY][YY]     = 2;
181         refBox_[ZZ][ZZ]     = 3;
182         const int atomCount = refTop_->atoms.nr;
183         refX_.reserve(atomCount);
184         for (int i = 0; i < atomCount; ++i)
185         {
186             refX_.emplace_back(i % 4, i / 4, (i / 2) % 3);
187         }
188     }
189
190     gmx::test::TestFileManager fileManager_;
191     std::string                referenceFilename_;
192     std::string                testFilename_;
193     t_topology*                refTop_;
194     std::vector<gmx::RVec>     refX_;
195     matrix                     refBox_;
196     t_topology*                testTop_;
197     rvec*                      testX_;
198     matrix                     testBox_;
199 };
200
201 TEST_P(StructureIORoundtripTest, ReadWriteTpsConf)
202 {
203     writeReferenceFile();
204     readReferenceFileTps();
205     testTopologies();
206     writeTestFileAndTest();
207 }
208
209 INSTANTIATE_TEST_SUITE_P(WithDifferentFormats,
210                          StructureIORoundtripTest,
211                          ::testing::Values(efGRO, efG96, efPDB, efESP));
212
213 } // namespace