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