| [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" | 
|---|
| [129204] | 24 | #include "Bond/bond.hpp" | 
|---|
| [d557374] | 25 | #include "CodePatterns/Assert.hpp" | 
|---|
| [ad011c] | 26 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 27 | #include "CodePatterns/Verbose.hpp" | 
|---|
| [3bdb6d] | 28 | #include "Element/element.hpp" | 
|---|
| [db7e6d] | 29 | #include "WorldTime.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 | { | 
|---|
| [5e2f80] | 42 | removeAllBonds(); | 
|---|
| [6b919f8] | 43 | }; | 
|---|
|  | 44 |  | 
|---|
|  | 45 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file. | 
|---|
|  | 46 | * \param *file output stream | 
|---|
|  | 47 | */ | 
|---|
| [b453f9] | 48 | void BondedParticle::OutputOrder(ofstream *file) const | 
|---|
| [6b919f8] | 49 | { | 
|---|
| [735b1c] | 50 | *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl; | 
|---|
|  | 51 | //Log() << Verbose(2) << "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl; | 
|---|
| [6b919f8] | 52 | }; | 
|---|
|  | 53 |  | 
|---|
|  | 54 | /** Prints all bonds of this atom with total degree. | 
|---|
|  | 55 | */ | 
|---|
| [4b5cf8] | 56 | void BondedParticle::OutputBondOfAtom(std::ostream &ost) const | 
|---|
| [6b919f8] | 57 | { | 
|---|
| [9d83b6] | 58 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [4b5cf8] | 59 | ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: "; | 
|---|
| [e138de] | 60 | int TotalDegree = 0; | 
|---|
|  | 61 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) { | 
|---|
| [4b5cf8] | 62 | ost << **Runner << "\t"; | 
|---|
| [e138de] | 63 | TotalDegree += (*Runner)->BondDegree; | 
|---|
|  | 64 | } | 
|---|
| [4b5cf8] | 65 | ost << " -- TotalDegree: " << TotalDegree; | 
|---|
| [6b919f8] | 66 | }; | 
|---|
|  | 67 |  | 
|---|
| [5309ba] | 68 | /** Output of atom::Nr along with all bond partners. | 
|---|
| [6b919f8] | 69 | * \param *AdjacencyFile output stream | 
|---|
|  | 70 | */ | 
|---|
| [1f1b23] | 71 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const | 
|---|
| [6b919f8] | 72 | { | 
|---|
| [9d83b6] | 73 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [735b1c] | 74 | *AdjacencyFile << getNr() << "\t"; | 
|---|
| [6b919f8] | 75 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) | 
|---|
| [735b1c] | 76 | *AdjacencyFile << (*Runner)->GetOtherAtom(this)->getNr() << "\t"; | 
|---|
| [6b919f8] | 77 | *AdjacencyFile << endl; | 
|---|
|  | 78 | }; | 
|---|
|  | 79 |  | 
|---|
| [5309ba] | 80 | /** Output of atom::Nr along each bond partner per line. | 
|---|
|  | 81 | * Only bonds are printed where atom::Nr is smaller than the one of the bond partner. | 
|---|
| [1f1b23] | 82 | * \param *AdjacencyFile output stream | 
|---|
|  | 83 | */ | 
|---|
|  | 84 | void BondedParticle::OutputBonds(ofstream * const BondFile) const | 
|---|
|  | 85 | { | 
|---|
| [9d83b6] | 86 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [1f1b23] | 87 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) | 
|---|
| [735b1c] | 88 | if (getNr() < (*Runner)->GetOtherAtom(this)->getNr()) | 
|---|
|  | 89 | *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n"; | 
|---|
| [1f1b23] | 90 | }; | 
|---|
|  | 91 |  | 
|---|
| [b8d4a3] | 92 | /** | 
|---|
| [db7e6d] | 93 | * Adds a bond between this bonded particle and another. Returns present instance if this | 
|---|
| [b8d4a3] | 94 | * bond already exists. | 
|---|
|  | 95 | * | 
|---|
| [073a9e4] | 96 | * @param _step time step to access | 
|---|
| [db7e6d] | 97 | * @param bonding partner | 
|---|
|  | 98 | * @return const reference to created bond or to already present bonds | 
|---|
| [b8d4a3] | 99 | */ | 
|---|
| [db7e6d] | 100 | const bond * BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner) | 
|---|
|  | 101 | { | 
|---|
|  | 102 | const BondList &bondlist = getListOfBondsAtStep(_step); | 
|---|
|  | 103 | for (BondList::const_iterator runner = bondlist.begin(); | 
|---|
|  | 104 | runner != bondlist.end(); | 
|---|
|  | 105 | runner++) { | 
|---|
|  | 106 | if ((*runner)->Contains(Partner)) | 
|---|
|  | 107 | return *runner; | 
|---|
| [b8d4a3] | 108 | } | 
|---|
|  | 109 |  | 
|---|
| [efe516] | 110 | bond* newBond = new bond((atom*) this, (atom*) Partner, 1); | 
|---|
| [073a9e4] | 111 | RegisterBond(_step, newBond); | 
|---|
|  | 112 | Partner->RegisterBond(_step, newBond); | 
|---|
| [db7e6d] | 113 |  | 
|---|
|  | 114 | return newBond; | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
|  | 117 | /** Removes a bond for this atom. | 
|---|
|  | 118 | * | 
|---|
|  | 119 | * @param Binder bond to remove | 
|---|
|  | 120 | */ | 
|---|
|  | 121 | void BondedParticle::removeBond(bond * binder) | 
|---|
|  | 122 | { | 
|---|
|  | 123 | UnregisterBond(binder); | 
|---|
| [b8d4a3] | 124 | } | 
|---|
|  | 125 |  | 
|---|
| [5e2f80] | 126 | /** Removes all bonds and their instances, too. | 
|---|
|  | 127 | * | 
|---|
|  | 128 | */ | 
|---|
|  | 129 | void BondedParticle::removeAllBonds() | 
|---|
|  | 130 | { | 
|---|
|  | 131 | OBSERVE; | 
|---|
|  | 132 | NOTIFY(BondedParticle::BondsChanged); | 
|---|
|  | 133 | for (size_t index = 0; index < ListOfBonds.size(); ++index) | 
|---|
|  | 134 | { | 
|---|
|  | 135 | for (BondList::iterator iter = ListOfBonds[index].begin(); | 
|---|
|  | 136 | !ListOfBonds[index].empty(); | 
|---|
|  | 137 | iter = ListOfBonds[index].begin()) { | 
|---|
|  | 138 | delete (*iter); | 
|---|
|  | 139 | // erase is done by bond::~bond() | 
|---|
|  | 140 | } | 
|---|
|  | 141 | } | 
|---|
|  | 142 | } | 
|---|
|  | 143 |  | 
|---|
| [6b919f8] | 144 | /** Puts a given bond into atom::ListOfBonds. | 
|---|
| [073a9e4] | 145 | * @param _step time step to access | 
|---|
| [6b919f8] | 146 | * \param *Binder bond to insert | 
|---|
|  | 147 | */ | 
|---|
| [073a9e4] | 148 | bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder) | 
|---|
| [6b919f8] | 149 | { | 
|---|
| [db7e6d] | 150 | OBSERVE; | 
|---|
| [6b919f8] | 151 | bool status = false; | 
|---|
|  | 152 | if (Binder != NULL) { | 
|---|
|  | 153 | if (Binder->Contains(this)) { | 
|---|
| [db7e6d] | 154 | if (WorldTime::getTime() == _step) | 
|---|
|  | 155 | NOTIFY(AtomObservable::BondsChanged); | 
|---|
| [d557374] | 156 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step); | 
|---|
| [5e2f80] | 157 | if (ListOfBonds.size() <= _step) | 
|---|
|  | 158 | ListOfBonds.resize(_step+1); | 
|---|
|  | 159 | ListOfBonds[_step].push_back(Binder); | 
|---|
| [6b919f8] | 160 | status = true; | 
|---|
|  | 161 | } else { | 
|---|
| [58ed4a] | 162 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl); | 
|---|
| [6b919f8] | 163 | } | 
|---|
|  | 164 | } else { | 
|---|
| [58ed4a] | 165 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl); | 
|---|
| [6b919f8] | 166 | } | 
|---|
|  | 167 | return status; | 
|---|
|  | 168 | }; | 
|---|
|  | 169 |  | 
|---|
|  | 170 | /** Removes a given bond from atom::ListOfBonds. | 
|---|
| [9d83b6] | 171 | * @param _step time step to access | 
|---|
| [6b919f8] | 172 | * \param *Binder bond to remove | 
|---|
|  | 173 | */ | 
|---|
|  | 174 | bool BondedParticle::UnregisterBond(bond *Binder) | 
|---|
|  | 175 | { | 
|---|
| [db7e6d] | 176 | OBSERVE; | 
|---|
| [6b919f8] | 177 | bool status = false; | 
|---|
| [d557374] | 178 | ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL."); | 
|---|
|  | 179 | const int step = ContainsBondAtStep(Binder); | 
|---|
|  | 180 | if (step != -1) { | 
|---|
| [db7e6d] | 181 | NOTIFY(AtomObservable::BondsChanged); | 
|---|
| [5e2f80] | 182 | //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step); | 
|---|
| [d557374] | 183 | ListOfBonds[step].remove(Binder); | 
|---|
|  | 184 | status = true; | 
|---|
| [6b919f8] | 185 | } else { | 
|---|
| [d557374] | 186 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl); | 
|---|
| [6b919f8] | 187 | } | 
|---|
|  | 188 | return status; | 
|---|
|  | 189 | }; | 
|---|
|  | 190 |  | 
|---|
|  | 191 | /** Removes all bonds from atom::ListOfBonds. | 
|---|
|  | 192 | * \note Does not do any memory de-allocation. | 
|---|
|  | 193 | */ | 
|---|
| [a2bdbe] | 194 | void BondedParticle::UnregisterAllBond(const unsigned int _step) | 
|---|
| [6b919f8] | 195 | { | 
|---|
| [af897f] | 196 | ListOfBonds[_step].clear(); | 
|---|
|  | 197 | } | 
|---|
| [6b919f8] | 198 |  | 
|---|
| [583081] | 199 | /** Removes all bonds of given \a _step with freeing memory. | 
|---|
|  | 200 | * | 
|---|
|  | 201 | * @param _step time step whose bonds to free | 
|---|
|  | 202 | */ | 
|---|
|  | 203 | void BondedParticle::ClearBondsAtStep(const unsigned int _step) | 
|---|
|  | 204 | { | 
|---|
| [db7e6d] | 205 | OBSERVE; | 
|---|
|  | 206 | if (WorldTime::getTime() == _step) | 
|---|
|  | 207 | NOTIFY(AtomObservable::BondsChanged); | 
|---|
| [583081] | 208 | //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]); | 
|---|
|  | 209 | for (BondList::iterator iter = (ListOfBonds[_step]).begin(); | 
|---|
|  | 210 | !(ListOfBonds[_step]).empty(); | 
|---|
|  | 211 | iter = (ListOfBonds[_step]).begin()) { | 
|---|
|  | 212 | //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds); | 
|---|
|  | 213 | delete((*iter)); // will also unregister with us and remove from list | 
|---|
|  | 214 | } | 
|---|
|  | 215 | } | 
|---|
|  | 216 |  | 
|---|
| [93c6e9] | 217 | /** Searches for the time step where the given bond \a *Binder is a bond of this particle. | 
|---|
|  | 218 | * | 
|---|
|  | 219 | * @param Binder bond to check | 
|---|
|  | 220 | * @return >=0 - first time step where bond appears, -1 - bond not present in lists | 
|---|
|  | 221 | */ | 
|---|
| [db7e6d] | 222 | int BondedParticle::ContainsBondAtStep(bond *Binder) const | 
|---|
| [93c6e9] | 223 | { | 
|---|
|  | 224 | int step = -1; | 
|---|
|  | 225 | int tempstep = 0; | 
|---|
|  | 226 | for(std::vector<BondList>::const_iterator iter = ListOfBonds.begin(); | 
|---|
|  | 227 | iter != ListOfBonds.end(); | 
|---|
|  | 228 | ++iter,++tempstep) { | 
|---|
|  | 229 | for (BondList::const_iterator bonditer = iter->begin(); | 
|---|
|  | 230 | bonditer != iter->end(); | 
|---|
|  | 231 | ++bonditer) { | 
|---|
|  | 232 | if ((*bonditer) == Binder) { | 
|---|
|  | 233 | step = tempstep; | 
|---|
|  | 234 | break; | 
|---|
|  | 235 | } | 
|---|
|  | 236 | } | 
|---|
|  | 237 | if (step != -1) | 
|---|
|  | 238 | break; | 
|---|
|  | 239 | } | 
|---|
|  | 240 |  | 
|---|
|  | 241 | return step; | 
|---|
|  | 242 | } | 
|---|
|  | 243 |  | 
|---|
| [6b919f8] | 244 | /** Corrects the bond degree by one at most if necessary. | 
|---|
| [93c6e9] | 245 | * \return number of corrections done | 
|---|
| [6b919f8] | 246 | */ | 
|---|
| [e138de] | 247 | int BondedParticle::CorrectBondDegree() | 
|---|
| [6b919f8] | 248 | { | 
|---|
| [db7e6d] | 249 | OBSERVE; | 
|---|
|  | 250 | NOTIFY(AtomObservable::BondsChanged); | 
|---|
| [6b919f8] | 251 | int NoBonds = 0; | 
|---|
|  | 252 | int OtherNoBonds = 0; | 
|---|
|  | 253 | int FalseBondDegree = 0; | 
|---|
|  | 254 | atom *OtherWalker = NULL; | 
|---|
|  | 255 | bond *CandidateBond = NULL; | 
|---|
|  | 256 |  | 
|---|
|  | 257 | NoBonds = CountBonds(); | 
|---|
| [791138] | 258 | //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl; | 
|---|
| [83f176] | 259 | if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch | 
|---|
| [9d83b6] | 260 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [6b919f8] | 261 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) { | 
|---|
|  | 262 | OtherWalker = (*Runner)->GetOtherAtom(this); | 
|---|
|  | 263 | OtherNoBonds = OtherWalker->CountBonds(); | 
|---|
| [791138] | 264 | //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl; | 
|---|
| [83f176] | 265 | if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate | 
|---|
| [9d83b6] | 266 | const BondList& OtherListOfBonds = OtherWalker->getListOfBonds(); | 
|---|
|  | 267 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first | 
|---|
| [6b919f8] | 268 | CandidateBond = (*Runner); | 
|---|
| [e138de] | 269 | //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl; | 
|---|
| [6b919f8] | 270 | } | 
|---|
|  | 271 | } | 
|---|
|  | 272 | } | 
|---|
|  | 273 | if ((CandidateBond != NULL)) { | 
|---|
|  | 274 | CandidateBond->BondDegree++; | 
|---|
| [791138] | 275 | //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl; | 
|---|
| [6b919f8] | 276 | } else { | 
|---|
| [58ed4a] | 277 | DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl); | 
|---|
| [6b919f8] | 278 | FalseBondDegree++; | 
|---|
|  | 279 | } | 
|---|
|  | 280 | } | 
|---|
|  | 281 | return FalseBondDegree; | 
|---|
|  | 282 | }; | 
|---|
|  | 283 |  | 
|---|
| [5e2f80] | 284 | /** Sets the weight of all connected bonds to one. | 
|---|
|  | 285 | */ | 
|---|
|  | 286 | void BondedParticle::resetBondDegree() | 
|---|
|  | 287 | { | 
|---|
|  | 288 | OBSERVE; | 
|---|
|  | 289 | NOTIFY(BondedParticle::BondsChanged); | 
|---|
|  | 290 | for (std::vector<BondList>::iterator Runner = ListOfBonds.begin(); | 
|---|
|  | 291 | Runner != ListOfBonds.end(); | 
|---|
|  | 292 | ++Runner) | 
|---|
|  | 293 | for (BondList::iterator BondRunner = (*Runner).begin(); | 
|---|
|  | 294 | BondRunner != (*Runner).end(); | 
|---|
|  | 295 | ++BondRunner) | 
|---|
|  | 296 | (*BondRunner)->BondDegree = 1; | 
|---|
|  | 297 | }; | 
|---|
|  | 298 |  | 
|---|
| [c0d9eb] | 299 | /** Counts the number of bonds weighted by bond::BondDegree. | 
|---|
|  | 300 | * @param _step time step to access | 
|---|
|  | 301 | * \param bonds times bond::BondDegree | 
|---|
|  | 302 | */ | 
|---|
|  | 303 | int BondedParticle::CountBonds() const | 
|---|
|  | 304 | { | 
|---|
|  | 305 | int NoBonds = 0; | 
|---|
| [9d83b6] | 306 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| [c0d9eb] | 307 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
|  | 308 | Runner != ListOfBonds.end(); | 
|---|
|  | 309 | (++Runner)) | 
|---|
|  | 310 | NoBonds += (*Runner)->BondDegree; | 
|---|
|  | 311 | return NoBonds; | 
|---|
|  | 312 | }; | 
|---|
|  | 313 |  | 
|---|
| [b70721] | 314 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner. | 
|---|
| [073a9e4] | 315 | * @param _step time step to access | 
|---|
| [b70721] | 316 | * \param *BondPartner atom to check for | 
|---|
|  | 317 | * \return true - bond exists, false - bond does not exist | 
|---|
|  | 318 | */ | 
|---|
| [073a9e4] | 319 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const | 
|---|
| [b70721] | 320 | { | 
|---|
|  | 321 | bool status = false; | 
|---|
|  | 322 |  | 
|---|
| [073a9e4] | 323 | const BondList& ListOfBonds = getListOfBondsAtStep(_step); | 
|---|
|  | 324 | for (BondList::const_iterator runner = ListOfBonds.begin(); | 
|---|
|  | 325 | runner != ListOfBonds.end(); | 
|---|
|  | 326 | runner++) { | 
|---|
| [b70721] | 327 | status = status || ((*runner)->Contains(BondPartner)); | 
|---|
|  | 328 | } | 
|---|
|  | 329 | return status; | 
|---|
|  | 330 | }; | 
|---|
|  | 331 |  | 
|---|
| [d74077] | 332 | std::ostream & BondedParticle::operator << (std::ostream &ost) const | 
|---|
|  | 333 | { | 
|---|
|  | 334 | ParticleInfo::operator<<(ost); | 
|---|
|  | 335 | ost << "," << getPosition(); | 
|---|
|  | 336 | return ost; | 
|---|
|  | 337 | } | 
|---|
|  | 338 |  | 
|---|
|  | 339 | std::ostream & operator << (std::ostream &ost, const BondedParticle &a) | 
|---|
|  | 340 | { | 
|---|
|  | 341 | a.ParticleInfo::operator<<(ost); | 
|---|
|  | 342 | ost << "," << a.getPosition(); | 
|---|
|  | 343 | return ost; | 
|---|
|  | 344 | } | 
|---|
|  | 345 |  | 
|---|