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