LOOS  v2.3.2
amber_rst.cpp
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 #include <amber_rst.hpp>
25 #include <AtomicGroup.hpp>
26 #include <iomanip>
27 #include <sstream>
28 
29 namespace loos {
30 
31 
32 
33  bool AmberRst::parseFrame(void) {
34  std::string buf;
35  greal x, y, z;
36 
37  // Skip the title...
38  std::getline(*(ifs()), buf);
39  if (ifs()->eof())
40  return(false); // Catch the initial EOF...
41 
42  // Check the # of atoms...
43  // Note: the Amber spec says that this line should contain both
44  // the number of atoms and the current time, but some restart
45  // files "in the wild" only have the number of atoms...
46  std::getline(*(ifs()), buf);
47  std::istringstream iss(buf);
48  uint na;
49  iss >> na;
50  if (na != _natoms)
51  throw(FileReadError(_filename, "Number of atoms mismatch in Amber restart file"));
52 
53  iss >> current_time;
54 
55 
56  // This should probably be in an initialization rather than here...???
57  frame.reserve(na);
58 
59  uint i;
60  for (i=0; i<_natoms && !(ifs()->eof()); ++i) {
61  *(ifs()) >> std::setw(12) >> x >> std::setw(12) >> y >> std::setw(12) >> z;
62  frame[i] = GCoord(x, y, z);
63  }
64 
65  if (i != _natoms)
66  throw(FileReadError(_filename, "Number of atoms read is not what was expected"));
67 
68  // Probe for velocities or periodic box...
69  greal a, b, c;
70  *(ifs()) >> std::setw(12) >> a >> std::setw(12) >> b >> std::setw(12) >> c;
71  if (ifs()->eof())
72  return(true);
73 
74  // Finish reading the putative box...
75  *(ifs()) >> std::setw(12) >> x >> std::setw(12) >> y >> std::setw(12) >> z;
76 
77  // Check to see if there are more numbers... If so, implies we're
78  // in velocities...
79  *(ifs()) >> std::setw(12) >> x >> std::setw(12) >> y >> std::setw(12) >> z;
80 
81  if (ifs()->eof()) {
82  periodic = true;
83  box = GCoord(a, b, c);
84  return(true);
85  }
86 
87  // This means we probably have velocities, so now we have to skip
88  // the appropriate # of atoms and try again for the box...
89 
90  for (uint i=3; i<_natoms && !(ifs()->eof()); ++i)
91  *(ifs()) >> std::setw(12) >> x >> std::setw(12) >> y >> std::setw(12) >> z;
92 
93  // Now read the box in...
94  *(ifs()) >> std::setw(12) >> a >> std::setw(12) >> b >> std::setw(12) >> c;
95  *(ifs()) >> std::setw(12) >> x >> std::setw(12) >> y >> std::setw(12) >> z;
96 
97  if (ifs()->eof() || ifs()->fail())
98  return(true);
99 
100  periodic = true;
101  box = GCoord(a, b, c);
102 
103  return(true);
104  }
105 
106 
107 
108  void AmberRst::updateGroupCoordsImpl(AtomicGroup& g) {
109  AtomicGroup::iterator gi;
110  pAtom pa;
111 
112 
113  for (gi = g.begin(); gi != g.end(); ++gi) {
114  uint i = (*gi)->index();
115  if (i >= _natoms)
116  throw(LOOSError(**gi, "Atom index into trajectory is out of bounds"));
117  (*gi)->coords(frame[i]);
118  }
119 
120  if (periodic)
121  g.periodicBox(box);
122  }
123 
124 
125  void AmberRst::seekNextFrameImpl(void) {
126  if (!seek_flag) {
127  seek_flag = true;
128  return;
129  }
130  }
131 
132  void AmberRst::seekFrameImpl(const uint i) {
133  }
134 
135 }
Errors that occur while reading a file.
Definition: exceptions.hpp:180
virtual bool parseFrame(void)
Parse an actual frame.
Definition: amber_rst.cpp:33
Generic LOOS exception.
Definition: exceptions.hpp:40
Class for handling groups of Atoms (pAtoms, actually)
Definition: AtomicGroup.hpp:87
Namespace for most things not already encapsulated within a class.
GCoord periodicBox(void) const
Fetch the periodic boundary conditions.