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