Debian Codegraph @

avogadro/core/avospglib.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 "avospglib.h"
7
8 #include "array.h"
9 #include "matrix.h"
10 #include "molecule.h"
11 #include "unitcell.h"
12 #include "vector.h"
13
14 #include <array>
15 #include <iostream>
16 #include <vector>
17
18 extern "C"
19 {
20 #include "spglib.h"
21 }
22
23 namespace Avogadro::Core {
24
25 unsigned short AvoSpglib::getHallNumber(Molecule& mol, double cartTol)
26 {
27 if (!mol.unitCell())
28 return 0;
29
30 const UnitCell* uc = mol.unitCell();
31 Matrix3 cellMat = uc->cellMatrix();
32
33 double lattice[3][3];
34 // Spglib expects column vectors
35 for (Index i = 0; i < 3; ++i) {
36 for (Index j = 0; j < 3; ++j) {
37 lattice[i][j] = cellMat(i, j);
38 }
39 }
40
41 Index numAtoms = mol.atomCount();
42 const Array<unsigned char>& atomicNums = mol.atomicNumbers();
43 const Array<Vector3>& pos = mol.atomPositions3d();
44
45 // Use vectors to filter out translational duplicates
46 std::vector<std::array<double, 3>> positionsVec;
47 std::vector<int> typesVec;
48 positionsVec.reserve(numAtoms);
49 typesVec.reserve(numAtoms);
50
51 // Positions need to be in fractional coordinates
52 for (Index i = 0; i < numAtoms; ++i) {
53 Vector3 fracCoords = uc->toFractional(pos[i]);
54 // Skip atoms with any fractional coordinate ~1.0
55 // (translational duplicates of other atoms ~0.0
56 if (std::abs(fracCoords[0] - 1.0) < 1e-8 ||
57 std::abs(fracCoords[1] - 1.0) < 1e-8 ||
58 std::abs(fracCoords[2] - 1.0) < 1e-8)
59 continue;
60 positionsVec.push_back({ fracCoords[0], fracCoords[1], fracCoords[2] });
61 typesVec.push_back(atomicNums[i]);
62 }
63
64 Index filteredNumAtoms = positionsVec.size();
65 if (filteredNumAtoms == 0) {
66 std::cerr << "No valid atoms found for spacegroup determination.\n";
67 return 0;
68 }
69
70 // Convert to C-style arrays for spglib
71 auto* positions = new double[filteredNumAtoms][3];
72 int* types = new int[filteredNumAtoms];
73 for (Index i = 0; i < filteredNumAtoms; ++i) {
74 positions[i][0] = positionsVec[i][0];
75 positions[i][1] = positionsVec[i][1];
76 positions[i][2] = positionsVec[i][2];
77 types[i] = typesVec[i];
78 }
79
80 SpglibDataset* data =
81 spg_get_dataset(lattice, positions, types, filteredNumAtoms, cartTol);
82
83 if (!data) {
84 std::cerr << "Cannot determine spacegroup.\n";
85 delete[] positions;
86 delete[] types;
87 return 0;
88 }
89
90 unsigned short hallNumber = data->hall_number;
91
92 // Cleanup time
93 spg_free_dataset(data);
94 delete[] positions;
95 delete[] types;
96
97 mol.setHallNumber(hallNumber);
98 return hallNumber;
99 }
100
101 bool AvoSpglib::reduceToPrimitive(Molecule& mol, double cartTol)
102 {
103 return standardizeCell(mol, cartTol, true, false);
104 }
105
106 bool AvoSpglib::conventionalizeCell(Molecule& mol, double cartTol)
107 {
108 return standardizeCell(mol, cartTol, false, true);
109 }
110
111 bool AvoSpglib::symmetrize(Molecule& mol, double cartTol)
112 {
113 return standardizeCell(mol, cartTol, true, true);
114 }
115
116 bool AvoSpglib::standardizeCell(Molecule& mol, double cartTol, bool toPrimitive,
117 bool idealize)
118 {
119 if (!mol.unitCell())
120 return false;
121
122 const UnitCell* uc = mol.unitCell();
123 Matrix3 cellMat = uc->cellMatrix();
124
125 double lattice[3][3];
126 // Spglib expects column vectors
127 for (Index i = 0; i < 3; ++i) {
128 for (Index j = 0; j < 3; ++j) {
129 lattice[i][j] = cellMat(i, j);
130 }
131 }
132
133 Index numAtoms = mol.atomCount();
134 const Array<unsigned char>& atomicNums = mol.atomicNumbers();
135 const Array<Vector3>& pos = mol.atomPositions3d();
136
137 // Use vectors to filter out translational duplicates
138 // (atoms at fracCoord ~1.0)
139 std::vector<std::array<double, 3>> positionsVec;
140 std::vector<int> typesVec;
141 positionsVec.reserve(numAtoms);
142 typesVec.reserve(numAtoms);
143
144 // Positions need to be in fractional coordinates
145 for (Index i = 0; i < numAtoms; ++i) {
146 Vector3 fracCoords = uc->toFractional(pos[i]);
147 // Skip atoms with any fractional coordinate ~1.0
148 // (translational duplicates of other atoms ~0.0
149 if (std::abs(fracCoords[0] - 1.0) < 1e-8 ||
150 std::abs(fracCoords[1] - 1.0) < 1e-8 ||
151 std::abs(fracCoords[2] - 1.0) < 1e-8)
152 continue;
153 positionsVec.push_back({ fracCoords[0], fracCoords[1], fracCoords[2] });
154 typesVec.push_back(atomicNums[i]);
155 }
156
157 Index filteredNumAtoms = positionsVec.size();
158 if (filteredNumAtoms == 0)
159 return false;
160
161 // spg_standardize_cell() can cause the number of atoms to increase by
162 // as much as 4x if toPrimitive is false.
163 // So, we must make these arrays at least 4x the number of atoms.
164 // If toPrimitive is true, then we will just use the number of atoms.
165 // See http://atztogo.github.io/spglib/api.html#spg-standardize-cell
166 int numAtomsMultiplier = toPrimitive ? 1 : 4;
167 auto* positions = new double[filteredNumAtoms * numAtomsMultiplier][3];
168 int* types = new int[filteredNumAtoms * numAtomsMultiplier];
169
170 // Copy filtered data to C-style arrays for spglib
171 for (Index i = 0; i < filteredNumAtoms; ++i) {
172 positions[i][0] = positionsVec[i][0];
173 positions[i][1] = positionsVec[i][1];
174 positions[i][2] = positionsVec[i][2];
175 types[i] = typesVec[i];
176 }
177
178 // Run the spglib algorithm
179 Index newNumAtoms =
180 spg_standardize_cell(lattice, positions, types, filteredNumAtoms,
181 toPrimitive, !idealize, cartTol);
182
183 // If 0 is returned, the algorithm failed.
184 if (newNumAtoms == 0) {
185 delete[] positions;
186 delete[] types;
187 return false;
188 }
189
190 // Let's create a new molecule with the information
191 Molecule newMol;
192
193 // First, we will make the unit cell
194 Matrix3 newCellMat;
195 for (Index i = 0; i < 3; ++i) {
196 for (Index j = 0; j < 3; ++j) {
197 newCellMat(i, j) = lattice[i][j];
198 }
199 }
200
201 auto* newCell = new UnitCell(newCellMat);
202 newMol.setUnitCell(newCell);
203
204 // Next, add in the atoms
205 for (Index i = 0; i < newNumAtoms; ++i) {
206 Atom newAtom = newMol.addAtom(types[i]);
207 Vector3 newAtomPos(positions[i][0], positions[i][1], positions[i][2]);
208 // We must convert it back to cartesian before adding it
209 newAtom.setPosition3d(newCell->toCartesian(newAtomPos));
210 }
211
212 delete[] positions;
213 delete[] types;
214
215 // Set the new molecule
216 mol = newMol;
217
218 // Set the space group in the molecule
219 getHallNumber(mol, cartTol);
220 return true;
221 }
222
223 } // namespace Avogadro::Core