| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "dcdformat.h" |
| 7 |
#include "struct.h" |
| 8 |
|
| 9 |
#include <avogadro/core/elements.h> |
| 10 |
#include <avogadro/core/molecule.h> |
| 11 |
#include <avogadro/core/unitcell.h> |
| 12 |
#include <avogadro/core/utilities.h> |
| 13 |
#include <avogadro/core/vector.h> |
| 14 |
|
| 15 |
#include <cmath> |
| 16 |
#include <istream> |
| 17 |
#include <ostream> |
| 18 |
#include <string> |
| 19 |
|
| 20 |
using std::map; |
| 21 |
using std::string; |
| 22 |
using std::to_string; |
| 23 |
using std::vector; |
| 24 |
|
| 25 |
namespace Avogadro::Io { |
| 26 |
|
| 27 |
using Core::Array; |
| 28 |
using Core::Atom; |
| 29 |
using Core::Molecule; |
| 30 |
using Core::UnitCell; |
| 31 |
|
| 32 |
#ifndef _WIN32 |
| 33 |
#endif |
| 34 |
|
| 35 |
#define DCD_EOF (-1) |
| 36 |
constexpr int DCD_MAGIC = 84; |
| 37 |
constexpr int DCD_IS_CHARMM = 0x01; |
| 38 |
constexpr int DCD_HAS_4DIMS = 0x02; |
| 39 |
constexpr int DCD_HAS_EXTRA_BLOCK = 0x04; |
| 40 |
|
| 41 |
int swap_integer(int inp) |
| 42 |
{ |
| 43 |
return (((inp << 24) & 0xff000000) | ((inp << 8) & 0x00ff0000) | |
| 44 |
((inp >> 8) & 0x0000ff00) | ((inp >> 24) & 0x000000ff)); |
| 45 |
} |
| 46 |
|
| 47 |
char swap_endian(char endian) |
| 48 |
{ |
| 49 |
if (endian == '>') |
| 50 |
return '<'; |
| 51 |
else |
| 52 |
return '>'; |
| 53 |
} |
| 54 |
|
| 55 |
bool DcdFormat::read(std::istream& inStream, Core::Molecule& mol) |
| 56 |
{ |
| 57 |
/** Endian type, Buffer and Format char containers for unpacking and storing |
| 58 |
* data using struct library */ |
| 59 |
char endian = '>'; |
| 60 |
char buff[BUFSIZ]; |
| 61 |
char fmt[BUFSIZ]; |
| 62 |
|
| 63 |
/** Variables to store various components from the binary data unpacked using |
| 64 |
* the struct library */ |
| 65 |
char raw[84]; |
| 66 |
char* remarks; |
| 67 |
double DELTA; |
| 68 |
int magic; |
| 69 |
int charmm; |
| 70 |
int NAMNF; |
| 71 |
int NTITLE; |
| 72 |
int lenRemarks; |
| 73 |
int NATOMS; |
| 74 |
int blockSize; |
| 75 |
|
| 76 |
// Determining size of file |
| 77 |
inStream.seekg(0, inStream.end); |
| 78 |
int fileLen = inStream.tellg(); |
| 79 |
inStream.seekg(0, inStream.beg); |
| 80 |
|
| 81 |
// Reading magic number |
| 82 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 83 |
inStream.read(buff, struct_calcsize(fmt)); |
| 84 |
struct_unpack(buff, fmt, &magic); |
| 85 |
if (magic != DCD_MAGIC) { |
| 86 |
magic = swap_integer(magic); |
| 87 |
endian = swap_endian(endian); |
| 88 |
if (magic != DCD_MAGIC) { |
| 89 |
appendError("File does not start with magic number 84."); |
| 90 |
return false; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// CORD |
| 95 |
snprintf(fmt, sizeof(fmt), "%c%ds", endian, magic); |
| 96 |
inStream.read(buff, struct_calcsize(fmt)); |
| 97 |
struct_unpack(buff, fmt, raw); |
| 98 |
if (raw[0] != 'C' || raw[1] != 'O' || raw[2] != 'R' || raw[3] != 'D') { |
| 99 |
appendError("Keyword CORD not found."); |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
// Determining whether the trajectory file is from CHARMM or not |
| 104 |
if (*(reinterpret_cast<int*>(raw + 80)) != 0) { |
| 105 |
charmm = DCD_IS_CHARMM; |
| 106 |
if (*(reinterpret_cast<int*>(raw + 44)) != 0) |
| 107 |
charmm |= DCD_HAS_EXTRA_BLOCK; |
| 108 |
|
| 109 |
if (*(reinterpret_cast<int*>(raw + 48)) == 1) |
| 110 |
charmm |= DCD_HAS_4DIMS; |
| 111 |
} else { |
| 112 |
charmm = 0; |
| 113 |
} |
| 114 |
|
| 115 |
// number of fixed atoms |
| 116 |
NAMNF = *(reinterpret_cast<int*>(raw + 36)); |
| 117 |
|
| 118 |
// DELTA (timestep) is stored as a double with X-PLOR but as a float with |
| 119 |
// CHARMM |
| 120 |
if (charmm & DCD_IS_CHARMM) { |
| 121 |
float ftmp; |
| 122 |
ftmp = *(reinterpret_cast<float*>(raw + 40)); |
| 123 |
|
| 124 |
DELTA = static_cast<double>(ftmp); |
| 125 |
} else { |
| 126 |
(DELTA) = *(reinterpret_cast<double*>(raw + 40)); |
| 127 |
} |
| 128 |
|
| 129 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 130 |
inStream.read(buff, struct_calcsize(fmt)); |
| 131 |
struct_unpack(buff, fmt, &magic); |
| 132 |
|
| 133 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 134 |
inStream.read(buff, struct_calcsize(fmt)); |
| 135 |
struct_unpack(buff, fmt, &blockSize); |
| 136 |
|
| 137 |
if (((blockSize - 4) % 80) == 0) { |
| 138 |
// Read NTITLE, the number of 80 character title strings |
| 139 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 140 |
inStream.read(buff, struct_calcsize(fmt)); |
| 141 |
struct_unpack(buff, fmt, &NTITLE); |
| 142 |
lenRemarks = NTITLE * 80; |
| 143 |
remarks = reinterpret_cast<char*>(malloc(lenRemarks)); |
| 144 |
snprintf(fmt, sizeof(fmt), "%c%ds", endian, lenRemarks); |
| 145 |
inStream.read(buff, struct_calcsize(fmt)); |
| 146 |
struct_unpack(buff, fmt, remarks); |
| 147 |
|
| 148 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 149 |
inStream.read(buff, struct_calcsize(fmt)); |
| 150 |
int endSize; |
| 151 |
struct_unpack(buff, fmt, &endSize); |
| 152 |
} else { |
| 153 |
appendError("Block size must be 4 plus a multiple of 80."); |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 158 |
inStream.read(buff, struct_calcsize(fmt)); |
| 159 |
int fourInput; |
| 160 |
struct_unpack(buff, fmt, &fourInput); |
| 161 |
if (fourInput != 4) { |
| 162 |
// Error |
| 163 |
} |
| 164 |
|
| 165 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 166 |
inStream.read(buff, struct_calcsize(fmt)); |
| 167 |
struct_unpack(buff, fmt, &NATOMS); |
| 168 |
|
| 169 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 170 |
inStream.read(buff, struct_calcsize(fmt)); |
| 171 |
struct_unpack(buff, fmt, &fourInput); |
| 172 |
if (fourInput != 4) { |
| 173 |
appendError("Expected token 4. Read token " + to_string(fourInput)); |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
if (NAMNF != 0) { |
| 178 |
int** FREEINDEXES = |
| 179 |
reinterpret_cast<int**>(calloc((NATOMS - NAMNF), sizeof(int))); |
| 180 |
if (*FREEINDEXES == nullptr) { |
| 181 |
appendError("MALLOC failed."); |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
/* Read in index array size */ |
| 186 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 187 |
inStream.read(buff, struct_calcsize(fmt)); |
| 188 |
int arrSize; |
| 189 |
struct_unpack(buff, fmt, &arrSize); |
| 190 |
|
| 191 |
if (arrSize != (NATOMS - NAMNF) * 4) { |
| 192 |
appendError("DCD file contains bad format."); |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
snprintf(fmt, sizeof(fmt), "%c%di", endian, (NATOMS - NAMNF)); |
| 197 |
inStream.read(buff, struct_calcsize(fmt)); |
| 198 |
struct_unpack(buff, fmt, *FREEINDEXES); |
| 199 |
|
| 200 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 201 |
inStream.read(buff, struct_calcsize(fmt)); |
| 202 |
struct_unpack(buff, fmt, &arrSize); |
| 203 |
|
| 204 |
if (arrSize != (NATOMS - NAMNF) * 4) { |
| 205 |
appendError("DCD file contains bad format."); |
| 206 |
return false; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
// CHARMM trajectories have an extra block to be read, that contains |
| 211 |
// information about the unit cell |
| 212 |
if ((charmm & DCD_IS_CHARMM) && (charmm & DCD_HAS_EXTRA_BLOCK)) { |
| 213 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 214 |
inStream.read(buff, struct_calcsize(fmt)); |
| 215 |
int leadingNum; |
| 216 |
struct_unpack(buff, fmt, &leadingNum); |
| 217 |
|
| 218 |
if (leadingNum == 48) { |
| 219 |
double unitcell[6]; |
| 220 |
for (double& aa : unitcell) { |
| 221 |
snprintf(fmt, sizeof(fmt), "%c%dd", endian, 1); |
| 222 |
inStream.read(buff, struct_calcsize(fmt)); |
| 223 |
struct_unpack(buff, fmt, &aa); |
| 224 |
} |
| 225 |
if (unitcell[1] >= -1.0 && unitcell[1] <= 1.0 && unitcell[3] >= -1.0 && |
| 226 |
unitcell[3] <= 1.0 && unitcell[4] >= -1.0 && unitcell[4] <= 1.0) { |
| 227 |
// CHARMM and certain NAMD files have the cosines instead of angles |
| 228 |
// This formulation improves rounding behavior for orthogonal cells |
| 229 |
// so that the angles end up at precisely 90 degrees, unlike acos() |
| 230 |
unitcell[4] = M_PI_2 - asin(unitcell[4]); /* cosBC */ |
| 231 |
unitcell[3] = M_PI_2 - asin(unitcell[3]); /* cosAC */ |
| 232 |
unitcell[1] = M_PI_2 - asin(unitcell[1]); /* cosAB */ |
| 233 |
} |
| 234 |
|
| 235 |
auto* cell = new UnitCell(unitcell[0], unitcell[2], unitcell[5], |
| 236 |
unitcell[4], unitcell[3], unitcell[1]); |
| 237 |
if (!cell->isRegular()) { |
| 238 |
appendError("cell matrix is singular"); |
| 239 |
delete cell; |
| 240 |
return false; |
| 241 |
} |
| 242 |
mol.setUnitCell(cell); |
| 243 |
} else { |
| 244 |
inStream.read(buff, leadingNum); |
| 245 |
} |
| 246 |
inStream.read(buff, sizeof(int)); |
| 247 |
} |
| 248 |
|
| 249 |
// Reading the atom coordinates |
| 250 |
int formatint[6]; |
| 251 |
Array<float> cx, cy, cz; |
| 252 |
cx.reserve(NATOMS); |
| 253 |
cy.reserve(NATOMS); |
| 254 |
cz.reserve(NATOMS); |
| 255 |
|
| 256 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 257 |
inStream.read(buff, struct_calcsize(fmt)); |
| 258 |
struct_unpack(buff, fmt, &formatint[0]); |
| 259 |
|
| 260 |
for (int i = 0; i < NATOMS; ++i) { |
| 261 |
// X coordinates |
| 262 |
snprintf(fmt, sizeof(fmt), "%c%df", endian, 1); |
| 263 |
inStream.read(buff, struct_calcsize(fmt)); |
| 264 |
struct_unpack(buff, fmt, &cx[i]); |
| 265 |
/* code */ |
| 266 |
} |
| 267 |
|
| 268 |
snprintf(fmt, sizeof(fmt), "%c2i", endian); |
| 269 |
inStream.read(buff, struct_calcsize(fmt)); |
| 270 |
struct_unpack(buff, fmt, &formatint[1], &formatint[2]); |
| 271 |
|
| 272 |
for (int i = 0; i < NATOMS; ++i) { |
| 273 |
// Y coordinates |
| 274 |
snprintf(fmt, sizeof(fmt), "%c%df", endian, 1); |
| 275 |
inStream.read(buff, struct_calcsize(fmt)); |
| 276 |
struct_unpack(buff, fmt, &cy[i]); |
| 277 |
} |
| 278 |
|
| 279 |
snprintf(fmt, sizeof(fmt), "%c2i", endian); |
| 280 |
inStream.read(buff, struct_calcsize(fmt)); |
| 281 |
struct_unpack(buff, fmt, &formatint[3], &formatint[4]); |
| 282 |
|
| 283 |
for (int i = 0; i < NATOMS; ++i) { |
| 284 |
// Z coordinates |
| 285 |
snprintf(fmt, sizeof(fmt), "%c%df", endian, 1); |
| 286 |
inStream.read(buff, struct_calcsize(fmt)); |
| 287 |
struct_unpack(buff, fmt, &cz[i]); |
| 288 |
} |
| 289 |
|
| 290 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 291 |
inStream.read(buff, struct_calcsize(fmt)); |
| 292 |
struct_unpack(buff, fmt, &formatint[5]); |
| 293 |
|
| 294 |
typedef map<string, unsigned char> AtomTypeMap; |
| 295 |
AtomTypeMap atomTypes; |
| 296 |
unsigned char customElementCounter = CustomElementMin; |
| 297 |
|
| 298 |
for (int i = 0; i < NATOMS; ++i) { |
| 299 |
Vector3 pos(cx[i], cy[i], cz[i]); |
| 300 |
|
| 301 |
AtomTypeMap::const_iterator it; |
| 302 |
atomTypes.insert(std::make_pair(to_string(i), customElementCounter++)); |
| 303 |
it = atomTypes.find(to_string(i)); |
| 304 |
// if (customElementCounter > CustomElementMax) { |
| 305 |
// appendError("Custom element type limit exceeded."); |
| 306 |
// return false; |
| 307 |
// } |
| 308 |
Atom newAtom = mol.addAtom(it->second); |
| 309 |
newAtom.setPosition3d(pos); |
| 310 |
} |
| 311 |
|
| 312 |
mol.setTimeStep(0, 0); |
| 313 |
|
| 314 |
// Skipping fourth dimension block |
| 315 |
if ((charmm & DCD_IS_CHARMM) && (charmm & DCD_HAS_EXTRA_BLOCK)) { |
| 316 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 317 |
inStream.read(buff, struct_calcsize(fmt)); |
| 318 |
int sizeToRead; |
| 319 |
struct_unpack(buff, fmt, &sizeToRead); |
| 320 |
|
| 321 |
inStream.read(buff, sizeToRead); |
| 322 |
|
| 323 |
inStream.read(buff, sizeof(int)); |
| 324 |
} |
| 325 |
|
| 326 |
// Set the custom element map if needed |
| 327 |
if (!atomTypes.empty()) { |
| 328 |
Molecule::CustomElementMap elementMap; |
| 329 |
for (const auto& atomType : atomTypes) { |
| 330 |
elementMap.insert( |
| 331 |
std::make_pair(atomType.second, "Atom " + atomType.first)); |
| 332 |
} |
| 333 |
mol.setCustomElementMap(elementMap); |
| 334 |
} |
| 335 |
|
| 336 |
mol.setCoordinate3d(mol.atomPositions3d(), 0); |
| 337 |
|
| 338 |
// Do we have an animation? |
| 339 |
int coordSet = 1; |
| 340 |
while ((static_cast<int>(inStream.tellg()) != fileLen) && |
| 341 |
(static_cast<int>(inStream.tellg()) != DCD_EOF)) { |
| 342 |
// Reading the atom coordinates |
| 343 |
Array<Vector3> positions; |
| 344 |
positions.reserve(NATOMS); |
| 345 |
|
| 346 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 347 |
inStream.read(buff, struct_calcsize(fmt)); |
| 348 |
struct_unpack(buff, fmt, &formatint[0]); |
| 349 |
|
| 350 |
for (int i = 0; i < NATOMS; ++i) { |
| 351 |
// X coordinates |
| 352 |
snprintf(fmt, sizeof(fmt), "%c%df", endian, 1); |
| 353 |
inStream.read(buff, struct_calcsize(fmt)); |
| 354 |
struct_unpack(buff, fmt, &cx[i]); |
| 355 |
} |
| 356 |
|
| 357 |
snprintf(fmt, sizeof(fmt), "%c2i", endian); |
| 358 |
inStream.read(buff, struct_calcsize(fmt)); |
| 359 |
struct_unpack(buff, fmt, &formatint[1], &formatint[2]); |
| 360 |
|
| 361 |
for (int i = 0; i < NATOMS; ++i) { |
| 362 |
// Y coordinates |
| 363 |
snprintf(fmt, sizeof(fmt), "%c%df", endian, 1); |
| 364 |
inStream.read(buff, struct_calcsize(fmt)); |
| 365 |
struct_unpack(buff, fmt, &cy[i]); |
| 366 |
} |
| 367 |
|
| 368 |
snprintf(fmt, sizeof(fmt), "%c2i", endian); |
| 369 |
inStream.read(buff, struct_calcsize(fmt)); |
| 370 |
struct_unpack(buff, fmt, &formatint[3], &formatint[4]); |
| 371 |
|
| 372 |
for (int i = 0; i < NATOMS; ++i) { |
| 373 |
// Z coordinates |
| 374 |
snprintf(fmt, sizeof(fmt), "%c%df", endian, 1); |
| 375 |
inStream.read(buff, struct_calcsize(fmt)); |
| 376 |
struct_unpack(buff, fmt, &cz[i]); |
| 377 |
} |
| 378 |
|
| 379 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 380 |
inStream.read(buff, struct_calcsize(fmt)); |
| 381 |
struct_unpack(buff, fmt, &formatint[5]); |
| 382 |
|
| 383 |
for (int i = 0; i < NATOMS; ++i) { |
| 384 |
Vector3 pos(cx[i], cy[i], cz[i]); |
| 385 |
positions.push_back(pos); |
| 386 |
} |
| 387 |
|
| 388 |
mol.setTimeStep(DELTA * coordSet, coordSet); |
| 389 |
|
| 390 |
// Skipping fourth dimension block |
| 391 |
if ((charmm & DCD_IS_CHARMM) && (charmm & DCD_HAS_EXTRA_BLOCK)) { |
| 392 |
snprintf(fmt, sizeof(fmt), "%c1i", endian); |
| 393 |
inStream.read(buff, struct_calcsize(fmt)); |
| 394 |
int sizeToRead; |
| 395 |
struct_unpack(buff, fmt, &sizeToRead); |
| 396 |
|
| 397 |
inStream.read(buff, sizeToRead); |
| 398 |
|
| 399 |
inStream.read(buff, sizeof(int)); |
| 400 |
} |
| 401 |
|
| 402 |
mol.setCoordinate3d(positions, coordSet++); |
| 403 |
} |
| 404 |
|
| 405 |
return true; |
| 406 |
} |
| 407 |
|
| 408 |
bool DcdFormat::write(std::ostream&, const Core::Molecule&) |
| 409 |
{ |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
std::vector<std::string> DcdFormat::fileExtensions() const |
| 414 |
{ |
| 415 |
std::vector<std::string> ext; |
| 416 |
ext.emplace_back("dcd"); |
| 417 |
return ext; |
| 418 |
} |
| 419 |
|
| 420 |
std::vector<std::string> DcdFormat::mimeTypes() const |
| 421 |
{ |
| 422 |
std::vector<std::string> mime; |
| 423 |
mime.emplace_back("application/octet-stream"); |
| 424 |
return mime; |
| 425 |
} |
| 426 |
|
| 427 |
} // namespace Avogadro::Io |