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