| 1 | // -*- Mode: C++; tab-width: 2; -*- |
| 2 | // vi: set ts=2: |
| 3 | // |
| 4 | // $Id: support.h,v 1.28 2005/12/23 17:01:52 amoll Exp $ |
| 5 | // |
| 6 | |
| 7 | |
| 8 | #ifndef BALL_MOLMEC_COMMON_SUPPORT_H |
| 9 | #define BALL_MOLMEC_COMMON_SUPPORT_H |
| 10 | |
| 11 | #ifndef BALL_COMMON_H |
| 12 | # include <BALL/common.h> |
| 13 | #endif |
| 14 | |
| 15 | #ifndef BALL_MATHS_SIMPLEBOX3_H |
| 16 | # include <BALL/MATHS/simpleBox3.h> |
| 17 | #endif |
| 18 | |
| 19 | #ifndef BALL_MOLMEC_COMMON_FORCEFIELD_H |
| 20 | # include <BALL/MOLMEC/COMMON/forceField.h> |
| 21 | #endif |
| 22 | |
| 23 | #ifndef BALL_MOLMEC_COMMON_ATOMVECTOR_H |
| 24 | # include <BALL/MOLMEC/COMMON/atomVector.h> |
| 25 | #endif |
| 26 | |
| 27 | #ifndef BALL_DATATYPE_HASHGRID_H |
| 28 | # include <BALL/DATATYPE/hashGrid.h> |
| 29 | #endif |
| 30 | |
| 31 | #ifndef BALL_KERNEL_BOND_H |
| 32 | # include <BALL/KERNEL/bond.h> |
| 33 | #endif |
| 34 | |
| 35 | #include <vector> |
| 36 | |
| 37 | namespace BALL |
| 38 | { |
| 39 | class Atom; |
| 40 | class System; |
| 41 | |
| 42 | /** MolmecSupport |
| 43 | \ingroup MolmecCommon |
| 44 | */ |
| 45 | namespace MolmecSupport |
| 46 | { |
| 47 | |
| 48 | using std::pair; |
| 49 | |
| 50 | /** @name Support Functions for the Implementation of Force Fields. |
| 51 | */ |
| 52 | //@{ |
| 53 | |
| 54 | /** @name Enums |
| 55 | */ |
| 56 | //@{ |
| 57 | |
| 58 | /** The type of algorithm used to calculate the pair list. |
| 59 | */ |
| 60 | enum PairListAlgorithmType |
| 61 | { |
| 62 | /** use a hash grid |
| 63 | */ |
| 64 | HASH_GRID, |
| 65 | |
| 66 | /** brute force all against all |
| 67 | */ |
| 68 | BRUTE_FORCE |
| 69 | }; |
| 70 | //@} |
| 71 | |
| 72 | /** Create a pair vector for non-bonded interactions. |
| 73 | Calculates a vector of atom pairs whose distance is smaller than |
| 74 | <tt>distance</tt>. The <tt>type</tt> determines if a brute force algorithm |
| 75 | (<tt>type == BRUTE_FORCE</tt>) or a more sophisticated grid method |
| 76 | (<tt>type == HASH_GRID</tt>) is used. |
| 77 | @param pair_vector the vector containing pairs of interacting atoms |
| 78 | @param atom_vector the atoms to be considered for pairs |
| 79 | @param box the periodic boundary used (if <tt> periodic_boundary_enabled == true</tt>) |
| 80 | @param distance the maximum distance (cut-off distance) for two atoms |
| 81 | @param periodic_boundary_enabled flag indicating the use of periodic boundary conditions |
| 82 | @param type the type of algorithm used to calculate the pair vector |
| 83 | @return the number of pairs generated (<tt>pair_vector.size()</tt>) |
| 84 | @throws BALL::Exception::OutOfMemory |
| 85 | */ |
| 86 | BALL_EXPORT BALL::Size calculateNonBondedAtomPairs |
| 87 | (ForceField::PairVector& pair_vector, |
| 88 | const AtomVector& atom_vector, const SimpleBox3& box, |
| 89 | double distance, bool periodic_boundary_enabled, |
| 90 | PairListAlgorithmType type); |
| 91 | |
| 92 | /** Sort the pair list. |
| 93 | The atom pairs in the list ar sorted in such a way, that those atom pairs |
| 94 | where at least one of the atoms is selected are at the beginning of the list. |
| 95 | The number of such pairs is returned. Running time is linear in the length of the list. |
| 96 | @return number of atom pairs in the list, where at least one atom is selected |
| 97 | @param pair_vector the unsorted pair_list (modified during the sorting) |
| 98 | */ |
| 99 | BALL_EXPORT Size sortNonBondedAtomPairsAfterSelection(ForceField::PairVector& pair_vector); |
| 100 | |
| 101 | /** Merge the non-overlapping molecules of a system into another system. |
| 102 | Fills <tt>system</tt> with copies of the solvent molecules stored in <tt>solvent</tt>. |
| 103 | Solvent molecules are added to <tt>system</tt> if the molecules lie in the |
| 104 | <tt>box</tt> and if they do not overlap with the molecules in <tt>solute_grid</tt>. |
| 105 | A molecule has an overlap with another molecule if any atom of the first |
| 106 | molecule is within <tt> distance</tt> of any atom of the other molecule. |
| 107 | @param system the system to be filled |
| 108 | @param solute_grid a hash grid containing the solute molecules |
| 109 | @param solvent the system containing the solvent |
| 110 | @param box the periodic boundary of <tt>system</tt> |
| 111 | @param distance used to determine overlaps between two atoms |
| 112 | @return the number of molecules added to <tt>system</tt> |
| 113 | */ |
| 114 | BALL_EXPORT BALL::Size addNonOverlappingMolecules |
| 115 | (System& system, const HashGrid3<const Atom*>& solute_grid, |
| 116 | const System& solvent, const SimpleBox3& box, double distance); |
| 117 | |
| 118 | /** Adapt periodic water (or general solvent) boxes to our periodic |
| 119 | boundary definition. |
| 120 | Periodic water boxes created by different programs might contain |
| 121 | molecules which have their center of gravity outside of the box |
| 122 | because of a different defintion of the periodic boundary. This |
| 123 | function adapts such boxes to our definition by simply translating |
| 124 | such molecules to the opposite boundary wall such that their center |
| 125 | of gravity lies in the box afterwards. \par |
| 126 | This might be obsoleted by PeriodicBoundarry::updateMolecules() |
| 127 | @param system the system containing the water box which is to be adapted |
| 128 | @param box the box definition for the periodic boundary |
| 129 | */ |
| 130 | BALL_EXPORT void adaptWaterBox(System& system, const SimpleBox3& box); |
| 131 | |
| 132 | /** Compute the minimum image |
| 133 | */ |
| 134 | // ????? |
| 135 | BALL_EXPORT void calculateMinimumImage |
| 136 | (Vector3& distance, const Vector3& period); |
| 137 | |
| 138 | /** Compute all torsions in a given set of molecules. |
| 139 | @return the number of torsions added to <tt>torsions</tt> |
| 140 | @param start an iterator pointing to the start of the atoms |
| 141 | @param end a past-the-end iterator for the atoms |
| 142 | @param torsions the vector of torsions |
| 143 | @param use_selection if set to <b>true</b>, a torsion will be added only if all four atoms are selected |
| 144 | */ |
| 145 | template <typename TorsionType, typename AtomIteratorType> |
| 146 | Size computeTorsions |
| 147 | (const AtomIteratorType& start, const AtomIteratorType& end, |
| 148 | std::vector<TorsionType>& torsions, bool use_selection = false); |
| 149 | |
| 150 | |
| 151 | // |
| 152 | template <typename TorsionType, typename AtomIteratorType> |
| 153 | Size computeTorsions |
| 154 | (const AtomIteratorType& start, const AtomIteratorType& end, |
| 155 | std::vector<TorsionType>& torsions, bool use_selection) |
| 156 | { |
| 157 | // pointers to the four atoms |
| 158 | Atom* a1; |
| 159 | Atom* a2; |
| 160 | Atom* a3; |
| 161 | Atom* a4; |
| 162 | |
| 163 | Size number_of_added_torsions = 0; |
| 164 | |
| 165 | // Iterate over all atoms... |
| 166 | // |
| 167 | AtomIteratorType atom_it = start; |
| 168 | for (; atom_it != end; ++atom_it) |
| 169 | { |
| 170 | // ...and check each bond whether it is part of |
| 171 | // a torsion. |
| 172 | Atom::BondIterator it1 = (*atom_it)->beginBond(); |
| 173 | for (; +it1 ; ++ it1) |
| 174 | { |
| 175 | // Consider each bond just once by making sure that |
| 176 | // our start atom is the *first* atom of the bond. |
| 177 | if (*atom_it == it1->getFirstAtom()) |
| 178 | { |
| 179 | // We know have the two central atoms of a potential |
| 180 | // torsion and store them in a2 and a3. |
| 181 | a2 = *atom_it; |
| 182 | a3 = const_cast<Atom*>(it1->getSecondAtom()); |
| 183 | |
| 184 | // Now, find all other atoms (atoms 1 and 4) |
| 185 | Atom::BondIterator it2; |
| 186 | Atom::BondIterator it3; |
| 187 | for (it2 = (*atom_it)->beginBond(); +it2 ; ++it2) |
| 188 | { |
| 189 | if (it2->getSecondAtom() != it1->getSecondAtom()) |
| 190 | { |
| 191 | // determine the first atom |
| 192 | if (it2->getFirstAtom() == *atom_it) |
| 193 | { |
| 194 | a1 = const_cast<Atom*>(it2->getSecondAtom()); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | a1 = const_cast<Atom*>(it2->getFirstAtom()); |
| 199 | } |
| 200 | |
| 201 | for (it3 = const_cast<Atom*>(it1->getSecondAtom())->beginBond(); +it3 ; ++it3) |
| 202 | { |
| 203 | if (it3->getFirstAtom() != a2 ) |
| 204 | { |
| 205 | // determine the fourth atom a4 |
| 206 | if (it3->getFirstAtom() == a3) |
| 207 | { |
| 208 | a4 = const_cast<Atom*>(it3->getSecondAtom()); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | a4 = const_cast<Atom*>(it3->getFirstAtom()); |
| 213 | } |
| 214 | |
| 215 | if (use_selection == false |
| 216 | || (use_selection == true |
| 217 | && (a1->isSelected() || a2->isSelected() || a3->isSelected() || a4->isSelected()))) |
| 218 | { |
| 219 | // Push the torsion onto the torsion vector. |
| 220 | TorsionType tmp; |
| 221 | tmp.atom1 = a1; |
| 222 | tmp.atom2 = a2; |
| 223 | tmp.atom3 = a3; |
| 224 | tmp.atom4 = a4; |
| 225 | |
| 226 | torsions.push_back(tmp); |
| 227 | number_of_added_torsions++; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // return the number of torsions computed |
| 238 | return number_of_added_torsions; |
| 239 | } |
| 240 | |
| 241 | } // namespace MolmecSupport |
| 242 | } // namespace BALL |
| 243 | |
| 244 | #endif // BALL_MOLMEC_COMMON_SUPPORT_H |