Update instructions in containers.rst
[gromacs.git] / src / gromacs / coordinateio / coordinatefile.h
blob86dcc9b4ab542d61c078fdc63921373c036783a5
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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 /*! \file
36 * \brief
37 * CoordinateFile takes care of opening files and writing output to them.
39 * \author
40 * \inpublicapi
41 * \ingroup module_coordinateio
43 #ifndef GMX_COORDINATEIO_COORDINATEFILE_H
44 #define GMX_COORDINATEIO_COORDINATEFILE_H
46 #include <string>
47 #include <utility>
49 #include "gromacs/coordinateio/ioutputadapter.h"
50 #include "gromacs/coordinateio/outputadaptercontainer.h"
51 #include "gromacs/math/vectypes.h"
52 #include "gromacs/topology/atoms.h"
54 struct gmx_mtop_t;
55 struct t_trxstatus;
57 namespace gmx
60 class Selection;
61 class TrajectoryFrameWriter;
62 struct OutputRequirements;
64 /*! \brief
65 * Factory function for TrajectoryFrameWriter.
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.
71 * If supplied, the modules contained within \p adapters are registered on
72 * the TrajectoryFrameWriter if possible.
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.
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.
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);
91 /*!\brief
92 * Low level method to take care of only file opening and closing.
94 * \inpublicapi
95 * \ingroup module_coordinateio
98 class TrajectoryFileOpener
100 public:
101 /*! \brief
102 * Constructor, taking all arguments needed to open valid coordinate files of any type.
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.
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)
120 /*! \brief
121 * Closes new trajectory file after finishing the writing to it.
123 ~TrajectoryFileOpener();
125 /*! \brief
126 * Get access to initialized output file object.
128 * Performs lazy initialization if needed.
130 t_trxstatus* outputFile();
132 private:
133 //! Name for the new coordinate file.
134 std::string outputFileName_;
136 //! File pointer to the coordinate file being written.
137 t_trxstatus* outputFile_;
139 //! Storage of file type for determing what kind of file will be written to disk.
140 int filetype_;
142 /*! \brief
143 * Selection of atoms to write out.
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.
150 const Selection& sel_;
152 //! Pointer to topology information if available.
153 const gmx_mtop_t* mtop_;
156 /*!\brief
157 * Writes coordinate frames to a sink, e.g. a trajectory file.
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.
164 * \inpublicapi
165 * \ingroup module_coordinateio
168 class TrajectoryFrameWriter
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);
177 /*! \brief
178 * Writes the input frame, after applying any IOutputAdapters.
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.
184 void prepareAndWriteFrame(int framenumber, const t_trxframe& input);
186 private:
187 /*! \brief
188 * Creates fully initialized object.
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.
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))
209 //! Underlying object for open/writing to file.
210 TrajectoryFileOpener file_;
212 //! Storage for list of output adapters.
213 OutputAdapterContainer outputAdapters_;
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_;
225 //! Smart pointer to manage the TrajectoryFrameWriter object.
226 using TrajectoryFrameWriterPointer = std::unique_ptr<TrajectoryFrameWriter>;
228 } // namespace gmx
230 #endif