Implemented changes to preprocessor to work with MiMiC QM/MM
[alexxy/gromacs.git] / src / gromacs / topology / block.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2010,2014,2015,2018, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifndef GMX_TOPOLOGY_BLOCK_H
38 #define GMX_TOPOLOGY_BLOCK_H
39
40 #include <stdio.h>
41
42 #include <vector>
43
44 #include "gromacs/utility/basedefinitions.h"
45 #include "gromacs/utility/gmxassert.h"
46
47 namespace gmx
48 {
49
50 /*! \brief Division of a range of indices into consecutive blocks
51  *
52  * A range of consecutive indices 0 to full.range.end() is divided
53  * into numBlocks() consecutive blocks of consecutive indices.
54  * Block b contains indices i for which block(b).begin() <= i < block(b).end().
55  */
56 class RangePartitioning
57 {
58     public:
59         /*! \brief Struct for returning the range of a block.
60          *
61          * Can be used in a range loop.
62          */
63         struct Block
64         {
65             public:
66                 /*! \brief An iterator that loops over integers */
67                 struct iterator
68                 {
69                     //! Constructor
70                     iterator(int value) : value_(value) {}
71                     //! Value
72                     operator int () const { return value_; }
73                     //! Reference
74                     operator int &()      { return value_; }
75                     //! Pointer
76                     int operator* () const { return value_; }
77                     //! Inequality comparison
78                     bool operator!= (const iterator other) { return value_ != other; }
79                     //! Increment operator
80                     iterator &operator++() { ++value_; return *this; }
81                     //! Increment operator
82                     iterator operator++(int) { iterator tmp(*this); ++value_; return tmp; }
83                     //! The actual value
84                     int value_;
85                 };
86
87                 /*! \brief Constructor, constructs a range starting at 0 with 0 blocks */
88                 Block(int begin,
89                       int end) :
90                     begin_(begin),
91                     end_(end)
92                 {
93                 }
94
95                 /*! \brief Begin iterator/value */
96                 const iterator begin() const { return begin_; }
97                 /*! \brief End iterator/value */
98                 const iterator end() const { return end_; }
99
100                 /*! \brief The number of items in the block */
101                 int size() const
102                 {
103                     return end_ - begin_;
104                 }
105
106                 /*! \brief Returns whether \p index is within range of the block */
107                 bool inRange(int index) const
108                 {
109                     return (begin_ <= index && index < end_);
110                 }
111
112             private:
113                 const int begin_; /**< The start index of the block */
114                 const int end_;   /**< The end index of the block */
115         };
116
117         /*! \brief Returns the number of blocks */
118         int numBlocks() const
119         {
120             return index_.size() - 1;
121         }
122
123         /*! \brief Returns the size of the block with index \p blockIndex */
124         Block block(int blockIndex) const
125         {
126             return Block(index_[blockIndex], index_[blockIndex + 1]);
127         }
128
129         /*! \brief Returns the full range */
130         Block fullRange() const
131         {
132             return Block(index_.front(), index_.back());
133         }
134
135         /*! \brief Returns a range starting at \p blockIndexBegin and ending at \p blockIndexEnd */
136         Block subRange(int blockIndexBegin,
137                        int blockIndexEnd) const
138         {
139             return Block(index_[blockIndexBegin], index_[blockIndexEnd]);
140         }
141
142         /*! \brief Returns true when all blocks have size 0 or numBlocks()=0 */
143         bool allBlocksHaveSizeOne() const
144         {
145             return (index_.back() == numBlocks());
146         }
147
148         /*! \brief Appends a block of size \p blockSize at the end of the range
149          *
150          * \note blocksize has to be >= 1
151          */
152         void appendBlock(int blockSize)
153         {
154             GMX_ASSERT(blockSize > 0, "block sizes should be >= 1");
155             index_.push_back(index_.back() + blockSize);
156         }
157
158         /*! \brief Removes all blocks */
159         void clear()
160         {
161             index_.resize(1);
162         }
163
164         /*! \brief Reduces the number of blocks to \p newNumBlocks
165          *
166          * \note \p newNumBlocks should be <= numBlocks().
167          */
168         void reduceNumBlocks(int newNumBlocks)
169         {
170             GMX_ASSERT(newNumBlocks <= numBlocks(), "Can only shrink to fewer blocks");
171             index_.resize(newNumBlocks + 1);
172         }
173
174         /*! \brief Sets the partitioning to \p numBlocks blocks each of size 1 */
175         void setAllBlocksSizeOne(int numBlocks);
176
177         /*! \brief Returns the raw block index array, avoid using this */
178         std::vector<int> &rawIndex()
179         {
180             return index_;
181         }
182
183     private:
184         std::vector<int> index_ = { 0 }; /**< The list of block begin/end indices */
185 };
186
187 }  // namespace gmx
188
189 /* Deprecated, C-style version of RangePartitioning */
190 typedef struct t_block
191 {
192     int blockSize(int blockIndex) const
193     {
194         GMX_ASSERT(blockIndex < nr, "blockIndex should be in range");
195         return index[blockIndex + 1] - index[blockIndex];
196     }
197
198     int      nr;           /* The number of blocks          */
199     int     *index;        /* Array of indices (dim: nr+1)  */
200     int      nalloc_index; /* The allocation size for index */
201 } t_block;
202
203 struct t_blocka
204 {
205     int      nr;    /* The number of blocks              */
206     int     *index; /* Array of indices in a (dim: nr+1) */
207     int      nra;   /* The number of atoms               */
208     int     *a;     /* Array of atom numbers in each group  */
209     /* (dim: nra)                           */
210     /* Block i (0<=i<nr) runs from          */
211     /* index[i] to index[i+1]-1. There will */
212     /* allways be an extra entry in index   */
213     /* to terminate the table               */
214     int nalloc_index;           /* The allocation size for index        */
215     int nalloc_a;               /* The allocation size for a            */
216 };
217
218 void init_block(t_block *block);
219 void init_blocka(t_blocka *block);
220 t_blocka *new_blocka();
221 /* allocate new block */
222
223 void done_block(t_block *block);
224 void done_blocka(t_blocka *block);
225
226 void copy_blocka(const t_blocka *src, t_blocka *dest);
227
228 void copy_block(const t_block *src, t_block *dst);
229
230 void stupid_fill_block(t_block *grp, int natom, gmx_bool bOneIndexGroup);
231 /* Fill a block structure with numbers identical to the index
232  * (0, 1, 2, .. natom-1)
233  * If bOneIndexGroup, then all atoms are  lumped in one index group,
234  * otherwise there is one atom per index entry
235  */
236
237 void stupid_fill_blocka(t_blocka *grp, int natom);
238 /* Fill a block structure with numbers identical to the index
239  * (0, 1, 2, .. natom-1)
240  * There is one atom per index entry
241  */
242
243 void pr_block(FILE *fp, int indent, const char *title, const t_block *block, gmx_bool bShowNumbers);
244 void pr_blocka(FILE *fp, int indent, const char *title, const t_blocka *block, gmx_bool bShowNumbers);
245
246 #endif