Rework nblib kernel checks and setup code
[alexxy/gromacs.git] / api / nblib / gmxsetup.h
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 /*! \libinternal \file
36  * \brief Translation layer to GROMACS data structures for force calculations.
37  *
38  * Implements the translation layer between the user scope and
39  * GROMACS data structures for force calculations. Sets up the
40  * non-bonded verlet.
41  *
42  * \author Victor Holanda <victor.holanda@cscs.ch>
43  * \author Joe Jordan <ejjordan@kth.se>
44  * \author Prashanth Kanduri <kanduri@cscs.ch>
45  * \author Sebastian Keller <keller@cscs.ch>
46  */
47
48 #ifndef NBLIB_GMXSETUP_H
49 #define NBLIB_GMXSETUP_H
50
51 #include "nblib/gmxcalculator.h"
52 #include "nblib/simulationstate.h"
53
54 namespace Nbnxm
55 {
56 struct KernelSetup;
57 }
58
59 namespace nblib
60 {
61
62 /*! \brief Sets up the GROMACS data structures for the non-bonded force calculator
63  *
64  * This data structure initializes the GmxForceCalculator object which internally
65  * contains various objects needed to perform non-bonded force calculations using
66  * the internal representation for the problem as required for GROMACS.
67  *
68  * The public functions of this class basically translate the problem description
69  * specified by the user in NBLIB. This ultimately returns the GmxForceCalculator
70  * object which is used by the ForceCalculator object in the user-facing library.
71  *
72  */
73 class NbvSetupUtil final
74 {
75 public:
76     NbvSetupUtil();
77
78     //! Sets hardware params from the execution context
79     void setExecutionContext(const NBKernelOptions& options);
80
81     //! Sets non-bonded parameters to be used to build GMX data structures
82     void setNonBondedParameters(const std::vector<ParticleType>& particleTypes,
83                                 const NonBondedInteractionMap&   nonBondedInteractionMap);
84
85     //! Marks particles to have Van der Waals interactions
86     void setParticleInfoAllVdv(size_t numParticles);
87
88     //! Set up StepWorkload data
89     void setupStepWorkload(const NBKernelOptions& options);
90
91     //! Return an interaction constants struct with members set appropriately
92     void setupInteractionConst(const NBKernelOptions& options);
93
94     //! Sets Particle Types and Charges and VdW params
95     void setAtomProperties(const std::vector<int>&  particleTypeIdOfAllParticles,
96                            const std::vector<real>& charges);
97
98     //! Sets up non-bonded verlet on the GmxForceCalculator
99     void setupNbnxmInstance(size_t numParticleTypes, const NBKernelOptions& options);
100
101     //! Puts particles on a grid based on bounds specified by the box
102     void setParticlesOnGrid(const std::vector<Vec3>& coordinates, const Box& box);
103
104     //! Constructs pair lists
105     void constructPairList(ExclusionLists<int> exclusionLists);
106
107     //! Sets up t_forcerec object on the GmxForceCalculator
108     void setupForceRec(const matrix& box);
109
110     //! Returns a unique pointer a GmxForceCalculator object
111     std::unique_ptr<GmxForceCalculator> getGmxForceCalculator()
112     {
113         return std::move(gmxForceCalculator_);
114     }
115
116 private:
117     //! Storage for parameters for short range interactions.
118     std::vector<real> nonbondedParameters_;
119
120     //! Particle info where all particles are marked to have Van der Waals interactions
121     std::vector<int64_t> particleInfoAllVdw_;
122
123     //! GROMACS force calculator to compute forces
124     std::unique_ptr<GmxForceCalculator> gmxForceCalculator_;
125 };
126
127 /*! \brief Calls the setup utilities needed to initialize a GmxForceCalculator object
128  *
129  * The GmxSetupDirector encapsulates the multi-stage setup of the GmxForceCalculator which
130  * is done using the public functions of the NbvSetupUtil. This separation ensures that the
131  * NbvSetupUtil object is temporary in scope. The function definition makes it easy for the
132  * developers to follow the sequence of calls and the dataflow involved in setting up
133  * the non-bonded force calculation backend. This is the only function needed to be called
134  * from the ForceCalculator during construction.
135  *
136  */
137 class GmxSetupDirector
138 {
139 public:
140     //! Sets up and returns a GmxForceCalculator
141     static std::unique_ptr<GmxForceCalculator> setupGmxForceCalculator(const SimulationState& system,
142                                                                        const NBKernelOptions& options);
143 };
144
145 } // namespace nblib
146 #endif // NBLIB_GMXSETUP_H