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