| [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 |  | 
|---|
| [6b919f8] | 8 | /* | 
|---|
|  | 9 | * atom_bondedparticle.cpp | 
|---|
|  | 10 | * | 
|---|
|  | 11 | *  Created on: Oct 19, 2009 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 21 |  | 
|---|
| [6b919f8] | 22 | #include "atom.hpp" | 
|---|
|  | 23 | #include "atom_bondedparticle.hpp" | 
|---|
|  | 24 | #include "bond.hpp" | 
|---|
| [d557374] | 25 | #include "CodePatterns/Assert.hpp" | 
|---|
| [ad011c] | 26 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 27 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [d557374] | 28 | #include "element.hpp" | 
|---|
|  | 29 | #include "lists.hpp" | 
|---|
| [6b919f8] | 30 |  | 
|---|
|  | 31 | /** Constructor of class BondedParticle. | 
|---|
|  | 32 | */ | 
|---|
| [70ff32] | 33 | BondedParticle::BondedParticle() | 
|---|
|  | 34 | { | 
|---|
| [9d83b6] | 35 | ListOfBonds.push_back(BondList()); | 
|---|
| [70ff32] | 36 | }; | 
|---|
| [6b919f8] | 37 |  | 
|---|
|  | 38 | /** Destructor of class BondedParticle. | 
|---|
|  | 39 | */ | 
|---|
|  | 40 | BondedParticle::~BondedParticle() | 
|---|
|  | 41 | { | 
|---|
| [9d83b6] | 42 | BondList::iterator Runner; | 
|---|
|  | 43 | for (std::vector<BondList>::iterator iter = ListOfBonds.begin(); | 
|---|
|  | 44 | !ListOfBonds.empty(); | 
|---|
|  | 45 | iter = ListOfBonds.begin()) { | 
|---|
|  | 46 | while (!(*iter).empty()) { | 
|---|
|  | 47 | Runner = (*iter).begin(); | 
|---|
|  | 48 | removewithoutcheck(*Runner); | 
|---|
|  | 49 | } | 
|---|
|  | 50 | ListOfBonds.erase(iter); | 
|---|
| [6b919f8] | 51 | } | 
|---|
|  | 52 | }; | 
|---|
|  | 53 |  | 
|---|
|  | 54 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file. | 
|---|
|  | 55 | * \param *file output stream | 
|---|
|  | 56 | */ | 
|---|
| [b453f9] | 57 | void BondedParticle::OutputOrder(ofstream *file) const | 
|---|
| [6b919f8] | 58 | { | 
|---|
|  | 59 | *file << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl; | 
|---|
| [e138de] | 60 | //Log() << Verbose(2) << "Storing: " << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl; | 
|---|
| [6b919f8] | 61 | }; | 
|---|
|  | 62 |  | 
|---|
|  | 63 | /** Prints all bonds of this atom with total degree. | 
|---|
|  | 64 | */ | 
|---|
| [e138de] | 65 | void BondedParticle::OutputBondOfAtom() const | 
|---|
| [6b919f8] | 66 | { | 
|---|
| [9d83b6] | 67 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [68f03d] | 68 | DoLog(4) && (Log() << Verbose(4) << "Atom " << getName() << "/" << nr << " with " << ListOfBonds.size() << " bonds: " << endl); | 
|---|
| [e138de] | 69 | int TotalDegree = 0; | 
|---|
|  | 70 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) { | 
|---|
| [a67d19] | 71 | DoLog(4) && (Log() << Verbose(4) << **Runner << endl); | 
|---|
| [e138de] | 72 | TotalDegree += (*Runner)->BondDegree; | 
|---|
|  | 73 | } | 
|---|
| [a67d19] | 74 | DoLog(4) && (Log() << Verbose(4) << " -- TotalDegree: " << TotalDegree << endl); | 
|---|
| [6b919f8] | 75 | }; | 
|---|
|  | 76 |  | 
|---|
|  | 77 | /** Output of atom::nr along with all bond partners. | 
|---|
|  | 78 | * \param *AdjacencyFile output stream | 
|---|
|  | 79 | */ | 
|---|
| [1f1b23] | 80 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const | 
|---|
| [6b919f8] | 81 | { | 
|---|
| [9d83b6] | 82 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [6b919f8] | 83 | *AdjacencyFile << nr << "\t"; | 
|---|
|  | 84 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) | 
|---|
|  | 85 | *AdjacencyFile << (*Runner)->GetOtherAtom(this)->nr << "\t"; | 
|---|
|  | 86 | *AdjacencyFile << endl; | 
|---|
|  | 87 | }; | 
|---|
|  | 88 |  | 
|---|
| [1f1b23] | 89 | /** Output of atom::nr along each bond partner per line. | 
|---|
|  | 90 | * Only bonds are printed where atom::nr is smaller than the one of the bond partner. | 
|---|
|  | 91 | * \param *AdjacencyFile output stream | 
|---|
|  | 92 | */ | 
|---|
|  | 93 | void BondedParticle::OutputBonds(ofstream * const BondFile) const | 
|---|
|  | 94 | { | 
|---|
| [9d83b6] | 95 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [1f1b23] | 96 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) | 
|---|
|  | 97 | if (nr < (*Runner)->GetOtherAtom(this)->nr) | 
|---|
|  | 98 | *BondFile << nr << "\t" << (*Runner)->GetOtherAtom(this)->nr << "\n"; | 
|---|
|  | 99 | }; | 
|---|
|  | 100 |  | 
|---|
| [b8d4a3] | 101 | /** | 
|---|
|  | 102 | * Adds a bond between this bonded particle and another. Does nothing if this | 
|---|
|  | 103 | * bond already exists. | 
|---|
|  | 104 | * | 
|---|
| [073a9e4] | 105 | * @param _step time step to access | 
|---|
| [b8d4a3] | 106 | * \param bonding partner | 
|---|
|  | 107 | */ | 
|---|
| [073a9e4] | 108 | void BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner) { | 
|---|
|  | 109 | if (IsBondedTo(_step, Partner)) { | 
|---|
| [b8d4a3] | 110 | return; | 
|---|
|  | 111 | } | 
|---|
|  | 112 |  | 
|---|
|  | 113 | bond* newBond = new bond((atom*) this, (atom*) Partner, 1, 0); | 
|---|
| [073a9e4] | 114 | RegisterBond(_step, newBond); | 
|---|
|  | 115 | Partner->RegisterBond(_step, newBond); | 
|---|
| [b8d4a3] | 116 | } | 
|---|
|  | 117 |  | 
|---|
| [6b919f8] | 118 | /** Puts a given bond into atom::ListOfBonds. | 
|---|
| [073a9e4] | 119 | * @param _step time step to access | 
|---|
| [6b919f8] | 120 | * \param *Binder bond to insert | 
|---|
|  | 121 | */ | 
|---|
| [073a9e4] | 122 | bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder) | 
|---|
| [6b919f8] | 123 | { | 
|---|
|  | 124 | bool status = false; | 
|---|
|  | 125 | if (Binder != NULL) { | 
|---|
|  | 126 | if (Binder->Contains(this)) { | 
|---|
| [d557374] | 127 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step); | 
|---|
| [073a9e4] | 128 | BondList& ListOfBonds = getListOfBondsAtStep(_step); | 
|---|
| [6b919f8] | 129 | ListOfBonds.push_back(Binder); | 
|---|
|  | 130 | status = true; | 
|---|
|  | 131 | } else { | 
|---|
| [58ed4a] | 132 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl); | 
|---|
| [6b919f8] | 133 | } | 
|---|
|  | 134 | } else { | 
|---|
| [58ed4a] | 135 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl); | 
|---|
| [6b919f8] | 136 | } | 
|---|
|  | 137 | return status; | 
|---|
|  | 138 | }; | 
|---|
|  | 139 |  | 
|---|
|  | 140 | /** Removes a given bond from atom::ListOfBonds. | 
|---|
| [9d83b6] | 141 | * @param _step time step to access | 
|---|
| [6b919f8] | 142 | * \param *Binder bond to remove | 
|---|
|  | 143 | */ | 
|---|
|  | 144 | bool BondedParticle::UnregisterBond(bond *Binder) | 
|---|
|  | 145 | { | 
|---|
|  | 146 | bool status = false; | 
|---|
| [d557374] | 147 | ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL."); | 
|---|
|  | 148 | const int step = ContainsBondAtStep(Binder); | 
|---|
|  | 149 | if (step != -1) { | 
|---|
|  | 150 | //LOG(3,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step); | 
|---|
|  | 151 | ListOfBonds[step].remove(Binder); | 
|---|
|  | 152 | status = true; | 
|---|
| [6b919f8] | 153 | } else { | 
|---|
| [d557374] | 154 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl); | 
|---|
| [6b919f8] | 155 | } | 
|---|
|  | 156 | return status; | 
|---|
|  | 157 | }; | 
|---|
|  | 158 |  | 
|---|
|  | 159 | /** Removes all bonds from atom::ListOfBonds. | 
|---|
|  | 160 | * \note Does not do any memory de-allocation. | 
|---|
|  | 161 | */ | 
|---|
| [a2bdbe] | 162 | void BondedParticle::UnregisterAllBond(const unsigned int _step) | 
|---|
| [6b919f8] | 163 | { | 
|---|
| [af897f] | 164 | ListOfBonds[_step].clear(); | 
|---|
|  | 165 | } | 
|---|
| [6b919f8] | 166 |  | 
|---|
| [583081] | 167 | /** Removes all bonds of given \a _step with freeing memory. | 
|---|
|  | 168 | * | 
|---|
|  | 169 | * @param _step time step whose bonds to free | 
|---|
|  | 170 | */ | 
|---|
|  | 171 | void BondedParticle::ClearBondsAtStep(const unsigned int _step) | 
|---|
|  | 172 | { | 
|---|
|  | 173 | //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]); | 
|---|
|  | 174 | for (BondList::iterator iter = (ListOfBonds[_step]).begin(); | 
|---|
|  | 175 | !(ListOfBonds[_step]).empty(); | 
|---|
|  | 176 | iter = (ListOfBonds[_step]).begin()) { | 
|---|
|  | 177 | //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds); | 
|---|
|  | 178 | delete((*iter)); // will also unregister with us and remove from list | 
|---|
|  | 179 | } | 
|---|
|  | 180 | } | 
|---|
|  | 181 |  | 
|---|
| [93c6e9] | 182 | /** Searches for the time step where the given bond \a *Binder is a bond of this particle. | 
|---|
|  | 183 | * | 
|---|
|  | 184 | * @param Binder bond to check | 
|---|
|  | 185 | * @return >=0 - first time step where bond appears, -1 - bond not present in lists | 
|---|
|  | 186 | */ | 
|---|
|  | 187 | int BondedParticle::ContainsBondAtStep(bond *Binder) | 
|---|
|  | 188 | { | 
|---|
|  | 189 | int step = -1; | 
|---|
|  | 190 | int tempstep = 0; | 
|---|
|  | 191 | for(std::vector<BondList>::const_iterator iter = ListOfBonds.begin(); | 
|---|
|  | 192 | iter != ListOfBonds.end(); | 
|---|
|  | 193 | ++iter,++tempstep) { | 
|---|
|  | 194 | for (BondList::const_iterator bonditer = iter->begin(); | 
|---|
|  | 195 | bonditer != iter->end(); | 
|---|
|  | 196 | ++bonditer) { | 
|---|
|  | 197 | if ((*bonditer) == Binder) { | 
|---|
|  | 198 | step = tempstep; | 
|---|
|  | 199 | break; | 
|---|
|  | 200 | } | 
|---|
|  | 201 | } | 
|---|
|  | 202 | if (step != -1) | 
|---|
|  | 203 | break; | 
|---|
|  | 204 | } | 
|---|
|  | 205 |  | 
|---|
|  | 206 | return step; | 
|---|
|  | 207 | } | 
|---|
|  | 208 |  | 
|---|
| [6b919f8] | 209 | /** Corrects the bond degree by one at most if necessary. | 
|---|
| [93c6e9] | 210 | * \return number of corrections done | 
|---|
| [6b919f8] | 211 | */ | 
|---|
| [e138de] | 212 | int BondedParticle::CorrectBondDegree() | 
|---|
| [6b919f8] | 213 | { | 
|---|
|  | 214 | int NoBonds = 0; | 
|---|
|  | 215 | int OtherNoBonds = 0; | 
|---|
|  | 216 | int FalseBondDegree = 0; | 
|---|
|  | 217 | atom *OtherWalker = NULL; | 
|---|
|  | 218 | bond *CandidateBond = NULL; | 
|---|
|  | 219 |  | 
|---|
|  | 220 | NoBonds = CountBonds(); | 
|---|
| [791138] | 221 | //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl; | 
|---|
| [83f176] | 222 | if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch | 
|---|
| [9d83b6] | 223 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [6b919f8] | 224 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) { | 
|---|
|  | 225 | OtherWalker = (*Runner)->GetOtherAtom(this); | 
|---|
|  | 226 | OtherNoBonds = OtherWalker->CountBonds(); | 
|---|
| [791138] | 227 | //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl; | 
|---|
| [83f176] | 228 | if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate | 
|---|
| [9d83b6] | 229 | const BondList& OtherListOfBonds = OtherWalker->getListOfBonds(); | 
|---|
|  | 230 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first | 
|---|
| [6b919f8] | 231 | CandidateBond = (*Runner); | 
|---|
| [e138de] | 232 | //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl; | 
|---|
| [6b919f8] | 233 | } | 
|---|
|  | 234 | } | 
|---|
|  | 235 | } | 
|---|
|  | 236 | if ((CandidateBond != NULL)) { | 
|---|
|  | 237 | CandidateBond->BondDegree++; | 
|---|
| [791138] | 238 | //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl; | 
|---|
| [6b919f8] | 239 | } else { | 
|---|
| [58ed4a] | 240 | DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl); | 
|---|
| [6b919f8] | 241 | FalseBondDegree++; | 
|---|
|  | 242 | } | 
|---|
|  | 243 | } | 
|---|
|  | 244 | return FalseBondDegree; | 
|---|
|  | 245 | }; | 
|---|
|  | 246 |  | 
|---|
| [c0d9eb] | 247 | /** Counts the number of bonds weighted by bond::BondDegree. | 
|---|
|  | 248 | * @param _step time step to access | 
|---|
|  | 249 | * \param bonds times bond::BondDegree | 
|---|
|  | 250 | */ | 
|---|
|  | 251 | int BondedParticle::CountBonds() const | 
|---|
|  | 252 | { | 
|---|
|  | 253 | int NoBonds = 0; | 
|---|
| [9d83b6] | 254 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [c0d9eb] | 255 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
|  | 256 | Runner != ListOfBonds.end(); | 
|---|
|  | 257 | (++Runner)) | 
|---|
|  | 258 | NoBonds += (*Runner)->BondDegree; | 
|---|
|  | 259 | return NoBonds; | 
|---|
|  | 260 | }; | 
|---|
|  | 261 |  | 
|---|
| [b70721] | 262 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner. | 
|---|
| [073a9e4] | 263 | * @param _step time step to access | 
|---|
| [b70721] | 264 | * \param *BondPartner atom to check for | 
|---|
|  | 265 | * \return true - bond exists, false - bond does not exist | 
|---|
|  | 266 | */ | 
|---|
| [073a9e4] | 267 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const | 
|---|
| [b70721] | 268 | { | 
|---|
|  | 269 | bool status = false; | 
|---|
|  | 270 |  | 
|---|
| [073a9e4] | 271 | const BondList& ListOfBonds = getListOfBondsAtStep(_step); | 
|---|
|  | 272 | for (BondList::const_iterator runner = ListOfBonds.begin(); | 
|---|
|  | 273 | runner != ListOfBonds.end(); | 
|---|
|  | 274 | runner++) { | 
|---|
| [b70721] | 275 | status = status || ((*runner)->Contains(BondPartner)); | 
|---|
|  | 276 | } | 
|---|
|  | 277 | return status; | 
|---|
|  | 278 | }; | 
|---|
|  | 279 |  | 
|---|
| [d74077] | 280 | std::ostream & BondedParticle::operator << (std::ostream &ost) const | 
|---|
|  | 281 | { | 
|---|
|  | 282 | ParticleInfo::operator<<(ost); | 
|---|
|  | 283 | ost << "," << getPosition(); | 
|---|
|  | 284 | return ost; | 
|---|
|  | 285 | } | 
|---|
|  | 286 |  | 
|---|
|  | 287 | std::ostream & operator << (std::ostream &ost, const BondedParticle &a) | 
|---|
|  | 288 | { | 
|---|
|  | 289 | a.ParticleInfo::operator<<(ost); | 
|---|
|  | 290 | ost << "," << a.getPosition(); | 
|---|
|  | 291 | return ost; | 
|---|
|  | 292 | } | 
|---|
|  | 293 |  | 
|---|