| 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 "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| 37 | #include "linkedcell.hpp"
 | 
|---|
| 38 | #include "molecule.hpp"
 | 
|---|
| 39 | #include "PointCloudAdaptor.hpp"
 | 
|---|
| 40 | #include "World.hpp"
 | 
|---|
| 41 | #include "WorldTime.hpp"
 | 
|---|
| 42 | 
 | 
|---|
| 43 | 
 | 
|---|
| 44 | /** Fills the bond structure of this chain list subgraphs that are derived from a complete \a *reference molecule.
 | 
|---|
| 45 |  * Calls this routine in each MoleculeLeafClass::next subgraph if it's not NULL.
 | 
|---|
| 46 |  * \param *reference reference molecule with the bond structure to be copied
 | 
|---|
| 47 |  * \param **&ListOfLocalAtoms Lookup table for this subgraph and index of each atom in \a *reference, may be NULL on start, then it is filled
 | 
|---|
| 48 |  * \param FreeList true - ***ListOfLocalAtoms is free'd before return, false - it is not
 | 
|---|
| 49 |  * \return true - success, false - failure
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | bool molecule::FillBondStructureFromReference(const molecule * const reference, atom **&ListOfLocalAtoms, bool FreeList)
 | 
|---|
| 52 | {
 | 
|---|
| 53 |   atom *OtherWalker = NULL;
 | 
|---|
| 54 |   atom *Father = NULL;
 | 
|---|
| 55 |   bool status = true;
 | 
|---|
| 56 |   int AtomNo;
 | 
|---|
| 57 | 
 | 
|---|
| 58 |   DoLog(1) && (Log() << Verbose(1) << "Begin of FillBondStructureFromReference." << endl);
 | 
|---|
| 59 |   // fill ListOfLocalAtoms if NULL was given
 | 
|---|
| 60 |   if (!FillListOfLocalAtoms(ListOfLocalAtoms, reference->getAtomCount())) {
 | 
|---|
| 61 |     DoLog(1) && (Log() << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl);
 | 
|---|
| 62 |     return false;
 | 
|---|
| 63 |   }
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   if (status) {
 | 
|---|
| 66 |     DoLog(1) && (Log() << Verbose(1) << "Creating adjacency list for molecule " << getName() << "." << endl);
 | 
|---|
| 67 |     // remove every bond from the list
 | 
|---|
| 68 |     for_each(begin(), end(),
 | 
|---|
| 69 |         boost::bind(&BondedParticle::ClearBondsAtStep,_1,WorldTime::getTime()));
 | 
|---|
| 70 | 
 | 
|---|
| 71 | 
 | 
|---|
| 72 |     for(molecule::const_iterator iter = begin(); iter != end(); ++iter) {
 | 
|---|
| 73 |       Father = (*iter)->GetTrueFather();
 | 
|---|
| 74 |       AtomNo = Father->getNr(); // global id of the current walker
 | 
|---|
| 75 |       const BondList& ListOfBonds = Father->getListOfBonds();
 | 
|---|
| 76 |       for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
| 77 |           Runner != ListOfBonds.end();
 | 
|---|
| 78 |           ++Runner) {
 | 
|---|
| 79 |         OtherWalker = ListOfLocalAtoms[(*Runner)->GetOtherAtom((*iter)->GetTrueFather())->getNr()]; // local copy of current bond partner of walker
 | 
|---|
| 80 |         if (OtherWalker != NULL) {
 | 
|---|
| 81 |           if (OtherWalker->getNr() > (*iter)->getNr())
 | 
|---|
| 82 |             AddBond((*iter), OtherWalker, (*Runner)->BondDegree);
 | 
|---|
| 83 |         } else {
 | 
|---|
| 84 |           DoLog(1) && (Log() << Verbose(1) << "OtherWalker = ListOfLocalAtoms[" << (*Runner)->GetOtherAtom((*iter)->GetTrueFather())->getNr() << "] is NULL!" << endl);
 | 
|---|
| 85 |           status = false;
 | 
|---|
| 86 |         }
 | 
|---|
| 87 |       }
 | 
|---|
| 88 |     }
 | 
|---|
| 89 |   }
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   if ((FreeList) && (ListOfLocalAtoms != NULL)) {
 | 
|---|
| 92 |     // free the index lookup list
 | 
|---|
| 93 |     delete[](ListOfLocalAtoms);
 | 
|---|
| 94 |   }
 | 
|---|
| 95 |   DoLog(1) && (Log() << Verbose(1) << "End of FillBondStructureFromReference." << endl);
 | 
|---|
| 96 |   return status;
 | 
|---|
| 97 | };
 | 
|---|
| 98 | 
 | 
|---|
| 99 | /** Checks for presence of bonds within atom list.
 | 
|---|
| 100 |  * TODO: more sophisticated check for bond structure (e.g. connected subgraph, ...)
 | 
|---|
| 101 |  * \return true - bonds present, false - no bonds
 | 
|---|
| 102 |  */
 | 
|---|
| 103 | bool molecule::hasBondStructure() const
 | 
|---|
| 104 | {
 | 
|---|
| 105 |   for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
 | 
|---|
| 106 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
| 107 |     if (!ListOfBonds.empty())
 | 
|---|
| 108 |       return true;
 | 
|---|
| 109 |   }
 | 
|---|
| 110 |   return false;
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | /** Prints a list of all bonds to \a *out.
 | 
|---|
| 114 |  */
 | 
|---|
| 115 | void molecule::OutputBondsList() const
 | 
|---|
| 116 | {
 | 
|---|
| 117 |   DoLog(1) && (Log() << Verbose(1) << endl << "From contents of bond chain list:");
 | 
|---|
| 118 |   for(molecule::const_iterator AtomRunner = molecule::begin(); AtomRunner != molecule::end(); ++AtomRunner) {
 | 
|---|
| 119 |     const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
 | 
|---|
| 120 |     for(BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
| 121 |         BondRunner != ListOfBonds.end();
 | 
|---|
| 122 |         ++BondRunner)
 | 
|---|
| 123 |       if ((*BondRunner)->leftatom == *AtomRunner) {
 | 
|---|
| 124 |         DoLog(0) && (Log() << Verbose(0) << *(*BondRunner) << "\t" << endl);
 | 
|---|
| 125 |       }
 | 
|---|
| 126 |   }
 | 
|---|
| 127 |   DoLog(0) && (Log() << Verbose(0) << endl);
 | 
|---|
| 128 | }
 | 
|---|
| 129 | ;
 | 
|---|
| 130 | 
 | 
|---|
| 131 | 
 | 
|---|
| 132 | /** Storing the bond structure of a molecule to file.
 | 
|---|
| 133 |  * Simply stores Atom::Nr and then the Atom::Nr of all bond partners per line.
 | 
|---|
| 134 |  * \param &filename name of file
 | 
|---|
| 135 |  * \param path path to file, defaults to empty
 | 
|---|
| 136 |  * \return true - file written successfully, false - writing failed
 | 
|---|
| 137 |  */
 | 
|---|
| 138 | bool molecule::StoreAdjacencyToFile(std::string filename, std::string path)
 | 
|---|
| 139 | {
 | 
|---|
| 140 |   ofstream AdjacencyFile;
 | 
|---|
| 141 |   string line;
 | 
|---|
| 142 |   bool status = true;
 | 
|---|
| 143 | 
 | 
|---|
| 144 |   if (path != "")
 | 
|---|
| 145 |     line = path + "/" + filename;
 | 
|---|
| 146 |   else
 | 
|---|
| 147 |     line = filename;
 | 
|---|
| 148 |   AdjacencyFile.open(line.c_str(), ios::out);
 | 
|---|
| 149 |   DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl);
 | 
|---|
| 150 |   if (AdjacencyFile.good()) {
 | 
|---|
| 151 |     AdjacencyFile << "m\tn" << endl;
 | 
|---|
| 152 |     for_each(atoms.begin(),atoms.end(),bind2nd(mem_fun(&atom::OutputAdjacency),&AdjacencyFile));
 | 
|---|
| 153 |     AdjacencyFile.close();
 | 
|---|
| 154 |     DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl);
 | 
|---|
| 155 |   } else {
 | 
|---|
| 156 |     DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line << "." << endl);
 | 
|---|
| 157 |     status = false;
 | 
|---|
| 158 |   }
 | 
|---|
| 159 | 
 | 
|---|
| 160 |   return status;
 | 
|---|
| 161 | }
 | 
|---|
| 162 | ;
 | 
|---|
| 163 | 
 | 
|---|
| 164 | /** Storing the bond structure of a molecule to file.
 | 
|---|
| 165 |  * Simply stores Atom::Nr and then the Atom::Nr of all bond partners, one per line.
 | 
|---|
| 166 |  * \param &filename name of file
 | 
|---|
| 167 |  * \param path path to file, defaults to empty
 | 
|---|
| 168 |  * \return true - file written successfully, false - writing failed
 | 
|---|
| 169 |  */
 | 
|---|
| 170 | bool molecule::StoreBondsToFile(std::string filename, std::string path)
 | 
|---|
| 171 | {
 | 
|---|
| 172 |   ofstream BondFile;
 | 
|---|
| 173 |   string line;
 | 
|---|
| 174 |   bool status = true;
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   if (path != "")
 | 
|---|
| 177 |     line = path + "/" + filename;
 | 
|---|
| 178 |   else
 | 
|---|
| 179 |     line = filename;
 | 
|---|
| 180 |   BondFile.open(line.c_str(), ios::out);
 | 
|---|
| 181 |   DoLog(1) && (Log() << Verbose(1) << "Saving adjacency list ... " << endl);
 | 
|---|
| 182 |   if (BondFile.good()) {
 | 
|---|
| 183 |     BondFile << "m\tn" << endl;
 | 
|---|
| 184 |     for_each(atoms.begin(),atoms.end(),bind2nd(mem_fun(&atom::OutputBonds),&BondFile));
 | 
|---|
| 185 |     BondFile.close();
 | 
|---|
| 186 |     DoLog(1) && (Log() << Verbose(1) << "\t... done." << endl);
 | 
|---|
| 187 |   } else {
 | 
|---|
| 188 |     DoLog(1) && (Log() << Verbose(1) << "\t... failed to open file " << line << "." << endl);
 | 
|---|
| 189 |     status = false;
 | 
|---|
| 190 |   }
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   return status;
 | 
|---|
| 193 | }
 | 
|---|
| 194 | ;
 | 
|---|
| 195 | 
 | 
|---|
| 196 | /** Adds a bond as a copy to a given one
 | 
|---|
| 197 |  * \param *left leftatom of new bond
 | 
|---|
| 198 |  * \param *right rightatom of new bond
 | 
|---|
| 199 |  * \param *CopyBond rest of fields in bond are copied from this
 | 
|---|
| 200 |  * \return pointer to new bond
 | 
|---|
| 201 |  */
 | 
|---|
| 202 | bond * molecule::CopyBond(atom *left, atom *right, bond *CopyBond)
 | 
|---|
| 203 | {
 | 
|---|
| 204 |   bond *Binder = AddBond(left, right, CopyBond->BondDegree);
 | 
|---|
| 205 |   Binder->Cyclic = CopyBond->Cyclic;
 | 
|---|
| 206 |   Binder->Type = CopyBond->Type;
 | 
|---|
| 207 |   return Binder;
 | 
|---|
| 208 | }
 | 
|---|
| 209 | ;
 | 
|---|
| 210 | 
 | 
|---|
| 211 | /** Fills a lookup list of father's Atom::nr -> atom for each subgraph.
 | 
|---|
| 212 |  * \param **&ListOfLocalAtoms Lookup table for each subgraph and index of each atom in global molecule, may be NULL on start, then it is filled
 | 
|---|
| 213 |  * \param GlobalAtomCount number of atoms in the complete molecule
 | 
|---|
| 214 |  * \return true - success, false - failure (ListOfLocalAtoms != NULL)
 | 
|---|
| 215 |  */
 | 
|---|
| 216 | bool molecule::FillListOfLocalAtoms(atom **&ListOfLocalAtoms, const int GlobalAtomCount)
 | 
|---|
| 217 | {
 | 
|---|
| 218 |   bool status = true;
 | 
|---|
| 219 | 
 | 
|---|
| 220 |   if (ListOfLocalAtoms == NULL) { // allocate and fill list of this fragment/subgraph
 | 
|---|
| 221 |     status = status && CreateFatherLookupTable(ListOfLocalAtoms, GlobalAtomCount);
 | 
|---|
| 222 |   } else
 | 
|---|
| 223 |     return false;
 | 
|---|
| 224 | 
 | 
|---|
| 225 |   return status;
 | 
|---|
| 226 | }
 | 
|---|
| 227 | 
 | 
|---|