[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[bcf653] | 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"
|
---|
[3bdb6d] | 28 | #include "Element/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 | {
|
---|
[5e2f80] | 42 | removeAllBonds();
|
---|
[6b919f8] | 43 | };
|
---|
| 44 |
|
---|
| 45 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
|
---|
| 46 | * \param *file output stream
|
---|
| 47 | */
|
---|
[b453f9] | 48 | void BondedParticle::OutputOrder(ofstream *file) const
|
---|
[6b919f8] | 49 | {
|
---|
[735b1c] | 50 | *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
|
---|
[47d041] | 51 | //LOG(2, "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << ".");
|
---|
[6b919f8] | 52 | };
|
---|
| 53 |
|
---|
| 54 | /** Prints all bonds of this atom with total degree.
|
---|
| 55 | */
|
---|
[4b5cf8] | 56 | void BondedParticle::OutputBondOfAtom(std::ostream &ost) const
|
---|
[6b919f8] | 57 | {
|
---|
[9d83b6] | 58 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[4b5cf8] | 59 | ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: ";
|
---|
[e138de] | 60 | int TotalDegree = 0;
|
---|
| 61 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
|
---|
[4b5cf8] | 62 | ost << **Runner << "\t";
|
---|
[e138de] | 63 | TotalDegree += (*Runner)->BondDegree;
|
---|
| 64 | }
|
---|
[4b5cf8] | 65 | ost << " -- TotalDegree: " << TotalDegree;
|
---|
[6b919f8] | 66 | };
|
---|
| 67 |
|
---|
[5309ba] | 68 | /** Output of atom::Nr along with all bond partners.
|
---|
[6b919f8] | 69 | * \param *AdjacencyFile output stream
|
---|
| 70 | */
|
---|
[1f1b23] | 71 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
|
---|
[6b919f8] | 72 | {
|
---|
[9d83b6] | 73 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[52ed5b] | 74 | *AdjacencyFile << getNr() << "\t";
|
---|
[6b919f8] | 75 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
---|
[52ed5b] | 76 | *AdjacencyFile << (*Runner)->GetOtherAtom(this)->getNr() << "\t";
|
---|
[6b919f8] | 77 | *AdjacencyFile << endl;
|
---|
| 78 | };
|
---|
| 79 |
|
---|
[5309ba] | 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.
|
---|
[1f1b23] | 82 | * \param *AdjacencyFile output stream
|
---|
| 83 | */
|
---|
| 84 | void BondedParticle::OutputBonds(ofstream * const BondFile) const
|
---|
| 85 | {
|
---|
[9d83b6] | 86 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[1f1b23] | 87 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
---|
[735b1c] | 88 | if (getNr() < (*Runner)->GetOtherAtom(this)->getNr())
|
---|
[52ed5b] | 89 | *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n";
|
---|
[1f1b23] | 90 | };
|
---|
| 91 |
|
---|
[b8d4a3] | 92 | /**
|
---|
[db7e6d] | 93 | * Adds a bond between this bonded particle and another. Returns present instance if this
|
---|
[b8d4a3] | 94 | * bond already exists.
|
---|
| 95 | *
|
---|
[073a9e4] | 96 | * @param _step time step to access
|
---|
[db7e6d] | 97 | * @param bonding partner
|
---|
| 98 | * @return const reference to created bond or to already present bonds
|
---|
[b8d4a3] | 99 | */
|
---|
[db7e6d] | 100 | const bond * BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner)
|
---|
| 101 | {
|
---|
| 102 | const BondList &bondlist = getListOfBondsAtStep(_step);
|
---|
| 103 | for (BondList::const_iterator runner = bondlist.begin();
|
---|
| 104 | runner != bondlist.end();
|
---|
| 105 | runner++) {
|
---|
| 106 | if ((*runner)->Contains(Partner))
|
---|
| 107 | return *runner;
|
---|
[b8d4a3] | 108 | }
|
---|
| 109 |
|
---|
[efe516] | 110 | bond* newBond = new bond((atom*) this, (atom*) Partner, 1);
|
---|
[073a9e4] | 111 | RegisterBond(_step, newBond);
|
---|
| 112 | Partner->RegisterBond(_step, newBond);
|
---|
[db7e6d] | 113 |
|
---|
| 114 | return newBond;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | /** Removes a bond for this atom.
|
---|
| 118 | *
|
---|
| 119 | * @param Binder bond to remove
|
---|
| 120 | */
|
---|
| 121 | void BondedParticle::removeBond(bond * binder)
|
---|
| 122 | {
|
---|
| 123 | UnregisterBond(binder);
|
---|
[b8d4a3] | 124 | }
|
---|
| 125 |
|
---|
[5e2f80] | 126 | /** Removes all bonds and their instances, too.
|
---|
| 127 | *
|
---|
| 128 | */
|
---|
| 129 | void BondedParticle::removeAllBonds()
|
---|
| 130 | {
|
---|
| 131 | for (size_t index = 0; index < ListOfBonds.size(); ++index)
|
---|
| 132 | {
|
---|
| 133 | for (BondList::iterator iter = ListOfBonds[index].begin();
|
---|
| 134 | !ListOfBonds[index].empty();
|
---|
| 135 | iter = ListOfBonds[index].begin()) {
|
---|
| 136 | delete (*iter);
|
---|
[74ec1f] | 137 | // unregister/NOTIFY is done by bond::~bond()
|
---|
[5e2f80] | 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
[6b919f8] | 142 | /** Puts a given bond into atom::ListOfBonds.
|
---|
[073a9e4] | 143 | * @param _step time step to access
|
---|
[6b919f8] | 144 | * \param *Binder bond to insert
|
---|
| 145 | */
|
---|
[073a9e4] | 146 | bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder)
|
---|
[6b919f8] | 147 | {
|
---|
[db7e6d] | 148 | OBSERVE;
|
---|
[6b919f8] | 149 | bool status = false;
|
---|
| 150 | if (Binder != NULL) {
|
---|
| 151 | if (Binder->Contains(this)) {
|
---|
[d557374] | 152 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
|
---|
[5e2f80] | 153 | if (ListOfBonds.size() <= _step)
|
---|
| 154 | ListOfBonds.resize(_step+1);
|
---|
| 155 | ListOfBonds[_step].push_back(Binder);
|
---|
[74ec1f] | 156 | if (WorldTime::getTime() == _step)
|
---|
[2ad1ec] | 157 | NOTIFY(AtomObservable::BondsAdded);
|
---|
[6b919f8] | 158 | status = true;
|
---|
| 159 | } else {
|
---|
[47d041] | 160 | ELOG(1, *Binder << " does not contain " << *this << ".");
|
---|
[6b919f8] | 161 | }
|
---|
| 162 | } else {
|
---|
[47d041] | 163 | ELOG(1, "Binder is " << Binder << ".");
|
---|
[6b919f8] | 164 | }
|
---|
| 165 | return status;
|
---|
| 166 | };
|
---|
| 167 |
|
---|
| 168 | /** Removes a given bond from atom::ListOfBonds.
|
---|
[9d83b6] | 169 | * @param _step time step to access
|
---|
[6b919f8] | 170 | * \param *Binder bond to remove
|
---|
| 171 | */
|
---|
| 172 | bool BondedParticle::UnregisterBond(bond *Binder)
|
---|
| 173 | {
|
---|
[db7e6d] | 174 | OBSERVE;
|
---|
[6b919f8] | 175 | bool status = false;
|
---|
[d557374] | 176 | ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL.");
|
---|
| 177 | const int step = ContainsBondAtStep(Binder);
|
---|
| 178 | if (step != -1) {
|
---|
[5e2f80] | 179 | //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
|
---|
[d557374] | 180 | ListOfBonds[step].remove(Binder);
|
---|
| 181 | status = true;
|
---|
[6b919f8] | 182 | } else {
|
---|
[47d041] | 183 | ELOG(1, *Binder << " does not contain " << *this << ".");
|
---|
[6b919f8] | 184 | }
|
---|
| 185 | return status;
|
---|
| 186 | };
|
---|
| 187 |
|
---|
| 188 | /** Removes all bonds from atom::ListOfBonds.
|
---|
| 189 | * \note Does not do any memory de-allocation.
|
---|
| 190 | */
|
---|
[a2bdbe] | 191 | void BondedParticle::UnregisterAllBond(const unsigned int _step)
|
---|
[6b919f8] | 192 | {
|
---|
[74ec1f] | 193 | OBSERVE;
|
---|
[af897f] | 194 | ListOfBonds[_step].clear();
|
---|
| 195 | }
|
---|
[6b919f8] | 196 |
|
---|
[583081] | 197 | /** Removes all bonds of given \a _step with freeing memory.
|
---|
| 198 | *
|
---|
| 199 | * @param _step time step whose bonds to free
|
---|
| 200 | */
|
---|
| 201 | void BondedParticle::ClearBondsAtStep(const unsigned int _step)
|
---|
| 202 | {
|
---|
[db7e6d] | 203 | OBSERVE;
|
---|
[583081] | 204 | //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]);
|
---|
| 205 | for (BondList::iterator iter = (ListOfBonds[_step]).begin();
|
---|
| 206 | !(ListOfBonds[_step]).empty();
|
---|
| 207 | iter = (ListOfBonds[_step]).begin()) {
|
---|
| 208 | //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds);
|
---|
| 209 | delete((*iter)); // will also unregister with us and remove from list
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[93c6e9] | 213 | /** Searches for the time step where the given bond \a *Binder is a bond of this particle.
|
---|
| 214 | *
|
---|
| 215 | * @param Binder bond to check
|
---|
| 216 | * @return >=0 - first time step where bond appears, -1 - bond not present in lists
|
---|
| 217 | */
|
---|
[db7e6d] | 218 | int BondedParticle::ContainsBondAtStep(bond *Binder) const
|
---|
[93c6e9] | 219 | {
|
---|
| 220 | int step = -1;
|
---|
| 221 | int tempstep = 0;
|
---|
| 222 | for(std::vector<BondList>::const_iterator iter = ListOfBonds.begin();
|
---|
| 223 | iter != ListOfBonds.end();
|
---|
| 224 | ++iter,++tempstep) {
|
---|
| 225 | for (BondList::const_iterator bonditer = iter->begin();
|
---|
| 226 | bonditer != iter->end();
|
---|
| 227 | ++bonditer) {
|
---|
| 228 | if ((*bonditer) == Binder) {
|
---|
| 229 | step = tempstep;
|
---|
| 230 | break;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | if (step != -1)
|
---|
| 234 | break;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | return step;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[6b919f8] | 240 | /** Corrects the bond degree by one at most if necessary.
|
---|
[93c6e9] | 241 | * \return number of corrections done
|
---|
[6b919f8] | 242 | */
|
---|
[e138de] | 243 | int BondedParticle::CorrectBondDegree()
|
---|
[6b919f8] | 244 | {
|
---|
[db7e6d] | 245 | OBSERVE;
|
---|
[74ec1f] | 246 | NOTIFY(AtomObservable::BondDegreeChanged);
|
---|
[6b919f8] | 247 | int NoBonds = 0;
|
---|
| 248 | int OtherNoBonds = 0;
|
---|
| 249 | int FalseBondDegree = 0;
|
---|
| 250 | atom *OtherWalker = NULL;
|
---|
| 251 | bond *CandidateBond = NULL;
|
---|
| 252 |
|
---|
| 253 | NoBonds = CountBonds();
|
---|
[47d041] | 254 | //LOG(3, "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?");
|
---|
[83f176] | 255 | if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
|
---|
[9d83b6] | 256 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[6b919f8] | 257 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
|
---|
| 258 | OtherWalker = (*Runner)->GetOtherAtom(this);
|
---|
| 259 | OtherNoBonds = OtherWalker->CountBonds();
|
---|
[47d041] | 260 | //LOG(3, "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?");
|
---|
[83f176] | 261 | if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate
|
---|
[9d83b6] | 262 | const BondList& OtherListOfBonds = OtherWalker->getListOfBonds();
|
---|
| 263 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first
|
---|
[6b919f8] | 264 | CandidateBond = (*Runner);
|
---|
[47d041] | 265 | //LOG(3, "New candidate is " << *CandidateBond << ".");
|
---|
[6b919f8] | 266 | }
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 | if ((CandidateBond != NULL)) {
|
---|
| 270 | CandidateBond->BondDegree++;
|
---|
[47d041] | 271 | //LOG(2, "Increased bond degree for bond " << *CandidateBond << ".");
|
---|
[6b919f8] | 272 | } else {
|
---|
[47d041] | 273 | ELOG(2, "Could not find correct degree for atom " << *this << ".");
|
---|
[6b919f8] | 274 | FalseBondDegree++;
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | return FalseBondDegree;
|
---|
| 278 | };
|
---|
| 279 |
|
---|
[5e2f80] | 280 | /** Sets the weight of all connected bonds to one.
|
---|
| 281 | */
|
---|
| 282 | void BondedParticle::resetBondDegree()
|
---|
| 283 | {
|
---|
| 284 | OBSERVE;
|
---|
[74ec1f] | 285 | NOTIFY(BondedParticle::BondDegreeChanged);
|
---|
[5e2f80] | 286 | for (std::vector<BondList>::iterator Runner = ListOfBonds.begin();
|
---|
| 287 | Runner != ListOfBonds.end();
|
---|
| 288 | ++Runner)
|
---|
| 289 | for (BondList::iterator BondRunner = (*Runner).begin();
|
---|
| 290 | BondRunner != (*Runner).end();
|
---|
| 291 | ++BondRunner)
|
---|
| 292 | (*BondRunner)->BondDegree = 1;
|
---|
| 293 | };
|
---|
| 294 |
|
---|
[c0d9eb] | 295 | /** Counts the number of bonds weighted by bond::BondDegree.
|
---|
| 296 | * @param _step time step to access
|
---|
| 297 | * \param bonds times bond::BondDegree
|
---|
| 298 | */
|
---|
| 299 | int BondedParticle::CountBonds() const
|
---|
| 300 | {
|
---|
| 301 | int NoBonds = 0;
|
---|
[9d83b6] | 302 | const BondList& ListOfBonds = getListOfBonds();
|
---|
[c0d9eb] | 303 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
| 304 | Runner != ListOfBonds.end();
|
---|
| 305 | (++Runner))
|
---|
| 306 | NoBonds += (*Runner)->BondDegree;
|
---|
| 307 | return NoBonds;
|
---|
| 308 | };
|
---|
| 309 |
|
---|
[b70721] | 310 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
|
---|
[073a9e4] | 311 | * @param _step time step to access
|
---|
[b70721] | 312 | * \param *BondPartner atom to check for
|
---|
| 313 | * \return true - bond exists, false - bond does not exist
|
---|
| 314 | */
|
---|
[073a9e4] | 315 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
|
---|
[b70721] | 316 | {
|
---|
| 317 | bool status = false;
|
---|
| 318 |
|
---|
[073a9e4] | 319 | const BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
---|
| 320 | for (BondList::const_iterator runner = ListOfBonds.begin();
|
---|
| 321 | runner != ListOfBonds.end();
|
---|
| 322 | runner++) {
|
---|
[b70721] | 323 | status = status || ((*runner)->Contains(BondPartner));
|
---|
| 324 | }
|
---|
| 325 | return status;
|
---|
| 326 | };
|
---|
| 327 |
|
---|
[d74077] | 328 | std::ostream & BondedParticle::operator << (std::ostream &ost) const
|
---|
| 329 | {
|
---|
| 330 | ParticleInfo::operator<<(ost);
|
---|
| 331 | ost << "," << getPosition();
|
---|
| 332 | return ost;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | std::ostream & operator << (std::ostream &ost, const BondedParticle &a)
|
---|
| 336 | {
|
---|
| 337 | a.ParticleInfo::operator<<(ost);
|
---|
| 338 | ost << "," << a.getPosition();
|
---|
| 339 | return ost;
|
---|
| 340 | }
|
---|
| 341 |
|
---|