2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2018, 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.
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.
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.
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.
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.
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.
36 #ifndef GROMACS_RESTRAINTMDMODULE_IMPL_H
37 #define GROMACS_RESTRAINTMDMODULE_IMPL_H
39 /*! \libinternal \file
40 * \brief Implementation details for RestraintMDModule
42 * \ingroup module_restraint
48 #include "gromacs/domdec/domdec_struct.h"
49 #include "gromacs/domdec/ga2la.h"
50 #include "gromacs/gmxlib/network.h"
51 #include "gromacs/math/vec.h"
52 #include "gromacs/mdtypes/commrec.h"
53 #include "gromacs/mdtypes/iforceprovider.h"
54 #include "gromacs/mdtypes/imdmodule.h"
55 #include "gromacs/mdtypes/imdoutputprovider.h"
56 #include "gromacs/mdtypes/imdpoptionprovider.h"
57 #include "gromacs/mdtypes/mdatom.h"
58 #include "gromacs/pbcutil/pbc.h"
59 #include "gromacs/restraint/restraintpotential.h"
60 #include "gromacs/utility/arrayref.h"
66 * \brief Abstraction for a restraint interaction site.
68 * A restraint may operate on a single atom or some other entity, such as a selection of atoms.
69 * The Restraint implementation is very independent from how coordinates are provided or what they mean.
71 * First implementation can only represent single atoms in a global context.
73 * Ultimately, this should be replaced with a more universal facility for acting
74 * on distributed atom data or simple transformations thereof.
77 * \ingroup module_restraint
82 /*! \brief Construct from global atom indices
84 * \param globalIndex Atom index in the global state (as input to the simulation)
86 explicit Site(int globalIndex
) :
92 * \brief Explicitly define copies.
94 * Implicit definition is not possible because of the mutex member,
95 * and a copy constructor is necessary to use Site in a std::vector. Really, we should make
96 * a copy point to the same implementation object to reuse its cache.
99 Site(const Site
&site
) :
104 /*! \brief Disallow assignment.
106 * Assignment doesn't make sense because it implies that a site's meaning is fuzzy.
107 * If the definition of a site is changing, just make a new site.
108 * There's nothing to be gained by reusing one or by creating it uninitialized.
110 Site
&operator=(const Site
&) = delete;
113 * \brief Get the global atom index of an atomic site.
115 * \return global index provided at construction.
118 int index() const { return index_
; }
121 * \brief Get the position of this site at time t.
123 * \param cr Communications record.
124 * \param nx Number of locally available atoms (size of local atom data arrays)
125 * \param x Array of locally available atom coordinates.
126 * \param t the current time.
127 * \return position vector.
130 * By providing the current time, we can cache results in order to use them once per timestep.
131 * In the long term, we would prefer to also allow client code to preregister interest in a
132 * position at a given time, or issue "futures".
134 RVec
centerOfMass(const t_commrec
&cr
,
136 ArrayRef
<const RVec
> x
,
139 // Center of mass to return for the site. Currently the only form of site
140 // implemented is as a global atomic coordinate.
141 gmx::RVec r
= {0, 0, 0};
142 if (DOMAINDECOMP(&cr
)) // Domain decomposition
144 // Get global-to-local indexing structure
145 auto crossRef
= cr
.dd
->ga2la
;
146 GMX_ASSERT(crossRef
, "Domain decomposition must provide global/local cross-reference.");
147 if (const auto localIndex
= crossRef
->findHome(index_
))
149 GMX_ASSERT(localIndex
, "Expect not to reach this point if findHome does not find index_.");
150 GMX_ASSERT(*localIndex
< static_cast<decltype(*localIndex
)>(nx
),
151 "We assume that the local index cannot be larger than the number of atoms.");
152 GMX_ASSERT(*localIndex
>= 0, "localIndex is a signed type, but is assumed >0.");
153 // If atom is local, get its location
154 copy_rvec(x
[*localIndex
], r
);
158 // Nothing to contribute on this rank. Leave position == [0,0,0].
160 // AllReduce across the ranks of the simulation to get the center-of-mass
161 // of the site locally available everywhere. For single-atom sites, this
162 // is trivial: exactly one rank should have a non-zero position.
163 // For future multi-atom selections,
164 // we will receive weighted center-of-mass contributions from
165 // each rank and combine to get the global center of mass.
166 // \todo use generalized "pull group" facility when available.
167 std::array
<double, 3> buffer
{{r
[0], r
[1], r
[2]}};
168 // This should be an all-reduce sum, which gmx_sumd appears to be.
169 gmx_sumd(3, buffer
.data(), &cr
);
170 r
[0] = static_cast<real
>(buffer
[0]);
171 r
[1] = static_cast<real
>(buffer
[1]);
172 r
[2] = static_cast<real
>(buffer
[2]);
174 } // end domain decomposition branch
177 // No DD so all atoms are local.
178 copy_rvec(x
[index_
], r
);
181 // Update cache and cache status.
189 * \brief Global index of the single-atom site.
191 * \todo This class should be a specialization of a more general Site data source.
192 * \todo use LocalAtomSet
197 * \brief Last known value of the center-of-mass.
199 * Updated with centerOfMass().
205 * \brief Provide IForceProvider for RestraintMDModuleImpl
207 * Adapter class from IForceProvider to IRestraintPotential.
208 * Objects of this type are uniquely owned by instances of RestraintMDModuleImpl. The object will
209 * dispatch calls to IForceProvider->calculateForces() to the functor managed by RestraintMDModuleImpl.
210 * \ingroup module_restraint
212 class RestraintForceProvider final
: public gmx::IForceProvider
216 * \brief Can only be constructed when initialized from a restraint.
218 RestraintForceProvider() = delete;
220 ~RestraintForceProvider() = default;
223 * \brief RAII construction with an IRestraintPotential
225 * Note, this object must outlive the pointer that will be provided to ForceProviders.
226 * \param restraint handle to an object providing restraint potential calculation
227 * \param sites List of atomic site indices
229 explicit RestraintForceProvider(std::shared_ptr
<gmx::IRestraintPotential
> restraint
,
230 const std::vector
<int> &sites
);
233 * \brief Implement the IForceProvider interface.
235 * Update the force array with restraint contribution(s) for local atoms.
237 * RestraintForceProvider is implemented with the assumption that few
238 * restraints apply to many atoms.
239 * That is, the number of restraints affecting a large number of atoms is small,
240 * though there may be several restraints that apply to few atoms each.
241 * Under this assumption, it is considered computationally inexpensive to iterate
242 * over restraints in an outer loop and iterate over atoms within each restraint.
243 * This would be an invalid assumption if, say, several restraints applied
244 * to an entire membrane or the entire solvent group.
246 * If the assumption causes performance problems, we can look for a good
247 * way to reduce from several restraints in a single pass or a very
248 * lightweight way to determine whether a given restraint applies to a given atom.
249 * There is also the notion in the pulling code of a limited number of
250 * "pull groups" used by the "pull coordinates".
251 * The right optimization will depend on how the code is being used.
253 * Call the evaluator(s) for the restraints for the configured sites.
254 * Forces are applied to atoms in the first and last site listed.
255 * Intermediate sites are used as reference coordinates when the relevant
256 * vector between sites is on the order of half a box length or otherwise
257 * ambiguous in the case of periodic boundary conditions.
259 void calculateForces(const ForceProviderInput
&forceProviderInput
,
260 ForceProviderOutput
*forceProviderOutput
) override
;
263 std::shared_ptr
<gmx::IRestraintPotential
> restraint_
;
264 std::vector
<Site
> sites_
;
268 * \brief IMDModule implementation for RestraintMDModule.
270 * Provides IMDModule interface.
272 * \ingroup module_restraint
274 class RestraintMDModuleImpl final
277 RestraintMDModuleImpl() = delete;
279 * \brief Wrap an object implementing IRestraintPotential
281 * \param restraint handle to restraint to wrap.
282 * \param sites list of sites for framework to process for restraint force calculator.
284 RestraintMDModuleImpl(std::shared_ptr
<gmx::IRestraintPotential
> restraint
,
285 const std::vector
<int>&sites
);
288 * \brief Allow moves.
292 RestraintMDModuleImpl(RestraintMDModuleImpl
&&) noexcept
= default;
293 RestraintMDModuleImpl
&operator=(RestraintMDModuleImpl
&&) noexcept
= default;
296 ~RestraintMDModuleImpl();
299 * \brief Unused implementation of IMDModule aspect
301 IMdpOptionProvider
*mdpOptionProvider();
304 * \brief Unused implementation of IMDModule aspect
306 IMDOutputProvider
*outputProvider();
309 * \brief Implement IMDModule interface.
311 * \param forceProviders force module manager in the force record that will call this.
313 * The calling code must ensure that this object stays alive as long as forceProviders needs
314 * the RestraintForceProvider, since forceProviders can't. Typically that is the duration of a do_md() call.
316 void initForceProviders(ForceProviders
*forceProviders
);
318 //! handle to RestraintForceProvider implementation
319 std::unique_ptr
<RestraintForceProvider
> forceProvider_
;
322 } // end namespace gmx
324 #endif //GROMACS_RESTRAINTMDMODULE_IMPL_H