Enable clang tidy/warnings for tests
[alexxy/gromacs.git] / src / gromacs / selection / tests / toputils.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014,2015,2016,2017,2018, 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  * Implements test helper routines from toputils.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_selection
41  */
42 #include "gmxpre.h"
43
44 #include "toputils.h"
45
46 #include <cstring>
47
48 #include <algorithm>
49
50 #include "gromacs/compat/make_unique.h"
51 #include "gromacs/fileio/confio.h"
52 #include "gromacs/fileio/trxio.h"
53 #include "gromacs/math/vec.h"
54 #include "gromacs/topology/atoms.h"
55 #include "gromacs/topology/mtop_util.h"
56 #include "gromacs/topology/topology.h"
57 #include "gromacs/trajectory/trajectoryframe.h"
58 #include "gromacs/utility/arrayref.h"
59 #include "gromacs/utility/cstringutil.h"
60 #include "gromacs/utility/gmxassert.h"
61 #include "gromacs/utility/smalloc.h"
62
63 #include "testutils/testfilemanager.h"
64
65 namespace gmx
66 {
67 namespace test
68 {
69
70 TopologyManager::TopologyManager()
71     :  frame_(nullptr)
72 {
73 }
74
75 TopologyManager::~TopologyManager()
76 {
77     if (frame_ != nullptr)
78     {
79         sfree(frame_->x);
80         sfree(frame_->v);
81         sfree(frame_->f);
82         sfree(frame_->index);
83         sfree(frame_);
84     }
85
86     for (char *atomtype : atomtypes_)
87     {
88         sfree(atomtype);
89     }
90 }
91
92 void TopologyManager::requestFrame()
93 {
94     GMX_RELEASE_ASSERT(mtop_ == nullptr,
95                        "Frame must be requested before initializing topology");
96     if (frame_ == nullptr)
97     {
98         snew(frame_, 1);
99     }
100 }
101
102 void TopologyManager::requestVelocities()
103 {
104     GMX_RELEASE_ASSERT(frame_ != nullptr,
105                        "Velocities requested before requesting a frame");
106     frame_->bV = TRUE;
107     if (frame_->natoms > 0)
108     {
109         snew(frame_->v, frame_->natoms);
110     }
111 }
112
113 void TopologyManager::requestForces()
114 {
115     GMX_RELEASE_ASSERT(frame_ != nullptr,
116                        "Forces requested before requesting a frame");
117     frame_->bF = TRUE;
118     if (frame_->natoms > 0)
119     {
120         snew(frame_->f, frame_->natoms);
121     }
122 }
123
124 void TopologyManager::loadTopology(const char *filename)
125 {
126     bool    fullTopology;
127     int     ePBC;
128     rvec   *xtop = nullptr;
129     matrix  box;
130
131     GMX_RELEASE_ASSERT(mtop_ == nullptr, "Topology initialized more than once");
132     mtop_ = gmx::compat::make_unique<gmx_mtop_t>();
133     readConfAndTopology(
134             gmx::test::TestFileManager::getInputFilePath(filename).c_str(),
135             &fullTopology, mtop_.get(), &ePBC, frame_ != nullptr ? &xtop : nullptr,
136             nullptr, box);
137
138     if (frame_ != nullptr)
139     {
140         GMX_ASSERT(xtop != nullptr, "Keep the static analyzer happy");
141         frame_->natoms = mtop_->natoms;
142         frame_->bX     = TRUE;
143         snew(frame_->x, frame_->natoms);
144         std::memcpy(frame_->x, xtop, sizeof(*frame_->x) * frame_->natoms);
145         frame_->bBox   = TRUE;
146         copy_mat(box, frame_->box);
147     }
148
149     sfree(xtop);
150 }
151
152 void TopologyManager::initAtoms(int count)
153 {
154     GMX_RELEASE_ASSERT(mtop_ == nullptr, "Topology initialized more than once");
155     mtop_ = gmx::compat::make_unique<gmx_mtop_t>();
156     mtop_->moltype.resize(1);
157     init_t_atoms(&mtop_->moltype[0].atoms, count, FALSE);
158     mtop_->molblock.resize(1);
159     mtop_->molblock[0].type            = 0;
160     mtop_->molblock[0].nmol            = 1;
161     mtop_->natoms                      = count;
162     mtop_->maxres_renum                = 0;
163     gmx_mtop_finalize(mtop_.get());
164     GMX_RELEASE_ASSERT(mtop_->maxres_renum == 0, "maxres_renum in mtop can be modified by an env.var., that is not supported in this test");
165     t_atoms &atoms = this->atoms();
166     for (int i = 0; i < count; ++i)
167     {
168         atoms.atom[i].m = (i % 3 == 0 ? 2.0 : 1.0);
169     }
170     atoms.haveMass = TRUE;
171     if (frame_ != nullptr)
172     {
173         frame_->natoms = count;
174         frame_->bX     = TRUE;
175         snew(frame_->x, count);
176         if (frame_->bV)
177         {
178             snew(frame_->v, count);
179         }
180         if (frame_->bF)
181         {
182             snew(frame_->f, count);
183         }
184     }
185 }
186
187 void TopologyManager::initAtomTypes(const ArrayRef<const char *const> &types)
188 {
189     GMX_RELEASE_ASSERT(mtop_ != nullptr, "Topology not initialized");
190     atomtypes_.reserve(types.size());
191     for (const char *type : types)
192     {
193         atomtypes_.push_back(gmx_strdup(type));
194     }
195     t_atoms &atoms = this->atoms();
196     snew(atoms.atomtype, atoms.nr);
197     index    j = 0;
198     for (int i = 0; i < atoms.nr; ++i, ++j)
199     {
200         if (j == types.size())
201         {
202             j = 0;
203         }
204         atoms.atomtype[i] = &atomtypes_[j];
205     }
206     atoms.haveType = TRUE;
207 }
208
209 void TopologyManager::initUniformResidues(int residueSize)
210 {
211     GMX_RELEASE_ASSERT(mtop_ != nullptr, "Topology not initialized");
212     t_atoms &atoms        = this->atoms();
213     int      residueIndex = -1;
214     for (int i = 0; i < atoms.nr; ++i)
215     {
216         if (i % residueSize == 0)
217         {
218             ++residueIndex;
219         }
220         atoms.atom[i].resind = residueIndex;
221     }
222     atoms.nres = residueIndex;
223 }
224
225 void TopologyManager::initUniformMolecules(int moleculeSize)
226 {
227     GMX_RELEASE_ASSERT(mtop_ != nullptr, "Topology not initialized");
228     GMX_RELEASE_ASSERT(mtop_->molblock.size() == 1, "initUniformMolecules only implemented for a single molblock");
229     gmx_molblock_t &molblock = mtop_->molblock[0];
230     t_atoms        &atoms    = mtop_->moltype[molblock.type].atoms;
231     GMX_RELEASE_ASSERT(atoms.nr % moleculeSize == 0,
232                        "The number of atoms should be a multiple of moleculeSize");
233     molblock.nmol  = atoms.nr/moleculeSize;
234     atoms.nr       = moleculeSize;
235     const int nres = atoms.atom[atoms.nr].resind;
236     GMX_RELEASE_ASSERT(atoms.atom[atoms.nr-1].resind != nres,
237                        "The residues should break at molecule boundaries");
238     atoms.nres                 = nres;
239     mtop_->haveMoleculeIndices = true;
240     gmx_mtop_finalize(mtop_.get());
241 }
242
243 void TopologyManager::initFrameIndices(const ArrayRef<const int> &index)
244 {
245     GMX_RELEASE_ASSERT(frame_ != nullptr, "Frame not initialized");
246     GMX_RELEASE_ASSERT(!frame_->bIndex, "Frame atom indices can only be set once");
247
248     frame_->bIndex = TRUE;
249     snew(frame_->index, index.size());
250     std::copy(index.begin(), index.end(), frame_->index);
251
252     frame_->natoms = index.size();
253 }
254
255 t_atoms &TopologyManager::atoms()
256 {
257     GMX_RELEASE_ASSERT(mtop_ != nullptr, "Topology not initialized");
258     GMX_RELEASE_ASSERT(mtop_->natoms == mtop_->moltype[0].atoms.nr,
259                        "Test setup assumes all atoms in a single molecule type");
260     return mtop_->moltype[0].atoms;
261 }
262
263 } // namespace test
264 } // namespace gmx