| 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.hpp" | 
|---|
| 25 | #include "CodePatterns/Assert.hpp" | 
|---|
| 26 | #include "CodePatterns/Log.hpp" | 
|---|
| 27 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 28 | #include "element.hpp" | 
|---|
| 29 | #include "lists.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 | 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); | 
|---|
| 51 | } | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file. | 
|---|
| 55 | * \param *file output stream | 
|---|
| 56 | */ | 
|---|
| 57 | void BondedParticle::OutputOrder(ofstream *file) const | 
|---|
| 58 | { | 
|---|
| 59 | *file << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl; | 
|---|
| 60 | //Log() << Verbose(2) << "Storing: " << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl; | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | /** Prints all bonds of this atom with total degree. | 
|---|
| 64 | */ | 
|---|
| 65 | void BondedParticle::OutputBondOfAtom() const | 
|---|
| 66 | { | 
|---|
| 67 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 68 | DoLog(4) && (Log() << Verbose(4) << "Atom " << getName() << "/" << nr << " with " << ListOfBonds.size() << " bonds: " << endl); | 
|---|
| 69 | int TotalDegree = 0; | 
|---|
| 70 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) { | 
|---|
| 71 | DoLog(4) && (Log() << Verbose(4) << **Runner << endl); | 
|---|
| 72 | TotalDegree += (*Runner)->BondDegree; | 
|---|
| 73 | } | 
|---|
| 74 | DoLog(4) && (Log() << Verbose(4) << " -- TotalDegree: " << TotalDegree << endl); | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | /** Output of atom::nr along with all bond partners. | 
|---|
| 78 | * \param *AdjacencyFile output stream | 
|---|
| 79 | */ | 
|---|
| 80 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const | 
|---|
| 81 | { | 
|---|
| 82 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 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 |  | 
|---|
| 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 | { | 
|---|
| 95 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 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 |  | 
|---|
| 101 | /** | 
|---|
| 102 | * Adds a bond between this bonded particle and another. Does nothing if this | 
|---|
| 103 | * bond already exists. | 
|---|
| 104 | * | 
|---|
| 105 | * @param _step time step to access | 
|---|
| 106 | * \param bonding partner | 
|---|
| 107 | */ | 
|---|
| 108 | void BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner) { | 
|---|
| 109 | if (IsBondedTo(_step, Partner)) { | 
|---|
| 110 | return; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | bond* newBond = new bond((atom*) this, (atom*) Partner, 1, 0); | 
|---|
| 114 | RegisterBond(_step, newBond); | 
|---|
| 115 | Partner->RegisterBond(_step, newBond); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | /** Puts a given bond into atom::ListOfBonds. | 
|---|
| 119 | * @param _step time step to access | 
|---|
| 120 | * \param *Binder bond to insert | 
|---|
| 121 | */ | 
|---|
| 122 | bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder) | 
|---|
| 123 | { | 
|---|
| 124 | bool status = false; | 
|---|
| 125 | if (Binder != NULL) { | 
|---|
| 126 | if (Binder->Contains(this)) { | 
|---|
| 127 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step); | 
|---|
| 128 | BondList& ListOfBonds = getListOfBondsAtStep(_step); | 
|---|
| 129 | ListOfBonds.push_back(Binder); | 
|---|
| 130 | status = true; | 
|---|
| 131 | } else { | 
|---|
| 132 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl); | 
|---|
| 133 | } | 
|---|
| 134 | } else { | 
|---|
| 135 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl); | 
|---|
| 136 | } | 
|---|
| 137 | return status; | 
|---|
| 138 | }; | 
|---|
| 139 |  | 
|---|
| 140 | /** Removes a given bond from atom::ListOfBonds. | 
|---|
| 141 | * @param _step time step to access | 
|---|
| 142 | * \param *Binder bond to remove | 
|---|
| 143 | */ | 
|---|
| 144 | bool BondedParticle::UnregisterBond(bond *Binder) | 
|---|
| 145 | { | 
|---|
| 146 | bool status = false; | 
|---|
| 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; | 
|---|
| 153 | } else { | 
|---|
| 154 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl); | 
|---|
| 155 | } | 
|---|
| 156 | return status; | 
|---|
| 157 | }; | 
|---|
| 158 |  | 
|---|
| 159 | /** Removes all bonds from atom::ListOfBonds. | 
|---|
| 160 | * \note Does not do any memory de-allocation. | 
|---|
| 161 | */ | 
|---|
| 162 | void BondedParticle::UnregisterAllBond(const unsigned int _step) | 
|---|
| 163 | { | 
|---|
| 164 | ListOfBonds[_step].clear(); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 209 | /** Corrects the bond degree by one at most if necessary. | 
|---|
| 210 | * \return number of corrections done | 
|---|
| 211 | */ | 
|---|
| 212 | int BondedParticle::CorrectBondDegree() | 
|---|
| 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(); | 
|---|
| 221 | //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl; | 
|---|
| 222 | if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch | 
|---|
| 223 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 224 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) { | 
|---|
| 225 | OtherWalker = (*Runner)->GetOtherAtom(this); | 
|---|
| 226 | OtherNoBonds = OtherWalker->CountBonds(); | 
|---|
| 227 | //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl; | 
|---|
| 228 | if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate | 
|---|
| 229 | const BondList& OtherListOfBonds = OtherWalker->getListOfBonds(); | 
|---|
| 230 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first | 
|---|
| 231 | CandidateBond = (*Runner); | 
|---|
| 232 | //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl; | 
|---|
| 233 | } | 
|---|
| 234 | } | 
|---|
| 235 | } | 
|---|
| 236 | if ((CandidateBond != NULL)) { | 
|---|
| 237 | CandidateBond->BondDegree++; | 
|---|
| 238 | //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl; | 
|---|
| 239 | } else { | 
|---|
| 240 | DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl); | 
|---|
| 241 | FalseBondDegree++; | 
|---|
| 242 | } | 
|---|
| 243 | } | 
|---|
| 244 | return FalseBondDegree; | 
|---|
| 245 | }; | 
|---|
| 246 |  | 
|---|
| 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; | 
|---|
| 254 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 255 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
| 256 | Runner != ListOfBonds.end(); | 
|---|
| 257 | (++Runner)) | 
|---|
| 258 | NoBonds += (*Runner)->BondDegree; | 
|---|
| 259 | return NoBonds; | 
|---|
| 260 | }; | 
|---|
| 261 |  | 
|---|
| 262 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner. | 
|---|
| 263 | * @param _step time step to access | 
|---|
| 264 | * \param *BondPartner atom to check for | 
|---|
| 265 | * \return true - bond exists, false - bond does not exist | 
|---|
| 266 | */ | 
|---|
| 267 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const | 
|---|
| 268 | { | 
|---|
| 269 | bool status = false; | 
|---|
| 270 |  | 
|---|
| 271 | const BondList& ListOfBonds = getListOfBondsAtStep(_step); | 
|---|
| 272 | for (BondList::const_iterator runner = ListOfBonds.begin(); | 
|---|
| 273 | runner != ListOfBonds.end(); | 
|---|
| 274 | runner++) { | 
|---|
| 275 | status = status || ((*runner)->Contains(BondPartner)); | 
|---|
| 276 | } | 
|---|
| 277 | return status; | 
|---|
| 278 | }; | 
|---|
| 279 |  | 
|---|
| 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 |  | 
|---|