[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[5aaa43] | 5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
[94d5ac6] | 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/>.
|
---|
[bcf653] | 22 | */
|
---|
| 23 |
|
---|
[6b919f8] | 24 | /*
|
---|
| 25 | * atom_bondedparticle.cpp
|
---|
| 26 | *
|
---|
| 27 | * Created on: Oct 19, 2009
|
---|
| 28 | * Author: heber
|
---|
| 29 | */
|
---|
| 30 |
|
---|
[bf3817] | 31 | // include config.h
|
---|
| 32 | #ifdef HAVE_CONFIG_H
|
---|
| 33 | #include <config.h>
|
---|
| 34 | #endif
|
---|
| 35 |
|
---|
[ad011c] | 36 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 37 |
|
---|
[f63e41] | 38 | #include <algorithm>
|
---|
| 39 | #include <boost/bind.hpp>
|
---|
| 40 |
|
---|
[6b919f8] | 41 | #include "atom.hpp"
|
---|
| 42 | #include "atom_bondedparticle.hpp"
|
---|
[129204] | 43 | #include "Bond/bond.hpp"
|
---|
[d557374] | 44 | #include "CodePatterns/Assert.hpp"
|
---|
[ad011c] | 45 | #include "CodePatterns/Log.hpp"
|
---|
| 46 | #include "CodePatterns/Verbose.hpp"
|
---|
[3bdb6d] | 47 | #include "Element/element.hpp"
|
---|
[db7e6d] | 48 | #include "WorldTime.hpp"
|
---|
[6b919f8] | 49 |
|
---|
| 50 | /** Constructor of class BondedParticle.
|
---|
| 51 | */
|
---|
[70ff32] | 52 | BondedParticle::BondedParticle()
|
---|
| 53 | {
|
---|
[9d83b6] | 54 | ListOfBonds.push_back(BondList());
|
---|
[70ff32] | 55 | };
|
---|
[6b919f8] | 56 |
|
---|
| 57 | /** Destructor of class BondedParticle.
|
---|
| 58 | */
|
---|
| 59 | BondedParticle::~BondedParticle()
|
---|
| 60 | {
|
---|
[5e2f80] | 61 | removeAllBonds();
|
---|
[6b919f8] | 62 | };
|
---|
| 63 |
|
---|
| 64 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
|
---|
| 65 | * \param *file output stream
|
---|
| 66 | */
|
---|
[b453f9] | 67 | void BondedParticle::OutputOrder(ofstream *file) const
|
---|
[6b919f8] | 68 | {
|
---|
[735b1c] | 69 | *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
|
---|
[d760bb] | 70 | //LOG(2, "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << MaxOrder << ".");
|
---|
[6b919f8] | 71 | };
|
---|
| 72 |
|
---|
| 73 | /** Prints all bonds of this atom with total degree.
|
---|
| 74 | */
|
---|
[4b5cf8] | 75 | void BondedParticle::OutputBondOfAtom(std::ostream &ost) const
|
---|
[6b919f8] | 76 | {
|
---|
[9d83b6] | 77 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[4b5cf8] | 78 | ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: ";
|
---|
[e138de] | 79 | int TotalDegree = 0;
|
---|
| 80 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
|
---|
[4b5cf8] | 81 | ost << **Runner << "\t";
|
---|
[1f693d] | 82 | TotalDegree += (*Runner)->getDegree();
|
---|
[e138de] | 83 | }
|
---|
[4b5cf8] | 84 | ost << " -- TotalDegree: " << TotalDegree;
|
---|
[6b919f8] | 85 | };
|
---|
| 86 |
|
---|
[5309ba] | 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.
|
---|
[1f1b23] | 89 | * \param *AdjacencyFile output stream
|
---|
| 90 | */
|
---|
| 91 | void BondedParticle::OutputBonds(ofstream * const BondFile) const
|
---|
| 92 | {
|
---|
[9d83b6] | 93 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[1f1b23] | 94 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
---|
[735b1c] | 95 | if (getNr() < (*Runner)->GetOtherAtom(this)->getNr())
|
---|
[52ed5b] | 96 | *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n";
|
---|
[1f1b23] | 97 | };
|
---|
| 98 |
|
---|
[b8d4a3] | 99 | /**
|
---|
[db7e6d] | 100 | * Adds a bond between this bonded particle and another. Returns present instance if this
|
---|
[b8d4a3] | 101 | * bond already exists.
|
---|
| 102 | *
|
---|
[073a9e4] | 103 | * @param _step time step to access
|
---|
[db7e6d] | 104 | * @param bonding partner
|
---|
[d948b4] | 105 | * @return pointer to created bond or to already present bonds
|
---|
[b8d4a3] | 106 | */
|
---|
[d948b4] | 107 | bond::ptr BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner)
|
---|
[db7e6d] | 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;
|
---|
[b8d4a3] | 115 | }
|
---|
| 116 |
|
---|
[7d82a5] | 117 | bond::ptr newBond(new bond((atom*) this, (atom*) Partner, 1));
|
---|
[073a9e4] | 118 | RegisterBond(_step, newBond);
|
---|
| 119 | Partner->RegisterBond(_step, newBond);
|
---|
[db7e6d] | 120 |
|
---|
| 121 | return newBond;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[d948b4] | 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 | {
|
---|
[1f693d] | 133 | return addBond(WorldTime::getTime(), Partner);
|
---|
[d948b4] | 134 | }
|
---|
| 135 |
|
---|
[7d82a5] | 136 | /** Helper function to find the time step to a given bond in \a Binder.
|
---|
[f63e41] | 137 | *
|
---|
[7d82a5] | 138 | * \param Binder bond to look for
|
---|
| 139 | * \return ListOfBonds::size() - not found, else - step containing \a Binder
|
---|
[f63e41] | 140 | */
|
---|
[7d82a5] | 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
|
---|
[f63e41] | 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)));
|
---|
[7d82a5] | 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);
|
---|
[f63e41] | 187 | } else
|
---|
| 188 | ELOG(1, "BondedParticle::removeBond() - I cannot find the bond in between "
|
---|
| 189 | +toString(getName())+" and "+toString(Partner->getName())+".");
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[d948b4] | 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 |
|
---|
[db7e6d] | 201 | /** Removes a bond for this atom.
|
---|
| 202 | *
|
---|
| 203 | * @param Binder bond to remove
|
---|
| 204 | */
|
---|
[7d82a5] | 205 | void BondedParticle::removeBond(bond::ptr &binder)
|
---|
[db7e6d] | 206 | {
|
---|
[7d82a5] | 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 | }
|
---|
[b8d4a3] | 220 | }
|
---|
| 221 |
|
---|
[cc9119] | 222 | /** Removes all bonds in all timesteps and their instances, too.
|
---|
[5e2f80] | 223 | *
|
---|
| 224 | */
|
---|
| 225 | void BondedParticle::removeAllBonds()
|
---|
| 226 | {
|
---|
| 227 | for (size_t index = 0; index < ListOfBonds.size(); ++index)
|
---|
[cc9119] | 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 | {
|
---|
[7d82a5] | 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);
|
---|
[5e2f80] | 248 | }
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[6b919f8] | 251 | /** Puts a given bond into atom::ListOfBonds.
|
---|
[073a9e4] | 252 | * @param _step time step to access
|
---|
[6b919f8] | 253 | * \param *Binder bond to insert
|
---|
| 254 | */
|
---|
[88c8ec] | 255 | bool BondedParticle::RegisterBond(const unsigned int _step, bond::ptr const Binder)
|
---|
[6b919f8] | 256 | {
|
---|
| 257 | bool status = false;
|
---|
| 258 | if (Binder != NULL) {
|
---|
[7d82a5] | 259 | OBSERVE;
|
---|
[6b919f8] | 260 | if (Binder->Contains(this)) {
|
---|
[d557374] | 261 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
|
---|
[5e2f80] | 262 | if (ListOfBonds.size() <= _step)
|
---|
| 263 | ListOfBonds.resize(_step+1);
|
---|
| 264 | ListOfBonds[_step].push_back(Binder);
|
---|
[74ec1f] | 265 | if (WorldTime::getTime() == _step)
|
---|
[2ad1ec] | 266 | NOTIFY(AtomObservable::BondsAdded);
|
---|
[6b919f8] | 267 | status = true;
|
---|
| 268 | } else {
|
---|
[47d041] | 269 | ELOG(1, *Binder << " does not contain " << *this << ".");
|
---|
[6b919f8] | 270 | }
|
---|
| 271 | } else {
|
---|
[47d041] | 272 | ELOG(1, "Binder is " << Binder << ".");
|
---|
[6b919f8] | 273 | }
|
---|
| 274 | return status;
|
---|
| 275 | };
|
---|
| 276 |
|
---|
| 277 | /** Removes a given bond from atom::ListOfBonds.
|
---|
[7d82a5] | 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 | *
|
---|
[9d83b6] | 282 | * @param _step time step to access
|
---|
[6b919f8] | 283 | * \param *Binder bond to remove
|
---|
| 284 | */
|
---|
[7d82a5] | 285 | bool BondedParticle::UnregisterBond(const unsigned int _step, bond::ptr const Binder)
|
---|
[6b919f8] | 286 | {
|
---|
| 287 | bool status = false;
|
---|
[7d82a5] | 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 | }
|
---|
[6b919f8] | 307 | } else {
|
---|
[7d82a5] | 308 | ELOG(1, "Binder is " << Binder << ".");
|
---|
[6b919f8] | 309 | }
|
---|
| 310 | return status;
|
---|
| 311 | };
|
---|
| 312 |
|
---|
[583081] | 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 | {
|
---|
[7d82a5] | 319 | removeAllBonds(_step);
|
---|
[583081] | 320 | }
|
---|
| 321 |
|
---|
[93c6e9] | 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 | */
|
---|
[88c8ec] | 327 | int BondedParticle::ContainsBondAtStep(bond::ptr Binder) const
|
---|
[93c6e9] | 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 |
|
---|
[c0d9eb] | 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;
|
---|
[9d83b6] | 356 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[c0d9eb] | 357 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 358 | Runner != ListOfBonds.end();
|
---|
| 359 | (++Runner))
|
---|
[1f693d] | 360 | NoBonds += (*Runner)->getDegree();
|
---|
[c0d9eb] | 361 | return NoBonds;
|
---|
| 362 | };
|
---|
| 363 |
|
---|
[b70721] | 364 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
|
---|
[073a9e4] | 365 | * @param _step time step to access
|
---|
[b70721] | 366 | * \param *BondPartner atom to check for
|
---|
| 367 | * \return true - bond exists, false - bond does not exist
|
---|
| 368 | */
|
---|
[073a9e4] | 369 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
|
---|
[b70721] | 370 | {
|
---|
| 371 | bool status = false;
|
---|
| 372 |
|
---|
[073a9e4] | 373 | const BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
---|
| 374 | for (BondList::const_iterator runner = ListOfBonds.begin();
|
---|
| 375 | runner != ListOfBonds.end();
|
---|
| 376 | runner++) {
|
---|
[b70721] | 377 | status = status || ((*runner)->Contains(BondPartner));
|
---|
| 378 | }
|
---|
| 379 | return status;
|
---|
| 380 | };
|
---|
| 381 |
|
---|
[d74077] | 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 |
|
---|