| 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 "atomutilities.h" |
| 7 |
|
| 8 |
#include <cmath> |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
constexpr double M_TETRAHED = 109.47122063449069389; |
| 12 |
|
| 13 |
namespace Avogadro::Core { |
| 14 |
|
| 15 |
using NeighborListType = Array<Bond>; |
| 16 |
|
| 17 |
inline unsigned int countExistingBonds(const NeighborListType& bonds) |
| 18 |
{ |
| 19 |
unsigned int result(0); |
| 20 |
for (auto bond : bonds) { |
| 21 |
result += static_cast<unsigned int>(bond.order()); |
| 22 |
} |
| 23 |
return result; |
| 24 |
} |
| 25 |
|
| 26 |
AtomHybridization AtomUtilities::perceiveHybridization(const Atom& atom) |
| 27 |
{ |
| 28 |
const NeighborListType bonds(atom.molecule()->bonds(atom)); |
| 29 |
const unsigned int numberOfBonds(countExistingBonds(bonds)); // bond order sum |
| 30 |
|
| 31 |
AtomHybridization hybridization = SP3; // default to sp3 |
| 32 |
|
| 33 |
// TODO: Handle hypervalent species, SO3, SO4, lone pairs, etc. |
| 34 |
|
| 35 |
if (numberOfBonds > 4) { |
| 36 |
// hybridization = numberOfBonds; // e.g., octahedral, trig. bipyr., |
| 37 |
// etc. |
| 38 |
} else { |
| 39 |
// Count multiple bonds |
| 40 |
unsigned int numTripleBonds = 0; |
| 41 |
unsigned int numDoubleBonds = 0; |
| 42 |
|
| 43 |
for (auto bond : bonds) { |
| 44 |
if (bond.order() == 2) |
| 45 |
numDoubleBonds++; |
| 46 |
else if (bond.order() == 3) |
| 47 |
numTripleBonds++; |
| 48 |
} |
| 49 |
|
| 50 |
if (numTripleBonds > 0 || numDoubleBonds > 1) |
| 51 |
hybridization = SP; // sp |
| 52 |
else if (numDoubleBonds > 0) |
| 53 |
hybridization = SP2; // sp2 |
| 54 |
|
| 55 |
// special case for nitrogen in an amide |
| 56 |
if (atom.atomicNumber() == 7 && hybridization == SP3) { |
| 57 |
// look through the neighbors for a C=O |
| 58 |
for (auto bond : bonds) { |
| 59 |
Atom a1 = bond.getOtherAtom(atom); |
| 60 |
if (a1.atomicNumber() == 6 && bond.order() == 1) { |
| 61 |
const NeighborListType nbrBonds(atom.molecule()->bonds(a1)); |
| 62 |
for (auto nbrBond : nbrBonds) { |
| 63 |
Atom a2 = nbrBond.getOtherAtom(a1); |
| 64 |
if (a2.index() == atom.index()) |
| 65 |
continue; // we want a *new* atom, not the nitrogen |
| 66 |
|
| 67 |
if (a2.atomicNumber() == 8 && nbrBond.order() == 2) { |
| 68 |
hybridization = SP2; |
| 69 |
break; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
return hybridization; |
| 77 |
} |
| 78 |
|
| 79 |
// Generate bond geometries |
| 80 |
// First, the default fallback (random vectors) |
| 81 |
// Also applies when you have a linear geometry and just need one new vector |
| 82 |
// (it doesn't matter where it goes). |
| 83 |
Vector3 AtomUtilities::generateNewBondVector( |
| 84 |
const Atom& atom, const std::vector<Vector3>& allVectors, |
| 85 |
AtomHybridization hybridization) |
| 86 |
{ |
| 87 |
Vector3 newPos; |
| 88 |
bool success = false; |
| 89 |
int currentValence = static_cast<int>(allVectors.size()); |
| 90 |
|
| 91 |
// No bonded atoms, just pick a random vector |
| 92 |
if (currentValence == 0) { |
| 93 |
newPos = Vector3::Random().normalized(); |
| 94 |
return newPos; |
| 95 |
} else if (currentValence == 1) { |
| 96 |
// One bonded atom |
| 97 |
const Vector3& bond1 = allVectors[0]; |
| 98 |
|
| 99 |
// Check what's attached to our neighbor -- we want to set trans to the |
| 100 |
// neighbor |
| 101 |
Vector3 bond2(0.0, 0.0, 0.0); |
| 102 |
|
| 103 |
const NeighborListType bonds(atom.molecule()->bonds(atom)); |
| 104 |
for (auto bond : bonds) { |
| 105 |
Atom a1 = bond.getOtherAtom(atom); |
| 106 |
const NeighborListType nbrBonds(atom.molecule()->bonds(a1)); |
| 107 |
for (auto nbrBond : nbrBonds) { |
| 108 |
Atom a2 = nbrBond.getOtherAtom(a1); |
| 109 |
if (a2.index() == atom.index()) |
| 110 |
continue; // we want a *new* atom |
| 111 |
|
| 112 |
Vector3 delta = a2.position3d() - a1.position3d(); |
| 113 |
if (!delta.isZero(1e-5)) |
| 114 |
bond2 = delta.normalized(); |
| 115 |
|
| 116 |
// Check for carboxylate (CO2) |
| 117 |
if ((atom.atomicNumber() == 8) // atom for H is O |
| 118 |
&& (a1.atomicNumber() == 6) // central atom is C |
| 119 |
&& (nbrBond.order() == 2) && (a2.atomicNumber() == 8)) |
| 120 |
break; // make sure the H will be trans to the C=O |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
Vector3 v1, v2; |
| 125 |
v1 = bond1.cross(bond2); |
| 126 |
|
| 127 |
if (bond2.norm() < 1.0e-5 || v1.norm() < 1.0e-5) { |
| 128 |
// std::cout << " creating a random paired atom " << std::endl; |
| 129 |
|
| 130 |
v2 = Vector3::Random().normalized(); |
| 131 |
|
| 132 |
double angle = fabs(acos(bond1.dot(v2))); |
| 133 |
while (angle < 45.0 * DEG_TO_RAD || angle > 135.0 * DEG_TO_RAD) { |
| 134 |
v2 = Vector3::Random().normalized(); |
| 135 |
angle = fabs(acos(bond1.dot(v2))); |
| 136 |
// std::cout << "angle = " << angle*RAD_TO_DEG << std::endl; |
| 137 |
} |
| 138 |
v1 = bond1.cross(v2); // so find a perpendicular, given the random vector |
| 139 |
v2 = bond1.cross(v1); |
| 140 |
} else { |
| 141 |
// std::cout << " found a neighbor for trans " << std::endl; |
| 142 |
v1 = bond1.cross(bond2); |
| 143 |
v2 = -1.0 * bond1.cross(v1); |
| 144 |
} |
| 145 |
v2.normalize(); |
| 146 |
|
| 147 |
switch (hybridization) { |
| 148 |
case SP: |
| 149 |
case SquarePlanar: |
| 150 |
case TrigonalBipyramidal: |
| 151 |
newPos = bond1; // 180 degrees away from the current neighbor |
| 152 |
break; |
| 153 |
case SP2: // sp2 |
| 154 |
newPos = bond1 - v2 * tan(DEG_TO_RAD * 120.0); |
| 155 |
break; |
| 156 |
case Octahedral: // octahedral |
| 157 |
newPos = bond1 - v2 * tan(DEG_TO_RAD * 90.0); |
| 158 |
break; |
| 159 |
case SP3: |
| 160 |
default: |
| 161 |
newPos = (bond1 - v2 * tan(DEG_TO_RAD * M_TETRAHED)); |
| 162 |
break; |
| 163 |
} |
| 164 |
|
| 165 |
// std::cout << " one bond " << newPos.normalized() << std::endl; |
| 166 |
return -1.0 * newPos.normalized(); |
| 167 |
} // end one bond |
| 168 |
else if (currentValence == 2) { |
| 169 |
const Vector3& bond1 = allVectors[0]; |
| 170 |
const Vector3& bond2 = allVectors[1]; |
| 171 |
|
| 172 |
Vector3 v1 = bond1 + bond2; |
| 173 |
v1.normalize(); |
| 174 |
|
| 175 |
switch (hybridization) { |
| 176 |
case SP: // shouldn't happen, but maybe with metal atoms? |
| 177 |
case SP2: |
| 178 |
newPos = v1; // point away from the two existing bonds |
| 179 |
break; |
| 180 |
case SP3: |
| 181 |
default: |
| 182 |
Vector3 v2 = bond1.cross(bond2); // find the perpendicular |
| 183 |
v2.normalize(); |
| 184 |
// newPos = bond1 - v2 * tan(DEG_TO_RAD * (M_TETRAHED)); |
| 185 |
newPos = v2 + v1 * (sqrt(2.0) / 2.0); |
| 186 |
} |
| 187 |
|
| 188 |
// std::cout << " two bonds " << newPos.normalized() << std::endl; |
| 189 |
return -1.0 * newPos.normalized(); |
| 190 |
} // end two bonds |
| 191 |
else if (currentValence == 3) { |
| 192 |
const Vector3& bond1 = allVectors[0]; |
| 193 |
const Vector3& bond2 = allVectors[1]; |
| 194 |
const Vector3& bond3 = allVectors[2]; |
| 195 |
|
| 196 |
// need to handle different hybridizations here |
| 197 |
|
| 198 |
// since the base of the tetrahedron should be symmetric |
| 199 |
// the sum of the three bond vectors should cancel the angular parts |
| 200 |
// and point in the new direction.. just need to normalize and rescale |
| 201 |
newPos = -1.0 * (bond1 + bond2 + bond3); |
| 202 |
|
| 203 |
// std::cout << " three bonds " << newPos.normalized() << std::endl; |
| 204 |
return newPos.normalized(); |
| 205 |
} |
| 206 |
|
| 207 |
// Fallback: |
| 208 |
// Try 10 times to generate a random vector that doesn't overlap with |
| 209 |
// an existing bond. If we can't, just give up and let the overlap occur. |
| 210 |
|
| 211 |
// Tolerance for two vectors being "too close" in radians (pi/8). |
| 212 |
const Avogadro::Real cosRadTol = |
| 213 |
cos(static_cast<Avogadro::Real>(M_PI) / static_cast<Avogadro::Real>(8.)); |
| 214 |
|
| 215 |
for (int attempt = 0; !success && attempt < 10; ++attempt) { |
| 216 |
newPos = Vector3::Random().normalized(); |
| 217 |
success = true; |
| 218 |
for (auto it = allVectors.begin(), itEnd = allVectors.end(); |
| 219 |
success && it != itEnd; ++it) { |
| 220 |
success = newPos.dot(*it) < cosRadTol; |
| 221 |
} |
| 222 |
} |
| 223 |
return newPos; |
| 224 |
} |
| 225 |
|
| 226 |
} // namespace Avogadro::Core |