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