86dcc9b4ab542d61c078fdc63921373c036783a5
[alexxy/gromacs.git] / src / gromacs / coordinateio / coordinatefile.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019,2020, 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 /*! \file
36  * \brief
37  * CoordinateFile takes care of opening files and writing output to them.
38  *
39  * \author
40  * \inpublicapi
41  * \ingroup module_coordinateio
42  */
43 #ifndef GMX_COORDINATEIO_COORDINATEFILE_H
44 #define GMX_COORDINATEIO_COORDINATEFILE_H
45
46 #include <string>
47 #include <utility>
48
49 #include "gromacs/coordinateio/ioutputadapter.h"
50 #include "gromacs/coordinateio/outputadaptercontainer.h"
51 #include "gromacs/math/vectypes.h"
52 #include "gromacs/topology/atoms.h"
53
54 struct gmx_mtop_t;
55 struct t_trxstatus;
56
57 namespace gmx
58 {
59
60 class Selection;
61 class TrajectoryFrameWriter;
62 struct OutputRequirements;
63
64 /*! \brief
65  * Factory function for TrajectoryFrameWriter.
66  *
67  * Used to initialize a new instance of TrajectoryFrameWriter with the user supplied information
68  * for writing trajectory data to disk. Information needed is the file type, file name
69  * corresponding to the type, if available topology information and selection information.
70  *
71  * If supplied, the modules contained within \p adapters are registered on
72  * the TrajectoryFrameWriter if possible.
73  *
74  * The factory function is responsible for the initial santity checks concerning file types and
75  * availability of topology information, with the registration of modules being the second part.
76  *
77  * \param[in] top                    Pointer to full topology or null.
78  * \param[in] sel                    Reference to global selection used to construct the object.
79  * \param[in] filename               Name of new output file, used to deduce file type.
80  * \param[in] atoms                  Smart Pointer to atoms data or null.
81  * \param[in] requirements           Container for settings obtained to specify which
82  *                                   OutputAdapters should be registered.
83  * \throws    InconsistentInputError When user input and requirements don't match.
84  */
85 std::unique_ptr<TrajectoryFrameWriter> createTrajectoryFrameWriter(const gmx_mtop_t*  top,
86                                                                    const Selection&   sel,
87                                                                    const std::string& filename,
88                                                                    AtomsDataPtr       atoms,
89                                                                    OutputRequirements requirements);
90
91 /*!\brief
92  * Low level method to take care of only file opening and closing.
93  *
94  * \inpublicapi
95  * \ingroup module_coordinateio
96  *
97  */
98 class TrajectoryFileOpener
99 {
100 public:
101     /*! \brief
102      * Constructor, taking all arguments needed to open valid coordinate files of any type.
103      *
104      * \param[in] name Name of the file to create.
105      * \param[in] filetype Internal filetype to know which kind we are going to have.
106      * \param[in] sel Reference to selection of atoms to write. Needs to be valid for
107      *                longer than the lifetime of the object created here.
108      * \param[in] mtop Topology used to create TNG output. Needs to be valid for longer
109      *                 than the object created here.
110      */
111     TrajectoryFileOpener(const std::string& name, int filetype, const Selection& sel, const gmx_mtop_t* mtop) :
112         outputFileName_(name),
113         outputFile_(nullptr),
114         filetype_(filetype),
115         sel_(sel),
116         mtop_(mtop)
117     {
118     }
119
120     /*! \brief
121      * Closes new trajectory file after finishing the writing to it.
122      */
123     ~TrajectoryFileOpener();
124
125     /*! \brief
126      * Get access to initialized output file object.
127      *
128      * Performs lazy initialization if needed.
129      */
130     t_trxstatus* outputFile();
131
132 private:
133     //! Name for the new coordinate file.
134     std::string outputFileName_;
135
136     //! File pointer to the coordinate file being written.
137     t_trxstatus* outputFile_;
138
139     //! Storage of file type for determing what kind of file will be written to disk.
140     int filetype_;
141
142     /*! \brief
143      * Selection of atoms to write out.
144      *
145      * Currently, CoordinateFile expects that the lifetime of the selection is longer
146      * than that of itself, and that the selection remains unchanged during this lifetime.
147      * A better approach will be to pass the selection to it and expect it to
148      * manage the lifetime instead.
149      */
150     const Selection& sel_;
151
152     //! Pointer to topology information if available.
153     const gmx_mtop_t* mtop_;
154 };
155
156 /*!\brief
157  * Writes coordinate frames to a sink, e.g. a trajectory file.
158  *
159  * Writes all frames passed to the trajectory file handle it
160  * manages. It can use IOutputAdapter modules to transform the
161  * frame data before writing. If any transform modules are used,
162  * makes a deep copy of the frame contents.
163  *
164  * \inpublicapi
165  * \ingroup module_coordinateio
166  *
167  */
168 class TrajectoryFrameWriter
169 {
170 public:
171     friend std::unique_ptr<TrajectoryFrameWriter> createTrajectoryFrameWriter(const gmx_mtop_t* top,
172                                                                               const Selection&  sel,
173                                                                               const std::string& filename,
174                                                                               AtomsDataPtr atoms,
175                                                                               OutputRequirements requirements);
176
177     /*! \brief
178      * Writes the input frame, after applying any IOutputAdapters.
179      *
180      * \param[in] framenumber Number of frame being currently processed.
181      * \param[in] input View of the constant t_trxframe object provided by the
182      *                  method that calls the output manager.
183      */
184     void prepareAndWriteFrame(int framenumber, const t_trxframe& input);
185
186 private:
187     /*! \brief
188      * Creates fully initialized object.
189      *
190      * \param[in] name Name of the file to create.
191      * \param[in] filetype Internal filetype to know which kind we are going to have.
192      * \param[in] sel Reference to selection of atoms to write. Needs to be valid for
193      *                longer than the lifetime of the object created here.
194      * \param[in] mtop Topology used to create TNG output. Needs to be valid for longer
195      *                 than the object created here.
196      * \param[in] adapters Container of methods that can modify the information written
197      *                     to the new file.
198      */
199     TrajectoryFrameWriter(const std::string&     name,
200                           int                    filetype,
201                           const Selection&       sel,
202                           const gmx_mtop_t*      mtop,
203                           OutputAdapterContainer adapters) :
204         file_(name, filetype, sel, mtop),
205         outputAdapters_(std::move(adapters))
206     {
207     }
208
209     //! Underlying object for open/writing to file.
210     TrajectoryFileOpener file_;
211
212     //! Storage for list of output adapters.
213     OutputAdapterContainer outputAdapters_;
214
215     //! Local storage for modified positions.
216     std::vector<RVec> localX_;
217     //! Local storage for modified velocities.
218     std::vector<RVec> localV_;
219     //! Local storage for modified forces.
220     std::vector<RVec> localF_;
221     //! Local storage for modified indices.
222     std::vector<int> localIndex_;
223 };
224
225 //! Smart pointer to manage the TrajectoryFrameWriter object.
226 using TrajectoryFrameWriterPointer = std::unique_ptr<TrajectoryFrameWriter>;
227
228 } // namespace gmx
229
230 #endif