bd23f0e90d5e48aced9f60156a4f6cd8f258db87
[alexxy/gromacs.git] / src / gromacs / trajectory / energyframe.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2004, The GROMACS development team.
6  * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 #ifndef GMX_TRAJECTORY_ENERGYFRAME_H
38 #define GMX_TRAJECTORY_ENERGYFRAME_H
39
40 #include <map>
41 #include <string>
42
43 #include "gromacs/utility/basedefinitions.h"
44 #include "gromacs/utility/real.h"
45
46 struct t_enxblock;
47
48 struct t_energy
49 {
50     //! The current energy.
51     real   e;
52     //! The running average of the energy
53     double eav;
54     //! The sum of energies until now.
55     double esum;
56 };
57
58 /* The frames that are read/written */
59 struct t_enxframe
60 {
61     double          t;            /* Timestamp of this frame                         */
62     int64_t         step;         /* MD step                                 */
63     int64_t         nsteps;       /* The number of steps between frames            */
64     double          dt;           /* The MD time step                              */
65     int             nsum;         /* The number of terms for the sums in ener      */
66     int             nre;          /* Number of energies                      */
67     int             e_size;       /* Size (in bytes) of energies                     */
68     int             e_alloc;      /* Allocated size (in elements) of ener          */
69     t_energy       *ener;         /* The energies                                  */
70     int             nblock;       /* Number of following energy blocks             */
71     t_enxblock     *block;        /* The blocks                                    */
72     int             nblock_alloc; /* The number of blocks allocated                */
73 };
74
75 namespace gmx
76 {
77
78 /*! \internal
79  * \brief Contains the content of an .edr frame read by an EnergyFrameReader
80  *
81  * The interface of this class is intended to resemble a subset of std::map. */
82 class EnergyFrame
83 {
84     public:
85         //! Convenience type
86         using MapType = std::map<std::string, real>;
87         //! Convenience type
88         using MapConstIterator = MapType::const_iterator;
89         //! Constructor
90         EnergyFrame(const t_enxframe &enxframe,
91                     const std::map<std::string, int> indicesOfEnergyFields);
92         /*! \brief Return string that helps users identify this frame, containing time and step number.
93          *
94          * \throws std::bad_alloc  when out of memory */
95         std::string frameName() const;
96         /*! \brief Return the value read for energy \c name.
97          *
98          * \throws APIError  if \c name was not registered with EnergyFileReader. */
99         const real &at(const std::string &name) const;
100         //! Return const interator to first element of values.
101         MapConstIterator begin() const;
102         //! Return const interator to past the end element of values.
103         MapConstIterator end() const;
104         //! Return a const interator to the element with \c key, or end() if not found.
105         MapConstIterator find(const std::string &key) const;
106     private:
107         //! Container for energy values, indexed by name
108         MapType      values_;
109         //! Step number read from the .edr file frame
110         std::int64_t step_;
111         //! Time read from the .edr file frame
112         double       time_;
113 };
114
115 } // namespace
116
117 #endif