| [bcf653] | 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 |  | 
|---|
| [cee0b57] | 8 | /* | 
|---|
|  | 9 | * molecule_graph.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Oct 5, 2009 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
| [aafd77] | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 21 |  | 
|---|
| [a564be] | 22 | #include <stack> | 
|---|
|  | 23 |  | 
|---|
| [f66195] | 24 | #include "atom.hpp" | 
|---|
| [129204] | 25 | #include "Bond/bond.hpp" | 
|---|
| [34c43a] | 26 | #include "Box.hpp" | 
|---|
|  | 27 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 28 | #include "CodePatterns/Info.hpp" | 
|---|
|  | 29 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 30 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [cee0b57] | 31 | #include "config.hpp" | 
|---|
| [49c059] | 32 | #include "Graph/DepthFirstSearchAnalysis.hpp" | 
|---|
| [f66195] | 33 | #include "element.hpp" | 
|---|
| [129204] | 34 | #include "Graph/BondGraph.hpp" | 
|---|
| [1d5afa5] | 35 | #include "Helpers/defs.hpp" | 
|---|
|  | 36 | #include "Helpers/fast_functions.hpp" | 
|---|
| [952f38] | 37 | #include "Helpers/helpers.hpp" | 
|---|
| [1d5afa5] | 38 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
| [b8b75d] | 39 | #include "linkedcell.hpp" | 
|---|
| [cee0b57] | 40 | #include "molecule.hpp" | 
|---|
| [34c43a] | 41 | #include "PointCloudAdaptor.hpp" | 
|---|
| [b34306] | 42 | #include "World.hpp" | 
|---|
| [9d83b6] | 43 | #include "WorldTime.hpp" | 
|---|
| [1d5afa5] | 44 |  | 
|---|
|  | 45 | #define MAXBONDS 8 | 
|---|
|  | 46 |  | 
|---|
| [cee0b57] | 47 |  | 
|---|
| [99752a] | 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 |  | 
|---|
| [e08c46] | 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 | */ | 
|---|
| [e4afb4] | 107 | bool molecule::hasBondStructure() const | 
|---|
| [e08c46] | 108 | { | 
|---|
| [9d83b6] | 109 | for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) { | 
|---|
|  | 110 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds(); | 
|---|
|  | 111 | if (!ListOfBonds.empty()) | 
|---|
| [e08c46] | 112 | return true; | 
|---|
| [9d83b6] | 113 | } | 
|---|
| [e08c46] | 114 | return false; | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
| [b8b75d] | 117 | /** Prints a list of all bonds to \a *out. | 
|---|
|  | 118 | */ | 
|---|
| [e138de] | 119 | void molecule::OutputBondsList() const | 
|---|
| [b8b75d] | 120 | { | 
|---|
| [a67d19] | 121 | DoLog(1) && (Log() << Verbose(1) << endl << "From contents of bond chain list:"); | 
|---|
| [9d83b6] | 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) | 
|---|
| [e08c46] | 127 | if ((*BondRunner)->leftatom == *AtomRunner) { | 
|---|
|  | 128 | DoLog(0) && (Log() << Verbose(0) << *(*BondRunner) << "\t" << endl); | 
|---|
|  | 129 | } | 
|---|
| [9d83b6] | 130 | } | 
|---|
| [a67d19] | 131 | DoLog(0) && (Log() << Verbose(0) << endl); | 
|---|
| [9eefda] | 132 | } | 
|---|
|  | 133 | ; | 
|---|
| [cee0b57] | 134 |  | 
|---|
|  | 135 |  | 
|---|
|  | 136 | /** Storing the bond structure of a molecule to file. | 
|---|
| [5309ba] | 137 | * Simply stores Atom::Nr and then the Atom::Nr of all bond partners per line. | 
|---|
| [35b698] | 138 | * \param &filename name of file | 
|---|
|  | 139 | * \param path path to file, defaults to empty | 
|---|
| [cee0b57] | 140 | * \return true - file written successfully, false - writing failed | 
|---|
|  | 141 | */ | 
|---|
| [e4afb4] | 142 | bool molecule::StoreAdjacencyToFile(std::string filename, std::string path) | 
|---|
| [cee0b57] | 143 | { | 
|---|
|  | 144 | ofstream AdjacencyFile; | 
|---|
| [35b698] | 145 | string line; | 
|---|
| [cee0b57] | 146 | bool status = true; | 
|---|
|  | 147 |  | 
|---|
| [35b698] | 148 | if (path != "") | 
|---|
|  | 149 | line = path + "/" + filename; | 
|---|
| [8ab0407] | 150 | else | 
|---|
| [35b698] | 151 | line = filename; | 
|---|
|  | 152 | AdjacencyFile.open(line.c_str(), ios::out); | 
|---|
| [acf800] | 153 | DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl); | 
|---|
| [35b698] | 154 | if (AdjacencyFile.good()) { | 
|---|
| [1f1b23] | 155 | AdjacencyFile << "m\tn" << endl; | 
|---|
| [00ef5c] | 156 | for_each(atoms.begin(),atoms.end(),bind2nd(mem_fun(&atom::OutputAdjacency),&AdjacencyFile)); | 
|---|
| [cee0b57] | 157 | AdjacencyFile.close(); | 
|---|
| [acf800] | 158 | DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl); | 
|---|
| [cee0b57] | 159 | } else { | 
|---|
| [35b698] | 160 | DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line << "." << endl); | 
|---|
| [cee0b57] | 161 | status = false; | 
|---|
|  | 162 | } | 
|---|
|  | 163 |  | 
|---|
|  | 164 | return status; | 
|---|
| [9eefda] | 165 | } | 
|---|
|  | 166 | ; | 
|---|
| [cee0b57] | 167 |  | 
|---|
| [1f1b23] | 168 | /** Storing the bond structure of a molecule to file. | 
|---|
| [5309ba] | 169 | * Simply stores Atom::Nr and then the Atom::Nr of all bond partners, one per line. | 
|---|
| [35b698] | 170 | * \param &filename name of file | 
|---|
|  | 171 | * \param path path to file, defaults to empty | 
|---|
| [1f1b23] | 172 | * \return true - file written successfully, false - writing failed | 
|---|
|  | 173 | */ | 
|---|
| [e4afb4] | 174 | bool molecule::StoreBondsToFile(std::string filename, std::string path) | 
|---|
| [1f1b23] | 175 | { | 
|---|
|  | 176 | ofstream BondFile; | 
|---|
| [35b698] | 177 | string line; | 
|---|
| [1f1b23] | 178 | bool status = true; | 
|---|
|  | 179 |  | 
|---|
| [35b698] | 180 | if (path != "") | 
|---|
|  | 181 | line = path + "/" + filename; | 
|---|
| [8ab0407] | 182 | else | 
|---|
| [35b698] | 183 | line = filename; | 
|---|
|  | 184 | BondFile.open(line.c_str(), ios::out); | 
|---|
| [acf800] | 185 | DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl); | 
|---|
| [35b698] | 186 | if (BondFile.good()) { | 
|---|
| [1f1b23] | 187 | BondFile << "m\tn" << endl; | 
|---|
| [00ef5c] | 188 | for_each(atoms.begin(),atoms.end(),bind2nd(mem_fun(&atom::OutputBonds),&BondFile)); | 
|---|
| [1f1b23] | 189 | BondFile.close(); | 
|---|
| [acf800] | 190 | DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl); | 
|---|
| [1f1b23] | 191 | } else { | 
|---|
| [35b698] | 192 | DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line << "." << endl); | 
|---|
| [1f1b23] | 193 | status = false; | 
|---|
|  | 194 | } | 
|---|
|  | 195 |  | 
|---|
|  | 196 | return status; | 
|---|
|  | 197 | } | 
|---|
|  | 198 | ; | 
|---|
|  | 199 |  | 
|---|
| [35b698] | 200 | bool CheckAdjacencyFileAgainstMolecule_Init(std::string &path, ifstream &File, int *&CurrentBonds) | 
|---|
| [ba4170] | 201 | { | 
|---|
| [35b698] | 202 | string filename; | 
|---|
|  | 203 | filename = path + ADJACENCYFILE; | 
|---|
|  | 204 | File.open(filename.c_str(), ios::out); | 
|---|
| [0de7e8] | 205 | DoLog(1) && (Log() << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... " << endl); | 
|---|
| [35b698] | 206 | if (File.fail()) | 
|---|
| [ba4170] | 207 | return false; | 
|---|
|  | 208 |  | 
|---|
|  | 209 | // allocate storage structure | 
|---|
| [1d5afa5] | 210 | CurrentBonds = new int[MAXBONDS]; // contains parsed bonds of current atom | 
|---|
|  | 211 | for(int i=0;i<MAXBONDS;i++) | 
|---|
| [920c70] | 212 | CurrentBonds[i] = 0; | 
|---|
| [ba4170] | 213 | return true; | 
|---|
| [9eefda] | 214 | } | 
|---|
|  | 215 | ; | 
|---|
| [ba4170] | 216 |  | 
|---|
| [e138de] | 217 | void CheckAdjacencyFileAgainstMolecule_Finalize(ifstream &File, int *&CurrentBonds) | 
|---|
| [ba4170] | 218 | { | 
|---|
|  | 219 | File.close(); | 
|---|
|  | 220 | File.clear(); | 
|---|
| [920c70] | 221 | delete[](CurrentBonds); | 
|---|
| [9eefda] | 222 | } | 
|---|
|  | 223 | ; | 
|---|
| [ba4170] | 224 |  | 
|---|
| [e138de] | 225 | void CheckAdjacencyFileAgainstMolecule_CompareBonds(bool &status, int &NonMatchNumber, atom *&Walker, size_t &CurrentBondsOfAtom, int AtomNr, int *&CurrentBonds, atom **ListOfAtoms) | 
|---|
| [ba4170] | 226 | { | 
|---|
|  | 227 | size_t j = 0; | 
|---|
|  | 228 | int id = -1; | 
|---|
|  | 229 |  | 
|---|
| [e138de] | 230 | //Log() << Verbose(2) << "Walker is " << *Walker << ", bond partners: "; | 
|---|
| [9d83b6] | 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) { | 
|---|
| [735b1c] | 236 | id = (*Runner)->GetOtherAtom(Walker)->getNr(); | 
|---|
| [ba4170] | 237 | j = 0; | 
|---|
| [9eefda] | 238 | for (; (j < CurrentBondsOfAtom) && (CurrentBonds[j++] != id);) | 
|---|
| [ba4170] | 239 | ; // check against all parsed bonds | 
|---|
| [9eefda] | 240 | if (CurrentBonds[j - 1] != id) { // no match ? Then mark in ListOfAtoms | 
|---|
| [ba4170] | 241 | ListOfAtoms[AtomNr] = NULL; | 
|---|
|  | 242 | NonMatchNumber++; | 
|---|
|  | 243 | status = false; | 
|---|
| [0de7e8] | 244 | DoeLog(2) && (eLog() << Verbose(2) << id << " can not be found in list." << endl); | 
|---|
| [ba4170] | 245 | } else { | 
|---|
| [0de7e8] | 246 | //Log() << Verbose(0) << "[" << id << "]\t"; | 
|---|
| [ba4170] | 247 | } | 
|---|
|  | 248 | } | 
|---|
| [e138de] | 249 | //Log() << Verbose(0) << endl; | 
|---|
| [ba4170] | 250 | } else { | 
|---|
| [9d83b6] | 251 | DoLog(0) && (Log() << Verbose(0) << "Number of bonds for Atom " << *Walker << " does not match, parsed " << CurrentBondsOfAtom << " against " << ListOfBonds.size() << "." << endl); | 
|---|
| [ba4170] | 252 | status = false; | 
|---|
|  | 253 | } | 
|---|
| [9eefda] | 254 | } | 
|---|
|  | 255 | ; | 
|---|
| [ba4170] | 256 |  | 
|---|
| [cee0b57] | 257 | /** Checks contents of adjacency file against bond structure in structure molecule. | 
|---|
|  | 258 | * \param *path path to file | 
|---|
| [5309ba] | 259 | * \param **ListOfAtoms allocated (molecule::AtomCount) and filled lookup table for ids (Atom::Nr) to *Atom | 
|---|
| [cee0b57] | 260 | * \return true - structure is equal, false - not equivalence | 
|---|
|  | 261 | */ | 
|---|
| [35b698] | 262 | bool molecule::CheckAdjacencyFileAgainstMolecule(std::string &path, atom **ListOfAtoms) | 
|---|
| [cee0b57] | 263 | { | 
|---|
|  | 264 | ifstream File; | 
|---|
|  | 265 | bool status = true; | 
|---|
| [266237] | 266 | atom *Walker = NULL; | 
|---|
| [ba4170] | 267 | int *CurrentBonds = NULL; | 
|---|
| [9eefda] | 268 | int NonMatchNumber = 0; // will number of atoms with differing bond structure | 
|---|
| [ba4170] | 269 | size_t CurrentBondsOfAtom = -1; | 
|---|
| [0de7e8] | 270 | const int AtomCount = getAtomCount(); | 
|---|
| [cee0b57] | 271 |  | 
|---|
| [e138de] | 272 | if (!CheckAdjacencyFileAgainstMolecule_Init(path, File, CurrentBonds)) { | 
|---|
| [a67d19] | 273 | DoLog(1) && (Log() << Verbose(1) << "Adjacency file not found." << endl); | 
|---|
| [ba4170] | 274 | return true; | 
|---|
|  | 275 | } | 
|---|
|  | 276 |  | 
|---|
| [920c70] | 277 | char buffer[MAXSTRINGSIZE]; | 
|---|
| [1d5afa5] | 278 | int tmp; | 
|---|
| [ba4170] | 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 | 
|---|
| [0de7e8] | 288 | if ((AtomNr >= 0) && (AtomNr < AtomCount)) { | 
|---|
| [ba4170] | 289 | Walker = ListOfAtoms[AtomNr]; | 
|---|
| [1d5afa5] | 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 | } | 
|---|
| [ba4170] | 297 | // compare against present bonds | 
|---|
| [e138de] | 298 | CheckAdjacencyFileAgainstMolecule_CompareBonds(status, NonMatchNumber, Walker, CurrentBondsOfAtom, AtomNr, CurrentBonds, ListOfAtoms); | 
|---|
| [0de7e8] | 299 | } else { | 
|---|
|  | 300 | if (AtomNr != -1) | 
|---|
|  | 301 | DoeLog(2) && (eLog() << Verbose(2) << AtomNr << " is not valid in the range of ids [" << 0 << "," << AtomCount << ")." << endl); | 
|---|
| [ba4170] | 302 | } | 
|---|
| [cee0b57] | 303 | } | 
|---|
| [e138de] | 304 | CheckAdjacencyFileAgainstMolecule_Finalize(File, CurrentBonds); | 
|---|
| [cee0b57] | 305 |  | 
|---|
| [ba4170] | 306 | if (status) { // if equal we parse the KeySetFile | 
|---|
| [a67d19] | 307 | DoLog(1) && (Log() << Verbose(1) << "done: Equal." << endl); | 
|---|
| [ba4170] | 308 | } else | 
|---|
| [a67d19] | 309 | DoLog(1) && (Log() << Verbose(1) << "done: Not equal by " << NonMatchNumber << " atoms." << endl); | 
|---|
| [cee0b57] | 310 | return status; | 
|---|
| [9eefda] | 311 | } | 
|---|
|  | 312 | ; | 
|---|
| [cee0b57] | 313 |  | 
|---|
| [266237] | 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; | 
|---|
| [9eefda] | 326 | } | 
|---|
|  | 327 | ; | 
|---|
| [266237] | 328 |  | 
|---|
| [e138de] | 329 | void BuildInducedSubgraph_Init(atom **&ParentList, int AtomCount) | 
|---|
| [cee0b57] | 330 | { | 
|---|
|  | 331 | // reset parent list | 
|---|
| [920c70] | 332 | ParentList = new atom*[AtomCount]; | 
|---|
|  | 333 | for (int i=0;i<AtomCount;i++) | 
|---|
|  | 334 | ParentList[i] = NULL; | 
|---|
| [a67d19] | 335 | DoLog(3) && (Log() << Verbose(3) << "Resetting ParentList." << endl); | 
|---|
| [9eefda] | 336 | } | 
|---|
|  | 337 | ; | 
|---|
| [cee0b57] | 338 |  | 
|---|
| [e138de] | 339 | void BuildInducedSubgraph_FillParentList(const molecule *mol, const molecule *Father, atom **&ParentList) | 
|---|
| [43587e] | 340 | { | 
|---|
| [cee0b57] | 341 | // fill parent list with sons | 
|---|
| [a67d19] | 342 | DoLog(3) && (Log() << Verbose(3) << "Filling Parent List." << endl); | 
|---|
| [9879f6] | 343 | for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) { | 
|---|
| [735b1c] | 344 | ParentList[(*iter)->father->getNr()] = (*iter); | 
|---|
| [cee0b57] | 345 | // Outputting List for debugging | 
|---|
| [735b1c] | 346 | DoLog(4) && (Log() << Verbose(4) << "Son[" << (*iter)->father->getNr() << "] of " << (*iter)->father << " is " << ParentList[(*iter)->father->getNr()] << "." << endl); | 
|---|
| [cee0b57] | 347 | } | 
|---|
| [a7b761b] | 348 | }; | 
|---|
| [43587e] | 349 |  | 
|---|
| [e138de] | 350 | void BuildInducedSubgraph_Finalize(atom **&ParentList) | 
|---|
| [43587e] | 351 | { | 
|---|
| [920c70] | 352 | delete[](ParentList); | 
|---|
| [9eefda] | 353 | } | 
|---|
|  | 354 | ; | 
|---|
| [43587e] | 355 |  | 
|---|
| [e138de] | 356 | bool BuildInducedSubgraph_CreateBondsFromParent(molecule *mol, const molecule *Father, atom **&ParentList) | 
|---|
| [43587e] | 357 | { | 
|---|
|  | 358 | bool status = true; | 
|---|
|  | 359 | atom *OtherAtom = NULL; | 
|---|
| [cee0b57] | 360 | // check each entry of parent list and if ok (one-to-and-onto matching) create bonds | 
|---|
| [a67d19] | 361 | DoLog(3) && (Log() << Verbose(3) << "Creating bonds." << endl); | 
|---|
| [9879f6] | 362 | for (molecule::const_iterator iter = Father->begin(); iter != Father->end(); ++iter) { | 
|---|
| [735b1c] | 363 | if (ParentList[(*iter)->getNr()] != NULL) { | 
|---|
|  | 364 | if (ParentList[(*iter)->getNr()]->father != (*iter)) { | 
|---|
| [cee0b57] | 365 | status = false; | 
|---|
|  | 366 | } else { | 
|---|
| [9d83b6] | 367 | const BondList& ListOfBonds = (*iter)->getListOfBonds(); | 
|---|
|  | 368 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
|  | 369 | Runner != ListOfBonds.end(); | 
|---|
|  | 370 | ++Runner) { | 
|---|
| [9879f6] | 371 | OtherAtom = (*Runner)->GetOtherAtom((*iter)); | 
|---|
| [735b1c] | 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); | 
|---|
| [cee0b57] | 375 | } | 
|---|
|  | 376 | } | 
|---|
|  | 377 | } | 
|---|
|  | 378 | } | 
|---|
|  | 379 | } | 
|---|
| [43587e] | 380 | return status; | 
|---|
| [9eefda] | 381 | } | 
|---|
|  | 382 | ; | 
|---|
| [cee0b57] | 383 |  | 
|---|
| [43587e] | 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 | */ | 
|---|
| [9d37ac] | 392 | bool molecule::BuildInducedSubgraph(const molecule *Father){ | 
|---|
| [43587e] | 393 | bool status = true; | 
|---|
|  | 394 | atom **ParentList = NULL; | 
|---|
| [a67d19] | 395 | DoLog(2) && (Log() << Verbose(2) << "Begin of BuildInducedSubgraph." << endl); | 
|---|
| [ea7176] | 396 | BuildInducedSubgraph_Init(ParentList, Father->getAtomCount()); | 
|---|
| [e138de] | 397 | BuildInducedSubgraph_FillParentList(this, Father, ParentList); | 
|---|
|  | 398 | status = BuildInducedSubgraph_CreateBondsFromParent(this, Father, ParentList); | 
|---|
|  | 399 | BuildInducedSubgraph_Finalize(ParentList); | 
|---|
| [a67d19] | 400 | DoLog(2) && (Log() << Verbose(2) << "End of BuildInducedSubgraph." << endl); | 
|---|
| [cee0b57] | 401 | return status; | 
|---|
| [9eefda] | 402 | } | 
|---|
|  | 403 | ; | 
|---|
| [cee0b57] | 404 |  | 
|---|
| [49c059] | 405 | /** Fills a lookup list of father's Atom::nr -> atom for each subgraph. | 
|---|
| [c6123b] | 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 |  | 
|---|