Add replacements for pbc enumerations
[gromacs.git] / src / gromacs / domdec / ga2la.h
blobff1e514683a40717a319218634437a955dfebff4
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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,2017,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.
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.
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.
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.
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.
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.
37 /*! \libinternal \file
38 * \brief
39 * Defines structures and functions for mapping from global to local atom
40 * indices. The functions are performance critical and should be inlined.
42 * \inlibraryapi
43 * \ingroup module_domdec
45 * \author Berk Hess <hess@kth.se>
48 #ifndef GMX_DOMDEC_GA2LA_H
49 #define GMX_DOMDEC_GA2LA_H
51 #include <vector>
53 #include "gromacs/domdec/hashedmap.h"
54 #include "gromacs/utility/gmxassert.h"
56 /*! \libinternal \brief Global to local atom mapping
58 * Used for efficient mapping from global atom indices to local atom indices
59 * in the domain decomposition.
61 class gmx_ga2la_t
63 public:
64 /*! \libinternal \brief Structure for the local atom info */
65 struct Entry
67 int la; /**< The local atom index */
68 int cell; /**< The DD zone index for neighboring domains, zone+zone otherwise */
71 /*! \brief Constructor
73 * \param[in] numAtomsTotal The total number of atoms in the system
74 * \param[in] numAtomsLocal An estimate of the number of home+communicated atoms
76 gmx_ga2la_t(int numAtomsTotal,
77 int numAtomsLocal);
78 ~gmx_ga2la_t()
80 usingDirect_ ? data_.direct.~vector() : data_.hashed.~HashedMap();
83 /*! \brief Inserts an entry, there should not already be an entry for \p a_gl
85 * \param[in] a_gl The global atom index
86 * \param[in] value The value to set for this index
88 void insert(int a_gl,
89 const Entry &value)
91 GMX_ASSERT(a_gl >= 0, "Only global atom indices >= 0 are supported");
92 if (usingDirect_)
94 GMX_ASSERT(data_.direct[a_gl].cell == -1, "The key to be inserted should not be present");
95 data_.direct[a_gl] = value;
97 else
99 data_.hashed.insert(a_gl, value);
103 //! Delete the entry for global atom a_gl
104 void erase(int a_gl)
106 if (usingDirect_)
108 data_.direct[a_gl].cell = -1;
110 else
112 data_.hashed.erase(a_gl);
116 //! Returns a pointer to the entry when present, nullptr otherwise
117 const Entry* find(int a_gl) const
119 if (usingDirect_)
121 return (data_.direct[a_gl].cell == -1) ? nullptr : &(data_.direct[a_gl]);
123 else
125 return (data_.hashed.find(a_gl));
129 //! Returns the local atom index if it is a home atom, nullptr otherwise
130 const int* findHome(int a_gl) const
132 const Entry* const e = find(a_gl);
133 return (e && e->cell == 0) ? &(e->la) : nullptr;
136 /*! \brief Returns a reference to the entry for a_gl
138 * A non-release assert checks that a_gl is present.
140 Entry &at(int a_gl)
142 if (usingDirect_)
144 GMX_ASSERT(data_.direct[a_gl].cell >= 0, "a_gl should be present");
145 return data_.direct[a_gl];
147 else
149 Entry *search = data_.hashed.find(a_gl);
150 GMX_ASSERT(search, "a_gl should be present");
151 return *search;
155 //! Clear all the entries in the list.
156 void clear()
158 if (usingDirect_)
160 for (Entry &entry : data_.direct) {entry.cell = -1; }
162 else
164 data_.hashed.clear();
168 private:
169 union Data
171 std::vector<Entry> direct;
172 gmx::HashedMap<Entry> hashed;
173 // constructor and destructor function in parent class
174 Data() {}
175 ~Data() {}
176 } data_;
177 const bool usingDirect_;
180 #endif