| 1 | /*
 | 
|---|
| 2 |  * atom_bondedparticle.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Oct 19, 2009
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | // include config.h
 | 
|---|
| 9 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 10 | #include <config.h>
 | 
|---|
| 11 | #endif
 | 
|---|
| 12 | 
 | 
|---|
| 13 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| 14 | 
 | 
|---|
| 15 | #include "atom.hpp"
 | 
|---|
| 16 | #include "atom_bondedparticle.hpp"
 | 
|---|
| 17 | #include "bond.hpp"
 | 
|---|
| 18 | #include "element.hpp"
 | 
|---|
| 19 | #include "lists.hpp"
 | 
|---|
| 20 | #include "Helpers/Log.hpp"
 | 
|---|
| 21 | #include "Helpers/Verbose.hpp"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /** Constructor of class BondedParticle.
 | 
|---|
| 24 |  */
 | 
|---|
| 25 | BondedParticle::BondedParticle()
 | 
|---|
| 26 | {
 | 
|---|
| 27 | };
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /** Destructor of class BondedParticle.
 | 
|---|
| 30 |  */
 | 
|---|
| 31 | BondedParticle::~BondedParticle()
 | 
|---|
| 32 | {
 | 
|---|
| 33 |   BondList::const_iterator Runner;
 | 
|---|
| 34 |   while (!ListOfBonds.empty()) {
 | 
|---|
| 35 |     Runner = ListOfBonds.begin();
 | 
|---|
| 36 |     removewithoutcheck(*Runner);
 | 
|---|
| 37 |   }
 | 
|---|
| 38 | };
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
 | 
|---|
| 41 |  * \param *file output stream
 | 
|---|
| 42 |  */
 | 
|---|
| 43 | void BondedParticle::OutputOrder(ofstream *file) const
 | 
|---|
| 44 | {
 | 
|---|
| 45 |   *file << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
 | 
|---|
| 46 |   //Log() << Verbose(2) << "Storing: " << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl;
 | 
|---|
| 47 | };
 | 
|---|
| 48 | 
 | 
|---|
| 49 | /** Prints all bonds of this atom with total degree.
 | 
|---|
| 50 |  */
 | 
|---|
| 51 | void BondedParticle::OutputBondOfAtom() const
 | 
|---|
| 52 | {
 | 
|---|
| 53 |   DoLog(4) && (Log() << Verbose(4) << "Atom " << getName() << "/" << nr << " with " << ListOfBonds.size() << " bonds: " << endl);
 | 
|---|
| 54 |   int TotalDegree = 0;
 | 
|---|
| 55 |   for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
 | 
|---|
| 56 |     DoLog(4) && (Log() << Verbose(4) << **Runner << endl);
 | 
|---|
| 57 |     TotalDegree += (*Runner)->BondDegree;
 | 
|---|
| 58 |   }
 | 
|---|
| 59 |   DoLog(4) && (Log() << Verbose(4) << " -- TotalDegree: " << TotalDegree << endl);
 | 
|---|
| 60 | };
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /** Output of atom::nr along with all bond partners.
 | 
|---|
| 63 |  * \param *AdjacencyFile output stream
 | 
|---|
| 64 |  */
 | 
|---|
| 65 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
 | 
|---|
| 66 | {
 | 
|---|
| 67 |   *AdjacencyFile << nr << "\t";
 | 
|---|
| 68 |   for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
 | 
|---|
| 69 |     *AdjacencyFile << (*Runner)->GetOtherAtom(this)->nr << "\t";
 | 
|---|
| 70 |   *AdjacencyFile << endl;
 | 
|---|
| 71 | };
 | 
|---|
| 72 | 
 | 
|---|
| 73 | /** Output of atom::nr along each bond partner per line.
 | 
|---|
| 74 |  * Only bonds are printed where atom::nr is smaller than the one of the bond partner.
 | 
|---|
| 75 |  * \param *AdjacencyFile output stream
 | 
|---|
| 76 |  */
 | 
|---|
| 77 | void BondedParticle::OutputBonds(ofstream * const BondFile) const
 | 
|---|
| 78 | {
 | 
|---|
| 79 |   for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
 | 
|---|
| 80 |     if (nr < (*Runner)->GetOtherAtom(this)->nr)
 | 
|---|
| 81 |       *BondFile << nr << "\t" << (*Runner)->GetOtherAtom(this)->nr << "\n";
 | 
|---|
| 82 | };
 | 
|---|
| 83 | 
 | 
|---|
| 84 | /**
 | 
|---|
| 85 |  * Adds a bond between this bonded particle and another. Does nothing if this
 | 
|---|
| 86 |  * bond already exists.
 | 
|---|
| 87 |  *
 | 
|---|
| 88 |  * \param bonding partner
 | 
|---|
| 89 |  */
 | 
|---|
| 90 | void BondedParticle::addBond(BondedParticle* Partner) {
 | 
|---|
| 91 |   if (IsBondedTo(Partner)) {
 | 
|---|
| 92 |     return;
 | 
|---|
| 93 |   }
 | 
|---|
| 94 | 
 | 
|---|
| 95 |   bond* newBond = new bond((atom*) this, (atom*) Partner, 1, 0);
 | 
|---|
| 96 |   RegisterBond(newBond);
 | 
|---|
| 97 |   Partner->RegisterBond(newBond);
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | /** Puts a given bond into atom::ListOfBonds.
 | 
|---|
| 101 |  * \param *Binder bond to insert
 | 
|---|
| 102 |  */
 | 
|---|
| 103 | bool BondedParticle::RegisterBond(bond *Binder)
 | 
|---|
| 104 | {
 | 
|---|
| 105 |   bool status = false;
 | 
|---|
| 106 |   if (Binder != NULL) {
 | 
|---|
| 107 |     if (Binder->Contains(this)) {
 | 
|---|
| 108 |       ListOfBonds.push_back(Binder);
 | 
|---|
| 109 |       status = true;
 | 
|---|
| 110 |     } else {
 | 
|---|
| 111 |       DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
 | 
|---|
| 112 |     }
 | 
|---|
| 113 |   } else {
 | 
|---|
| 114 |     DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
 | 
|---|
| 115 |   }
 | 
|---|
| 116 |   return status;
 | 
|---|
| 117 | };
 | 
|---|
| 118 | 
 | 
|---|
| 119 | /** Removes a given bond from atom::ListOfBonds.
 | 
|---|
| 120 |  * \param *Binder bond to remove
 | 
|---|
| 121 |  */
 | 
|---|
| 122 | bool BondedParticle::UnregisterBond(bond *Binder)
 | 
|---|
| 123 | {
 | 
|---|
| 124 |   bool status = false;
 | 
|---|
| 125 |   if (Binder != NULL) {
 | 
|---|
| 126 |     if (Binder->Contains(this)) {
 | 
|---|
| 127 |       ListOfBonds.remove(Binder);
 | 
|---|
| 128 |       status = true;
 | 
|---|
| 129 |     } else {
 | 
|---|
| 130 |       DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
 | 
|---|
| 131 |     }
 | 
|---|
| 132 |   } else {
 | 
|---|
| 133 |     DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
 | 
|---|
| 134 |   }
 | 
|---|
| 135 |   return status;
 | 
|---|
| 136 | };
 | 
|---|
| 137 | 
 | 
|---|
| 138 | /** Removes all bonds from atom::ListOfBonds.
 | 
|---|
| 139 |  * \note Does not do any memory de-allocation.
 | 
|---|
| 140 |  */
 | 
|---|
| 141 | void BondedParticle::UnregisterAllBond()
 | 
|---|
| 142 | {
 | 
|---|
| 143 |   ListOfBonds.clear();
 | 
|---|
| 144 | };
 | 
|---|
| 145 | 
 | 
|---|
| 146 | /** Corrects the bond degree by one at most if necessary.
 | 
|---|
| 147 |  * \param *out output stream for debugging
 | 
|---|
| 148 |  */
 | 
|---|
| 149 | int BondedParticle::CorrectBondDegree()
 | 
|---|
| 150 | {
 | 
|---|
| 151 |   int NoBonds = 0;
 | 
|---|
| 152 |   int OtherNoBonds = 0;
 | 
|---|
| 153 |   int FalseBondDegree = 0;
 | 
|---|
| 154 |   atom *OtherWalker = NULL;
 | 
|---|
| 155 |   bond *CandidateBond = NULL;
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   NoBonds = CountBonds();
 | 
|---|
| 158 |   //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl;
 | 
|---|
| 159 |   if ((int)(getType()->NoValenceOrbitals) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
 | 
|---|
| 160 |     for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
 | 
|---|
| 161 |       OtherWalker = (*Runner)->GetOtherAtom(this);
 | 
|---|
| 162 |       OtherNoBonds = OtherWalker->CountBonds();
 | 
|---|
| 163 |       //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl;
 | 
|---|
| 164 |       if ((int)(OtherWalker->getType()->NoValenceOrbitals) > OtherNoBonds) { // check if possible candidate
 | 
|---|
| 165 |         if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherWalker->ListOfBonds.size())) { // pick the one with fewer number of bonds first
 | 
|---|
| 166 |           CandidateBond = (*Runner);
 | 
|---|
| 167 |           //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl;
 | 
|---|
| 168 |         }
 | 
|---|
| 169 |       }
 | 
|---|
| 170 |     }
 | 
|---|
| 171 |     if ((CandidateBond != NULL)) {
 | 
|---|
| 172 |       CandidateBond->BondDegree++;
 | 
|---|
| 173 |       //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl;
 | 
|---|
| 174 |     } else {
 | 
|---|
| 175 |       DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl);
 | 
|---|
| 176 |       FalseBondDegree++;
 | 
|---|
| 177 |     }
 | 
|---|
| 178 |   }
 | 
|---|
| 179 |   return FalseBondDegree;
 | 
|---|
| 180 | };
 | 
|---|
| 181 | 
 | 
|---|
| 182 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
 | 
|---|
| 183 |  * \param *BondPartner atom to check for
 | 
|---|
| 184 |  * \return true - bond exists, false - bond does not exist
 | 
|---|
| 185 |  */
 | 
|---|
| 186 | bool BondedParticle::IsBondedTo(BondedParticle * const BondPartner)
 | 
|---|
| 187 | {
 | 
|---|
| 188 |   bool status = false;
 | 
|---|
| 189 | 
 | 
|---|
| 190 |   for (BondList::iterator runner = ListOfBonds.begin(); runner != ListOfBonds.end(); runner++) {
 | 
|---|
| 191 |     status = status || ((*runner)->Contains(BondPartner));
 | 
|---|
| 192 |   }
 | 
|---|
| 193 |   return status;
 | 
|---|
| 194 | };
 | 
|---|
| 195 | 
 | 
|---|
| 196 | std::ostream & BondedParticle::operator << (std::ostream &ost) const
 | 
|---|
| 197 | {
 | 
|---|
| 198 |   ParticleInfo::operator<<(ost);
 | 
|---|
| 199 |   ost << "," << getPosition();
 | 
|---|
| 200 |   return ost;
 | 
|---|
| 201 | }
 | 
|---|
| 202 | 
 | 
|---|
| 203 | std::ostream & operator << (std::ostream &ost, const BondedParticle &a)
 | 
|---|
| 204 | {
 | 
|---|
| 205 |   a.ParticleInfo::operator<<(ost);
 | 
|---|
| 206 |   ost << "," << a.getPosition();
 | 
|---|
| 207 |   return ost;
 | 
|---|
| 208 | }
 | 
|---|
| 209 | 
 | 
|---|