2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
35 /*! \libinternal \file
36 * \brief Header for the code that writes energy-like quantities.
38 * \author Mark Abraham <mark.j.abraham@gmail.com>
39 * \author Paul Bauer <paul.bauer.q@gmail.com>
40 * \author Artem Zhmurov <zhmurov@gmail.com>
42 * \ingroup module_mdlib
45 #ifndef GMX_MDLIB_ENERGYOUTPUT_H
46 #define GMX_MDLIB_ENERGYOUTPUT_H
50 #include "gromacs/mdtypes/enerdata.h"
52 class energyhistory_t;
54 struct gmx_ekindata_t;
55 struct gmx_enerdata_t;
56 struct SimulationGroups;
58 struct gmx_output_env_t;
68 struct t_mde_delta_h_coll;
74 struct MdModulesNotifier;
75 enum class StartingBehavior;
78 //! \brief Printed names for intergroup energies
79 extern const char* egrp_nm[egNR + 1];
81 /* \brief delta_h block type enum: the kinds of energies written out. */
84 //! Delta H BAR energy difference
86 //! dH/dlambda derivative
92 //! Expanded ensemble statistics
94 //! Total number of energy types in this enum
101 /* Energy output class
103 * This is the collection of energy averages collected during mdrun, and to
104 * be written out to the .edr file.
106 * \todo Use more std containers.
107 * \todo Remove GMX_CONSTRAINTVIR
108 * \todo Write free-energy output also to energy file (after adding more tests)
113 /*! \brief Initiate MD energy bin
115 * \param[in] fp_ene Energy output file.
116 * \param[in] mtop Topology.
117 * \param[in] ir Input parameters.
118 * \param[in] pull_work Pulling simulations data
119 * \param[in] fp_dhdl FEP file.
120 * \param[in] isRerun Is this is a rerun instead of the simulations.
121 * \param[in] startingBehavior Run starting behavior.
122 * \param[in] mdModulesNotifier Notifications to MD modules.
124 EnergyOutput(ener_file* fp_ene,
125 const gmx_mtop_t* mtop,
126 const t_inputrec* ir,
127 const pull_t* pull_work,
130 StartingBehavior startingBehavior,
131 const MdModulesNotifier& mdModulesNotifier);
135 /*! \brief Update the averaging structures.
137 * Called every step on which the thermodynamic values are evaluated.
139 * \param[in] bDoDHDL Whether the FEP is enabled.
140 * \param[in] bSum If this stepshould be recorded to compute sums and averaes.
141 * \param[in] time Current simulation time.
142 * \param[in] tmass Total mass
143 * \param[in] enerd Energy data object.
144 * \param[in] state System state.
145 * \param[in] fep FEP data.
146 * \param[in] expand Expanded ensemble (for FEP).
147 * \param[in] lastbox PBC data.
148 * \param[in] svir Constraint virial.
149 * \param[in] fvir Force virial.
150 * \param[in] vir Total virial.
151 * \param[in] pres Pressure.
152 * \param[in] ekind Kinetic energy data.
153 * \param[in] mu_tot Total dipole.
154 * \param[in] constr Constraints object to get RMSD from (for LINCS only).
156 void addDataAtEnergyStep(bool bDoDHDL,
160 const gmx_enerdata_t* enerd,
161 const t_state* state,
163 const t_expanded* expand,
164 const matrix lastbox,
169 const gmx_ekindata_t* ekind,
171 const gmx::Constraints* constr);
173 /*! \brief Update the data averaging structure counts.
175 * Updates the number of steps, the values have not being computed.
177 void recordNonEnergyStep();
179 /*! \brief Writes current quantites to log and energy files.
181 * Prints current values of energies, pressure, temperature, restraint
182 * data, etc. to energy output file and to the log file (if not nullptr).
184 * This function only does something useful when bEne || bDR || bOR || log.
186 * \todo Perhaps this responsibility should involve some other
187 * object visiting all the contributing objects.
189 * \param[in] fp_ene Energy file for the output.
190 * \param[in] bEne If it is a step for energy output or last step.
191 * \param[in] bDR If it is a step of writing distance restraints.
192 * \param[in] bOR If it is a step of writing orientation restraints.
193 * \param[in] log Pointer to the log file.
194 * \param[in] step Current step.
195 * \param[in] time Current simulation time.
196 * \param[in] fcd Bonded force computation data,
197 * including orientation and distance restraints.
198 * \param[in] awh AWH data.
200 void printStepToEnergyFile(ener_file* fp_ene,
210 /*! \brief Print reference temperatures for annealing groups.
212 * Nothing is done if log is nullptr.
214 * \param[in] log Log file to print to.
215 * \param[in] groups Information on atom groups.
216 * \param[in] opts Atom temperature coupling groups options
217 * (annealing is done by groups).
219 void printAnnealingTemperatures(FILE* log, SimulationGroups* groups, t_grpopts* opts);
221 /*! \brief Prints average values to log file.
223 * This is called at the end of the simulation run to print accumulated average values.
224 * Nothing it done if log is nullptr.
226 * \param[in] log Where to print.
227 * \param[in] groups Atom groups.
229 void printAverages(FILE* log, const SimulationGroups* groups);
231 /*! \brief Get the number of thermodynamic terms recorded.
233 * \todo Refactor this to return the expected output size,
234 * rather than exposing the implementation details about
235 * thermodynamic terms.
237 * \returns Number of thermodynamic terms.
239 int numEnergyTerms() const;
241 /*! \brief Fill the energyhistory_t data.
243 * Between .edr writes, the averages are history dependent,
244 * and that history needs to be retained in checkpoints.
245 * These functions set/read the energyhistory_t class
246 * that is written to checkpoints.
248 * \param[out] enerhist Energy history data structure.
250 void fillEnergyHistory(energyhistory_t* enerhist) const;
252 /*! \brief Restore from energyhistory_t data.
254 * \param[in] enerhist Energy history data structure.
256 void restoreFromEnergyHistory(const energyhistory_t& enerhist);
258 //! Print an output header to the log file.
259 void printHeader(FILE* log, int64_t steps, double time);
265 //! Structure to store energy components and their running averages
266 t_ebin* ebin_ = nullptr;
268 //! Is the periodic box triclinic
269 bool bTricl_ = false;
270 //! NHC trotter is used
271 bool bNHC_trotter_ = false;
272 //! If Nose-Hoover chains should be printed
273 bool bPrintNHChains_ = false;
274 //! If MTTK integrator was used
277 //! Temperature control scheme
280 //! Which of the main energy terms should be printed
281 bool bEner_[F_NRE] = { false };
282 //! Index for main energy terms
284 //! Number of energy terms from F_NRE list to be saved (i.e. number of 'true' in bEner)
287 //! Index for constraints RMSD
289 /* !\brief How many constraints RMSD terms there are.
290 * Usually 1 if LINCS is used and 0 otherwise)
291 * nCrmsd > 0 indicates when constraints RMSD is saves, hence no boolean
295 //! Is the periodic box dynamic
296 bool bDynBox_ = false;
297 //! Index for box dimensions
299 //! Index for box volume
301 //! Index for density
303 //! Triclinic box and not a rerun
304 bool bDiagPres_ = false;
305 //! Reference pressure, averaged over dimensions
307 //! Index for thermodynamic work (pV)
309 //! Index for entalpy (pV + total energy)
312 /*! \brief If the constraints virial should be printed.
313 * Can only be true if "GMX_CONSTRAINTVIR" environmental variable is set */
314 bool bConstrVir_ = false;
315 //! Index for constrains virial
317 //! Index for force virial
320 //! If we have pressure computed
322 //! Index for total virial
324 //! Index for pressure
326 /*! \brief Index for surface tension
327 * [(pres[ZZ][ZZ]-(pres[XX][XX]+pres[YY][YY])*0.5)*box[ZZ][ZZ]]*/
330 //! Pressure control scheme
332 //! Index for velocity of the box borders
335 //! If dipole was calculated
337 //! Index for the dipole
340 //! Index for coseine acceleration used for viscocity calculation
342 //! Index for viscocity
345 //! Which energy terms from egNR list should be printed in group-to-group block
346 bool bEInd_[egNR] = { false };
347 //! Number of energy terms to be printed (these, for which bEInd[] == true)
349 //! Number of energy output groups
351 //! Number of intergroup energy sets to be printed for each energy term (nE = (nEg*(nEg+1))/2)
353 //! Indexes for integroup energy sets (each set with nEc energies)
354 int* igrp_ = nullptr;
356 //! Number of temperature coupling groups
358 //! Index for temperature
361 //! Number of Nose-Hoover chains
363 //! Number of temperature coupling coefficients in case of NH Chains
365 //! Index for temperature coupling coefficient in case of NH chains
368 //! Number of temperature coupling terms if the temperature control is dealt by barostat (MTTK case)
370 //! Scalling factors for temperaturs control in MTTK
372 //! Index for scalling factor of MTTK
375 //! Number of acceleration groups
377 //! Index for group velocities
380 //! Array to accumulate values during update
381 real* tmp_r_ = nullptr;
382 //! Array to accumulate values during update
383 rvec* tmp_v_ = nullptr;
385 //! The dhdl.xvg output file
386 FILE* fp_dhdl_ = nullptr;
387 //! Energy components for dhdl.xvg output
388 double* dE_ = nullptr;
389 //! The delta U components (raw data + histogram)
390 t_mde_delta_h_coll* dhc_ = nullptr;
391 //! Temperatures for simulated tempering groups
392 real* temperatures_ = nullptr;
393 //! Number of temperatures actually saved
394 int numTemperatures_ = 0;
399 //! Open the dhdl file for output
400 FILE* open_dhdl(const char* filename, const t_inputrec* ir, const gmx_output_env_t* oenv);