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