17774c2f77e2173faff0fc4608c07650db8c453e
[alexxy/gromacs.git] / api / nblib / topology.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020, 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/molecules.h"
52 #include "nblib/topologyhelpers.h"
53
54 namespace gmx
55 {
56 template<typename>
57 class ListOfLists;
58 } // namespace gmx
59
60 namespace nblib
61 {
62
63 /*! \inpublicapi
64  * \ingroup nblib
65  * \brief System Topology
66  *
67  * Contains all topology information meant to be used by the simulation
68  * engine internally. Private constructor ensures that a Topology object
69  * exists in a scope in a valid state after it has been built using a
70  * Topology Builder.
71  */
72 class Topology final
73 {
74
75 public:
76     //! Returns the total number of particles in the system
77     int numParticles() const;
78
79     //! Returns a vector of particle types
80     std::vector<ParticleType> getParticleTypes() const;
81
82     //! Return the ParticleType ID of all particles
83     std::vector<int> getParticleTypeIdOfAllParticles() const;
84
85     //! Returns a vector of particles partial charges
86     std::vector<real> getCharges() const;
87
88     //! Returns exclusions in proper, performant, GROMACS layout
89     gmx::ListOfLists<int> getGmxExclusions() const;
90
91     //! Returns the unique ID of a specific particle belonging to a molecule in the global space
92     int sequenceID(MoleculeName moleculeName, int moleculeNr, ResidueName residueName, ParticleName particleName) const;
93
94     //! Returns a map of non-bonded force parameters indexed by ParticleType names
95     NonBondedInteractionMap getNonBondedInteractionMap() const;
96
97     //! Returns the combination rule used to generate the NonBondedInteractionMap
98     CombinationRule getCombinationRule() const;
99
100 private:
101     Topology() = default;
102
103     friend class TopologyBuilder;
104
105     //! Total number of particles in the system
106     int numParticles_;
107     //! unique collection of ParticleTypes
108     std::vector<ParticleType> particleTypes_;
109     //! store an ID of each particles's type
110     std::vector<int> particleTypeIdOfAllParticles_;
111     //! Storage for particles partial charges
112     std::vector<real> charges_;
113     //! Information about exclusions.
114     gmx::ListOfLists<int> exclusions_;
115     //! Associate molecule, residue and particle names with sequence numbers
116     detail::ParticleSequencer particleSequencer_;
117     //! Map that should hold all nonbonded interactions for all particle types
118     NonBondedInteractionMap nonBondedInteractionMap_;
119     //! Combination Rule used to generate the nonbonded interactions
120     CombinationRule combinationRule_;
121 };
122
123 /*! \brief Topology Builder
124  *
125  * \libinternal
126  * \ingroup nblib
127  *
128  * A helper class to assist building of topologies. They also ensure that
129  * topologies only exist in a valid state within the scope of the
130  * simulation program.
131  *
132  */
133 class TopologyBuilder final
134 {
135 public:
136     //! Constructor
137     TopologyBuilder();
138
139     /*! \brief
140      * Builds and Returns a valid Topology
141      *
142      * This function accounts for all the molecules added along with their
143      * exclusions and returns a topology with a valid state that is usable
144      * by the GROMACS back-end.
145      */
146     Topology buildTopology();
147
148     //! Adds a molecules of a certain type into the topology
149     TopologyBuilder& addMolecule(const Molecule& moleculeType, int nMolecules);
150
151     //! Add non-bonded interaction map to the topology
152     void addParticleTypesInteractions(const ParticleTypesInteractions& particleTypesInteractions);
153
154 private:
155     //! Internally stored topology
156     Topology topology_;
157
158     //! Total number of particles in the system
159     int numParticles_;
160
161     //! List of molecule types and number of molecules
162     std::vector<std::tuple<Molecule, int>> molecules_;
163
164     //! Builds a GROMACS-compliant performant exclusions list aggregating exclusions from all molecules
165     gmx::ListOfLists<int> createExclusionsListOfLists() const;
166
167     //! Helper function to extract quantities like mass, charge, etc from the system
168     template<typename T, class Extractor>
169     std::vector<T> extractParticleTypeQuantity(Extractor&& extractor);
170
171     //! Distinct collection of ParticleTypes
172     std::unordered_map<std::string, ParticleType> particleTypes_;
173
174     //! ParticleType nonbonded parameters
175     ParticleTypesInteractions particleTypesInteractions_;
176 };
177
178 } // namespace nblib
179
180 #endif // NBLIB_TOPOLOGY_H