Make CpuPpLongRangeNonbondeds class
[alexxy/gromacs.git] / src / gromacs / modularsimulator / forceelement.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,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 Declares the force element for the modular simulator
37  *
38  * This element calculates the forces, with or without shells or
39  * flexible constraints.
40  *
41  * \author Pascal Merz <pascal.merz@me.com>
42  * \ingroup module_modularsimulator
43  *
44  * This header is only used within the modular simulator module
45  */
46
47 #ifndef GMX_MODULARSIMULATOR_FORCEELEMENT_H
48 #define GMX_MODULARSIMULATOR_FORCEELEMENT_H
49
50 #include <array>
51
52 #include "gromacs/domdec/dlbtiming.h"
53 #include "gromacs/mdtypes/md_enums.h"
54 #include "gromacs/utility/enumerationhelpers.h"
55 #include "gromacs/utility/real.h"
56
57 #include "modularsimulatorinterfaces.h"
58 #include "topologyholder.h"
59
60 struct gmx_enfrot;
61 struct gmx_shellfc_t;
62 struct gmx_wallcycle;
63 class CpuPpLongRangeNonbondeds;
64 struct pull_t;
65 struct t_nrnb;
66
67 namespace gmx
68 {
69 class Awh;
70 class EnergyData;
71 class FreeEnergyPerturbationData;
72 class GlobalCommunicationHelper;
73 class ImdSession;
74 class LegacySimulatorData;
75 class MDAtoms;
76 class MdrunScheduleWorkload;
77 class ModularSimulatorAlgorithmBuilderHelper;
78 class ObservablesReducer;
79 class StatePropagatorData;
80 class VirtualSitesHandler;
81
82 /*! \internal
83  * \ingroup module_modularsimulator
84  * \brief Force element
85  *
86  * The force element manages the call to either
87  * do_force(...) or relax_shell_flexcon(...)
88  */
89 class ForceElement final :
90     public ISimulatorElement,
91     public ITopologyHolderClient,
92     public INeighborSearchSignallerClient,
93     public IEnergySignallerClient,
94     public IDomDecHelperClient
95 {
96 public:
97     //! Constructor
98     ForceElement(StatePropagatorData*        statePropagatorData,
99                  EnergyData*                 energyData,
100                  FreeEnergyPerturbationData* freeEnergyPerturbationData,
101                  bool                        isVerbose,
102                  bool                        isDynamicBox,
103                  FILE*                       fplog,
104                  const t_commrec*            cr,
105                  const t_inputrec*           inputrec,
106                  const MDAtoms*              mdAtoms,
107                  t_nrnb*                     nrnb,
108                  t_forcerec*                 fr,
109                  gmx_wallcycle*              wcycle,
110                  MdrunScheduleWorkload*      runScheduleWork,
111                  VirtualSitesHandler*        vsite,
112                  ImdSession*                 imdSession,
113                  pull_t*                     pull_work,
114                  Constraints*                constr,
115                  const gmx_mtop_t&           globalTopology,
116                  gmx_enfrot*                 enforcedRotation);
117
118     /*! \brief Register force calculation for step / time
119      *
120      * \param step                 The step number
121      * \param time                 The time
122      * \param registerRunFunction  Function allowing to register a run function
123      */
124     void scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction) override;
125
126     //! Check that we got the local topology
127     void elementSetup() override;
128     //! Print some final output
129     void elementTeardown() override;
130
131     /*! \brief Factory method implementation
132      *
133      * \param legacySimulatorData  Pointer allowing access to simulator level data
134      * \param builderHelper  ModularSimulatorAlgorithmBuilder helper object
135      * \param statePropagatorData  Pointer to the \c StatePropagatorData object
136      * \param energyData  Pointer to the \c EnergyData object
137      * \param freeEnergyPerturbationData  Pointer to the \c FreeEnergyPerturbationData object
138      * \param globalCommunicationHelper   Pointer to the \c GlobalCommunicationHelper object
139      * \param observablesReducer          Pointer to the \c ObservablesReducer object
140      *
141      * \return  Pointer to the element to be added. Element needs to have been stored using \c storeElement
142      */
143     static ISimulatorElement* getElementPointerImpl(LegacySimulatorData* legacySimulatorData,
144                                                     ModularSimulatorAlgorithmBuilderHelper* builderHelper,
145                                                     StatePropagatorData*        statePropagatorData,
146                                                     EnergyData*                 energyData,
147                                                     FreeEnergyPerturbationData* freeEnergyPerturbationData,
148                                                     GlobalCommunicationHelper* globalCommunicationHelper,
149                                                     ObservablesReducer*        observablesReducer);
150
151     //! Callback on domain decomposition repartitioning
152     DomDecCallback registerDomDecCallback() override;
153
154 private:
155     //! ITopologyHolderClient implementation
156     void setTopology(const gmx_localtop_t* top) override;
157     //! INeighborSearchSignallerClient implementation
158     std::optional<SignallerCallback> registerNSCallback() override;
159     //! IEnergySignallerClient implementation
160     std::optional<SignallerCallback> registerEnergyCallback(EnergySignallerEvent event) override;
161     //! The actual do_force call
162     template<bool doShellFC>
163     void run(Step step, Time time, unsigned int flags);
164
165     //! The shell / FC helper struct
166     gmx_shellfc_t* shellfc_;
167     //! Whether shells or flexible constraints are present
168     const bool doShellFC_;
169
170     //! The next NS step
171     Step nextNSStep_;
172     //! The next energy calculation step
173     Step nextEnergyCalculationStep_;
174     //! The next energy calculation step
175     Step nextVirialCalculationStep_;
176     //! The next free energy calculation step
177     Step nextFreeEnergyCalculationStep_;
178
179     // TODO: Clarify relationship to data objects and find a more robust alternative to raw pointers (#3583)
180     //! Pointer to the micro state
181     StatePropagatorData* statePropagatorData_;
182     //! Pointer to the energy data
183     EnergyData* energyData_;
184     //! Pointer to the free energy perturbation data
185     FreeEnergyPerturbationData* freeEnergyPerturbationData_;
186
187     //! The local topology - updated by Topology via Client system
188     const gmx_localtop_t* localTopology_;
189
190     //! Whether we're having a dynamic box
191     const bool isDynamicBox_;
192     //! Whether we're being verbose
193     const bool isVerbose_;
194     //! The number of shell relaxation steps we did
195     Step nShellRelaxationSteps_;
196
197     //! DD / DLB helper object
198     const DDBalanceRegionHandler ddBalanceRegionHandler_;
199     //! Long range force calculator
200     std::unique_ptr<CpuPpLongRangeNonbondeds> longRangeNonbondeds_;
201
202     /* \brief The FEP lambda vector
203      *
204      * Used if FEP is off, since do_force
205      * requires lambda to be allocated anyway
206      */
207     gmx::EnumerationArray<FreeEnergyPerturbationType, real> lambda_;
208
209     // Access to ISimulator data
210     //! Handles logging.
211     FILE* fplog_;
212     //! Handles communication.
213     const t_commrec* cr_;
214     //! Contains user input mdp options.
215     const t_inputrec* inputrec_;
216     //! Atom parameters for this domain.
217     const MDAtoms* mdAtoms_;
218     //! Manages flop accounting.
219     t_nrnb* nrnb_;
220     //! Manages wall cycle accounting.
221     gmx_wallcycle* wcycle_;
222     //! Parameters for force calculations.
223     t_forcerec* fr_;
224     //! Handles virtual sites.
225     VirtualSitesHandler* vsite_;
226     //! The Interactive Molecular Dynamics session.
227     ImdSession* imdSession_;
228     //! The pull work object.
229     pull_t* pull_work_;
230     //! Schedule of work for each MD step for this task.
231     MdrunScheduleWorkload* runScheduleWork_;
232     //! Handles constraints.
233     Constraints* constr_;
234     //! Handles enforced rotation.
235     gmx_enfrot* enforcedRotation_;
236 };
237
238 } // namespace gmx
239
240 #endif // GMX_MODULARSIMULATOR_FORCEELEMENT_H