[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 | {
|
---|
[8cc22f] | 54 | ListOfBonds.insert( std::make_pair(0, emptyList) );
|
---|
[70ff32] | 55 | };
|
---|
[6b919f8] | 56 |
|
---|
| 57 | /** Destructor of class BondedParticle.
|
---|
| 58 | */
|
---|
| 59 | BondedParticle::~BondedParticle()
|
---|
| 60 | {
|
---|
[8cc22f] | 61 | for(BondTrajectory_t::iterator iter = ListOfBonds.begin(); !ListOfBonds.empty();
|
---|
| 62 | iter = ListOfBonds.begin()) {
|
---|
| 63 | removeAllBonds(iter->first);
|
---|
| 64 | ListOfBonds.erase(iter);
|
---|
| 65 | }
|
---|
[6b919f8] | 66 | };
|
---|
| 67 |
|
---|
| 68 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
|
---|
| 69 | * \param *file output stream
|
---|
| 70 | */
|
---|
[b453f9] | 71 | void BondedParticle::OutputOrder(ofstream *file) const
|
---|
[6b919f8] | 72 | {
|
---|
[735b1c] | 73 | *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
|
---|
[d760bb] | 74 | //LOG(2, "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << MaxOrder << ".");
|
---|
[6b919f8] | 75 | };
|
---|
| 76 |
|
---|
| 77 | /** Prints all bonds of this atom with total degree.
|
---|
| 78 | */
|
---|
[4b5cf8] | 79 | void BondedParticle::OutputBondOfAtom(std::ostream &ost) const
|
---|
[6b919f8] | 80 | {
|
---|
[9d83b6] | 81 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[4b5cf8] | 82 | ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: ";
|
---|
[e138de] | 83 | int TotalDegree = 0;
|
---|
| 84 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
|
---|
[4b5cf8] | 85 | ost << **Runner << "\t";
|
---|
[1f693d] | 86 | TotalDegree += (*Runner)->getDegree();
|
---|
[e138de] | 87 | }
|
---|
[4b5cf8] | 88 | ost << " -- TotalDegree: " << TotalDegree;
|
---|
[6b919f8] | 89 | };
|
---|
| 90 |
|
---|
[5309ba] | 91 | /** Output of atom::Nr along each bond partner per line.
|
---|
| 92 | * Only bonds are printed where atom::Nr is smaller than the one of the bond partner.
|
---|
[1f1b23] | 93 | * \param *AdjacencyFile output stream
|
---|
| 94 | */
|
---|
| 95 | void BondedParticle::OutputBonds(ofstream * const BondFile) const
|
---|
| 96 | {
|
---|
[9d83b6] | 97 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[1f1b23] | 98 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
---|
[735b1c] | 99 | if (getNr() < (*Runner)->GetOtherAtom(this)->getNr())
|
---|
[52ed5b] | 100 | *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n";
|
---|
[1f1b23] | 101 | };
|
---|
| 102 |
|
---|
[b8d4a3] | 103 | /**
|
---|
[db7e6d] | 104 | * Adds a bond between this bonded particle and another. Returns present instance if this
|
---|
[b8d4a3] | 105 | * bond already exists.
|
---|
| 106 | *
|
---|
[073a9e4] | 107 | * @param _step time step to access
|
---|
[db7e6d] | 108 | * @param bonding partner
|
---|
[d948b4] | 109 | * @return pointer to created bond or to already present bonds
|
---|
[b8d4a3] | 110 | */
|
---|
[d948b4] | 111 | bond::ptr BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner)
|
---|
[db7e6d] | 112 | {
|
---|
| 113 | const BondList &bondlist = getListOfBondsAtStep(_step);
|
---|
| 114 | for (BondList::const_iterator runner = bondlist.begin();
|
---|
| 115 | runner != bondlist.end();
|
---|
| 116 | runner++) {
|
---|
| 117 | if ((*runner)->Contains(Partner))
|
---|
| 118 | return *runner;
|
---|
[b8d4a3] | 119 | }
|
---|
| 120 |
|
---|
[7d82a5] | 121 | bond::ptr newBond(new bond((atom*) this, (atom*) Partner, 1));
|
---|
[073a9e4] | 122 | RegisterBond(_step, newBond);
|
---|
| 123 | Partner->RegisterBond(_step, newBond);
|
---|
[db7e6d] | 124 |
|
---|
| 125 | return newBond;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[d948b4] | 128 | /**
|
---|
| 129 | * Adds a bond between this bonded particle and another. Returns present instance if this
|
---|
| 130 | * bond already exists.
|
---|
| 131 | *
|
---|
| 132 | * @param bonding partner
|
---|
| 133 | * @return pointer to created bond or to already present bonds
|
---|
| 134 | */
|
---|
| 135 | bond::ptr BondedParticle::addBond(BondedParticle* Partner)
|
---|
| 136 | {
|
---|
[1f693d] | 137 | return addBond(WorldTime::getTime(), Partner);
|
---|
[d948b4] | 138 | }
|
---|
| 139 |
|
---|
[7d82a5] | 140 | /** Helper function to find the time step to a given bond in \a Binder.
|
---|
[f63e41] | 141 | *
|
---|
[7d82a5] | 142 | * \param Binder bond to look for
|
---|
| 143 | * \return ListOfBonds::size() - not found, else - step containing \a Binder
|
---|
[f63e41] | 144 | */
|
---|
[7d82a5] | 145 | unsigned int BondedParticle::findBondsStep(bond::ptr const Binder) const
|
---|
| 146 | {
|
---|
| 147 |
|
---|
| 148 | size_t _step = 0;
|
---|
| 149 | for (;_step < ListOfBonds.size();++_step) {
|
---|
| 150 | const BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
---|
| 151 | if (std::find(ListOfBonds.begin(), ListOfBonds.end(), Binder) != ListOfBonds.end())
|
---|
| 152 | break;
|
---|
| 153 | }
|
---|
| 154 | return _step;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /** Helper function to find the iterator to a bond at a given time \a step to
|
---|
| 158 | * a given bond partner in \a Partner.
|
---|
| 159 | *
|
---|
| 160 | * \param _step time step to look at
|
---|
| 161 | * \param Partner bond partner to look for
|
---|
| 162 | * \return ListOfBonds::end() - not found, else - iterator pointing \a Binder
|
---|
| 163 | */
|
---|
| 164 | BondList::const_iterator BondedParticle::findBondPartnerAtStep(
|
---|
| 165 | const unsigned int _step,
|
---|
| 166 | BondedParticle * const Partner) const
|
---|
[f63e41] | 167 | {
|
---|
| 168 | const BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
---|
| 169 | BondList::const_iterator iter = std::find_if(ListOfBonds.begin(), ListOfBonds.end(),
|
---|
| 170 | boost::bind(
|
---|
| 171 | static_cast<bool (bond::*)(const ParticleInfo * const) const>(&bond::Contains),
|
---|
| 172 | _1,
|
---|
| 173 | boost::cref(Partner)));
|
---|
[7d82a5] | 174 | return iter;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /** Removes a bond of this atom to a given \a Partner.
|
---|
| 178 | *
|
---|
| 179 | * @param _step time step
|
---|
| 180 | * @param Partner bond partner
|
---|
| 181 | */
|
---|
| 182 | void BondedParticle::removeBond(const unsigned int _step, BondedParticle * const Partner)
|
---|
| 183 | {
|
---|
| 184 | BondList::const_iterator iter = findBondPartnerAtStep(_step, Partner);
|
---|
| 185 | if (iter != getListOfBondsAtStep(_step).end()) {
|
---|
| 186 | // iter becomes invalid upon first unregister,
|
---|
| 187 | // hence store the bond someplace else first
|
---|
| 188 | bond::ptr const Binder = *iter;
|
---|
| 189 | UnregisterBond(_step, Binder);
|
---|
| 190 | Partner->UnregisterBond(_step, Binder);
|
---|
[f63e41] | 191 | } else
|
---|
| 192 | ELOG(1, "BondedParticle::removeBond() - I cannot find the bond in between "
|
---|
| 193 | +toString(getName())+" and "+toString(Partner->getName())+".");
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[d948b4] | 196 | /** Removes a bond of this atom to a given \a Partner.
|
---|
| 197 | *
|
---|
| 198 | * @param Partner bond partner
|
---|
| 199 | */
|
---|
| 200 | void BondedParticle::removeBond(BondedParticle * const Partner)
|
---|
| 201 | {
|
---|
| 202 | removeBond(WorldTime::getTime(), Partner);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[db7e6d] | 205 | /** Removes a bond for this atom.
|
---|
| 206 | *
|
---|
| 207 | * @param Binder bond to remove
|
---|
| 208 | */
|
---|
[7d82a5] | 209 | void BondedParticle::removeBond(bond::ptr &binder)
|
---|
[db7e6d] | 210 | {
|
---|
[7d82a5] | 211 | if (binder != NULL) {
|
---|
| 212 | atom * const Other = binder->GetOtherAtom(this);
|
---|
| 213 | ASSERT( Other != NULL,
|
---|
| 214 | "BondedParticle::removeBonds() - cannot find bond partner for "
|
---|
| 215 | +toString(*binder)+".");
|
---|
| 216 | // find bond at step
|
---|
| 217 | unsigned int step = findBondsStep(binder);
|
---|
| 218 | if (step != ListOfBonds.size()) {
|
---|
| 219 | UnregisterBond(step, binder);
|
---|
| 220 | Other->UnregisterBond(step, binder);
|
---|
| 221 | binder.reset();
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
[b8d4a3] | 224 | }
|
---|
| 225 |
|
---|
[8cc22f] | 226 | /** Removes all bonds in current timestep and their instances, too.
|
---|
[5e2f80] | 227 | *
|
---|
| 228 | */
|
---|
| 229 | void BondedParticle::removeAllBonds()
|
---|
| 230 | {
|
---|
[8cc22f] | 231 | removeAllBonds(WorldTime::getTime());
|
---|
[cc9119] | 232 | }
|
---|
| 233 |
|
---|
| 234 | /** Removes all bonds for a given \a _step and their instances, too.
|
---|
| 235 | *
|
---|
| 236 | * @param _step time step to access
|
---|
| 237 | */
|
---|
| 238 | void BondedParticle::removeAllBonds(const unsigned int _step)
|
---|
| 239 | {
|
---|
[7d82a5] | 240 | //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]);
|
---|
[8cc22f] | 241 | BondTrajectory_t::iterator listiter = ListOfBonds.find(_step);
|
---|
| 242 | if (listiter != ListOfBonds.end())
|
---|
| 243 | for (BondList::iterator iter = listiter->second.begin();
|
---|
| 244 | !listiter->second.empty();
|
---|
| 245 | iter = listiter->second.begin()) {
|
---|
| 246 | //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds);
|
---|
| 247 | atom * const Other = (*iter)->GetOtherAtom(this);
|
---|
| 248 | ASSERT( Other != NULL,
|
---|
| 249 | "BondedParticle::removeAllBonds() - cannot find bond partner for "
|
---|
| 250 | +toString(**iter)+".");
|
---|
| 251 | Other->UnregisterBond(_step, *iter);
|
---|
| 252 | UnregisterBond(_step, *iter);
|
---|
| 253 | }
|
---|
[5e2f80] | 254 | }
|
---|
| 255 |
|
---|
[6b919f8] | 256 | /** Puts a given bond into atom::ListOfBonds.
|
---|
[073a9e4] | 257 | * @param _step time step to access
|
---|
[6b919f8] | 258 | * \param *Binder bond to insert
|
---|
| 259 | */
|
---|
[88c8ec] | 260 | bool BondedParticle::RegisterBond(const unsigned int _step, bond::ptr const Binder)
|
---|
[6b919f8] | 261 | {
|
---|
| 262 | bool status = false;
|
---|
| 263 | if (Binder != NULL) {
|
---|
[7d82a5] | 264 | OBSERVE;
|
---|
[6b919f8] | 265 | if (Binder->Contains(this)) {
|
---|
[d557374] | 266 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
|
---|
[8cc22f] | 267 | std::pair< BondTrajectory_t::iterator, bool> inserter =
|
---|
| 268 | ListOfBonds.insert( std::make_pair( _step, BondList(1, Binder)) );
|
---|
| 269 | if (!inserter.second)
|
---|
| 270 | inserter.first->second.push_back(Binder);
|
---|
[74ec1f] | 271 | if (WorldTime::getTime() == _step)
|
---|
[2ad1ec] | 272 | NOTIFY(AtomObservable::BondsAdded);
|
---|
[6b919f8] | 273 | status = true;
|
---|
| 274 | } else {
|
---|
[47d041] | 275 | ELOG(1, *Binder << " does not contain " << *this << ".");
|
---|
[6b919f8] | 276 | }
|
---|
| 277 | } else {
|
---|
[47d041] | 278 | ELOG(1, "Binder is " << Binder << ".");
|
---|
[6b919f8] | 279 | }
|
---|
| 280 | return status;
|
---|
| 281 | };
|
---|
| 282 |
|
---|
| 283 | /** Removes a given bond from atom::ListOfBonds.
|
---|
[7d82a5] | 284 | *
|
---|
| 285 | * \warning This only removes this atom not its bond partner, i.e.
|
---|
| 286 | * both atoms need to call this function to fully empty a bond.
|
---|
| 287 | *
|
---|
[9d83b6] | 288 | * @param _step time step to access
|
---|
[6b919f8] | 289 | * \param *Binder bond to remove
|
---|
| 290 | */
|
---|
[7d82a5] | 291 | bool BondedParticle::UnregisterBond(const unsigned int _step, bond::ptr const Binder)
|
---|
[6b919f8] | 292 | {
|
---|
| 293 | bool status = false;
|
---|
[7d82a5] | 294 | if (Binder != NULL) {
|
---|
| 295 | if (Binder->Contains(this)) {
|
---|
| 296 | OBSERVE;
|
---|
| 297 | //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
|
---|
[8cc22f] | 298 | BondTrajectory_t::iterator listiter = ListOfBonds.find(_step);
|
---|
| 299 | if (listiter != ListOfBonds.end()) {
|
---|
[7d82a5] | 300 | #ifndef NDEBUG
|
---|
[8cc22f] | 301 | BondList::const_iterator iter =
|
---|
| 302 | std::find(listiter->second.begin(), listiter->second.end(), Binder);
|
---|
| 303 | ASSERT( iter != listiter->second.end(),
|
---|
| 304 | "BondedParticle::UnregisterBond() - "+toString(*Binder)+" not contained at "
|
---|
| 305 | +toString(_step));
|
---|
[7d82a5] | 306 | #endif
|
---|
[8cc22f] | 307 | Binder->removeAtom(this);
|
---|
| 308 | listiter->second.remove(Binder);
|
---|
| 309 | if (WorldTime::getTime() == _step)
|
---|
| 310 | NOTIFY(AtomObservable::BondsRemoved);
|
---|
| 311 | status = true;
|
---|
| 312 | }
|
---|
[7d82a5] | 313 | } else {
|
---|
| 314 | ELOG(1, *Binder << " does not contain " << *this << ".");
|
---|
| 315 | }
|
---|
[6b919f8] | 316 | } else {
|
---|
[7d82a5] | 317 | ELOG(1, "Binder is " << Binder << ".");
|
---|
[6b919f8] | 318 | }
|
---|
| 319 | return status;
|
---|
| 320 | };
|
---|
| 321 |
|
---|
[583081] | 322 | /** Removes all bonds of given \a _step with freeing memory.
|
---|
| 323 | *
|
---|
| 324 | * @param _step time step whose bonds to free
|
---|
| 325 | */
|
---|
| 326 | void BondedParticle::ClearBondsAtStep(const unsigned int _step)
|
---|
| 327 | {
|
---|
[7d82a5] | 328 | removeAllBonds(_step);
|
---|
[583081] | 329 | }
|
---|
| 330 |
|
---|
[93c6e9] | 331 | /** Searches for the time step where the given bond \a *Binder is a bond of this particle.
|
---|
| 332 | *
|
---|
| 333 | * @param Binder bond to check
|
---|
| 334 | * @return >=0 - first time step where bond appears, -1 - bond not present in lists
|
---|
| 335 | */
|
---|
[88c8ec] | 336 | int BondedParticle::ContainsBondAtStep(bond::ptr Binder) const
|
---|
[93c6e9] | 337 | {
|
---|
| 338 | int step = -1;
|
---|
[8cc22f] | 339 | for(BondTrajectory_t::const_iterator listiter = ListOfBonds.begin();
|
---|
| 340 | listiter != ListOfBonds.end();
|
---|
| 341 | ++listiter) {
|
---|
| 342 | for (BondList::const_iterator bonditer = listiter->second.begin();
|
---|
| 343 | bonditer != listiter->second.end();
|
---|
[93c6e9] | 344 | ++bonditer) {
|
---|
| 345 | if ((*bonditer) == Binder) {
|
---|
[8cc22f] | 346 | step = listiter->first;
|
---|
[93c6e9] | 347 | break;
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 | if (step != -1)
|
---|
| 351 | break;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | return step;
|
---|
| 355 | }
|
---|
| 356 |
|
---|
[c0d9eb] | 357 | /** Counts the number of bonds weighted by bond::BondDegree.
|
---|
| 358 | * @param _step time step to access
|
---|
| 359 | * \param bonds times bond::BondDegree
|
---|
| 360 | */
|
---|
| 361 | int BondedParticle::CountBonds() const
|
---|
| 362 | {
|
---|
| 363 | int NoBonds = 0;
|
---|
[9d83b6] | 364 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[c0d9eb] | 365 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 366 | Runner != ListOfBonds.end();
|
---|
| 367 | (++Runner))
|
---|
[1f693d] | 368 | NoBonds += (*Runner)->getDegree();
|
---|
[c0d9eb] | 369 | return NoBonds;
|
---|
| 370 | };
|
---|
| 371 |
|
---|
[b70721] | 372 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
|
---|
[073a9e4] | 373 | * @param _step time step to access
|
---|
[b70721] | 374 | * \param *BondPartner atom to check for
|
---|
| 375 | * \return true - bond exists, false - bond does not exist
|
---|
| 376 | */
|
---|
[073a9e4] | 377 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
|
---|
[b70721] | 378 | {
|
---|
| 379 | bool status = false;
|
---|
| 380 |
|
---|
[073a9e4] | 381 | const BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
---|
| 382 | for (BondList::const_iterator runner = ListOfBonds.begin();
|
---|
| 383 | runner != ListOfBonds.end();
|
---|
| 384 | runner++) {
|
---|
[b70721] | 385 | status = status || ((*runner)->Contains(BondPartner));
|
---|
| 386 | }
|
---|
| 387 | return status;
|
---|
| 388 | };
|
---|
| 389 |
|
---|
[d74077] | 390 | std::ostream & BondedParticle::operator << (std::ostream &ost) const
|
---|
| 391 | {
|
---|
| 392 | ParticleInfo::operator<<(ost);
|
---|
| 393 | ost << "," << getPosition();
|
---|
| 394 | return ost;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | std::ostream & operator << (std::ostream &ost, const BondedParticle &a)
|
---|
| 398 | {
|
---|
| 399 | a.ParticleInfo::operator<<(ost);
|
---|
| 400 | ost << "," << a.getPosition();
|
---|
| 401 | return ost;
|
---|
| 402 | }
|
---|
| 403 |
|
---|