NBLIB listed forces API update
[alexxy/gromacs.git] / api / nblib / topologyhelpers.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 helper functions needed for the nblib topology
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_HELPERS_H
46 #define NBLIB_TOPOLOGY_HELPERS_H
47
48 #include <tuple>
49 #include <unordered_map>
50 #include <vector>
51
52 #include "gromacs/utility/listoflists.h"
53 #include "nblib/listed_forces/traits.h"
54 #include "nblib/molecules.h"
55
56 namespace gmx
57 {
58 struct ExclusionBlock;
59 }
60
61 namespace nblib
62 {
63
64 namespace detail
65 {
66
67 //! Converts tuples of particle indices to exclude to the gmx::ExclusionBlock format
68 std::vector<gmx::ExclusionBlock> toGmxExclusionBlock(const std::vector<std::tuple<int, int>>& tupleList);
69
70 //! Add offset to all indices in inBlock
71 std::vector<gmx::ExclusionBlock> offsetGmxBlock(std::vector<gmx::ExclusionBlock> inBlock, int offset);
72
73 /*!
74  * \brief
75  * Extract all interactions of type I from a vector of molecules. The second argument tuple element
76  * specifies multiples of the molecule given as first tuple element. Let (S, I) denote the return
77  * value tuple. Then J[i] = I[S[i]] for all i in 0...S.size() is the full sequence of BondType
78  * instances as they occur in the input tuple
79  *
80  */
81 template<class I>
82 std::tuple<std::vector<size_t>, std::vector<I>>
83 collectInteractions(const std::vector<std::tuple<Molecule, int>>&);
84
85 #define COLLECT_BONDS_EXTERN_TEMPLATE(x)                                                 \
86     extern template std::tuple<std::vector<size_t>, std::vector<x>> collectInteractions( \
87             const std::vector<std::tuple<Molecule, int>>&);
88 MAP(COLLECT_BONDS_EXTERN_TEMPLATE, SUPPORTED_TWO_CENTER_TYPES)
89 #undef COLLECT_BONDS_EXTERN_TEMPLATE
90
91 /*!
92  * \brief
93  * Return a list of unique BondType instances U and an index list S of size aggregatedBonds.size()
94  * such that the BondType instance at aggregatedBonds[i] is equal to U[S[i]]
95  * returns std::tuple(S, U)
96  *
97  */
98 template<class I>
99 std::tuple<std::vector<size_t>, std::vector<I>> eliminateDuplicateInteractions(const std::vector<I>& collectedBonds);
100
101 /// \cond DO_NOT_DOCUMENT
102 #define ELIMINATE_DUPLICATE_EXTERN_TEMPLATE(x)                                                      \
103     extern template std::tuple<std::vector<size_t>, std::vector<x>> eliminateDuplicateInteractions( \
104             const std::vector<x>& collectedBonds);
105 MAP(ELIMINATE_DUPLICATE_EXTERN_TEMPLATE, SUPPORTED_LISTED_TYPES)
106 #undef ELIMINATE_DUPLICATE_EXTERN_TEMPLATE
107 /// \endcond
108
109 //! Helper class for Topology to keep track of particle IDs
110 class ParticleSequencer
111 {
112     //! Alias for storing by (molecule name, molecule nr, residue name, particle name)
113     using DataType = std::unordered_map<
114             std::string,
115             std::unordered_map<int, std::unordered_map<std::string, std::unordered_map<std::string, int>>>>;
116
117 public:
118     //! Build sequence from a list of molecules
119     void build(const std::vector<std::tuple<Molecule, int>>& moleculesList);
120
121     //! Access ID by (molecule name, molecule nr, residue name, particle name)
122     int operator()(const MoleculeName&, int, const ResidueName&, const ParticleName&) const;
123
124 private:
125     DataType data_;
126 };
127
128 //!
129 template<class B>
130 std::vector<CoordinateIndex<B>> sequenceIDs(const std::vector<std::tuple<Molecule, int>>&,
131                                             const detail::ParticleSequencer&);
132
133 /// \cond DO_NOT_DOCUMENT
134 #define SEQUENCE_PAIR_ID_EXTERN_TEMPLATE(x)                         \
135     extern template std::vector<CoordinateIndex<x>> sequenceIDs<x>( \
136             const std::vector<std::tuple<Molecule, int>>&, const detail::ParticleSequencer&);
137 MAP(SEQUENCE_PAIR_ID_EXTERN_TEMPLATE, SUPPORTED_LISTED_TYPES)
138 #undef SEQUENCE_PAIR_ID_EXTERN_TEMPLATE
139 /// \endcond
140
141 } // namespace detail
142
143 } // namespace nblib
144
145 #endif // NBLIB_TOPOLOGY_HELPERS_H