Merge branch 'release-2019' into master
[alexxy/gromacs.git] / src / gromacs / mdlib / energyoutput.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019, 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 #ifndef GMX_MDLIB_ENERGYOUTPUT_H
36 #define GMX_MDLIB_ENERGYOUTPUT_H
37
38 #include <cstdio>
39
40 #include "gromacs/mdtypes/enerdata.h"
41
42 class energyhistory_t;
43 struct ener_file;
44 struct gmx_ekindata_t;
45 struct gmx_enerdata_t;
46 struct gmx_groups_t;
47 struct gmx_mtop_t;
48 struct gmx_output_env_t;
49 struct t_ebin;
50 struct t_expanded;
51 struct t_fcdata;
52 struct t_grpopts;
53 struct t_inputrec;
54 struct t_lambda;
55 class t_state;
56
57 namespace gmx
58 {
59 class Awh;
60 class Constraints;
61 }
62
63 extern const char *egrp_nm[egNR+1];
64
65 /* delta_h block type enum: the kinds of energies written out. */
66 enum
67 {
68     dhbtDH   = 0, /* delta H BAR energy difference*/
69     dhbtDHDL = 1, /* dH/dlambda derivative */
70     dhbtEN,       /* System energy */
71     dhbtPV,       /* pV term */
72     dhbtEXPANDED, /* expanded ensemble statistics */
73     dhbtNR
74 };
75
76 namespace gmx
77 {
78
79 // TODO remove use of detail namespace when removing t_mdebin in
80 // favour of an Impl class.
81 namespace detail
82 {
83 struct t_mdebin;
84 }
85
86 /* The functions & data structures here determine the content for outputting
87    the .edr file; the file format and actual writing is done with functions
88    defined in enxio.h */
89
90 class EnergyOutput
91 {
92     public:
93         EnergyOutput();
94         /*! \brief Initiate MD energy bin
95          *
96          * This second phase of construction is needed until we have
97          * modules that understand how to request output from
98          * EnergyOutput.
99          *
100          * \todo Refactor to separate a function to write the energy
101          * file header. Perhaps transform the remainder into a factory
102          * function.
103          */
104         void prepare(ener_file        *fp_ene,
105                      const gmx_mtop_t *mtop,
106                      const t_inputrec *ir,
107                      FILE             *fp_dhdl,
108                      bool              isRerun = false);
109         ~EnergyOutput();
110         /*! \brief Update the averaging structures.
111          *
112          * Called every step on which the energies are evaluated. */
113         void addDataAtEnergyStep(bool                    bDoDHDL,
114                                  bool                    bSum,
115                                  double                  time,
116                                  real                    tmass,
117                                  gmx_enerdata_t         *enerd,
118                                  t_state                *state,
119                                  t_lambda               *fep,
120                                  t_expanded             *expand,
121                                  matrix                  lastbox,
122                                  tensor                  svir,
123                                  tensor                  fvir,
124                                  tensor                  vir,
125                                  tensor                  pres,
126                                  gmx_ekindata_t         *ekind,
127                                  rvec                    mu_tot,
128                                  const gmx::Constraints *constr);
129         /*! \brief Updated the averaging structures
130          *
131          * Called every step on which the energies are not evaluated.
132          *
133          * \todo This schedule is known in advance, and should be made
134          * an intrinsic behaviour of EnergyOutput, rather than being
135          * wastefully called every step. */
136         void recordNonEnergyStep();
137
138         /*! \brief Help write quantites to the energy file
139          *
140          * \todo Perhaps this responsibility should involve some other
141          * object visiting all the contributing objects. */
142         void printStepToEnergyFile(ener_file *fp_ene, bool bEne, bool bDR, bool bOR,
143                                    FILE *log,
144                                    int64_t step, double time,
145                                    int mode,
146                                    t_fcdata *fcd,
147                                    gmx_groups_t *groups, t_grpopts *opts,
148                                    gmx::Awh *awh);
149         /*! \brief Get the number of energy terms recorded.
150          *
151          * \todo Refactor this to return the expected output size,
152          * rather than exposing the implementation details about
153          * energy terms. */
154         int numEnergyTerms() const;
155         /*! \brief Getter used for testing t_ebin
156          *
157          * \todo Find a better approach for this. */
158         t_ebin *getEbin();
159
160         /* Between .edr writes, the averages are history dependent,
161            and that history needs to be retained in checkpoints.
162            These functions set/read the energyhistory_t class
163            that is written to checkpoints in checkpoint.c */
164
165         //! Fill the energyhistory_t data.
166         void fillEnergyHistory(energyhistory_t * enerhist) const;
167         //! Restore from energyhistory_t data.
168         void restoreFromEnergyHistory(const energyhistory_t &enerhist);
169
170     private:
171         // TODO transform this into an impl class.
172         detail::t_mdebin *mdebin = nullptr;
173 };
174
175 } // namespace gmx
176
177 //! Open the dhdl file for output
178 FILE *open_dhdl(const char *filename, const t_inputrec *ir,
179                 const gmx_output_env_t *oenv);
180
181 namespace gmx
182 {
183
184 //! Print an energy-output header to the log file
185 void print_ebin_header(FILE *log, int64_t steps, double time);
186
187 } // namespace gmx
188
189 #endif