Chemical Data Processing Library C++ API - Version 1.2.0
ResonanceStructureGenerator.hpp
Go to the documentation of this file.
1 /*
2  * ResonanceStructureGenerator.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_CHEM_RESONANCESTRUCTUREGENERATOR_HPP
30 #define CDPL_CHEM_RESONANCESTRUCTUREGENERATOR_HPP
31 
32 #include <vector>
33 #include <cstddef>
34 #include <unordered_set>
35 #include <memory>
36 
37 #include <boost/iterator/indirect_iterator.hpp>
38 
39 #include "CDPL/Chem/APIPrefix.hpp"
40 #include "CDPL/Util/Array.hpp"
41 #include "CDPL/Util/BitSet.hpp"
42 #include "CDPL/Util/ObjectPool.hpp"
43 
44 
45 namespace CDPL
46 {
47 
48  namespace Chem
49  {
50 
51  class MolecularGraph;
52  class Atom;
53 
58  {
59 
60  public:
61  class StructureData;
62 
63  private:
65  typedef StructureDataCache::SharedObjectPointer StructureDataPtr;
66  typedef std::vector<StructureDataPtr> StructureDataList;
67 
68  public:
69  typedef std::shared_ptr<ResonanceStructureGenerator> SharedPointer;
70  typedef boost::indirect_iterator<StructureDataList::const_iterator, const StructureData> ConstStructureDataIterator;
71 
73  {
74 
76 
77  public:
78  const Util::LArray& getAtomCharges() const;
79 
80  const Util::STArray& getBondOrders() const;
81 
82  private:
83  Util::LArray atomCharges;
84  Util::STArray bondOrders;
85  std::size_t numCharges;
86  };
87 
92 
94 
96 
98 
100 
102 
103  void minimizeOctetRuleViolations(bool minimize);
104 
106 
107  void minimizeSP1GeometryViolations(bool minimize);
108 
110 
111  void minimizeCarbonBond12Charges(bool minimize);
112 
114 
115  void setChargeCountWindow(std::size_t win_size);
116 
117  std::size_t getChargeCountWindow() const;
118 
119  void setMaxNumGeneratedStructures(std::size_t max_num);
120 
121  std::size_t getMaxNumGeneratedStructures() const;
122 
127  void generate(const MolecularGraph& molgraph);
128 
129  std::size_t getNumStructures() const;
130 
131  const StructureData& getStructureData(std::size_t idx) const;
132 
134 
136 
138 
140 
141  private:
142  struct BondData
143  {
144 
145  std::size_t atom1Index;
146  std::size_t atom2Index;
147  std::size_t bondIndex;
148  };
149 
150  class AtomData
151  {
152 
153  public:
154  long init(const Atom& atom, const MolecularGraph& molgraph, std::size_t idx);
155 
156  bool canShiftElectrons() const;
157 
158  std::size_t getNumBonds() const;
159 
160  std::size_t getBondIndex(std::size_t list_idx) const;
161 
162  std::size_t getAtomIndex(std::size_t list_idx) const;
163 
164  std::size_t getIndex() const;
165 
166  unsigned int getType() const;
167 
168  double getElectronegativity() const;
169 
170  bool isSP1Hybridized(const StructureData& res_struct) const;
171 
172  bool checkValenceState(const StructureData& res_struct, long val_diff, long charge_diff) const;
173 
174  bool octetRuleFulfilled(const StructureData& res_struct) const;
175 
176  std::size_t countRepChargePairs(const Util::LArray& charges) const;
177 
178  bool getVisitedFlag() const;
179 
180  void setVisitedFlag();
181 
182  bool getInSmallRingFlag() const;
183 
184  void setInSmallRingFlag();
185 
186  private:
187  typedef std::vector<std::size_t> IndexArray;
188 
189  bool canShiftElecs;
190  std::size_t index;
191  unsigned int type;
192  long valElecCount;
193  std::size_t unprdElecCount;
194  std::size_t implHCount;
195  double enegativity;
196  IndexArray bondIndices;
197  IndexArray atomIndices;
198  bool inSmallRing;
199  bool visited;
200  };
201 
202  struct StructureDataPtrHashFunc
203  {
204 
205  std::size_t operator()(const StructureDataPtr& rs_ptr) const;
206  };
207 
208  struct StructureDataPtrCmpFunc
209  {
210 
211  bool operator()(const StructureDataPtr& rs_ptr1, const StructureDataPtr& rs_ptr2) const
212  {
213  return (rs_ptr1->getBondOrders() == rs_ptr2->getBondOrders() &&
214  rs_ptr1->getAtomCharges() == rs_ptr2->getAtomCharges());
215  }
216  };
217 
218  typedef std::vector<AtomData> AtomDataArray;
219  typedef std::vector<const AtomData*> AtomDataPtrArray;
220  typedef std::vector<BondData> BondDataList;
221  typedef std::unordered_set<StructureDataPtr, StructureDataPtrHashFunc, StructureDataPtrCmpFunc> StructureDataSet;
222 
223  void init(const MolecularGraph& molgraph);
224 
225  void createInputResStructData();
226 
227  void extractResBonds();
228  void extractResBonds(AtomData& atom_data);
229 
230  void genStartResStructs();
231  void genStartResStructs(std::size_t depth, std::size_t num_rep_chg_pairs);
232 
233  std::size_t countRepChargePairs() const;
234 
235  void genOutputResStructs();
236  void genOutputResStructs(StructureData& res_struct, std::size_t depth, std::size_t con_idx,
237  std::size_t num_charges);
238 
239  void postprocOutputResStructs();
240 
241  void minimzeResStructProperty(std::size_t (ResonanceStructureGenerator::*prop_func)(const StructureData&) const);
242 
243  std::size_t countOctetRuleViolations(const StructureData& res_struct) const;
244  std::size_t countSP1GeometryViolations(const StructureData& res_struct) const;
245  std::size_t count12ChargedCBonds(const StructureData& res_struct) const;
246 
247  void modifyResStruct(StructureData& res_struct, std::size_t bond_idx, std::size_t atom1_idx, std::size_t atom2_idx,
248  long bond_order_diff, long atom1_chg_diff, long atom2_chg_diff) const;
249 
250  StructureDataPtr copyResStructPtr(const StructureDataPtr& res_struct_ptr);
251  StructureDataPtr copyResStruct(const StructureData& res_struct);
252 
253  StructureDataCache resStructDataCache;
254  bool minOctRuleViolations;
255  bool minSP1GeomViolations;
256  bool minCBond12Charges;
257  std::size_t chargeCountWin;
258  Util::BitSet octRuleCheckAtomTypes;
259  std::size_t maxNumGenStructs;
260  const MolecularGraph* molGraph;
261  AtomDataArray atomData;
262  AtomDataPtrArray resAtoms;
263  BondDataList resBonds;
264  StructureData inputResStruct;
265  StructureDataList startResStructs;
266  StructureDataSet workingResStructs;
267  StructureDataList outputResStructs;
268  StructureDataList tmpOutputResStructs;
269  Util::LArray chargeDiffPtn;
270  Util::BitSet visBondMask;
271  std::size_t minNumRepChargePairs;
272  std::size_t minNumCharges;
273  };
274  } // namespace Chem
275 } // namespace CDPL
276 
277 #endif // CDPL_CHEM_RESONANCESTRUCTUREGENERATOR_HPP
Definition of the class CDPL::Util::Array.
Definition of the type CDPL::Util::BitSet.
Definition of the preprocessor macro CDPL_CHEM_API.
#define CDPL_CHEM_API
Tells the compiler/linker which classes, functions and variables are part of the library API.
Definition of the class CDPL::Util::ObjectPool.
Atom.
Definition: Atom.hpp:52
MolecularGraph.
Definition: MolecularGraph.hpp:52
Definition: ResonanceStructureGenerator.hpp:73
ResonanceStructureGenerator.
Definition: ResonanceStructureGenerator.hpp:58
std::shared_ptr< ResonanceStructureGenerator > SharedPointer
Definition: ResonanceStructureGenerator.hpp:69
ConstStructureDataIterator begin() const
ResonanceStructureGenerator(const ResonanceStructureGenerator &gen)
void minimizeOctetRuleViolations(bool minimize)
void setChargeCountWindow(std::size_t win_size)
std::size_t getMaxNumGeneratedStructures() const
ConstStructureDataIterator getStructureDataEnd() const
ConstStructureDataIterator end() const
ResonanceStructureGenerator & operator=(const ResonanceStructureGenerator &gen)
const StructureData & getStructureData(std::size_t idx) const
ResonanceStructureGenerator()
Constructs the ResonanceStructureGenerator instance.
boost::indirect_iterator< StructureDataList::const_iterator, const StructureData > ConstStructureDataIterator
Definition: ResonanceStructureGenerator.hpp:70
ConstStructureDataIterator getStructureDataBegin() const
const Util::BitSet & getOctetRuleCheckAtomTypes() const
void generate(const MolecularGraph &molgraph)
Generates all unique resonanceStructures of the molecular graph molgraph.
void minimizeCarbonBond12Charges(bool minimize)
virtual ~ResonanceStructureGenerator()
Definition: ResonanceStructureGenerator.hpp:95
void setMaxNumGeneratedStructures(std::size_t max_num)
void minimizeSP1GeometryViolations(bool minimize)
std::shared_ptr< ObjectType > SharedObjectPointer
Definition: ObjectPool.hpp:65
CDPL_CHEM_API unsigned int getType(const Atom &atom)
Array< std::size_t > STArray
An array of unsigned integers of type std::size_t.
Definition: Array.hpp:567
Array< long > LArray
An array of unsigned integers of type long.
Definition: Array.hpp:572
boost::dynamic_bitset BitSet
A dynamic bitset class.
Definition: BitSet.hpp:46
The namespace of the Chemical Data Processing Library.