Debian Codegraph @

avogadro/io/lammpsformat.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 "lammpsformat.h"
7
8 #include <avogadro/core/crystaltools.h>
9 #include <avogadro/core/elements.h>
10 #include <avogadro/core/molecule.h>
11 #include <avogadro/core/spacegroups.h>
12 #include <avogadro/core/unitcell.h>
13 #include <avogadro/core/utilities.h>
14 #include <avogadro/core/vector.h>
15
16 #include <cstdint>
17 #include <istream>
18 #include <ostream>
19 #include <sstream>
20 #include <string>
21
22 using std::getline;
23 using std::map;
24 using std::string;
25 using std::to_string;
26
27 namespace Avogadro::Io {
28
29 using Core::Array;
30 using Core::Atom;
31 using Core::Bond;
32 using Core::CrystalTools;
33 using Core::Elements;
34 using Core::lexicalCast;
35 using Core::Molecule;
36 using Core::SpaceGroups;
37 using Core::split;
38 using Core::trimmed;
39 using Core::UnitCell;
40
41 bool LammpsTrajectoryFormat::read(std::istream& inStream, Core::Molecule& mol)
42 {
43 size_t numAtoms = 0, timestep = 0, x_idx = SIZE_MAX, y_idx = SIZE_MAX,
44 z_idx = SIZE_MAX, type_idx = SIZE_MAX, id_idx = SIZE_MAX;
45 double x_min = 0, x_max = 0, y_min = 0, y_max = 0, z_min = 0, z_max = 0,
46 tilt_xy = 0, tilt_xz = 0, tilt_yz = 0, scale_x = 0., scale_y = 0.,
47 scale_z = 0.;
48
49 // This can likely be removed as it is clearly not used anywhere, suppress for
50 // now.
51 AVO_UNUSED(id_idx);
52
53 string buffer;
54 getline(inStream, buffer); // Finish the first line
55 buffer = trimmed(buffer);
56 if (buffer != "ITEM: TIMESTEP") {
57 appendError("No timestep item found.");
58 return false;
59 }
60 getline(inStream, buffer);
61 if (!buffer.empty()) {
62 timestep = lexicalCast<size_t>(buffer).value_or(0);
63 mol.setTimeStep(timestep, 0);
64 }
65
66 getline(inStream, buffer);
67 buffer = trimmed(buffer);
68 if (buffer != "ITEM: NUMBER OF ATOMS") {
69 appendError("No number of atoms item found.");
70 return false;
71 }
72 getline(inStream, buffer);
73 if (!buffer.empty()) {
74 if (auto n = lexicalCast<size_t>(buffer)) {
75 numAtoms = *n;
76 } else {
77 appendError("Error to read number of atoms");
78 return false;
79 }
80 }
81
82 // If unit cell is triclinic, tilt factors are needed to define the supercell
83 getline(inStream, buffer);
84 if (buffer.find("ITEM: BOX BOUNDS xy xz yz") == 0) {
85 // Read x_min, x_max, tiltfactor_xy
86 getline(inStream, buffer);
87 std::vector<string> box_bounds_x(split(buffer, ' '));
88 x_min = lexicalCast<double>(box_bounds_x.at(0)).value_or(0.0);
89 x_max = lexicalCast<double>(box_bounds_x.at(1)).value_or(0.0);
90 tilt_xy = lexicalCast<double>(box_bounds_x.at(2)).value_or(0.0);
91 // Read y_min, y_max, tiltfactor_xz
92 getline(inStream, buffer);
93 std::vector<string> box_bounds_y(split(buffer, ' '));
94 y_min = lexicalCast<double>(box_bounds_y.at(0)).value_or(0.0);
95 y_max = lexicalCast<double>(box_bounds_y.at(1)).value_or(0.0);
96 tilt_xz = lexicalCast<double>(box_bounds_y.at(2)).value_or(0.0);
97 getline(inStream, buffer);
98 // Read z_min, z_max, tiltfactor_yz
99 std::vector<string> box_bounds_z(split(buffer, ' '));
100 z_min = lexicalCast<double>(box_bounds_z.at(0)).value_or(0.0);
101 z_max = lexicalCast<double>(box_bounds_z.at(1)).value_or(0.0);
102 tilt_yz = lexicalCast<double>(box_bounds_z.at(2)).value_or(0.0);
103
104 x_min -= std::min({ tilt_xy, tilt_xz, tilt_xy + tilt_xz, 0.0 });
105 x_max -= std::max({ tilt_xy, tilt_xz, tilt_xy + tilt_xz, 0.0 });
106 y_min -= std::min(tilt_yz, 0.0);
107 y_max -= std::max(tilt_yz, 0.0);
108 }
109
110 // Else if unit cell is orthogonal, tilt factors are zero
111 else if (buffer.find("ITEM: BOX BOUNDS") == 0) {
112 // Read x_min, x_max
113 getline(inStream, buffer);
114 std::vector<string> box_bounds_x(split(buffer, ' '));
115 x_min = lexicalCast<double>(box_bounds_x.at(0)).value_or(0.0);
116 x_max = lexicalCast<double>(box_bounds_x.at(1)).value_or(0.0);
117 // Read y_min, y_max
118 getline(inStream, buffer);
119 std::vector<string> box_bounds_y(split(buffer, ' '));
120 y_min = lexicalCast<double>(box_bounds_y.at(0)).value_or(0.0);
121 y_max = lexicalCast<double>(box_bounds_y.at(1)).value_or(0.0);
122 // Read z_min, z_max
123 getline(inStream, buffer);
124 std::vector<string> box_bounds_z(split(buffer, ' '));
125 z_min = lexicalCast<double>(box_bounds_z.at(0)).value_or(0.0);
126 z_max = lexicalCast<double>(box_bounds_z.at(1)).value_or(0.0);
127 }
128
129 typedef map<string, unsigned char> AtomTypeMap;
130 AtomTypeMap atomTypes;
131 unsigned char customElementCounter = CustomElementMin;
132
133 // x,y,z stand for the coordinate axes
134 // s stands for scaled coordinates
135 // u stands for unwrapped coordinates
136 // scale_x = 0. if coordinates are cartesian and 1 if fractional (scaled)
137 getline(inStream, buffer);
138 std::vector<string> labels(split(buffer, ' '));
139 for (size_t i = 0; i < labels.size(); i++) {
140 if (labels[i] == "x" || labels[i] == "xu") {
141 x_idx = i;
142 scale_x = 0.;
143 } else if (labels[i] == "xs" || labels[i] == "xsu") {
144 x_idx = i;
145 scale_x = 1.;
146 } else if (labels[i] == "y" || labels[i] == "yu") {
147 y_idx = i;
148 scale_y = 0.;
149 } else if (labels[i] == "ys" || labels[i] == "ysu") {
150 y_idx = i;
151 scale_y = 1.;
152 } else if (labels[i] == "z" || labels[i] == "zu") {
153 z_idx = i;
154 scale_z = 0.;
155 } else if (labels[i] == "zs" || labels[i] == "zsu") {
156 z_idx = i;
157 scale_z = 1.;
158 } else if (labels[i] == "type") {
159 type_idx = i;
160 } else if (labels[i] == "id") {
161 id_idx = i;
162 }
163 }
164
165 if (x_idx == SIZE_MAX || y_idx == SIZE_MAX || z_idx == SIZE_MAX ||
166 type_idx == SIZE_MAX) {
167 appendError("Failed to parse attributes: " + buffer);
168 return false;
169 }
170
171 // Parse atoms
172 for (size_t i = 0; i < numAtoms; ++i) {
173 getline(inStream, buffer);
174 std::vector<string> tokens(split(buffer, ' '));
175
176 if (tokens.size() < labels.size() - 2) {
177 appendError("Not enough tokens in this line: " + buffer);
178 return false;
179 }
180
181 unsigned char atomicNum =
182 lexicalCast<short int>(tokens[type_idx - 2]).value_or(0);
183
184 // If parsed coordinates are fractional, the corresponding unscaling is
185 // done. Else the positions are assigned as parsed.
186 Vector3 pos(
187 (1 - scale_x) * lexicalCast<double>(tokens[x_idx - 2]).value_or(0.0) +
188 scale_x *
189 (x_min + (x_max - x_min) *
190 lexicalCast<double>(tokens[x_idx - 2]).value_or(0.0)),
191 (1 - scale_y) * lexicalCast<double>(tokens[y_idx - 2]).value_or(0.0) +
192 scale_y *
193 (y_min + (y_max - y_min) *
194 lexicalCast<double>(tokens[y_idx - 2]).value_or(0.0)),
195 (1 - scale_z) * lexicalCast<double>(tokens[z_idx - 2]).value_or(0.0) +
196 scale_z *
197 (z_min + (z_max - z_min) *
198 lexicalCast<double>(tokens[z_idx - 2]).value_or(0.0)));
199
200 auto it = atomTypes.find(to_string(atomicNum));
201 if (it == atomTypes.end()) {
202 atomTypes.insert(
203 std::make_pair(to_string(atomicNum), customElementCounter++));
204 it = atomTypes.find(to_string(atomicNum));
205 if (customElementCounter > CustomElementMax) {
206 appendError("Custom element type limit exceeded.");
207 return false;
208 }
209 }
210 Atom newAtom = mol.addAtom(it->second);
211 newAtom.setPosition3d(pos);
212 }
213
214 // Set the custom element map if needed:
215 if (!atomTypes.empty()) {
216 Molecule::CustomElementMap elementMap;
217 for (const auto& atomType : atomTypes) {
218 elementMap.insert(std::make_pair(atomType.second, atomType.first));
219 }
220 mol.setCustomElementMap(elementMap);
221 }
222
223 // Check that all atoms were handled.
224 if (mol.atomCount() != numAtoms) {
225 std::ostringstream errorStream;
226 errorStream << "Error parsing atom at index " << mol.atomCount()
227 << " (line " << 10 + mol.atomCount() << ").\n"
228 << buffer;
229 appendError(errorStream.str());
230 return false;
231 }
232 mol.setCoordinate3d(mol.atomPositions3d(), 0);
233 auto* uc = new UnitCell(Vector3(x_max - x_min, 0, 0),
234 Vector3(tilt_xy, y_max - y_min, 0),
235 Vector3(tilt_xz, tilt_yz, z_max - z_min));
236 if (!uc->isRegular()) {
237 appendError(
238 "'ITEM: BOX BOUNDS' does not give linear-independent lattive vectors");
239 delete uc;
240 return false;
241 }
242 mol.setUnitCell(uc);
243
244 // Do we have an animation?
245 size_t numAtoms2;
246 int coordSet = 1;
247 while (getline(inStream, buffer) && trimmed(buffer) == "ITEM: TIMESTEP") {
248 x_idx = SIZE_MAX;
249 y_idx = SIZE_MAX;
250 z_idx = SIZE_MAX;
251 type_idx = SIZE_MAX;
252 id_idx = SIZE_MAX;
253 x_min = 0;
254 x_max = 0;
255 y_min = 0;
256 y_max = 0;
257 z_min = 0;
258 z_max = 0;
259 tilt_xy = 0;
260 tilt_xz = 0;
261 tilt_yz = 0;
262 scale_x = 0.;
263 scale_y = 0.;
264 scale_z = 0.;
265
266 getline(inStream, buffer);
267 if (!buffer.empty()) {
268 timestep = lexicalCast<size_t>(buffer).value_or(0);
269 mol.setTimeStep(timestep, coordSet);
270 }
271
272 getline(inStream, buffer);
273 buffer = trimmed(buffer);
274 if (buffer != "ITEM: NUMBER OF ATOMS") {
275 appendError("No number of atoms item found.");
276 return false;
277 }
278 getline(inStream, buffer);
279 if (!buffer.empty())
280 numAtoms2 = lexicalCast<size_t>(buffer).value_or(0);
281
282 if (numAtoms2 != numAtoms) {
283 appendError("Number of atoms isn't constant in the trajectory.");
284 }
285
286 // If unit cell is triclinic, tilt factors are needed to define the
287 // supercell
288 getline(inStream, buffer);
289 if (buffer.find("ITEM: BOX BOUNDS xy xz yz") == 0) {
290 // Read x_min, x_max, tiltfactor_xy
291 getline(inStream, buffer);
292 std::vector<string> box_bounds_x(split(buffer, ' '));
293 x_min = lexicalCast<double>(box_bounds_x.at(0)).value_or(0.0);
294 x_max = lexicalCast<double>(box_bounds_x.at(1)).value_or(0.0);
295 tilt_xy = lexicalCast<double>(box_bounds_x.at(2)).value_or(0.0);
296 // Read y_min, y_max, tiltfactor_xz
297 getline(inStream, buffer);
298 std::vector<string> box_bounds_y(split(buffer, ' '));
299 y_min = lexicalCast<double>(box_bounds_y.at(0)).value_or(0.0);
300 y_max = lexicalCast<double>(box_bounds_y.at(1)).value_or(0.0);
301 tilt_xz = lexicalCast<double>(box_bounds_y.at(2)).value_or(0.0);
302 getline(inStream, buffer);
303 // Read z_min, z_max, tiltfactor_yz
304 std::vector<string> box_bounds_z(split(buffer, ' '));
305 z_min = lexicalCast<double>(box_bounds_z.at(0)).value_or(0.0);
306 z_max = lexicalCast<double>(box_bounds_z.at(1)).value_or(0.0);
307 tilt_yz = lexicalCast<double>(box_bounds_z.at(2)).value_or(0.0);
308
309 x_min -= std::min({ tilt_xy, tilt_xz, tilt_xy + tilt_xz, 0.0 });
310 x_max -= std::max({ tilt_xy, tilt_xz, tilt_xy + tilt_xz, 0.0 });
311 y_min -= std::min(tilt_yz, 0.0);
312 y_max -= std::max(tilt_yz, 0.0);
313 }
314
315 // Else if unit cell is orthogonal, tilt factors are zero
316 else if (buffer.find("ITEM: BOX BOUNDS") == 0) {
317 // Read x_min, x_max
318 getline(inStream, buffer);
319 std::vector<string> box_bounds_x(split(buffer, ' '));
320 x_min = lexicalCast<double>(box_bounds_x.at(0)).value_or(0.0);
321 x_max = lexicalCast<double>(box_bounds_x.at(1)).value_or(0.0);
322 // Read y_min, y_max
323 getline(inStream, buffer);
324 std::vector<string> box_bounds_y(split(buffer, ' '));
325 y_min = lexicalCast<double>(box_bounds_y.at(0)).value_or(0.0);
326 y_max = lexicalCast<double>(box_bounds_y.at(1)).value_or(0.0);
327 // Read z_min, z_max
328 getline(inStream, buffer);
329 std::vector<string> box_bounds_z(split(buffer, ' '));
330 z_min = lexicalCast<double>(box_bounds_z.at(0)).value_or(0.0);
331 z_max = lexicalCast<double>(box_bounds_z.at(1)).value_or(0.0);
332 }
333
334 // x,y,z stand for the coordinate axes
335 // s stands for scaled coordinates
336 // u stands for unwrapped coordinates
337 // scale_x = 0. if coordinates are cartesian and 1 if fractional (scaled)
338 getline(inStream, buffer);
339 labels = std::vector<string>(split(buffer, ' '));
340 for (size_t i = 0; i < labels.size(); ++i) {
341 if (labels[i] == "x" || labels[i] == "xu") {
342 x_idx = i;
343 scale_x = 0.;
344 } else if (labels[i] == "xs" || labels[i] == "xsu") {
345 x_idx = i;
346 scale_x = 1.;
347 } else if (labels[i] == "y" || labels[i] == "yu") {
348 y_idx = i;
349 scale_y = 0.;
350 } else if (labels[i] == "ys" || labels[i] == "ysu") {
351 y_idx = i;
352 scale_y = 1.;
353 } else if (labels[i] == "z" || labels[i] == "zu") {
354 z_idx = i;
355 scale_z = 0.;
356 } else if (labels[i] == "zs" || labels[i] == "zsu") {
357 z_idx = i;
358 scale_z = 1.;
359 } else if (labels[i] == "type") {
360 type_idx = i;
361 } else if (labels[i] == "id") {
362 id_idx = i;
363 }
364 }
365
366 if (x_idx == SIZE_MAX || y_idx == SIZE_MAX || z_idx == SIZE_MAX ||
367 type_idx == SIZE_MAX) {
368 appendError("Failed to parse attributes: " + buffer);
369 return false;
370 }
371
372 Array<Vector3> positions;
373 positions.reserve(numAtoms);
374
375 for (size_t i = 0; i < numAtoms; ++i) {
376 getline(inStream, buffer);
377 std::vector<string> tokens(split(buffer, ' '));
378 if (tokens.size() < 5) {
379 appendError("Not enough tokens in this line: " + buffer);
380 return false;
381 }
382 // If parsed coordinates are fractional, the corresponding unscaling is
383 // done. Else the positions are assigned as parsed.
384 Vector3 pos(
385 (1 - scale_x) * lexicalCast<double>(tokens[x_idx - 2]).value_or(0.0) +
386 scale_x *
387 (x_min + (x_max - x_min) *
388 lexicalCast<double>(tokens[x_idx - 2]).value_or(0.0)),
389 (1 - scale_y) * lexicalCast<double>(tokens[y_idx - 2]).value_or(0.0) +
390 scale_y *
391 (y_min + (y_max - y_min) *
392 lexicalCast<double>(tokens[y_idx - 2]).value_or(0.0)),
393 (1 - scale_z) * lexicalCast<double>(tokens[z_idx - 2]).value_or(0.0) +
394 scale_z *
395 (z_min + (z_max - z_min) *
396 lexicalCast<double>(tokens[z_idx - 2]).value_or(0.0)));
397 positions.push_back(pos);
398 }
399
400 mol.setCoordinate3d(positions, coordSet++);
401 auto* uc = new UnitCell(Vector3(x_max - x_min, 0, 0),
402 Vector3(tilt_xy, y_max - y_min, 0),
403 Vector3(tilt_xz, tilt_yz, z_max - z_min));
404 if (!uc->isRegular()) {
405 appendError(
406 "'ITEM: BOX BOUNDS' does not give linear-independent lattive vectors");
407 return false;
408 }
409 mol.setUnitCell(uc);
410 }
411
412 return true;
413 }
414
415 bool LammpsTrajectoryFormat::write(std::ostream&, const Core::Molecule&)
416 {
417 return false;
418 }
419
420 std::vector<std::string> LammpsTrajectoryFormat::fileExtensions() const
421 {
422 std::vector<std::string> ext;
423 ext.emplace_back("dump");
424 ext.emplace_back("lammpstrj");
425 return ext;
426 }
427
428 std::vector<std::string> LammpsTrajectoryFormat::mimeTypes() const
429 {
430 std::vector<std::string> mime;
431 mime.emplace_back("text/lammps");
432 return mime;
433 }
434
435 bool LammpsDataFormat::read(std::istream&, Core::Molecule&)
436 {
437 return false;
438 }
439
440 bool LammpsDataFormat::write(std::ostream& outStream, const Core::Molecule& mol)
441 {
442 Core::Molecule mol2(mol);
443 // enforce right-handed cell
444 // https://docs.lammps.org/read_data.html#header-specification-of-the-simulation-box-size-and-shape
445 CrystalTools::rotateToStandardOrientation(mol2, CrystalTools::TransformAtoms |
446 CrystalTools::RightHanded);
447
448 // Title
449 if (mol2.data("name").toString().length())
450 outStream << mol2.data("name").toString() << std::endl;
451 else
452 outStream << "LAMMPS data file generated by Avogadro" << std::endl;
453
454 std::ostringstream massStream, atomStream, bondStream;
455 double xmin, xmax, ymin, ymax, zmin, zmax;
456 xmin = xmax = ymin = ymax = zmin = zmax = 0.0;
457
458 // Filter out translational duplicates if there's a unit cell
459 Array<Index> uniqueIndices = SpaceGroups::translationalUniqueAtoms(mol2);
460 bool useFiltering = !uniqueIndices.empty();
461 if (!useFiltering) {
462 // No unit cell or no filtering needed - use all atoms
463 for (Index i = 0; i < mol2.atomCount(); ++i)
464 uniqueIndices.push_back(i);
465 }
466
467 // Build mapping from old indices to new indices (for bonds)
468 std::map<Index, Index> indexMap;
469 for (Index newIdx = 0; newIdx < uniqueIndices.size(); ++newIdx) {
470 indexMap[uniqueIndices[newIdx]] = newIdx;
471 }
472
473 size_t numAtoms = uniqueIndices.size();
474 outStream << to_string(numAtoms) << " atoms\n";
475
476 // Count bonds where both atoms are in the unique set
477 size_t numBonds = 0;
478 for (Index i = 0; i < mol2.bondCount(); ++i) {
479 Bond b = mol2.bond(i);
480 if (indexMap.count(b.atom1().index()) && indexMap.count(b.atom2().index()))
481 ++numBonds;
482 }
483 outStream << to_string(numBonds) << " bonds\n";
484
485 // A map of atomic symbols to their quantity.
486 size_t idx = 1;
487 std::map<unsigned char, size_t> composition;
488 for (Index atomIdx : uniqueIndices) {
489 unsigned char atomicNumber = mol2.atomicNumber(atomIdx);
490 if (composition.find(atomicNumber) == composition.end()) {
491 composition[atomicNumber] = idx++;
492 }
493 }
494
495 outStream << composition.size() << " atom types\n";
496
497 // Masses
498 massStream << "Masses\n\n";
499 auto iter = composition.begin();
500 while (iter != composition.end()) {
501 massStream << iter->second << " " << Elements::mass(iter->first) << "\n";
502 ++iter;
503 }
504 massStream << std::endl << std::endl << std::endl;
505
506 if (numAtoms) {
507 // Atomic coordinates
508 atomStream << "Atoms\n\n";
509 Index outputIdx = 0;
510 for (Index atomIdx : uniqueIndices) {
511 Atom atom = mol2.atom(atomIdx);
512 if (!atom.isValid()) {
513 appendError("Internal error: Atom invalid.");
514 return false;
515 }
516 Vector3 coords = atom.position3d();
517 if (outputIdx == 0) {
518 xmin = coords[0];
519 xmax = coords[0];
520 ymin = coords[1];
521 ymax = coords[1];
522 zmin = coords[2];
523 zmax = coords[2];
524 } else {
525 xmin = std::min(coords[0], xmin);
526 xmax = std::max(coords[0], xmax);
527 ymin = std::min(coords[1], ymin);
528 ymax = std::max(coords[1], ymax);
529 zmin = std::min(coords[2], zmin);
530 zmax = std::max(coords[2], zmax);
531 }
532
533 const unsigned int lineSize = 256;
534 char atomline[lineSize];
535 snprintf(atomline, lineSize - 1, "%-*d %d %10f %10f %10f\n",
536 static_cast<int>(log(numAtoms)) + 1,
537 static_cast<int>(outputIdx + 1),
538 static_cast<int>(composition[atom.atomicNumber()]), coords.x(),
539 coords.y(), coords.z());
540 atomStream << atomline;
541 ++outputIdx;
542 }
543
544 atomStream << std::endl << std::endl;
545 }
546
547 if (numBonds) {
548 // Bonds - only include bonds where both atoms are in the unique set
549 std::map<std::pair<unsigned char, unsigned char>, int> bondIds;
550 int bondItr = 1;
551 bondStream << "Bonds\n\n";
552 const unsigned int lineSize = 256;
553 Index bondOutputIdx = 0;
554 for (Index i = 0; i < mol2.bondCount(); ++i) {
555 Bond b = mol2.bond(i);
556 // Skip bonds where either atom was filtered out
557 auto it1 = indexMap.find(b.atom1().index());
558 auto it2 = indexMap.find(b.atom2().index());
559 if (it1 == indexMap.end() || it2 == indexMap.end())
560 continue;
561
562 // Use remapped indices (1-based for output)
563 Index newIdx1 = it1->second + 1;
564 Index newIdx2 = it2->second + 1;
565
566 char bondline[lineSize];
567 if (bondIds.find(std::make_pair(b.atom1().atomicNumber(),
568 b.atom2().atomicNumber())) !=
569 bondIds.end()) {
570 snprintf(bondline, lineSize - 1, "%-*d %7d %7d %7d\n",
571 static_cast<int>(log(numAtoms) + 1),
572 static_cast<int>(bondOutputIdx + 1),
573 bondIds[std::make_pair(b.atom1().atomicNumber(),
574 b.atom2().atomicNumber())],
575 static_cast<int>(newIdx1), static_cast<int>(newIdx2));
576 bondStream << bondline;
577 } else if (bondIds.find(std::make_pair(b.atom2().atomicNumber(),
578 b.atom1().atomicNumber())) !=
579 bondIds.end()) {
580 snprintf(bondline, lineSize - 1, "%-*d %7d %7d %7d\n",
581 static_cast<int>(log(numAtoms) + 1),
582 static_cast<int>(bondOutputIdx + 1),
583 bondIds[std::make_pair(b.atom1().atomicNumber(),
584 b.atom2().atomicNumber())],
585 static_cast<int>(newIdx2), static_cast<int>(newIdx1));
586 bondStream << bondline;
587 } else {
588 bondIds.insert(std::make_pair(
589 std::make_pair(b.atom1().atomicNumber(), b.atom2().atomicNumber()),
590 bondItr++));
591 snprintf(bondline, lineSize - 1, "%-*d %7d %7d %7d\n",
592 static_cast<int>(log(numAtoms) + 1),
593 static_cast<int>(bondOutputIdx + 1),
594 bondIds[std::make_pair(b.atom1().atomicNumber(),
595 b.atom2().atomicNumber())],
596 static_cast<int>(newIdx1), static_cast<int>(newIdx2));
597 bondStream << bondline;
598 }
599 ++bondOutputIdx;
600 }
601 }
602
603 UnitCell* unitcell = mol2.unitCell();
604 const unsigned int lineSize = 256;
605 char simBoxBlock[lineSize];
606 if (unitcell) {
607 const Matrix3& mat = unitcell->cellMatrix().transpose();
608 snprintf(simBoxBlock, lineSize - 1,
609 "%10f %10f xlo xhi\n%10f %10f ylo yhi\n%10f %10f zlo zhi\n%10f "
610 "%10f %10f xy xz yz",
611 0.0, mat(0, 0), 0.0, mat(1, 1), 0.0, mat(2, 2), mat(1, 0),
612 mat(2, 0), mat(2, 1));
613 outStream << simBoxBlock;
614 } else {
615 snprintf(simBoxBlock, lineSize - 1,
616 "%10f %10f xlo xhi\n%10f %10f ylo yhi\n%10f %10f zlo zhi\n%10f "
617 "%10f %10f xy xz yz",
618 xmin - 0.5, xmax - 0.5, ymin - 0.5, ymax - 0.5, zmin - 0.5,
619 zmax - 0.5, 0.0, 0.0, 0.0);
620 outStream << simBoxBlock;
621 }
622 outStream << std::endl << std::endl << std::endl;
623 outStream << massStream.str();
624 outStream << atomStream.str();
625 outStream << bondStream.str();
626
627 return true;
628 }
629
630 std::vector<std::string> LammpsDataFormat::fileExtensions() const
631 {
632 std::vector<std::string> ext;
633 ext.emplace_back("lmpdat");
634 return ext;
635 }
636
637 std::vector<std::string> LammpsDataFormat::mimeTypes() const
638 {
639 std::vector<std::string> mime;
640 mime.emplace_back("N/A");
641 return mime;
642 }
643
644 } // namespace Avogadro::Io