d5564ba1b5277000c0f71561b88458cb567c2246
[alexxy/gromacs.git] / src / gromacs / modularsimulator / constraintelement.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 constraint element for the modular simulator
37  *
38  * \author Pascal Merz <pascal.merz@me.com>
39  * \ingroup module_modularsimulator
40  *
41  * This header is only used within the modular simulator module
42  */
43
44 #ifndef GMX_MODULARSIMULATOR_CONSTRAINTELEMENT_H
45 #define GMX_MODULARSIMULATOR_CONSTRAINTELEMENT_H
46
47 #include "gromacs/mdlib/constr.h"
48
49 #include "modularsimulatorinterfaces.h"
50
51 namespace gmx
52 {
53 class Constraints;
54 class EnergyData;
55 class FreeEnergyPerturbationData;
56 class GlobalCommunicationHelper;
57 class LegacySimulatorData;
58 class ModularSimulatorAlgorithmBuilderHelper;
59 class StatePropagatorData;
60
61 /*! \internal
62  * \ingroup module_modularsimulator
63  * \brief Constraints element
64  *
65  * The ConstraintsElement is implemented for the position-and-velocity and the
66  * velocity-only case. It does not change the constraint implementation itself,
67  * but uses the current constraints implementation and the data management
68  * introduced with the modular simulator.
69  *
70  * \tparam variable  The constraining variable
71  */
72 template<ConstraintVariable variable>
73 class ConstraintsElement final :
74     public ISimulatorElement,
75     public IEnergySignallerClient,
76     public ITrajectorySignallerClient,
77     public ILoggingSignallerClient
78 {
79 public:
80     //! Constructor
81     ConstraintsElement(Constraints*                constr,
82                        StatePropagatorData*        statePropagatorData,
83                        EnergyData*                 energyData,
84                        FreeEnergyPerturbationData* freeEnergyPerturbationData,
85                        bool                        isMaster,
86                        FILE*                       fplog,
87                        const t_inputrec*           inputrec,
88                        const t_mdatoms*            mdAtoms);
89
90     /*! \brief Register constraining function for step / time
91      *
92      * Under LF, this is expected to be run once, constraining positions and velocities
93      * Under VV, this is expected to be run twice, once contraining velocities only,
94      * a second time constraining positions and velocities.
95      *
96      * \param step                 The step number
97      * \param time                 The time
98      * \param registerRunFunction  Function allowing to register a run function
99      */
100     void scheduleTask(Step step, Time time, const RegisterRunFunction& registerRunFunction) override;
101
102     /*! \brief Performs inital constraining
103      *  \todo Should this rather happen at grompp time? Right position of this operation is currently
104      *        depending on the integrator algorithm (after domdec, before compute globals...),
105      *        so doing this earlier would be much more stable!
106      */
107     void elementSetup() override;
108     //! No element teardown needed
109     void elementTeardown() override {}
110
111     /*! \brief Factory method implementation
112      *
113      * \param legacySimulatorData  Pointer allowing access to simulator level data
114      * \param builderHelper  ModularSimulatorAlgorithmBuilder helper object
115      * \param statePropagatorData  Pointer to the \c StatePropagatorData object
116      * \param energyData  Pointer to the \c EnergyData object
117      * \param freeEnergyPerturbationData  Pointer to the \c FreeEnergyPerturbationData object
118      * \param globalCommunicationHelper  Pointer to the \c GlobalCommunicationHelper object
119      *
120      * \return  Pointer to the element to be added. Element needs to have been stored using \c storeElement
121      */
122     static ISimulatorElement* getElementPointerImpl(LegacySimulatorData* legacySimulatorData,
123                                                     ModularSimulatorAlgorithmBuilderHelper* builderHelper,
124                                                     StatePropagatorData*        statePropagatorData,
125                                                     EnergyData*                 energyData,
126                                                     FreeEnergyPerturbationData* freeEnergyPerturbationData,
127                                                     GlobalCommunicationHelper* globalCommunicationHelper);
128
129 private:
130     //! The actual constraining computation
131     void apply(Step step, bool calculateVirial, bool writeLog, bool writeEnergy);
132
133     //! IEnergySignallerClient implementation
134     std::optional<SignallerCallback> registerEnergyCallback(EnergySignallerEvent event) override;
135     //! ITrajectorySignallerClient implementation
136     std::optional<SignallerCallback> registerTrajectorySignallerCallback(TrajectoryEvent event) override;
137     //! ILoggingSignallerClient implementation
138     std::optional<SignallerCallback> registerLoggingCallback() override;
139
140     //! The next energy calculation step
141     Step nextVirialCalculationStep_;
142     //! The next energy writing step
143     Step nextEnergyWritingStep_;
144     //! The next logging step
145     Step nextLogWritingStep_;
146
147     //! Whether we're master rank
148     const bool isMasterRank_;
149
150     // TODO: Clarify relationship to data objects and find a more robust alternative to raw pointers (#3583)
151     //! Pointer to the micro state
152     StatePropagatorData* statePropagatorData_;
153     //! Pointer to the energy data
154     EnergyData* energyData_;
155     //! Pointer to the free energy perturbation data
156     FreeEnergyPerturbationData* freeEnergyPerturbationData_;
157
158     // Access to ISimulator data
159     //! Handles constraints.
160     Constraints* constr_;
161     //! Handles logging.
162     FILE* fplog_;
163     //! Contains user input mdp options.
164     const t_inputrec* inputrec_;
165     //! Atom parameters for this domain.
166     const t_mdatoms* mdAtoms_;
167 };
168
169 } // namespace gmx
170
171 #endif // GMX_MODULARSIMULATOR_CONSTRAINTELEMENT_H