[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"
|
---|
[e138de] | 13 | #include "log.hpp"
|
---|
[6b919f8] | 14 | #include "verbose.hpp"
|
---|
| 15 |
|
---|
| 16 | /** Constructor of class BondedParticle.
|
---|
| 17 | */
|
---|
[70ff32] | 18 | BondedParticle::BondedParticle()
|
---|
| 19 | {
|
---|
| 20 | };
|
---|
[6b919f8] | 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 | */
|
---|
[b453f9] | 36 | void BondedParticle::OutputOrder(ofstream *file) const
|
---|
[6b919f8] | 37 | {
|
---|
| 38 | *file << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
|
---|
[e138de] | 39 | //Log() << Verbose(2) << "Storing: " << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl;
|
---|
[6b919f8] | 40 | };
|
---|
| 41 |
|
---|
| 42 | /** Prints all bonds of this atom with total degree.
|
---|
| 43 | */
|
---|
[e138de] | 44 | void BondedParticle::OutputBondOfAtom() const
|
---|
[6b919f8] | 45 | {
|
---|
[a67d19] | 46 | DoLog(4) && (Log() << Verbose(4) << "Atom " << Name << "/" << nr << " with " << ListOfBonds.size() << " bonds: " << endl);
|
---|
[e138de] | 47 | int TotalDegree = 0;
|
---|
| 48 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
|
---|
[a67d19] | 49 | DoLog(4) && (Log() << Verbose(4) << **Runner << endl);
|
---|
[e138de] | 50 | TotalDegree += (*Runner)->BondDegree;
|
---|
| 51 | }
|
---|
[a67d19] | 52 | DoLog(4) && (Log() << Verbose(4) << " -- TotalDegree: " << TotalDegree << endl);
|
---|
[6b919f8] | 53 | };
|
---|
| 54 |
|
---|
| 55 | /** Output of atom::nr along with all bond partners.
|
---|
| 56 | * \param *AdjacencyFile output stream
|
---|
| 57 | */
|
---|
[1f1b23] | 58 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
|
---|
[6b919f8] | 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 |
|
---|
[1f1b23] | 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 |
|
---|
[b8d4a3] | 77 | /**
|
---|
| 78 | * Adds a bond between this bonded particle and another. Does nothing if this
|
---|
| 79 | * bond already exists.
|
---|
| 80 | *
|
---|
| 81 | * \param bonding partner
|
---|
| 82 | */
|
---|
| 83 | void BondedParticle::addBond(BondedParticle* Partner) {
|
---|
| 84 | if (IsBondedTo(Partner)) {
|
---|
| 85 | return;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | bond* newBond = new bond((atom*) this, (atom*) Partner, 1, 0);
|
---|
| 89 | RegisterBond(newBond);
|
---|
| 90 | Partner->RegisterBond(newBond);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[6b919f8] | 93 | /** Puts a given bond into atom::ListOfBonds.
|
---|
| 94 | * \param *Binder bond to insert
|
---|
| 95 | */
|
---|
| 96 | bool BondedParticle::RegisterBond(bond *Binder)
|
---|
| 97 | {
|
---|
| 98 | bool status = false;
|
---|
| 99 | if (Binder != NULL) {
|
---|
| 100 | if (Binder->Contains(this)) {
|
---|
| 101 | ListOfBonds.push_back(Binder);
|
---|
| 102 | status = true;
|
---|
| 103 | } else {
|
---|
[58ed4a] | 104 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
|
---|
[6b919f8] | 105 | }
|
---|
| 106 | } else {
|
---|
[58ed4a] | 107 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
|
---|
[6b919f8] | 108 | }
|
---|
| 109 | return status;
|
---|
| 110 | };
|
---|
| 111 |
|
---|
| 112 | /** Removes a given bond from atom::ListOfBonds.
|
---|
| 113 | * \param *Binder bond to remove
|
---|
| 114 | */
|
---|
| 115 | bool BondedParticle::UnregisterBond(bond *Binder)
|
---|
| 116 | {
|
---|
| 117 | bool status = false;
|
---|
| 118 | if (Binder != NULL) {
|
---|
| 119 | if (Binder->Contains(this)) {
|
---|
| 120 | ListOfBonds.remove(Binder);
|
---|
| 121 | status = true;
|
---|
| 122 | } else {
|
---|
[58ed4a] | 123 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
|
---|
[6b919f8] | 124 | }
|
---|
| 125 | } else {
|
---|
[58ed4a] | 126 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
|
---|
[6b919f8] | 127 | }
|
---|
| 128 | return status;
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | /** Removes all bonds from atom::ListOfBonds.
|
---|
| 132 | * \note Does not do any memory de-allocation.
|
---|
| 133 | */
|
---|
| 134 | void BondedParticle::UnregisterAllBond()
|
---|
| 135 | {
|
---|
| 136 | ListOfBonds.clear();
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | /** Corrects the bond degree by one at most if necessary.
|
---|
| 140 | * \param *out output stream for debugging
|
---|
| 141 | */
|
---|
[e138de] | 142 | int BondedParticle::CorrectBondDegree()
|
---|
[6b919f8] | 143 | {
|
---|
| 144 | int NoBonds = 0;
|
---|
| 145 | int OtherNoBonds = 0;
|
---|
| 146 | int FalseBondDegree = 0;
|
---|
| 147 | atom *OtherWalker = NULL;
|
---|
| 148 | bond *CandidateBond = NULL;
|
---|
| 149 |
|
---|
| 150 | NoBonds = CountBonds();
|
---|
[791138] | 151 | //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl;
|
---|
[6b919f8] | 152 | if ((int)(type->NoValenceOrbitals) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
|
---|
| 153 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
|
---|
| 154 | OtherWalker = (*Runner)->GetOtherAtom(this);
|
---|
| 155 | OtherNoBonds = OtherWalker->CountBonds();
|
---|
[791138] | 156 | //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl;
|
---|
| 157 | if ((int)(OtherWalker->type->NoValenceOrbitals) > OtherNoBonds) { // check if possible candidate
|
---|
[6b919f8] | 158 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherWalker->ListOfBonds.size())) { // pick the one with fewer number of bonds first
|
---|
| 159 | CandidateBond = (*Runner);
|
---|
[e138de] | 160 | //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl;
|
---|
[6b919f8] | 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | if ((CandidateBond != NULL)) {
|
---|
| 165 | CandidateBond->BondDegree++;
|
---|
[791138] | 166 | //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl;
|
---|
[6b919f8] | 167 | } else {
|
---|
[58ed4a] | 168 | DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl);
|
---|
[6b919f8] | 169 | FalseBondDegree++;
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 | return FalseBondDegree;
|
---|
| 173 | };
|
---|
| 174 |
|
---|
[b70721] | 175 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
|
---|
| 176 | * \param *BondPartner atom to check for
|
---|
| 177 | * \return true - bond exists, false - bond does not exist
|
---|
| 178 | */
|
---|
| 179 | bool BondedParticle::IsBondedTo(BondedParticle * const BondPartner)
|
---|
| 180 | {
|
---|
| 181 | bool status = false;
|
---|
| 182 |
|
---|
| 183 | for (BondList::iterator runner = ListOfBonds.begin(); runner != ListOfBonds.end(); runner++) {
|
---|
| 184 | status = status || ((*runner)->Contains(BondPartner));
|
---|
| 185 | }
|
---|
| 186 | return status;
|
---|
| 187 | };
|
---|
| 188 |
|
---|