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 |
|
---|
33 | /****************************************** forward declarations *****************************/
|
---|
34 |
|
---|
35 | class molecule;
|
---|
36 | class BondedParticle;
|
---|
37 | class MatrixContainer;
|
---|
38 |
|
---|
39 | /********************************************** definitions *********************************/
|
---|
40 |
|
---|
41 | /********************************************** declarations *******************************/
|
---|
42 |
|
---|
43 |
|
---|
44 | class BondGraph {
|
---|
45 | public:
|
---|
46 | /** Constructor of class BondGraph.
|
---|
47 | * This classes contains typical bond lengths and thus may be used to construct a bond graph for a given molecule.
|
---|
48 | */
|
---|
49 | BondGraph(bool IsA);
|
---|
50 |
|
---|
51 | /** Destructor of class BondGraph.
|
---|
52 | */
|
---|
53 | ~BondGraph();
|
---|
54 |
|
---|
55 | /** Parses the bond lengths in a given file and puts them int a matrix form.
|
---|
56 | * Allocates \a MatrixContainer for BondGraph::BondLengthMatrix, using MatrixContainer::ParseMatrix(),
|
---|
57 | * but only if parsing is successful. Otherwise variable is left as NULL.
|
---|
58 | * \param &input input stream to parse table from
|
---|
59 | * \return true - success in parsing file, false - failed to parse the file
|
---|
60 | */
|
---|
61 | bool LoadBondLengthTable(std::istream &input);
|
---|
62 |
|
---|
63 | /** Returns the entry for a given index pair.
|
---|
64 | * \param firstelement index/atom number of first element (row index)
|
---|
65 | * \param secondelement index/atom number of second element (column index)
|
---|
66 | * \note matrix is of course symmetric.
|
---|
67 | */
|
---|
68 | double GetBondLength(int firstelement, int secondelement);
|
---|
69 |
|
---|
70 | /** Determines the maximum of all element::CovalentRadius for elements present in \a &Set.
|
---|
71 | *
|
---|
72 | * Sets BondGraph::max_distance
|
---|
73 | *
|
---|
74 | * \param &Set PointCloud with all particles
|
---|
75 | */
|
---|
76 | template <class container_type,
|
---|
77 | class iterator_type,
|
---|
78 | class const_iterator_type>
|
---|
79 | double SetMaxDistanceToMaxOfCovalentRadii(const AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set)
|
---|
80 | {
|
---|
81 | max_distance = 0.;
|
---|
82 |
|
---|
83 | for(const_iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
84 | const double radius = (*AtomRunner)->getType()->getCovalentRadius();
|
---|
85 | if (radius > max_distance)
|
---|
86 | max_distance = radius;
|
---|
87 | }
|
---|
88 | max_distance *= 2.;
|
---|
89 | max_distance += BondThreshold;
|
---|
90 |
|
---|
91 | return max_distance;
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Returns the upper limit on possible bond distances.
|
---|
95 | *
|
---|
96 | * Is set by SetMaxDistanceToMaxOfCovalentRadii(), is needed e.g. as cutoff
|
---|
97 | * for linked-cell.
|
---|
98 | *
|
---|
99 | * \return maximum possible bond distance
|
---|
100 | */
|
---|
101 | double getMaxDistance() const;
|
---|
102 |
|
---|
103 | /** Returns bond criterion for given pair based on a bond length matrix.
|
---|
104 | * This calls either the covalent or the bond matrix criterion.
|
---|
105 | * \param *Walker first BondedParticle
|
---|
106 | * \param *OtherWalker second BondedParticle
|
---|
107 | * \param &MinDistance lower bond bound on return
|
---|
108 | * \param &MaxDistance upper bond bound on return
|
---|
109 | * \param IsAngstroem whether units are in angstroem or bohr radii
|
---|
110 | */
|
---|
111 | void getMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem);
|
---|
112 |
|
---|
113 | /** Creates the adjacency list for a given \a Range of iterable atoms.
|
---|
114 | *
|
---|
115 | * @param Set Range with begin and end iterator
|
---|
116 | */
|
---|
117 | template <class container_type,
|
---|
118 | class iterator_type,
|
---|
119 | class const_iterator_type>
|
---|
120 | void CreateAdjacency(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set)
|
---|
121 | {
|
---|
122 | LOG(1, "STATUS: Removing all present bonds.");
|
---|
123 | cleanAdjacencyList(Set);
|
---|
124 |
|
---|
125 | // count atoms in molecule = dimension of matrix (also give each unique name and continuous numbering)
|
---|
126 | const unsigned int counter = Set.size();
|
---|
127 | if (counter > 1) {
|
---|
128 | LOG(1, "STATUS: Setting max bond distance.");
|
---|
129 | SetMaxDistanceToMaxOfCovalentRadii(Set);
|
---|
130 |
|
---|
131 | LOG(1, "STATUS: Creating LinkedCell structure for given " << counter << " atoms.");
|
---|
132 | PointCloudAdaptor< AtomSetMixin<container_type,iterator_type> > cloud(&Set, "SetOfAtoms");
|
---|
133 | LinkedCell *LC = new LinkedCell(cloud, max_distance);
|
---|
134 |
|
---|
135 | CreateAdjacency(*LC);
|
---|
136 | delete (LC);
|
---|
137 |
|
---|
138 | // correct bond degree by comparing valence and bond degree
|
---|
139 | LOG(1, "STATUS: Correcting bond degree.");
|
---|
140 | CorrectBondDegree(Set);
|
---|
141 |
|
---|
142 | // output bonds for debugging (if bond chain list was correctly installed)
|
---|
143 | LOG(2, "STATUS: Printing list of created bonds.");
|
---|
144 | std::stringstream output;
|
---|
145 | for(const_iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
146 | (*AtomRunner)->OutputBondOfAtom(output);
|
---|
147 | output << std::endl << "\t\t";
|
---|
148 | }
|
---|
149 | LOG(2, output.str());
|
---|
150 | } else {
|
---|
151 | LOG(1, "REJECT: AtomCount is " << counter << ", thus no bonds, no connections.");
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /** Creates an adjacency list of the molecule.
|
---|
156 | * Generally, we use the CSD approach to bond recognition, that is the the distance
|
---|
157 | * between two atoms A and B must be within [Rcov(A)+Rcov(B)-t,Rcov(A)+Rcov(B)+t] with
|
---|
158 | * a threshold t = 0.4 Angstroem.
|
---|
159 | * To make it O(N log N) the function uses the linked-cell technique as follows:
|
---|
160 | * The procedure is step-wise:
|
---|
161 | * -# Remove every bond in list
|
---|
162 | * -# Count the atoms in the molecule with CountAtoms()
|
---|
163 | * -# partition cell into smaller linked cells of size \a bonddistance
|
---|
164 | * -# put each atom into its corresponding cell
|
---|
165 | * -# go through every cell, check the atoms therein against all possible bond partners in the 27 adjacent cells, add bond if true
|
---|
166 | * -# correct the bond degree iteratively (single->double->triple bond)
|
---|
167 | * -# finally print the bond list to \a *out if desired
|
---|
168 | * \param &LC Linked Cell Container with all atoms
|
---|
169 | */
|
---|
170 | void CreateAdjacency(LinkedCell &LC);
|
---|
171 |
|
---|
172 | /** Removes all bonds within the given set of iterable atoms.
|
---|
173 | *
|
---|
174 | * @param Set Range with atoms
|
---|
175 | */
|
---|
176 | template <class container_type,
|
---|
177 | class iterator_type,
|
---|
178 | class const_iterator_type>
|
---|
179 | void cleanAdjacencyList(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set)
|
---|
180 | {
|
---|
181 | // remove every bond from the list
|
---|
182 | for(iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
183 | BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
184 | for(BondList::iterator BondRunner = ListOfBonds.begin();
|
---|
185 | !ListOfBonds.empty();
|
---|
186 | BondRunner = ListOfBonds.begin()) {
|
---|
187 | ASSERT((*BondRunner)->Contains(*AtomRunner),
|
---|
188 | "BondGraph::cleanAdjacencyList() - "+
|
---|
189 | toString(*BondRunner)+" does not contain "+
|
---|
190 | toString(*AtomRunner)+".");
|
---|
191 | delete((*BondRunner));
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | /** correct bond degree by comparing valence and bond degree.
|
---|
197 | * correct Bond degree of each bond by checking both bond partners for a mismatch between valence and current sum of bond degrees,
|
---|
198 | * iteratively increase the one first where the other bond partner has the fewest number of bonds (i.e. in general bonds oxygene
|
---|
199 | * preferred over carbon bonds). Beforehand, we had picked the first mismatching partner, which lead to oxygenes with single instead of
|
---|
200 | * double bonds as was expected.
|
---|
201 | * @param Set Range with atoms
|
---|
202 | * \return number of bonds that could not be corrected
|
---|
203 | */
|
---|
204 | template <class container_type,
|
---|
205 | class iterator_type,
|
---|
206 | class const_iterator_type>
|
---|
207 | int CorrectBondDegree(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
---|
208 | {
|
---|
209 | // reset
|
---|
210 | resetBondDegree(Set);
|
---|
211 | // re-calculate
|
---|
212 | return calculateBondDegree(Set);
|
---|
213 | }
|
---|
214 |
|
---|
215 | private:
|
---|
216 | static const double BondThreshold;
|
---|
217 |
|
---|
218 | /** Returns bond criterion for given pair based on a bond length matrix.
|
---|
219 | * The matrix should be contained in \a this BondGraph and contain an element-
|
---|
220 | * to-element length.
|
---|
221 | * \param *Walker first BondedParticle
|
---|
222 | * \param *OtherWalker second BondedParticle
|
---|
223 | * \param &MinDistance lower bond bound on return
|
---|
224 | * \param &MaxDistance upper bond bound on return
|
---|
225 | * \param IsAngstroem whether units are in angstroem or bohr radii
|
---|
226 | */
|
---|
227 | void BondLengthMatrixMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem);
|
---|
228 |
|
---|
229 | /** Returns bond criterion for given pair based on covalent radius.
|
---|
230 | * \param *Walker first BondedParticle
|
---|
231 | * \param *OtherWalker second BondedParticle
|
---|
232 | * \param &MinDistance lower bond bound on return
|
---|
233 | * \param &MaxDistance upper bond bound on return
|
---|
234 | * \param IsAngstroem whether units are in angstroem or bohr radii
|
---|
235 | */
|
---|
236 | void CovalentMinMaxDistance(const BondedParticle * const Walker, const BondedParticle * const OtherWalker, double &MinDistance, double &MaxDistance, bool IsAngstroem);
|
---|
237 |
|
---|
238 |
|
---|
239 | /** Resets the bond::BondDegree of all atoms in the set to 1.
|
---|
240 | *
|
---|
241 | * @param Set Range with atoms
|
---|
242 | */
|
---|
243 | template <class container_type,
|
---|
244 | class iterator_type,
|
---|
245 | class const_iterator_type>
|
---|
246 | void resetBondDegree(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
---|
247 | {
|
---|
248 | // reset bond degrees
|
---|
249 | for(iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
250 | BondList &ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
251 | for (BondList::iterator BondIter = ListOfBonds.begin();
|
---|
252 | BondIter != ListOfBonds.end();
|
---|
253 | ++BondIter)
|
---|
254 | (*BondIter)->BondDegree = 1;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | /** Calculates the bond degree for each atom on the set.
|
---|
259 | *
|
---|
260 | * @param Set Range with atoms
|
---|
261 | * @return number of non-matching bonds
|
---|
262 | */
|
---|
263 | template <class container_type,
|
---|
264 | class iterator_type,
|
---|
265 | class const_iterator_type>
|
---|
266 | int calculateBondDegree(AtomSetMixin<container_type,iterator_type,const_iterator_type> &Set) const
|
---|
267 | {
|
---|
268 | //DoLog(1) && (Log() << Verbose(1) << "Correcting Bond degree of each bond ... " << endl);
|
---|
269 | int No = 0, OldNo = -1;
|
---|
270 | do {
|
---|
271 | OldNo = No;
|
---|
272 | No=0;
|
---|
273 | for(iterator_type AtomRunner = Set.begin(); AtomRunner != Set.end(); ++AtomRunner) {
|
---|
274 | No+=(*AtomRunner)->CorrectBondDegree();
|
---|
275 | }
|
---|
276 | } while (OldNo != No);
|
---|
277 | //DoLog(0) && (Log() << Verbose(0) << " done." << endl);
|
---|
278 | return No;
|
---|
279 | }
|
---|
280 |
|
---|
281 | //!> Matrix with bond lenth per two elements
|
---|
282 | MatrixContainer *BondLengthMatrix;
|
---|
283 | //!> maximum distance over all bonds possible
|
---|
284 | double max_distance;
|
---|
285 | //!> distance units are angstroem (true), bohr radii (false)
|
---|
286 | bool IsAngstroem;
|
---|
287 | };
|
---|
288 |
|
---|
289 | #endif /* BONDGRAPH_HPP_ */
|
---|