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