1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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 /*! \inpublicapi \file
36  * \brief
37  * Implements nblib Topology and TopologyBuilder
38  *
39  * \author Victor Holanda <victor.holanda@cscs.ch>
40  * \author Joe Jordan <ejjordan@kth.se>
41  * \author Prashanth Kanduri <kanduri@cscs.ch>
42  * \author Sebastian Keller <keller@cscs.ch>
43  * \author Artem Zhmurov <zhmurov@gmail.com>
44  */
45 #ifndef NBLIB_TOPOLOGY_H
46 #define NBLIB_TOPOLOGY_H
47 
48 #include <vector>
49 
50 #include "nblib/interactions.h"
51 #include "nblib/listed_forces/definitions.h"
52 #include "nblib/molecules.h"
53 #include "nblib/particlesequencer.h"
54 
55 namespace nblib
56 {
57 
58 /*! \brief A list of exclusion lists, optimized for performance
59  *
60  * This struct is to hold data that comes from, and will be copied into,
61  * gmx::ListOfLists. Since the ListElements and ListRanges are sufficient
62  * to create a gmx::ListOfLists, they are stored in the topoology instead
63  * of a gmx::ListOfLists to avoid dependencies.
64  */
65 template<typename T>
66 struct ExclusionLists
67 {
68     //! Ranges of the concatenated lists
69     std::vector<int> ListRanges;
70     //! Elements of the concatenated lists
71     std::vector<T> ListElements;
72 };
73 
74 /*! \inpublicapi
75  * \ingroup nblib
76  * \brief System Topology
77  *
78  * Contains all topology information meant to be used by the simulation
79  * engine internally. Private constructor ensures that a Topology object
80  * exists in a scope in a valid state after it has been built using a
81  * Topology Builder.
82  */
83 class Topology final
84 {
85 
86 public:
87     //! Returns the total number of particles in the system
88     int numParticles() const;
89 
90     //! Returns a vector of particle types
91     std::vector<ParticleType> getParticleTypes() const;
92 
93     //! Return the ParticleType ID of all particles
94     std::vector<int> getParticleTypeIdOfAllParticles() const;
95 
96     //! Returns a vector of particles partial charges
97     std::vector<real> getCharges() const;
98 
99     //! Returns exclusions in a format that can be used to create gmx::ListOfLists
100     ExclusionLists<int> exclusionLists() const;
101 
102     //! Returns the unique ID of a specific particle belonging to a molecule in the global space
103     int sequenceID(MoleculeName moleculeName, int moleculeNr, ResidueName residueName, ParticleName particleName) const;
104 
105     //! Returns a map of non-bonded force parameters indexed by ParticleType names
106     NonBondedInteractionMap getNonBondedInteractionMap() const;
107 
108     //! Returns the interaction data
109     ListedInteractionData getInteractionData() const;
110 
111     //! Returns the combination rule used to generate the NonBondedInteractionMap
112     CombinationRule getCombinationRule() const;
113 
114 private:
115     Topology() = default;
116 
117     friend class TopologyBuilder;
118 
119     //! Total number of particles in the system
120     int numParticles_;
121     //! unique collection of ParticleTypes
122     std::vector<ParticleType> particleTypes_;
123     //! store an ID of each particles's type
124     std::vector<int> particleTypeIdOfAllParticles_;
125     //! Storage for particles partial charges
126     std::vector<real> charges_;
127     //! Information about exclusions.
128     ExclusionLists<int> exclusionLists_;
129     //! Associate molecule, residue and particle names with sequence numbers
130     ParticleSequencer particleSequencer_;
131     //! Map that should hold all nonbonded interactions for all particle types
132     NonBondedInteractionMap nonBondedInteractionMap_;
133     //! data about bonds for all supported types
134     ListedInteractionData interactionData_;
135     //! Combination Rule used to generate the nonbonded interactions
136     CombinationRule combinationRule_;
137 };
138 
139 /*! \brief Topology Builder
140  *
141  * \libinternal
142  * \ingroup nblib
143  *
144  * A helper class to assist building of topologies. They also ensure that
145  * topologies only exist in a valid state within the scope of the
146  * simulation program.
147  *
148  */
149 class TopologyBuilder final
150 {
151 public:
152     //! Constructor
153     TopologyBuilder();
154 
155     /*! \brief
156      * Builds and Returns a valid Topology
157      *
158      * This function accounts for all the molecules added along with their
159      * exclusions and returns a topology with a valid state that is usable
160      * by the GROMACS back-end.
161      */
162     Topology buildTopology();
163 
164     //! Adds a molecules of a certain type into the topology
165     TopologyBuilder& addMolecule(const Molecule& moleculeType, int nMolecules);
166 
167     //! Add non-bonded interaction map to the topology
168     void addParticleTypesInteractions(const ParticleTypesInteractions& particleTypesInteractions);
169 
170 private:
171     //! Internally stored topology
172     Topology topology_;
173 
174     //! Total number of particles in the system
175     int numParticles_;
176 
177     //! List of molecule types and number of molecules
178     std::vector<std::tuple<Molecule, int>> molecules_;
179 
180     //! Builds an exclusions list aggregating exclusions from all molecules
181     ExclusionLists<int> createExclusionsLists() const;
182 
183     //! Gather interaction data from molecules
184     ListedInteractionData createInteractionData(const ParticleSequencer&);
185 
186     //! Helper function to extract quantities like mass, charge, etc from the system
187     template<typename T, class Extractor>
188     std::vector<T> extractParticleTypeQuantity(Extractor&& extractor);
189 
190     //! Distinct collection of ParticleTypes
191     std::unordered_map<std::string, ParticleType> particleTypes_;
192 
193     //! ParticleType nonbonded parameters
194     ParticleTypesInteractions particleTypesInteractions_;
195 };
196 
197 //! utility function to extract Particle quantities and expand them to the full
198 //! array of length numParticles()
199 template<class F>
expandQuantity(const Topology & topology,F && particleTypeExtractor)200 inline auto expandQuantity(const Topology& topology, F&& particleTypeExtractor)
201 {
202     using ValueType = decltype((std::declval<ParticleType>().*std::declval<F>())());
203 
204     std::vector<ValueType> ret;
205     ret.reserve(topology.numParticles());
206 
207     const std::vector<ParticleType>& particleTypes = topology.getParticleTypes();
208 
209     for (size_t id : topology.getParticleTypeIdOfAllParticles())
210     {
211         ret.push_back((particleTypes[id].*particleTypeExtractor)());
212     }
213 
214     return ret;
215 }
216 
217 } // namespace nblib
218 
219 #endif // NBLIB_TOPOLOGY_H
220