Debian Codegraph @

avogadro/core/molecule.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_CORE_MOLECULE_H
7 #define AVOGADRO_CORE_MOLECULE_H
8
9 #include "avogadrocoreexport.h"
10
11 #include "avogadrocore.h"
12
13 #include "array.h"
14 #include "bond.h"
15 #include "constraint.h"
16 #include "elements.h"
17 #include "graph.h"
18 #include "layer.h"
19 #include "variantmap.h"
20 #include "vector.h"
21
22 #include <bitset>
23 #include <cassert>
24 #include <cstddef>
25 #include <list>
26 #include <map>
27 #include <string>
28
29 namespace Avogadro::Core {
30 class BasisSet;
31 class Cube;
32 class Mesh;
33 class Residue;
34 class UnitCell;
35
36 /** Concrete atom/bond proxy classes for Core::Molecule. @{ */
37 class Atom;
38 class Bond;
39 /** @} */
40
41 /**
42 * @class Molecule molecule.h <avogadro/core/molecule.h>
43 * @brief The Molecule class represents a chemical molecule.
44 */
45 class AVOGADROCORE_EXPORT Molecule
46 {
47 public:
48 /** Typedef for Atom class. */
49 using AtomType = Atom;
50
51 /** Typedef for Bond class. */
52 using BondType = Bond;
53 /** Type for custom element map. */
54 using CustomElementMap = std::map<unsigned char, std::string>;
55
56 /** Type for element masks (e.g., does this molecule contain certain elements)
57 */
58 using ElementMask = std::bitset<element_count>;
59
60 /** Creates a new, empty molecule. */
61 Molecule();
62
63 /** Copy constructor */
64 Molecule(const Molecule& other);
65
66 /** Move constructor */
67 Molecule(Molecule&& other) noexcept;
68
69 /** Assignment operator */
70 Molecule& operator=(const Molecule& other);
71
72 /** Move assignment operator */
73 Molecule& operator=(Molecule&& other) noexcept;
74
75 /** Destroys the molecule object. */
76 virtual ~Molecule();
77
78 /**
79 * Adds the properties from the supplied
80 * molecule to this molecule. Does not otherwise
81 * modify atoms / bonds / residues, etc.
82 */
83 void readProperties(const Molecule& other);
84
85 /** Sets the data value with @p name to @p value. */
86 void setData(const std::string& name, const Variant& value);
87
88 /** @return the data value for @p name. */
89 Variant data(const std::string& name) const;
90
91 /**
92 * @return true if the molecule has data with the given key, false otherwise.
93 */
94 bool hasData(const std::string& name) const;
95
96 /** Set the molecule's variant data to the entries in map. */
97 void setDataMap(const VariantMap& map);
98
99 /** @return the molecule's variant data. */
100 const VariantMap& dataMap() const;
101
102 /** \overload */
103 VariantMap& dataMap();
104
105 /** @return a specific spectra entry */
106 MatrixX spectra(const std::string& name) const;
107
108 /** Sets the spectra value with @p name to @p value. */
109 void setSpectra(const std::string& name, const MatrixX& value);
110
111 /** @return the list of available spectra */
112 std::set<std::string> spectraTypes() const;
113
114 /** Sets atomic partial charges with @p type to @p value. */
115 void setPartialCharges(const std::string& type, const MatrixX& value);
116
117 /** @return the atomic partial charges of type @p type */
118 MatrixX partialCharges(const std::string& type) const;
119
120 /** @return the types of partial charges available stored with this molecule.
121 */
122 std::set<std::string> partialChargeTypes() const;
123
124 /** @return a vector of hybridizations for the atoms in the molecule. */
125 Array<AtomHybridization>& hybridizations();
126
127 /** \overload */
128 const Array<AtomHybridization>& hybridizations() const;
129
130 /**
131 * Get the hybridization for the requested atom.
132 * @param atomId The index of the atom.
133 * @return The hybridization of the atom indexed at @a atomId, or
134 * 0 if @a atomId is invalid.
135 */
136 AtomHybridization hybridization(Index atomId) const;
137
138 /**
139 * Replace the current array of hybridizations.
140 * @param hybs The new hybridization array. Must be of length atomCount().
141 * @return True on success, false otherwise.
142 */
143 bool setHybridizations(const Core::Array<AtomHybridization>& hybs);
144
145 /**
146 * Set the hybridization of a single atom.
147 * @param atomId The index of the atom to modify.
148 * @param hybridization The new hybridization.
149 * @return True on success, false otherwise.
150 */
151 bool setHybridization(Index atomId, AtomHybridization hybridization);
152
153 /** @return a vector of formal charges for the atoms in the molecule. */
154 Array<signed char>& formalCharges();
155
156 /** \overload */
157 const Array<signed char>& formalCharges() const;
158
159 /**
160 * Get the formal charge for the requested atom.
161 * @param atomId The index of the atom.
162 * @return The formal charge of the atom indexed at @a atomId, or
163 * 0 if @a atomId is invalid.
164 */
165 signed char formalCharge(Index atomId) const;
166
167 /**
168 * Replace the current array of formal charges.
169 * @param charges The new formal charge array. Must be of length atomCount().
170 * @return True on success, false otherwise.
171 */
172 bool setFormalCharges(const Core::Array<signed char>& charges);
173
174 /**
175 * Set the formal charge of a single atom.
176 * @param atomId The index of the atom to modify.
177 * @param charge The new formal charge.
178 * @return True on success, false otherwise.
179 */
180 bool setFormalCharge(Index atomId, signed char charge);
181
182 /**
183 * Get the total charge on the molecule.
184 * The method will first check to see if a total charge has been set. If not,
185 * it will calculate the total charge from the formal charges (if set).
186 * If neither has been set, it will assume the total charge is zero.
187 * @return The total charge of the molecule.
188 */
189 signed char totalCharge() const;
190
191 /**
192 * Get the total spin multiplicity of the molecule.
193 * The method will first check to see if a total spin has been set. If not,
194 * it will either suggest a singlet if an even number of electrons are
195 * present, or a doublet if an odd number of electrons are present.
196 * @return The total spin multiplicity of the molecule.
197 */
198 char totalSpinMultiplicity() const;
199
200 /** @return an array of the isotopes for the atoms in the molecule. */
201 Array<unsigned short>& isotopes();
202
203 /** \overload */
204 const Array<unsigned short>& isotopes() const;
205
206 /**
207 * Get the isotope for the requested atom.
208 * @param atomId The index of the atom.
209 * @return The isotope of the atom indexed at @a atomId, or
210 * 0 if @a atomId is invalid.
211 */
212 unsigned short isotope(Index atomId) const;
213
214 /**
215 * Replace the current array of isotopes.
216 * @param charges The new isotope array. Must be of length atomCount().
217 * @return True on success, false otherwise.
218 */
219 bool setIsotopes(const Core::Array<unsigned short>& isotopes);
220
221 /**
222 * Set the isotope of a single atom.
223 * @param atomId The index of the atom to modify.
224 * @param isotope The new isotope (or zero for most common)
225 * @return True on success, false otherwise.
226 */
227 bool setIsotope(Index atomId, unsigned short isotope);
228
229 /** \returns a vector of colors for the atoms in the moleucle. */
230 Array<Vector3ub>& colors();
231
232 /** \overload */
233 const Array<Vector3ub>& colors() const;
234
235 /**
236 * Get the color for the requested atom.
237 * @param atomId The index of the atom.
238 * @return The color of the atom indexed at @a atomId, or
239 * (0,0,0) if @a atomId is invalid. If no color is set for the
240 * given atomId, the default color for the atomic number of
241 * the atomId is returned.
242 */
243 Vector3ub color(Index atomId) const;
244
245 /**
246 * Replace the current array of colors.
247 * @param colors The new color array. Must be of length atomCount().
248 * @return True on success, false otherwise.
249 */
250 bool setColors(const Core::Array<Vector3ub>& colors);
251
252 /**
253 * Set the color of a single atom.
254 * @param atomId The index of the atom to modify.
255 * @param color The new color.
256 * @return True on success, false otherwise.
257 */
258 bool setColor(Index atomId, Vector3ub color);
259
260 bool setLayer(Index atomId, size_t layer);
261 size_t layer(Index atomId) const;
262
263 /** @return a vector of 2d atom positions for the atoms in the molecule. */
264 const Array<Vector2>& atomPositions2d() const;
265
266 /** \overload */
267 Array<Vector2>& atomPositions2d();
268
269 /**
270 * Get the 2D position of a single atom.
271 * @param atomId The index of the atom.
272 * @return The position of the atom, or Vector3::Zero() if no position
273 * information has been set.
274 */
275 Vector2 atomPosition2d(Index atomId) const;
276
277 /**
278 * Replace the current array of 2D atomic coordinates.
279 * @param pos The new coordinate array. Must be of length atomCount().
280 * @return True on success, false otherwise.
281 */
282 bool setAtomPositions2d(const Core::Array<Vector2>& pos);
283
284 /**
285 * Set the 2D position of a single atom.
286 * @param atomId The index of the atom to modify.
287 * @param pos The new position of the atom.
288 * @return True on success, false otherwise.
289 */
290 bool setAtomPosition2d(Index atomId, const Vector2& pos);
291
292 /** @return a vector of 3d atom positions for the atoms in the molecule. */
293 const Array<Vector3>& atomPositions3d() const;
294
295 /** \overload */
296 Array<Vector3>& atomPositions3d();
297
298 /**
299 * Get the 3D position of a single atom.
300 * @param atomId The index of the atom.
301 * @return The position of the atom, or Vector3::Zero() if no position
302 * information has been set.
303 */
304 Vector3 atomPosition3d(Index atomId) const;
305
306 /**
307 * Replace the current array of 3D atomic coordinates.
308 * @param pos The new coordinate array. Must be of length atomCount().
309 * @return True on success, false otherwise.
310 */
311 bool setAtomPositions3d(const Core::Array<Vector3>& pos);
312
313 /**
314 * Set the 3D position of a single atom.
315 * @param atomId The index of the atom to modify.
316 * @param pos The new position of the atom.
317 * @return True on success, false otherwise.
318 */
319 bool setAtomPosition3d(Index atomId, const Vector3& pos);
320
321 /**
322 * @return Any custom label for the requested atom.
323 * @param atomId The index of the atom.
324 */
325 std::string atomLabel(Index atomId) const;
326 /**
327 * Set the custom label of a single atom.
328 * @param atomId The index of the atom to modify.
329 * @param label The new label of the atom.
330 * @return True on success, false otherwise.
331 */
332 bool setAtomLabel(Index atomId, const std::string& label);
333
334 const Core::Array<std::string> atomLabels() const { return m_atomLabels; }
335
336 /**
337 * Set all the atom labels in the molecule.
338 * @param label The new label array. Must be of length atomCount().
339 * @return True on success, false otherwise.
340 */
341 bool setAtomLabels(const Core::Array<std::string>& label);
342
343 /**
344 * Set whether the specified atom is selected or not.
345 */
346 void setAtomSelected(Index atomId, bool selected);
347
348 /**
349 * Query whether the supplied atom index has been selected.
350 */
351 bool atomSelected(Index atomId) const;
352
353 /** @return whether the selection is empty or not */
354 bool isSelectionEmpty() const;
355
356 /** A map of custom element atomic numbers to string identifiers. These ids
357 * can be used to override the generic custom element names returned by the
358 * Elements class, and should be somewhat meaningful to the user.
359 *
360 * @note Custom element atomic numbers lie between CustomElementMin and
361 * CustomElementMax.
362 * @sa Avogadro::QtGui::CustomElementDialog
363 * @sa hasCustomElements
364 * @{ */
365 const CustomElementMap& customElementMap() const;
366 void setCustomElementMap(const CustomElementMap& map);
367 /** @} */
368
369 /** @return the elements currently in this molecule */
370 const ElementMask elements() const;
371
372 /** Adds an atom to the molecule. */
373 virtual AtomType addAtom(unsigned char atomicNumber);
374 AtomType addAtom(unsigned char atomicNumber, Vector3 position3d);
375
376 /**
377 * @brief Remove the specified atom from the molecule.
378 * @param index The index of the atom to be removed.
379 * @return True on success, false if the atom was not found.
380 */
381 virtual bool removeAtom(Index index);
382
383 /**
384 * @brief Remove the specified atom from the molecule.
385 * @param atom The atom to be removed.
386 * @return True on success, false if the atom was not found.
387 * @overload
388 */
389 virtual bool removeAtom(const AtomType& atom);
390
391 /**
392 * Remove all atoms from the molecule.
393 */
394 virtual void clearAtoms();
395
396 /**
397 * @return the atom at @p index in the molecule.
398 */
399 AtomType atom(Index index) const;
400
401 /**
402 * Create a new bond in the molecule.
403 * @param atom1 The first atom in the bond.
404 * @param atom2 The second atom in the bond.
405 * @param order The bond order.
406 * @return The new bond object. Will be invalid if @a atom1 or @a atom2 does
407 * not exist.
408 * @{
409 */
410 virtual BondType addBond(Index atom1, Index atom2, unsigned char order = 1);
411 virtual BondType addBond(const AtomType& atom1, const AtomType& atom2,
412 unsigned char order = 1);
413 /** @} */
414
415 /**
416 * @brief Remove the specified bond.
417 * @param index The index of the bond to be removed.
418 * @return True on success, false if the bond was not found.
419 */
420 virtual bool removeBond(Index index);
421
422 /**
423 * @brief Remove the specified bond.
424 * @param bond The bond to be removed.
425 * @return True on success, false if the bond was not found.
426 * @overload
427 */
428 virtual bool removeBond(const BondType& bond);
429
430 /**
431 * @brief Remove the specified bond.
432 * @param atom1 One atom in the bond.
433 * @param atom2 The other atom in the bond.
434 * @return True on success, false if the bond was not found.
435 * @overload
436 * @{
437 */
438 virtual bool removeBond(Index atom1, Index atom2);
439 virtual bool removeBond(const AtomType& atom1, const AtomType& atom2);
440 /** @} */
441
442 /**
443 * Remove all bonds from the molecule.
444 */
445 virtual void clearBonds();
446
447 /** @return the bond at @p index in the molecule. */
448 BondType bond(Index index) const;
449
450 /** @return the bond between atoms @p a and @p b. */
451 BondType bond(const AtomType& a, const AtomType& b) const;
452
453 /** @return the bond between atomId1 and atomId2. */
454 BondType bond(Index atomId1, Index atomId2) const;
455
456 /**
457 * @return Any custom label for the requested bond.
458 * @param bondIndex The index of the bond.
459 */
460 std::string bondLabel(Index bondIndex) const;
461 /**
462 * Set the custom label of a single bond.
463 * @param bondIndex The index of the bond to modify.
464 * @param label The new label of the bond.
465 * @return True on success, false otherwise.
466 */
467 bool setBondLabel(Index bondIndex, const std::string& label);
468
469 const Core::Array<std::string> bondLabels() const { return m_bondLabels; }
470
471 /**
472 * Set all the bond labels in the molecule.
473 * @param label The new label array. Must be of length bondCount().
474 * @return True on success, false otherwise.
475 */
476 bool setBondLabels(const Core::Array<std::string>& label);
477
478 /**
479 * @brief Get all bonds to @p a.
480 * @return A vector of bonds to the supplied atom @p a.
481 * @{
482 */
483 Array<BondType> bonds(const AtomType& a);
484 Array<BondType> bonds(Index a);
485 Array<const BondType*> bonds(Index a) const;
486 /** @} */
487
488 /**
489 * @brief Add a mesh to the molecule.
490 * @return The mesh object added to the molecule.
491 */
492 Mesh* addMesh();
493
494 Mesh* mesh(Index index);
495 const Mesh* mesh(Index index) const;
496
497 Index meshCount() const { return static_cast<Index>(m_meshes.size()); }
498
499 void clearMeshes();
500
501 /**
502 * @brief Add a cube to the molecule.
503 * @return The cube object added to the molecule.
504 */
505 Cube* addCube();
506
507 Cube* cube(Index index);
508 const Cube* cube(Index index) const;
509
510 Index cubeCount() const { return static_cast<Index>(m_cubes.size()); }
511
512 void clearCubes();
513
514 /**
515 * @brief Set the active cube index for volume rendering.
516 * @param index The index of the cube to make active.
517 * @return True if the index is valid and was set, false otherwise.
518 */
519 bool setActiveCubeIndex(Index index);
520
521 /**
522 * @brief Get the index of the active cube.
523 * @return The active cube index, or 0 if no cubes exist.
524 */
525 Index activeCubeIndex() const { return m_activeCubeIndex; }
526
527 /**
528 * @brief Get the active cube for volume rendering.
529 * @return A pointer to the active cube, or nullptr if no cubes exist.
530 */
531 Cube* activeCube();
532 const Cube* activeCube() const;
533
534 /**
535 * @brief Get the cubes vector set (if present) for the molecule.
536 * @return The cube vector for the molecule
537 */
538 std::vector<Cube*> cubes() { return m_cubes; }
539 const std::vector<Cube*> cubes() const { return m_cubes; }
540
541 /**
542 * @return the chemical formula of the molecule.
543 * @param delimiter Delimiter to insert between tokens, defaults to none.
544 * @param showCountsOver Show atom counts above this (defaults to 1).
545 */
546 std::string formula(const std::string& delimiter = "",
547 int showCountsOver = 1) const;
548
549 /**
550 * @return The mass of the molecule obtained by summing constituent atomic
551 * masses.
552 */
553 double mass() const;
554
555 /**
556 * @return the monoisotopic mass of the molecule obtained by summing
557 * the isotopic masses of the atoms (including the most common isotope
558 * if not specified).
559 */
560 double monoisotopicMass() const;
561
562 /**
563 * @return The center of geometry of the molecule obtained by summing the
564 * coordinates of the atoms.
565 */
566 Vector3 centerOfGeometry() const;
567
568 /**
569 * @return The center of mass of the molecule obtained by summing the
570 * coordinates of the atoms weighted by mass.
571 */
572 Vector3 centerOfMass() const;
573
574 /**
575 * @return The minimum radius of a sphere centered on centerOfGeometry
576 * containing all the centers of the atoms.
577 */
578 double radius() const;
579
580 /**
581 * @return The (centroid, normal vector) pair of the best-fit plane of
582 * the atoms of the molecule.
583 */
584 std::pair<Vector3, Vector3> bestFitPlane() const;
585
586 /**
587 * @return The normal vector of the best-fit plane of some specific atoms.
588 */
589 static std::pair<Vector3, Vector3> bestFitPlane(const Array<Vector3>& pos);
590
591 /**
592 * Set the basis set for the molecule, note that the molecule takes ownership
593 * of the object.
594 */
595 void setBasisSet(BasisSet* basis) { m_basisSet = basis; }
596
597 /**
598 * @return the basis set (if present) for the molecule.
599 */
600 BasisSet* basisSet() { return m_basisSet; }
601 const BasisSet* basisSet() const { return m_basisSet; }
602
603 /**
604 * The unit cell for this molecule. May be nullptr for non-periodic
605 * structures.
606 * @{
607 */
608 void setUnitCell(UnitCell* uc);
609 UnitCell* unitCell() { return m_unitCell; }
610 const UnitCell* unitCell() const { return m_unitCell; }
611 /** @} */
612
613 /**
614 * The space group for this molecule. It is updated after every
615 * space group operation.
616 * @{
617 */
618 void setHallNumber(unsigned short hallNumber) { m_hallNumber = hallNumber; }
619 unsigned short hallNumber() const { return m_hallNumber; }
620 /** @} */
621
622 Array<double> vibrationFrequencies() const;
623 void setVibrationFrequencies(const Array<double>& freq);
624 Array<double> vibrationIRIntensities() const;
625 void setVibrationIRIntensities(const Array<double>& intensities);
626 Array<double> vibrationRamanIntensities() const;
627 void setVibrationRamanIntensities(const Array<double>& intensities);
628 Array<Vector3> vibrationLx(int mode) const;
629 void setVibrationLx(const Array<Array<Vector3>>& lx);
630
631 /**
632 * Perceives bonds in the molecule based on the 3D coordinates of the atoms.
633 * atoms are considered bonded if within the sum of radii
634 * plus a small @p tolerance.
635 * @param tolerance The calculation tolerance.
636 * @param minDistance = atoms closer than the square of this are ignored
637 */
638 void perceiveBondsSimple(const double tolerance = 0.45,
639 const double minDistance = 0.32);
640
641 /**
642 * Perceives bonds in the molecule based on preset residue data.
643 *
644 * Use this if you have residue data available (e.g., reading PDB or MMTF
645 * files) Otherwise consider @sa perceiveBondsSimple and @sa
646 * perceiveBondOrders
647 */
648 void perceiveBondsFromResidueData();
649
650 void perceiveBondOrders();
651
652 /**
653 * Perceives all-carbon-substituted onium ions of nitrogen, oxygen,
654 * phosphorus, sulfur, arsenic and selenium.
655 */
656 void perceiveSubstitutedCations();
657
658 size_t coordinate3dCount() const;
659 bool setCoordinate3d(int coord);
660 Array<Vector3> coordinate3d(size_t index) const;
661 bool setCoordinate3d(const Array<Vector3>& coords, size_t index);
662
663 /**
664 * Clear coordinate sets (except the default set)
665 */
666 void clearCoordinate3d();
667
668 /**
669 * Estimate velocities from the coordinate sets and timesteps.
670 */
671 void estimateVelocities();
672
673 /**
674 * Get the velocities for the specified index.
675 */
676 Array<Vector3> velocities(int index) const;
677
678 /**
679 * Set the velocities for the specified index.
680 */
681 bool setVelocities(const Array<Vector3>& velocities, int index);
682
683 /**
684 * Clear all velocity sets.
685 */
686 void clearVelocities();
687
688 /**
689 * Timestep property is used when molecular dynamics trajectories are read
690 */
691 bool setTimeStep(double timestep, int index);
692 double timeStep(int index, bool& status);
693
694 /** @return a vector of forces for the atoms in the molecule. */
695 const Array<Vector3>& forceVectors() const;
696
697 /** \overload */
698 Array<Vector3>& forceVectors();
699
700 /**
701 * Get the force of a single atom.
702 * @param atomId The index of the atom.
703 * @return The force vector of the atom, or Vector3::Zero() if no force
704 * information has been set.
705 */
706 Vector3 forceVector(Index atomId) const;
707
708 /**
709 * Replace the current array of force vectors.
710 * @param forces The new coordinate array. Must be of length atomCount().
711 * @return True on success, false otherwise.
712 */
713 bool setForceVectors(const Core::Array<Vector3>& forces);
714
715 /**
716 * Set the 3D position of a single atom.
717 * @param atomId The index of the atom to modify.
718 * @param force The new position of the atom.
719 * @return True on success, false otherwise.
720 */
721 bool setForceVector(Index atomId, const Vector3& force);
722
723 Residue& addResidue(std::string& name, Index& number, char& id);
724 void addResidue(Residue& residue);
725 Residue& residue(Index index);
726 const Residue& residue(Index index) const;
727
728 Array<Residue>& residues() { return m_residues; }
729 const Array<Residue>& residues() const { return m_residues; }
730
731 /** @return The number of residues in the molecule. */
732 Index residueCount() const;
733
734 /**
735 * @return Any custom label for the requested residue.
736 * @param residueIndex The index of the residue.
737 */
738 std::string residueLabel(Index residueIndex) const;
739 /**
740 * Set the custom label of a single residue.
741 * @param residueIndex The index of the residue to modify.
742 * @param label The new label of the residue.
743 * @return True on success, false otherwise.
744 */
745 bool setResidueLabel(Index residueIndex, const std::string& label);
746
747 const Core::Array<std::string> residueLabels() const
748 {
749 return m_residueLabels;
750 }
751
752 /**
753 * Set all the residue labels in the molecule.
754 * @param label The new label array. Must be of length residueCount().
755 * @return True on success, false otherwise.
756 */
757 bool setResidueLabels(const Core::Array<std::string>& label);
758
759 /** @return The number of atoms in the molecule. */
760 Index atomCount() const;
761
762 /**
763 * @brief Get the number of atoms in the molecule that match atomicNumber.
764 * @param atomicNumber The atomic number to match on.
765 * @return The number of atoms with the supplied atomic number.
766 */
767 Index atomCount(unsigned char atomicNumber) const;
768 /** @return the number of bonds in the molecule. */
769 inline Index bondCount() const;
770
771 // getters and setters
772 /**
773 * Get the set of bonded atoms corresponding to @a bondId.
774 * @param bondId The index of the requested bond.
775 * @return The bonded atom pair, represented as a pair of atom indices.
776 */
777 inline std::pair<Index, Index> bondPair(Index bondId) const;
778
779 /**
780 * Replace the current array of bonded atoms.
781 * @param pairs The array.
782 * @return True on success, false on failure.
783 * @note The bonded atoms are represented as a pair of bond indices.
784 * @note If needed, the elements in @a pairs will be modified to ensure that
785 * the first atom index is less than the second.
786 */
787 bool setBondPairs(const Array<std::pair<Index, Index>>& pairs);
788
789 /** @return a vector of pairs of atom indices of the bonds in the molecule. */
790 inline const Array<std::pair<Index, Index>>& bondPairs() const;
791
792 /** @return a vector of the bond orders for the bonds in the molecule. */
793 inline const Array<unsigned char>& bondOrders() const;
794
795 /** @return the graph for the molecule. */
796 inline const Graph& graph() const;
797
798 /** @return a vector of atomic numbers for the atoms in the molecule. */
799 inline const Array<unsigned char>& atomicNumbers() const;
800
801 /**
802 * Get the atomic number for the requested atom.
803 * @param atomId The index of the atom.
804 * @return The atomic number of the atom indexed at @a atomId, or
805 * Avogadro::InvalidElement if @a atomId is invalid.
806 */
807 unsigned char atomicNumber(Index atomId) const;
808
809 /**
810 * Replace the current array of bond orders.
811 * @param orders The new array.
812 * @return True on success, false on failure.
813 */
814 bool setBondOrders(const Array<unsigned char>& orders);
815
816 /**
817 * Set the order of a bond in the molecule.
818 * @param bondId The bond's index.
819 * @param order The new order of the bond.
820 * @return True on success, false on failure.
821 */
822 bool setBondOrder(Index bondId, unsigned char order);
823 /**
824 * @return True if custom elements exist in the molecule.
825 * @note Custom element atomic numbers lie between CustomElementMin and
826 * CustomElementMax.
827 */
828 bool hasCustomElements() const;
829
830 /**
831 * Set the bonded atoms for a bond.
832 * @param bondId The bond to modify.
833 * @param pair The new bond pair.
834 * @return True on success, false otherwise.
835 * @note If needed, @a pair will be modified to ensure that the first atom
836 * index is less than the second.
837 */
838 bool setBondPair(Index bondId, const std::pair<Index, Index>& pair);
839
840 /**
841 * Get the order of a bond.
842 * @param bondId The id of the bond.
843 * @return The bond order.
844 */
845 unsigned char bondOrder(Index bondId) const;
846 /**
847 * Replace the current array of atomic numbers.
848 * @param nums The new atomic number array. Must be of length atomCount().
849 * @return True on success, false otherwise.
850 */
851 bool setAtomicNumbers(const Core::Array<unsigned char>& nums);
852
853 /**
854 * Set the atomic number of a single atom.
855 * @param atomId The index of the atom to modify.
856 * @param atomicNumber The new atomic number.
857 * @return True on success, false otherwise.
858 */
859 bool setAtomicNumber(Index atomId, unsigned char atomicNumber);
860
861 /** @name Constraints
862 * Methods for distance, angle, torsion, etc. constraints
863 */
864 ///@{
865 void addConstraint(Real Value, Index a, Index b, Index c = MaxIndex,
866 Index d = MaxIndex);
867 void addConstraint(Constraint& c) { m_constraints.push_back(c); }
868
869 void removeConstraint(Index a, Index b, Index c = MaxIndex,
870 Index d = MaxIndex);
871 void clearConstraints() { m_constraints.clear(); }
872 void setConstraints(const std::vector<Core::Constraint>& constraints)
873 {
874 m_constraints = constraints;
875 }
876 std::vector<Core::Constraint>& constraints() { return m_constraints; };
877 const std::vector<Core::Constraint>& constraints() const
878 {
879 return m_constraints;
880 }
881
882 /**
883 * Freeze or unfreeze an atom for optimization
884 */
885 void setFrozenAtom(Index atomId, bool frozen);
886
887 /**
888 * @return whether the specified atom is frozen
889 */
890 bool frozenAtom(Index atomId) const;
891 /**
892 * @param atomId The index of the atom to check
893 * @param axis The axis to check (0 = x, 1 = y, 2 = z)
894 * @return whether the specified axis of the specified atom is frozen
895 */
896 bool frozenAtomAxis(Index atomId, int axis) const;
897
898 /**
899 * Freeze or unfreeze X, Y, or Z coordinate of an atom for optimization
900 * @param atomId The index of the atom to modify.
901 * @param axis The axis to freeze (0, 1, or 2 for X, Y, or Z)
902 * @param frozen True to freeze, false to unfreeze
903 */
904 void setFrozenAtomAxis(Index atomId, int axis, bool frozen);
905
906 /**
907 * @return the frozen status of atoms (i.e., 3*N array of 1.0 or 0.0)
908 * 0.0 means the atom is frozen, 1.0 means the atom is not frozen.
909 * (i.e., multiply this mask with gradients to freeze atoms)
910 */
911 Eigen::VectorXd frozenAtomMask() const { return m_frozenAtomMask; }
912 ///@} end of constraint methods
913
914 /**
915 * @return a map of components and count.
916 */
917 std::map<unsigned char, size_t> composition() const;
918
919 /**
920 * @return a map of element symbols (including isotopes like D, T, 13C) to
921 * their count. Handles unit cell fractional contributions for atoms at
922 * corners (1/8), edges (1/4), and faces (1/2).
923 */
924 std::map<std::string, size_t> formulaComposition() const;
925
926 /**
927 * @return the atom pairs for all bonds to the atom indexed at @a index.
928 */
929 Array<std::pair<Index, Index>> getAtomBonds(Index index) const;
930 /**
931 * @return the bond orders for all bonds to the atom indexed at @a index.
932 */
933 Array<unsigned char> getAtomOrders(Index index) const;
934
935 inline static std::pair<Index, Index> makeBondPair(const Index& a,
936 const Index& b);
937 /** Remove bonds to @a atom */
938 bool removeBonds(Index atom);
939
940 void addBonds(const Array<std::pair<Index, Index>>& bonds,
941 const Array<unsigned char>& orders);
942
943 // chenge the bond index position
944 void swapBond(Index a, Index b);
945 // channge the Atom index position
946 void swapAtom(Index a, Index b);
947
948 std::list<Index> getAtomsAtLayer(size_t layer);
949
950 Layer& layer();
951 const Layer& layer() const;
952
953 /**
954 * Calculte and return bounding box of the whole molecule or selected atoms
955 * only.
956 * @param boxMin [out] the minimum corner (first end of the box diagonal)
957 * @param boxMax [out] the maximum corner (second end of the box diagonal)
958 * @param radius [in] radius of a single sphere
959 */
960 void boundingBox(Vector3& boxMin, Vector3& boxMax,
961 const double radius = 1.0) const;
962
963 protected:
964 VariantMap m_data;
965 std::map<std::string, MatrixX>
966 m_partialCharges; //!< Sets of atomic partial charges
967
968 std::map<std::string, MatrixX> m_spectra; //!< Sets of spectra
969 CustomElementMap m_customElementMap;
970 ElementMask m_elements; //!< Which elements this molecule contains (e.g., for
971 //!< force fields)
972 Array<Vector2> m_positions2d;
973 Array<Vector3> m_positions3d;
974 Array<std::string> m_atomLabels;
975 Array<std::string> m_bondLabels;
976 Array<std::string> m_residueLabels;
977 Array<Array<Vector3>> m_coordinates3d; //!< Store conformers/trajectories.
978 Array<Array<Vector3>> m_velocities; //!< Store velocities.
979 Array<double> m_timesteps;
980 Array<AtomHybridization> m_hybridizations;
981 Array<signed char> m_formalCharges;
982 Array<unsigned short> m_isotopes; //!< Store isotopes of the atoms
983 Array<Vector3> m_forceVectors;
984 Array<Vector3ub> m_colors;
985 // Vibration data if available.
986 Array<double> m_vibrationFrequencies;
987 Array<double> m_vibrationIRIntensities;
988 Array<double> m_vibrationRamanIntensities;
989 Array<Array<Vector3>> m_vibrationLx;
990
991 // Array declaring whether atoms are selected or not.
992 std::vector<bool> m_selectedAtoms;
993
994 std::vector<Mesh*> m_meshes;
995 std::vector<Cube*> m_cubes;
996 Index m_activeCubeIndex = 0;
997
998 BasisSet* m_basisSet;
999 UnitCell* m_unitCell;
1000 Array<Residue> m_residues;
1001
1002 // This will be stored from the last space group operation
1003 unsigned short m_hallNumber = 0;
1004
1005 std::vector<Core::Constraint> m_constraints;
1006 Eigen::VectorXd m_frozenAtomMask;
1007
1008 private:
1009 mutable Graph m_graph; // A transformation of the molecule to a graph.
1010 // edge information
1011 Array<unsigned char> m_bondOrders;
1012 // vertex information
1013 Array<unsigned char> m_atomicNumbers;
1014 Layer& m_layers;
1015 };
1016
1017 class AVOGADROCORE_EXPORT Atom : public AtomTemplate<Molecule>
1018 {
1019 public:
1020 Atom() = default;
1021 Atom(Molecule* m, Index i) : AtomTemplate<Molecule>(m, i) {}
1022 };
1023
1024 class AVOGADROCORE_EXPORT Bond : public BondTemplate<Molecule>
1025 {
1026 public:
1027 Bond() = default;
1028 Bond(Molecule* m, Index i) : BondTemplate<Molecule>(m, i) {}
1029 };
1030
1031 inline AtomHybridization Molecule::hybridization(Index atomId) const
1032 {
1033 AtomHybridization hyb = HybridizationUnknown;
1034 if (atomId < m_hybridizations.size())
1035 return m_hybridizations[atomId];
1036
1037 return hyb;
1038 }
1039
1040 inline bool Molecule::setHybridizations(
1041 const Core::Array<AtomHybridization>& hybs)
1042 {
1043 if (hybs.size() == atomCount()) {
1044 m_hybridizations = hybs;
1045 return true;
1046 }
1047 return false;
1048 }
1049
1050 inline bool Molecule::setHybridization(Index atomId, AtomHybridization hyb)
1051 {
1052 if (atomId < atomCount()) {
1053 if (atomId >= m_hybridizations.size())
1054 m_hybridizations.resize(atomCount(), HybridizationUnknown);
1055 m_hybridizations[atomId] = hyb;
1056 return true;
1057 }
1058 return false;
1059 }
1060
1061 inline signed char Molecule::formalCharge(Index atomId) const
1062 {
1063 return atomId < m_formalCharges.size() ? m_formalCharges[atomId] : 0;
1064 }
1065
1066 inline bool Molecule::setFormalCharges(const Core::Array<signed char>& charges)
1067 {
1068 if (charges.size() == atomCount()) {
1069 m_formalCharges = charges;
1070 return true;
1071 }
1072 return false;
1073 }
1074
1075 inline bool Molecule::setFormalCharge(Index atomId, signed char charge)
1076 {
1077 if (atomId < atomCount()) {
1078 if (atomId >= m_formalCharges.size())
1079 m_formalCharges.resize(atomCount(), 0);
1080 m_formalCharges[atomId] = charge;
1081 return true;
1082 }
1083 return false;
1084 }
1085
1086 inline unsigned short Molecule::isotope(Index atomId) const
1087 {
1088 return atomId < m_isotopes.size() ? m_isotopes[atomId] : 0;
1089 }
1090
1091 inline bool Molecule::setIsotopes(const Core::Array<unsigned short>& isotopes)
1092 {
1093 if (isotopes.size() == atomCount()) {
1094 m_isotopes = isotopes;
1095 return true;
1096 }
1097 return false;
1098 }
1099
1100 inline bool Molecule::setIsotope(Index atomId, unsigned short isotope)
1101 {
1102 if (atomId < atomCount()) {
1103 if (atomId >= m_isotopes.size())
1104 m_isotopes.resize(atomCount(), 0);
1105 m_isotopes[atomId] = isotope;
1106 return true;
1107 }
1108 return false;
1109 }
1110
1111 inline const Molecule::ElementMask Molecule::elements() const
1112 {
1113 return m_elements;
1114 }
1115
1116 inline Vector3ub Molecule::color(Index atomId) const
1117 {
1118 if (atomId >= atomCount())
1119 return Vector3ub(0, 0, 0);
1120
1121 if (atomId < m_colors.size())
1122 return m_colors[atomId];
1123
1124 return Vector3ub(Elements::color(atomicNumber(atomId)));
1125 }
1126
1127 inline bool Molecule::setColors(const Core::Array<Vector3ub>& colors)
1128 {
1129 if (colors.size() == atomCount()) {
1130 m_colors = colors;
1131 return true;
1132 }
1133 return false;
1134 }
1135
1136 inline bool Molecule::setColor(Index atomId, Vector3ub color)
1137 {
1138 if (atomId < atomCount()) {
1139 if (atomId >= m_colors.size()) {
1140 for (Index i = m_colors.size(); i < atomCount(); ++i) {
1141 m_colors.push_back(Vector3ub(Elements::color(atomicNumber(i))));
1142 }
1143 }
1144 m_colors[atomId] = color;
1145 return true;
1146 }
1147 return false;
1148 }
1149
1150 inline size_t Molecule::layer(Index atomId) const
1151 {
1152 return m_layers.getLayerID(atomId);
1153 }
1154
1155 inline bool Molecule::setLayer(Index atomId, size_t layer)
1156 {
1157 if (atomId < atomCount()) {
1158 m_layers.addAtom(layer, atomId);
1159 return true;
1160 }
1161 return false;
1162 }
1163
1164 inline Vector2 Molecule::atomPosition2d(Index atomId) const
1165 {
1166 return atomId < m_positions2d.size() ? m_positions2d[atomId] : Vector2();
1167 }
1168
1169 inline bool Molecule::setAtomPositions2d(const Core::Array<Vector2>& pos)
1170 {
1171 if (pos.size() == atomCount() || pos.size() == 0) {
1172 m_positions2d = pos;
1173 return true;
1174 }
1175 return false;
1176 }
1177
1178 inline bool Molecule::setAtomPosition2d(Index atomId, const Vector2& pos)
1179 {
1180 if (atomId < atomCount()) {
1181 if (atomId >= m_positions2d.size())
1182 m_positions2d.resize(atomCount(), Vector2::Zero());
1183 m_positions2d[atomId] = pos;
1184 return true;
1185 }
1186 return false;
1187 }
1188
1189 inline Vector3 Molecule::atomPosition3d(Index atomId) const
1190 {
1191 return atomId < m_positions3d.size() ? m_positions3d[atomId] : Vector3();
1192 }
1193
1194 inline bool Molecule::setAtomPositions3d(const Core::Array<Vector3>& pos)
1195 {
1196 if (pos.size() == atomCount() || pos.size() == 0) {
1197 m_positions3d = pos;
1198 return true;
1199 }
1200 return false;
1201 }
1202
1203 inline bool Molecule::setAtomPosition3d(Index atomId, const Vector3& pos)
1204 {
1205 if (atomId < atomCount()) {
1206 if (atomId >= m_positions3d.size())
1207 m_positions3d.resize(atomCount(), Vector3::Zero());
1208 m_positions3d[atomId] = pos;
1209 return true;
1210 }
1211 return false;
1212 }
1213
1214 inline std::string Molecule::atomLabel(Index atomId) const
1215 {
1216 return atomId < m_atomLabels.size() ? m_atomLabels[atomId] : "";
1217 }
1218
1219 inline bool Molecule::setAtomLabels(const Core::Array<std::string>& labels)
1220 {
1221 if (labels.size() == atomCount() || labels.size() == 0) {
1222 m_atomLabels = labels;
1223 return true;
1224 }
1225 return false;
1226 }
1227
1228 inline bool Molecule::setAtomLabel(Index atomId, const std::string& label)
1229 {
1230 if (atomId < atomCount()) {
1231 if (atomId >= m_atomLabels.size())
1232 m_atomLabels.resize(atomCount(), "");
1233 m_atomLabels[atomId] = label;
1234 return true;
1235 }
1236 return false;
1237 }
1238
1239 inline void Molecule::setAtomSelected(Index atomId, bool selected)
1240 {
1241 if (atomId < atomCount()) {
1242 if (atomId >= m_selectedAtoms.size())
1243 m_selectedAtoms.resize(atomCount(), false);
1244 m_selectedAtoms[atomId] = selected;
1245 }
1246 }
1247
1248 inline bool Molecule::atomSelected(Index atomId) const
1249 {
1250 return atomId < m_selectedAtoms.size() ? m_selectedAtoms[atomId] : false;
1251 }
1252
1253 inline bool Molecule::isSelectionEmpty() const
1254 {
1255 for (Index i = 0; i < m_selectedAtoms.size(); ++i) {
1256 if (m_selectedAtoms[i])
1257 return false;
1258 }
1259 return true;
1260 }
1261
1262 inline Vector3 Molecule::forceVector(Index atomId) const
1263 {
1264 return atomId < m_forceVectors.size() ? m_forceVectors[atomId] : Vector3();
1265 }
1266
1267 inline bool Molecule::setForceVectors(const Core::Array<Vector3>& forces)
1268 {
1269 if (forces.size() == atomCount() || forces.size() == 0) {
1270 m_forceVectors = forces;
1271 return true;
1272 }
1273 return false;
1274 }
1275
1276 inline bool Molecule::setForceVector(Index atomId, const Vector3& force)
1277 {
1278 if (atomId < atomCount()) {
1279 if (atomId >= m_forceVectors.size())
1280 m_forceVectors.resize(atomCount(), Vector3::Zero());
1281 m_forceVectors[atomId] = force;
1282 return true;
1283 }
1284 return false;
1285 }
1286
1287 // Make an std::pair where the lower index is always first in the pair. This
1288 // offers us the guarantee that any given pair of atoms will always result in
1289 // a pair that is the same no matter what the order of the atoms given.
1290 std::pair<Index, Index> Molecule::makeBondPair(const Index& a, const Index& b)
1291 {
1292 return a < b ? std::make_pair(a, b) : std::make_pair(b, a);
1293 }
1294
1295 inline Index Molecule::bondCount() const
1296 {
1297 assert(m_graph.edgeCount() == m_bondOrders.size());
1298 return m_graph.edgeCount();
1299 }
1300
1301 inline const Array<std::pair<Index, Index>>& Molecule::bondPairs() const
1302 {
1303 return m_graph.edgePairs();
1304 }
1305
1306 inline const Array<unsigned char>& Molecule::bondOrders() const
1307 {
1308 return m_bondOrders;
1309 }
1310
1311 inline std::string Molecule::bondLabel(Index bondId) const
1312 {
1313 return bondId < m_bondLabels.size() ? m_bondLabels[bondId] : "";
1314 }
1315
1316 inline bool Molecule::setBondLabels(const Core::Array<std::string>& labels)
1317 {
1318 if (labels.size() == bondCount() || labels.size() == 0) {
1319 m_bondLabels = labels;
1320 return true;
1321 }
1322 return false;
1323 }
1324
1325 inline bool Molecule::setBondLabel(Index bondId, const std::string& label)
1326 {
1327 if (bondId < bondCount()) {
1328 if (bondId >= m_bondLabels.size())
1329 m_bondLabels.resize(bondCount(), "");
1330 m_bondLabels[bondId] = label;
1331 return true;
1332 }
1333 return false;
1334 }
1335
1336 inline const Graph& Molecule::graph() const
1337 {
1338 return m_graph;
1339 }
1340
1341 inline const Array<unsigned char>& Molecule::atomicNumbers() const
1342 {
1343 return m_atomicNumbers;
1344 }
1345
1346 inline std::pair<Index, Index> Molecule::bondPair(Index bondId) const
1347 {
1348 return bondId < bondCount() ? m_graph.endpoints(bondId)
1349 : std::make_pair(MaxIndex, MaxIndex);
1350 }
1351
1352 inline Index Molecule::atomCount() const
1353 {
1354 return static_cast<Index>(m_atomicNumbers.size());
1355 }
1356
1357 inline unsigned char Molecule::atomicNumber(Index atomId) const
1358 {
1359 return atomId < m_atomicNumbers.size() ? m_atomicNumbers[atomId]
1360 : InvalidElement;
1361 }
1362
1363 } // namespace Avogadro::Core
1364
1365 #endif // AVOGADRO_CORE_MOLECULE_H