Tidy: modernize-use-nullptr
[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, 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/fileio/confio.h"
51 #include "gromacs/fileio/trxio.h"
52 #include "gromacs/math/vec.h"
53 #include "gromacs/topology/atoms.h"
54 #include "gromacs/topology/mtop_util.h"
55 #include "gromacs/topology/topology.h"
56 #include "gromacs/trajectory/trajectoryframe.h"
57 #include "gromacs/utility/arrayref.h"
58 #include "gromacs/utility/cstringutil.h"
59 #include "gromacs/utility/gmxassert.h"
60 #include "gromacs/utility/smalloc.h"
61
62 #include "testutils/testfilemanager.h"
63
64 namespace gmx
65 {
66 namespace test
67 {
68
69 TopologyManager::TopologyManager()
70     : mtop_(nullptr), frame_(nullptr)
71 {
72 }
73
74 TopologyManager::~TopologyManager()
75 {
76     if (mtop_ != nullptr)
77     {
78         done_mtop(mtop_);
79         sfree(mtop_);
80     }
81
82     if (frame_ != nullptr)
83     {
84         sfree(frame_->x);
85         sfree(frame_->v);
86         sfree(frame_->f);
87         sfree(frame_->index);
88         sfree(frame_);
89     }
90
91     for (char *atomtype : atomtypes_)
92     {
93         sfree(atomtype);
94     }
95 }
96
97 void TopologyManager::requestFrame()
98 {
99     GMX_RELEASE_ASSERT(mtop_ == nullptr,
100                        "Frame must be requested before initializing topology");
101     if (frame_ == nullptr)
102     {
103         snew(frame_, 1);
104     }
105 }
106
107 void TopologyManager::requestVelocities()
108 {
109     GMX_RELEASE_ASSERT(frame_ != nullptr,
110                        "Velocities requested before requesting a frame");
111     frame_->bV = TRUE;
112     if (frame_->natoms > 0)
113     {
114         snew(frame_->v, frame_->natoms);
115     }
116 }
117
118 void TopologyManager::requestForces()
119 {
120     GMX_RELEASE_ASSERT(frame_ != nullptr,
121                        "Forces requested before requesting a frame");
122     frame_->bF = TRUE;
123     if (frame_->natoms > 0)
124     {
125         snew(frame_->f, frame_->natoms);
126     }
127 }
128
129 void TopologyManager::loadTopology(const char *filename)
130 {
131     bool    fullTopology;
132     int     ePBC;
133     rvec   *xtop = nullptr;
134     matrix  box;
135
136     GMX_RELEASE_ASSERT(mtop_ == nullptr, "Topology initialized more than once");
137     snew(mtop_, 1);
138     readConfAndTopology(
139             gmx::test::TestFileManager::getInputFilePath(filename).c_str(),
140             &fullTopology, mtop_, &ePBC, frame_ != nullptr ? &xtop : nullptr,
141             nullptr, box);
142
143     if (frame_ != nullptr)
144     {
145         frame_->natoms = mtop_->natoms;
146         frame_->bX     = TRUE;
147         snew(frame_->x, frame_->natoms);
148         std::memcpy(frame_->x, xtop, sizeof(*frame_->x) * frame_->natoms);
149         frame_->bBox   = TRUE;
150         copy_mat(box, frame_->box);
151     }
152
153     sfree(xtop);
154 }
155
156 void TopologyManager::initAtoms(int count)
157 {
158     GMX_RELEASE_ASSERT(mtop_ == nullptr, "Topology initialized more than once");
159     snew(mtop_, 1);
160     mtop_->nmoltype = 1;
161     snew(mtop_->moltype, 1);
162     init_t_atoms(&mtop_->moltype[0].atoms, count, FALSE);
163     mtop_->nmolblock = 1;
164     snew(mtop_->molblock, 1);
165     mtop_->molblock[0].type            = 0;
166     mtop_->molblock[0].nmol            = 1;
167     mtop_->molblock[0].natoms_mol      = count;
168     mtop_->natoms                      = count;
169     mtop_->maxres_renum                = 0;
170     gmx_mtop_finalize(mtop_);
171     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");
172     t_atoms &atoms = this->atoms();
173     for (int i = 0; i < count; ++i)
174     {
175         atoms.atom[i].m = (i % 3 == 0 ? 2.0 : 1.0);
176     }
177     atoms.haveMass = TRUE;
178     if (frame_ != nullptr)
179     {
180         frame_->natoms = count;
181         frame_->bX     = TRUE;
182         snew(frame_->x, count);
183         if (frame_->bV)
184         {
185             snew(frame_->v, count);
186         }
187         if (frame_->bF)
188         {
189             snew(frame_->f, count);
190         }
191     }
192 }
193
194 void TopologyManager::initAtomTypes(const ConstArrayRef<const char *> &types)
195 {
196     GMX_RELEASE_ASSERT(mtop_ != nullptr, "Topology not initialized");
197     atomtypes_.reserve(types.size());
198     for (const char *type : types)
199     {
200         atomtypes_.push_back(gmx_strdup(type));
201     }
202     t_atoms &atoms = this->atoms();
203     snew(atoms.atomtype, atoms.nr);
204     size_t   j = 0;
205     for (int i = 0; i < atoms.nr; ++i, ++j)
206     {
207         if (j == types.size())
208         {
209             j = 0;
210         }
211         atoms.atomtype[i] = &atomtypes_[j];
212     }
213     atoms.haveType = TRUE;
214 }
215
216 void TopologyManager::initUniformResidues(int residueSize)
217 {
218     GMX_RELEASE_ASSERT(mtop_ != nullptr, "Topology not initialized");
219     t_atoms &atoms        = this->atoms();
220     int      residueIndex = -1;
221     for (int i = 0; i < atoms.nr; ++i)
222     {
223         if (i % residueSize == 0)
224         {
225             ++residueIndex;
226         }
227         atoms.atom[i].resind = residueIndex;
228     }
229 }
230
231 void TopologyManager::initUniformMolecules(int moleculeSize)
232 {
233     GMX_RELEASE_ASSERT(mtop_ != nullptr, "Topology not initialized");
234     int index = 0;
235     mtop_->mols.nalloc_index = (mtop_->natoms + moleculeSize - 1) / moleculeSize + 1;
236     srenew(mtop_->mols.index, mtop_->mols.nalloc_index);
237     mtop_->mols.nr = 0;
238     while (index < mtop_->natoms)
239     {
240         mtop_->mols.index[mtop_->mols.nr] = index;
241         ++mtop_->mols.nr;
242         index += moleculeSize;
243     }
244     mtop_->mols.index[mtop_->mols.nr] = mtop_->natoms;
245 }
246
247 void TopologyManager::initFrameIndices(const ConstArrayRef<int> &index)
248 {
249     GMX_RELEASE_ASSERT(frame_ != nullptr, "Frame not initialized");
250     GMX_RELEASE_ASSERT(!frame_->bIndex, "Frame atom indices can only be set once");
251
252     frame_->bIndex = TRUE;
253     snew(frame_->index, index.size());
254     std::copy(index.begin(), index.end(), frame_->index);
255
256     frame_->natoms = index.size();
257 }
258
259 t_atoms &TopologyManager::atoms()
260 {
261     GMX_RELEASE_ASSERT(mtop_ != nullptr, "Topology not initialized");
262     GMX_RELEASE_ASSERT(mtop_->natoms == mtop_->moltype[0].atoms.nr,
263                        "Test setup assumes all atoms in a single molecule type");
264     return mtop_->moltype[0].atoms;
265 }
266
267 } // namespace test
268 } // namespace gmx