ba679f8451dbbca8ca9be980565ecf626942bdee
[alexxy/gromacs.git] / api / nblib / gmxsetup.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief Translation layer to GROMACS data structures for force calculations.
37  *
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>
42  */
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"
68
69 namespace nblib
70 {
71
72 NbvSetupUtil::NbvSetupUtil() : gmxForceCalculator_(std::make_unique<GmxForceCalculator>()) {}
73
74 void NbvSetupUtil::setExecutionContext(const NBKernelOptions& options)
75 {
76     setGmxNonBondedNThreads(options.numOpenMPThreads);
77 }
78
79 Nbnxm::KernelSetup NbvSetupUtil::getKernelSetup(const NBKernelOptions& options)
80 {
81     return createKernelSetupCPU(options);
82 }
83
84 void NbvSetupUtil::setParticleInfoAllVdv(const size_t numParticles)
85
86 {
87     particleInfoAllVdw_ = createParticleInfoAllVdv(numParticles);
88 }
89
90 void NbvSetupUtil::setNonBondedParameters(const std::vector<ParticleType>& particleTypes,
91                                           const NonBondedInteractionMap&   nonBondedInteractionMap)
92 {
93     nonbondedParameters_ = createNonBondedParameters(particleTypes, nonBondedInteractionMap);
94 }
95
96 void NbvSetupUtil::setAtomProperties(const std::vector<int>&  particleTypeIdOfAllParticles,
97                                      const std::vector<real>& charges)
98 {
99     gmxForceCalculator_->nbv_->setAtomProperties(particleTypeIdOfAllParticles, charges, particleInfoAllVdw_);
100 }
101
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)
104 {
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);
110
111     checkKernelSetup(options.nbnxmSimd); // throws exception is setup is invalid
112
113     Nbnxm::KernelSetup kernelSetup = getKernelSetup(options);
114
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);
121
122     auto atomData = std::make_unique<nbnxn_atomdata_t>(pinPolicy,
123                                                        gmx::MDLogger(),
124                                                        kernelSetup.kernelType,
125                                                        combinationRule,
126                                                        numParticleTypes,
127                                                        nonbondedParameters_,
128                                                        1,
129                                                        numThreads);
130
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);
134
135     gmxForceCalculator_->nbv_ = std::move(nbv);
136 }
137
138 void NbvSetupUtil::setupStepWorkload(const NBKernelOptions& options)
139 {
140     gmxForceCalculator_->stepWork_ = std::make_unique<gmx::StepWorkload>(createStepWorkload(options));
141 }
142
143 void NbvSetupUtil::setupInteractionConst(const NBKernelOptions& options)
144 {
145     gmxForceCalculator_->interactionConst_ =
146             std::make_unique<interaction_const_t>(createInteractionConst(options));
147 }
148
149 void NbvSetupUtil::setupForceRec(const matrix& box)
150 {
151     updateForcerec(gmxForceCalculator_->forcerec_.get(), box);
152 }
153
154 void NbvSetupUtil::setParticlesOnGrid(const std::vector<Vec3>& coordinates, const Box& box)
155 {
156     gmxForceCalculator_->setParticlesOnGrid(particleInfoAllVdw_, coordinates, box);
157 }
158
159 void NbvSetupUtil::constructPairList(ExclusionLists<int> exclusionLists)
160 {
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());
165 }
166
167
168 std::unique_ptr<GmxForceCalculator> GmxSetupDirector::setupGmxForceCalculator(const SimulationState& system,
169                                                                               const NBKernelOptions& options)
170 {
171     NbvSetupUtil nbvSetupUtil;
172     nbvSetupUtil.setExecutionContext(options);
173     nbvSetupUtil.setNonBondedParameters(system.topology().getParticleTypes(),
174                                         system.topology().getNonBondedInteractionMap());
175     nbvSetupUtil.setParticleInfoAllVdv(system.topology().numParticles());
176
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());
185
186     return nbvSetupUtil.getGmxForceCalculator();
187 }
188
189 } // namespace nblib