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