LOOS  v2.3.2
dcd.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_DCD_HPP)
25 #define LOOS_DCD_HPP
26 
27 
28 #include <iostream>
29 #include <string>
30 #include <stdexcept>
31 #include <exception>
32 
33 #include <loos_defs.hpp>
34 
35 #include <StreamWrapper.hpp>
36 #include <Trajectory.hpp>
37 
38 
39 namespace loos {
40 
41 
43 
64  class DCD : public Trajectory {
65  static bool suppress_warnings;
66 
67 
68  // Use a union to convert data to appropriate type...
69  typedef union { unsigned int ui; int i; char c[4]; float f; } DataOverlay;
70 
71  public:
72  class EndOfFile : public LOOSError {
73  public:
74  EndOfFile() : LOOSError("unexpected end of file while reading DCD") { }
75  };
76 
78  explicit DCD(const std::string s) : Trajectory(s), _natoms(0),
79  qcrys(std::vector<double>(6)),
80  frame_size(0), first_frame_pos(0),
81  swabbing(false) { initTrajectory(); }
82 
84  explicit DCD(const char* s) : Trajectory(s), _natoms(0),
85  qcrys(std::vector<double>(6)), frame_size(0),
86  first_frame_pos(0), swabbing(false) { initTrajectory(); }
87 
89  explicit DCD(std::istream& fs) : Trajectory(fs), _natoms(0),
90  qcrys(std::vector<double>(6)), frame_size(0), first_frame_pos(0),
91  swabbing(false) { initTrajectory(); };
92 
93  std::string description() const { return("CHARMM/NAMD DCD"); }
94 
95  static pTraj create(const std::string& fname, const AtomicGroup& model) {
96  return(pTraj(new DCD(fname)));
97  }
98 
99 
100 
101  // Accessor methods...
102 
103  virtual uint natoms(void) const;
104  virtual bool hasPeriodicBox(void) const;
105  virtual GCoord periodicBox(void) const;
106 
107  std::vector<std::string> titles(void) const;
108 
109  int icntrl(const int) const;
110  void icntrl(const int, const int);
111 
112  // * legacy *
113  std::vector<double> crystalParams(void) const;
114  bool hasCrystalParams(void) const;
115 
116  virtual float timestep(void) const;
117  virtual uint nframes(void) const;
118 
120  std::vector<dcd_real> xcoords(void) const;
122  std::vector<dcd_real> ycoords(void) const;
124  std::vector<dcd_real> zcoords(void) const;
125 
126  // The following track CHARMm names (more or less...)
127  unsigned int nsteps(void) const;
128  float delta(void) const;
129  int nsavc(void) const;
130  int nfile(void) const;
131  int nfixed(void) const;
132 
134  bool nativeFormat(void) const;
135 
137 
138  virtual std::vector<GCoord> coords(void);
139 
141  // This is slated to go away...
142  std::vector<GCoord> mappedCoords(const std::vector<int>& map);
143 
144 
145 
146  static void setSuppression(const bool b) { suppress_warnings = b; }
147 
149  virtual bool parseFrame(void);
150 
151  private:
152 
154  void readHeader(void);
155 
156  void initTrajectory();
157 
158  // Trajectory member functions we must provide...
159  virtual void seekNextFrameImpl(void) { } // DCD frames are always contiguous, so do nothing...
161  virtual void seekFrameImpl(const uint);
162 
164  virtual void rewindImpl(void);
165 
167  virtual void updateGroupCoordsImpl(AtomicGroup& g);
168 
169 
170 
171  void allocateSpace(const int n);
172  bool readCrystalParams(void);
173  bool readCoordLine(std::vector<float>& v);
174 
175  void endianMatch(StreamWrapper& fsw);
176 
177  // For reading F77 I/O
178  unsigned int readRecordLen(void);
179  DataOverlay* readF77Line(unsigned int *len);
180 
181 
182 
183  private:
184  int _icntrl[20]; // DCD header data
185  uint _natoms; // # of atoms
186  std::vector<std::string> _titles; // Vector of title lines from DCD
187  std::vector<double> qcrys; // Crystal params
188  float _delta; // Timestep (extracted from _icntrl)
189 
190  unsigned long frame_size; // *Internal* size (in bytes) of each frame
191  unsigned long first_frame_pos; // *Internal* location in file of start of data frames
192 
193  bool swabbing; // DCD being read is not in native format...
194 
195  std::vector<dcd_real> xcrds, ycrds, zcrds;
196 
197  };
198 
199 }
200 
201 
202 
203 #endif
std::vector< dcd_real > xcoords(void) const
Return the raw coords...
Definition: dcd.cpp:62
DCD(std::istream &fs)
Begin reading from the stream ifs.
Definition: dcd.hpp:89
std::vector< dcd_real > zcoords(void) const
Return the raw coords...
Definition: dcd.cpp:64
virtual float timestep(void) const
Timestep per frame.
Definition: dcd.cpp:59
Class for reading DCD files.
Definition: dcd.hpp:64
bool nativeFormat(void) const
Returns true if the DCD file being read is in the native endian format.
Definition: dcd.cpp:73
STL namespace.
virtual std::vector< GCoord > coords(void)
Auto-interleave the coords into a vector of GCoord()'s.
Definition: dcd.cpp:362
virtual bool hasPeriodicBox(void) const
Definition: dcd.cpp:43
std::string description() const
Return a string describing trajectory format.
Definition: dcd.hpp:93
std::vector< GCoord > mappedCoords(const std::vector< int > &map)
Interleave coords, selecting entries indexed by map.
Definition: dcd.cpp:374
std::vector< dcd_real > ycoords(void) const
Return the raw coords...
Definition: dcd.cpp:63
Generic LOOS exception.
Definition: exceptions.hpp:40
virtual GCoord periodicBox(void) const
Returns the periodic box for the current frame/trajectory.
Definition: dcd.cpp:44
virtual uint nframes(void) const
Number of frames in the trajectory.
Definition: dcd.cpp:60
virtual uint natoms(void) const
of atoms per frame
Definition: dcd.cpp:42
Class for handling groups of Atoms (pAtoms, actually)
Definition: AtomicGroup.hpp:87
Namespace for most things not already encapsulated within a class.
Base-class for polymorphic trajectories.
Definition: Trajectory.hpp:64
virtual bool parseFrame(void)
Parse a frame of the DCD.
Definition: dcd.cpp:326
DCD(const char *s)
Begin reading from the file named s.
Definition: dcd.hpp:84
DCD(const std::string s)
Begin reading from the file named s.
Definition: dcd.hpp:78