[b70721] | 1 | /*
|
---|
| 2 | * bondgraph.hpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Oct 29, 2009
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #ifndef BONDGRAPH_HPP_
|
---|
| 9 | #define BONDGRAPH_HPP_
|
---|
| 10 |
|
---|
| 11 | using namespace std;
|
---|
| 12 |
|
---|
| 13 | /*********************************************** includes ***********************************/
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[986ed3] | 20 | #include <iosfwd>
|
---|
[b70721] | 21 |
|
---|
[3738f0] | 22 | #include "AtomSet.hpp"
|
---|
[129204] | 23 | #include "Bond/bond.hpp"
|
---|
[3738f0] | 24 | #include "CodePatterns/Assert.hpp"
|
---|
| 25 | #include "CodePatterns/Log.hpp"
|
---|
| 26 | #include "CodePatterns/Range.hpp"
|
---|
| 27 | #include "CodePatterns/Verbose.hpp"
|
---|
| 28 | #include "element.hpp"
|
---|
| 29 | #include "linkedcell.hpp"
|
---|
| 30 | #include "IPointCloud.hpp"
|
---|
| 31 | #include "PointCloudAdaptor.hpp"
|
---|
[0cbad2] | 32 | #include "WorldTime.hpp"
|
---|
[b48ba6] | 33 |
|
---|
[b70721] | 34 | /****************************************** forward declarations *****************************/
|
---|
| 35 |
|
---|
| 36 | class molecule;
|
---|
[97ebf8] | 37 | class BondedParticle;
|
---|
[b70721] | 38 | class MatrixContainer;
|
---|
| 39 |
|
---|
| 40 | /********************************************** definitions *********************************/
|
---|
| 41 |
|
---|
| 42 | /********************************************** declarations *******************************/
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | class BondGraph {
|
---|
[300220] | 46 | //!> analysis bonds unit test should be friend to access private parts.
|
---|
| 47 | friend class AnalysisBondsTest;
|
---|
| 48 | //!> own bond graph unit test should be friend to access private parts.
|
---|
| 49 | friend class BondGraphTest;
|
---|
[b70721] | 50 | public:
|
---|
[e7350d4] | 51 | /** Constructor of class BondGraph.
|
---|
| 52 | * This classes contains typical bond lengths and thus may be used to construct a bond graph for a given molecule.
|
---|
| 53 | */
|
---|
[b70721] | 54 | BondGraph(bool IsA);
|
---|
[e7350d4] | 55 |
|
---|
| 56 | /** Destructor of class BondGraph.
|
---|
| 57 | */
|
---|
[b70721] | 58 | ~BondGraph();
|
---|
[e7350d4] | 59 |
|
---|
| 60 | /** Parses the bond lengths in a given file and puts them int a matrix form.
|
---|
| 61 | * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(),
|
---|
| 62 | * but only if parsing is successful. Otherwise variable is left as NULL.
|
---|
| 63 | * \param &input input stream to parse table from
|
---|
| 64 | * \return true - success in parsing file, false - failed to parse the file
|
---|
| 65 | */
|
---|
[4e855e] | 66 | bool LoadBondLengthTable(std::istream &input);
|
---|
[e7350d4] | 67 |
|
---|
[3738f0] | 68 | /** Determines the maximum of all element::CovalentRadius for elements present in \a &Set.
|
---|
| 69 | *
|
---|
[0ec7fe] | 70 | * I.e. the function returns a sensible cutoff criteria for bond recognition,
|
---|
| 71 | * e.g. to be used for LinkedCell or others.
|
---|
[3738f0] | 72 | *
|
---|
[0ec7fe] | 73 | * \param &Set AtomSetMixin with all particles to consider
|
---|
[3738f0] | 74 | */
|
---|
| 75 | template <class container_type,
|
---|
| 76 | class iterator_type,
|
---|
| 77 | class const_iterator_type>
|
---|
[0ec7fe] | 78 | double getMaxPossibleBondDistance(
|
---|
| 79 | const AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
---|
[3738f0] | 80 | {
|
---|
[0ec7fe] | 81 | double max_distance = 0.;
|
---|
| 82 | // get all elements
|
---|
| 83 | std::set< const element *> PresentElements;
|
---|
[3738f0] | 84 | for(const_iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
[0ec7fe] | 85 | PresentElements.insert( (*AtomRunner)->getType() );
|
---|
| 86 | }
|
---|
| 87 | // create all element combinations
|
---|
| 88 | for (std::set< const element *>::const_iterator iter = PresentElements.begin();
|
---|
| 89 | iter != PresentElements.end();
|
---|
| 90 | ++iter) {
|
---|
| 91 | for (std::set< const element *>::const_iterator otheriter = iter;
|
---|
| 92 | otheriter != PresentElements.end();
|
---|
| 93 | ++otheriter) {
|
---|
[607eab] | 94 | const range<double> MinMaxDistance(getMinMaxDistance((*iter),(*otheriter)));
|
---|
[0ec7fe] | 95 | if (MinMaxDistance.last > max_distance)
|
---|
| 96 | max_distance = MinMaxDistance.last;
|
---|
| 97 | }
|
---|
[3738f0] | 98 | }
|
---|
| 99 | return max_distance;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[72d90e] | 102 | /** Returns bond criterion for given pair based on a bond length matrix.
|
---|
[300220] | 103 | * This calls element-version of getMinMaxDistance().
|
---|
[72d90e] | 104 | * \param *Walker first BondedParticle
|
---|
| 105 | * \param *OtherWalker second BondedParticle
|
---|
[607eab] | 106 | * \return Range with bond interval
|
---|
[72d90e] | 107 | */
|
---|
[607eab] | 108 | range<double> getMinMaxDistance(
|
---|
[300220] | 109 | const BondedParticle * const Walker,
|
---|
[607eab] | 110 | const BondedParticle * const OtherWalker) const;
|
---|
[300220] | 111 |
|
---|
| 112 | /** Returns SQUARED bond criterion for given pair based on a bond length matrix.
|
---|
| 113 | * This calls element-version of getMinMaxDistance() and squares the values
|
---|
| 114 | * of either interval end.
|
---|
| 115 | * \param *Walker first BondedParticle
|
---|
| 116 | * \param *OtherWalker second BondedParticle
|
---|
[607eab] | 117 | * \return Range with bond interval
|
---|
[300220] | 118 | */
|
---|
[607eab] | 119 | range<double> getMinMaxDistanceSquared(
|
---|
[300220] | 120 | const BondedParticle * const Walker,
|
---|
[607eab] | 121 | const BondedParticle * const OtherWalker) const;
|
---|
[b70721] | 122 |
|
---|
[3738f0] | 123 | /** Creates the adjacency list for a given \a Range of iterable atoms.
|
---|
| 124 | *
|
---|
| 125 | * @param Set Range with begin and end iterator
|
---|
[e7350d4] | 126 | */
|
---|
[3738f0] | 127 | template <class container_type,
|
---|
| 128 | class iterator_type,
|
---|
| 129 | class const_iterator_type>
|
---|
[111f4a] | 130 | void CreateAdjacency(
|
---|
| 131 | AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
---|
[3738f0] | 132 | {
|
---|
| 133 | LOG(1, "STATUS: Removing all present bonds.");
|
---|
| 134 | cleanAdjacencyList(Set);
|
---|
| 135 |
|
---|
| 136 | // count atoms in molecule = dimension of matrix (also give each unique name and continuous numbering)
|
---|
| 137 | const unsigned int counter = Set.size();
|
---|
| 138 | if (counter > 1) {
|
---|
| 139 | LOG(1, "STATUS: Setting max bond distance.");
|
---|
[0ec7fe] | 140 | const double max_distance = getMaxPossibleBondDistance(Set);
|
---|
[3738f0] | 141 |
|
---|
| 142 | LOG(1, "STATUS: Creating LinkedCell structure for given " << counter << " atoms.");
|
---|
| 143 | PointCloudAdaptor< AtomSetMixin<container_type,iterator_type> > cloud(&Set, "SetOfAtoms");
|
---|
| 144 | LinkedCell *LC = new LinkedCell(cloud, max_distance);
|
---|
| 145 |
|
---|
| 146 | CreateAdjacency(*LC);
|
---|
| 147 | delete (LC);
|
---|
| 148 |
|
---|
| 149 | // correct bond degree by comparing valence and bond degree
|
---|
| 150 | LOG(1, "STATUS: Correcting bond degree.");
|
---|
| 151 | CorrectBondDegree(Set);
|
---|
| 152 |
|
---|
| 153 | // output bonds for debugging (if bond chain list was correctly installed)
|
---|
| 154 | LOG(2, "STATUS: Printing list of created bonds.");
|
---|
| 155 | std::stringstream output;
|
---|
| 156 | for(const_iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
| 157 | (*AtomRunner)->OutputBondOfAtom(output);
|
---|
| 158 | output << std::endl << "\t\t";
|
---|
| 159 | }
|
---|
| 160 | LOG(2, output.str());
|
---|
| 161 | } else {
|
---|
| 162 | LOG(1, "REJECT: AtomCount is " << counter << ", thus no bonds, no connections.");
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[0cbad2] | 166 | /** Creates an adjacency list of the given \a Set of atoms.
|
---|
| 167 | *
|
---|
| 168 | * Note that the input stream is required to refer to the same number of
|
---|
| 169 | * atoms also contained in \a Set.
|
---|
| 170 | *
|
---|
| 171 | * \param &Set container with atoms
|
---|
| 172 | * \param *input input stream to parse
|
---|
| 173 | * \param skiplines how many header lines to skip
|
---|
| 174 | * \param id_offset is base id compared to World startin at 0
|
---|
| 175 | */
|
---|
| 176 | template <class container_type,
|
---|
| 177 | class iterator_type,
|
---|
| 178 | class const_iterator_type>
|
---|
| 179 | void CreateAdjacencyListFromDbondFile(
|
---|
| 180 | AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set,
|
---|
| 181 | ifstream *input,
|
---|
| 182 | unsigned int skiplines,
|
---|
| 183 | int id_offset) const
|
---|
| 184 | {
|
---|
| 185 | char line[MAXSTRINGSIZE];
|
---|
| 186 |
|
---|
| 187 | // check input stream
|
---|
| 188 | if (input->fail()) {
|
---|
| 189 | ELOG(0, "Opening of bond file failed \n");
|
---|
| 190 | return;
|
---|
| 191 | };
|
---|
| 192 | // skip headers
|
---|
| 193 | unsigned int bondcount = 0;
|
---|
| 194 | for (unsigned int i=0;i<skiplines;i++)
|
---|
| 195 | input->getline(line,MAXSTRINGSIZE);
|
---|
| 196 |
|
---|
| 197 | // create lookup map
|
---|
| 198 | LOG(1, "STATUS: Creating lookup map.");
|
---|
| 199 | std::map< unsigned int, atom *> AtomLookup;
|
---|
| 200 | unsigned int counter = id_offset; // if ids do not start at 0
|
---|
| 201 | for (iterator_type iter = Set.begin(); iter != Set.end(); ++iter) {
|
---|
| 202 | AtomLookup.insert( make_pair( counter++, *iter) );
|
---|
| 203 | }
|
---|
| 204 | LOG(2, "INFO: There are " << counter << " atoms in the given set.");
|
---|
| 205 |
|
---|
| 206 | LOG(1, "STATUS: Scanning file.");
|
---|
| 207 | unsigned int atom1, atom2;
|
---|
| 208 | unsigned int bondcounter = 0;
|
---|
| 209 | while (!input->eof()) // Check whether we read everything already
|
---|
| 210 | {
|
---|
| 211 | input->getline(line,MAXSTRINGSIZE);
|
---|
| 212 | stringstream zeile(line);
|
---|
| 213 | if (zeile.str().empty())
|
---|
| 214 | continue;
|
---|
| 215 | zeile >> atom1;
|
---|
| 216 | zeile >> atom2;
|
---|
| 217 |
|
---|
| 218 | LOG(4, "INFO: Looking for atoms " << atom1 << " and " << atom2 << ".");
|
---|
| 219 | if (atom2 < atom1) //Sort indices of atoms in order
|
---|
| 220 | std::swap(atom1, atom2);
|
---|
| 221 | ASSERT(atom2 < counter,
|
---|
| 222 | "BondGraph::CreateAdjacencyListFromDbondFile() - ID "
|
---|
| 223 | +toString(atom2)+" exceeds number of present atoms "+toString(counter)+".");
|
---|
| 224 | ASSERT(AtomLookup.count(atom1),
|
---|
| 225 | "BondGraph::CreateAdjacencyListFromDbondFile() - Could not find an atom with the ID given in dbond file");
|
---|
| 226 | ASSERT(AtomLookup.count(atom2),
|
---|
| 227 | "BondGraph::CreateAdjacencyListFromDbondFile() - Could not find an atom with the ID given in dbond file");
|
---|
| 228 | atom * const Walker = AtomLookup[atom1];
|
---|
| 229 | atom * const OtherWalker = AtomLookup[atom2];
|
---|
| 230 |
|
---|
| 231 | LOG(3, "INFO: Creating bond between atoms " << atom1 << " and " << atom2 << ".");
|
---|
[db7e6d] | 232 | //const bond * Binder =
|
---|
| 233 | Walker->addBond(WorldTime::getTime(), OtherWalker);
|
---|
[0cbad2] | 234 | bondcounter++;
|
---|
| 235 | }
|
---|
| 236 | LOG(1, "STATUS: "<< bondcounter << " bonds have been parsed.");
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[3738f0] | 239 | /** Creates an adjacency list of the molecule.
|
---|
| 240 | * Generally, we use the CSD approach to bond recognition, that is the the distance
|
---|
| 241 | * between two atoms A and B must be within [Rcov(A)+Rcov(B)-t,Rcov(A)+Rcov(B)+t] with
|
---|
| 242 | * a threshold t = 0.4 Angstroem.
|
---|
| 243 | * To make it O(N log N) the function uses the linked-cell technique as follows:
|
---|
| 244 | * The procedure is step-wise:
|
---|
| 245 | * -# Remove every bond in list
|
---|
| 246 | * -# Count the atoms in the molecule with CountAtoms()
|
---|
| 247 | * -# partition cell into smaller linked cells of size \a bonddistance
|
---|
| 248 | * -# put each atom into its corresponding cell
|
---|
| 249 | * -# go through every cell, check the atoms therein against all possible bond partners in the 27 adjacent cells, add bond if true
|
---|
| 250 | * -# correct the bond degree iteratively (single->double->triple bond)
|
---|
| 251 | * -# finally print the bond list to \a *out if desired
|
---|
| 252 | * \param &LC Linked Cell Container with all atoms
|
---|
| 253 | */
|
---|
[111f4a] | 254 | void CreateAdjacency(LinkedCell &LC) const;
|
---|
[3738f0] | 255 |
|
---|
| 256 | /** Removes all bonds within the given set of iterable atoms.
|
---|
| 257 | *
|
---|
| 258 | * @param Set Range with atoms
|
---|
| 259 | */
|
---|
| 260 | template <class container_type,
|
---|
| 261 | class iterator_type,
|
---|
| 262 | class const_iterator_type>
|
---|
[111f4a] | 263 | void cleanAdjacencyList(
|
---|
| 264 | AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
---|
[3738f0] | 265 | {
|
---|
| 266 | // remove every bond from the list
|
---|
| 267 | for(iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
[5e2f80] | 268 | (*AtomRunner)->removeAllBonds();
|
---|
| 269 | // BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
| 270 | // for(BondList::iterator BondRunner = ListOfBonds.begin();
|
---|
| 271 | // !ListOfBonds.empty();
|
---|
| 272 | // BondRunner = ListOfBonds.begin()) {
|
---|
| 273 | // ASSERT((*BondRunner)->Contains(*AtomRunner),
|
---|
| 274 | // "BondGraph::cleanAdjacencyList() - "+
|
---|
| 275 | // toString(*BondRunner)+" does not contain "+
|
---|
| 276 | // toString(*AtomRunner)+".");
|
---|
| 277 | // delete((*BondRunner));
|
---|
| 278 | // }
|
---|
[3738f0] | 279 | }
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | /** correct bond degree by comparing valence and bond degree.
|
---|
| 283 | * correct Bond degree of each bond by checking both bond partners for a mismatch between valence and current sum of bond degrees,
|
---|
| 284 | * iteratively increase the one first where the other bond partner has the fewest number of bonds (i.e. in general bonds oxygene
|
---|
| 285 | * preferred over carbon bonds). Beforehand, we had picked the first mismatching partner, which lead to oxygenes with single instead of
|
---|
| 286 | * double bonds as was expected.
|
---|
| 287 | * @param Set Range with atoms
|
---|
| 288 | * \return number of bonds that could not be corrected
|
---|
| 289 | */
|
---|
| 290 | template <class container_type,
|
---|
| 291 | class iterator_type,
|
---|
| 292 | class const_iterator_type>
|
---|
[111f4a] | 293 | int CorrectBondDegree(
|
---|
| 294 | AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
---|
[3738f0] | 295 | {
|
---|
| 296 | // reset
|
---|
| 297 | resetBondDegree(Set);
|
---|
| 298 | // re-calculate
|
---|
| 299 | return calculateBondDegree(Set);
|
---|
| 300 | }
|
---|
[af2c424] | 301 |
|
---|
[b70721] | 302 | private:
|
---|
[88b400] | 303 | static const double BondThreshold;
|
---|
| 304 |
|
---|
[300220] | 305 | /** Returns the BondLengthMatrix entry for a given index pair.
|
---|
| 306 | * \param firstelement index/atom number of first element (row index)
|
---|
| 307 | * \param secondelement index/atom number of second element (column index)
|
---|
| 308 | * \note matrix is of course symmetric.
|
---|
| 309 | */
|
---|
| 310 | double GetBondLength(
|
---|
| 311 | int firstelement,
|
---|
| 312 | int secondelement) const;
|
---|
| 313 |
|
---|
[e7350d4] | 314 | /** Returns bond criterion for given pair based on a bond length matrix.
|
---|
[111f4a] | 315 | * This calls either the covalent or the bond matrix criterion.
|
---|
[e7350d4] | 316 | * \param *Walker first BondedParticle
|
---|
| 317 | * \param *OtherWalker second BondedParticle
|
---|
[607eab] | 318 | * \return Range with bond interval
|
---|
[e7350d4] | 319 | */
|
---|
[607eab] | 320 | range<double> getMinMaxDistance(
|
---|
[300220] | 321 | const element * const Walker,
|
---|
[607eab] | 322 | const element * const OtherWalker) const;
|
---|
[72d90e] | 323 |
|
---|
[300220] | 324 | /** Returns bond criterion for given pair of elements based on a bond length matrix.
|
---|
| 325 | * The matrix should be contained in \a this BondGraph and contain an element-
|
---|
| 326 | * to-element length.
|
---|
| 327 | * \param *Walker first element
|
---|
| 328 | * \param *OtherWalker second element
|
---|
[607eab] | 329 | * \return Range with bond interval
|
---|
[300220] | 330 | */
|
---|
[607eab] | 331 | range<double> BondLengthMatrixMinMaxDistance(
|
---|
[300220] | 332 | const element * const Walker,
|
---|
[607eab] | 333 | const element * const OtherWalker) const;
|
---|
[300220] | 334 |
|
---|
| 335 | /** Returns bond criterion for given pair of elements based on covalent radius.
|
---|
| 336 | * \param *Walker first element
|
---|
| 337 | * \param *OtherWalker second element
|
---|
[607eab] | 338 | * \return Range with bond interval
|
---|
[e7350d4] | 339 | */
|
---|
[607eab] | 340 | range<double> CovalentMinMaxDistance(
|
---|
[300220] | 341 | const element * const Walker,
|
---|
[607eab] | 342 | const element * const OtherWalker) const;
|
---|
[72d90e] | 343 |
|
---|
[3738f0] | 344 |
|
---|
| 345 | /** Resets the bond::BondDegree of all atoms in the set to 1.
|
---|
| 346 | *
|
---|
| 347 | * @param Set Range with atoms
|
---|
| 348 | */
|
---|
| 349 | template <class container_type,
|
---|
| 350 | class iterator_type,
|
---|
| 351 | class const_iterator_type>
|
---|
[111f4a] | 352 | void resetBondDegree(
|
---|
| 353 | AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
---|
[3738f0] | 354 | {
|
---|
| 355 | // reset bond degrees
|
---|
| 356 | for(iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
[5e2f80] | 357 | (*AtomRunner)->resetBondDegree();
|
---|
[3738f0] | 358 | }
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | /** Calculates the bond degree for each atom on the set.
|
---|
| 362 | *
|
---|
| 363 | * @param Set Range with atoms
|
---|
| 364 | * @return number of non-matching bonds
|
---|
| 365 | */
|
---|
| 366 | template <class container_type,
|
---|
| 367 | class iterator_type,
|
---|
| 368 | class const_iterator_type>
|
---|
[111f4a] | 369 | int calculateBondDegree(
|
---|
| 370 | AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
---|
[3738f0] | 371 | {
|
---|
| 372 | //DoLog(1) && (Log() << Verbose(1) << "Correcting Bond degree of each bond ... " << endl);
|
---|
| 373 | int No = 0, OldNo = -1;
|
---|
| 374 | do {
|
---|
| 375 | OldNo = No;
|
---|
| 376 | No=0;
|
---|
| 377 | for(iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
| 378 | No+=(*AtomRunner)->CorrectBondDegree();
|
---|
| 379 | }
|
---|
| 380 | } while (OldNo != No);
|
---|
| 381 | //DoLog(0) && (Log() << Verbose(0) << " done." << endl);
|
---|
| 382 | return No;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
[e7350d4] | 385 | //!> Matrix with bond lenth per two elements
|
---|
[b70721] | 386 | MatrixContainer *BondLengthMatrix;
|
---|
[e7350d4] | 387 | //!> distance units are angstroem (true), bohr radii (false)
|
---|
[b70721] | 388 | bool IsAngstroem;
|
---|
| 389 | };
|
---|
| 390 |
|
---|
| 391 | #endif /* BONDGRAPH_HPP_ */
|
---|