| 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 | * 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 reference to created bond or to already present bonds | 
|---|
| 99 | */ | 
|---|
| 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; | 
|---|
| 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 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 |  | 
|---|
| 144 | /** Puts a given bond into atom::ListOfBonds. | 
|---|
| 145 | * @param _step time step to access | 
|---|
| 146 | * \param *Binder bond to insert | 
|---|
| 147 | */ | 
|---|
| 148 | bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder) | 
|---|
| 149 | { | 
|---|
| 150 | OBSERVE; | 
|---|
| 151 | bool status = false; | 
|---|
| 152 | if (Binder != NULL) { | 
|---|
| 153 | if (Binder->Contains(this)) { | 
|---|
| 154 | if (WorldTime::getTime() == _step) | 
|---|
| 155 | NOTIFY(AtomObservable::BondsChanged); | 
|---|
| 156 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step); | 
|---|
| 157 | if (ListOfBonds.size() <= _step) | 
|---|
| 158 | ListOfBonds.resize(_step+1); | 
|---|
| 159 | ListOfBonds[_step].push_back(Binder); | 
|---|
| 160 | status = true; | 
|---|
| 161 | } else { | 
|---|
| 162 | ELOG(1, *Binder << " does not contain " << *this << "."); | 
|---|
| 163 | } | 
|---|
| 164 | } else { | 
|---|
| 165 | ELOG(1, "Binder is " << Binder << "."); | 
|---|
| 166 | } | 
|---|
| 167 | return status; | 
|---|
| 168 | }; | 
|---|
| 169 |  | 
|---|
| 170 | /** Removes a given bond from atom::ListOfBonds. | 
|---|
| 171 | * @param _step time step to access | 
|---|
| 172 | * \param *Binder bond to remove | 
|---|
| 173 | */ | 
|---|
| 174 | bool BondedParticle::UnregisterBond(bond *Binder) | 
|---|
| 175 | { | 
|---|
| 176 | OBSERVE; | 
|---|
| 177 | bool status = false; | 
|---|
| 178 | ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL."); | 
|---|
| 179 | const int step = ContainsBondAtStep(Binder); | 
|---|
| 180 | if (step != -1) { | 
|---|
| 181 | NOTIFY(AtomObservable::BondsChanged); | 
|---|
| 182 | //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step); | 
|---|
| 183 | ListOfBonds[step].remove(Binder); | 
|---|
| 184 | status = true; | 
|---|
| 185 | } else { | 
|---|
| 186 | ELOG(1, *Binder << " does not contain " << *this << "."); | 
|---|
| 187 | } | 
|---|
| 188 | return status; | 
|---|
| 189 | }; | 
|---|
| 190 |  | 
|---|
| 191 | /** Removes all bonds from atom::ListOfBonds. | 
|---|
| 192 | * \note Does not do any memory de-allocation. | 
|---|
| 193 | */ | 
|---|
| 194 | void BondedParticle::UnregisterAllBond(const unsigned int _step) | 
|---|
| 195 | { | 
|---|
| 196 | ListOfBonds[_step].clear(); | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 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 | { | 
|---|
| 205 | OBSERVE; | 
|---|
| 206 | if (WorldTime::getTime() == _step) | 
|---|
| 207 | NOTIFY(AtomObservable::BondsChanged); | 
|---|
| 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 |  | 
|---|
| 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 | */ | 
|---|
| 222 | int BondedParticle::ContainsBondAtStep(bond *Binder) const | 
|---|
| 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 |  | 
|---|
| 244 | /** Corrects the bond degree by one at most if necessary. | 
|---|
| 245 | * \return number of corrections done | 
|---|
| 246 | */ | 
|---|
| 247 | int BondedParticle::CorrectBondDegree() | 
|---|
| 248 | { | 
|---|
| 249 | OBSERVE; | 
|---|
| 250 | NOTIFY(AtomObservable::BondsChanged); | 
|---|
| 251 | int NoBonds = 0; | 
|---|
| 252 | int OtherNoBonds = 0; | 
|---|
| 253 | int FalseBondDegree = 0; | 
|---|
| 254 | atom *OtherWalker = NULL; | 
|---|
| 255 | bond *CandidateBond = NULL; | 
|---|
| 256 |  | 
|---|
| 257 | NoBonds = CountBonds(); | 
|---|
| 258 | //LOG(3, "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?"); | 
|---|
| 259 | if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch | 
|---|
| 260 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 261 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) { | 
|---|
| 262 | OtherWalker = (*Runner)->GetOtherAtom(this); | 
|---|
| 263 | OtherNoBonds = OtherWalker->CountBonds(); | 
|---|
| 264 | //LOG(3, "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?"); | 
|---|
| 265 | if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate | 
|---|
| 266 | const BondList& OtherListOfBonds = OtherWalker->getListOfBonds(); | 
|---|
| 267 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first | 
|---|
| 268 | CandidateBond = (*Runner); | 
|---|
| 269 | //LOG(3, "New candidate is " << *CandidateBond << "."); | 
|---|
| 270 | } | 
|---|
| 271 | } | 
|---|
| 272 | } | 
|---|
| 273 | if ((CandidateBond != NULL)) { | 
|---|
| 274 | CandidateBond->BondDegree++; | 
|---|
| 275 | //LOG(2, "Increased bond degree for bond " << *CandidateBond << "."); | 
|---|
| 276 | } else { | 
|---|
| 277 | ELOG(2, "Could not find correct degree for atom " << *this << "."); | 
|---|
| 278 | FalseBondDegree++; | 
|---|
| 279 | } | 
|---|
| 280 | } | 
|---|
| 281 | return FalseBondDegree; | 
|---|
| 282 | }; | 
|---|
| 283 |  | 
|---|
| 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 |  | 
|---|
| 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; | 
|---|
| 306 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 307 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
| 308 | Runner != ListOfBonds.end(); | 
|---|
| 309 | (++Runner)) | 
|---|
| 310 | NoBonds += (*Runner)->BondDegree; | 
|---|
| 311 | return NoBonds; | 
|---|
| 312 | }; | 
|---|
| 313 |  | 
|---|
| 314 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner. | 
|---|
| 315 | * @param _step time step to access | 
|---|
| 316 | * \param *BondPartner atom to check for | 
|---|
| 317 | * \return true - bond exists, false - bond does not exist | 
|---|
| 318 | */ | 
|---|
| 319 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const | 
|---|
| 320 | { | 
|---|
| 321 | bool status = false; | 
|---|
| 322 |  | 
|---|
| 323 | const BondList& ListOfBonds = getListOfBondsAtStep(_step); | 
|---|
| 324 | for (BondList::const_iterator runner = ListOfBonds.begin(); | 
|---|
| 325 | runner != ListOfBonds.end(); | 
|---|
| 326 | runner++) { | 
|---|
| 327 | status = status || ((*runner)->Contains(BondPartner)); | 
|---|
| 328 | } | 
|---|
| 329 | return status; | 
|---|
| 330 | }; | 
|---|
| 331 |  | 
|---|
| 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 |  | 
|---|