f7a292e649ae45c2871808fdff958a54eee87f49
[alexxy/gromacs.git] / src / gromacs / trajectoryanalysis / topologyinformation.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2018,2019, 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 classes in topologyinformation.h.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_trajectoryanalysis
41  */
42 #include "gmxpre.h"
43
44 #include "topologyinformation.h"
45
46 #include "gromacs/compat/make_unique.h"
47 #include "gromacs/fileio/confio.h"
48 #include "gromacs/math/vec.h"
49 #include "gromacs/pbcutil/rmpbc.h"
50 #include "gromacs/topology/mtop_util.h"
51 #include "gromacs/topology/topology.h"
52 #include "gromacs/utility/arrayref.h"
53 #include "gromacs/utility/exceptions.h"
54 #include "gromacs/utility/gmxassert.h"
55 #include "gromacs/utility/smalloc.h"
56 #include "gromacs/utility/unique_cptr.h"
57
58 namespace gmx
59 {
60
61 TopologyInformation::TopologyInformation()
62     : mtop_(compat::make_unique<gmx_mtop_t>()),
63       hasLoadedMtop_(false),
64       expandedTopology_(nullptr),
65       atoms_ (nullptr),
66       bTop_(false), ePBC_(-1)
67 {
68 }
69
70
71 TopologyInformation::~TopologyInformation()
72 {
73 }
74
75 void TopologyInformation::fillFromInputFile(const std::string &filename)
76 {
77     mtop_ = gmx::compat::make_unique<gmx_mtop_t>();
78     // TODO When filename is not a .tpr, then using readConfAndAtoms
79     // would be efficient for not doing multiple conversions for
80     // makeAtomsData. However we'd also need to be able to copy the
81     // t_atoms that we'd keep, which we currently can't do.
82     // TODO Once there are fewer callers of the file-reading
83     // functionality, make them read directly into std::vector.
84     rvec *x, *v;
85     readConfAndTopology(filename.c_str(), &bTop_, mtop_.get(),
86                         &ePBC_, &x, &v,
87                         boxtop_);
88     xtop_.assign(x, x + mtop_->natoms);
89     vtop_.assign(v, v + mtop_->natoms);
90     sfree(x);
91     sfree(v);
92     hasLoadedMtop_ = true;
93     // TODO: Only load this here if the tool actually needs it; selections
94     // take care of themselves.
95     for (gmx_moltype_t &moltype : mtop_->moltype)
96     {
97         if (!moltype.atoms.haveMass)
98         {
99             // Try to read masses from database, be silent about missing masses
100             atomsSetMassesBasedOnNames(&moltype.atoms, FALSE);
101         }
102     }
103 }
104
105 const gmx_localtop_t *TopologyInformation::expandedTopology() const
106 {
107     // Do lazy initialization
108     if (expandedTopology_ == nullptr && hasTopology())
109     {
110         expandedTopology_ = gmx::compat::make_unique<gmx_localtop_t>();
111         gmx_mtop_generate_local_top(*mtop_, expandedTopology_.get(), false);
112     }
113
114     return expandedTopology_.get();
115 }
116
117 namespace
118 {
119
120 //! Helps implement lazy initialization.
121 AtomsDataPtr makeAtoms(const TopologyInformation &top_)
122 {
123     AtomsDataPtr atoms(new t_atoms);
124     if (top_.hasTopology())
125     {
126         *atoms = gmx_mtop_global_atoms(top_.mtop());
127     }
128     else
129     {
130         init_atom(atoms.get());
131     }
132     return atoms;
133 }
134
135 }   // namespace
136
137 const t_atoms *TopologyInformation::atoms() const
138 {
139     // Do lazy initialization
140     if (atoms_ == nullptr)
141     {
142         atoms_ = makeAtoms(*this);
143     }
144
145     return atoms_.get();
146 }
147
148 AtomsDataPtr TopologyInformation::copyAtoms() const
149 {
150     // Note that we do not return atoms_, so that regardless of
151     // whether the user has already used it, or will use it in the
152     // future, any transformation operations on the data structure
153     // returned here cannot have unintended effects.
154     return makeAtoms(*this);
155 }
156
157 ArrayRef<const RVec>
158 TopologyInformation::x() const
159 {
160     if (xtop_.empty())
161     {
162         GMX_THROW(APIError("Topology coordinates requested without setting efUseTopX"));
163     }
164     return xtop_;
165 }
166
167 ArrayRef<const RVec>
168 TopologyInformation::v() const
169 {
170     if (vtop_.empty())
171     {
172         GMX_THROW(APIError("Topology coordinates requested without setting efUseTopV"));
173     }
174     return vtop_;
175 }
176
177 void
178 TopologyInformation::getBox(matrix box) const
179 {
180     GMX_RELEASE_ASSERT(box != nullptr, "Must have valid box to fill");
181     copy_mat(const_cast<rvec *>(boxtop_), box);
182 }
183
184 const char *
185 TopologyInformation::name() const
186 {
187     if (hasTopology() && mtop_->name)
188     {
189         return *mtop_->name;
190     }
191     return nullptr;
192 }
193
194 gmx_rmpbc_t gmx_rmpbc_init(const gmx::TopologyInformation &topInfo)
195 {
196     GMX_RELEASE_ASSERT(topInfo.hasTopology(), "Cannot remove PBC without a topology");
197
198     return gmx_rmpbc_init(&topInfo.expandedTopology()->idef, topInfo.ePBC(), topInfo.mtop()->natoms);
199 }
200
201 } // namespace gmx