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