| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 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"
 | 
|---|
 | 24 | #include "bond.hpp"
 | 
|---|
| [d557374] | 25 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [ad011c] | 26 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 27 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [d557374] | 28 | #include "element.hpp"
 | 
|---|
 | 29 | #include "lists.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 | {
 | 
|---|
| [9d83b6] | 42 |   BondList::iterator Runner;
 | 
|---|
 | 43 |   for (std::vector<BondList>::iterator iter = ListOfBonds.begin();
 | 
|---|
 | 44 |       !ListOfBonds.empty();
 | 
|---|
 | 45 |       iter = ListOfBonds.begin()) {
 | 
|---|
 | 46 |     while (!(*iter).empty()) {
 | 
|---|
 | 47 |       Runner = (*iter).begin();
 | 
|---|
 | 48 |       removewithoutcheck(*Runner);
 | 
|---|
 | 49 |     }
 | 
|---|
 | 50 |     ListOfBonds.erase(iter);
 | 
|---|
| [6b919f8] | 51 |   }
 | 
|---|
 | 52 | };
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
 | 
|---|
 | 55 |  * \param *file output stream
 | 
|---|
 | 56 |  */
 | 
|---|
| [b453f9] | 57 | void BondedParticle::OutputOrder(ofstream *file) const
 | 
|---|
| [6b919f8] | 58 | {
 | 
|---|
 | 59 |   *file << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
 | 
|---|
| [e138de] | 60 |   //Log() << Verbose(2) << "Storing: " << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl;
 | 
|---|
| [6b919f8] | 61 | };
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | /** Prints all bonds of this atom with total degree.
 | 
|---|
 | 64 |  */
 | 
|---|
| [e138de] | 65 | void BondedParticle::OutputBondOfAtom() const
 | 
|---|
| [6b919f8] | 66 | {
 | 
|---|
| [9d83b6] | 67 |   const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| [68f03d] | 68 |   DoLog(4) && (Log() << Verbose(4) << "Atom " << getName() << "/" << nr << " with " << ListOfBonds.size() << " bonds: " << endl);
 | 
|---|
| [e138de] | 69 |   int TotalDegree = 0;
 | 
|---|
 | 70 |   for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
 | 
|---|
| [a67d19] | 71 |     DoLog(4) && (Log() << Verbose(4) << **Runner << endl);
 | 
|---|
| [e138de] | 72 |     TotalDegree += (*Runner)->BondDegree;
 | 
|---|
 | 73 |   }
 | 
|---|
| [a67d19] | 74 |   DoLog(4) && (Log() << Verbose(4) << " -- TotalDegree: " << TotalDegree << endl);
 | 
|---|
| [6b919f8] | 75 | };
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 | /** Output of atom::nr along with all bond partners.
 | 
|---|
 | 78 |  * \param *AdjacencyFile output stream
 | 
|---|
 | 79 |  */
 | 
|---|
| [1f1b23] | 80 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
 | 
|---|
| [6b919f8] | 81 | {
 | 
|---|
| [9d83b6] | 82 |   const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| [6b919f8] | 83 |   *AdjacencyFile << nr << "\t";
 | 
|---|
 | 84 |   for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
 | 
|---|
 | 85 |     *AdjacencyFile << (*Runner)->GetOtherAtom(this)->nr << "\t";
 | 
|---|
 | 86 |   *AdjacencyFile << endl;
 | 
|---|
 | 87 | };
 | 
|---|
 | 88 | 
 | 
|---|
| [1f1b23] | 89 | /** Output of atom::nr along each bond partner per line.
 | 
|---|
 | 90 |  * Only bonds are printed where atom::nr is smaller than the one of the bond partner.
 | 
|---|
 | 91 |  * \param *AdjacencyFile output stream
 | 
|---|
 | 92 |  */
 | 
|---|
 | 93 | void BondedParticle::OutputBonds(ofstream * const BondFile) const
 | 
|---|
 | 94 | {
 | 
|---|
| [9d83b6] | 95 |   const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| [1f1b23] | 96 |   for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
 | 
|---|
 | 97 |     if (nr < (*Runner)->GetOtherAtom(this)->nr)
 | 
|---|
 | 98 |       *BondFile << nr << "\t" << (*Runner)->GetOtherAtom(this)->nr << "\n";
 | 
|---|
 | 99 | };
 | 
|---|
 | 100 | 
 | 
|---|
| [b8d4a3] | 101 | /**
 | 
|---|
 | 102 |  * Adds a bond between this bonded particle and another. Does nothing if this
 | 
|---|
 | 103 |  * bond already exists.
 | 
|---|
 | 104 |  *
 | 
|---|
| [073a9e4] | 105 |  * @param _step time step to access
 | 
|---|
| [b8d4a3] | 106 |  * \param bonding partner
 | 
|---|
 | 107 |  */
 | 
|---|
| [073a9e4] | 108 | void BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner) {
 | 
|---|
 | 109 |   if (IsBondedTo(_step, Partner)) {
 | 
|---|
| [b8d4a3] | 110 |     return;
 | 
|---|
 | 111 |   }
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |   bond* newBond = new bond((atom*) this, (atom*) Partner, 1, 0);
 | 
|---|
| [073a9e4] | 114 |   RegisterBond(_step, newBond);
 | 
|---|
 | 115 |   Partner->RegisterBond(_step, newBond);
 | 
|---|
| [b8d4a3] | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
| [6b919f8] | 118 | /** Puts a given bond into atom::ListOfBonds.
 | 
|---|
| [073a9e4] | 119 |  * @param _step time step to access
 | 
|---|
| [6b919f8] | 120 |  * \param *Binder bond to insert
 | 
|---|
 | 121 |  */
 | 
|---|
| [073a9e4] | 122 | bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder)
 | 
|---|
| [6b919f8] | 123 | {
 | 
|---|
 | 124 |   bool status = false;
 | 
|---|
 | 125 |   if (Binder != NULL) {
 | 
|---|
 | 126 |     if (Binder->Contains(this)) {
 | 
|---|
| [d557374] | 127 |       //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
 | 
|---|
| [073a9e4] | 128 |       BondList& ListOfBonds = getListOfBondsAtStep(_step);
 | 
|---|
| [6b919f8] | 129 |       ListOfBonds.push_back(Binder);
 | 
|---|
 | 130 |       status = true;
 | 
|---|
 | 131 |     } else {
 | 
|---|
| [58ed4a] | 132 |       DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
 | 
|---|
| [6b919f8] | 133 |     }
 | 
|---|
 | 134 |   } else {
 | 
|---|
| [58ed4a] | 135 |     DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
 | 
|---|
| [6b919f8] | 136 |   }
 | 
|---|
 | 137 |   return status;
 | 
|---|
 | 138 | };
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 | /** Removes a given bond from atom::ListOfBonds.
 | 
|---|
| [9d83b6] | 141 |  * @param _step time step to access
 | 
|---|
| [6b919f8] | 142 |  * \param *Binder bond to remove
 | 
|---|
 | 143 |  */
 | 
|---|
 | 144 | bool BondedParticle::UnregisterBond(bond *Binder)
 | 
|---|
 | 145 | {
 | 
|---|
 | 146 |   bool status = false;
 | 
|---|
| [d557374] | 147 |   ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL.");
 | 
|---|
 | 148 |   const int step = ContainsBondAtStep(Binder);
 | 
|---|
 | 149 |   if (step != -1) {
 | 
|---|
 | 150 |     //LOG(3,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
 | 
|---|
 | 151 |     ListOfBonds[step].remove(Binder);
 | 
|---|
 | 152 |     status = true;
 | 
|---|
| [6b919f8] | 153 |   } else {
 | 
|---|
| [d557374] | 154 |     DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
 | 
|---|
| [6b919f8] | 155 |   }
 | 
|---|
 | 156 |   return status;
 | 
|---|
 | 157 | };
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 | /** Removes all bonds from atom::ListOfBonds.
 | 
|---|
 | 160 |  * \note Does not do any memory de-allocation.
 | 
|---|
 | 161 |  */
 | 
|---|
| [a2bdbe] | 162 | void BondedParticle::UnregisterAllBond(const unsigned int _step)
 | 
|---|
| [6b919f8] | 163 | {
 | 
|---|
| [a2bdbe] | 164 |   BondList& ListOfBonds = getListOfBondsAtStep(_step);
 | 
|---|
| [6b919f8] | 165 |   ListOfBonds.clear();
 | 
|---|
 | 166 | };
 | 
|---|
 | 167 | 
 | 
|---|
| [583081] | 168 | /** Removes all bonds of given \a _step with freeing memory.
 | 
|---|
 | 169 |  *
 | 
|---|
 | 170 |  * @param _step time step whose bonds to free
 | 
|---|
 | 171 |  */
 | 
|---|
 | 172 | void BondedParticle::ClearBondsAtStep(const unsigned int _step)
 | 
|---|
 | 173 | {
 | 
|---|
 | 174 |   //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]);
 | 
|---|
 | 175 |   for (BondList::iterator iter = (ListOfBonds[_step]).begin();
 | 
|---|
 | 176 |       !(ListOfBonds[_step]).empty();
 | 
|---|
 | 177 |       iter = (ListOfBonds[_step]).begin()) {
 | 
|---|
 | 178 |     //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds);
 | 
|---|
 | 179 |     delete((*iter)); // will also unregister with us and remove from list
 | 
|---|
 | 180 |   }
 | 
|---|
 | 181 | }
 | 
|---|
 | 182 | 
 | 
|---|
| [93c6e9] | 183 | /** Searches for the time step where the given bond \a *Binder is a bond of this particle.
 | 
|---|
 | 184 |  *
 | 
|---|
 | 185 |  * @param Binder bond to check
 | 
|---|
 | 186 |  * @return >=0 - first time step where bond appears, -1 - bond not present in lists
 | 
|---|
 | 187 |  */
 | 
|---|
 | 188 | int BondedParticle::ContainsBondAtStep(bond *Binder)
 | 
|---|
 | 189 | {
 | 
|---|
 | 190 |   int step = -1;
 | 
|---|
 | 191 |   int tempstep = 0;
 | 
|---|
 | 192 |   for(std::vector<BondList>::const_iterator iter = ListOfBonds.begin();
 | 
|---|
 | 193 |       iter != ListOfBonds.end();
 | 
|---|
 | 194 |       ++iter,++tempstep) {
 | 
|---|
 | 195 |     for (BondList::const_iterator bonditer = iter->begin();
 | 
|---|
 | 196 |         bonditer != iter->end();
 | 
|---|
 | 197 |         ++bonditer) {
 | 
|---|
 | 198 |       if ((*bonditer) == Binder) {
 | 
|---|
 | 199 |         step = tempstep;
 | 
|---|
 | 200 |         break;
 | 
|---|
 | 201 |       }
 | 
|---|
 | 202 |     }
 | 
|---|
 | 203 |     if (step != -1)
 | 
|---|
 | 204 |       break;
 | 
|---|
 | 205 |   }
 | 
|---|
 | 206 | 
 | 
|---|
 | 207 |   return step;
 | 
|---|
 | 208 | }
 | 
|---|
 | 209 | 
 | 
|---|
| [6b919f8] | 210 | /** Corrects the bond degree by one at most if necessary.
 | 
|---|
| [93c6e9] | 211 |  * \return number of corrections done
 | 
|---|
| [6b919f8] | 212 |  */
 | 
|---|
| [e138de] | 213 | int BondedParticle::CorrectBondDegree()
 | 
|---|
| [6b919f8] | 214 | {
 | 
|---|
 | 215 |   int NoBonds = 0;
 | 
|---|
 | 216 |   int OtherNoBonds = 0;
 | 
|---|
 | 217 |   int FalseBondDegree = 0;
 | 
|---|
 | 218 |   atom *OtherWalker = NULL;
 | 
|---|
 | 219 |   bond *CandidateBond = NULL;
 | 
|---|
 | 220 | 
 | 
|---|
 | 221 |   NoBonds = CountBonds();
 | 
|---|
| [791138] | 222 |   //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl;
 | 
|---|
| [83f176] | 223 |   if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
 | 
|---|
| [9d83b6] | 224 |     const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| [6b919f8] | 225 |     for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
 | 
|---|
 | 226 |       OtherWalker = (*Runner)->GetOtherAtom(this);
 | 
|---|
 | 227 |       OtherNoBonds = OtherWalker->CountBonds();
 | 
|---|
| [791138] | 228 |       //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl;
 | 
|---|
| [83f176] | 229 |       if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate
 | 
|---|
| [9d83b6] | 230 |         const BondList& OtherListOfBonds = OtherWalker->getListOfBonds();
 | 
|---|
 | 231 |         if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first
 | 
|---|
| [6b919f8] | 232 |           CandidateBond = (*Runner);
 | 
|---|
| [e138de] | 233 |           //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl;
 | 
|---|
| [6b919f8] | 234 |         }
 | 
|---|
 | 235 |       }
 | 
|---|
 | 236 |     }
 | 
|---|
 | 237 |     if ((CandidateBond != NULL)) {
 | 
|---|
 | 238 |       CandidateBond->BondDegree++;
 | 
|---|
| [791138] | 239 |       //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl;
 | 
|---|
| [6b919f8] | 240 |     } else {
 | 
|---|
| [58ed4a] | 241 |       DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl);
 | 
|---|
| [6b919f8] | 242 |       FalseBondDegree++;
 | 
|---|
 | 243 |     }
 | 
|---|
 | 244 |   }
 | 
|---|
 | 245 |   return FalseBondDegree;
 | 
|---|
 | 246 | };
 | 
|---|
 | 247 | 
 | 
|---|
| [c0d9eb] | 248 | /** Counts the number of bonds weighted by bond::BondDegree.
 | 
|---|
 | 249 |    * @param _step time step to access
 | 
|---|
 | 250 |  * \param bonds times bond::BondDegree
 | 
|---|
 | 251 |  */
 | 
|---|
 | 252 | int BondedParticle::CountBonds() const
 | 
|---|
 | 253 | {
 | 
|---|
 | 254 |   int NoBonds = 0;
 | 
|---|
| [9d83b6] | 255 |   const BondList& ListOfBonds = getListOfBonds();
 | 
|---|
| [c0d9eb] | 256 |   for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
 | 257 |       Runner != ListOfBonds.end();
 | 
|---|
 | 258 |       (++Runner))
 | 
|---|
 | 259 |     NoBonds += (*Runner)->BondDegree;
 | 
|---|
 | 260 |   return NoBonds;
 | 
|---|
 | 261 | };
 | 
|---|
 | 262 | 
 | 
|---|
| [b70721] | 263 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
 | 
|---|
| [073a9e4] | 264 |  * @param _step time step to access
 | 
|---|
| [b70721] | 265 |  * \param *BondPartner atom to check for
 | 
|---|
 | 266 |  * \return true - bond exists, false - bond does not exist
 | 
|---|
 | 267 |  */
 | 
|---|
| [073a9e4] | 268 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
 | 
|---|
| [b70721] | 269 | {
 | 
|---|
 | 270 |   bool status = false;
 | 
|---|
 | 271 | 
 | 
|---|
| [073a9e4] | 272 |   const BondList& ListOfBonds = getListOfBondsAtStep(_step);
 | 
|---|
 | 273 |   for (BondList::const_iterator runner = ListOfBonds.begin();
 | 
|---|
 | 274 |       runner != ListOfBonds.end();
 | 
|---|
 | 275 |       runner++) {
 | 
|---|
| [b70721] | 276 |     status = status || ((*runner)->Contains(BondPartner));
 | 
|---|
 | 277 |   }
 | 
|---|
 | 278 |   return status;
 | 
|---|
 | 279 | };
 | 
|---|
 | 280 | 
 | 
|---|
| [d74077] | 281 | std::ostream & BondedParticle::operator << (std::ostream &ost) const
 | 
|---|
 | 282 | {
 | 
|---|
 | 283 |   ParticleInfo::operator<<(ost);
 | 
|---|
 | 284 |   ost << "," << getPosition();
 | 
|---|
 | 285 |   return ost;
 | 
|---|
 | 286 | }
 | 
|---|
 | 287 | 
 | 
|---|
 | 288 | std::ostream & operator << (std::ostream &ost, const BondedParticle &a)
 | 
|---|
 | 289 | {
 | 
|---|
 | 290 |   a.ParticleInfo::operator<<(ost);
 | 
|---|
 | 291 |   ost << "," << a.getPosition();
 | 
|---|
 | 292 |   return ost;
 | 
|---|
 | 293 | }
 | 
|---|
 | 294 | 
 | 
|---|