| 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_CONSTRAINT_H |
| 7 |
#define AVOGADRO_CORE_CONSTRAINT_H |
| 8 |
|
| 9 |
#include "avogadrocoreexport.h" |
| 10 |
|
| 11 |
#include <avogadro/core/avogadrocore.h> |
| 12 |
#include <tuple> |
| 13 |
|
| 14 |
namespace Avogadro { |
| 15 |
namespace Core { |
| 16 |
|
| 17 |
/** |
| 18 |
* @class Constraint constraint.h <avogadro/core/constraint.h> |
| 19 |
* @brief Constraints for optimization / dynamics |
| 20 |
* @author Geoffrey R. Hutchison |
| 21 |
* |
| 22 |
* This class represents a distance, angle, or torsional constraint / restraint |
| 23 |
* during optimization or dynamics. More technically, these are implemented as |
| 24 |
* stiff harmonic oscillators restraining a particular atom set towards the |
| 25 |
* value. |
| 26 |
*/ |
| 27 |
class AVOGADROCORE_EXPORT Constraint |
| 28 |
{ |
| 29 |
public: |
| 30 |
enum Type |
| 31 |
{ |
| 32 |
None = 0, |
| 33 |
DistanceConstraint, |
| 34 |
AngleConstraint, |
| 35 |
TorsionConstraint, |
| 36 |
OutOfPlaneConstraint, |
| 37 |
UnknownConstraint |
| 38 |
}; |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor, results in a zero distance constraint |
| 42 |
* @param a Atom index of the first atom of the constraint |
| 43 |
* @param b Atom index of the second atom of the constraint |
| 44 |
* @param c Atom index of the third atom (for angles or torsions) or MaxIndex |
| 45 |
* @param d Atom index of the fourth atom (for torsion constraints) or |
| 46 |
* MaxIndex |
| 47 |
* @param value The value of the constraint, either Angstrom for distance or |
| 48 |
* radians) |
| 49 |
*/ |
| 50 |
Constraint(Index a, Index b, Index c = MaxIndex, Index d = MaxIndex, |
| 51 |
Real value = 0.0) |
| 52 |
: m_aIndex(a), m_bIndex(b), m_cIndex(c), m_dIndex(d), m_value(value) |
| 53 |
{ |
| 54 |
} |
| 55 |
|
| 56 |
/** Set the constraint |
| 57 |
* @param a Atom index of the first atom of the constraint |
| 58 |
* @param b Atom index of the second atom of the constraint |
| 59 |
* @param c Atom index of the third atom (for angles or torsions) or MaxIndex |
| 60 |
* @param d Atom index of the fourth atom (for torsion constraints) or |
| 61 |
* MaxIndex |
| 62 |
* @param value The value of the constraint, either Angstrom for distance or |
| 63 |
* radians) |
| 64 |
*/ |
| 65 |
void set(Index a, Index b, Index c = MaxIndex, Index d = MaxIndex, |
| 66 |
Real value = 0.0) |
| 67 |
{ |
| 68 |
m_aIndex = a; |
| 69 |
m_bIndex = b; |
| 70 |
m_cIndex = c; |
| 71 |
m_dIndex = d; |
| 72 |
m_value = value; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Set the constraint value (distance, angle, dihedral) |
| 77 |
* @param value The value of the constraint, either Angstrom for distance or |
| 78 |
* radians) |
| 79 |
*/ |
| 80 |
void setValue(Real value) { m_value = value; } |
| 81 |
|
| 82 |
/** |
| 83 |
* @return the constraint value |
| 84 |
*/ |
| 85 |
Real value() const { return m_value; } |
| 86 |
|
| 87 |
/** |
| 88 |
* @return the atoms in the constraint as a tuple |
| 89 |
*/ |
| 90 |
std::tuple<Index, Index, Index, Index> atoms() const |
| 91 |
{ |
| 92 |
return std::make_tuple(m_aIndex, m_bIndex, m_cIndex, m_dIndex); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @return the atom index from the constraint or MaxIndex |
| 97 |
*/ |
| 98 |
Index aIndex() const { return m_aIndex; } |
| 99 |
Index bIndex() const { return m_bIndex; } |
| 100 |
Index cIndex() const { return m_cIndex; } |
| 101 |
Index dIndex() const { return m_dIndex; } |
| 102 |
|
| 103 |
Real k() const { return m_k; } |
| 104 |
void setK(Real k) { m_k = k; } |
| 105 |
|
| 106 |
/** |
| 107 |
* @return the type of constraint |
| 108 |
*/ |
| 109 |
Constraint::Type type() const |
| 110 |
{ |
| 111 |
if (m_type != None) |
| 112 |
return m_type; |
| 113 |
|
| 114 |
if (m_cIndex == MaxIndex && m_dIndex == MaxIndex) |
| 115 |
m_type = DistanceConstraint; |
| 116 |
else if (m_dIndex == MaxIndex) |
| 117 |
m_type = AngleConstraint; |
| 118 |
else if (m_dIndex != MaxIndex) |
| 119 |
m_type = TorsionConstraint; |
| 120 |
else |
| 121 |
m_type = UnknownConstraint; |
| 122 |
|
| 123 |
return m_type; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Set the type of constraint |
| 128 |
* @param type The type of constraint |
| 129 |
*/ |
| 130 |
void setType(Constraint::Type type) const { m_type = type; } |
| 131 |
|
| 132 |
protected: |
| 133 |
Index m_aIndex = MaxIndex; |
| 134 |
Index m_bIndex = MaxIndex; |
| 135 |
Index m_cIndex = MaxIndex; |
| 136 |
Index m_dIndex = MaxIndex; |
| 137 |
Real m_value = 0.0; |
| 138 |
Real m_k = 41840.0; // force constant, default in kJ/mol/Angstrom^2 |
| 139 |
mutable Constraint::Type m_type = None; // cached type, initialized to None |
| 140 |
}; |
| 141 |
|
| 142 |
} // End namespace Core |
| 143 |
} // End namespace Avogadro |
| 144 |
|
| 145 |
#endif // AVOGADRO_CORE_CONSTRAINT_H |