LOOS  v2.3.2
dcdwriter.hpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2008, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 
23 
24 #if !defined(LOOS_DCDWRITER_HPP)
25 #define LOOS_DCDWRITER_HPP
26 
27 #include <ios>
28 #include <iostream>
29 #include <fstream>
30 #include <string>
31 #include <stdexcept>
32 #include <exception>
33 #include <vector>
34 
35 //#include <sys/types.h>
36 //#include <sys/stat.h>
37 //#include <unistd.h>
38 
39 #include <loos_defs.hpp>
40 
41 #include <AtomicGroup.hpp>
42 #include <dcd.hpp>
43 #include <trajwriter.hpp>
44 
45 namespace loos {
46 
48  class DCDWriter : public TrajectoryWriter {
49 
50  // Use a union to convert data to appropriate type...
51  typedef union { unsigned int ui; int i; char c[4]; float f; } DataOverlay;
52 
53  public:
54 
55  static pTrajectoryWriter create(const std::string& s, const bool append = false) {
56  return(pTrajectoryWriter(new DCDWriter(s, append)));
57  }
58 
60 
70  explicit DCDWriter(const std::string& s, const bool append = false) :
71  TrajectoryWriter(s, append),
72  _natoms(0), _nsteps(0),
73  _timestep(0.001), _current(0),
74  _has_box(false),
75  _header_written(false)
76  {
77  if (appending_)
78  prepareToAppend();
79  else
80  _titles.push_back("AUTO GENERATED BY LOOS");
81  }
82 
84  explicit DCDWriter(std::iostream& fs, const bool append = false) :
85  TrajectoryWriter(&fs, append),
86  _natoms(0), _nsteps(0), _timestep(0.001), _current(0),
87  _has_box(false), _header_written(false)
88  {
89  if (appending_)
90  prepareToAppend();
91  else
92  _titles.push_back("AUTO GENERATED BY LOOS");
93  }
94 
96  DCDWriter(const std::string& s, const std::vector<AtomicGroup>& grps, const bool append = false) :
97  TrajectoryWriter(s, append),
98  _natoms(grps[0].size()),
99  _nsteps(grps.size()),
100  _timestep(1e-3),
101  _current(0),
102  _has_box(grps[0].isPeriodic()),
103  _header_written(false)
104  {
105  if (appending_)
106  prepareToAppend();
107  else
108  _titles.push_back("AUTO GENERATED BY LOOS");
109 
110  writeHeader();
111  writeFrames(grps);
112  }
113 
115  DCDWriter(const std::string& s, const std::vector<AtomicGroup>& grps, const std::string& comment, const bool append = false) :
116  TrajectoryWriter(s, append),
117  _natoms(grps[0].size()),
118  _nsteps(grps.size()),
119  _timestep(1e-3),
120  _current(0),
121  _has_box(grps[0].isPeriodic()),
122  _header_written(false)
123  {
124  if (appending_)
125  prepareToAppend();
126  else
127  _titles.push_back(comment);
128 
129  writeHeader();
130  writeFrames(grps);
131  }
132 
134  DCDWriter(const std::string& s, const std::vector<AtomicGroup>& grps, const std::vector<std::string>& comments, const bool append = false) :
135  TrajectoryWriter(s, append),
136  _natoms(grps[0].size()),
137  _nsteps(grps.size()),
138  _timestep(1e-3),
139  _current(0),
140  _has_box(grps[0].isPeriodic()),
141  _header_written(false)
142  {
143  _titles = comments;
144 
145  writeHeader();
146  writeFrames(grps);
147  }
148 
149  ~DCDWriter() {
150  }
151 
152 
154 
162  void setHeader(const int na, const int ns, const greal ts, const bool bf) {
163  if (_header_written)
164  throw(std::logic_error("Cannot set header after having written it"));
165 
166  _natoms = na;
167  _nsteps = ns;
168  _timestep = ts;
169  _has_box = bf;
170  }
171 
172  void setTitles(const std::vector<std::string>& titles) {
173  if (_header_written)
174  throw(std::logic_error("Cannot set header after having written it"));
175  _titles = titles;
176  }
177 
178  void setTitle(const std::string& s) {
179  if (_header_written)
180  throw(std::logic_error("Cannot set header after having written it"));
181  _titles.clear(); addTitle(s);
182  }
183 
184  void addTitle(const std::string& s) {
185  if (_header_written)
186  throw(std::logic_error("Cannot set header after having written it"));
187  _titles.push_back(s);
188  }
189 
190  void setComments(const std::vector<std::string>& comments) { setTitles(comments); }
191  bool hasComments() const { return(true); }
192 
194 
201  void writeFrame(const AtomicGroup& grp);
202 
203 
205  void writeFrames(const std::vector<AtomicGroup>& grps);
206 
207  void writeHeader(void);
208 
209  uint framesWritten(void) const { return(_current); }
210 
211  private:
212  void writeF77Line(const char* const data, const unsigned int len);
213  std::string fixStringSize(const std::string& s, const unsigned int size);
214  void writeBox(const GCoord& box);
215 
216  void prepareToAppend();
217 
218 
219  private:
220  uint _natoms, _nsteps;
221  greal _timestep;
222  uint _current;
223  bool _has_box;
224  bool _header_written;
225  std::vector<std::string> _titles;
226  };
227 
228 }
229 
230 
231 #endif
Basic 3-D coordinates class.
Definition: Coord.hpp:36
bool hasComments() const
Does format support comments in metadata?
Definition: dcdwriter.hpp:191
DCDWriter(const std::string &s, const std::vector< AtomicGroup > &grps, const bool append=false)
Writes the coordinates of grps to file s as a DCD.
Definition: dcdwriter.hpp:96
void writeFrame(const AtomicGroup &grp)
Writes a frame to a growing DCD.
Definition: dcdwriter.cpp:109
DCDWriter(const std::string &s, const bool append=false)
Setup for writing to a file named by s.
Definition: dcdwriter.hpp:70
void writeFrames(const std::vector< AtomicGroup > &grps)
Same as writeFrame(), but writes out the vector of frames...
Definition: dcdwriter.cpp:157
void setComments(const std::vector< std::string > &comments)
Set comments in metadata (not all formats support)
Definition: dcdwriter.hpp:190
A very lightweight class for writing simple DCDs.
Definition: dcdwriter.hpp:48
uint framesWritten(void) const
Total frames in output file.
Definition: dcdwriter.hpp:209
Class for handling groups of Atoms (pAtoms, actually)
Definition: AtomicGroup.hpp:87
Namespace for most things not already encapsulated within a class.
DCDWriter(const std::string &s, const std::vector< AtomicGroup > &grps, const std::string &comment, const bool append=false)
Writes coordinates of grps adding comment as a TITLE record.
Definition: dcdwriter.hpp:115
void setHeader(const int na, const int ns, const greal ts, const bool bf)
Sets header parameters.
Definition: dcdwriter.hpp:162
DCDWriter(std::iostream &fs, const bool append=false)
Setup for writing to a stream.
Definition: dcdwriter.hpp:84
Base class for writing trajectories.
Definition: trajwriter.hpp:53
DCDWriter(const std::string &s, const std::vector< AtomicGroup > &grps, const std::vector< std::string > &comments, const bool append=false)
Writes coordinates of grps adding comments as TITLE records.
Definition: dcdwriter.hpp:134