| 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 with all bond partners.
 | 
|---|
| 84 |  * \param *AdjacencyFile output stream
 | 
|---|
| 85 |  */
 | 
|---|
| 86 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
 | 
|---|
| 87 | {
 | 
|---|
| 88 |   const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| 89 |   *AdjacencyFile << getNr() << "\t";
 | 
|---|
| 90 |   for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
 | 
|---|
| 91 |     *AdjacencyFile << (*Runner)->GetOtherAtom(this)->getNr() << "\t";
 | 
|---|
| 92 |   *AdjacencyFile << endl;
 | 
|---|
| 93 | };
 | 
|---|
| 94 | 
 | 
|---|
| 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.
 | 
|---|
| 97 |  * \param *AdjacencyFile output stream
 | 
|---|
| 98 |  */
 | 
|---|
| 99 | void BondedParticle::OutputBonds(ofstream * const BondFile) const
 | 
|---|
| 100 | {
 | 
|---|
| 101 |   const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| 102 |   for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
 | 
|---|
| 103 |     if (getNr() < (*Runner)->GetOtherAtom(this)->getNr())
 | 
|---|
| 104 |       *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n";
 | 
|---|
| 105 | };
 | 
|---|
| 106 | 
 | 
|---|
| 107 | /**
 | 
|---|
| 108 |  * Adds a bond between this bonded particle and another. Returns present instance if this
 | 
|---|
| 109 |  * bond already exists.
 | 
|---|
| 110 |  *
 | 
|---|
| 111 |  * @param _step time step to access
 | 
|---|
| 112 |  * @param bonding partner
 | 
|---|
| 113 |  * @return const pointer to created bond or to already present bonds
 | 
|---|
| 114 |  */
 | 
|---|
| 115 | bond * const BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner)
 | 
|---|
| 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;
 | 
|---|
| 123 |   }
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   bond* newBond = new bond((atom*) this, (atom*) Partner, 1);
 | 
|---|
| 126 |   RegisterBond(_step, newBond);
 | 
|---|
| 127 |   Partner->RegisterBond(_step, newBond);
 | 
|---|
| 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);
 | 
|---|
| 139 | }
 | 
|---|
| 140 | 
 | 
|---|
| 141 | /** Removes all bonds in all timesteps and their instances, too.
 | 
|---|
| 142 |  *
 | 
|---|
| 143 |  */
 | 
|---|
| 144 | void BondedParticle::removeAllBonds()
 | 
|---|
| 145 | {
 | 
|---|
| 146 |   for (size_t index = 0; index < ListOfBonds.size(); ++index)
 | 
|---|
| 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()
 | 
|---|
| 161 |   }
 | 
|---|
| 162 | }
 | 
|---|
| 163 | 
 | 
|---|
| 164 | /** Puts a given bond into atom::ListOfBonds.
 | 
|---|
| 165 |  * @param _step time step to access
 | 
|---|
| 166 |  * \param *Binder bond to insert
 | 
|---|
| 167 |  */
 | 
|---|
| 168 | bool BondedParticle::RegisterBond(const unsigned int _step, bond * const Binder)
 | 
|---|
| 169 | {
 | 
|---|
| 170 |   OBSERVE;
 | 
|---|
| 171 |   bool status = false;
 | 
|---|
| 172 |   if (Binder != NULL) {
 | 
|---|
| 173 |     if (Binder->Contains(this)) {
 | 
|---|
| 174 |       //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
 | 
|---|
| 175 |       if (ListOfBonds.size() <= _step)
 | 
|---|
| 176 |         ListOfBonds.resize(_step+1);
 | 
|---|
| 177 |       ListOfBonds[_step].push_back(Binder);
 | 
|---|
| 178 |       if (WorldTime::getTime() == _step)
 | 
|---|
| 179 |         NOTIFY(AtomObservable::BondsAdded);
 | 
|---|
| 180 |       status = true;
 | 
|---|
| 181 |     } else {
 | 
|---|
| 182 |       ELOG(1, *Binder << " does not contain " << *this << ".");
 | 
|---|
| 183 |     }
 | 
|---|
| 184 |   } else {
 | 
|---|
| 185 |     ELOG(1, "Binder is " << Binder << ".");
 | 
|---|
| 186 |   }
 | 
|---|
| 187 |   return status;
 | 
|---|
| 188 | };
 | 
|---|
| 189 | 
 | 
|---|
| 190 | /** Removes a given bond from atom::ListOfBonds.
 | 
|---|
| 191 |  * @param _step time step to access
 | 
|---|
| 192 |  * \param *Binder bond to remove
 | 
|---|
| 193 |  */
 | 
|---|
| 194 | bool BondedParticle::UnregisterBond(bond * const Binder)
 | 
|---|
| 195 | {
 | 
|---|
| 196 |   OBSERVE;
 | 
|---|
| 197 |   bool status = false;
 | 
|---|
| 198 |   ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL.");
 | 
|---|
| 199 |   const int step = ContainsBondAtStep(Binder);
 | 
|---|
| 200 |   if (step != -1) {
 | 
|---|
| 201 |     //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
 | 
|---|
| 202 |     ListOfBonds[step].remove(Binder);
 | 
|---|
| 203 |     if (WorldTime::getTime() == step)
 | 
|---|
| 204 |       NOTIFY(AtomObservable::BondsRemoved);
 | 
|---|
| 205 |     status = true;
 | 
|---|
| 206 |   } else {
 | 
|---|
| 207 |     ELOG(1, *Binder << " does not contain " << *this << ".");
 | 
|---|
| 208 |   }
 | 
|---|
| 209 |   return status;
 | 
|---|
| 210 | };
 | 
|---|
| 211 | 
 | 
|---|
| 212 | /** Removes all bonds from atom::ListOfBonds.
 | 
|---|
| 213 |  * \note Does not do any memory de-allocation.
 | 
|---|
| 214 |  */
 | 
|---|
| 215 | void BondedParticle::UnregisterAllBond(const unsigned int _step)
 | 
|---|
| 216 | {
 | 
|---|
| 217 |   OBSERVE;
 | 
|---|
| 218 |   NOTIFY(AtomObservable::BondsRemoved);
 | 
|---|
| 219 |   ListOfBonds[_step].clear();
 | 
|---|
| 220 | }
 | 
|---|
| 221 | 
 | 
|---|
| 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 | {
 | 
|---|
| 228 |   OBSERVE;
 | 
|---|
| 229 |   NOTIFY(AtomObservable::BondsRemoved);
 | 
|---|
| 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 | 
 | 
|---|
| 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 |  */
 | 
|---|
| 244 | int BondedParticle::ContainsBondAtStep(bond *Binder) const
 | 
|---|
| 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 | 
 | 
|---|
| 266 | /** Corrects the bond degree by one at most if necessary.
 | 
|---|
| 267 |  * \return number of corrections done
 | 
|---|
| 268 |  */
 | 
|---|
| 269 | int BondedParticle::CorrectBondDegree()
 | 
|---|
| 270 | {
 | 
|---|
| 271 |   OBSERVE;
 | 
|---|
| 272 |   NOTIFY(AtomObservable::BondDegreeChanged);
 | 
|---|
| 273 |   int NoBonds = 0;
 | 
|---|
| 274 |   int OtherNoBonds = 0;
 | 
|---|
| 275 |   int FalseBondDegree = 0;
 | 
|---|
| 276 |   atom *OtherWalker = NULL;
 | 
|---|
| 277 |   bond *CandidateBond = NULL;
 | 
|---|
| 278 | 
 | 
|---|
| 279 |   NoBonds = CountBonds();
 | 
|---|
| 280 |   //LOG(3, "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?");
 | 
|---|
| 281 |   if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
 | 
|---|
| 282 |     const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| 283 |     for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
 | 
|---|
| 284 |       OtherWalker = (*Runner)->GetOtherAtom(this);
 | 
|---|
| 285 |       OtherNoBonds = OtherWalker->CountBonds();
 | 
|---|
| 286 |       //LOG(3, "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?");
 | 
|---|
| 287 |       if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate
 | 
|---|
| 288 |         const BondList& OtherListOfBonds = OtherWalker->getListOfBonds();
 | 
|---|
| 289 |         if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first
 | 
|---|
| 290 |           CandidateBond = (*Runner);
 | 
|---|
| 291 |           //LOG(3, "New candidate is " << *CandidateBond << ".");
 | 
|---|
| 292 |         }
 | 
|---|
| 293 |       }
 | 
|---|
| 294 |     }
 | 
|---|
| 295 |     if ((CandidateBond != NULL)) {
 | 
|---|
| 296 |       CandidateBond->BondDegree++;
 | 
|---|
| 297 |       //LOG(2, "Increased bond degree for bond " << *CandidateBond << ".");
 | 
|---|
| 298 |     } else {
 | 
|---|
| 299 |       ELOG(2, "Could not find correct degree for atom " << *this << ".");
 | 
|---|
| 300 |       FalseBondDegree++;
 | 
|---|
| 301 |     }
 | 
|---|
| 302 |   }
 | 
|---|
| 303 |   return FalseBondDegree;
 | 
|---|
| 304 | };
 | 
|---|
| 305 | 
 | 
|---|
| 306 | /** Sets the weight of all connected bonds to one.
 | 
|---|
| 307 |  */
 | 
|---|
| 308 | void BondedParticle::resetBondDegree()
 | 
|---|
| 309 | {
 | 
|---|
| 310 |   OBSERVE;
 | 
|---|
| 311 |   NOTIFY(BondedParticle::BondDegreeChanged);
 | 
|---|
| 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 | 
 | 
|---|
| 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;
 | 
|---|
| 328 |   const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| 329 |   for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
| 330 |       Runner != ListOfBonds.end();
 | 
|---|
| 331 |       (++Runner))
 | 
|---|
| 332 |     NoBonds += (*Runner)->BondDegree;
 | 
|---|
| 333 |   return NoBonds;
 | 
|---|
| 334 | };
 | 
|---|
| 335 | 
 | 
|---|
| 336 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
 | 
|---|
| 337 |  * @param _step time step to access
 | 
|---|
| 338 |  * \param *BondPartner atom to check for
 | 
|---|
| 339 |  * \return true - bond exists, false - bond does not exist
 | 
|---|
| 340 |  */
 | 
|---|
| 341 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
 | 
|---|
| 342 | {
 | 
|---|
| 343 |   bool status = false;
 | 
|---|
| 344 | 
 | 
|---|
| 345 |   const BondList& ListOfBonds = getListOfBondsAtStep(_step);
 | 
|---|
| 346 |   for (BondList::const_iterator runner = ListOfBonds.begin();
 | 
|---|
| 347 |       runner != ListOfBonds.end();
 | 
|---|
| 348 |       runner++) {
 | 
|---|
| 349 |     status = status || ((*runner)->Contains(BondPartner));
 | 
|---|
| 350 |   }
 | 
|---|
| 351 |   return status;
 | 
|---|
| 352 | };
 | 
|---|
| 353 | 
 | 
|---|
| 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 | 
 | 
|---|