|
6 | 6 | #include "tool/error.h" |
7 | 7 | #include "tool/externfunc.h" |
8 | 8 | #include "tool/gpucard.h" |
9 | | -#include <cassert> |
10 | 9 | #include <tinker/detail/atomid.hh> |
11 | 10 | #include <tinker/detail/atoms.hh> |
12 | 11 | #include <tinker/detail/bound.hh> |
13 | 12 | #include <tinker/detail/usage.hh> |
14 | 13 |
|
| 14 | +#include <cassert> |
| 15 | + |
15 | 16 | namespace tinker { |
16 | 17 | void nData(RcOp op) |
17 | 18 | { |
@@ -154,49 +155,166 @@ void bounds() |
154 | 155 | copyPosToXyz(); |
155 | 156 | } |
156 | 157 |
|
157 | | -void readFrameCopyinToXyz(std::istream& ipt, bool& done) |
| 158 | +inline namespace v1 { |
| 159 | +enum |
158 | 160 | { |
159 | | - if (done) |
160 | | - return; |
| 161 | + DCD_HEADER = 0, |
| 162 | + DCD_TDELTA = 10, |
| 163 | + DCD_USEBOX = 11, |
| 164 | + DCD_CTRL_LEN = 21, |
161 | 165 |
|
162 | | - if (ipt) { |
163 | | - std::string line; |
164 | | - std::getline(ipt, line); // n and title |
165 | | - std::getline(ipt, line); // either box size or first atom |
166 | | - // 18.643000 18.643000 18.643000 90.000000 90.000000 90.000000 |
167 | | - // 1 O 8.733783 7.084710 -0.688468 1 2 3 |
168 | | - double l1, l2, l3, a1, a2, a3; |
169 | | - int matched = std::sscanf(line.data(), "%lf%lf%lf%lf%lf%lf", &l1, &l2, &l3, &a1, &a2, &a3); |
170 | | - int row = 0; |
171 | | - int index; |
172 | | - char name[32]; |
173 | | - double xr, yr, zr; |
174 | | - if (matched == 6) { |
175 | | - Box p; |
176 | | - boxLattice(p, box_shape, l1, l2, l3, a1, a2, a3); |
177 | | - boxSetCurrent(p); |
178 | | - } else { |
179 | | - std::sscanf(line.data(), "%d%s%lf%lf%lf", &index, name, &xr, &yr, &zr); |
180 | | - index -= 1; |
181 | | - atoms::x[index] = xr; |
182 | | - atoms::y[index] = yr; |
183 | | - atoms::z[index] = zr; |
184 | | - row = 1; |
185 | | - } |
| 166 | + DCD_TITLE_NCHARS = 80, |
186 | 167 |
|
187 | | - for (int ir = row; ir < n; ++ir) { |
188 | | - std::getline(ipt, line); |
189 | | - std::sscanf(line.data(), "%d%s%lf%lf%lf", &index, name, &xr, &yr, &zr); |
190 | | - index -= 1; |
191 | | - atoms::x[index] = xr; |
192 | | - atoms::y[index] = yr; |
193 | | - atoms::z[index] = zr; |
194 | | - } |
| 168 | + DCD_AX = 0, |
| 169 | + DCD_COS_G = 1, |
| 170 | + DCD_BX = 2, |
| 171 | + DCD_COS_B = 3, |
| 172 | + DCD_COS_A = 4, |
| 173 | + DCD_CX = 5, |
| 174 | + DCD_XTAL_LEN = 6, |
| 175 | +}; |
| 176 | + |
| 177 | +enum class Archive |
| 178 | +{ |
| 179 | + NONE = 0, |
| 180 | + XYZ = 1, |
| 181 | + DCD = 2, |
| 182 | +}; |
| 183 | +} |
| 184 | + |
| 185 | +static int dcdControl[DCD_CTRL_LEN] = {0}; |
| 186 | +static std::vector<float> dcdx, dcdy, dcdz; |
| 187 | +static Archive archive = Archive::NONE; |
| 188 | + |
| 189 | +static void dcdReadIntoBuffer(void* buffer, int nbyte, std::ifstream& ipt) |
| 190 | +{ |
| 191 | + int size1, size2; |
| 192 | + ipt.read((char*)&size1, sizeof(int)); |
| 193 | + if (nbyte > 0) assert(nbyte == size1); |
| 194 | + ipt.read((char*)buffer, size1); |
| 195 | + ipt.read((char*)&size2, sizeof(int)); |
| 196 | +} |
| 197 | + |
| 198 | +void readFrameOpen(const std::string& filename, std::ifstream& ipt) |
| 199 | +{ |
| 200 | + // get file format type by inspection of first character |
| 201 | + char a1; |
| 202 | + ipt.open(filename); |
| 203 | + ipt >> a1; |
| 204 | + auto arc = Archive::NONE; |
| 205 | + if (a1 == ' ') |
| 206 | + arc = Archive::XYZ; |
| 207 | + else if ('0' <= a1 and a1 <= '9') |
| 208 | + arc = Archive::XYZ; |
| 209 | + else |
| 210 | + arc = Archive::DCD; |
| 211 | + |
| 212 | + if (arc == Archive::DCD) { |
| 213 | + ipt.close(); |
| 214 | + ipt.open(filename, std::ios::in | std::ios::binary); |
| 215 | + |
| 216 | + // read header info along with title and number of atoms |
| 217 | + dcdReadIntoBuffer(dcdControl, sizeof(int) * DCD_CTRL_LEN, ipt); |
| 218 | + |
| 219 | + int dcdTitleRecordLen; |
| 220 | + ipt.read((char*)&dcdTitleRecordLen, sizeof(int)); |
| 221 | + std::vector<char> titlebuf; |
| 222 | + titlebuf.resize(dcdTitleRecordLen + sizeof(int)); |
| 223 | + ipt.read(titlebuf.data(), dcdTitleRecordLen + sizeof(int)); |
| 224 | + |
| 225 | + int dcdNAtom; |
| 226 | + dcdReadIntoBuffer(&dcdNAtom, sizeof(int), ipt); |
| 227 | + assert(n == dcdNAtom); |
| 228 | + dcdx.resize(n); |
| 229 | + dcdy.resize(n); |
| 230 | + dcdz.resize(n); |
| 231 | + } |
| 232 | + |
| 233 | + archive = arc; |
| 234 | +} |
| 235 | + |
| 236 | +static void readFrameDCD(std::ifstream& ipt) |
| 237 | +{ |
| 238 | + if (dcdControl[DCD_USEBOX]) { |
| 239 | + double dcdXtal[DCD_XTAL_LEN]; |
| 240 | + dcdReadIntoBuffer(dcdXtal, sizeof(double) * DCD_XTAL_LEN, ipt); |
| 241 | + double ax = dcdXtal[DCD_AX], bx = dcdXtal[DCD_BX], cx = dcdXtal[DCD_CX]; |
| 242 | + double al = 90., be = 90., ga = 90.; |
| 243 | + if (dcdXtal[DCD_COS_A] != 0.0) al = std::acos(dcdXtal[DCD_COS_A]) * radian; |
| 244 | + if (dcdXtal[DCD_COS_B] != 0.0) be = std::acos(dcdXtal[DCD_COS_B]) * radian; |
| 245 | + if (dcdXtal[DCD_COS_G] != 0.0) ga = std::acos(dcdXtal[DCD_COS_G]) * radian; |
| 246 | + Box p; |
| 247 | + boxLattice(p, box_shape, ax, bx, cx, al, be, ga); |
| 248 | + boxSetCurrent(p); |
| 249 | + } |
| 250 | + |
| 251 | + dcdReadIntoBuffer(dcdx.data(), sizeof(float) * n, ipt); |
| 252 | + dcdReadIntoBuffer(dcdy.data(), sizeof(float) * n, ipt); |
| 253 | + dcdReadIntoBuffer(dcdz.data(), sizeof(float) * n, ipt); |
| 254 | + for (int i = 0; i < n; ++i) { |
| 255 | + atoms::x[i] = dcdx[i]; |
| 256 | + atoms::y[i] = dcdy[i]; |
| 257 | + atoms::z[i] = dcdz[i]; |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +static void readFrameXYZ(std::ifstream& ipt) |
| 262 | +{ |
| 263 | + std::string line; |
| 264 | + std::getline(ipt, line); // n and title |
| 265 | + std::getline(ipt, line); // either box size or first atom |
| 266 | + // 18.643000 18.643000 18.643000 90.000000 90.000000 90.000000 |
| 267 | + // 1 O 8.733783 7.084710 -0.688468 1 2 3 |
| 268 | + double l1, l2, l3, a1, a2, a3; |
| 269 | + int matched = std::sscanf(line.data(), "%lf%lf%lf%lf%lf%lf", &l1, &l2, &l3, &a1, &a2, &a3); |
| 270 | + int row = 0; |
| 271 | + int index; |
| 272 | + char name[32]; |
| 273 | + double xr, yr, zr; |
| 274 | + if (matched == 6) { |
| 275 | + Box p; |
| 276 | + boxLattice(p, box_shape, l1, l2, l3, a1, a2, a3); |
| 277 | + boxSetCurrent(p); |
| 278 | + } else { |
| 279 | + std::sscanf(line.data(), "%d%s%lf%lf%lf", &index, name, &xr, &yr, &zr); |
| 280 | + index -= 1; |
| 281 | + atoms::x[index] = xr; |
| 282 | + atoms::y[index] = yr; |
| 283 | + atoms::z[index] = zr; |
| 284 | + row = 1; |
| 285 | + } |
195 | 286 |
|
196 | | - xyzData(RcOp::INIT); |
| 287 | + for (int ir = row; ir < n; ++ir) { |
| 288 | + std::getline(ipt, line); |
| 289 | + std::sscanf(line.data(), "%d%s%lf%lf%lf", &index, name, &xr, &yr, &zr); |
| 290 | + index -= 1; |
| 291 | + atoms::x[index] = xr; |
| 292 | + atoms::y[index] = yr; |
| 293 | + atoms::z[index] = zr; |
197 | 294 | } |
| 295 | +} |
| 296 | + |
| 297 | +void readFrameCopyinToXyz(std::ifstream& ipt, bool& done) |
| 298 | +{ |
| 299 | + if (!ipt) done = true; |
| 300 | + if (done) return; |
198 | 301 |
|
199 | | - if (ipt.peek() == EOF) |
200 | | - done = true; |
| 302 | + if (archive == Archive::XYZ) |
| 303 | + readFrameXYZ(ipt); |
| 304 | + else if (archive == Archive::DCD) |
| 305 | + readFrameDCD(ipt); |
| 306 | + |
| 307 | + xyzData(RcOp::INIT); |
| 308 | + |
| 309 | + if (!ipt.good() or ipt.peek() == EOF) done = true; |
| 310 | +} |
| 311 | + |
| 312 | +void readFrameClose(std::ifstream& ipt) |
| 313 | +{ |
| 314 | + ipt.close(); |
| 315 | + dcdx.clear(); |
| 316 | + dcdy.clear(); |
| 317 | + dcdz.clear(); |
| 318 | + archive = Archive::NONE; |
201 | 319 | } |
202 | 320 | } |
0 commit comments