[bcf653] | 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 |
|
---|
[6b919f8] | 8 | /*
|
---|
| 9 | * atom_bondedparticle.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Oct 19, 2009
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 21 |
|
---|
[6b919f8] | 22 | #include "atom.hpp"
|
---|
| 23 | #include "atom_bondedparticle.hpp"
|
---|
[129204] | 24 | #include "Bond/bond.hpp"
|
---|
[d557374] | 25 | #include "CodePatterns/Assert.hpp"
|
---|
[ad011c] | 26 | #include "CodePatterns/Log.hpp"
|
---|
| 27 | #include "CodePatterns/Verbose.hpp"
|
---|
[d557374] | 28 | #include "element.hpp"
|
---|
[db7e6d] | 29 | #include "WorldTime.hpp"
|
---|
[6b919f8] | 30 |
|
---|
| 31 | /** Constructor of class BondedParticle.
|
---|
| 32 | */
|
---|
[70ff32] | 33 | BondedParticle::BondedParticle()
|
---|
| 34 | {
|
---|
[9d83b6] | 35 | ListOfBonds.push_back(BondList());
|
---|
[70ff32] | 36 | };
|
---|
[6b919f8] | 37 |
|
---|
| 38 | /** Destructor of class BondedParticle.
|
---|
| 39 | */
|
---|
| 40 | BondedParticle::~BondedParticle()
|
---|
| 41 | {
|
---|
[03c77c] | 42 | const size_t max = ListOfBonds.size();
|
---|
| 43 | for (size_t i = 0; i < max; ++i) {
|
---|
| 44 | ClearBondsAtStep(i);
|
---|
[6b919f8] | 45 | }
|
---|
| 46 | };
|
---|
| 47 |
|
---|
| 48 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
|
---|
| 49 | * \param *file output stream
|
---|
| 50 | */
|
---|
[b453f9] | 51 | void BondedParticle::OutputOrder(ofstream *file) const
|
---|
[6b919f8] | 52 | {
|
---|
[735b1c] | 53 | *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
|
---|
| 54 | //Log() << Verbose(2) << "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl;
|
---|
[6b919f8] | 55 | };
|
---|
| 56 |
|
---|
| 57 | /** Prints all bonds of this atom with total degree.
|
---|
| 58 | */
|
---|
[4b5cf8] | 59 | void BondedParticle::OutputBondOfAtom(std::ostream &ost) const
|
---|
[6b919f8] | 60 | {
|
---|
[9d83b6] | 61 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[4b5cf8] | 62 | ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: ";
|
---|
[e138de] | 63 | int TotalDegree = 0;
|
---|
| 64 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
|
---|
[4b5cf8] | 65 | ost << **Runner << "\t";
|
---|
[e138de] | 66 | TotalDegree += (*Runner)->BondDegree;
|
---|
| 67 | }
|
---|
[4b5cf8] | 68 | ost << " -- TotalDegree: " << TotalDegree;
|
---|
[6b919f8] | 69 | };
|
---|
| 70 |
|
---|
[5309ba] | 71 | /** Output of atom::Nr along with all bond partners.
|
---|
[6b919f8] | 72 | * \param *AdjacencyFile output stream
|
---|
| 73 | */
|
---|
[1f1b23] | 74 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
|
---|
[6b919f8] | 75 | {
|
---|
[9d83b6] | 76 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[735b1c] | 77 | *AdjacencyFile << getNr() << "\t";
|
---|
[6b919f8] | 78 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
---|
[735b1c] | 79 | *AdjacencyFile << (*Runner)->GetOtherAtom(this)->getNr() << "\t";
|
---|
[6b919f8] | 80 | *AdjacencyFile << endl;
|
---|
| 81 | };
|
---|
| 82 |
|
---|
[5309ba] | 83 | /** Output of atom::Nr along each bond partner per line.
|
---|
| 84 | * Only bonds are printed where atom::Nr is smaller than the one of the bond partner.
|
---|
[1f1b23] | 85 | * \param *AdjacencyFile output stream
|
---|
| 86 | */
|
---|
| 87 | void BondedParticle::OutputBonds(ofstream * const BondFile) const
|
---|
| 88 | {
|
---|
[9d83b6] | 89 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[1f1b23] | 90 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
---|
[735b1c] | 91 | if (getNr() < (*Runner)->GetOtherAtom(this)->getNr())
|
---|
| 92 | *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n";
|
---|
[1f1b23] | 93 | };
|
---|
| 94 |
|
---|
[b8d4a3] | 95 | /**
|
---|
[db7e6d] | 96 | * Adds a bond between this bonded particle and another. Returns present instance if this
|
---|
[b8d4a3] | 97 | * bond already exists.
|
---|
| 98 | *
|
---|
[073a9e4] | 99 | * @param _step time step to access
|
---|
[db7e6d] | 100 | * @param bonding partner
|
---|
| 101 | * @return const reference to created bond or to already present bonds
|
---|
[b8d4a3] | 102 | */
|
---|
[db7e6d] | 103 | const bond * BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner)
|
---|
| 104 | {
|
---|
| 105 | const BondList &bondlist = getListOfBondsAtStep(_step);
|
---|
| 106 | for (BondList::const_iterator runner = bondlist.begin();
|
---|
| 107 | runner != bondlist.end();
|
---|
| 108 | runner++) {
|
---|
| 109 | if ((*runner)->Contains(Partner))
|
---|
| 110 | return *runner;
|
---|
[b8d4a3] | 111 | }
|
---|
| 112 |
|
---|
[efe516] | 113 | bond* newBond = new bond((atom*) this, (atom*) Partner, 1);
|
---|
[073a9e4] | 114 | RegisterBond(_step, newBond);
|
---|
| 115 | Partner->RegisterBond(_step, newBond);
|
---|
[db7e6d] | 116 |
|
---|
| 117 | return newBond;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /** Removes a bond for this atom.
|
---|
| 121 | *
|
---|
| 122 | * @param Binder bond to remove
|
---|
| 123 | */
|
---|
| 124 | void BondedParticle::removeBond(bond * binder)
|
---|
| 125 | {
|
---|
| 126 | UnregisterBond(binder);
|
---|
[b8d4a3] | 127 | }
|
---|
| 128 |
|
---|
[6b919f8] | 129 | /** Puts a given bond into atom::ListOfBonds.
|
---|
[073a9e4] | 130 | * @param _step time step to access
|
---|
[6b919f8] | 131 | * \param *Binder bond to insert
|
---|
| 132 | */
|
---|
[073a9e4] | 133 | bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder)
|
---|
[6b919f8] | 134 | {
|
---|
[db7e6d] | 135 | OBSERVE;
|
---|
[6b919f8] | 136 | bool status = false;
|
---|
| 137 | if (Binder != NULL) {
|
---|
| 138 | if (Binder->Contains(this)) {
|
---|
[db7e6d] | 139 | if (WorldTime::getTime() == _step)
|
---|
| 140 | NOTIFY(AtomObservable::BondsChanged);
|
---|
[d557374] | 141 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
|
---|
[073a9e4] | 142 | BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
---|
[6b919f8] | 143 | ListOfBonds.push_back(Binder);
|
---|
| 144 | status = true;
|
---|
| 145 | } else {
|
---|
[58ed4a] | 146 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
|
---|
[6b919f8] | 147 | }
|
---|
| 148 | } else {
|
---|
[58ed4a] | 149 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
|
---|
[6b919f8] | 150 | }
|
---|
| 151 | return status;
|
---|
| 152 | };
|
---|
| 153 |
|
---|
| 154 | /** Removes a given bond from atom::ListOfBonds.
|
---|
[9d83b6] | 155 | * @param _step time step to access
|
---|
[6b919f8] | 156 | * \param *Binder bond to remove
|
---|
| 157 | */
|
---|
| 158 | bool BondedParticle::UnregisterBond(bond *Binder)
|
---|
| 159 | {
|
---|
[db7e6d] | 160 | OBSERVE;
|
---|
[6b919f8] | 161 | bool status = false;
|
---|
[d557374] | 162 | ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL.");
|
---|
| 163 | const int step = ContainsBondAtStep(Binder);
|
---|
| 164 | if (step != -1) {
|
---|
[db7e6d] | 165 | NOTIFY(AtomObservable::BondsChanged);
|
---|
[d557374] | 166 | //LOG(3,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
|
---|
| 167 | ListOfBonds[step].remove(Binder);
|
---|
| 168 | status = true;
|
---|
[6b919f8] | 169 | } else {
|
---|
[d557374] | 170 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
|
---|
[6b919f8] | 171 | }
|
---|
| 172 | return status;
|
---|
| 173 | };
|
---|
| 174 |
|
---|
| 175 | /** Removes all bonds from atom::ListOfBonds.
|
---|
| 176 | * \note Does not do any memory de-allocation.
|
---|
| 177 | */
|
---|
[a2bdbe] | 178 | void BondedParticle::UnregisterAllBond(const unsigned int _step)
|
---|
[6b919f8] | 179 | {
|
---|
[af897f] | 180 | ListOfBonds[_step].clear();
|
---|
| 181 | }
|
---|
[6b919f8] | 182 |
|
---|
[583081] | 183 | /** Removes all bonds of given \a _step with freeing memory.
|
---|
| 184 | *
|
---|
| 185 | * @param _step time step whose bonds to free
|
---|
| 186 | */
|
---|
| 187 | void BondedParticle::ClearBondsAtStep(const unsigned int _step)
|
---|
| 188 | {
|
---|
[db7e6d] | 189 | OBSERVE;
|
---|
| 190 | if (WorldTime::getTime() == _step)
|
---|
| 191 | NOTIFY(AtomObservable::BondsChanged);
|
---|
[583081] | 192 | //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]);
|
---|
| 193 | for (BondList::iterator iter = (ListOfBonds[_step]).begin();
|
---|
| 194 | !(ListOfBonds[_step]).empty();
|
---|
| 195 | iter = (ListOfBonds[_step]).begin()) {
|
---|
| 196 | //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds);
|
---|
| 197 | delete((*iter)); // will also unregister with us and remove from list
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[93c6e9] | 201 | /** Searches for the time step where the given bond \a *Binder is a bond of this particle.
|
---|
| 202 | *
|
---|
| 203 | * @param Binder bond to check
|
---|
| 204 | * @return >=0 - first time step where bond appears, -1 - bond not present in lists
|
---|
| 205 | */
|
---|
[db7e6d] | 206 | int BondedParticle::ContainsBondAtStep(bond *Binder) const
|
---|
[93c6e9] | 207 | {
|
---|
| 208 | int step = -1;
|
---|
| 209 | int tempstep = 0;
|
---|
| 210 | for(std::vector<BondList>::const_iterator iter = ListOfBonds.begin();
|
---|
| 211 | iter != ListOfBonds.end();
|
---|
| 212 | ++iter,++tempstep) {
|
---|
| 213 | for (BondList::const_iterator bonditer = iter->begin();
|
---|
| 214 | bonditer != iter->end();
|
---|
| 215 | ++bonditer) {
|
---|
| 216 | if ((*bonditer) == Binder) {
|
---|
| 217 | step = tempstep;
|
---|
| 218 | break;
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 | if (step != -1)
|
---|
| 222 | break;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | return step;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[6b919f8] | 228 | /** Corrects the bond degree by one at most if necessary.
|
---|
[93c6e9] | 229 | * \return number of corrections done
|
---|
[6b919f8] | 230 | */
|
---|
[e138de] | 231 | int BondedParticle::CorrectBondDegree()
|
---|
[6b919f8] | 232 | {
|
---|
[db7e6d] | 233 | OBSERVE;
|
---|
| 234 | NOTIFY(AtomObservable::BondsChanged);
|
---|
[6b919f8] | 235 | int NoBonds = 0;
|
---|
| 236 | int OtherNoBonds = 0;
|
---|
| 237 | int FalseBondDegree = 0;
|
---|
| 238 | atom *OtherWalker = NULL;
|
---|
| 239 | bond *CandidateBond = NULL;
|
---|
| 240 |
|
---|
| 241 | NoBonds = CountBonds();
|
---|
[791138] | 242 | //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl;
|
---|
[83f176] | 243 | if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
|
---|
[9d83b6] | 244 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[6b919f8] | 245 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
|
---|
| 246 | OtherWalker = (*Runner)->GetOtherAtom(this);
|
---|
| 247 | OtherNoBonds = OtherWalker->CountBonds();
|
---|
[791138] | 248 | //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl;
|
---|
[83f176] | 249 | if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate
|
---|
[9d83b6] | 250 | const BondList& OtherListOfBonds = OtherWalker->getListOfBonds();
|
---|
| 251 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first
|
---|
[6b919f8] | 252 | CandidateBond = (*Runner);
|
---|
[e138de] | 253 | //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl;
|
---|
[6b919f8] | 254 | }
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | if ((CandidateBond != NULL)) {
|
---|
| 258 | CandidateBond->BondDegree++;
|
---|
[791138] | 259 | //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl;
|
---|
[6b919f8] | 260 | } else {
|
---|
[58ed4a] | 261 | DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl);
|
---|
[6b919f8] | 262 | FalseBondDegree++;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 | return FalseBondDegree;
|
---|
| 266 | };
|
---|
| 267 |
|
---|
[c0d9eb] | 268 | /** Counts the number of bonds weighted by bond::BondDegree.
|
---|
| 269 | * @param _step time step to access
|
---|
| 270 | * \param bonds times bond::BondDegree
|
---|
| 271 | */
|
---|
| 272 | int BondedParticle::CountBonds() const
|
---|
| 273 | {
|
---|
| 274 | int NoBonds = 0;
|
---|
[9d83b6] | 275 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[c0d9eb] | 276 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 277 | Runner != ListOfBonds.end();
|
---|
| 278 | (++Runner))
|
---|
| 279 | NoBonds += (*Runner)->BondDegree;
|
---|
| 280 | return NoBonds;
|
---|
| 281 | };
|
---|
| 282 |
|
---|
[b70721] | 283 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
|
---|
[073a9e4] | 284 | * @param _step time step to access
|
---|
[b70721] | 285 | * \param *BondPartner atom to check for
|
---|
| 286 | * \return true - bond exists, false - bond does not exist
|
---|
| 287 | */
|
---|
[073a9e4] | 288 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
|
---|
[b70721] | 289 | {
|
---|
| 290 | bool status = false;
|
---|
| 291 |
|
---|
[073a9e4] | 292 | const BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
---|
| 293 | for (BondList::const_iterator runner = ListOfBonds.begin();
|
---|
| 294 | runner != ListOfBonds.end();
|
---|
| 295 | runner++) {
|
---|
[b70721] | 296 | status = status || ((*runner)->Contains(BondPartner));
|
---|
| 297 | }
|
---|
| 298 | return status;
|
---|
| 299 | };
|
---|
| 300 |
|
---|
[d74077] | 301 | std::ostream & BondedParticle::operator << (std::ostream &ost) const
|
---|
| 302 | {
|
---|
| 303 | ParticleInfo::operator<<(ost);
|
---|
| 304 | ost << "," << getPosition();
|
---|
| 305 | return ost;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | std::ostream & operator << (std::ostream &ost, const BondedParticle &a)
|
---|
| 309 | {
|
---|
| 310 | a.ParticleInfo::operator<<(ost);
|
---|
| 311 | ost << "," << a.getPosition();
|
---|
| 312 | return ost;
|
---|
| 313 | }
|
---|
| 314 |
|
---|