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     //! Returns the kernel setup
89     Nbnxm::KernelSetup getKernelSetup(const NBKernelOptions& options);
90 
91     //! Set up StepWorkload data
92     void setupStepWorkload(const NBKernelOptions& options);
93 
94     //! Return an interaction constants struct with members set appropriately
95     void setupInteractionConst(const NBKernelOptions& options);
96 
97     //! Sets Particle Types and Charges and VdW params
98     void setAtomProperties(const std::vector<int>&  particleTypeIdOfAllParticles,
99                            const std::vector<real>& charges);
100 
101     //! Sets up non-bonded verlet on the GmxForceCalculator
102     void setupNbnxmInstance(size_t numParticleTypes, const NBKernelOptions& options);
103 
104     //! Puts particles on a grid based on bounds specified by the box
105     void setParticlesOnGrid(const std::vector<Vec3>& coordinates, const Box& box);
106 
107     //! Constructs pair lists
108     void constructPairList(ExclusionLists<int> exclusionLists);
109 
110     //! Sets up t_forcerec object on the GmxForceCalculator
111     void setupForceRec(const matrix& box);
112 
113     //! Returns a unique pointer a GmxForceCalculator object
getGmxForceCalculator()114     std::unique_ptr<GmxForceCalculator> getGmxForceCalculator()
115     {
116         return std::move(gmxForceCalculator_);
117     }
118 
119 private:
120     //! Storage for parameters for short range interactions.
121     std::vector<real> nonbondedParameters_;
122 
123     //! Particle info where all particles are marked to have Van der Waals interactions
124     std::vector<int> particleInfoAllVdw_;
125 
126     //! GROMACS force calculator to compute forces
127     std::unique_ptr<GmxForceCalculator> gmxForceCalculator_;
128 };
129 
130 /*! \brief Calls the setup utilities needed to initialize a GmxForceCalculator object
131  *
132  * The GmxSetupDirector encapsulates the multi-stage setup of the GmxForceCalculator which
133  * is done using the public functions of the NbvSetupUtil. This separation ensures that the
134  * NbvSetupUtil object is temporary in scope. The function definition makes it easy for the
135  * developers to follow the sequence of calls and the dataflow involved in setting up
136  * the non-bonded force calculation backend. This is the only function needed to be called
137  * from the ForceCalculator during construction.
138  *
139  */
140 class GmxSetupDirector
141 {
142 public:
143     //! Sets up and returns a GmxForceCalculator
144     static std::unique_ptr<GmxForceCalculator> setupGmxForceCalculator(const SimulationState& system,
145                                                                        const NBKernelOptions& options);
146 };
147 
148 } // namespace nblib
149 #endif // NBLIB_GMXSETUP_H
150