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