[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[bcf653] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[6b919f8] | 8 | /*
|
---|
| 9 | * atom_bondedparticle.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Oct 19, 2009
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 21 |
|
---|
[6b919f8] | 22 | #include "atom.hpp"
|
---|
| 23 | #include "atom_bondedparticle.hpp"
|
---|
[129204] | 24 | #include "Bond/bond.hpp"
|
---|
[d557374] | 25 | #include "CodePatterns/Assert.hpp"
|
---|
[ad011c] | 26 | #include "CodePatterns/Log.hpp"
|
---|
| 27 | #include "CodePatterns/Verbose.hpp"
|
---|
[3bdb6d] | 28 | #include "Element/element.hpp"
|
---|
[db7e6d] | 29 | #include "WorldTime.hpp"
|
---|
[6b919f8] | 30 |
|
---|
| 31 | /** Constructor of class BondedParticle.
|
---|
| 32 | */
|
---|
[70ff32] | 33 | BondedParticle::BondedParticle()
|
---|
| 34 | {
|
---|
[9d83b6] | 35 | ListOfBonds.push_back(BondList());
|
---|
[70ff32] | 36 | };
|
---|
[6b919f8] | 37 |
|
---|
| 38 | /** Destructor of class BondedParticle.
|
---|
| 39 | */
|
---|
| 40 | BondedParticle::~BondedParticle()
|
---|
| 41 | {
|
---|
[5e2f80] | 42 | removeAllBonds();
|
---|
[6b919f8] | 43 | };
|
---|
| 44 |
|
---|
| 45 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
|
---|
| 46 | * \param *file output stream
|
---|
| 47 | */
|
---|
[b453f9] | 48 | void BondedParticle::OutputOrder(ofstream *file) const
|
---|
[6b919f8] | 49 | {
|
---|
[735b1c] | 50 | *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
|
---|
[47d041] | 51 | //LOG(2, "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << ".");
|
---|
[6b919f8] | 52 | };
|
---|
| 53 |
|
---|
| 54 | /** Prints all bonds of this atom with total degree.
|
---|
| 55 | */
|
---|
[4b5cf8] | 56 | void BondedParticle::OutputBondOfAtom(std::ostream &ost) const
|
---|
[6b919f8] | 57 | {
|
---|
[9d83b6] | 58 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[4b5cf8] | 59 | ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: ";
|
---|
[e138de] | 60 | int TotalDegree = 0;
|
---|
| 61 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
|
---|
[4b5cf8] | 62 | ost << **Runner << "\t";
|
---|
[e138de] | 63 | TotalDegree += (*Runner)->BondDegree;
|
---|
| 64 | }
|
---|
[4b5cf8] | 65 | ost << " -- TotalDegree: " << TotalDegree;
|
---|
[6b919f8] | 66 | };
|
---|
| 67 |
|
---|
[5309ba] | 68 | /** Output of atom::Nr along with all bond partners.
|
---|
[6b919f8] | 69 | * \param *AdjacencyFile output stream
|
---|
| 70 | */
|
---|
[1f1b23] | 71 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
|
---|
[6b919f8] | 72 | {
|
---|
[9d83b6] | 73 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[52ed5b] | 74 | *AdjacencyFile << getNr() << "\t";
|
---|
[6b919f8] | 75 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
---|
[52ed5b] | 76 | *AdjacencyFile << (*Runner)->GetOtherAtom(this)->getNr() << "\t";
|
---|
[6b919f8] | 77 | *AdjacencyFile << endl;
|
---|
| 78 | };
|
---|
| 79 |
|
---|
[5309ba] | 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.
|
---|
[1f1b23] | 82 | * \param *AdjacencyFile output stream
|
---|
| 83 | */
|
---|
| 84 | void BondedParticle::OutputBonds(ofstream * const BondFile) const
|
---|
| 85 | {
|
---|
[9d83b6] | 86 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[1f1b23] | 87 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
---|
[735b1c] | 88 | if (getNr() < (*Runner)->GetOtherAtom(this)->getNr())
|
---|
[52ed5b] | 89 | *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n";
|
---|
[1f1b23] | 90 | };
|
---|
| 91 |
|
---|
[b8d4a3] | 92 | /**
|
---|
[db7e6d] | 93 | * Adds a bond between this bonded particle and another. Returns present instance if this
|
---|
[b8d4a3] | 94 | * bond already exists.
|
---|
| 95 | *
|
---|
[073a9e4] | 96 | * @param _step time step to access
|
---|
[db7e6d] | 97 | * @param bonding partner
|
---|
[05a885] | 98 | * @return const pointer to created bond or to already present bonds
|
---|
[b8d4a3] | 99 | */
|
---|
[05a885] | 100 | bond * const BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner)
|
---|
[db7e6d] | 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;
|
---|
[b8d4a3] | 108 | }
|
---|
| 109 |
|
---|
[efe516] | 110 | bond* newBond = new bond((atom*) this, (atom*) Partner, 1);
|
---|
[073a9e4] | 111 | RegisterBond(_step, newBond);
|
---|
| 112 | Partner->RegisterBond(_step, newBond);
|
---|
[db7e6d] | 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);
|
---|
[b8d4a3] | 124 | }
|
---|
| 125 |
|
---|
[cc9119] | 126 | /** Removes all bonds in all timesteps and their instances, too.
|
---|
[5e2f80] | 127 | *
|
---|
| 128 | */
|
---|
| 129 | void BondedParticle::removeAllBonds()
|
---|
| 130 | {
|
---|
| 131 | for (size_t index = 0; index < ListOfBonds.size(); ++index)
|
---|
[cc9119] | 132 | removeAllBonds(index);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /** Removes all bonds for a given \a _step and their instances, too.
|
---|
| 136 | *
|
---|
| 137 | * @param _step time step to access
|
---|
| 138 | */
|
---|
| 139 | void BondedParticle::removeAllBonds(const unsigned int _step)
|
---|
| 140 | {
|
---|
| 141 | for (BondList::iterator iter = ListOfBonds[_step].begin();
|
---|
| 142 | !ListOfBonds[_step].empty();
|
---|
| 143 | iter = ListOfBonds[_step].begin()) {
|
---|
| 144 | delete (*iter);
|
---|
| 145 | // unregister/NOTIFY is done by bond::~bond()
|
---|
[5e2f80] | 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[6b919f8] | 149 | /** Puts a given bond into atom::ListOfBonds.
|
---|
[073a9e4] | 150 | * @param _step time step to access
|
---|
[6b919f8] | 151 | * \param *Binder bond to insert
|
---|
| 152 | */
|
---|
[05a885] | 153 | bool BondedParticle::RegisterBond(const unsigned int _step, bond * const Binder)
|
---|
[6b919f8] | 154 | {
|
---|
[db7e6d] | 155 | OBSERVE;
|
---|
[6b919f8] | 156 | bool status = false;
|
---|
| 157 | if (Binder != NULL) {
|
---|
| 158 | if (Binder->Contains(this)) {
|
---|
[d557374] | 159 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
|
---|
[5e2f80] | 160 | if (ListOfBonds.size() <= _step)
|
---|
| 161 | ListOfBonds.resize(_step+1);
|
---|
| 162 | ListOfBonds[_step].push_back(Binder);
|
---|
[74ec1f] | 163 | if (WorldTime::getTime() == _step)
|
---|
[2ad1ec] | 164 | NOTIFY(AtomObservable::BondsAdded);
|
---|
[6b919f8] | 165 | status = true;
|
---|
| 166 | } else {
|
---|
[47d041] | 167 | ELOG(1, *Binder << " does not contain " << *this << ".");
|
---|
[6b919f8] | 168 | }
|
---|
| 169 | } else {
|
---|
[47d041] | 170 | ELOG(1, "Binder is " << Binder << ".");
|
---|
[6b919f8] | 171 | }
|
---|
| 172 | return status;
|
---|
| 173 | };
|
---|
| 174 |
|
---|
| 175 | /** Removes a given bond from atom::ListOfBonds.
|
---|
[9d83b6] | 176 | * @param _step time step to access
|
---|
[6b919f8] | 177 | * \param *Binder bond to remove
|
---|
| 178 | */
|
---|
[05a885] | 179 | bool BondedParticle::UnregisterBond(bond * const Binder)
|
---|
[6b919f8] | 180 | {
|
---|
[db7e6d] | 181 | OBSERVE;
|
---|
[6b919f8] | 182 | bool status = false;
|
---|
[d557374] | 183 | ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL.");
|
---|
| 184 | const int step = ContainsBondAtStep(Binder);
|
---|
| 185 | if (step != -1) {
|
---|
[5e2f80] | 186 | //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
|
---|
[d557374] | 187 | ListOfBonds[step].remove(Binder);
|
---|
[760c4c] | 188 | if (WorldTime::getTime() == step)
|
---|
| 189 | NOTIFY(AtomObservable::BondsRemoved);
|
---|
[d557374] | 190 | status = true;
|
---|
[6b919f8] | 191 | } else {
|
---|
[47d041] | 192 | ELOG(1, *Binder << " does not contain " << *this << ".");
|
---|
[6b919f8] | 193 | }
|
---|
| 194 | return status;
|
---|
| 195 | };
|
---|
| 196 |
|
---|
| 197 | /** Removes all bonds from atom::ListOfBonds.
|
---|
| 198 | * \note Does not do any memory de-allocation.
|
---|
| 199 | */
|
---|
[a2bdbe] | 200 | void BondedParticle::UnregisterAllBond(const unsigned int _step)
|
---|
[6b919f8] | 201 | {
|
---|
[74ec1f] | 202 | OBSERVE;
|
---|
[760c4c] | 203 | NOTIFY(AtomObservable::BondsRemoved);
|
---|
[af897f] | 204 | ListOfBonds[_step].clear();
|
---|
| 205 | }
|
---|
[6b919f8] | 206 |
|
---|
[583081] | 207 | /** Removes all bonds of given \a _step with freeing memory.
|
---|
| 208 | *
|
---|
| 209 | * @param _step time step whose bonds to free
|
---|
| 210 | */
|
---|
| 211 | void BondedParticle::ClearBondsAtStep(const unsigned int _step)
|
---|
| 212 | {
|
---|
[db7e6d] | 213 | OBSERVE;
|
---|
[760c4c] | 214 | NOTIFY(AtomObservable::BondsRemoved);
|
---|
[583081] | 215 | //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]);
|
---|
| 216 | for (BondList::iterator iter = (ListOfBonds[_step]).begin();
|
---|
| 217 | !(ListOfBonds[_step]).empty();
|
---|
| 218 | iter = (ListOfBonds[_step]).begin()) {
|
---|
| 219 | //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds);
|
---|
| 220 | delete((*iter)); // will also unregister with us and remove from list
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[93c6e9] | 224 | /** Searches for the time step where the given bond \a *Binder is a bond of this particle.
|
---|
| 225 | *
|
---|
| 226 | * @param Binder bond to check
|
---|
| 227 | * @return >=0 - first time step where bond appears, -1 - bond not present in lists
|
---|
| 228 | */
|
---|
[db7e6d] | 229 | int BondedParticle::ContainsBondAtStep(bond *Binder) const
|
---|
[93c6e9] | 230 | {
|
---|
| 231 | int step = -1;
|
---|
| 232 | int tempstep = 0;
|
---|
| 233 | for(std::vector<BondList>::const_iterator iter = ListOfBonds.begin();
|
---|
| 234 | iter != ListOfBonds.end();
|
---|
| 235 | ++iter,++tempstep) {
|
---|
| 236 | for (BondList::const_iterator bonditer = iter->begin();
|
---|
| 237 | bonditer != iter->end();
|
---|
| 238 | ++bonditer) {
|
---|
| 239 | if ((*bonditer) == Binder) {
|
---|
| 240 | step = tempstep;
|
---|
| 241 | break;
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 | if (step != -1)
|
---|
| 245 | break;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | return step;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[6b919f8] | 251 | /** Corrects the bond degree by one at most if necessary.
|
---|
[93c6e9] | 252 | * \return number of corrections done
|
---|
[6b919f8] | 253 | */
|
---|
[e138de] | 254 | int BondedParticle::CorrectBondDegree()
|
---|
[6b919f8] | 255 | {
|
---|
[db7e6d] | 256 | OBSERVE;
|
---|
[74ec1f] | 257 | NOTIFY(AtomObservable::BondDegreeChanged);
|
---|
[6b919f8] | 258 | int NoBonds = 0;
|
---|
| 259 | int OtherNoBonds = 0;
|
---|
| 260 | int FalseBondDegree = 0;
|
---|
| 261 | atom *OtherWalker = NULL;
|
---|
| 262 | bond *CandidateBond = NULL;
|
---|
| 263 |
|
---|
| 264 | NoBonds = CountBonds();
|
---|
[47d041] | 265 | //LOG(3, "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?");
|
---|
[83f176] | 266 | if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
|
---|
[9d83b6] | 267 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[6b919f8] | 268 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
|
---|
| 269 | OtherWalker = (*Runner)->GetOtherAtom(this);
|
---|
| 270 | OtherNoBonds = OtherWalker->CountBonds();
|
---|
[47d041] | 271 | //LOG(3, "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?");
|
---|
[83f176] | 272 | if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate
|
---|
[9d83b6] | 273 | const BondList& OtherListOfBonds = OtherWalker->getListOfBonds();
|
---|
| 274 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first
|
---|
[6b919f8] | 275 | CandidateBond = (*Runner);
|
---|
[47d041] | 276 | //LOG(3, "New candidate is " << *CandidateBond << ".");
|
---|
[6b919f8] | 277 | }
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | if ((CandidateBond != NULL)) {
|
---|
| 281 | CandidateBond->BondDegree++;
|
---|
[47d041] | 282 | //LOG(2, "Increased bond degree for bond " << *CandidateBond << ".");
|
---|
[6b919f8] | 283 | } else {
|
---|
[47d041] | 284 | ELOG(2, "Could not find correct degree for atom " << *this << ".");
|
---|
[6b919f8] | 285 | FalseBondDegree++;
|
---|
| 286 | }
|
---|
| 287 | }
|
---|
| 288 | return FalseBondDegree;
|
---|
| 289 | };
|
---|
| 290 |
|
---|
[5e2f80] | 291 | /** Sets the weight of all connected bonds to one.
|
---|
| 292 | */
|
---|
| 293 | void BondedParticle::resetBondDegree()
|
---|
| 294 | {
|
---|
| 295 | OBSERVE;
|
---|
[74ec1f] | 296 | NOTIFY(BondedParticle::BondDegreeChanged);
|
---|
[5e2f80] | 297 | for (std::vector<BondList>::iterator Runner = ListOfBonds.begin();
|
---|
| 298 | Runner != ListOfBonds.end();
|
---|
| 299 | ++Runner)
|
---|
| 300 | for (BondList::iterator BondRunner = (*Runner).begin();
|
---|
| 301 | BondRunner != (*Runner).end();
|
---|
| 302 | ++BondRunner)
|
---|
| 303 | (*BondRunner)->BondDegree = 1;
|
---|
| 304 | };
|
---|
| 305 |
|
---|
[c0d9eb] | 306 | /** Counts the number of bonds weighted by bond::BondDegree.
|
---|
| 307 | * @param _step time step to access
|
---|
| 308 | * \param bonds times bond::BondDegree
|
---|
| 309 | */
|
---|
| 310 | int BondedParticle::CountBonds() const
|
---|
| 311 | {
|
---|
| 312 | int NoBonds = 0;
|
---|
[9d83b6] | 313 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[c0d9eb] | 314 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 315 | Runner != ListOfBonds.end();
|
---|
| 316 | (++Runner))
|
---|
| 317 | NoBonds += (*Runner)->BondDegree;
|
---|
| 318 | return NoBonds;
|
---|
| 319 | };
|
---|
| 320 |
|
---|
[b70721] | 321 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
|
---|
[073a9e4] | 322 | * @param _step time step to access
|
---|
[b70721] | 323 | * \param *BondPartner atom to check for
|
---|
| 324 | * \return true - bond exists, false - bond does not exist
|
---|
| 325 | */
|
---|
[073a9e4] | 326 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
|
---|
[b70721] | 327 | {
|
---|
| 328 | bool status = false;
|
---|
| 329 |
|
---|
[073a9e4] | 330 | const BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
---|
| 331 | for (BondList::const_iterator runner = ListOfBonds.begin();
|
---|
| 332 | runner != ListOfBonds.end();
|
---|
| 333 | runner++) {
|
---|
[b70721] | 334 | status = status || ((*runner)->Contains(BondPartner));
|
---|
| 335 | }
|
---|
| 336 | return status;
|
---|
| 337 | };
|
---|
| 338 |
|
---|
[d74077] | 339 | std::ostream & BondedParticle::operator << (std::ostream &ost) const
|
---|
| 340 | {
|
---|
| 341 | ParticleInfo::operator<<(ost);
|
---|
| 342 | ost << "," << getPosition();
|
---|
| 343 | return ost;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | std::ostream & operator << (std::ostream &ost, const BondedParticle &a)
|
---|
| 347 | {
|
---|
| 348 | a.ParticleInfo::operator<<(ost);
|
---|
| 349 | ost << "," << a.getPosition();
|
---|
| 350 | return ost;
|
---|
| 351 | }
|
---|
| 352 |
|
---|