| 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 "crystaltools.h" |
| 7 |
|
| 8 |
#include "molecule.h" |
| 9 |
#include "unitcell.h" |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
#include <iostream> |
| 13 |
|
| 14 |
namespace Avogadro::Core { |
| 15 |
|
| 16 |
namespace { |
| 17 |
struct WrapAtomsToCellFunctor |
| 18 |
{ |
| 19 |
const UnitCell& unitCell; |
| 20 |
|
| 21 |
WrapAtomsToCellFunctor(Molecule& molecule) : unitCell(*molecule.unitCell()) {} |
| 22 |
|
| 23 |
void operator()(Vector3& pos) { unitCell.wrapCartesian(pos, pos); } |
| 24 |
}; |
| 25 |
} // namespace |
| 26 |
|
| 27 |
bool CrystalTools::wrapAtomsToUnitCell(Molecule& molecule) |
| 28 |
{ |
| 29 |
if (!molecule.unitCell()) |
| 30 |
return false; |
| 31 |
|
| 32 |
// remove any bonds first - otherwise they may wrap |
| 33 |
// across the unit cell strangely |
| 34 |
molecule.clearBonds(); |
| 35 |
|
| 36 |
std::for_each(molecule.atomPositions3d().begin(), |
| 37 |
molecule.atomPositions3d().end(), |
| 38 |
WrapAtomsToCellFunctor(molecule)); |
| 39 |
|
| 40 |
molecule.perceiveBondsSimple(); |
| 41 |
molecule.perceiveBondOrders(); |
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
bool CrystalTools::rotateToStandardOrientation(Molecule& molecule, Options opts) |
| 46 |
{ |
| 47 |
if (!molecule.unitCell()) |
| 48 |
return false; |
| 49 |
|
| 50 |
const UnitCell& cell = *molecule.unitCell(); |
| 51 |
|
| 52 |
const Matrix3& before = cell.cellMatrix(); |
| 53 |
|
| 54 |
// Extract vector components: |
| 55 |
const Real& x1 = before(0, 0); |
| 56 |
const Real& y1 = before(1, 0); |
| 57 |
const Real& z1 = before(2, 0); |
| 58 |
|
| 59 |
const Real& x2 = before(0, 1); |
| 60 |
const Real& y2 = before(1, 1); |
| 61 |
const Real& z2 = before(2, 1); |
| 62 |
|
| 63 |
const Real& x3 = before(0, 2); |
| 64 |
const Real& y3 = before(1, 2); |
| 65 |
const Real& z3 = before(2, 2); |
| 66 |
|
| 67 |
// Cache some frequently used values: |
| 68 |
// Length of v1 |
| 69 |
const Real L1 = std::hypot(x1, y1, z1); |
| 70 |
// Squared norm of v1's yz projection |
| 71 |
const Real sqrdnorm1yz = y1 * y1 + z1 * z1; |
| 72 |
// Squared norm of v2's yz projection |
| 73 |
const Real sqrdnorm2yz = y2 * y2 + z2 * z2; |
| 74 |
// Determinant of v1 and v2's projections in yz plane |
| 75 |
const Real detv1v2yz = y2 * z1 - y1 * z2; |
| 76 |
// Scalar product of v1 and v2's projections in yz plane |
| 77 |
const Real dotv1v2yz = y1 * y2 + z1 * z2; |
| 78 |
|
| 79 |
// Used for denominators, since we want to check that they are |
| 80 |
// sufficiently far from 0 to keep things reasonable: |
| 81 |
Real denom; |
| 82 |
const Real DENOM_TOL = 1e-5; |
| 83 |
|
| 84 |
// Create target matrix, fill with zeros |
| 85 |
Matrix3 newMat(Matrix3::Zero()); |
| 86 |
|
| 87 |
// Set components of new v1: |
| 88 |
newMat(0, 0) = L1; |
| 89 |
|
| 90 |
// Set components of new v2: |
| 91 |
denom = L1; |
| 92 |
if (fabs(denom) < DENOM_TOL) |
| 93 |
return false; |
| 94 |
|
| 95 |
newMat(0, 1) = (x1 * x2 + y1 * y2 + z1 * z2) / denom; |
| 96 |
|
| 97 |
newMat(1, 1) = sqrt(x2 * x2 * sqrdnorm1yz + detv1v2yz * detv1v2yz - |
| 98 |
2 * x1 * x2 * dotv1v2yz + x1 * x1 * sqrdnorm2yz) / |
| 99 |
denom; |
| 100 |
|
| 101 |
// Set components of new v3 |
| 102 |
newMat(0, 2) = (x1 * x3 + y1 * y3 + z1 * z3) / denom; |
| 103 |
|
| 104 |
denom = L1 * L1 * newMat(1, 1); |
| 105 |
if (fabs(denom) < DENOM_TOL) |
| 106 |
return false; |
| 107 |
|
| 108 |
newMat(1, 2) = (x1 * x1 * (y2 * y3 + z2 * z3) + |
| 109 |
x2 * (x3 * sqrdnorm1yz - x1 * (y1 * y3 + z1 * z3)) + |
| 110 |
detv1v2yz * (y3 * z1 - y1 * z3) - x1 * x3 * dotv1v2yz) / |
| 111 |
denom; |
| 112 |
|
| 113 |
denom = L1 * newMat(1, 1); |
| 114 |
if (fabs(denom) < DENOM_TOL) |
| 115 |
return false; |
| 116 |
|
| 117 |
// Numerator is determinant of original cell: |
| 118 |
newMat(2, 2) = before.determinant() / denom; |
| 119 |
|
| 120 |
if (opts & RightHanded && newMat(2, 2) < 0.0) |
| 121 |
newMat(2, 2) *= -1.0; |
| 122 |
|
| 123 |
return setCellMatrix(molecule, newMat, opts & TransformAtoms); |
| 124 |
} |
| 125 |
|
| 126 |
bool CrystalTools::setVolume(Molecule& molecule, Real newVolume, Options opts) |
| 127 |
{ |
| 128 |
if (!molecule.unitCell()) |
| 129 |
return false; |
| 130 |
|
| 131 |
const UnitCell& cell = *molecule.unitCell(); |
| 132 |
|
| 133 |
const Real scaleFactor = |
| 134 |
std::pow(newVolume / cell.volume(), static_cast<Real>(1.0 / 3.0)); |
| 135 |
|
| 136 |
const Matrix3 newMatrix(cell.cellMatrix() * scaleFactor); |
| 137 |
|
| 138 |
return setCellMatrix(molecule, newMatrix, opts & TransformAtoms); |
| 139 |
} |
| 140 |
|
| 141 |
// A collection of fuzzy comparison operators used in the niggli reduction |
| 142 |
// algorithm: |
| 143 |
namespace { |
| 144 |
const double FUZZY_TOL(1e-5); |
| 145 |
template <typename T> |
| 146 |
bool fuzzyLessThan(T v1, T v2, T prec = static_cast<T>(FUZZY_TOL)) |
| 147 |
{ |
| 148 |
return (v1 < (v2 - prec)); |
| 149 |
} |
| 150 |
|
| 151 |
template <typename T> |
| 152 |
bool fuzzyGreaterThan(T v1, T v2, T prec = static_cast<T>(FUZZY_TOL)) |
| 153 |
{ |
| 154 |
return (v2 < (v1 - prec)); |
| 155 |
} |
| 156 |
|
| 157 |
template <typename T> |
| 158 |
bool fuzzyEqual(T v1, T v2, T prec = static_cast<T>(FUZZY_TOL)) |
| 159 |
{ |
| 160 |
return (!(fuzzyLessThan(v1, v2, prec) || fuzzyGreaterThan(v1, v2, prec))); |
| 161 |
} |
| 162 |
|
| 163 |
template <typename T> |
| 164 |
bool fuzzyNotEqual(T v1, T v2, T prec = static_cast<T>(FUZZY_TOL)) |
| 165 |
{ |
| 166 |
return (!(fuzzyEqual(v1, v2, prec))); |
| 167 |
} |
| 168 |
|
| 169 |
template <typename T> |
| 170 |
bool fuzzyLessThanEq(T v1, T v2, T prec = static_cast<T>(FUZZY_TOL)) |
| 171 |
{ |
| 172 |
return (!fuzzyGreaterThan(v1, v2, prec)); |
| 173 |
} |
| 174 |
|
| 175 |
template <typename T> |
| 176 |
bool fuzzyGreaterThanEq(T v1, T v2, T prec = static_cast<T>(FUZZY_TOL)) |
| 177 |
{ |
| 178 |
return (!lt(v1, v2, prec)); |
| 179 |
} |
| 180 |
|
| 181 |
template <typename T> |
| 182 |
T niggliSign(T v) |
| 183 |
{ |
| 184 |
// consider 0 to be positive |
| 185 |
return (v >= static_cast<T>(0.)) ? static_cast<T>(1.0) : static_cast<T>(-1.0); |
| 186 |
} |
| 187 |
|
| 188 |
template <typename T> |
| 189 |
T niggliRound(T v, T dec) |
| 190 |
{ |
| 191 |
const T shift = std::pow(10.0, dec); |
| 192 |
const T shifted = v * shift; |
| 193 |
return std::floor(shifted + 0.5) / shift; |
| 194 |
} |
| 195 |
} // namespace |
| 196 |
|
| 197 |
bool CrystalTools::niggliReduce(Molecule& molecule, Options opts) |
| 198 |
{ |
| 199 |
if (!molecule.unitCell()) |
| 200 |
return false; |
| 201 |
|
| 202 |
UnitCell& cell = *molecule.unitCell(); |
| 203 |
|
| 204 |
// Maximum number of iterations |
| 205 |
const unsigned int maxIterations = 1000; |
| 206 |
|
| 207 |
// Get cell parameters in storage units, convert deg->rad |
| 208 |
Real a = cell.a(); |
| 209 |
Real b = cell.b(); |
| 210 |
Real c = cell.c(); |
| 211 |
Real alpha = cell.alpha(); |
| 212 |
Real beta = cell.beta(); |
| 213 |
Real gamma = cell.gamma(); |
| 214 |
|
| 215 |
// Compute characteristic (step 0) |
| 216 |
Real A = a * a; |
| 217 |
Real B = b * b; |
| 218 |
Real C = c * c; |
| 219 |
Real xi = 2 * b * c * std::cos(alpha); |
| 220 |
Real eta = 2 * a * c * std::cos(beta); |
| 221 |
Real zeta = 2 * a * b * std::cos(gamma); |
| 222 |
|
| 223 |
// Return value. |
| 224 |
bool ret = false; |
| 225 |
|
| 226 |
// Comparison tolerance. |
| 227 |
Real tol = FUZZY_TOL * std::pow(a * b * c, static_cast<Real>(1.0 / 3.0)); |
| 228 |
|
| 229 |
// Initialize change of basis matrices: |
| 230 |
// |
| 231 |
// Although the reduction algorithm produces quantities directly |
| 232 |
// relatable to a,b,c,alpha,beta,gamma, we will calculate a change |
| 233 |
// of basis matrix to use instead, and discard A, B, C, xi, eta, |
| 234 |
// zeta. By multiplying the change of basis matrix against the |
| 235 |
// current cell matrix, we avoid the problem of handling the |
| 236 |
// orientation matrix already present in the cell. The inverse of |
| 237 |
// this matrix can also be used later to convert the atomic |
| 238 |
// positions. |
| 239 |
|
| 240 |
// tmpMat is used to build other matrices |
| 241 |
Matrix3 tmpMat; |
| 242 |
|
| 243 |
// Cache static matrices: |
| 244 |
|
| 245 |
// Swap x, y (Used in Step 1). Negatives ensure proper sign of final |
| 246 |
// determinant. |
| 247 |
tmpMat << 0, -1, 0, -1, 0, 0, 0, 0, -1; |
| 248 |
const Matrix3 C1(tmpMat); |
| 249 |
// Swap y, z (Used in Step 2). Negatives ensure proper sign of final |
| 250 |
// determinant |
| 251 |
tmpMat << -1, 0, 0, 0, 0, -1, 0, -1, 0; |
| 252 |
const Matrix3 C2(tmpMat); |
| 253 |
// For step 8: |
| 254 |
tmpMat << 1, 0, 1, 0, 1, 1, 0, 0, 1; |
| 255 |
const Matrix3 C8(tmpMat); |
| 256 |
|
| 257 |
// initial change of basis matrix |
| 258 |
tmpMat << 1, 0, 0, 0, 1, 0, 0, 0, 1; |
| 259 |
Matrix3 cob(tmpMat); |
| 260 |
|
| 261 |
// Enable debugging output here: |
| 262 |
/* |
| 263 |
#define NIGGLI_DEBUG(step) \ |
| 264 |
std::cout << iter << " " << step << " " << A << " " << B << " " << C \ |
| 265 |
<< " " << xi << " " << eta << " " << zeta << std::endl; |
| 266 |
*/ |
| 267 |
#define NIGGLI_DEBUG(step) |
| 268 |
|
| 269 |
// Allow Argument Dependent Lookup for swap |
| 270 |
using std::swap; |
| 271 |
|
| 272 |
// Perform iterative reduction: |
| 273 |
unsigned int iter; |
| 274 |
for (iter = 0; iter < maxIterations; ++iter) { |
| 275 |
// Step 1: |
| 276 |
if (fuzzyGreaterThan(A, B, tol) || |
| 277 |
(fuzzyEqual(A, B, tol) && |
| 278 |
fuzzyGreaterThan(std::fabs(xi), std::fabs(eta), tol))) { |
| 279 |
cob *= C1; |
| 280 |
swap(A, B); |
| 281 |
swap(xi, eta); |
| 282 |
NIGGLI_DEBUG(1); |
| 283 |
} |
| 284 |
|
| 285 |
// Step 2: |
| 286 |
if (fuzzyGreaterThan(B, C, tol) || |
| 287 |
(fuzzyEqual(B, C, tol) && |
| 288 |
fuzzyGreaterThan(std::fabs(eta), std::fabs(zeta), tol))) { |
| 289 |
cob *= C2; |
| 290 |
swap(B, C); |
| 291 |
swap(eta, zeta); |
| 292 |
NIGGLI_DEBUG(2); |
| 293 |
continue; |
| 294 |
} |
| 295 |
|
| 296 |
// Step 3: |
| 297 |
// Use exact comparisons in steps 3 and 4. |
| 298 |
if (xi * eta * zeta > 0) { |
| 299 |
// Update change of basis matrix: |
| 300 |
tmpMat << niggliSign(xi), 0, 0, 0, niggliSign(eta), 0, 0, 0, |
| 301 |
niggliSign(zeta); |
| 302 |
cob *= tmpMat; |
| 303 |
|
| 304 |
// Update characteristic |
| 305 |
xi = std::fabs(xi); |
| 306 |
eta = std::fabs(eta); |
| 307 |
zeta = std::fabs(zeta); |
| 308 |
NIGGLI_DEBUG(3); |
| 309 |
++iter; |
| 310 |
} |
| 311 |
|
| 312 |
// Step 4: |
| 313 |
// Use exact comparisons for steps 3 and 4 |
| 314 |
else { // either step 3 or 4 should run |
| 315 |
// Update change of basis matrix: |
| 316 |
Real* p = nullptr; |
| 317 |
Real i = 1; |
| 318 |
Real j = 1; |
| 319 |
Real k = 1; |
| 320 |
if (xi > 0) { |
| 321 |
i = -1; |
| 322 |
} else if (!(xi < 0)) { |
| 323 |
p = &i; |
| 324 |
} |
| 325 |
if (eta > 0) { |
| 326 |
j = -1; |
| 327 |
} else if (!(eta < 0)) { |
| 328 |
p = &j; |
| 329 |
} |
| 330 |
if (zeta > 0) { |
| 331 |
k = -1; |
| 332 |
} else if (!(zeta < 0)) { |
| 333 |
p = &k; |
| 334 |
} |
| 335 |
if (i * j * k < 0) { |
| 336 |
if (!p) { |
| 337 |
// This was originally an error message displayed in a dialog: |
| 338 |
// Niggli-reduction failed. The input structure's lattice is confusing |
| 339 |
// the Niggli-reduction algorithm. Try making a small perturbation |
| 340 |
// (approx. 2 orders of magnitude smaller than the tolerance) to the |
| 341 |
// input lattices and try again. |
| 342 |
return false; |
| 343 |
} |
| 344 |
*p = -1; |
| 345 |
} |
| 346 |
tmpMat << i, 0, 0, 0, j, 0, 0, 0, k; |
| 347 |
cob *= tmpMat; |
| 348 |
|
| 349 |
// Update characteristic |
| 350 |
xi = -std::fabs(xi); |
| 351 |
eta = -std::fabs(eta); |
| 352 |
zeta = -std::fabs(zeta); |
| 353 |
NIGGLI_DEBUG(4); |
| 354 |
++iter; |
| 355 |
} |
| 356 |
|
| 357 |
// Step 5: |
| 358 |
if (fuzzyGreaterThan(std::fabs(xi), B, tol) || |
| 359 |
(fuzzyEqual(xi, B, tol) && fuzzyLessThan(2 * eta, zeta, tol)) || |
| 360 |
(fuzzyEqual(xi, -B, tol) && fuzzyLessThan(zeta, Real(0), tol))) { |
| 361 |
Real signXi = niggliSign(xi); |
| 362 |
// Update change of basis matrix: |
| 363 |
tmpMat << 1, 0, 0, 0, 1, -signXi, 0, 0, 1; |
| 364 |
cob *= tmpMat; |
| 365 |
|
| 366 |
// Update characteristic |
| 367 |
C = B + C - xi * signXi; |
| 368 |
eta = eta - zeta * signXi; |
| 369 |
xi = xi - 2 * B * signXi; |
| 370 |
NIGGLI_DEBUG(5); |
| 371 |
continue; |
| 372 |
} |
| 373 |
|
| 374 |
// Step 6: |
| 375 |
if (fuzzyGreaterThan(std::fabs(eta), A, tol) || |
| 376 |
(fuzzyEqual(eta, A, tol) && fuzzyLessThan(2 * xi, zeta, tol)) || |
| 377 |
(fuzzyEqual(eta, -A, tol) && fuzzyLessThan(zeta, Real(0), tol))) { |
| 378 |
Real signEta = niggliSign(eta); |
| 379 |
// Update change of basis matrix: |
| 380 |
tmpMat << 1, 0, -signEta, 0, 1, 0, 0, 0, 1; |
| 381 |
cob *= tmpMat; |
| 382 |
|
| 383 |
// Update characteristic |
| 384 |
C = A + C - eta * signEta; |
| 385 |
xi = xi - zeta * signEta; |
| 386 |
eta = eta - 2 * A * signEta; |
| 387 |
NIGGLI_DEBUG(6); |
| 388 |
continue; |
| 389 |
} |
| 390 |
|
| 391 |
// Step 7: |
| 392 |
if (fuzzyGreaterThan(std::fabs(zeta), A, tol) || |
| 393 |
(fuzzyEqual(zeta, A, tol) && fuzzyLessThan(2 * xi, eta, tol)) || |
| 394 |
(fuzzyEqual(zeta, -A, tol) && fuzzyLessThan(eta, Real(0), tol))) { |
| 395 |
Real signZeta = niggliSign(zeta); |
| 396 |
// Update change of basis matrix: |
| 397 |
tmpMat << 1, -signZeta, 0, 0, 1, 0, 0, 0, 1; |
| 398 |
cob *= tmpMat; |
| 399 |
|
| 400 |
// Update characteristic |
| 401 |
B = A + B - zeta * signZeta; |
| 402 |
xi = xi - eta * signZeta; |
| 403 |
zeta = zeta - 2 * A * signZeta; |
| 404 |
NIGGLI_DEBUG(7); |
| 405 |
continue; |
| 406 |
} |
| 407 |
|
| 408 |
// Step 8: |
| 409 |
Real sumAllButC = A + B + xi + eta + zeta; |
| 410 |
if (fuzzyLessThan(sumAllButC, Real(0), tol) || |
| 411 |
(fuzzyEqual(sumAllButC, Real(0), tol) && |
| 412 |
fuzzyGreaterThan(2 * (A + eta) + zeta, Real(0), tol))) { |
| 413 |
// Update change of basis matrix: |
| 414 |
cob *= C8; |
| 415 |
|
| 416 |
// Update characteristic |
| 417 |
C = sumAllButC + C; |
| 418 |
xi = 2 * B + xi + zeta; |
| 419 |
eta = 2 * A + eta + zeta; |
| 420 |
NIGGLI_DEBUG(8); |
| 421 |
continue; |
| 422 |
} |
| 423 |
|
| 424 |
// Done! |
| 425 |
ret = true; |
| 426 |
break; |
| 427 |
} |
| 428 |
|
| 429 |
// No change |
| 430 |
if (iter == 0) |
| 431 |
return true; |
| 432 |
|
| 433 |
// Iteration limit exceeded: |
| 434 |
if (!ret) |
| 435 |
return false; |
| 436 |
|
| 437 |
// Update atoms if needed |
| 438 |
if (opts & TransformAtoms) { |
| 439 |
// Get fractional coordinates |
| 440 |
Array<Vector3> fcoords; |
| 441 |
if (!fractionalCoordinates(molecule, fcoords)) |
| 442 |
return false; |
| 443 |
|
| 444 |
// fix coordinates with COB matrix: |
| 445 |
const Matrix3 invCob(cob.inverse()); |
| 446 |
for (auto& fcoord : fcoords) { |
| 447 |
fcoord = invCob * fcoord; |
| 448 |
} |
| 449 |
|
| 450 |
// Update cell |
| 451 |
cell.setCellMatrix(cell.cellMatrix() * cob); |
| 452 |
|
| 453 |
// Reapply the fractional coordinates |
| 454 |
setFractionalCoordinates(molecule, fcoords); |
| 455 |
} else { |
| 456 |
// just update the matrix: |
| 457 |
cell.setCellMatrix(cell.cellMatrix() * cob); |
| 458 |
} |
| 459 |
return true; |
| 460 |
} |
| 461 |
|
| 462 |
bool CrystalTools::isNiggliReduced(const Molecule& molecule) |
| 463 |
{ |
| 464 |
if (!molecule.unitCell()) |
| 465 |
return false; |
| 466 |
|
| 467 |
const UnitCell& cell = *molecule.unitCell(); |
| 468 |
|
| 469 |
const Real a = cell.a(); |
| 470 |
const Real b = cell.b(); |
| 471 |
const Real c = cell.c(); |
| 472 |
const Real alpha = cell.alpha(); |
| 473 |
const Real beta = cell.beta(); |
| 474 |
const Real gamma = cell.gamma(); |
| 475 |
|
| 476 |
const Real A = a * a; |
| 477 |
const Real B = b * b; |
| 478 |
const Real C = c * c; |
| 479 |
const Real xi = static_cast<Real>(2) * b * c * std::cos(alpha); |
| 480 |
const Real eta = static_cast<Real>(2) * a * c * std::cos(beta); |
| 481 |
const Real zeta = static_cast<Real>(2) * a * b * std::cos(gamma); |
| 482 |
|
| 483 |
const Real tol = FUZZY_TOL * ((a + b + c) * static_cast<Real>(1. / 3.)); |
| 484 |
|
| 485 |
// First check the Buerger conditions. Taken from: Gruber B.. Acta |
| 486 |
// Cryst. A. 1973;29(4):433-440. Available at: |
| 487 |
// http://scripts.iucr.org/cgi-bin/paper?S0567739473001063 |
| 488 |
// [Accessed December 15, 2010]. |
| 489 |
if (fuzzyGreaterThan(A, B, tol) || fuzzyGreaterThan(B, C, tol)) |
| 490 |
return false; |
| 491 |
|
| 492 |
if (fuzzyEqual(A, B, tol) && |
| 493 |
fuzzyGreaterThan(std::fabs(xi), std::fabs(eta), tol)) { |
| 494 |
return false; |
| 495 |
} |
| 496 |
|
| 497 |
if (fuzzyEqual(B, C, tol) && |
| 498 |
fuzzyGreaterThan(std::fabs(eta), std::fabs(zeta), tol)) { |
| 499 |
return false; |
| 500 |
} |
| 501 |
|
| 502 |
if (!(fuzzyGreaterThan(xi, static_cast<Real>(0.0), tol) && |
| 503 |
fuzzyGreaterThan(eta, static_cast<Real>(0.0), tol) && |
| 504 |
fuzzyGreaterThan(zeta, static_cast<Real>(0.0), tol)) && |
| 505 |
!(fuzzyLessThanEq(zeta, static_cast<Real>(0.0), tol) && |
| 506 |
fuzzyLessThanEq(zeta, static_cast<Real>(0.0), tol) && |
| 507 |
fuzzyLessThanEq(zeta, static_cast<Real>(0.0), tol))) { |
| 508 |
return false; |
| 509 |
} |
| 510 |
|
| 511 |
// Check against Niggli conditions (taken from Gruber 1973). The |
| 512 |
// logic of the second comparison is reversed from the paper to |
| 513 |
// simplify the algorithm. |
| 514 |
if (fuzzyEqual(xi, B, tol) && |
| 515 |
fuzzyGreaterThan(zeta, static_cast<Real>(2) * eta, tol)) { |
| 516 |
return false; |
| 517 |
} |
| 518 |
if (fuzzyEqual(eta, A, tol) && |
| 519 |
fuzzyGreaterThan(zeta, static_cast<Real>(2) * xi, tol)) { |
| 520 |
return false; |
| 521 |
} |
| 522 |
if (fuzzyEqual(zeta, A, tol) && |
| 523 |
fuzzyGreaterThan(eta, static_cast<Real>(2) * xi, tol)) { |
| 524 |
return false; |
| 525 |
} |
| 526 |
if (fuzzyEqual(xi, -B, tol) && |
| 527 |
fuzzyNotEqual(zeta, static_cast<Real>(0), tol)) { |
| 528 |
return false; |
| 529 |
} |
| 530 |
if (fuzzyEqual(eta, -A, tol) && |
| 531 |
fuzzyNotEqual(zeta, static_cast<Real>(0), tol)) { |
| 532 |
return false; |
| 533 |
} |
| 534 |
if (fuzzyEqual(zeta, -A, tol) && |
| 535 |
fuzzyNotEqual(eta, static_cast<Real>(0), tol)) { |
| 536 |
return false; |
| 537 |
} |
| 538 |
if (fuzzyEqual(xi + eta + zeta + A + B, static_cast<Real>(0), tol) && |
| 539 |
fuzzyGreaterThan(static_cast<Real>(2) * (A + eta) + zeta, |
| 540 |
static_cast<Real>(0), tol)) { |
| 541 |
return false; |
| 542 |
} |
| 543 |
|
| 544 |
// all good! |
| 545 |
return true; |
| 546 |
} |
| 547 |
|
| 548 |
bool CrystalTools::buildSupercell(Molecule& molecule, unsigned int a, |
| 549 |
unsigned int b, unsigned int c) |
| 550 |
{ |
| 551 |
if (!molecule.unitCell()) |
| 552 |
return false; |
| 553 |
|
| 554 |
// Just a check. Hopefully this won't happen |
| 555 |
if (a == 0 || b == 0 || c == 0) { |
| 556 |
std::cerr << "Warning: in buildSupercell(), a, b, or c were set to zero." |
| 557 |
<< "This function will not proceed. Returning false."; |
| 558 |
return false; |
| 559 |
} |
| 560 |
|
| 561 |
// Get the old vectors |
| 562 |
Vector3 oldA = molecule.unitCell()->aVector(); |
| 563 |
Vector3 oldB = molecule.unitCell()->bVector(); |
| 564 |
Vector3 oldC = molecule.unitCell()->cVector(); |
| 565 |
|
| 566 |
// Calculate new vectors |
| 567 |
Vector3 newA = oldA * a; |
| 568 |
Vector3 newB = oldB * b; |
| 569 |
Vector3 newC = oldC * c; |
| 570 |
|
| 571 |
// archive the old bond pairs and orders |
| 572 |
Array<std::pair<Index, Index>> bondPairs = molecule.bondPairs(); |
| 573 |
Array<unsigned char> bondOrders = molecule.bondOrders(); |
| 574 |
|
| 575 |
// Add in the atoms to the new subcells of the supercell. |
| 576 |
// Track index mapping: indexMap[copyIndex][origAtomIndex] -> new atom index, |
| 577 |
// so bonds can be copied correctly even when duplicates are skipped. |
| 578 |
Index numAtoms = molecule.atomCount(); |
| 579 |
Array<Vector3> atoms = molecule.atomPositions3d(); |
| 580 |
Array<unsigned char> atomicNums = molecule.atomicNumbers(); |
| 581 |
const double dupTol = 0.01; |
| 582 |
|
| 583 |
// Copy 0 is the original subcell — identity mapping |
| 584 |
unsigned totalCopies = a * b * c; |
| 585 |
std::vector<std::vector<Index>> indexMap(totalCopies, |
| 586 |
std::vector<Index>(numAtoms)); |
| 587 |
for (Index i = 0; i < numAtoms; ++i) |
| 588 |
indexMap[0][i] = i; |
| 589 |
|
| 590 |
unsigned copyIdx = 1; |
| 591 |
for (Index ind_a = 0; ind_a < a; ++ind_a) { |
| 592 |
for (Index ind_b = 0; ind_b < b; ++ind_b) { |
| 593 |
for (Index ind_c = 0; ind_c < c; ++ind_c) { |
| 594 |
// Skip over the subcell that already exists |
| 595 |
if (ind_a == 0 && ind_b == 0 && ind_c == 0) |
| 596 |
continue; |
| 597 |
// The positions of the new atoms are displacements of the old atoms |
| 598 |
Vector3 displacement = ind_a * oldA + ind_b * oldB + ind_c * oldC; |
| 599 |
for (Index i = 0; i < numAtoms; ++i) { |
| 600 |
Vector3 newPos = atoms.at(i) + displacement; |
| 601 |
// Check if this duplicates an existing atom (e.g., translational |
| 602 |
// copies at fractional ~1.0 overlap with the next subcell's ~0.0) |
| 603 |
Index existingIdx = molecule.atomCount(); // sentinel |
| 604 |
for (Index k = 0; k < molecule.atomCount(); ++k) { |
| 605 |
if ((molecule.atomPosition3d(k) - newPos).norm() < dupTol) { |
| 606 |
existingIdx = k; |
| 607 |
break; |
| 608 |
} |
| 609 |
} |
| 610 |
if (existingIdx < molecule.atomCount()) { |
| 611 |
indexMap[copyIdx][i] = existingIdx; |
| 612 |
} else { |
| 613 |
Atom newAtom = molecule.addAtom(atomicNums.at(i)); |
| 614 |
newAtom.setPosition3d(newPos); |
| 615 |
indexMap[copyIdx][i] = newAtom.index(); |
| 616 |
} |
| 617 |
} |
| 618 |
++copyIdx; |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
// Now add the bonds using the index map |
| 624 |
for (Index i = 0; i < bondPairs.size(); ++i) { |
| 625 |
std::pair<Index, Index> bond = bondPairs.at(i); |
| 626 |
for (unsigned j = 0; j < totalCopies; ++j) { |
| 627 |
Index a1 = indexMap[j][bond.first]; |
| 628 |
Index a2 = indexMap[j][bond.second]; |
| 629 |
// Avoid adding duplicate bonds (duplicated atoms may share bonds) |
| 630 |
if (j > 0 && !molecule.bond(a1, a2).isValid()) |
| 631 |
molecule.addBond(a1, a2, bondOrders.at(i)); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
// Now set the unit cell |
| 636 |
molecule.unitCell()->setAVector(newA); |
| 637 |
molecule.unitCell()->setBVector(newB); |
| 638 |
molecule.unitCell()->setCVector(newC); |
| 639 |
|
| 640 |
// We're done! |
| 641 |
return true; |
| 642 |
} |
| 643 |
|
| 644 |
namespace { |
| 645 |
struct TransformAtomsFunctor |
| 646 |
{ |
| 647 |
TransformAtomsFunctor(const Matrix3& t) : transform(t) {} |
| 648 |
const Matrix3& transform; |
| 649 |
|
| 650 |
void operator()(Vector3& pos) { pos = transform * pos; } |
| 651 |
}; |
| 652 |
} // namespace |
| 653 |
|
| 654 |
bool CrystalTools::setCellMatrix(Molecule& molecule, |
| 655 |
const Matrix3& newCellColMatrix, Options opt) |
| 656 |
{ |
| 657 |
if (opt & TransformAtoms && molecule.unitCell()) { |
| 658 |
const Matrix3 xform(newCellColMatrix * |
| 659 |
molecule.unitCell()->cellMatrix().inverse()); |
| 660 |
std::for_each(molecule.atomPositions3d().begin(), |
| 661 |
molecule.atomPositions3d().end(), |
| 662 |
TransformAtomsFunctor(xform)); |
| 663 |
} |
| 664 |
|
| 665 |
if (!UnitCell::isRegular(newCellColMatrix)) { |
| 666 |
std::cerr << __FUNCTION__ << " cell matrix is singular\n"; |
| 667 |
return false; |
| 668 |
} |
| 669 |
|
| 670 |
// only create a new unit cell if it doesn't exist |
| 671 |
if (!molecule.unitCell()) |
| 672 |
molecule.setUnitCell(new UnitCell); |
| 673 |
|
| 674 |
molecule.unitCell()->setCellMatrix(newCellColMatrix); |
| 675 |
|
| 676 |
return true; |
| 677 |
} |
| 678 |
|
| 679 |
namespace { |
| 680 |
struct FractionalCoordinatesFunctor |
| 681 |
{ |
| 682 |
const UnitCell& unitCell; |
| 683 |
|
| 684 |
FractionalCoordinatesFunctor(const UnitCell& uc) : unitCell(uc) {} |
| 685 |
|
| 686 |
void operator()(Vector3& pos) { unitCell.toFractional(pos, pos); } |
| 687 |
}; |
| 688 |
} // namespace |
| 689 |
|
| 690 |
bool CrystalTools::fractionalCoordinates(const UnitCell& unitCell, |
| 691 |
const Array<Vector3>& cart, |
| 692 |
Array<Vector3>& frac) |
| 693 |
{ |
| 694 |
if (&frac != &cart) // avoid self-copy... |
| 695 |
frac = cart; |
| 696 |
|
| 697 |
std::for_each(frac.begin(), frac.end(), |
| 698 |
FractionalCoordinatesFunctor(unitCell)); |
| 699 |
|
| 700 |
return true; |
| 701 |
} |
| 702 |
|
| 703 |
bool CrystalTools::fractionalCoordinates(const Molecule& molecule, |
| 704 |
Array<Vector3>& coords) |
| 705 |
{ |
| 706 |
if (!molecule.unitCell()) |
| 707 |
return false; |
| 708 |
|
| 709 |
coords = molecule.atomPositions3d(); |
| 710 |
coords.resize(molecule.atomCount()); |
| 711 |
|
| 712 |
return fractionalCoordinates(*molecule.unitCell(), coords, coords); |
| 713 |
} |
| 714 |
|
| 715 |
namespace { |
| 716 |
struct SetFractionalCoordinatesFunctor |
| 717 |
{ |
| 718 |
const UnitCell& unitCell; |
| 719 |
|
| 720 |
SetFractionalCoordinatesFunctor(const Molecule& molecule) |
| 721 |
: unitCell(*molecule.unitCell()) |
| 722 |
{ |
| 723 |
} |
| 724 |
|
| 725 |
Vector3 operator()(const Vector3& pos) { return unitCell.toCartesian(pos); } |
| 726 |
}; |
| 727 |
} // namespace |
| 728 |
|
| 729 |
bool CrystalTools::setFractionalCoordinates(Molecule& molecule, |
| 730 |
const Array<Vector3>& coords) |
| 731 |
{ |
| 732 |
if (!molecule.unitCell()) |
| 733 |
return false; |
| 734 |
|
| 735 |
if (coords.size() != molecule.atomCount()) |
| 736 |
return false; |
| 737 |
|
| 738 |
Array<Vector3>& output = molecule.atomPositions3d(); |
| 739 |
output.resize(coords.size()); |
| 740 |
|
| 741 |
std::transform(coords.begin(), coords.end(), output.begin(), |
| 742 |
SetFractionalCoordinatesFunctor(molecule)); |
| 743 |
|
| 744 |
return true; |
| 745 |
} |
| 746 |
|
| 747 |
} // namespace Avogadro::Core |