[6b919f8] | 1 | /*
|
---|
| 2 | * atom_bondedparticle.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Oct 19, 2009
|
---|
| 5 | * Author: heber
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[112b09] | 8 | #include "Helpers/MemDebug.hpp"
|
---|
| 9 |
|
---|
[6b919f8] | 10 | #include "atom.hpp"
|
---|
| 11 | #include "atom_bondedparticle.hpp"
|
---|
| 12 | #include "bond.hpp"
|
---|
| 13 | #include "element.hpp"
|
---|
| 14 | #include "lists.hpp"
|
---|
[e138de] | 15 | #include "log.hpp"
|
---|
[6b919f8] | 16 | #include "verbose.hpp"
|
---|
| 17 |
|
---|
| 18 | /** Constructor of class BondedParticle.
|
---|
| 19 | */
|
---|
[70ff32] | 20 | BondedParticle::BondedParticle()
|
---|
| 21 | {
|
---|
| 22 | };
|
---|
[6b919f8] | 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 | */
|
---|
[b453f9] | 38 | void BondedParticle::OutputOrder(ofstream *file) const
|
---|
[6b919f8] | 39 | {
|
---|
| 40 | *file << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
|
---|
[e138de] | 41 | //Log() << Verbose(2) << "Storing: " << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl;
|
---|
[6b919f8] | 42 | };
|
---|
| 43 |
|
---|
| 44 | /** Prints all bonds of this atom with total degree.
|
---|
| 45 | */
|
---|
[e138de] | 46 | void BondedParticle::OutputBondOfAtom() const
|
---|
[6b919f8] | 47 | {
|
---|
[68f03d] | 48 | DoLog(4) && (Log() << Verbose(4) << "Atom " << getName() << "/" << nr << " with " << ListOfBonds.size() << " bonds: " << endl);
|
---|
[e138de] | 49 | int TotalDegree = 0;
|
---|
| 50 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
|
---|
[a67d19] | 51 | DoLog(4) && (Log() << Verbose(4) << **Runner << endl);
|
---|
[e138de] | 52 | TotalDegree += (*Runner)->BondDegree;
|
---|
| 53 | }
|
---|
[a67d19] | 54 | DoLog(4) && (Log() << Verbose(4) << " -- TotalDegree: " << TotalDegree << endl);
|
---|
[6b919f8] | 55 | };
|
---|
| 56 |
|
---|
| 57 | /** Output of atom::nr along with all bond partners.
|
---|
| 58 | * \param *AdjacencyFile output stream
|
---|
| 59 | */
|
---|
[1f1b23] | 60 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
|
---|
[6b919f8] | 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 |
|
---|
[1f1b23] | 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 |
|
---|
[b8d4a3] | 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 |
|
---|
[6b919f8] | 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 {
|
---|
[58ed4a] | 106 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
|
---|
[6b919f8] | 107 | }
|
---|
| 108 | } else {
|
---|
[58ed4a] | 109 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
|
---|
[6b919f8] | 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 {
|
---|
[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 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 | */
|
---|
[e138de] | 144 | int BondedParticle::CorrectBondDegree()
|
---|
[6b919f8] | 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();
|
---|
[791138] | 153 | //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl;
|
---|
[6b919f8] | 154 | if ((int)(type->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();
|
---|
[791138] | 158 | //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl;
|
---|
| 159 | if ((int)(OtherWalker->type->NoValenceOrbitals) > OtherNoBonds) { // check if possible candidate
|
---|
[6b919f8] | 160 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherWalker->ListOfBonds.size())) { // pick the one with fewer number of bonds first
|
---|
| 161 | CandidateBond = (*Runner);
|
---|
[e138de] | 162 | //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl;
|
---|
[6b919f8] | 163 | }
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | if ((CandidateBond != NULL)) {
|
---|
| 167 | CandidateBond->BondDegree++;
|
---|
[791138] | 168 | //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl;
|
---|
[6b919f8] | 169 | } else {
|
---|
[58ed4a] | 170 | DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl);
|
---|
[6b919f8] | 171 | FalseBondDegree++;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | return FalseBondDegree;
|
---|
| 175 | };
|
---|
| 176 |
|
---|
[b70721] | 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 |
|
---|