| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | /*
 | 
|---|
| 9 |  * molecule_graph.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: Oct 5, 2009
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include <stack>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include "atom.hpp"
 | 
|---|
| 25 | #include "Bond/bond.hpp"
 | 
|---|
| 26 | #include "Box.hpp"
 | 
|---|
| 27 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 28 | #include "CodePatterns/Info.hpp"
 | 
|---|
| 29 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 30 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 31 | #include "config.hpp"
 | 
|---|
| 32 | #include "Graph/DepthFirstSearchAnalysis.hpp"
 | 
|---|
| 33 | #include "element.hpp"
 | 
|---|
| 34 | #include "Graph/BondGraph.hpp"
 | 
|---|
| 35 | #include "Helpers/defs.hpp"
 | 
|---|
| 36 | #include "Helpers/fast_functions.hpp"
 | 
|---|
| 37 | #include "Helpers/helpers.hpp"
 | 
|---|
| 38 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| 39 | #include "linkedcell.hpp"
 | 
|---|
| 40 | #include "molecule.hpp"
 | 
|---|
| 41 | #include "PointCloudAdaptor.hpp"
 | 
|---|
| 42 | #include "World.hpp"
 | 
|---|
| 43 | #include "WorldTime.hpp"
 | 
|---|
| 44 | 
 | 
|---|
| 45 | #define MAXBONDS 8
 | 
|---|
| 46 | 
 | 
|---|
| 47 | 
 | 
|---|
| 48 | /** Fills the bond structure of this chain list subgraphs that are derived from a complete \a *reference molecule.
 | 
|---|
| 49 |  * Calls this routine in each MoleculeLeafClass::next subgraph if it's not NULL.
 | 
|---|
| 50 |  * \param *reference reference molecule with the bond structure to be copied
 | 
|---|
| 51 |  * \param **&ListOfLocalAtoms Lookup table for this subgraph and index of each atom in \a *reference, may be NULL on start, then it is filled
 | 
|---|
| 52 |  * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not
 | 
|---|
| 53 |  * \return true - success, false - failure
 | 
|---|
| 54 |  */
 | 
|---|
| 55 | bool molecule::FillBondStructureFromReference(const molecule * const reference, atom **&ListOfLocalAtoms, bool FreeList)
 | 
|---|
| 56 | {
 | 
|---|
| 57 |   atom *OtherWalker = NULL;
 | 
|---|
| 58 |   atom *Father = NULL;
 | 
|---|
| 59 |   bool status = true;
 | 
|---|
| 60 |   int AtomNo;
 | 
|---|
| 61 | 
 | 
|---|
| 62 |   DoLog(1) && (Log() << Verbose(1) << "Begin of FillBondStructureFromReference." << endl);
 | 
|---|
| 63 |   // fill ListOfLocalAtoms if NULL was given
 | 
|---|
| 64 |   if (!FillListOfLocalAtoms(ListOfLocalAtoms, reference->getAtomCount())) {
 | 
|---|
| 65 |     DoLog(1) && (Log() << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl);
 | 
|---|
| 66 |     return false;
 | 
|---|
| 67 |   }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   if (status) {
 | 
|---|
| 70 |     DoLog(1) && (Log() << Verbose(1) << "Creating adjacency list for molecule " << getName() << "." << endl);
 | 
|---|
| 71 |     // remove every bond from the list
 | 
|---|
| 72 |     for_each(begin(), end(),
 | 
|---|
| 73 |         boost::bind(&BondedParticle::ClearBondsAtStep,_1,WorldTime::getTime()));
 | 
|---|
| 74 | 
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     for(molecule::const_iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
| 77 |       Father = (*iter)->GetTrueFather();
 | 
|---|
| 78 |       AtomNo = Father->getNr(); // global id of the current walker
 | 
|---|
| 79 |       const BondList& ListOfBonds = Father->getListOfBonds();
 | 
|---|
| 80 |       for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
| 81 |           Runner != ListOfBonds.end();
 | 
|---|
| 82 |           ++Runner) {
 | 
|---|
| 83 |         OtherWalker = ListOfLocalAtoms[(*Runner)->GetOtherAtom((*iter)->GetTrueFather())->getNr()]; // local copy of current bond partner of walker
 | 
|---|
| 84 |         if (OtherWalker != NULL) {
 | 
|---|
| 85 |           if (OtherWalker->getNr() > (*iter)->getNr())
 | 
|---|
| 86 |             AddBond((*iter), OtherWalker, (*Runner)->BondDegree);
 | 
|---|
| 87 |         } else {
 | 
|---|
| 88 |           DoLog(1) && (Log() << Verbose(1) << "OtherWalker = ListOfLocalAtoms[" << (*Runner)->GetOtherAtom((*iter)->GetTrueFather())->getNr() << "] is NULL!" << endl);
 | 
|---|
| 89 |           status = false;
 | 
|---|
| 90 |         }
 | 
|---|
| 91 |       }
 | 
|---|
| 92 |     }
 | 
|---|
| 93 |   }
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   if ((FreeList) && (ListOfLocalAtoms != NULL)) {
 | 
|---|
| 96 |     // free the index lookup list
 | 
|---|
| 97 |     delete[](ListOfLocalAtoms);
 | 
|---|
| 98 |   }
 | 
|---|
| 99 |   DoLog(1) && (Log() << Verbose(1) << "End of FillBondStructureFromReference." << endl);
 | 
|---|
| 100 |   return status;
 | 
|---|
| 101 | };
 | 
|---|
| 102 | 
 | 
|---|
| 103 | /** Checks for presence of bonds within atom list.
 | 
|---|
| 104 |  * TODO: more sophisticated check for bond structure (e.g. connected subgraph, ...)
 | 
|---|
| 105 |  * \return true - bonds present, false - no bonds
 | 
|---|
| 106 |  */
 | 
|---|
| 107 | bool molecule::hasBondStructure() const
 | 
|---|
| 108 | {
 | 
|---|
| 109 |   for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
 | 
|---|
| 110 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
| 111 |     if (!ListOfBonds.empty())
 | 
|---|
| 112 |       return true;
 | 
|---|
| 113 |   }
 | 
|---|
| 114 |   return false;
 | 
|---|
| 115 | }
 | 
|---|
| 116 | 
 | 
|---|
| 117 | /** Prints a list of all bonds to \a *out.
 | 
|---|
| 118 |  */
 | 
|---|
| 119 | void molecule::OutputBondsList() const
 | 
|---|
| 120 | {
 | 
|---|
| 121 |   DoLog(1) && (Log() << Verbose(1) << endl << "From contents of bond chain list:");
 | 
|---|
| 122 |   for(molecule::const_iterator AtomRunner = molecule::begin(); AtomRunner != molecule::end(); ++AtomRunner) {
 | 
|---|
| 123 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
| 124 |     for(BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
| 125 |         BondRunner != ListOfBonds.end();
 | 
|---|
| 126 |         ++BondRunner)
 | 
|---|
| 127 |       if ((*BondRunner)->leftatom == *AtomRunner) {
 | 
|---|
| 128 |         DoLog(0) && (Log() << Verbose(0) << *(*BondRunner) << "\t" << endl);
 | 
|---|
| 129 |       }
 | 
|---|
| 130 |   }
 | 
|---|
| 131 |   DoLog(0) && (Log() << Verbose(0) << endl);
 | 
|---|
| 132 | }
 | 
|---|
| 133 | ;
 | 
|---|
| 134 | 
 | 
|---|
| 135 | 
 | 
|---|
| 136 | /** Storing the bond structure of a molecule to file.
 | 
|---|
| 137 |  * Simply stores Atom::Nr and then the Atom::Nr of all bond partners per line.
 | 
|---|
| 138 |  * \param &filename name of file
 | 
|---|
| 139 |  * \param path path to file, defaults to empty
 | 
|---|
| 140 |  * \return true - file written successfully, false - writing failed
 | 
|---|
| 141 |  */
 | 
|---|
| 142 | bool molecule::StoreAdjacencyToFile(std::string filename, std::string path)
 | 
|---|
| 143 | {
 | 
|---|
| 144 |   ofstream AdjacencyFile;
 | 
|---|
| 145 |   string line;
 | 
|---|
| 146 |   bool status = true;
 | 
|---|
| 147 | 
 | 
|---|
| 148 |   if (path != "")
 | 
|---|
| 149 |     line = path + "/" + filename;
 | 
|---|
| 150 |   else
 | 
|---|
| 151 |     line = filename;
 | 
|---|
| 152 |   AdjacencyFile.open(line.c_str(), ios::out);
 | 
|---|
| 153 |   DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl);
 | 
|---|
| 154 |   if (AdjacencyFile.good()) {
 | 
|---|
| 155 |     AdjacencyFile << "m\tn" << endl;
 | 
|---|
| 156 |     for_each(atoms.begin(),atoms.end(),bind2nd(mem_fun(&atom::OutputAdjacency),&AdjacencyFile));
 | 
|---|
| 157 |     AdjacencyFile.close();
 | 
|---|
| 158 |     DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl);
 | 
|---|
| 159 |   } else {
 | 
|---|
| 160 |     DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line << "." << endl);
 | 
|---|
| 161 |     status = false;
 | 
|---|
| 162 |   }
 | 
|---|
| 163 | 
 | 
|---|
| 164 |   return status;
 | 
|---|
| 165 | }
 | 
|---|
| 166 | ;
 | 
|---|
| 167 | 
 | 
|---|
| 168 | /** Storing the bond structure of a molecule to file.
 | 
|---|
| 169 |  * Simply stores Atom::Nr and then the Atom::Nr of all bond partners, one per line.
 | 
|---|
| 170 |  * \param &filename name of file
 | 
|---|
| 171 |  * \param path path to file, defaults to empty
 | 
|---|
| 172 |  * \return true - file written successfully, false - writing failed
 | 
|---|
| 173 |  */
 | 
|---|
| 174 | bool molecule::StoreBondsToFile(std::string filename, std::string path)
 | 
|---|
| 175 | {
 | 
|---|
| 176 |   ofstream BondFile;
 | 
|---|
| 177 |   string line;
 | 
|---|
| 178 |   bool status = true;
 | 
|---|
| 179 | 
 | 
|---|
| 180 |   if (path != "")
 | 
|---|
| 181 |     line = path + "/" + filename;
 | 
|---|
| 182 |   else
 | 
|---|
| 183 |     line = filename;
 | 
|---|
| 184 |   BondFile.open(line.c_str(), ios::out);
 | 
|---|
| 185 |   DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl);
 | 
|---|
| 186 |   if (BondFile.good()) {
 | 
|---|
| 187 |     BondFile << "m\tn" << endl;
 | 
|---|
| 188 |     for_each(atoms.begin(),atoms.end(),bind2nd(mem_fun(&atom::OutputBonds),&BondFile));
 | 
|---|
| 189 |     BondFile.close();
 | 
|---|
| 190 |     DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl);
 | 
|---|
| 191 |   } else {
 | 
|---|
| 192 |     DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line << "." << endl);
 | 
|---|
| 193 |     status = false;
 | 
|---|
| 194 |   }
 | 
|---|
| 195 | 
 | 
|---|
| 196 |   return status;
 | 
|---|
| 197 | }
 | 
|---|
| 198 | ;
 | 
|---|
| 199 | 
 | 
|---|
| 200 | bool CheckAdjacencyFileAgainstMolecule_Init(std::string &path, ifstream &File, int *&CurrentBonds)
 | 
|---|
| 201 | {
 | 
|---|
| 202 |   string filename;
 | 
|---|
| 203 |   filename = path + ADJACENCYFILE;
 | 
|---|
| 204 |   File.open(filename.c_str(), ios::out);
 | 
|---|
| 205 |   DoLog(1) && (Log() << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... " << endl);
 | 
|---|
| 206 |   if (File.fail())
 | 
|---|
| 207 |     return false;
 | 
|---|
| 208 | 
 | 
|---|
| 209 |   // allocate storage structure
 | 
|---|
| 210 |   CurrentBonds = new int[MAXBONDS]; // contains parsed bonds of current atom
 | 
|---|
| 211 |   for(int i=0;i<MAXBONDS;i++)
 | 
|---|
| 212 |     CurrentBonds[i] = 0;
 | 
|---|
| 213 |   return true;
 | 
|---|
| 214 | }
 | 
|---|
| 215 | ;
 | 
|---|
| 216 | 
 | 
|---|
| 217 | void CheckAdjacencyFileAgainstMolecule_Finalize(ifstream &File, int *&CurrentBonds)
 | 
|---|
| 218 | {
 | 
|---|
| 219 |   File.close();
 | 
|---|
| 220 |   File.clear();
 | 
|---|
| 221 |   delete[](CurrentBonds);
 | 
|---|
| 222 | }
 | 
|---|
| 223 | ;
 | 
|---|
| 224 | 
 | 
|---|
| 225 | void CheckAdjacencyFileAgainstMolecule_CompareBonds(bool &status, int &NonMatchNumber, atom *&Walker, size_t &CurrentBondsOfAtom, int AtomNr, int *&CurrentBonds, atom **ListOfAtoms)
 | 
|---|
| 226 | {
 | 
|---|
| 227 |   size_t j = 0;
 | 
|---|
| 228 |   int id = -1;
 | 
|---|
| 229 | 
 | 
|---|
| 230 |   //Log() << Verbose(2) << "Walker is " << *Walker << ", bond partners: ";
 | 
|---|
| 231 |   const BondList& ListOfBonds = Walker->getListOfBonds();
 | 
|---|
| 232 |   if (CurrentBondsOfAtom == ListOfBonds.size()) {
 | 
|---|
| 233 |     for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
| 234 |         Runner != ListOfBonds.end();
 | 
|---|
| 235 |         ++Runner) {
 | 
|---|
| 236 |       id = (*Runner)->GetOtherAtom(Walker)->getNr();
 | 
|---|
| 237 |       j = 0;
 | 
|---|
| 238 |       for (; (j < CurrentBondsOfAtom) && (CurrentBonds[j++] != id);)
 | 
|---|
| 239 |         ; // check against all parsed bonds
 | 
|---|
| 240 |       if (CurrentBonds[j - 1] != id) { // no match ? Then mark in ListOfAtoms
 | 
|---|
| 241 |         ListOfAtoms[AtomNr] = NULL;
 | 
|---|
| 242 |         NonMatchNumber++;
 | 
|---|
| 243 |         status = false;
 | 
|---|
| 244 |         DoeLog(2) && (eLog() << Verbose(2) << id << " can not be found in list." << endl);
 | 
|---|
| 245 |       } else {
 | 
|---|
| 246 |         //Log() << Verbose(0) << "[" << id << "]\t";
 | 
|---|
| 247 |       }
 | 
|---|
| 248 |     }
 | 
|---|
| 249 |     //Log() << Verbose(0) << endl;
 | 
|---|
| 250 |   } else {
 | 
|---|
| 251 |     DoLog(0) && (Log() << Verbose(0) << "Number of bonds for Atom " << *Walker << " does not match, parsed " << CurrentBondsOfAtom << " against " << ListOfBonds.size() << "." << endl);
 | 
|---|
| 252 |     status = false;
 | 
|---|
| 253 |   }
 | 
|---|
| 254 | }
 | 
|---|
| 255 | ;
 | 
|---|
| 256 | 
 | 
|---|
| 257 | /** Checks contents of adjacency file against bond structure in structure molecule.
 | 
|---|
| 258 |  * \param *path path to file
 | 
|---|
| 259 |  * \param **ListOfAtoms allocated (molecule::AtomCount) and filled lookup table for ids (Atom::Nr) to *Atom
 | 
|---|
| 260 |  * \return true - structure is equal, false - not equivalence
 | 
|---|
| 261 |  */
 | 
|---|
| 262 | bool molecule::CheckAdjacencyFileAgainstMolecule(std::string &path, atom **ListOfAtoms)
 | 
|---|
| 263 | {
 | 
|---|
| 264 |   ifstream File;
 | 
|---|
| 265 |   bool status = true;
 | 
|---|
| 266 |   atom *Walker = NULL;
 | 
|---|
| 267 |   int *CurrentBonds = NULL;
 | 
|---|
| 268 |   int NonMatchNumber = 0; // will number of atoms with differing bond structure
 | 
|---|
| 269 |   size_t CurrentBondsOfAtom = -1;
 | 
|---|
| 270 |   const int AtomCount = getAtomCount();
 | 
|---|
| 271 | 
 | 
|---|
| 272 |   if (!CheckAdjacencyFileAgainstMolecule_Init(path, File, CurrentBonds)) {
 | 
|---|
| 273 |     DoLog(1) && (Log() << Verbose(1) << "Adjacency file not found." << endl);
 | 
|---|
| 274 |     return true;
 | 
|---|
| 275 |   }
 | 
|---|
| 276 | 
 | 
|---|
| 277 |   char buffer[MAXSTRINGSIZE];
 | 
|---|
| 278 |   int tmp;
 | 
|---|
| 279 |   // Parse the file line by line and count the bonds
 | 
|---|
| 280 |   while (!File.eof()) {
 | 
|---|
| 281 |     File.getline(buffer, MAXSTRINGSIZE);
 | 
|---|
| 282 |     stringstream line;
 | 
|---|
| 283 |     line.str(buffer);
 | 
|---|
| 284 |     int AtomNr = -1;
 | 
|---|
| 285 |     line >> AtomNr;
 | 
|---|
| 286 |     CurrentBondsOfAtom = -1; // we count one too far due to line end
 | 
|---|
| 287 |     // parse into structure
 | 
|---|
| 288 |     if ((AtomNr >= 0) && (AtomNr < AtomCount)) {
 | 
|---|
| 289 |       Walker = ListOfAtoms[AtomNr];
 | 
|---|
| 290 |       while (line >> ws >> tmp) {
 | 
|---|
| 291 |         std::cout << "Recognized bond partner " << tmp << std::endl;
 | 
|---|
| 292 |         CurrentBonds[++CurrentBondsOfAtom] = tmp;
 | 
|---|
| 293 |         ASSERT(CurrentBondsOfAtom < MAXBONDS,
 | 
|---|
| 294 |             "molecule::CheckAdjacencyFileAgainstMolecule() - encountered more bonds than allowed: "
 | 
|---|
| 295 |             +toString(CurrentBondsOfAtom)+" >= "+toString(MAXBONDS)+"!");
 | 
|---|
| 296 |       }
 | 
|---|
| 297 |       // compare against present bonds
 | 
|---|
| 298 |       CheckAdjacencyFileAgainstMolecule_CompareBonds(status, NonMatchNumber, Walker, CurrentBondsOfAtom, AtomNr, CurrentBonds, ListOfAtoms);
 | 
|---|
| 299 |     } else {
 | 
|---|
| 300 |       if (AtomNr != -1)
 | 
|---|
| 301 |         DoeLog(2) && (eLog() << Verbose(2) << AtomNr << " is not valid in the range of ids [" << 0 << "," << AtomCount << ")." << endl);
 | 
|---|
| 302 |     }
 | 
|---|
| 303 |   }
 | 
|---|
| 304 |   CheckAdjacencyFileAgainstMolecule_Finalize(File, CurrentBonds);
 | 
|---|
| 305 | 
 | 
|---|
| 306 |   if (status) { // if equal we parse the KeySetFile
 | 
|---|
| 307 |     DoLog(1) && (Log() << Verbose(1) << "done: Equal." << endl);
 | 
|---|
| 308 |   } else
 | 
|---|
| 309 |     DoLog(1) && (Log() << Verbose(1) << "done: Not equal by " << NonMatchNumber << " atoms." << endl);
 | 
|---|
| 310 |   return status;
 | 
|---|
| 311 | }
 | 
|---|
| 312 | ;
 | 
|---|
| 313 | 
 | 
|---|
| 314 | /** Adds a bond as a copy to a given one
 | 
|---|
| 315 |  * \param *left leftatom of new bond
 | 
|---|
| 316 |  * \param *right rightatom of new bond
 | 
|---|
| 317 |  * \param *CopyBond rest of fields in bond are copied from this
 | 
|---|
| 318 |  * \return pointer to new bond
 | 
|---|
| 319 |  */
 | 
|---|
| 320 | bond * molecule::CopyBond(atom *left, atom *right, bond *CopyBond)
 | 
|---|
| 321 | {
 | 
|---|
| 322 |   bond *Binder = AddBond(left, right, CopyBond->BondDegree);
 | 
|---|
| 323 |   Binder->Cyclic = CopyBond->Cyclic;
 | 
|---|
| 324 |   Binder->Type = CopyBond->Type;
 | 
|---|
| 325 |   return Binder;
 | 
|---|
| 326 | }
 | 
|---|
| 327 | ;
 | 
|---|
| 328 | 
 | 
|---|
| 329 | void BuildInducedSubgraph_Init(atom **&ParentList, int AtomCount)
 | 
|---|
| 330 | {
 | 
|---|
| 331 |   // reset parent list
 | 
|---|
| 332 |   ParentList = new atom*[AtomCount];
 | 
|---|
| 333 |   for (int i=0;i<AtomCount;i++)
 | 
|---|
| 334 |     ParentList[i] = NULL;
 | 
|---|
| 335 |   DoLog(3) && (Log() << Verbose(3) << "Resetting ParentList." << endl);
 | 
|---|
| 336 | }
 | 
|---|
| 337 | ;
 | 
|---|
| 338 | 
 | 
|---|
| 339 | void BuildInducedSubgraph_FillParentList(const molecule *mol, const molecule *Father, atom **&ParentList)
 | 
|---|
| 340 | {
 | 
|---|
| 341 |   // fill parent list with sons
 | 
|---|
| 342 |   DoLog(3) && (Log() << Verbose(3) << "Filling Parent List." << endl);
 | 
|---|
| 343 |   for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
 | 
|---|
| 344 |     ParentList[(*iter)->father->getNr()] = (*iter);
 | 
|---|
| 345 |     // Outputting List for debugging
 | 
|---|
| 346 |     DoLog(4) && (Log() << Verbose(4) << "Son[" << (*iter)->father->getNr() << "] of " << (*iter)->father << " is " << ParentList[(*iter)->father->getNr()] << "." << endl);
 | 
|---|
| 347 |   }
 | 
|---|
| 348 | };
 | 
|---|
| 349 | 
 | 
|---|
| 350 | void BuildInducedSubgraph_Finalize(atom **&ParentList)
 | 
|---|
| 351 | {
 | 
|---|
| 352 |   delete[](ParentList);
 | 
|---|
| 353 | }
 | 
|---|
| 354 | ;
 | 
|---|
| 355 | 
 | 
|---|
| 356 | bool BuildInducedSubgraph_CreateBondsFromParent(molecule *mol, const molecule *Father, atom **&ParentList)
 | 
|---|
| 357 | {
 | 
|---|
| 358 |   bool status = true;
 | 
|---|
| 359 |   atom *OtherAtom = NULL;
 | 
|---|
| 360 |   // check each entry of parent list and if ok (one-to-and-onto matching) create bonds
 | 
|---|
| 361 |   DoLog(3) && (Log() << Verbose(3) << "Creating bonds." << endl);
 | 
|---|
| 362 |   for (molecule::const_iterator iter = Father->begin(); iter != Father->end(); ++iter) {
 | 
|---|
| 363 |     if (ParentList[(*iter)->getNr()] != NULL) {
 | 
|---|
| 364 |       if (ParentList[(*iter)->getNr()]->father != (*iter)) {
 | 
|---|
| 365 |         status = false;
 | 
|---|
| 366 |       } else {
 | 
|---|
| 367 |         const BondList& ListOfBonds = (*iter)->getListOfBonds();
 | 
|---|
| 368 |         for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
| 369 |             Runner != ListOfBonds.end();
 | 
|---|
| 370 |             ++Runner) {
 | 
|---|
| 371 |           OtherAtom = (*Runner)->GetOtherAtom((*iter));
 | 
|---|
| 372 |           if (ParentList[OtherAtom->getNr()] != NULL) { // if otheratom is also a father of an atom on this molecule, create the bond
 | 
|---|
| 373 |             DoLog(4) && (Log() << Verbose(4) << "Endpoints of Bond " << (*Runner) << " are both present: " << ParentList[(*iter)->getNr()]->getName() << " and " << ParentList[OtherAtom->getNr()]->getName() << "." << endl);
 | 
|---|
| 374 |             mol->AddBond(ParentList[(*iter)->getNr()], ParentList[OtherAtom->getNr()], (*Runner)->BondDegree);
 | 
|---|
| 375 |           }
 | 
|---|
| 376 |         }
 | 
|---|
| 377 |       }
 | 
|---|
| 378 |     }
 | 
|---|
| 379 |   }
 | 
|---|
| 380 |   return status;
 | 
|---|
| 381 | }
 | 
|---|
| 382 | ;
 | 
|---|
| 383 | 
 | 
|---|
| 384 | /** Adds bond structure to this molecule from \a Father molecule.
 | 
|---|
| 385 |  * This basically causes this molecule to become an induced subgraph of the \a Father, i.e. for every bond in Father
 | 
|---|
| 386 |  * with end points present in this molecule, bond is created in this molecule.
 | 
|---|
| 387 |  * Special care was taken to ensure that this is of complexity O(N), where N is the \a Father's molecule::AtomCount.
 | 
|---|
| 388 |  * \param *Father father molecule
 | 
|---|
| 389 |  * \return true - is induced subgraph, false - there are atoms with fathers not in \a Father
 | 
|---|
| 390 |  * \todo not checked, not fully working probably
 | 
|---|
| 391 |  */
 | 
|---|
| 392 | bool molecule::BuildInducedSubgraph(const molecule *Father){
 | 
|---|
| 393 |   bool status = true;
 | 
|---|
| 394 |   atom **ParentList = NULL;
 | 
|---|
| 395 |   DoLog(2) && (Log() << Verbose(2) << "Begin of BuildInducedSubgraph." << endl);
 | 
|---|
| 396 |   BuildInducedSubgraph_Init(ParentList, Father->getAtomCount());
 | 
|---|
| 397 |   BuildInducedSubgraph_FillParentList(this, Father, ParentList);
 | 
|---|
| 398 |   status = BuildInducedSubgraph_CreateBondsFromParent(this, Father, ParentList);
 | 
|---|
| 399 |   BuildInducedSubgraph_Finalize(ParentList);
 | 
|---|
| 400 |   DoLog(2) && (Log() << Verbose(2) << "End of BuildInducedSubgraph." << endl);
 | 
|---|
| 401 |   return status;
 | 
|---|
| 402 | }
 | 
|---|
| 403 | ;
 | 
|---|
| 404 | 
 | 
|---|
| 405 | /** Fills a lookup list of father's Atom::nr -> atom for each subgraph.
 | 
|---|
| 406 |  * \param **&ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled
 | 
|---|
| 407 |  * \param GlobalAtomCount number of atoms in the complete molecule
 | 
|---|
| 408 |  * \return true - success, false - failure (ListOfLocalAtoms != NULL)
 | 
|---|
| 409 |  */
 | 
|---|
| 410 | bool molecule::FillListOfLocalAtoms(atom **&ListOfLocalAtoms, const int GlobalAtomCount)
 | 
|---|
| 411 | {
 | 
|---|
| 412 |   bool status = true;
 | 
|---|
| 413 | 
 | 
|---|
| 414 |   if (ListOfLocalAtoms == NULL) { // allocate and fill list of this fragment/subgraph
 | 
|---|
| 415 |     status = status && CreateFatherLookupTable(ListOfLocalAtoms, GlobalAtomCount);
 | 
|---|
| 416 |   } else
 | 
|---|
| 417 |     return false;
 | 
|---|
| 418 | 
 | 
|---|
| 419 |   return status;
 | 
|---|
| 420 | }
 | 
|---|
| 421 | 
 | 
|---|