Debian Codegraph @

avogadro/core/coordinateblockgenerator.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 "coordinateblockgenerator.h"
7
8 #include <avogadro/core/atom.h>
9 #include <avogadro/core/avogadrocore.h>
10 #include <avogadro/core/elements.h>
11 #include <avogadro/core/molecule.h>
12 #include <avogadro/core/unitcell.h>
13
14 #include <iomanip>
15
16 namespace Avogadro::Core {
17
18 std::string CoordinateBlockGenerator::generateCoordinateBlock()
19 {
20 if (!m_molecule)
21 return {};
22
23 // Reset stream.
24 m_stream.str("");
25 m_stream.clear();
26
27 // Create/cache some iterators for the specification string.
28 std::string::const_iterator it;
29 const std::string::const_iterator begin = m_specification.begin();
30 const std::string::const_iterator end = m_specification.end();
31
32 // Check the spec to see if certain items are needed.
33 bool needElementSymbol(false);
34 bool needElementName(false);
35 bool needPosition(false);
36 bool needFractionalPosition(false);
37 for (it = begin; it != end; ++it) {
38 switch (*it) {
39 case 'S':
40 case 'L':
41 needElementSymbol = true;
42 break;
43 case 'N':
44 needElementName = true;
45 break;
46 case 'x':
47 case 'y':
48 case 'z':
49 needPosition = true;
50 break;
51 case 'a':
52 case 'b':
53 case 'c':
54 needFractionalPosition = true;
55 break;
56 }
57 }
58
59 // Variables for loops below
60 const Index numAtoms = m_molecule->atomCount();
61 Atom atom;
62 unsigned char atomicNumber;
63 const char* symbol = "\0";
64 const char* name = "\0";
65 Vector3 pos3d = Vector3::Zero();
66 Vector3 fpos3d = Vector3::Zero();
67 const UnitCell* cell =
68 needFractionalPosition ? molecule()->unitCell() : nullptr;
69
70 // widths/precisions
71 enum
72 {
73 atomicNumberPrecision = 0,
74 atomicNumberWidth = 3,
75 atomicLabelWidth = 8, // 3 element symbol + 5 index
76 coordinatePrecision = 6,
77 coordinateWidth = 11,
78 elementNameWidth = 13, // Currently the longest element name
79 elementSymbolWidth = 3,
80 gamessAtomicNumberPrecision = 1,
81 gamessAtomicNumberWidth = 5
82 };
83 const int indexWidth(
84 static_cast<int>(std::log10(static_cast<float>(numAtoms))) + 1);
85
86 // Use fixed number format.
87 m_stream << std::fixed;
88
89 // Count the number for each element
90 std::vector<unsigned int> elementCounts(Elements::elementCount(), 0);
91
92 // Iterate through the atoms
93 for (Index atomI = 0; atomI < numAtoms; ++atomI) {
94 atom = m_molecule->atom(atomI);
95 atomicNumber = atom.atomicNumber();
96 elementCounts[atomicNumber]++;
97 if (needElementSymbol)
98 symbol = Core::Elements::symbol(atomicNumber);
99 if (needElementName)
100 name = Core::Elements::name(atomicNumber);
101 if (needPosition)
102 pos3d = atom.position3d();
103 if (needFractionalPosition)
104 fpos3d = cell ? cell->toFractional(atom.position3d()) : Vector3::Zero();
105
106 switch (m_distanceUnit) {
107 case Bohr:
108 pos3d *= ANGSTROM_TO_BOHR_F;
109 break;
110 default:
111 case Angstrom:
112 break;
113 }
114
115 for (it = begin; it != end; ++it) {
116 switch (*it) {
117 case '_':
118 // Space character. If we are not at the end of the spec, a space will
119 // be added by default after the switch clause. If we are at the end,
120 // add a space before the newline that will be added.
121 if (it + 1 == end)
122 m_stream << std::setw(1) << " ";
123 break;
124 case '#':
125 m_stream << std::left << std::setw(indexWidth)
126 << static_cast<int>(atomI + 1);
127 break;
128 case 'Z':
129 m_stream << std::left << std::setw(atomicNumberWidth)
130 << std::setprecision(atomicNumberPrecision)
131 << static_cast<int>(atomicNumber);
132 break;
133 case 'G':
134 m_stream << std::right << std::setw(gamessAtomicNumberWidth)
135 << std::setprecision(gamessAtomicNumberPrecision)
136 << static_cast<float>(atomicNumber);
137 break;
138 case 'S':
139 m_stream << std::left << std::setw(elementSymbolWidth) << symbol;
140 break;
141 case 'L':
142 m_stream << std::left << symbol << elementCounts[atomicNumber] << " ";
143 break;
144 case 'N':
145 m_stream << std::left << std::setw(elementNameWidth) << name;
146 break;
147 case 'x':
148 m_stream << std::right << std::setw(coordinateWidth)
149 << std::setprecision(coordinatePrecision) << pos3d.x();
150 break;
151 case 'y':
152 m_stream << std::right << std::setw(coordinateWidth)
153 << std::setprecision(coordinatePrecision) << pos3d.y();
154 break;
155 case 'z':
156 m_stream << std::right << std::setw(coordinateWidth)
157 << std::setprecision(coordinatePrecision) << pos3d.z();
158 break;
159 case '0':
160 m_stream << std::left << std::setw(1) << 0;
161 break;
162 case '1':
163 m_stream << std::left << std::setw(1) << 1;
164 break;
165 case 'a':
166 if (cell) {
167 m_stream << std::right << std::setw(coordinateWidth)
168 << std::setprecision(coordinatePrecision) << fpos3d.x();
169 } else {
170 m_stream << std::right << std::setw(coordinateWidth) << "N/A";
171 }
172 break;
173 case 'b':
174 if (cell) {
175 m_stream << std::right << std::setw(coordinateWidth)
176 << std::setprecision(coordinatePrecision) << fpos3d.y();
177 } else {
178 m_stream << std::right << std::setw(coordinateWidth) << "N/A";
179 }
180 break;
181 case 'c':
182 if (cell) {
183 m_stream << std::right << std::setw(coordinateWidth)
184 << std::setprecision(coordinatePrecision) << fpos3d.z();
185 } else {
186 m_stream << std::right << std::setw(coordinateWidth) << "N/A";
187 }
188 break;
189 } // end switch
190
191 // Prepare for next value. Push a space into the output stream if we are
192 // not at the end of the line, or a newline if we are.
193 m_stream << std::setw(1) << (it + 1 != end ? ' ' : '\n');
194 } // end spec char
195 } // end for atom
196
197 return m_stream.str();
198 }
199
200 } // namespace Avogadro::Core