| 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 "molecule.h" |
| 7 |
|
| 8 |
#include "basisset.h" |
| 9 |
#include "cube.h" |
| 10 |
#include "elements.h" |
| 11 |
#include "gaussianset.h" |
| 12 |
#include "layermanager.h" |
| 13 |
#include "mdlvalence_p.h" |
| 14 |
#include "mesh.h" |
| 15 |
#include "neighborperceiver.h" |
| 16 |
#include "residue.h" |
| 17 |
#include "slaterset.h" |
| 18 |
#include "unitcell.h" |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <cassert> |
| 22 |
#include <cmath> |
| 23 |
#include <cstddef> |
| 24 |
#include <cmath> |
| 25 |
#include <iostream> |
| 26 |
#include <utility> |
| 27 |
|
| 28 |
namespace Avogadro::Core { |
| 29 |
|
| 30 |
Molecule::Molecule() |
| 31 |
: m_basisSet(nullptr), m_unitCell(nullptr), |
| 32 |
m_layers(LayerManager::getMoleculeLayer(this)) |
| 33 |
{ |
| 34 |
m_elements.reset(); |
| 35 |
} |
| 36 |
|
| 37 |
Molecule::Molecule(const Molecule& other) |
| 38 |
: m_data(other.m_data), m_partialCharges(other.m_partialCharges), |
| 39 |
m_spectra(other.m_spectra), m_customElementMap(other.m_customElementMap), |
| 40 |
m_elements(other.m_elements), m_positions2d(other.m_positions2d), |
| 41 |
m_positions3d(other.m_positions3d), m_atomLabels(other.m_atomLabels), |
| 42 |
m_bondLabels(other.m_bondLabels), m_residueLabels(other.m_residueLabels), |
| 43 |
m_coordinates3d(other.m_coordinates3d), m_velocities(other.m_velocities), |
| 44 |
m_timesteps(other.m_timesteps), m_hybridizations(other.m_hybridizations), |
| 45 |
m_formalCharges(other.m_formalCharges), m_isotopes(other.m_isotopes), |
| 46 |
m_forceVectors(other.m_forceVectors), m_colors(other.m_colors), |
| 47 |
m_vibrationFrequencies(other.m_vibrationFrequencies), |
| 48 |
m_vibrationIRIntensities(other.m_vibrationIRIntensities), |
| 49 |
m_vibrationRamanIntensities(other.m_vibrationRamanIntensities), |
| 50 |
m_vibrationLx(other.m_vibrationLx), m_selectedAtoms(other.m_selectedAtoms), |
| 51 |
m_meshes(), m_cubes(), |
| 52 |
m_basisSet(other.m_basisSet ? other.m_basisSet->clone() : nullptr), |
| 53 |
m_unitCell(other.m_unitCell ? new UnitCell(*other.m_unitCell) : nullptr), |
| 54 |
m_residues(other.m_residues), m_hallNumber(other.m_hallNumber), |
| 55 |
m_constraints(other.m_constraints), |
| 56 |
m_frozenAtomMask(other.m_frozenAtomMask), m_graph(other.m_graph), |
| 57 |
m_bondOrders(other.m_bondOrders), m_atomicNumbers(other.m_atomicNumbers), |
| 58 |
m_layers(LayerManager::getMoleculeLayer(&other, this)) |
| 59 |
{ |
| 60 |
// Copy over any meshes |
| 61 |
for (Index i = 0; i < other.meshCount(); ++i) { |
| 62 |
Mesh* m = addMesh(); |
| 63 |
*m = *other.mesh(i); |
| 64 |
} |
| 65 |
|
| 66 |
// Copy over any cubes |
| 67 |
for (Index i = 0; i < other.cubeCount(); ++i) { |
| 68 |
Cube* c = addCube(); |
| 69 |
*c = *other.cube(i); |
| 70 |
} |
| 71 |
m_activeCubeIndex = other.m_activeCubeIndex; |
| 72 |
|
| 73 |
// Make sure all the atoms are in the active layer |
| 74 |
if (other.m_layers.maxLayer() == 0) { |
| 75 |
for (Index i = 0; i < atomCount(); ++i) |
| 76 |
m_layers.addAtomToActiveLayer(i); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void Molecule::readProperties(const Molecule& other) |
| 81 |
{ |
| 82 |
m_atomLabels = other.m_atomLabels; |
| 83 |
m_bondLabels = other.m_bondLabels; |
| 84 |
m_residueLabels = other.m_residueLabels; |
| 85 |
m_colors = other.m_colors; |
| 86 |
// merge data maps by iterating through other's map |
| 87 |
for (auto it = other.m_data.constBegin(); it != other.m_data.constEnd(); |
| 88 |
++it) { |
| 89 |
// even if we have the same key, we want to overwrite |
| 90 |
m_data.setValue(it->first, it->second); |
| 91 |
} |
| 92 |
// merge partial charge maps |
| 93 |
for (auto it = other.m_partialCharges.cbegin(); |
| 94 |
it != other.m_partialCharges.cend(); ++it) { |
| 95 |
m_partialCharges[it->first] = it->second; |
| 96 |
} |
| 97 |
|
| 98 |
// copy spectra |
| 99 |
m_spectra = other.m_spectra; |
| 100 |
|
| 101 |
// copy orbital information |
| 102 |
SlaterSet* slaterSet = dynamic_cast<SlaterSet*>(other.m_basisSet); |
| 103 |
if (slaterSet != nullptr) { |
| 104 |
m_basisSet = slaterSet->clone(); |
| 105 |
m_basisSet->setMolecule(this); |
| 106 |
} |
| 107 |
GaussianSet* gaussianSet = dynamic_cast<GaussianSet*>(other.m_basisSet); |
| 108 |
if (gaussianSet != nullptr) { |
| 109 |
m_basisSet = gaussianSet->clone(); |
| 110 |
m_basisSet->setMolecule(this); |
| 111 |
} |
| 112 |
|
| 113 |
// copy over spectra information |
| 114 |
if (other.m_vibrationFrequencies.size() > 0) { |
| 115 |
m_vibrationFrequencies = other.m_vibrationFrequencies; |
| 116 |
m_vibrationIRIntensities = other.m_vibrationIRIntensities; |
| 117 |
m_vibrationRamanIntensities = other.m_vibrationRamanIntensities; |
| 118 |
m_vibrationLx = other.m_vibrationLx; |
| 119 |
} |
| 120 |
|
| 121 |
// Copy over any meshes |
| 122 |
for (Index i = 0; i < other.meshCount(); ++i) { |
| 123 |
Mesh* m = addMesh(); |
| 124 |
*m = *other.mesh(i); |
| 125 |
} |
| 126 |
|
| 127 |
// Copy over any cubes |
| 128 |
for (Index i = 0; i < other.cubeCount(); ++i) { |
| 129 |
Cube* c = addCube(); |
| 130 |
*c = *other.cube(i); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
Molecule::Molecule(Molecule&& other) noexcept |
| 135 |
: m_data(other.m_data), m_partialCharges(std::move(other.m_partialCharges)), |
| 136 |
m_spectra(other.m_spectra), |
| 137 |
m_customElementMap(std::move(other.m_customElementMap)), |
| 138 |
m_elements(other.m_elements), m_positions2d(other.m_positions2d), |
| 139 |
m_positions3d(other.m_positions3d), m_atomLabels(other.m_atomLabels), |
| 140 |
m_bondLabels(other.m_bondLabels), m_residueLabels(other.m_residueLabels), |
| 141 |
m_coordinates3d(other.m_coordinates3d), m_velocities(other.m_velocities), |
| 142 |
m_timesteps(other.m_timesteps), m_hybridizations(other.m_hybridizations), |
| 143 |
m_formalCharges(other.m_formalCharges), m_isotopes(other.m_isotopes), |
| 144 |
m_colors(other.m_colors), |
| 145 |
m_vibrationFrequencies(other.m_vibrationFrequencies), |
| 146 |
m_vibrationIRIntensities(other.m_vibrationIRIntensities), |
| 147 |
m_vibrationRamanIntensities(other.m_vibrationRamanIntensities), |
| 148 |
m_vibrationLx(other.m_vibrationLx), |
| 149 |
m_selectedAtoms(std::move(other.m_selectedAtoms)), |
| 150 |
m_meshes(std::move(other.m_meshes)), m_cubes(std::move(other.m_cubes)), |
| 151 |
m_basisSet(std::exchange(other.m_basisSet, nullptr)), |
| 152 |
m_unitCell(std::exchange(other.m_unitCell, nullptr)), |
| 153 |
m_residues(other.m_residues), m_hallNumber(other.m_hallNumber), |
| 154 |
m_constraints(other.m_constraints), |
| 155 |
m_frozenAtomMask(other.m_frozenAtomMask), m_graph(other.m_graph), |
| 156 |
m_bondOrders(other.m_bondOrders), m_atomicNumbers(other.m_atomicNumbers), |
| 157 |
m_layers(LayerManager::getMoleculeLayer(this)) |
| 158 |
{ |
| 159 |
// Copy the layers, only if they exist |
| 160 |
if (other.m_layers.maxLayer() > 0) |
| 161 |
m_layers = LayerManager::getMoleculeLayer(&other, this); |
| 162 |
else { |
| 163 |
// make sure all the atoms are in the active layer |
| 164 |
for (Index i = 0; i < atomCount(); ++i) |
| 165 |
m_layers.addAtomToActiveLayer(i); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
Molecule& Molecule::operator=(const Molecule& other) |
| 170 |
{ |
| 171 |
if (this != &other) { |
| 172 |
m_data = other.m_data; |
| 173 |
m_partialCharges = other.m_partialCharges; |
| 174 |
m_spectra = other.m_spectra; |
| 175 |
m_customElementMap = other.m_customElementMap; |
| 176 |
m_elements = other.m_elements; |
| 177 |
m_positions2d = other.m_positions2d; |
| 178 |
m_positions3d = other.m_positions3d; |
| 179 |
m_atomLabels = other.m_atomLabels; |
| 180 |
m_bondLabels = other.m_bondLabels; |
| 181 |
m_residueLabels = other.m_residueLabels; |
| 182 |
m_coordinates3d = other.m_coordinates3d; |
| 183 |
m_velocities = other.m_velocities; |
| 184 |
m_timesteps = other.m_timesteps; |
| 185 |
m_hybridizations = other.m_hybridizations; |
| 186 |
m_formalCharges = other.m_formalCharges; |
| 187 |
m_isotopes = other.m_isotopes; |
| 188 |
m_colors = other.m_colors, |
| 189 |
m_vibrationFrequencies = other.m_vibrationFrequencies; |
| 190 |
m_vibrationIRIntensities = other.m_vibrationIRIntensities; |
| 191 |
m_vibrationRamanIntensities = other.m_vibrationRamanIntensities; |
| 192 |
m_vibrationLx = other.m_vibrationLx; |
| 193 |
m_selectedAtoms = other.m_selectedAtoms; |
| 194 |
m_residues = other.m_residues; |
| 195 |
m_graph = other.m_graph; |
| 196 |
m_bondOrders = other.m_bondOrders; |
| 197 |
m_atomicNumbers = other.m_atomicNumbers; |
| 198 |
m_hallNumber = other.m_hallNumber; |
| 199 |
m_constraints = other.m_constraints; |
| 200 |
m_frozenAtomMask = other.m_frozenAtomMask; |
| 201 |
|
| 202 |
clearMeshes(); |
| 203 |
|
| 204 |
// Copy over any meshes |
| 205 |
for (Index i = 0; i < other.meshCount(); ++i) { |
| 206 |
Mesh* m = addMesh(); |
| 207 |
*m = *other.mesh(i); |
| 208 |
} |
| 209 |
|
| 210 |
clearCubes(); |
| 211 |
|
| 212 |
// Copy over any cubes |
| 213 |
for (Index i = 0; i < other.cubeCount(); ++i) { |
| 214 |
Cube* c = addCube(); |
| 215 |
*c = *other.cube(i); |
| 216 |
} |
| 217 |
m_activeCubeIndex = other.m_activeCubeIndex; |
| 218 |
|
| 219 |
delete m_basisSet; |
| 220 |
m_basisSet = other.m_basisSet ? other.m_basisSet->clone() : nullptr; |
| 221 |
delete m_unitCell; |
| 222 |
m_unitCell = other.m_unitCell ? new UnitCell(*other.m_unitCell) : nullptr; |
| 223 |
|
| 224 |
// Copy the layers, only if they exist |
| 225 |
if (other.m_layers.maxLayer() > 0) |
| 226 |
m_layers = LayerManager::getMoleculeLayer(&other, this); |
| 227 |
else { |
| 228 |
// make sure all the atoms are in the active layer |
| 229 |
for (Index i = 0; i < atomCount(); ++i) |
| 230 |
m_layers.addAtomToActiveLayer(i); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return *this; |
| 235 |
} |
| 236 |
|
| 237 |
Molecule& Molecule::operator=(Molecule&& other) noexcept |
| 238 |
{ |
| 239 |
if (this != &other) { |
| 240 |
m_data = other.m_data; |
| 241 |
m_partialCharges = std::move(other.m_partialCharges); |
| 242 |
m_spectra = other.m_spectra; |
| 243 |
m_customElementMap = std::move(other.m_customElementMap); |
| 244 |
m_elements = other.m_elements; |
| 245 |
m_positions2d = other.m_positions2d; |
| 246 |
m_positions3d = other.m_positions3d; |
| 247 |
m_atomLabels = other.m_atomLabels; |
| 248 |
m_bondLabels = other.m_bondLabels; |
| 249 |
m_residueLabels = other.m_residueLabels; |
| 250 |
m_coordinates3d = other.m_coordinates3d; |
| 251 |
m_velocities = other.m_velocities; |
| 252 |
m_timesteps = other.m_timesteps; |
| 253 |
m_hybridizations = other.m_hybridizations; |
| 254 |
m_formalCharges = other.m_formalCharges; |
| 255 |
m_isotopes = other.m_isotopes; |
| 256 |
m_colors = other.m_colors; |
| 257 |
m_vibrationFrequencies = other.m_vibrationFrequencies; |
| 258 |
m_vibrationIRIntensities = other.m_vibrationIRIntensities; |
| 259 |
m_vibrationRamanIntensities = other.m_vibrationRamanIntensities; |
| 260 |
m_vibrationLx = other.m_vibrationLx; |
| 261 |
m_selectedAtoms = std::move(other.m_selectedAtoms); |
| 262 |
m_residues = other.m_residues; |
| 263 |
m_graph = other.m_graph; |
| 264 |
m_bondOrders = other.m_bondOrders; |
| 265 |
m_atomicNumbers = other.m_atomicNumbers; |
| 266 |
m_hallNumber = other.m_hallNumber; |
| 267 |
m_constraints = other.m_constraints; |
| 268 |
m_frozenAtomMask = other.m_frozenAtomMask; |
| 269 |
|
| 270 |
clearMeshes(); |
| 271 |
m_meshes = std::move(other.m_meshes); |
| 272 |
|
| 273 |
clearCubes(); |
| 274 |
m_cubes = std::move(other.m_cubes); |
| 275 |
m_activeCubeIndex = other.m_activeCubeIndex; |
| 276 |
|
| 277 |
delete m_basisSet; |
| 278 |
m_basisSet = std::exchange(other.m_basisSet, nullptr); |
| 279 |
|
| 280 |
delete m_unitCell; |
| 281 |
m_unitCell = std::exchange(other.m_unitCell, nullptr); |
| 282 |
|
| 283 |
// Copy the layers, if they exist |
| 284 |
if (other.m_layers.maxLayer() > 0) |
| 285 |
m_layers = LayerManager::getMoleculeLayer(&other, this); |
| 286 |
else { |
| 287 |
// make sure all the atoms are in the active layer |
| 288 |
for (Index i = 0; i < atomCount(); ++i) |
| 289 |
m_layers.addAtomToActiveLayer(i); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return *this; |
| 294 |
} |
| 295 |
|
| 296 |
Molecule::~Molecule() |
| 297 |
{ |
| 298 |
// LayerManager::deleteMolecule(this); |
| 299 |
delete m_basisSet; |
| 300 |
delete m_unitCell; |
| 301 |
clearMeshes(); |
| 302 |
clearCubes(); |
| 303 |
} |
| 304 |
|
| 305 |
Layer& Molecule::layer() |
| 306 |
{ |
| 307 |
return m_layers; |
| 308 |
} |
| 309 |
const Layer& Molecule::layer() const |
| 310 |
{ |
| 311 |
return m_layers; |
| 312 |
} |
| 313 |
|
| 314 |
void Molecule::setPartialCharges(const std::string& type, const MatrixX& value) |
| 315 |
{ |
| 316 |
if (static_cast<Index>(value.size()) != atomCount()) |
| 317 |
return; |
| 318 |
|
| 319 |
m_partialCharges[type] = value; |
| 320 |
} |
| 321 |
|
| 322 |
MatrixX Molecule::partialCharges(const std::string& type) const |
| 323 |
{ |
| 324 |
auto search = m_partialCharges.find(type); |
| 325 |
if (search != m_partialCharges.end()) { |
| 326 |
return search->second; // value from the map |
| 327 |
} else { |
| 328 |
MatrixX charges(atomCount(), 1); |
| 329 |
charges.fill(0.0); |
| 330 |
return charges; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
std::set<std::string> Molecule::partialChargeTypes() const |
| 335 |
{ |
| 336 |
std::set<std::string> types; |
| 337 |
for (auto& it : m_partialCharges) |
| 338 |
types.insert(it.first); |
| 339 |
return types; |
| 340 |
} |
| 341 |
|
| 342 |
std::set<std::string> Molecule::spectraTypes() const |
| 343 |
{ |
| 344 |
std::set<std::string> types; |
| 345 |
for (auto& it : m_spectra) |
| 346 |
types.insert(it.first); |
| 347 |
return types; |
| 348 |
} |
| 349 |
|
| 350 |
void Molecule::setSpectra(const std::string& type, const MatrixX& value) |
| 351 |
{ |
| 352 |
m_spectra[type] = value; |
| 353 |
} |
| 354 |
|
| 355 |
MatrixX Molecule::spectra(const std::string& type) const |
| 356 |
{ |
| 357 |
MatrixX value; |
| 358 |
|
| 359 |
auto search = m_spectra.find(type); |
| 360 |
if (search != m_spectra.end()) { |
| 361 |
value = search->second; // value from the map |
| 362 |
} |
| 363 |
|
| 364 |
return value; |
| 365 |
} |
| 366 |
|
| 367 |
void Molecule::addConstraint(Real value, Index a, Index b, Index c, Index d) |
| 368 |
{ |
| 369 |
Constraint newConstraint(a, b, c, d, value); |
| 370 |
m_constraints.push_back(newConstraint); |
| 371 |
} |
| 372 |
|
| 373 |
void Molecule::removeConstraint(Index a, Index b, Index c, Index d) |
| 374 |
{ |
| 375 |
// loop through and remove if the constraint matches all atom indexes |
| 376 |
for (auto it = m_constraints.begin(); it != m_constraints.end();) { |
| 377 |
if (it->aIndex() == a && it->bIndex() == b && it->cIndex() == c && |
| 378 |
it->dIndex() == d) { |
| 379 |
it = m_constraints.erase(it); |
| 380 |
return; |
| 381 |
} else |
| 382 |
++it; |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
void Molecule::setFrozenAtom(Index atomId, bool frozen) |
| 387 |
{ |
| 388 |
if (atomId >= m_atomicNumbers.size()) |
| 389 |
return; |
| 390 |
|
| 391 |
Eigen::Index size = m_frozenAtomMask.rows(); |
| 392 |
auto newSize = static_cast<Eigen::Index>(3 * m_atomicNumbers.size()); |
| 393 |
|
| 394 |
if (m_frozenAtomMask.rows() != newSize) |
| 395 |
m_frozenAtomMask.conservativeResize(newSize); |
| 396 |
|
| 397 |
if (m_frozenAtomMask.rows() > size) |
| 398 |
for (Eigen::Index i = size; i < m_frozenAtomMask.rows(); ++i) |
| 399 |
m_frozenAtomMask[i] = 1.0f; |
| 400 |
|
| 401 |
float value = frozen ? 0.0f : 1.0f; |
| 402 |
auto base = static_cast<Eigen::Index>(atomId * 3); |
| 403 |
if (base <= m_frozenAtomMask.rows() - 3) { |
| 404 |
m_frozenAtomMask[base] = value; |
| 405 |
m_frozenAtomMask[base + 1] = value; |
| 406 |
m_frozenAtomMask[base + 2] = value; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
bool Molecule::frozenAtom(Index atomId) const |
| 411 |
{ |
| 412 |
auto base = static_cast<Eigen::Index>(atomId * 3); |
| 413 |
if (base <= m_frozenAtomMask.rows() - 3) { |
| 414 |
return (m_frozenAtomMask[base] == 0.0 && |
| 415 |
m_frozenAtomMask[base + 1] == 0.0 && |
| 416 |
m_frozenAtomMask[base + 2] == 0.0); |
| 417 |
} |
| 418 |
return false; |
| 419 |
} |
| 420 |
|
| 421 |
bool Molecule::frozenAtomAxis(Index atomId, int axis) const |
| 422 |
{ |
| 423 |
auto base = static_cast<Eigen::Index>(atomId * 3); |
| 424 |
if (base <= m_frozenAtomMask.rows() - 3) { |
| 425 |
return (m_frozenAtomMask[base + axis] == 0.0); |
| 426 |
} |
| 427 |
return false; |
| 428 |
} |
| 429 |
|
| 430 |
void Molecule::setFrozenAtomAxis(Index atomId, int axis, bool frozen) |
| 431 |
{ |
| 432 |
Eigen::Index size = m_frozenAtomMask.rows(); |
| 433 |
auto newSize = static_cast<Eigen::Index>(3 * m_atomicNumbers.size()); |
| 434 |
|
| 435 |
if (m_frozenAtomMask.rows() != newSize) |
| 436 |
m_frozenAtomMask.conservativeResize(newSize); |
| 437 |
|
| 438 |
if (m_frozenAtomMask.rows() > size) |
| 439 |
for (Eigen::Index i = size; i < m_frozenAtomMask.rows(); ++i) |
| 440 |
m_frozenAtomMask[i] = 1.0f; |
| 441 |
|
| 442 |
float value = frozen ? 0.0f : 1.0f; |
| 443 |
auto base = static_cast<Eigen::Index>(atomId * 3); |
| 444 |
if (axis >= 0 && axis < 3 && base <= m_frozenAtomMask.rows() - 3) { |
| 445 |
m_frozenAtomMask[base + axis] = value; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
void Molecule::setData(const std::string& name, const Variant& value) |
| 450 |
{ |
| 451 |
m_data.setValue(name, value); |
| 452 |
} |
| 453 |
|
| 454 |
Variant Molecule::data(const std::string& name) const |
| 455 |
{ |
| 456 |
return m_data.value(name); |
| 457 |
} |
| 458 |
|
| 459 |
bool Molecule::hasData(const std::string& name) const |
| 460 |
{ |
| 461 |
return m_data.hasValue(name); |
| 462 |
} |
| 463 |
|
| 464 |
void Molecule::setDataMap(const VariantMap& map) |
| 465 |
{ |
| 466 |
m_data = map; |
| 467 |
} |
| 468 |
|
| 469 |
const VariantMap& Molecule::dataMap() const |
| 470 |
{ |
| 471 |
return m_data; |
| 472 |
} |
| 473 |
|
| 474 |
VariantMap& Molecule::dataMap() |
| 475 |
{ |
| 476 |
return m_data; |
| 477 |
} |
| 478 |
|
| 479 |
Array<AtomHybridization>& Molecule::hybridizations() |
| 480 |
{ |
| 481 |
return m_hybridizations; |
| 482 |
} |
| 483 |
|
| 484 |
const Array<AtomHybridization>& Molecule::hybridizations() const |
| 485 |
{ |
| 486 |
return m_hybridizations; |
| 487 |
} |
| 488 |
|
| 489 |
Array<unsigned short>& Molecule::isotopes() |
| 490 |
{ |
| 491 |
return m_isotopes; |
| 492 |
} |
| 493 |
|
| 494 |
const Array<unsigned short>& Molecule::isotopes() const |
| 495 |
{ |
| 496 |
return m_isotopes; |
| 497 |
} |
| 498 |
|
| 499 |
Array<signed char>& Molecule::formalCharges() |
| 500 |
{ |
| 501 |
return m_formalCharges; |
| 502 |
} |
| 503 |
|
| 504 |
const Array<signed char>& Molecule::formalCharges() const |
| 505 |
{ |
| 506 |
return m_formalCharges; |
| 507 |
} |
| 508 |
|
| 509 |
signed char Molecule::totalCharge() const |
| 510 |
{ |
| 511 |
signed char charge = 0; |
| 512 |
|
| 513 |
// check the data map first |
| 514 |
if (m_data.hasValue("totalCharge")) { |
| 515 |
charge = m_data.value("totalCharge").toInt(); |
| 516 |
} else if (m_formalCharges.size() > 0) { |
| 517 |
for (Index i = 0; i < m_formalCharges.size(); ++i) |
| 518 |
charge += m_formalCharges[i]; |
| 519 |
return charge; |
| 520 |
} |
| 521 |
return charge; // should be zero |
| 522 |
} |
| 523 |
|
| 524 |
char Molecule::totalSpinMultiplicity() const |
| 525 |
{ |
| 526 |
char spin = 1; |
| 527 |
|
| 528 |
// check the data map first |
| 529 |
if (m_data.hasValue("totalSpinMultiplicity")) { |
| 530 |
spin = m_data.value("totalSpinMultiplicity").toInt(); |
| 531 |
} else { |
| 532 |
// add up the electrons |
| 533 |
unsigned long electrons = 0; |
| 534 |
for (Index i = 0; i < m_atomicNumbers.size(); ++i) |
| 535 |
electrons += m_atomicNumbers[i]; |
| 536 |
|
| 537 |
// adjust by the total charge |
| 538 |
electrons -= totalCharge(); |
| 539 |
|
| 540 |
// if there are an even number of electrons, the spin is 1 |
| 541 |
// if there are an odd number of electrons, the spin is 2 |
| 542 |
// (might not be true, but a good default for many molecules) |
| 543 |
// %todo - adjust for inorganic / organometallics |
| 544 |
if (electrons % 2 == 0) |
| 545 |
spin = 1; |
| 546 |
else |
| 547 |
spin = 2; |
| 548 |
} |
| 549 |
|
| 550 |
return spin; // should be zero |
| 551 |
} |
| 552 |
|
| 553 |
Array<Vector3ub>& Molecule::colors() |
| 554 |
{ |
| 555 |
return m_colors; |
| 556 |
} |
| 557 |
|
| 558 |
const Array<Vector3ub>& Molecule::colors() const |
| 559 |
{ |
| 560 |
return m_colors; |
| 561 |
} |
| 562 |
|
| 563 |
Array<Vector2>& Molecule::atomPositions2d() |
| 564 |
{ |
| 565 |
return m_positions2d; |
| 566 |
} |
| 567 |
|
| 568 |
const Array<Vector2>& Molecule::atomPositions2d() const |
| 569 |
{ |
| 570 |
return m_positions2d; |
| 571 |
} |
| 572 |
|
| 573 |
Array<Vector3>& Molecule::atomPositions3d() |
| 574 |
{ |
| 575 |
return m_positions3d; |
| 576 |
} |
| 577 |
|
| 578 |
const Array<Vector3>& Molecule::atomPositions3d() const |
| 579 |
{ |
| 580 |
return m_positions3d; |
| 581 |
} |
| 582 |
|
| 583 |
const Molecule::CustomElementMap& Molecule::customElementMap() const |
| 584 |
{ |
| 585 |
return m_customElementMap; |
| 586 |
} |
| 587 |
|
| 588 |
void Molecule::setCustomElementMap(const Molecule::CustomElementMap& map) |
| 589 |
{ |
| 590 |
m_customElementMap = map; |
| 591 |
} |
| 592 |
|
| 593 |
Molecule::AtomType Molecule::addAtom(unsigned char number) |
| 594 |
{ |
| 595 |
m_graph.addVertex(); |
| 596 |
m_atomicNumbers.push_back(number); |
| 597 |
// we're not going to easily handle custom elements |
| 598 |
if (number < element_count) |
| 599 |
m_elements.set(number); |
| 600 |
else |
| 601 |
m_elements.set(element_count - 1); // custom element |
| 602 |
|
| 603 |
m_layers.addAtomToActiveLayer(atomCount() - 1); |
| 604 |
m_partialCharges.clear(); |
| 605 |
return AtomType(this, static_cast<Index>(atomCount() - 1)); |
| 606 |
} |
| 607 |
|
| 608 |
Molecule::AtomType Molecule::addAtom(unsigned char number, Vector3 position3d) |
| 609 |
{ |
| 610 |
if (m_positions3d.size() == atomCount()) { |
| 611 |
m_positions3d.push_back(position3d); |
| 612 |
} |
| 613 |
return addAtom(number); // Use virtual dispatch |
| 614 |
} |
| 615 |
|
| 616 |
void Molecule::swapBond(Index a, Index b) |
| 617 |
{ |
| 618 |
// Allow Argument Dependent Lookup for swap |
| 619 |
using std::swap; |
| 620 |
|
| 621 |
m_graph.swapEdgeIndices(a, b); |
| 622 |
swap(m_bondOrders[a], m_bondOrders[b]); |
| 623 |
} |
| 624 |
void Molecule::swapAtom(Index a, Index b) |
| 625 |
{ |
| 626 |
// Allow Argument Dependent Lookup for swap |
| 627 |
using std::swap; |
| 628 |
|
| 629 |
Index max = a > b ? a : b; |
| 630 |
if (m_positions2d.size() > max) |
| 631 |
swap(m_positions2d[a], m_positions2d[b]); |
| 632 |
if (m_positions3d.size() > max) |
| 633 |
swap(m_positions3d[a], m_positions3d[b]); |
| 634 |
if (m_hybridizations.size() > max) |
| 635 |
swap(m_hybridizations[a], m_hybridizations[b]); |
| 636 |
if (m_formalCharges.size() > max) |
| 637 |
swap(m_formalCharges[a], m_formalCharges[b]); |
| 638 |
if (m_colors.size() > max) |
| 639 |
swap(m_colors[a], m_colors[b]); |
| 640 |
|
| 641 |
swap(m_atomicNumbers[a], m_atomicNumbers[b]); |
| 642 |
m_graph.swapVertexIndices(a, b); |
| 643 |
m_layers.swapLayer(a, b); |
| 644 |
} |
| 645 |
|
| 646 |
bool Molecule::removeAtom(Index index) |
| 647 |
{ |
| 648 |
if (index >= atomCount()) |
| 649 |
return false; |
| 650 |
if (m_positions2d.size() == atomCount()) |
| 651 |
m_positions2d.swapAndPop(index); |
| 652 |
if (m_positions3d.size() == atomCount()) |
| 653 |
m_positions3d.swapAndPop(index); |
| 654 |
if (m_hybridizations.size() == atomCount()) |
| 655 |
m_hybridizations.swapAndPop(index); |
| 656 |
if (m_formalCharges.size() == atomCount()) |
| 657 |
m_formalCharges.swapAndPop(index); |
| 658 |
if (m_colors.size() == atomCount()) |
| 659 |
m_colors.swapAndPop(index); |
| 660 |
|
| 661 |
if (m_selectedAtoms.size() == atomCount()) { |
| 662 |
// swap and pop on std::vector<bool> |
| 663 |
if (index != m_selectedAtoms.size() - 1) { |
| 664 |
m_selectedAtoms[index] = m_selectedAtoms.back(); |
| 665 |
} |
| 666 |
m_selectedAtoms.pop_back(); |
| 667 |
} |
| 668 |
|
| 669 |
m_partialCharges.clear(); |
| 670 |
removeBonds(index); |
| 671 |
|
| 672 |
// before we remove, check if there's any other atom of this element |
| 673 |
// (e.g., we removed the last oxygen) |
| 674 |
auto elementToRemove = m_atomicNumbers[index]; |
| 675 |
bool foundAnother = false; |
| 676 |
for (Index i = 0; i < atomCount(); ++i) { |
| 677 |
if (i == index) |
| 678 |
continue; |
| 679 |
|
| 680 |
if (m_atomicNumbers[index] == elementToRemove) { |
| 681 |
foundAnother = true; |
| 682 |
break; // we're done |
| 683 |
} |
| 684 |
} |
| 685 |
if (!foundAnother) |
| 686 |
m_elements.reset(elementToRemove); |
| 687 |
|
| 688 |
m_atomicNumbers.swapAndPop(index); |
| 689 |
m_graph.removeVertex(index); |
| 690 |
|
| 691 |
m_layers.removeAtom(index); |
| 692 |
|
| 693 |
return true; |
| 694 |
} |
| 695 |
|
| 696 |
bool Molecule::removeAtom(const AtomType& atom_) |
| 697 |
{ |
| 698 |
return removeAtom(atom_.index()); |
| 699 |
} |
| 700 |
|
| 701 |
void Molecule::clearAtoms() |
| 702 |
{ |
| 703 |
m_positions2d.clear(); |
| 704 |
m_positions3d.clear(); |
| 705 |
m_atomLabels.clear(); |
| 706 |
m_hybridizations.clear(); |
| 707 |
m_formalCharges.clear(); |
| 708 |
m_colors.clear(); |
| 709 |
m_atomicNumbers.clear(); |
| 710 |
m_bondOrders.clear(); |
| 711 |
m_bondLabels.clear(); |
| 712 |
m_residueLabels.clear(); |
| 713 |
m_graph.clear(); |
| 714 |
m_partialCharges.clear(); |
| 715 |
m_elements.reset(); |
| 716 |
} |
| 717 |
|
| 718 |
Molecule::AtomType Molecule::atom(Index index) const |
| 719 |
{ |
| 720 |
assert(index < atomCount()); |
| 721 |
return AtomType(const_cast<Molecule*>(this), index); |
| 722 |
} |
| 723 |
|
| 724 |
Molecule::BondType Molecule::addBond(Index atom1, Index atom2, |
| 725 |
unsigned char order) |
| 726 |
{ |
| 727 |
assert(atom1 < m_atomicNumbers.size()); |
| 728 |
assert(atom2 < m_atomicNumbers.size()); |
| 729 |
Index index = bond(atom1, atom2).index(); |
| 730 |
if (index >= bondCount()) { |
| 731 |
m_graph.addEdge(atom1, atom2); |
| 732 |
m_bondOrders.push_back(order); |
| 733 |
index = static_cast<Index>(m_graph.edgeCount() - 1); |
| 734 |
} else { |
| 735 |
m_bondOrders[index] = order; |
| 736 |
} |
| 737 |
// any existing charges are invalidated |
| 738 |
m_partialCharges.clear(); |
| 739 |
return BondType(this, index); |
| 740 |
} |
| 741 |
|
| 742 |
Molecule::BondType Molecule::addBond(const AtomType& a, const AtomType& b, |
| 743 |
unsigned char order) |
| 744 |
{ |
| 745 |
assert(a.isValid() && a.molecule() == this); |
| 746 |
assert(b.isValid() && b.molecule() == this); |
| 747 |
|
| 748 |
return addBond(a.index(), b.index(), order); |
| 749 |
} |
| 750 |
|
| 751 |
size_t calcNlogN(size_t n) |
| 752 |
{ |
| 753 |
size_t aproxLog = 1; |
| 754 |
auto aux = static_cast<float>(n); |
| 755 |
while (aux > 2.0f) { |
| 756 |
aux /= 2.0f; |
| 757 |
++aproxLog; |
| 758 |
} |
| 759 |
return n * aproxLog; |
| 760 |
} |
| 761 |
|
| 762 |
bool Molecule::removeBond(Index index) |
| 763 |
{ |
| 764 |
if (index >= bondCount()) |
| 765 |
return false; |
| 766 |
m_graph.removeEdge(index); |
| 767 |
m_bondOrders.swapAndPop(index); |
| 768 |
m_partialCharges.clear(); |
| 769 |
return true; |
| 770 |
} |
| 771 |
|
| 772 |
bool Molecule::removeBond(const BondType& bond_) |
| 773 |
{ |
| 774 |
return removeBond(bond_.index()); |
| 775 |
} |
| 776 |
|
| 777 |
bool Molecule::removeBond(Index a, Index b) |
| 778 |
{ |
| 779 |
return removeBond(bond(a, b).index()); |
| 780 |
} |
| 781 |
|
| 782 |
bool Molecule::removeBond(const AtomType& a, const AtomType& b) |
| 783 |
{ |
| 784 |
return removeBond(bond(a, b).index()); |
| 785 |
} |
| 786 |
|
| 787 |
void Molecule::clearBonds() |
| 788 |
{ |
| 789 |
m_bondOrders.clear(); |
| 790 |
m_graph.removeEdges(); |
| 791 |
m_graph.setSize(atomCount()); |
| 792 |
m_partialCharges.clear(); |
| 793 |
} |
| 794 |
|
| 795 |
Molecule::BondType Molecule::bond(Index index) const |
| 796 |
{ |
| 797 |
assert(index < bondCount()); |
| 798 |
|
| 799 |
return BondType(const_cast<Molecule*>(this), index); |
| 800 |
} |
| 801 |
|
| 802 |
Molecule::BondType Molecule::bond(const AtomType& a, const AtomType& b) const |
| 803 |
{ |
| 804 |
assert(a.isValid() && a.molecule() == this); |
| 805 |
assert(b.isValid() && b.molecule() == this); |
| 806 |
|
| 807 |
return bond(a.index(), b.index()); |
| 808 |
} |
| 809 |
|
| 810 |
Molecule::BondType Molecule::bond(Index atomId1, Index atomId2) const |
| 811 |
{ |
| 812 |
assert(atomId1 < atomCount()); |
| 813 |
assert(atomId2 < atomCount()); |
| 814 |
|
| 815 |
const std::vector<Index>& edgeIndices = m_graph.edges(atomId1); |
| 816 |
for (unsigned long index : edgeIndices) { |
| 817 |
const std::pair<Index, Index>& pair = m_graph.endpoints(index); |
| 818 |
if (pair.first == atomId2 || pair.second == atomId2) |
| 819 |
return BondType(const_cast<Molecule*>(this), index); |
| 820 |
} |
| 821 |
return BondType(); |
| 822 |
} |
| 823 |
|
| 824 |
Array<Molecule::BondType> Molecule::bonds(const AtomType& a) |
| 825 |
{ |
| 826 |
if (!a.isValid()) |
| 827 |
return Array<BondType>(); |
| 828 |
|
| 829 |
return bonds(a.index()); |
| 830 |
} |
| 831 |
|
| 832 |
Array<const Molecule::BondType*> Molecule::bonds(Index a) const |
| 833 |
{ |
| 834 |
Array<const BondType*> atomBonds; |
| 835 |
if (a < atomCount()) { |
| 836 |
const std::vector<Index>& edgeIndices = m_graph.edges(a); |
| 837 |
for (unsigned long index : edgeIndices) { |
| 838 |
if (m_graph.endpoints(index).first == a || |
| 839 |
m_graph.endpoints(index).second == a) { |
| 840 |
// work around to consult bonds without breaking constantness |
| 841 |
atomBonds.push_back(new BondType(const_cast<Molecule*>(this), index)); |
| 842 |
} |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
std::sort(atomBonds.begin(), atomBonds.end(), |
| 847 |
[](const BondType*& ba, const BondType*& bb) { |
| 848 |
return ba->index() < bb->index(); |
| 849 |
}); |
| 850 |
return atomBonds; |
| 851 |
} |
| 852 |
|
| 853 |
Array<Molecule::BondType> Molecule::bonds(Index a) |
| 854 |
{ |
| 855 |
Array<BondType> atomBonds; |
| 856 |
if (a < atomCount()) { |
| 857 |
const std::vector<Index>& edgeIndices = m_graph.edges(a); |
| 858 |
for (unsigned long index : edgeIndices) { |
| 859 |
auto bond = bondPair(index); |
| 860 |
if (bond.first == a || bond.second == a) |
| 861 |
atomBonds.push_back(BondType(this, index)); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
std::sort(atomBonds.begin(), atomBonds.end(), |
| 866 |
[](BondType& ba, BondType& bb) { return ba.index() < bb.index(); }); |
| 867 |
return atomBonds; |
| 868 |
} |
| 869 |
|
| 870 |
Mesh* Molecule::addMesh() |
| 871 |
{ |
| 872 |
m_meshes.push_back(new Mesh); |
| 873 |
return m_meshes.back(); |
| 874 |
} |
| 875 |
|
| 876 |
Mesh* Molecule::mesh(Index index) |
| 877 |
{ |
| 878 |
if (index < static_cast<Index>(m_meshes.size())) |
| 879 |
return m_meshes[index]; |
| 880 |
else |
| 881 |
return nullptr; |
| 882 |
} |
| 883 |
|
| 884 |
const Mesh* Molecule::mesh(Index index) const |
| 885 |
{ |
| 886 |
if (index < static_cast<Index>(m_meshes.size())) |
| 887 |
return m_meshes[index]; |
| 888 |
else |
| 889 |
return nullptr; |
| 890 |
} |
| 891 |
|
| 892 |
void Molecule::clearMeshes() |
| 893 |
{ |
| 894 |
while (!m_meshes.empty()) { |
| 895 |
delete m_meshes.back(); |
| 896 |
m_meshes.pop_back(); |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
Cube* Molecule::addCube() |
| 901 |
{ |
| 902 |
m_cubes.push_back(new Cube); |
| 903 |
return m_cubes.back(); |
| 904 |
} |
| 905 |
|
| 906 |
Cube* Molecule::cube(Index index) |
| 907 |
{ |
| 908 |
if (index < static_cast<Index>(m_cubes.size())) |
| 909 |
return m_cubes[index]; |
| 910 |
else |
| 911 |
return nullptr; |
| 912 |
} |
| 913 |
|
| 914 |
const Cube* Molecule::cube(Index index) const |
| 915 |
{ |
| 916 |
if (index < static_cast<Index>(m_cubes.size())) |
| 917 |
return m_cubes[index]; |
| 918 |
else |
| 919 |
return nullptr; |
| 920 |
} |
| 921 |
|
| 922 |
void Molecule::clearCubes() |
| 923 |
{ |
| 924 |
while (!m_cubes.empty()) { |
| 925 |
delete m_cubes.back(); |
| 926 |
m_cubes.pop_back(); |
| 927 |
} |
| 928 |
m_activeCubeIndex = 0; |
| 929 |
} |
| 930 |
|
| 931 |
bool Molecule::setActiveCubeIndex(Index index) |
| 932 |
{ |
| 933 |
if (index < static_cast<Index>(m_cubes.size())) { |
| 934 |
m_activeCubeIndex = index; |
| 935 |
return true; |
| 936 |
} |
| 937 |
return false; |
| 938 |
} |
| 939 |
|
| 940 |
Cube* Molecule::activeCube() |
| 941 |
{ |
| 942 |
if (m_activeCubeIndex < static_cast<Index>(m_cubes.size())) |
| 943 |
return m_cubes[m_activeCubeIndex]; |
| 944 |
else if (!m_cubes.empty()) |
| 945 |
return m_cubes[0]; |
| 946 |
return nullptr; |
| 947 |
} |
| 948 |
|
| 949 |
const Cube* Molecule::activeCube() const |
| 950 |
{ |
| 951 |
if (m_activeCubeIndex < static_cast<Index>(m_cubes.size())) |
| 952 |
return m_cubes[m_activeCubeIndex]; |
| 953 |
else if (!m_cubes.empty()) |
| 954 |
return m_cubes[0]; |
| 955 |
return nullptr; |
| 956 |
} |
| 957 |
|
| 958 |
std::string Molecule::formula(const std::string& delimiter, int over) const |
| 959 |
{ |
| 960 |
// A map of element symbols (including isotopes) to their quantity. |
| 961 |
std::map<std::string, size_t> componentsCount = formulaComposition(); |
| 962 |
|
| 963 |
std::stringstream result; |
| 964 |
std::map<std::string, size_t>::iterator iter; |
| 965 |
|
| 966 |
// Carbons first |
| 967 |
iter = componentsCount.find("C"); |
| 968 |
if (iter != componentsCount.end()) { |
| 969 |
result << "C"; |
| 970 |
if (iter->second > static_cast<size_t>(over)) |
| 971 |
result << delimiter << iter->second; |
| 972 |
componentsCount.erase(iter); |
| 973 |
|
| 974 |
// If carbon is present, hydrogens are next (including D and T). |
| 975 |
iter = componentsCount.find("H"); |
| 976 |
if (iter != componentsCount.end()) { |
| 977 |
result << delimiter << "H"; |
| 978 |
if (iter->second > static_cast<size_t>(over)) |
| 979 |
result << delimiter << iter->second; |
| 980 |
componentsCount.erase(iter); |
| 981 |
} |
| 982 |
iter = componentsCount.find("D"); |
| 983 |
if (iter != componentsCount.end()) { |
| 984 |
result << delimiter << "D"; |
| 985 |
if (iter->second > static_cast<size_t>(over)) |
| 986 |
result << delimiter << iter->second; |
| 987 |
componentsCount.erase(iter); |
| 988 |
} |
| 989 |
iter = componentsCount.find("T"); |
| 990 |
if (iter != componentsCount.end()) { |
| 991 |
result << delimiter << "T"; |
| 992 |
if (iter->second > static_cast<size_t>(over)) |
| 993 |
result << delimiter << iter->second; |
| 994 |
componentsCount.erase(iter); |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
// The rest (alphabetically, since std::map<std::string> sorts by key): |
| 999 |
iter = componentsCount.begin(); |
| 1000 |
while (iter != componentsCount.end()) { |
| 1001 |
result << delimiter << iter->first; |
| 1002 |
if (iter->second > static_cast<size_t>(over)) |
| 1003 |
result << delimiter << iter->second; |
| 1004 |
++iter; |
| 1005 |
} |
| 1006 |
|
| 1007 |
return result.str(); |
| 1008 |
} |
| 1009 |
|
| 1010 |
void Molecule::setUnitCell(UnitCell* uc) |
| 1011 |
{ |
| 1012 |
if (uc != m_unitCell) { |
| 1013 |
delete m_unitCell; |
| 1014 |
m_unitCell = uc; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
double Molecule::mass() const |
| 1019 |
{ |
| 1020 |
double m(0.0); |
| 1021 |
for (Index i = 0; i < atomCount(); ++i) { |
| 1022 |
if (isotope(i) > 0) |
| 1023 |
m += Elements::isotopeMass(m_atomicNumbers[i], isotope(i)); |
| 1024 |
else |
| 1025 |
m += Elements::mass(m_atomicNumbers[i]); |
| 1026 |
} |
| 1027 |
return m; |
| 1028 |
} |
| 1029 |
|
| 1030 |
double Molecule::monoisotopicMass() const |
| 1031 |
{ |
| 1032 |
double m(0.0); |
| 1033 |
for (Index i = 0; i < atomCount(); ++i) |
| 1034 |
// default will be the most common isotope |
| 1035 |
m += Elements::isotopeMass(m_atomicNumbers[i], isotope(i)); |
| 1036 |
return m; |
| 1037 |
} |
| 1038 |
|
| 1039 |
Vector3 Molecule::centerOfGeometry() const |
| 1040 |
{ |
| 1041 |
Vector3 center(0.0, 0.0, 0.0); |
| 1042 |
for (Index i = 0; i < atomCount(); ++i) |
| 1043 |
center += atom(i).position3d(); |
| 1044 |
return center / atomCount(); |
| 1045 |
} |
| 1046 |
|
| 1047 |
Vector3 Molecule::centerOfMass() const |
| 1048 |
{ |
| 1049 |
Vector3 center(0.0, 0.0, 0.0); |
| 1050 |
Real totalMass = 0.0; |
| 1051 |
for (Index i = 0; i < atomCount(); ++i) { |
| 1052 |
AtomType curr_atom = atom(i); |
| 1053 |
auto mass = Elements::mass(m_atomicNumbers[i]); |
| 1054 |
if (isotope(i) > 0) |
| 1055 |
mass = Elements::isotopeMass(m_atomicNumbers[i], isotope(i)); |
| 1056 |
|
| 1057 |
center += (curr_atom.position3d() * mass); |
| 1058 |
totalMass += mass; |
| 1059 |
} |
| 1060 |
center /= totalMass; |
| 1061 |
return center; |
| 1062 |
} |
| 1063 |
|
| 1064 |
double Molecule::radius() const |
| 1065 |
{ |
| 1066 |
double radius = 0.0; |
| 1067 |
if (atomCount() > 0) { |
| 1068 |
radius = (centerOfGeometry() - atom(0).position3d()).norm(); |
| 1069 |
} |
| 1070 |
return radius; |
| 1071 |
} |
| 1072 |
|
| 1073 |
std::pair<Vector3, Vector3> Molecule::bestFitPlane() const |
| 1074 |
{ |
| 1075 |
return bestFitPlane(atomPositions3d()); |
| 1076 |
} |
| 1077 |
|
| 1078 |
std::pair<Vector3, Vector3> Molecule::bestFitPlane(const Array<Vector3>& pos) |
| 1079 |
{ |
| 1080 |
// copy coordinates to matrix in Eigen format |
| 1081 |
size_t num_atoms = pos.size(); |
| 1082 |
assert(num_atoms >= 3); |
| 1083 |
Eigen::Matrix<Vector3::Scalar, Eigen::Dynamic, Eigen::Dynamic> coord( |
| 1084 |
3, num_atoms); |
| 1085 |
for (size_t i = 0; i < num_atoms; ++i) { |
| 1086 |
coord.col(i) = pos[i]; |
| 1087 |
} |
| 1088 |
|
| 1089 |
// calculate centroid |
| 1090 |
Vector3 centroid = coord.rowwise().mean(); |
| 1091 |
|
| 1092 |
// subtract centroid |
| 1093 |
coord.colwise() -= centroid; |
| 1094 |
|
| 1095 |
// we only need the left-singular matrix |
| 1096 |
Eigen::JacobiSVD<MatrixX> svd(coord, |
| 1097 |
Eigen::ComputeThinU | Eigen::ComputeThinV); |
| 1098 |
Vector3 plane_normal = svd.matrixU().rightCols<1>(); |
| 1099 |
|
| 1100 |
return std::make_pair(centroid, plane_normal); |
| 1101 |
} |
| 1102 |
|
| 1103 |
Array<double> Molecule::vibrationFrequencies() const |
| 1104 |
{ |
| 1105 |
return m_vibrationFrequencies; |
| 1106 |
} |
| 1107 |
|
| 1108 |
void Molecule::setVibrationFrequencies(const Array<double>& freq) |
| 1109 |
{ |
| 1110 |
m_vibrationFrequencies = freq; |
| 1111 |
} |
| 1112 |
|
| 1113 |
Array<double> Molecule::vibrationIRIntensities() const |
| 1114 |
{ |
| 1115 |
return m_vibrationIRIntensities; |
| 1116 |
} |
| 1117 |
|
| 1118 |
void Molecule::setVibrationIRIntensities(const Array<double>& intensities) |
| 1119 |
{ |
| 1120 |
m_vibrationIRIntensities = intensities; |
| 1121 |
} |
| 1122 |
|
| 1123 |
Array<double> Molecule::vibrationRamanIntensities() const |
| 1124 |
{ |
| 1125 |
return m_vibrationRamanIntensities; |
| 1126 |
} |
| 1127 |
|
| 1128 |
void Molecule::setVibrationRamanIntensities(const Array<double>& intensities) |
| 1129 |
{ |
| 1130 |
m_vibrationRamanIntensities = intensities; |
| 1131 |
} |
| 1132 |
|
| 1133 |
Array<Vector3> Molecule::vibrationLx(int mode) const |
| 1134 |
{ |
| 1135 |
if (mode >= 0 && mode < static_cast<int>(m_vibrationLx.size())) |
| 1136 |
return m_vibrationLx[mode]; |
| 1137 |
return Array<Vector3>(); |
| 1138 |
} |
| 1139 |
|
| 1140 |
void Molecule::setVibrationLx(const Array<Array<Vector3>>& lx) |
| 1141 |
{ |
| 1142 |
m_vibrationLx = lx; |
| 1143 |
} |
| 1144 |
|
| 1145 |
void Molecule::perceiveBondOrders() |
| 1146 |
{ |
| 1147 |
// check for coordinates and that there are some bonds |
| 1148 |
if (m_positions3d.size() != atomCount() || m_positions3d.size() < 2 || |
| 1149 |
m_graph.edgeCount() == 0) |
| 1150 |
return; |
| 1151 |
|
| 1152 |
// save the existing bonds and bond orders |
| 1153 |
// first calculate the unsaturated valence for every atom |
| 1154 |
Array<unsigned char> originalBonds = m_bondOrders; |
| 1155 |
Array<unsigned char> unsaturatedValence(atomCount(), 0); |
| 1156 |
bool anyUnsaturated = false; |
| 1157 |
for (Index i = 0; i < atomCount(); ++i) { |
| 1158 |
unsigned char boSum = 0; |
| 1159 |
for (auto bond : bonds(i)) { |
| 1160 |
boSum += bond.order(); |
| 1161 |
} |
| 1162 |
unsaturatedValence[i] = |
| 1163 |
atomValence(atomicNumber(i), formalCharge(i), bonds(i).size()) - boSum; |
| 1164 |
|
| 1165 |
if (unsaturatedValence[i] > 0) |
| 1166 |
anyUnsaturated = true; |
| 1167 |
} |
| 1168 |
|
| 1169 |
Index startIndex = 0; |
| 1170 |
Index initialAtom = 0; |
| 1171 |
while (anyUnsaturated) { |
| 1172 |
|
| 1173 |
// okay, we're first going to try placing *one* bond from our start atom |
| 1174 |
// .. then we can try placing bonds anywhere |
| 1175 |
|
| 1176 |
// find the first atom with unsaturated valence of ONE |
| 1177 |
bool foundStart = false; |
| 1178 |
for (Index i = startIndex; i < atomCount(); ++i) { |
| 1179 |
if (unsaturatedValence[i] == 1) { |
| 1180 |
startIndex = i; |
| 1181 |
foundStart = true; |
| 1182 |
break; |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
// if we didn't find an atom with unsaturated valence of ONE, |
| 1187 |
// .. then find *something* |
| 1188 |
if (!foundStart) { |
| 1189 |
for (Index i = startIndex; i < atomCount(); ++i) { |
| 1190 |
if (unsaturatedValence[i] > 0) { |
| 1191 |
startIndex = i; |
| 1192 |
foundStart = true; |
| 1193 |
break; |
| 1194 |
} |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
if (foundStart) { |
| 1199 |
// std::cerr << "Found start index " << startIndex << std::endl; |
| 1200 |
|
| 1201 |
// look at the neighbors of our start atom |
| 1202 |
Index bestIndex = MaxIndex; |
| 1203 |
unsigned bestValence = 256; // something impossible |
| 1204 |
Real bestDistance = 100.0; // 10 Angstroms squared |
| 1205 |
Vector3 startPosition = m_positions3d[startIndex]; |
| 1206 |
// iterate through the Indexes of the neighbors |
| 1207 |
for (auto neighbor : graph().neighbors(startIndex)) { |
| 1208 |
// if this neighbor doesn't have an unsaturated valence, skip it |
| 1209 |
if (unsaturatedValence[neighbor] == 0) { |
| 1210 |
continue; |
| 1211 |
} |
| 1212 |
|
| 1213 |
if (unsaturatedValence[neighbor] < bestValence) { |
| 1214 |
bestIndex = neighbor; |
| 1215 |
bestValence = unsaturatedValence[neighbor]; |
| 1216 |
bestDistance = |
| 1217 |
(m_positions3d[neighbor] - startPosition).squaredNorm(); |
| 1218 |
} else if (unsaturatedValence[neighbor] == bestValence) { |
| 1219 |
// check if this neighbor is closer |
| 1220 |
Real distance = |
| 1221 |
(m_positions3d[neighbor] - startPosition).squaredNorm(); |
| 1222 |
if (distance < bestDistance) { |
| 1223 |
bestIndex = neighbor; |
| 1224 |
bestDistance = distance; |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
// if we found a neighbor, then we can assign a bond order and update |
| 1229 |
// charges |
| 1230 |
if (bestIndex != MaxIndex) { |
| 1231 |
/*std::cerr << "Assigning bond " << startIndex << " " << bestIndex |
| 1232 |
<< std::endl; */ |
| 1233 |
|
| 1234 |
// assign the bond order |
| 1235 |
m_bondOrders[bond(startIndex, bestIndex).index()] += 1; |
| 1236 |
// update the unsaturated valence of the start atom |
| 1237 |
unsaturatedValence[startIndex] -= 1; |
| 1238 |
// update the unsaturated valence of the neighbor atom |
| 1239 |
unsaturatedValence[bestIndex] -= 1; |
| 1240 |
|
| 1241 |
startIndex = 0; // we can now try placing bonds anywhere |
| 1242 |
} else { |
| 1243 |
startIndex += 1; |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
// TODO: update the current formal charges |
| 1248 |
|
| 1249 |
anyUnsaturated = false; // check if we're done |
| 1250 |
for (Index i = 0; i < atomCount(); ++i) { |
| 1251 |
if (unsaturatedValence[i] > 0) { |
| 1252 |
anyUnsaturated = true; |
| 1253 |
break; |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
if (!foundStart && anyUnsaturated) { |
| 1258 |
// we've gone through and it's not working |
| 1259 |
// try a new starting atom and reset the bond orders |
| 1260 |
// std::cerr << " didn't work " << initialAtom << std::endl; |
| 1261 |
|
| 1262 |
initialAtom += 1; |
| 1263 |
startIndex = initialAtom; |
| 1264 |
for (Index i = 0; i < m_bondOrders.size(); ++i) { |
| 1265 |
unsigned change = m_bondOrders[i] - originalBonds[i]; |
| 1266 |
if (change > 0) { |
| 1267 |
// update the valences |
| 1268 |
unsaturatedValence[bond(i).atom1().index()] += change; |
| 1269 |
unsaturatedValence[bond(i).atom2().index()] += change; |
| 1270 |
} |
| 1271 |
m_bondOrders[i] = originalBonds[i]; |
| 1272 |
} |
| 1273 |
} |
| 1274 |
|
| 1275 |
if (initialAtom >= atomCount()) { |
| 1276 |
break; |
| 1277 |
} |
| 1278 |
|
| 1279 |
} // keep going until we've assigned all the bond orders |
| 1280 |
} |
| 1281 |
|
| 1282 |
void Molecule::perceiveBondsSimple(const double tolerance, const double min) |
| 1283 |
{ |
| 1284 |
// check for coordinates |
| 1285 |
if (m_positions3d.size() != atomCount() || m_positions3d.size() < 2) |
| 1286 |
return; |
| 1287 |
|
| 1288 |
// cache atomic radii |
| 1289 |
std::vector<double> radii(atomCount()); |
| 1290 |
double max_radius = 0.0; |
| 1291 |
for (size_t i = 0; i < radii.size(); i++) { |
| 1292 |
radii[i] = Elements::radiusCovalent(atomicNumber(i)); |
| 1293 |
if (radii[i] <= 0.0) |
| 1294 |
radii[i] = 2.0; |
| 1295 |
if (radii[i] > max_radius) |
| 1296 |
max_radius = radii[i]; |
| 1297 |
} |
| 1298 |
|
| 1299 |
float maxDistance = 2.0 * max_radius + tolerance; |
| 1300 |
auto neighborPerceiver = NeighborPerceiver(m_positions3d, maxDistance); |
| 1301 |
|
| 1302 |
// check for bonds |
| 1303 |
// O(n) average-case, O(n^2) worst-case |
| 1304 |
// note that the "worst case" here would need to be an invalid molecule |
| 1305 |
Array<Index> neighbors; |
| 1306 |
for (Index i = 0; i < atomCount(); i++) { |
| 1307 |
Vector3 ipos = m_positions3d[i]; |
| 1308 |
neighborPerceiver.getNeighborsInclusiveInPlace(neighbors, ipos); |
| 1309 |
for (unsigned long j : neighbors) { |
| 1310 |
double cutoff = radii[i] + radii[j] + tolerance; |
| 1311 |
Vector3 jpos = m_positions3d[j]; |
| 1312 |
Vector3 diff = jpos - ipos; |
| 1313 |
|
| 1314 |
// Don't automatically bond nobel gases to anything |
| 1315 |
switch (atomicNumber(i)) { |
| 1316 |
case 2: // He |
| 1317 |
case 10: // Ne |
| 1318 |
case 18: // Ar |
| 1319 |
case 36: // Kr |
| 1320 |
continue; |
| 1321 |
default: |
| 1322 |
break; |
| 1323 |
} |
| 1324 |
|
| 1325 |
// now for the other atom |
| 1326 |
switch (atomicNumber(j)) { |
| 1327 |
case 2: // He |
| 1328 |
case 10: // Ne |
| 1329 |
case 18: // Ar |
| 1330 |
case 36: // Kr |
| 1331 |
continue; |
| 1332 |
default: |
| 1333 |
break; |
| 1334 |
} |
| 1335 |
|
| 1336 |
if (std::fabs(diff[0]) > cutoff || std::fabs(diff[1]) > cutoff || |
| 1337 |
std::fabs(diff[2]) > cutoff || |
| 1338 |
(atomicNumber(i) == 1 && atomicNumber(j) == 1)) |
| 1339 |
continue; |
| 1340 |
|
| 1341 |
// check radius and add bond if needed |
| 1342 |
double cutoffSq = cutoff * cutoff; |
| 1343 |
double diffsq = diff.squaredNorm(); |
| 1344 |
if (diffsq < cutoffSq && diffsq > min * min) |
| 1345 |
addBond(atom(i), atom(j), 1); |
| 1346 |
} |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
void Molecule::perceiveBondsFromResidueData() |
| 1351 |
{ |
| 1352 |
for (auto& m_residue : m_residues) { |
| 1353 |
m_residue.resolveResidueBonds(*this); |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
size_t Molecule::coordinate3dCount() const |
| 1358 |
{ |
| 1359 |
return m_coordinates3d.size(); |
| 1360 |
} |
| 1361 |
|
| 1362 |
bool Molecule::setCoordinate3d(int coord) |
| 1363 |
{ |
| 1364 |
if (coord >= 0 && coord < static_cast<int>(m_coordinates3d.size())) { |
| 1365 |
m_positions3d = m_coordinates3d[coord]; |
| 1366 |
return true; |
| 1367 |
} |
| 1368 |
return false; |
| 1369 |
} |
| 1370 |
|
| 1371 |
void Molecule::clearCoordinate3d() |
| 1372 |
{ |
| 1373 |
m_coordinates3d.clear(); |
| 1374 |
} |
| 1375 |
|
| 1376 |
Array<Vector3> Molecule::coordinate3d(size_t index) const |
| 1377 |
{ |
| 1378 |
return m_coordinates3d[index]; |
| 1379 |
} |
| 1380 |
|
| 1381 |
bool Molecule::setCoordinate3d(const Array<Vector3>& coords, size_t index) |
| 1382 |
{ |
| 1383 |
if (m_coordinates3d.size() <= index) |
| 1384 |
m_coordinates3d.resize(index + 1); |
| 1385 |
m_coordinates3d[index] = coords; |
| 1386 |
return true; |
| 1387 |
} |
| 1388 |
|
| 1389 |
void Molecule::estimateVelocities() |
| 1390 |
{ |
| 1391 |
if (m_coordinates3d.size() < 2 || |
| 1392 |
m_timesteps.size() != m_coordinates3d.size()) |
| 1393 |
return; |
| 1394 |
|
| 1395 |
m_velocities.resize(m_coordinates3d.size()); |
| 1396 |
|
| 1397 |
for (size_t i = 0; i < m_coordinates3d.size(); ++i) { |
| 1398 |
double dt = 0.0; |
| 1399 |
if (i > 0) { |
| 1400 |
dt = m_timesteps[i] - m_timesteps[i - 1]; |
| 1401 |
} else if (m_timesteps.size() > 1) { |
| 1402 |
dt = m_timesteps[1] - m_timesteps[0]; |
| 1403 |
} |
| 1404 |
|
| 1405 |
if (std::abs(dt) < 1e-6) |
| 1406 |
continue; |
| 1407 |
|
| 1408 |
m_velocities[i].resize(atomCount()); |
| 1409 |
|
| 1410 |
const Array<Vector3>& currentCoords = m_coordinates3d[i]; |
| 1411 |
const Array<Vector3>& prevCoords = |
| 1412 |
(i > 0) ? m_coordinates3d[i - 1] : m_coordinates3d[0]; |
| 1413 |
const Array<Vector3>& nextCoords = (i < m_coordinates3d.size() - 1) |
| 1414 |
? m_coordinates3d[i + 1] |
| 1415 |
: m_coordinates3d[i]; |
| 1416 |
|
| 1417 |
if (i > 0) { |
| 1418 |
// Backward difference for i > 0 |
| 1419 |
for (Index j = 0; j < atomCount(); ++j) { |
| 1420 |
m_velocities[i][j] = (currentCoords[j] - prevCoords[j]) / dt; |
| 1421 |
} |
| 1422 |
} else { |
| 1423 |
// Forward difference for i == 0 |
| 1424 |
for (Index j = 0; j < atomCount(); ++j) { |
| 1425 |
m_velocities[i][j] = (nextCoords[j] - currentCoords[j]) / dt; |
| 1426 |
} |
| 1427 |
} |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
Array<Vector3> Molecule::velocities(int index) const |
| 1432 |
{ |
| 1433 |
if (index >= 0 && index < static_cast<int>(m_velocities.size())) |
| 1434 |
return m_velocities[index]; |
| 1435 |
return Array<Vector3>(); |
| 1436 |
} |
| 1437 |
|
| 1438 |
bool Molecule::setVelocities(const Array<Vector3>& velocities, int index) |
| 1439 |
{ |
| 1440 |
if (index < 0) |
| 1441 |
return false; |
| 1442 |
|
| 1443 |
if (static_cast<int>(m_velocities.size()) <= index) |
| 1444 |
m_velocities.resize(index + 1); |
| 1445 |
m_velocities[index] = velocities; |
| 1446 |
return true; |
| 1447 |
} |
| 1448 |
|
| 1449 |
void Molecule::clearVelocities() |
| 1450 |
{ |
| 1451 |
m_velocities.clear(); |
| 1452 |
} |
| 1453 |
|
| 1454 |
double Molecule::timeStep(int index, bool& status) |
| 1455 |
{ |
| 1456 |
if (static_cast<int>(m_timesteps.size()) <= index) { |
| 1457 |
status = false; |
| 1458 |
return 0.0; |
| 1459 |
} |
| 1460 |
status = true; |
| 1461 |
return m_timesteps[index]; |
| 1462 |
} |
| 1463 |
|
| 1464 |
bool Molecule::setTimeStep(double timestep, int index) |
| 1465 |
{ |
| 1466 |
if (static_cast<int>(m_timesteps.size()) <= index) |
| 1467 |
m_timesteps.resize(index + 1); |
| 1468 |
m_timesteps[index] = timestep; |
| 1469 |
return true; |
| 1470 |
} |
| 1471 |
|
| 1472 |
Array<Vector3>& Molecule::forceVectors() |
| 1473 |
{ |
| 1474 |
return m_forceVectors; |
| 1475 |
} |
| 1476 |
|
| 1477 |
const Array<Vector3>& Molecule::forceVectors() const |
| 1478 |
{ |
| 1479 |
return m_forceVectors; |
| 1480 |
} |
| 1481 |
|
| 1482 |
Residue& Molecule::addResidue(std::string& name, Index& number, char& id) |
| 1483 |
{ |
| 1484 |
Residue newResidue(name, number, id); |
| 1485 |
m_residues.push_back(newResidue); |
| 1486 |
return m_residues[m_residues.size() - 1]; |
| 1487 |
} |
| 1488 |
|
| 1489 |
void Molecule::addResidue(Residue& residue) |
| 1490 |
{ |
| 1491 |
m_residues.push_back(residue); |
| 1492 |
} |
| 1493 |
|
| 1494 |
Residue& Molecule::residue(Index index) |
| 1495 |
{ |
| 1496 |
return m_residues[index]; |
| 1497 |
} |
| 1498 |
|
| 1499 |
const Residue& Molecule::residue(Index index) const |
| 1500 |
{ |
| 1501 |
return m_residues[index]; |
| 1502 |
} |
| 1503 |
|
| 1504 |
Index Molecule::residueCount() const |
| 1505 |
{ |
| 1506 |
return static_cast<Index>(m_residues.size()); |
| 1507 |
} |
| 1508 |
|
| 1509 |
std::string Molecule::residueLabel(Index residueId) const |
| 1510 |
{ |
| 1511 |
return residueId < m_residueLabels.size() ? m_residueLabels[residueId] : ""; |
| 1512 |
} |
| 1513 |
|
| 1514 |
bool Molecule::setResidueLabels(const Core::Array<std::string>& labels) |
| 1515 |
{ |
| 1516 |
if (labels.size() == residueCount() || labels.size() == 0) { |
| 1517 |
m_residueLabels = labels; |
| 1518 |
return true; |
| 1519 |
} |
| 1520 |
return false; |
| 1521 |
} |
| 1522 |
|
| 1523 |
bool Molecule::setResidueLabel(Index residueId, const std::string& label) |
| 1524 |
{ |
| 1525 |
if (residueId < residueCount()) { |
| 1526 |
if (residueId >= m_residueLabels.size()) |
| 1527 |
m_residueLabels.resize(residueCount(), ""); |
| 1528 |
m_residueLabels[residueId] = label; |
| 1529 |
return true; |
| 1530 |
} |
| 1531 |
return false; |
| 1532 |
} |
| 1533 |
|
| 1534 |
bool Molecule::setBondPairs(const Array<std::pair<Index, Index>>& pairs) |
| 1535 |
{ |
| 1536 |
if (pairs.size() == bondCount()) { |
| 1537 |
Index bond = 0; |
| 1538 |
for (const auto& pair : pairs) { |
| 1539 |
setBondPair(bond, pair); |
| 1540 |
++bond; |
| 1541 |
} |
| 1542 |
return true; |
| 1543 |
} |
| 1544 |
return false; |
| 1545 |
} |
| 1546 |
|
| 1547 |
bool Molecule::setBondPair(Index bondId, const std::pair<Index, Index>& pair) |
| 1548 |
{ |
| 1549 |
if (bondId < bondCount()) { |
| 1550 |
m_graph.editEdgeInPlace(bondId, pair.first, pair.second); |
| 1551 |
return true; |
| 1552 |
} |
| 1553 |
return false; |
| 1554 |
} |
| 1555 |
|
| 1556 |
unsigned char Molecule::bondOrder(Index bondId) const |
| 1557 |
{ |
| 1558 |
return bondId < m_bondOrders.size() ? m_bondOrders[bondId] : 0; |
| 1559 |
} |
| 1560 |
|
| 1561 |
bool Molecule::setBondOrders(const Array<unsigned char>& orders) |
| 1562 |
{ |
| 1563 |
if (orders.size() == bondCount()) { |
| 1564 |
m_bondOrders = orders; |
| 1565 |
return true; |
| 1566 |
} |
| 1567 |
return false; |
| 1568 |
} |
| 1569 |
|
| 1570 |
bool Molecule::setBondOrder(Index bondId, unsigned char order) |
| 1571 |
{ |
| 1572 |
if (bondId < bondCount()) { |
| 1573 |
m_bondOrders[bondId] = order; |
| 1574 |
return true; |
| 1575 |
} |
| 1576 |
return false; |
| 1577 |
} |
| 1578 |
|
| 1579 |
Index Molecule::atomCount(unsigned char number) const |
| 1580 |
{ |
| 1581 |
Index count(0); |
| 1582 |
for (unsigned char m_atomicNumber : m_atomicNumbers) { |
| 1583 |
if (m_atomicNumber == number) |
| 1584 |
++count; |
| 1585 |
} |
| 1586 |
return count; |
| 1587 |
} |
| 1588 |
|
| 1589 |
bool Molecule::setAtomicNumbers(const Core::Array<unsigned char>& nums) |
| 1590 |
{ |
| 1591 |
if (nums.size() == atomCount()) { |
| 1592 |
m_atomicNumbers = nums; |
| 1593 |
|
| 1594 |
// update element mask |
| 1595 |
m_elements.reset(); |
| 1596 |
// update colors too |
| 1597 |
if (nums.size() == m_colors.size()) { |
| 1598 |
for (Index i = 0; i < nums.size(); ++i) { |
| 1599 |
m_colors[i] = Vector3ub(Elements::color(m_atomicNumbers[i])); |
| 1600 |
m_elements.set(m_atomicNumbers[i]); |
| 1601 |
} |
| 1602 |
} |
| 1603 |
|
| 1604 |
return true; |
| 1605 |
} |
| 1606 |
return false; |
| 1607 |
} |
| 1608 |
|
| 1609 |
bool Molecule::setAtomicNumber(Index atomId, unsigned char number) |
| 1610 |
{ |
| 1611 |
if (atomId < atomCount()) { |
| 1612 |
m_atomicNumbers[atomId] = number; |
| 1613 |
|
| 1614 |
// recalculate the element mask |
| 1615 |
m_elements.reset(); |
| 1616 |
for (unsigned char m_atomicNumber : m_atomicNumbers) { |
| 1617 |
m_elements.set(m_atomicNumber); |
| 1618 |
} |
| 1619 |
|
| 1620 |
// update colors too |
| 1621 |
if (atomId < m_colors.size()) |
| 1622 |
m_colors[atomId] = Vector3ub(Elements::color(number)); |
| 1623 |
|
| 1624 |
return true; |
| 1625 |
} |
| 1626 |
return false; |
| 1627 |
} |
| 1628 |
|
| 1629 |
bool Molecule::hasCustomElements() const |
| 1630 |
{ |
| 1631 |
for (unsigned char m_atomicNumber : m_atomicNumbers) { |
| 1632 |
if (Core::isCustomElement(m_atomicNumber)) |
| 1633 |
return true; |
| 1634 |
} |
| 1635 |
return false; |
| 1636 |
} |
| 1637 |
|
| 1638 |
std::map<unsigned char, size_t> Molecule::composition() const |
| 1639 |
{ |
| 1640 |
const double tolerance = 1.0e-3; |
| 1641 |
|
| 1642 |
// A map of atomic numbers to their quantity (using double for fractional |
| 1643 |
// contributions when there's a unit cell) |
| 1644 |
std::map<unsigned char, double> compositionDouble; |
| 1645 |
|
| 1646 |
// Check if we have a unit cell - if so, we need to account for fractional |
| 1647 |
// atoms at corners, edges, and faces |
| 1648 |
if (m_unitCell != nullptr) { |
| 1649 |
for (Index i = 0; i < atomCount(); ++i) { |
| 1650 |
unsigned char atomicNum = m_atomicNumbers[i]; |
| 1651 |
Vector3 fracCoords = m_unitCell->toFractional(m_positions3d[i]); |
| 1652 |
|
| 1653 |
// Count how many coordinates are at boundaries (0 or 1) |
| 1654 |
int boundaryCount = 0; |
| 1655 |
for (int j = 0; j < 3; ++j) { |
| 1656 |
double coord = fracCoords[j]; |
| 1657 |
// Check if close to 0 or 1 |
| 1658 |
if (std::fabs(coord) < tolerance || |
| 1659 |
std::fabs(coord - 1.0) < tolerance) { |
| 1660 |
++boundaryCount; |
| 1661 |
} |
| 1662 |
} |
| 1663 |
|
| 1664 |
// Calculate fractional contribution based on boundary count |
| 1665 |
// Corner atoms (3 boundaries): 1/8 |
| 1666 |
// Edge atoms (2 boundaries): 1/4 |
| 1667 |
// Face atoms (1 boundary): 1/2 |
| 1668 |
// Interior atoms (0 boundaries): 1 |
| 1669 |
double weight = 1.0; |
| 1670 |
if (boundaryCount == 3) { |
| 1671 |
weight = 1.0 / 8.0; |
| 1672 |
} else if (boundaryCount == 2) { |
| 1673 |
weight = 1.0 / 4.0; |
| 1674 |
} else if (boundaryCount == 1) { |
| 1675 |
weight = 1.0 / 2.0; |
| 1676 |
} |
| 1677 |
|
| 1678 |
compositionDouble[atomicNum] += weight; |
| 1679 |
} |
| 1680 |
} else { |
| 1681 |
// No unit cell, just count atoms normally |
| 1682 |
for (unsigned char atomicNum : m_atomicNumbers) { |
| 1683 |
compositionDouble[atomicNum] += 1.0; |
| 1684 |
} |
| 1685 |
} |
| 1686 |
|
| 1687 |
// Convert to size_t by rounding to nearest integer |
| 1688 |
std::map<unsigned char, size_t> composition; |
| 1689 |
for (const auto& pair : compositionDouble) { |
| 1690 |
size_t roundedCount = static_cast<size_t>(std::round(pair.second)); |
| 1691 |
if (roundedCount > 0) { |
| 1692 |
composition[pair.first] = roundedCount; |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
return composition; |
| 1697 |
} |
| 1698 |
|
| 1699 |
std::map<std::string, size_t> Molecule::formulaComposition() const |
| 1700 |
{ |
| 1701 |
const double tolerance = 1.0e-3; |
| 1702 |
|
| 1703 |
// A map of element symbols (with isotopes) to their quantity |
| 1704 |
// Using double to accumulate fractional contributions from unit cells |
| 1705 |
std::map<std::string, double> compositionDouble; |
| 1706 |
|
| 1707 |
for (Index i = 0; i < atomCount(); ++i) { |
| 1708 |
unsigned char atomicNum = m_atomicNumbers[i]; |
| 1709 |
std::string atomSymbol(Elements::symbol(atomicNum)); |
| 1710 |
|
| 1711 |
// Handle isotopes |
| 1712 |
unsigned short iso = isotope(i); |
| 1713 |
if (iso > 0) { |
| 1714 |
if (atomicNum == 1 && iso == 1) |
| 1715 |
atomSymbol = "H"; |
| 1716 |
else if (atomicNum == 1 && iso == 2) |
| 1717 |
atomSymbol = "D"; |
| 1718 |
else if (atomicNum == 1 && iso == 3) |
| 1719 |
atomSymbol = "T"; |
| 1720 |
else |
| 1721 |
// e.g., 13C |
| 1722 |
atomSymbol = std::to_string(iso) + atomSymbol; |
| 1723 |
} |
| 1724 |
|
| 1725 |
// Calculate fractional contribution for unit cells |
| 1726 |
double weight = 1.0; |
| 1727 |
if (m_unitCell != nullptr) { |
| 1728 |
Vector3 fracCoords = m_unitCell->toFractional(m_positions3d[i]); |
| 1729 |
|
| 1730 |
// Count how many coordinates are at boundaries (0 or 1) |
| 1731 |
int boundaryCount = 0; |
| 1732 |
for (int j = 0; j < 3; ++j) { |
| 1733 |
double coord = fracCoords[j]; |
| 1734 |
if (std::fabs(coord) < tolerance || |
| 1735 |
std::fabs(coord - 1.0) < tolerance) { |
| 1736 |
++boundaryCount; |
| 1737 |
} |
| 1738 |
} |
| 1739 |
|
| 1740 |
// Corner atoms (3 boundaries): 1/8 |
| 1741 |
// Edge atoms (2 boundaries): 1/4 |
| 1742 |
// Face atoms (1 boundary): 1/2 |
| 1743 |
if (boundaryCount == 3) { |
| 1744 |
weight = 1.0 / 8.0; |
| 1745 |
} else if (boundaryCount == 2) { |
| 1746 |
weight = 1.0 / 4.0; |
| 1747 |
} else if (boundaryCount == 1) { |
| 1748 |
weight = 1.0 / 2.0; |
| 1749 |
} |
| 1750 |
} |
| 1751 |
|
| 1752 |
compositionDouble[atomSymbol] += weight; |
| 1753 |
} |
| 1754 |
|
| 1755 |
// Convert to size_t by rounding to nearest integer |
| 1756 |
std::map<std::string, size_t> composition; |
| 1757 |
for (const auto& pair : compositionDouble) { |
| 1758 |
size_t roundedCount = static_cast<size_t>(std::round(pair.second)); |
| 1759 |
if (roundedCount > 0) { |
| 1760 |
composition[pair.first] = roundedCount; |
| 1761 |
} |
| 1762 |
} |
| 1763 |
|
| 1764 |
return composition; |
| 1765 |
} |
| 1766 |
|
| 1767 |
bool Molecule::removeBonds(Index atom) |
| 1768 |
{ |
| 1769 |
if (atom >= atomCount()) |
| 1770 |
return false; |
| 1771 |
|
| 1772 |
while (true) { |
| 1773 |
const std::vector<size_t>& bondList = m_graph.edges(atom); |
| 1774 |
if (!bondList.size()) |
| 1775 |
break; |
| 1776 |
size_t bond = bondList[0]; |
| 1777 |
removeBond(bond); |
| 1778 |
} |
| 1779 |
return true; |
| 1780 |
} |
| 1781 |
|
| 1782 |
Array<std::pair<Index, Index>> Molecule::getAtomBonds(Index index) const |
| 1783 |
{ |
| 1784 |
Array<std::pair<Index, Index>> result; |
| 1785 |
const std::vector<Index>& edgeIndices = m_graph.edges(index); |
| 1786 |
for (unsigned long edgeIndice : edgeIndices) { |
| 1787 |
result.push_back(m_graph.endpoints(edgeIndice)); |
| 1788 |
} |
| 1789 |
return result; |
| 1790 |
} |
| 1791 |
|
| 1792 |
Array<unsigned char> Molecule::getAtomOrders(Index index) const |
| 1793 |
{ |
| 1794 |
Array<unsigned char> result; |
| 1795 |
const std::vector<Index>& edgeIndices = m_graph.edges(index); |
| 1796 |
for (unsigned long edgeIndice : edgeIndices) { |
| 1797 |
result.push_back(m_bondOrders[edgeIndice]); |
| 1798 |
} |
| 1799 |
return result; |
| 1800 |
} |
| 1801 |
|
| 1802 |
void Molecule::addBonds(const Array<std::pair<Index, Index>>& bonds, |
| 1803 |
const Array<unsigned char>& orders) |
| 1804 |
{ |
| 1805 |
Index i = 0; |
| 1806 |
for (auto p : bonds) { |
| 1807 |
addBond(p.first, p.second, orders[i]); |
| 1808 |
++i; |
| 1809 |
} |
| 1810 |
} |
| 1811 |
|
| 1812 |
std::list<Index> Molecule::getAtomsAtLayer(size_t layer) |
| 1813 |
{ |
| 1814 |
std::list<Index> result; |
| 1815 |
// get the index in decreasing order so deleting won't corrupt data |
| 1816 |
for (Index i = atomCount(); i > 0; --i) { |
| 1817 |
if (m_layers.getLayerID(i - 1) == layer) { |
| 1818 |
result.push_back(i - 1); |
| 1819 |
} |
| 1820 |
} |
| 1821 |
return result; |
| 1822 |
} |
| 1823 |
|
| 1824 |
void Molecule::boundingBox(Vector3& boxMin, Vector3& boxMax, |
| 1825 |
const double radius) const |
| 1826 |
{ |
| 1827 |
boxMin.setConstant(std::numeric_limits<double>::max()); |
| 1828 |
boxMax.setConstant(-std::numeric_limits<double>::max()); |
| 1829 |
|
| 1830 |
const bool noSelection = isSelectionEmpty(); |
| 1831 |
|
| 1832 |
for (uint32_t i = 0; i < atomCount(); i++) { |
| 1833 |
if (noSelection || m_selectedAtoms[i]) { |
| 1834 |
|
| 1835 |
const Vector3 boxMinBuffer = atom(i).position3d().array() - radius; |
| 1836 |
const Vector3 boxMaxBuffer = atom(i).position3d().array() + radius; |
| 1837 |
|
| 1838 |
boxMin.x() = std::min(boxMinBuffer.x(), boxMin.x()); |
| 1839 |
boxMin.y() = std::min(boxMinBuffer.y(), boxMin.y()); |
| 1840 |
boxMin.z() = std::min(boxMinBuffer.z(), boxMin.z()); |
| 1841 |
|
| 1842 |
boxMax.x() = std::max(boxMaxBuffer.x(), boxMax.x()); |
| 1843 |
boxMax.y() = std::max(boxMaxBuffer.y(), boxMax.y()); |
| 1844 |
boxMax.z() = std::max(boxMaxBuffer.z(), boxMax.z()); |
| 1845 |
} |
| 1846 |
} |
| 1847 |
} |
| 1848 |
|
| 1849 |
} // namespace Avogadro::Core |