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 Translation layer to GROMACS data structures for force calculations.
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 "nblib/gmxsetup.h"
44 #include "gromacs/ewald/ewald_utils.h"
45 #include "gromacs/gmxlib/nrnb.h"
46 #include "gromacs/mdlib/forcerec.h"
47 #include "gromacs/mdlib/gmx_omp_nthreads.h"
48 #include "gromacs/mdlib/rf_util.h"
49 #include "gromacs/mdtypes/atominfo.h"
50 #include "gromacs/mdtypes/forcerec.h"
51 #include "gromacs/mdtypes/interaction_const.h"
52 #include "gromacs/mdtypes/simulation_workload.h"
53 #include "gromacs/nbnxm/atomdata.h"
54 #include "gromacs/nbnxm/nbnxm.h"
55 #include "gromacs/nbnxm/nbnxm_simd.h"
56 #include "gromacs/nbnxm/pairlistset.h"
57 #include "gromacs/nbnxm/pairlistsets.h"
58 #include "gromacs/nbnxm/pairsearch.h"
59 #include "gromacs/pbcutil/pbc.h"
60 #include "gromacs/utility/logger.h"
61 #include "gromacs/utility/listoflists.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "nblib/exception.h"
64 #include "nblib/kerneloptions.h"
65 #include "nblib/nbnxmsetuphelpers.h"
66 #include "nblib/particletype.h"
67 #include "nblib/simulationstate.h"
72 NbvSetupUtil::NbvSetupUtil() : gmxForceCalculator_(std::make_unique<GmxForceCalculator>()) {}
74 void NbvSetupUtil::setExecutionContext(const NBKernelOptions& options)
76 setGmxNonBondedNThreads(options.numOpenMPThreads);
79 Nbnxm::KernelSetup NbvSetupUtil::getKernelSetup(const NBKernelOptions& options)
81 return createKernelSetupCPU(options);
84 void NbvSetupUtil::setParticleInfoAllVdv(const size_t numParticles)
87 particleInfoAllVdw_ = createParticleInfoAllVdv(numParticles);
90 void NbvSetupUtil::setNonBondedParameters(const std::vector<ParticleType>& particleTypes,
91 const NonBondedInteractionMap& nonBondedInteractionMap)
93 nonbondedParameters_ = createNonBondedParameters(particleTypes, nonBondedInteractionMap);
96 void NbvSetupUtil::setAtomProperties(const std::vector<int>& particleTypeIdOfAllParticles,
97 const std::vector<real>& charges)
99 gmxForceCalculator_->nbv_->setAtomProperties(particleTypeIdOfAllParticles, charges, particleInfoAllVdw_);
102 //! Sets up and returns a Nbnxm object for the given options and system
103 void NbvSetupUtil::setupNbnxmInstance(const size_t numParticleTypes, const NBKernelOptions& options)
105 const auto pinPolicy = (options.useGpu ? gmx::PinningPolicy::PinnedIfSupported
106 : gmx::PinningPolicy::CannotBePinned);
107 const int numThreads = options.numOpenMPThreads;
108 // Note: the options and Nbnxm combination rule enums values should match
109 const int combinationRule = static_cast<int>(options.ljCombinationRule);
111 checkKernelSetup(options.nbnxmSimd); // throws exception is setup is invalid
113 Nbnxm::KernelSetup kernelSetup = getKernelSetup(options);
115 PairlistParams pairlistParams(kernelSetup.kernelType, false, options.pairlistCutoff, false);
116 Nbnxm::GridSet gridSet(
117 PbcType::Xyz, false, nullptr, nullptr, pairlistParams.pairlistType, false, numThreads, pinPolicy);
118 auto pairlistSets = std::make_unique<PairlistSets>(pairlistParams, false, 0);
119 auto pairSearch = std::make_unique<PairSearch>(
120 PbcType::Xyz, false, nullptr, nullptr, pairlistParams.pairlistType, false, numThreads, pinPolicy);
122 auto atomData = std::make_unique<nbnxn_atomdata_t>(pinPolicy,
124 kernelSetup.kernelType,
127 nonbondedParameters_,
131 // Put everything together
132 auto nbv = std::make_unique<nonbonded_verlet_t>(
133 std::move(pairlistSets), std::move(pairSearch), std::move(atomData), kernelSetup, nullptr, nullptr);
135 gmxForceCalculator_->nbv_ = std::move(nbv);
138 void NbvSetupUtil::setupStepWorkload(const NBKernelOptions& options)
140 gmxForceCalculator_->stepWork_ = std::make_unique<gmx::StepWorkload>(createStepWorkload(options));
143 void NbvSetupUtil::setupInteractionConst(const NBKernelOptions& options)
145 gmxForceCalculator_->interactionConst_ =
146 std::make_unique<interaction_const_t>(createInteractionConst(options));
149 void NbvSetupUtil::setupForceRec(const matrix& box)
151 updateForcerec(gmxForceCalculator_->forcerec_.get(), box);
154 void NbvSetupUtil::setParticlesOnGrid(const std::vector<Vec3>& coordinates, const Box& box)
156 gmxForceCalculator_->setParticlesOnGrid(particleInfoAllVdw_, coordinates, box);
159 void NbvSetupUtil::constructPairList(ExclusionLists<int> exclusionLists)
161 gmx::ListOfLists<int> exclusions(std::move(exclusionLists.ListRanges),
162 std::move(exclusionLists.ListElements));
163 gmxForceCalculator_->nbv_->constructPairlist(
164 gmx::InteractionLocality::Local, exclusions, 0, gmxForceCalculator_->nrnb_.get());
168 std::unique_ptr<GmxForceCalculator> GmxSetupDirector::setupGmxForceCalculator(const SimulationState& system,
169 const NBKernelOptions& options)
171 NbvSetupUtil nbvSetupUtil;
172 nbvSetupUtil.setExecutionContext(options);
173 nbvSetupUtil.setNonBondedParameters(system.topology().getParticleTypes(),
174 system.topology().getNonBondedInteractionMap());
175 nbvSetupUtil.setParticleInfoAllVdv(system.topology().numParticles());
177 nbvSetupUtil.setupInteractionConst(options);
178 nbvSetupUtil.setupStepWorkload(options);
179 nbvSetupUtil.setupNbnxmInstance(system.topology().getParticleTypes().size(), options);
180 nbvSetupUtil.setParticlesOnGrid(system.coordinates(), system.box());
181 nbvSetupUtil.constructPairList(system.topology().exclusionLists());
182 nbvSetupUtil.setAtomProperties(system.topology().getParticleTypeIdOfAllParticles(),
183 system.topology().getCharges());
184 nbvSetupUtil.setupForceRec(system.box().legacyMatrix());
186 return nbvSetupUtil.getGmxForceCalculator();