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