| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| 6 | * | 
|---|
| 7 | * | 
|---|
| 8 | *   This file is part of MoleCuilder. | 
|---|
| 9 | * | 
|---|
| 10 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 11 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 12 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 13 | *    (at your option) any later version. | 
|---|
| 14 | * | 
|---|
| 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 18 | *    GNU General Public License for more details. | 
|---|
| 19 | * | 
|---|
| 20 | *    You should have received a copy of the GNU General Public License | 
|---|
| 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | /* | 
|---|
| 25 | * atom_bondedparticle.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: Oct 19, 2009 | 
|---|
| 28 | *      Author: heber | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | // include config.h | 
|---|
| 32 | #ifdef HAVE_CONFIG_H | 
|---|
| 33 | #include <config.h> | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include <algorithm> | 
|---|
| 39 | #include <boost/bind.hpp> | 
|---|
| 40 |  | 
|---|
| 41 | #include "atom.hpp" | 
|---|
| 42 | #include "atom_bondedparticle.hpp" | 
|---|
| 43 | #include "Bond/bond.hpp" | 
|---|
| 44 | #include "CodePatterns/Assert.hpp" | 
|---|
| 45 | #include "CodePatterns/Log.hpp" | 
|---|
| 46 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 47 | #include "Element/element.hpp" | 
|---|
| 48 | #include "WorldTime.hpp" | 
|---|
| 49 |  | 
|---|
| 50 | /** Constructor of class BondedParticle. | 
|---|
| 51 | */ | 
|---|
| 52 | BondedParticle::BondedParticle() | 
|---|
| 53 | { | 
|---|
| 54 | ListOfBonds.push_back(BondList()); | 
|---|
| 55 | }; | 
|---|
| 56 |  | 
|---|
| 57 | /** Destructor of class BondedParticle. | 
|---|
| 58 | */ | 
|---|
| 59 | BondedParticle::~BondedParticle() | 
|---|
| 60 | { | 
|---|
| 61 | removeAllBonds(); | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file. | 
|---|
| 65 | * \param *file output stream | 
|---|
| 66 | */ | 
|---|
| 67 | void BondedParticle::OutputOrder(ofstream *file) const | 
|---|
| 68 | { | 
|---|
| 69 | *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl; | 
|---|
| 70 | //LOG(2, "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << MaxOrder << "."); | 
|---|
| 71 | }; | 
|---|
| 72 |  | 
|---|
| 73 | /** Prints all bonds of this atom with total degree. | 
|---|
| 74 | */ | 
|---|
| 75 | void BondedParticle::OutputBondOfAtom(std::ostream &ost) const | 
|---|
| 76 | { | 
|---|
| 77 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 78 | ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: "; | 
|---|
| 79 | int TotalDegree = 0; | 
|---|
| 80 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) { | 
|---|
| 81 | ost << **Runner << "\t"; | 
|---|
| 82 | TotalDegree += (*Runner)->getDegree(); | 
|---|
| 83 | } | 
|---|
| 84 | ost << " -- TotalDegree: " << TotalDegree; | 
|---|
| 85 | }; | 
|---|
| 86 |  | 
|---|
| 87 | /** Output of atom::Nr along each bond partner per line. | 
|---|
| 88 | * Only bonds are printed where atom::Nr is smaller than the one of the bond partner. | 
|---|
| 89 | * \param *AdjacencyFile output stream | 
|---|
| 90 | */ | 
|---|
| 91 | void BondedParticle::OutputBonds(ofstream * const BondFile) const | 
|---|
| 92 | { | 
|---|
| 93 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 94 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) | 
|---|
| 95 | if (getNr() < (*Runner)->GetOtherAtom(this)->getNr()) | 
|---|
| 96 | *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n"; | 
|---|
| 97 | }; | 
|---|
| 98 |  | 
|---|
| 99 | /** | 
|---|
| 100 | * Adds a bond between this bonded particle and another. Returns present instance if this | 
|---|
| 101 | * bond already exists. | 
|---|
| 102 | * | 
|---|
| 103 | * @param _step time step to access | 
|---|
| 104 | * @param bonding partner | 
|---|
| 105 | * @return pointer to created bond or to already present bonds | 
|---|
| 106 | */ | 
|---|
| 107 | bond::ptr BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner) | 
|---|
| 108 | { | 
|---|
| 109 | const BondList &bondlist = getListOfBondsAtStep(_step); | 
|---|
| 110 | for (BondList::const_iterator runner = bondlist.begin(); | 
|---|
| 111 | runner != bondlist.end(); | 
|---|
| 112 | runner++) { | 
|---|
| 113 | if ((*runner)->Contains(Partner)) | 
|---|
| 114 | return *runner; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | bond::ptr newBond(new bond((atom*) this, (atom*) Partner, 1)); | 
|---|
| 118 | RegisterBond(_step, newBond); | 
|---|
| 119 | Partner->RegisterBond(_step, newBond); | 
|---|
| 120 |  | 
|---|
| 121 | return newBond; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | /** | 
|---|
| 125 | * Adds a bond between this bonded particle and another. Returns present instance if this | 
|---|
| 126 | * bond already exists. | 
|---|
| 127 | * | 
|---|
| 128 | * @param bonding partner | 
|---|
| 129 | * @return pointer to created bond or to already present bonds | 
|---|
| 130 | */ | 
|---|
| 131 | bond::ptr BondedParticle::addBond(BondedParticle* Partner) | 
|---|
| 132 | { | 
|---|
| 133 | return addBond(WorldTime::getTime(), Partner); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | /** Helper function to find the time step to a given bond in \a Binder. | 
|---|
| 137 | * | 
|---|
| 138 | * \param Binder bond to look for | 
|---|
| 139 | * \return ListOfBonds::size() - not found, else - step containing \a Binder | 
|---|
| 140 | */ | 
|---|
| 141 | unsigned int BondedParticle::findBondsStep(bond::ptr const Binder) const | 
|---|
| 142 | { | 
|---|
| 143 |  | 
|---|
| 144 | size_t _step = 0; | 
|---|
| 145 | for (;_step < ListOfBonds.size();++_step) { | 
|---|
| 146 | const BondList& ListOfBonds = getListOfBondsAtStep(_step); | 
|---|
| 147 | if (std::find(ListOfBonds.begin(), ListOfBonds.end(), Binder) != ListOfBonds.end()) | 
|---|
| 148 | break; | 
|---|
| 149 | } | 
|---|
| 150 | return _step; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | /** Helper function to find the iterator to a bond at a given time \a step to | 
|---|
| 154 | * a given bond partner in \a Partner. | 
|---|
| 155 | * | 
|---|
| 156 | * \param _step time step to look at | 
|---|
| 157 | * \param Partner bond partner to look for | 
|---|
| 158 | * \return ListOfBonds::end() - not found, else - iterator pointing \a Binder | 
|---|
| 159 | */ | 
|---|
| 160 | BondList::const_iterator BondedParticle::findBondPartnerAtStep( | 
|---|
| 161 | const unsigned int _step, | 
|---|
| 162 | BondedParticle * const Partner) const | 
|---|
| 163 | { | 
|---|
| 164 | const BondList& ListOfBonds = getListOfBondsAtStep(_step); | 
|---|
| 165 | BondList::const_iterator iter = std::find_if(ListOfBonds.begin(), ListOfBonds.end(), | 
|---|
| 166 | boost::bind( | 
|---|
| 167 | static_cast<bool (bond::*)(const ParticleInfo * const) const>(&bond::Contains), | 
|---|
| 168 | _1, | 
|---|
| 169 | boost::cref(Partner))); | 
|---|
| 170 | return iter; | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | /** Removes a bond of this atom to a given \a Partner. | 
|---|
| 174 | * | 
|---|
| 175 | * @param _step time step | 
|---|
| 176 | * @param Partner bond partner | 
|---|
| 177 | */ | 
|---|
| 178 | void BondedParticle::removeBond(const unsigned int _step, BondedParticle * const Partner) | 
|---|
| 179 | { | 
|---|
| 180 | BondList::const_iterator iter = findBondPartnerAtStep(_step, Partner); | 
|---|
| 181 | if (iter != getListOfBondsAtStep(_step).end()) { | 
|---|
| 182 | // iter becomes invalid upon first unregister, | 
|---|
| 183 | // hence store the bond someplace else first | 
|---|
| 184 | bond::ptr const Binder = *iter; | 
|---|
| 185 | UnregisterBond(_step, Binder); | 
|---|
| 186 | Partner->UnregisterBond(_step, Binder); | 
|---|
| 187 | } else | 
|---|
| 188 | ELOG(1, "BondedParticle::removeBond() - I cannot find the bond in between " | 
|---|
| 189 | +toString(getName())+" and "+toString(Partner->getName())+"."); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | /** Removes a bond of this atom to a given \a Partner. | 
|---|
| 193 | * | 
|---|
| 194 | * @param Partner bond partner | 
|---|
| 195 | */ | 
|---|
| 196 | void BondedParticle::removeBond(BondedParticle * const Partner) | 
|---|
| 197 | { | 
|---|
| 198 | removeBond(WorldTime::getTime(), Partner); | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | /** Removes a bond for this atom. | 
|---|
| 202 | * | 
|---|
| 203 | * @param Binder bond to remove | 
|---|
| 204 | */ | 
|---|
| 205 | void BondedParticle::removeBond(bond::ptr &binder) | 
|---|
| 206 | { | 
|---|
| 207 | if (binder != NULL) { | 
|---|
| 208 | atom * const Other = binder->GetOtherAtom(this); | 
|---|
| 209 | ASSERT( Other != NULL, | 
|---|
| 210 | "BondedParticle::removeBonds() - cannot find bond partner for " | 
|---|
| 211 | +toString(*binder)+"."); | 
|---|
| 212 | // find bond at step | 
|---|
| 213 | unsigned int step = findBondsStep(binder); | 
|---|
| 214 | if (step != ListOfBonds.size()) { | 
|---|
| 215 | UnregisterBond(step, binder); | 
|---|
| 216 | Other->UnregisterBond(step, binder); | 
|---|
| 217 | binder.reset(); | 
|---|
| 218 | } | 
|---|
| 219 | } | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | /** Removes all bonds in all timesteps and their instances, too. | 
|---|
| 223 | * | 
|---|
| 224 | */ | 
|---|
| 225 | void BondedParticle::removeAllBonds() | 
|---|
| 226 | { | 
|---|
| 227 | for (size_t index = 0; index < ListOfBonds.size(); ++index) | 
|---|
| 228 | removeAllBonds(index); | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | /** Removes all bonds for a given \a _step and their instances, too. | 
|---|
| 232 | * | 
|---|
| 233 | * @param _step time step to access | 
|---|
| 234 | */ | 
|---|
| 235 | void BondedParticle::removeAllBonds(const unsigned int _step) | 
|---|
| 236 | { | 
|---|
| 237 | //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]); | 
|---|
| 238 | for (BondList::iterator iter = (ListOfBonds[_step]).begin(); | 
|---|
| 239 | !(ListOfBonds[_step]).empty(); | 
|---|
| 240 | iter = (ListOfBonds[_step]).begin()) { | 
|---|
| 241 | //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds); | 
|---|
| 242 | atom * const Other = (*iter)->GetOtherAtom(this); | 
|---|
| 243 | ASSERT( Other != NULL, | 
|---|
| 244 | "BondedParticle::removeAllBonds() - cannot find bond partner for " | 
|---|
| 245 | +toString(**iter)+"."); | 
|---|
| 246 | Other->UnregisterBond(_step, *iter); | 
|---|
| 247 | UnregisterBond(_step, *iter); | 
|---|
| 248 | } | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | /** Puts a given bond into atom::ListOfBonds. | 
|---|
| 252 | * @param _step time step to access | 
|---|
| 253 | * \param *Binder bond to insert | 
|---|
| 254 | */ | 
|---|
| 255 | bool BondedParticle::RegisterBond(const unsigned int _step, bond::ptr const Binder) | 
|---|
| 256 | { | 
|---|
| 257 | bool status = false; | 
|---|
| 258 | if (Binder != NULL) { | 
|---|
| 259 | OBSERVE; | 
|---|
| 260 | if (Binder->Contains(this)) { | 
|---|
| 261 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step); | 
|---|
| 262 | if (ListOfBonds.size() <= _step) | 
|---|
| 263 | ListOfBonds.resize(_step+1); | 
|---|
| 264 | ListOfBonds[_step].push_back(Binder); | 
|---|
| 265 | if (WorldTime::getTime() == _step) | 
|---|
| 266 | NOTIFY(AtomObservable::BondsAdded); | 
|---|
| 267 | status = true; | 
|---|
| 268 | } else { | 
|---|
| 269 | ELOG(1, *Binder << " does not contain " << *this << "."); | 
|---|
| 270 | } | 
|---|
| 271 | } else { | 
|---|
| 272 | ELOG(1, "Binder is " << Binder << "."); | 
|---|
| 273 | } | 
|---|
| 274 | return status; | 
|---|
| 275 | }; | 
|---|
| 276 |  | 
|---|
| 277 | /** Removes a given bond from atom::ListOfBonds. | 
|---|
| 278 | * | 
|---|
| 279 | * \warning This only removes this atom not its bond partner, i.e. | 
|---|
| 280 | * both atoms need to call this function to fully empty a bond. | 
|---|
| 281 | * | 
|---|
| 282 | * @param _step time step to access | 
|---|
| 283 | * \param *Binder bond to remove | 
|---|
| 284 | */ | 
|---|
| 285 | bool BondedParticle::UnregisterBond(const unsigned int _step, bond::ptr const Binder) | 
|---|
| 286 | { | 
|---|
| 287 | bool status = false; | 
|---|
| 288 | if (Binder != NULL) { | 
|---|
| 289 | if (Binder->Contains(this)) { | 
|---|
| 290 | OBSERVE; | 
|---|
| 291 | //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step); | 
|---|
| 292 | #ifndef NDEBUG | 
|---|
| 293 | BondList::const_iterator iter = | 
|---|
| 294 | std::find(ListOfBonds[_step].begin(), ListOfBonds[_step].end(), Binder); | 
|---|
| 295 | ASSERT( iter != ListOfBonds[_step].end(), | 
|---|
| 296 | "BondedParticle::UnregisterBond() - "+toString(*Binder)+" not contained at " | 
|---|
| 297 | +toString(_step)); | 
|---|
| 298 | #endif | 
|---|
| 299 | Binder->removeAtom(this); | 
|---|
| 300 | ListOfBonds[_step].remove(Binder); | 
|---|
| 301 | if (WorldTime::getTime() == _step) | 
|---|
| 302 | NOTIFY(AtomObservable::BondsRemoved); | 
|---|
| 303 | status = true; | 
|---|
| 304 | } else { | 
|---|
| 305 | ELOG(1, *Binder << " does not contain " << *this << "."); | 
|---|
| 306 | } | 
|---|
| 307 | } else { | 
|---|
| 308 | ELOG(1, "Binder is " << Binder << "."); | 
|---|
| 309 | } | 
|---|
| 310 | return status; | 
|---|
| 311 | }; | 
|---|
| 312 |  | 
|---|
| 313 | /** Removes all bonds of given \a _step with freeing memory. | 
|---|
| 314 | * | 
|---|
| 315 | * @param _step time step whose bonds to free | 
|---|
| 316 | */ | 
|---|
| 317 | void BondedParticle::ClearBondsAtStep(const unsigned int _step) | 
|---|
| 318 | { | 
|---|
| 319 | removeAllBonds(_step); | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | /** Searches for the time step where the given bond \a *Binder is a bond of this particle. | 
|---|
| 323 | * | 
|---|
| 324 | * @param Binder bond to check | 
|---|
| 325 | * @return >=0 - first time step where bond appears, -1 - bond not present in lists | 
|---|
| 326 | */ | 
|---|
| 327 | int BondedParticle::ContainsBondAtStep(bond::ptr Binder) const | 
|---|
| 328 | { | 
|---|
| 329 | int step = -1; | 
|---|
| 330 | int tempstep = 0; | 
|---|
| 331 | for(std::vector<BondList>::const_iterator iter = ListOfBonds.begin(); | 
|---|
| 332 | iter != ListOfBonds.end(); | 
|---|
| 333 | ++iter,++tempstep) { | 
|---|
| 334 | for (BondList::const_iterator bonditer = iter->begin(); | 
|---|
| 335 | bonditer != iter->end(); | 
|---|
| 336 | ++bonditer) { | 
|---|
| 337 | if ((*bonditer) == Binder) { | 
|---|
| 338 | step = tempstep; | 
|---|
| 339 | break; | 
|---|
| 340 | } | 
|---|
| 341 | } | 
|---|
| 342 | if (step != -1) | 
|---|
| 343 | break; | 
|---|
| 344 | } | 
|---|
| 345 |  | 
|---|
| 346 | return step; | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | /** Counts the number of bonds weighted by bond::BondDegree. | 
|---|
| 350 | * @param _step time step to access | 
|---|
| 351 | * \param bonds times bond::BondDegree | 
|---|
| 352 | */ | 
|---|
| 353 | int BondedParticle::CountBonds() const | 
|---|
| 354 | { | 
|---|
| 355 | int NoBonds = 0; | 
|---|
| 356 | const BondList& ListOfBonds = getListOfBonds(); | 
|---|
| 357 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
| 358 | Runner != ListOfBonds.end(); | 
|---|
| 359 | (++Runner)) | 
|---|
| 360 | NoBonds += (*Runner)->getDegree(); | 
|---|
| 361 | return NoBonds; | 
|---|
| 362 | }; | 
|---|
| 363 |  | 
|---|
| 364 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner. | 
|---|
| 365 | * @param _step time step to access | 
|---|
| 366 | * \param *BondPartner atom to check for | 
|---|
| 367 | * \return true - bond exists, false - bond does not exist | 
|---|
| 368 | */ | 
|---|
| 369 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const | 
|---|
| 370 | { | 
|---|
| 371 | bool status = false; | 
|---|
| 372 |  | 
|---|
| 373 | const BondList& ListOfBonds = getListOfBondsAtStep(_step); | 
|---|
| 374 | for (BondList::const_iterator runner = ListOfBonds.begin(); | 
|---|
| 375 | runner != ListOfBonds.end(); | 
|---|
| 376 | runner++) { | 
|---|
| 377 | status = status || ((*runner)->Contains(BondPartner)); | 
|---|
| 378 | } | 
|---|
| 379 | return status; | 
|---|
| 380 | }; | 
|---|
| 381 |  | 
|---|
| 382 | std::ostream & BondedParticle::operator << (std::ostream &ost) const | 
|---|
| 383 | { | 
|---|
| 384 | ParticleInfo::operator<<(ost); | 
|---|
| 385 | ost << "," << getPosition(); | 
|---|
| 386 | return ost; | 
|---|
| 387 | } | 
|---|
| 388 |  | 
|---|
| 389 | std::ostream & operator << (std::ostream &ost, const BondedParticle &a) | 
|---|
| 390 | { | 
|---|
| 391 | a.ParticleInfo::operator<<(ost); | 
|---|
| 392 | ost << "," << a.getPosition(); | 
|---|
| 393 | return ost; | 
|---|
| 394 | } | 
|---|
| 395 |  | 
|---|