Debian Codegraph @

avogadro/io/trrformat.cpp

raw
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 "trrformat.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 <istream>
16 #include <ostream>
17 #include <string>
18
19 using std::map;
20 using std::pair;
21 using std::string;
22 using std::to_string;
23
24 namespace Avogadro::Io {
25
26 using Core::Array;
27 using Core::Atom;
28 using Core::Molecule;
29 using Core::UnitCell;
30
31 constexpr int GROMACS_MAGIC = 1993;
32 constexpr int DIM = 3;
33 constexpr float NM_TO_ANGSTROM = 10.0;
34 string TRRVERSION = "GMX_trn_file";
35 string HEADITEMS[] = { "ir_size", "e_size", "box_size", "vir_size",
36 "pres_size", "top_size", "sym_size", "x_size",
37 "v_size", "f_size", "natoms", "step",
38 "nre", "time", "lambda" };
39
40 int swapInteger(int inp)
41 {
42 return (((inp << 24) & 0xff000000) | ((inp << 8) & 0x00ff0000) |
43 ((inp >> 8) & 0x0000ff00) | ((inp >> 24) & 0x000000ff));
44 }
45
46 char swapEndian(char endian)
47 {
48 if (endian == '>')
49 return '<';
50 else
51 return '>';
52 }
53
54 /* Checks whether the data stored in the binary file is of float or double type
55 */
56 int isDouble(map<string, int>& header)
57 {
58 int SIZE_DOUBLE = struct_calcsize("d");
59 int size = 0;
60 string headerKeys[] = { "box_size", "x_size", "v_size", "f_size" };
61
62 for (auto& headerKey : headerKeys) {
63 if (header[headerKey] != 0) {
64 if (headerKey == "box_size") {
65 size = (int)(header[headerKey] / DIM * DIM);
66 break;
67 } else {
68 size = (int)(header[headerKey] / (header["natoms"] * DIM));
69 break;
70 }
71 }
72 }
73 return size == SIZE_DOUBLE;
74 }
75
76 bool TrrFormat::read(std::istream& inStream, Core::Molecule& mol)
77 {
78 bool doubleStatus;
79 char endian = '>', buff[BUFSIZ], fmt[BUFSIZ], raw[1000];
80 int magic, natoms, slen0, slen1, headval[13];
81 string subs, keyCheck[] = { "box_size", "vir_size", "pres_size" },
82 keyCheck2[] = { "x_size", "v_size", "f_size" };
83 map<string, int> header;
84
85 // Determining size of file
86 inStream.seekg(0, inStream.end);
87 int fileLen = inStream.tellg();
88 inStream.seekg(0, inStream.beg);
89
90 // Binary file must start with 1993
91 snprintf(fmt, sizeof(fmt), "%c1i", endian);
92 inStream.read(buff, struct_calcsize(fmt));
93 struct_unpack(buff, fmt, &magic);
94 if (magic != GROMACS_MAGIC) {
95 // Endian conversion
96 magic = swapInteger(magic);
97 endian = swapEndian(endian);
98 if (magic != GROMACS_MAGIC) {
99 appendError("Frame does not start with magic number 1993.");
100 return false;
101 }
102 }
103
104 snprintf(fmt, sizeof(fmt), "%c2i", endian);
105 inStream.read(buff, struct_calcsize(fmt));
106 struct_unpack(buff, fmt, &slen0, &slen1);
107
108 // Reading trajectory version string
109 snprintf(fmt, sizeof(fmt), "%c%ds", endian, slen0 - 1);
110 inStream.read(buff, struct_calcsize(fmt));
111 struct_unpack(buff, fmt, raw);
112 subs = string(raw).substr(0, 12);
113 if (subs != TRRVERSION) {
114 appendError("Gromacs version string mismatch.");
115 return false;
116 }
117
118 // "ir_size", "e_size", "box_size", "vir_size", "pres_size",
119 // "top_size", "sym_size", "x_size", "v_size", "f_size",
120 // "natoms", "step", "nre"
121 snprintf(fmt, sizeof(fmt), "%c13i", endian);
122 inStream.read(buff, struct_calcsize(fmt));
123 struct_unpack(buff, fmt, &headval[0], &headval[1], &headval[2], &headval[3],
124 &headval[4], &headval[5], &headval[6], &headval[7], &headval[8],
125 &headval[9], &headval[10], &headval[11], &headval[12]);
126 for (int i = 0; i < 13; ++i) {
127 header.insert(pair<string, int>(HEADITEMS[i], headval[i]));
128 }
129
130 // Reading timestep and lambda
131 doubleStatus = isDouble(header);
132 if (doubleStatus) {
133 double header0, header1;
134 snprintf(fmt, sizeof(fmt), "%c2d", endian);
135 inStream.read(buff, struct_calcsize(fmt));
136 struct_unpack(buff, fmt, &header0, &header1);
137 header.insert(pair<string, int>("time", header0));
138 header.insert(pair<string, int>("lambda", header1));
139 } else {
140 float header0, header1;
141 snprintf(fmt, sizeof(fmt), "%c2f", endian);
142 inStream.read(buff, struct_calcsize(fmt));
143 struct_unpack(buff, fmt, &header0, &header1);
144 header.insert(pair<string, int>("time", header0));
145 header.insert(pair<string, int>("lambda", header1));
146 }
147
148 // Reading matrices corresponding to "box_size", "vir_size", "pres_size"
149 for (auto& _kid : keyCheck) {
150 if (header[_kid] != 0) {
151 if (doubleStatus) {
152 snprintf(fmt, sizeof(fmt), "%c%dd", endian, DIM * DIM);
153 double mat[DIM][DIM];
154 inStream.read(buff, struct_calcsize(fmt));
155 struct_unpack(buff, fmt, &mat[0][0], &mat[0][1], &mat[0][2], &mat[1][0],
156 &mat[1][1], &mat[1][2], &mat[2][0], &mat[2][1],
157 &mat[2][2]);
158 if (_kid == "box_size") {
159 auto* uc = new UnitCell(
160 Vector3(mat[0][0] * NM_TO_ANGSTROM, mat[0][1] * NM_TO_ANGSTROM,
161 mat[0][2] * NM_TO_ANGSTROM),
162 Vector3(mat[1][0] * NM_TO_ANGSTROM, mat[1][1] * NM_TO_ANGSTROM,
163 mat[1][2] * NM_TO_ANGSTROM),
164 Vector3(mat[2][0] * NM_TO_ANGSTROM, mat[2][1] * NM_TO_ANGSTROM,
165 mat[2][2] * NM_TO_ANGSTROM));
166 if (!uc->isRegular()) {
167 appendError("lattice vectors are not linear independent");
168 delete uc;
169 return false;
170 }
171 mol.setUnitCell(uc);
172 }
173 } else {
174 snprintf(fmt, sizeof(fmt), "%c%df", endian, DIM * DIM);
175 float mat[DIM][DIM];
176 inStream.read(buff, struct_calcsize(fmt));
177 struct_unpack(buff, fmt, &mat[0][0], &mat[0][1], &mat[0][2], &mat[1][0],
178 &mat[1][1], &mat[1][2], &mat[2][0], &mat[2][1],
179 &mat[2][2]);
180 if (_kid == "box_size") {
181 auto* uc = new UnitCell(
182 Vector3(mat[0][0] * NM_TO_ANGSTROM, mat[0][1] * NM_TO_ANGSTROM,
183 mat[0][2] * NM_TO_ANGSTROM),
184 Vector3(mat[1][0] * NM_TO_ANGSTROM, mat[1][1] * NM_TO_ANGSTROM,
185 mat[1][2] * NM_TO_ANGSTROM),
186 Vector3(mat[2][0] * NM_TO_ANGSTROM, mat[2][1] * NM_TO_ANGSTROM,
187 mat[2][2] * NM_TO_ANGSTROM));
188 if (!uc->isRegular()) {
189 appendError("lattice vectors are not linear independent");
190 return false;
191 }
192 mol.setUnitCell(uc);
193 }
194 }
195 }
196 }
197
198 using AtomTypeMap = map<string, unsigned char>;
199 AtomTypeMap atomTypes;
200 unsigned char customElementCounter = CustomElementMin;
201
202 // Reading the coordinates of positions, velocities and forces
203 for (auto& _kid : keyCheck2) {
204 natoms = header["natoms"];
205 double coordsDouble[DIM];
206 float coordsFloat[DIM];
207 for (int i = 0; i < natoms; ++i) {
208 if (header[_kid] != 0) {
209 memset(coordsDouble, 0, sizeof(coordsDouble));
210 memset(coordsFloat, 0, sizeof(coordsFloat));
211 if (doubleStatus) {
212 snprintf(fmt, sizeof(fmt), "%c%dd", endian, DIM);
213 inStream.read(buff, struct_calcsize(fmt));
214 struct_unpack(buff, fmt, &coordsDouble[0], &coordsDouble[1],
215 &coordsDouble[2]);
216 } else {
217 snprintf(fmt, sizeof(fmt), "%c%df", endian, DIM);
218 inStream.read(buff, struct_calcsize(fmt));
219 struct_unpack(buff, fmt, &coordsFloat[0], &coordsFloat[1],
220 &coordsFloat[2]);
221 }
222
223 if (_kid == "x_size") {
224 // If parsed coordinates are fractional, the corresponding unscaling
225 // is done. Else the positions are assigned as parsed.
226 Vector3 pos(
227 coordsDouble[0] * NM_TO_ANGSTROM + coordsFloat[0] * NM_TO_ANGSTROM,
228 coordsDouble[1] * NM_TO_ANGSTROM + coordsFloat[1] * NM_TO_ANGSTROM,
229 coordsDouble[2] * NM_TO_ANGSTROM + coordsFloat[2] * NM_TO_ANGSTROM);
230
231 AtomTypeMap::const_iterator it;
232 // if (it == atomTypes.end()) {
233 atomTypes.insert(
234 std::make_pair(to_string(i), customElementCounter++));
235 it = atomTypes.find(to_string(i));
236 // if (customElementCounter > CustomElementMax) {
237 // appendError("Custom element type limit exceeded.");
238 // return false;
239 // }
240 Atom newAtom = mol.addAtom(it->second);
241 newAtom.setPosition3d(pos);
242 }
243 }
244 }
245
246 // Set the custom element map if needed
247 if (!atomTypes.empty()) {
248 Molecule::CustomElementMap elementMap;
249 for (const auto& atomType : atomTypes) {
250 elementMap.insert(
251 std::make_pair(atomType.second, "Atom " + atomType.first));
252 }
253 mol.setCustomElementMap(elementMap);
254 }
255 }
256 mol.setCoordinate3d(mol.atomPositions3d(), 0);
257
258 // Do we have an animation?
259 // EOF check
260 int coordSet = 1;
261 while (static_cast<int>(inStream.tellg()) != fileLen) {
262 // Binary header must start with 1993
263 snprintf(fmt, sizeof(fmt), "%c1i", endian);
264 inStream.read(buff, struct_calcsize(fmt));
265 struct_unpack(buff, fmt, &magic);
266 if (magic != GROMACS_MAGIC) {
267 // Endian conversion
268 magic = swapInteger(magic);
269 endian = swapEndian(endian);
270 if (magic != GROMACS_MAGIC) {
271 appendError("Frame does not start with magic number 1993.");
272 return false;
273 }
274 }
275
276 snprintf(fmt, sizeof(fmt), "%c2i", endian);
277 inStream.read(buff, struct_calcsize(fmt));
278 struct_unpack(buff, fmt, &slen0, &slen1);
279
280 // Reading trajectory version string
281 snprintf(fmt, sizeof(fmt), "%c%ds", endian, slen0 - 1);
282 inStream.read(buff, struct_calcsize(fmt));
283 struct_unpack(buff, fmt, raw);
284 subs = string(raw).substr(0, 12);
285 if (subs != TRRVERSION) {
286 appendError("Gromacs version string mismatch.");
287 return false;
288 }
289
290 // "ir_size", "e_size", "box_size", "vir_size", "pres_size",
291 // "top_size", "sym_size", "x_size", "v_size", "f_size",
292 // "natoms", "step", "nre"
293 snprintf(fmt, sizeof(fmt), "%c13i", endian);
294 inStream.read(buff, struct_calcsize(fmt));
295 struct_unpack(buff, fmt, &headval[0], &headval[1], &headval[2], &headval[3],
296 &headval[4], &headval[5], &headval[6], &headval[7],
297 &headval[8], &headval[9], &headval[10], &headval[11],
298 &headval[12]);
299 for (int i = 0; i < 13; ++i) {
300 header.insert(pair<string, int>(HEADITEMS[i], headval[i]));
301 }
302
303 // Reading timestep and lambda
304 doubleStatus = isDouble(header);
305 if (doubleStatus) {
306 double header0, header1;
307 snprintf(fmt, sizeof(fmt), "%c2d", endian);
308 inStream.read(buff, struct_calcsize(fmt));
309 struct_unpack(buff, fmt, &header0, &header1);
310 header.insert(pair<string, int>("time", header0));
311 header.insert(pair<string, int>("lambda", header1));
312 } else {
313 float header0, header1;
314 snprintf(fmt, sizeof(fmt), "%c2f", endian);
315 inStream.read(buff, struct_calcsize(fmt));
316 struct_unpack(buff, fmt, &header0, &header1);
317 header.insert(pair<string, int>("time", header0));
318 header.insert(pair<string, int>("lambda", header1));
319 }
320
321 // Reading matrices corresponding to "box_size", "vir_size", "pres_size"
322 for (auto& _kid : keyCheck) {
323 if (header[_kid] != 0) {
324 natoms = header["natoms"];
325 if (doubleStatus) {
326 snprintf(fmt, sizeof(fmt), "%c%dd", endian, DIM * DIM);
327 double mat[DIM][DIM];
328 inStream.read(buff, struct_calcsize(fmt));
329 struct_unpack(buff, fmt, &mat[0][0], &mat[0][1], &mat[0][2],
330 &mat[1][0], &mat[1][1], &mat[1][2], &mat[2][0],
331 &mat[2][1], &mat[2][2]);
332 if (_kid == "box_size") {
333 auto* uc = new UnitCell(
334 Vector3(mat[0][0] * NM_TO_ANGSTROM, mat[0][1] * NM_TO_ANGSTROM,
335 mat[0][2] * NM_TO_ANGSTROM),
336 Vector3(mat[1][0] * NM_TO_ANGSTROM, mat[1][1] * NM_TO_ANGSTROM,
337 mat[1][2] * NM_TO_ANGSTROM),
338 Vector3(mat[2][0] * NM_TO_ANGSTROM, mat[2][1] * NM_TO_ANGSTROM,
339 mat[2][2] * NM_TO_ANGSTROM));
340 if (!uc->isRegular()) {
341 appendError("lattice vectors are not linear independent");
342 return false;
343 }
344 mol.setUnitCell(uc);
345 }
346 } else {
347 snprintf(fmt, sizeof(fmt), "%c%df", endian, DIM * DIM);
348 float mat[DIM][DIM];
349 inStream.read(buff, struct_calcsize(fmt));
350 struct_unpack(buff, fmt, &mat[0][0], &mat[0][1], &mat[0][2],
351 &mat[1][0], &mat[1][1], &mat[1][2], &mat[2][0],
352 &mat[2][1], &mat[2][2]);
353 if (_kid == "box_size") {
354 auto* uc = new UnitCell(
355 Vector3(mat[0][0] * NM_TO_ANGSTROM, mat[0][1] * NM_TO_ANGSTROM,
356 mat[0][2] * NM_TO_ANGSTROM),
357 Vector3(mat[1][0] * NM_TO_ANGSTROM, mat[1][1] * NM_TO_ANGSTROM,
358 mat[1][2] * NM_TO_ANGSTROM),
359 Vector3(mat[2][0] * NM_TO_ANGSTROM, mat[2][1] * NM_TO_ANGSTROM,
360 mat[2][2] * NM_TO_ANGSTROM));
361 if (!uc->isRegular()) {
362 appendError("lattice vectors are not linear independent");
363 return false;
364 }
365 mol.setUnitCell(uc);
366 }
367 }
368 }
369 }
370
371 natoms = header["natoms"];
372 Array<Vector3> positions;
373 positions.reserve(natoms);
374
375 // Reading the coordinates of positions, velocities and forces
376 for (auto& _kid : keyCheck2) {
377 double coordsDouble[DIM];
378 float coordsFloat[DIM];
379 for (int i = 0; i < natoms; ++i) {
380 if (header[_kid] != 0) {
381 memset(coordsDouble, 0, sizeof(coordsDouble));
382 memset(coordsFloat, 0, sizeof(coordsFloat));
383 if (doubleStatus) {
384 snprintf(fmt, sizeof(fmt), "%c%dd", endian, DIM);
385 inStream.read(buff, struct_calcsize(fmt));
386 struct_unpack(buff, fmt, &coordsDouble[0], &coordsDouble[1],
387 &coordsDouble[2]);
388 } else {
389 snprintf(fmt, sizeof(fmt), "%c%df", endian, DIM);
390 inStream.read(buff, struct_calcsize(fmt));
391 struct_unpack(buff, fmt, &coordsFloat[0], &coordsFloat[1],
392 &coordsFloat[2]);
393 }
394
395 if (_kid == "x_size") {
396 // If parsed coordinates are fractional, the corresponding unscaling
397 // is done. Else the positions are assigned as parsed.
398 Vector3 pos(coordsDouble[0] * NM_TO_ANGSTROM +
399 coordsFloat[0] * NM_TO_ANGSTROM,
400 coordsDouble[1] * NM_TO_ANGSTROM +
401 coordsFloat[1] * NM_TO_ANGSTROM,
402 coordsDouble[2] * NM_TO_ANGSTROM +
403 coordsFloat[2] * NM_TO_ANGSTROM);
404 positions.push_back(pos);
405 }
406 }
407 }
408 }
409 mol.setCoordinate3d(positions, coordSet++);
410 positions.clear();
411 }
412 return true;
413 }
414
415 bool TrrFormat::write(std::ostream&, const Core::Molecule&)
416 {
417 return false;
418 }
419
420 std::vector<std::string> TrrFormat::fileExtensions() const
421 {
422 std::vector<std::string> ext;
423 ext.emplace_back("trr");
424 return ext;
425 }
426
427 std::vector<std::string> TrrFormat::mimeTypes() const
428 {
429 std::vector<std::string> mime;
430 mime.emplace_back("application/octet-stream");
431 return mime;
432 }
433
434 } // namespace Avogadro::Io