Remove PrivateImplPointer in favour of std::unique_ptr
[alexxy/gromacs.git] / src / gromacs / imd / imd.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2014,2015,2016,2017,2018 by the GROMACS development team.
5  * Copyright (c) 2019,2020,2021, by the GROMACS development team, led by
6  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7  * and including many others, as listed in the AUTHORS file in the
8  * top-level source directory and at http://www.gromacs.org.
9  *
10  * GROMACS is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  *
15  * GROMACS is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with GROMACS; if not, see
22  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24  *
25  * If you want to redistribute modifications to GROMACS, please
26  * consider that scientific software is very special. Version
27  * control is crucial - bugs must be traceable. We will be happy to
28  * consider code for inclusion in the official distribution, but
29  * derived work must not be called official GROMACS. Details are found
30  * in the README & COPYING files - if they are missing, get the
31  * official version at http://www.gromacs.org.
32  *
33  * To help us fund GROMACS development, we humbly ask that you cite
34  * the research papers on the package. Check out http://www.gromacs.org.
35  */
36
37 /*! \libinternal
38  * \defgroup module_imd Interactive molecular dynamics (IMD)
39  * \ingroup group_mdrun
40  *
41  * \brief
42  * Allows mdrun to interface with VMD via the interactive molecular dynamics
43  * (IMD) protocol.
44  *
45  * \author Martin Hoefling, Carsten Kutzner <ckutzne@gwdg.de>
46  *
47  * \todo Rename the directory, source and test files to
48  * interactive_md, and prefer type names like
49  * InteractiveMDSession. Avoid ambiguity with IMDModule.
50  */
51
52 /*! \libinternal \file
53  *
54  * \brief This file declares the class that coordinates with VMD via
55  * the Interactive Molecular Dynamics protocol, along with supporting
56  * free functions.
57  *
58  * \author Martin Hoefling, Carsten Kutzner <ckutzne@gwdg.de>
59  *
60  * \inlibraryapi
61  * \ingroup module_imd
62  */
63
64 #ifndef GMX_IMD_IMD_H
65 #define GMX_IMD_IMD_H
66
67 #include <memory>
68
69 #include "gromacs/math/vectypes.h"
70 #include "gromacs/utility/basedefinitions.h"
71
72 struct gmx_domdec_t;
73 struct gmx_enerdata_t;
74 struct gmx_mtop_t;
75 struct gmx_multisim_t;
76 struct gmx_output_env_t;
77 struct gmx_wallcycle;
78 struct t_commrec;
79 struct t_filenm;
80 struct t_IMD;
81 struct t_inputrec;
82 class t_state;
83
84 namespace gmx
85 {
86 enum class StartingBehavior;
87 class IMDModule;
88 class ImdSession;
89 class InteractiveMolecularDynamics;
90 class MDLogger;
91 struct ImdOptions;
92 struct MdrunOptions;
93
94 /*! \brief
95  * Creates a module for interactive molecular dynamics.
96  */
97 std::unique_ptr<IMDModule> createInteractiveMolecularDynamicsModule();
98
99 static const char IMDstr[] = "IMD:"; /**< Tag output from the IMD module with this string. */
100
101 /*! \brief Writes out the group of atoms selected for interactive manipulation.
102  *
103  * Called by grompp.
104  * The resulting file has to be read in by VMD if one wants it to connect to mdrun.
105  *
106  * \param bIMD    Only springs into action if bIMD is TRUE. Otherwise returns directly.
107  * \param ir      Structure containing MD input parameters, among those
108  *                the IMD data structure.
109  * \param state   The current state of the MD system.
110  * \param sys     The global, complete system topology.
111  * \param nfile   Number of files.
112  * \param fnm     Filename struct.
113  */
114 void write_IMDgroup_to_file(bool              bIMD,
115                             t_inputrec*       ir,
116                             const t_state*    state,
117                             const gmx_mtop_t* sys,
118                             int               nfile,
119                             const t_filenm    fnm[]);
120
121
122 /*! \brief Makes and returns an initialized IMD session, which may be inactive.
123  *
124  * This function is called before the main MD loop over time steps.
125  *
126  * \param ir           The inputrec structure containing the MD input parameters
127  * \param cr           Information structure for MPI communication.
128  * \param wcycle       Count wallcycles of IMD routines for diagnostic output.
129  * \param enerd        Contains the GROMACS energies for the different interaction types.
130  * \param ms           Handler for multi-simulations.
131  * \param top_global   The topology of the whole system.
132  * \param mdlog        Logger
133  * \param x            The starting positions of the atoms.
134  * \param nfile        Number of files.
135  * \param fnm          Struct containing file names etc.
136  * \param oenv         Output options.
137  * \param options      Options for interactive MD.
138  * \param startingBehavior  Describes whether this is a restart appending to output files
139  */
140 std::unique_ptr<ImdSession> makeImdSession(const t_inputrec*       ir,
141                                            const t_commrec*        cr,
142                                            gmx_wallcycle*          wcycle,
143                                            gmx_enerdata_t*         enerd,
144                                            const gmx_multisim_t*   ms,
145                                            const gmx_mtop_t*       top_global,
146                                            const MDLogger&         mdlog,
147                                            const rvec              x[],
148                                            int                     nfile,
149                                            const t_filenm          fnm[],
150                                            const gmx_output_env_t* oenv,
151                                            const ImdOptions&       options,
152                                            StartingBehavior        startingBehavior);
153
154 class ImdSession
155 {
156 private:
157     //! Private constructor, to force the use of makeImdSession()
158     ImdSession(const MDLogger& mdlog);
159
160 public:
161     ~ImdSession();
162
163     /*! \brief Make a selection of the home atoms for the IMD group.
164      *
165      * Should be called at every domain decomposition.
166      * Each node checks which of the atoms from "ind" are local and puts its local
167      * atom numbers into the "ind_local" array. Furthermore, in "xa_ind" it is
168      * stored at which position each local atom belongs in the assembled/collective
169      * array, so that on the master node all positions can be merged into the
170      * assembled array correctly.
171      *
172      * \param dd          Structure containing domain decomposition data.
173      */
174     void dd_make_local_IMD_atoms(const gmx_domdec_t* dd);
175
176     /*! \brief Prepare to do IMD if required in this time step
177      * Also checks for new IMD connection and syncs the nodes.
178      *
179      * \param step         The time step.
180      * \param bNS          Is this a neighbor searching step?
181      * \param box          The simulation box.
182      * \param x            The local atomic positions on this node.
183      * \param t            The time.
184      *
185      * \returns            Whether or not we have to do IMD communication at this step.
186      */
187     bool run(int64_t step, bool bNS, const matrix box, const rvec x[], double t);
188
189     /*! \brief Add external forces from a running interactive molecular dynamics session.
190      *
191      * \param f            The forces.
192      */
193     void applyForces(rvec* f);
194
195     /*! \brief Copy energies and convert to float from enerdata to the IMD energy record.
196      *
197      * We do no conversion, so units in client are SI!
198      *
199      * \param step             The time step.
200      * \param bHaveNewEnergies Only copy energies if we have done global summing of them before.
201      */
202     void fillEnergyRecord(int64_t step, bool bHaveNewEnergies);
203
204     /*! \brief Send positions and energies to the client. */
205     void sendPositionsAndEnergies();
206
207     /*! \brief Updates the energy record sent to VMD if needed, and sends positions and energies.
208      *
209      * \param bIMDstep         If true, transfer the positions. Otherwise just update the time step and potentially the energy record.
210      * \param step             The time step.
211      * \param bHaveNewEnergies Update the energy record if we have done global summing of the energies.
212      */
213     void updateEnergyRecordAndSendPositionsAndEnergies(bool bIMDstep, int64_t step, bool bHaveNewEnergies);
214
215 private:
216     //! Implementation type.
217     class Impl;
218     //! Implementation object.
219     std::unique_ptr<Impl> impl_;
220
221 public:
222     // Befriend the factory function.
223     friend std::unique_ptr<ImdSession> makeImdSession(const t_inputrec*       ir,
224                                                       const t_commrec*        cr,
225                                                       gmx_wallcycle*          wcycle,
226                                                       gmx_enerdata_t*         enerd,
227                                                       const gmx_multisim_t*   ms,
228                                                       const gmx_mtop_t*       top_global,
229                                                       const MDLogger&         mdlog,
230                                                       const rvec              x[],
231                                                       int                     nfile,
232                                                       const t_filenm          fnm[],
233                                                       const gmx_output_env_t* oenv,
234                                                       const ImdOptions&       options,
235                                                       StartingBehavior        startingBehavior);
236 };
237
238 } // namespace gmx
239
240 #endif