2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2020,2021, 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 * \brief Implements a force calculator based on GROMACS data structures.
38 * \author Victor Holanda <victor.holanda@cscs.ch>
39 * \author Joe Jordan <ejjordan@kth.se>
40 * \author Prashanth Kanduri <kanduri@cscs.ch>
41 * \author Sebastian Keller <keller@cscs.ch>
43 #include "gromacs/ewald/ewald_utils.h"
44 #include "gromacs/mdtypes/enerdata.h"
45 #include "gromacs/mdtypes/interaction_const.h"
46 #include "gromacs/nbnxm/atomdata.h"
47 #include "gromacs/nbnxm/nbnxm.h"
48 #include "gromacs/nbnxm/pairlistset.h"
49 #include "gromacs/nbnxm/pairlistsets.h"
50 #include "gromacs/nbnxm/pairsearch.h"
51 #include "gromacs/utility/listoflists.h"
52 #include "gromacs/utility/range.h"
53 #include "nblib/exception.h"
54 #include "nblib/gmxbackenddata.h"
55 #include "nblib/gmxcalculatorcpu.h"
56 #include "nblib/nbnxmsetuphelpers.h"
57 #include "nblib/pbc.hpp"
58 #include "nblib/systemdescription.h"
59 #include "nblib/topology.h"
60 #include "nblib/virials.h"
65 class GmxNBForceCalculatorCpu::CpuImpl final
68 CpuImpl(gmx::ArrayRef<int> particleTypeIdOfAllParticles,
69 gmx::ArrayRef<real> nonBondedParams,
70 gmx::ArrayRef<real> charges,
71 gmx::ArrayRef<int64_t> particleInteractionFlags,
72 gmx::ArrayRef<int> exclusionRanges,
73 gmx::ArrayRef<int> exclusionElements,
74 const NBKernelOptions& options);
76 //! calculates a new pair list based on new coordinates (for every NS step)
77 void updatePairlist(gmx::ArrayRef<const gmx::RVec> coordinates, const Box& box);
79 //! Compute forces and return
80 void compute(gmx::ArrayRef<const gmx::RVec> coordinateInput,
82 gmx::ArrayRef<gmx::RVec> forceOutput);
84 //! Compute forces and virial tensor
85 void compute(gmx::ArrayRef<const gmx::RVec> coordinateInput,
87 gmx::ArrayRef<gmx::RVec> forceOutput,
88 gmx::ArrayRef<real> virialOutput);
90 //! Compute forces, virial tensor and potential energies
91 void compute(gmx::ArrayRef<const gmx::RVec> coordinateInput,
93 gmx::ArrayRef<gmx::RVec> forceOutput,
94 gmx::ArrayRef<real> virialOutput,
95 gmx::ArrayRef<real> energyOutput);
98 //! \brief client-side provided system description data
99 SystemDescription system_;
101 //! \brief Gmx backend objects, employed for calculating the forces
102 GmxBackendData backend_;
105 GmxNBForceCalculatorCpu::CpuImpl::CpuImpl(gmx::ArrayRef<int> particleTypeIdOfAllParticles,
106 gmx::ArrayRef<real> nonBondedParams,
107 gmx::ArrayRef<real> charges,
108 gmx::ArrayRef<int64_t> particleInteractionFlags,
109 gmx::ArrayRef<int> exclusionRanges,
110 gmx::ArrayRef<int> exclusionElements,
111 const NBKernelOptions& options) :
112 system_(SystemDescription(particleTypeIdOfAllParticles, nonBondedParams, charges, particleInteractionFlags)),
113 backend_(GmxBackendData(options, findNumEnergyGroups(particleInteractionFlags), exclusionRanges, exclusionElements))
115 // Set up non-bonded verlet in the backend
116 backend_.nbv_ = createNbnxmCPU(system_.numParticleTypes_,
118 findNumEnergyGroups(particleInteractionFlags),
119 system_.nonBondedParams_);
122 void GmxNBForceCalculatorCpu::CpuImpl::updatePairlist(gmx::ArrayRef<const gmx::RVec> coordinates,
125 if (coordinates.size() != system_.numParticles_)
127 throw InputException(
128 "Coordinate array containing different number of entries than particles in the "
132 const auto* legacyBox = box.legacyMatrix();
134 updateForcerec(&backend_.forcerec_, box.legacyMatrix());
135 if (TRICLINIC(legacyBox))
137 throw InputException("Only rectangular unit-cells are supported here");
140 const rvec lowerCorner = { 0, 0, 0 };
141 const rvec upperCorner = { legacyBox[dimX][dimX], legacyBox[dimY][dimY], legacyBox[dimZ][dimZ] };
143 const real particleDensity = static_cast<real>(coordinates.size()) / det(legacyBox);
145 // Put particles on a grid based on bounds specified by the box
146 nbnxn_put_on_grid(backend_.nbv_.get(),
152 { 0, int(coordinates.size()) },
154 system_.particleInfo_,
159 backend_.nbv_->constructPairlist(
160 gmx::InteractionLocality::Local, backend_.exclusions_, 0, &backend_.nrnb_);
162 // Set Particle Types and Charges and VdW params
163 backend_.nbv_->setAtomProperties(
164 system_.particleTypeIdOfAllParticles_, system_.charges_, system_.particleInfo_);
165 backend_.updatePairlistCalled = true;
168 void GmxNBForceCalculatorCpu::CpuImpl::compute(gmx::ArrayRef<const gmx::RVec> coordinateInput,
170 gmx::ArrayRef<gmx::RVec> forceOutput,
171 gmx::ArrayRef<real> virialOutput,
172 gmx::ArrayRef<real> energyOutput)
174 if (coordinateInput.size() != forceOutput.size())
176 throw InputException("coordinate array and force buffer size mismatch");
179 if (!backend_.updatePairlistCalled)
181 throw InputException("compute called without updating pairlist at least once");
184 // update the box if changed
185 if (!(system_.box_ == box))
188 updateForcerec(&backend_.forcerec_, box.legacyMatrix());
191 bool computeVirial = !virialOutput.empty();
192 bool computeEnergies = !energyOutput.empty();
193 backend_.stepWork_.computeVirial = computeVirial;
194 backend_.stepWork_.computeEnergy = computeEnergies;
196 // update the coordinates in the backend
197 backend_.nbv_->convertCoordinates(gmx::AtomLocality::Local, coordinateInput);
199 backend_.nbv_->dispatchNonbondedKernel(
200 gmx::InteractionLocality::Local,
201 backend_.interactionConst_,
204 backend_.forcerec_.shift_vec,
205 backend_.enerd_.grpp.energyGroupPairTerms[backend_.forcerec_.haveBuckingham ? NonBondedEnergyTerms::BuckinghamSR
206 : NonBondedEnergyTerms::LJSR],
207 backend_.enerd_.grpp.energyGroupPairTerms[NonBondedEnergyTerms::CoulombSR],
210 backend_.nbv_->atomdata_add_nbat_f_to_f(gmx::AtomLocality::All, forceOutput);
214 // calculate shift forces and turn into an array ref
215 std::vector<Vec3> shiftForcesVector(gmx::c_numShiftVectors, Vec3(0.0, 0.0, 0.0));
216 nbnxn_atomdata_add_nbat_fshift_to_fshift(*backend_.nbv_->nbat, shiftForcesVector);
217 auto shiftForcesRef = constArrayRefFromArray(shiftForcesVector.data(), shiftForcesVector.size());
219 std::vector<Vec3> shiftVectorsArray(gmx::c_numShiftVectors);
221 // copy shift vectors from ForceRec
222 std::copy(backend_.forcerec_.shift_vec.begin(),
223 backend_.forcerec_.shift_vec.end(),
224 shiftVectorsArray.begin());
227 coordinateInput, forceOutput, shiftVectorsArray, shiftForcesRef, box, virialOutput);
230 // extract term energies (per interaction type)
233 int nGroupPairs = backend_.enerd_.grpp.nener;
234 if (int(energyOutput.size()) != int(NonBondedEnergyTerms::Count) * nGroupPairs)
236 throw InputException("Array size for energy output is wrong\n");
239 for (int eg = 0; eg < int(NonBondedEnergyTerms::Count); ++eg)
241 std::copy(begin(backend_.enerd_.grpp.energyGroupPairTerms[eg]),
242 end(backend_.enerd_.grpp.energyGroupPairTerms[eg]),
243 energyOutput.begin() + eg * nGroupPairs);
248 void GmxNBForceCalculatorCpu::CpuImpl::compute(gmx::ArrayRef<const gmx::RVec> coordinateInput,
250 gmx::ArrayRef<gmx::RVec> forceOutput)
252 // compute forces and fill in force buffer
253 compute(coordinateInput, box, forceOutput, gmx::ArrayRef<real>{}, gmx::ArrayRef<real>{});
256 void GmxNBForceCalculatorCpu::CpuImpl::compute(gmx::ArrayRef<const gmx::RVec> coordinateInput,
258 gmx::ArrayRef<gmx::RVec> forceOutput,
259 gmx::ArrayRef<real> virialOutput)
261 // compute forces and fill in force buffer
262 compute(coordinateInput, box, forceOutput, virialOutput, gmx::ArrayRef<real>{});
265 GmxNBForceCalculatorCpu::GmxNBForceCalculatorCpu(gmx::ArrayRef<int> particleTypeIdOfAllParticles,
266 gmx::ArrayRef<real> nonBondedParams,
267 gmx::ArrayRef<real> charges,
268 gmx::ArrayRef<int64_t> particleInteractionFlags,
269 gmx::ArrayRef<int> exclusionRanges,
270 gmx::ArrayRef<int> exclusionElements,
271 const NBKernelOptions& options)
275 throw InputException("Use GmxNBForceCalculatorGpu for GPU support");
278 impl_ = std::make_unique<CpuImpl>(particleTypeIdOfAllParticles,
281 particleInteractionFlags,
287 GmxNBForceCalculatorCpu::~GmxNBForceCalculatorCpu() = default;
289 //! calculates a new pair list based on new coordinates (for every NS step)
290 void GmxNBForceCalculatorCpu::updatePairlist(gmx::ArrayRef<const gmx::RVec> coordinates, const Box& box)
292 impl_->updatePairlist(coordinates, box);
295 //! Compute forces and return
296 void GmxNBForceCalculatorCpu::compute(gmx::ArrayRef<const gmx::RVec> coordinateInput,
298 gmx::ArrayRef<gmx::RVec> forceOutput)
300 impl_->compute(coordinateInput, box, forceOutput);
303 //! Compute forces and virial tensor
304 void GmxNBForceCalculatorCpu::compute(gmx::ArrayRef<const gmx::RVec> coordinateInput,
306 gmx::ArrayRef<gmx::RVec> forceOutput,
307 gmx::ArrayRef<real> virialOutput)
309 impl_->compute(coordinateInput, box, forceOutput, virialOutput);
312 //! Compute forces, virial tensor and potential energies
313 void GmxNBForceCalculatorCpu::compute(gmx::ArrayRef<const gmx::RVec> coordinateInput,
315 gmx::ArrayRef<gmx::RVec> forceOutput,
316 gmx::ArrayRef<real> virialOutput,
317 gmx::ArrayRef<real> energyOutput)
319 impl_->compute(coordinateInput, box, forceOutput, virialOutput, energyOutput);
322 std::unique_ptr<GmxNBForceCalculatorCpu> setupGmxForceCalculatorCpu(const Topology& topology,
323 const NBKernelOptions& options)
325 std::vector<real> nonBondedParameters = createNonBondedParameters(
326 topology.getParticleTypes(), topology.getNonBondedInteractionMap());
328 std::vector<int64_t> particleInteractionFlags = createParticleInfoAllVdw(topology.numParticles());
330 return std::make_unique<GmxNBForceCalculatorCpu>(topology.getParticleTypeIdOfAllParticles(),
332 topology.getCharges(),
333 particleInteractionFlags,
334 topology.exclusionLists().ListRanges,
335 topology.exclusionLists().ListElements,