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