| 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 "CodePatterns/MemDebug.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include "atom.hpp"
|
|---|
| 23 | #include "atom_bondedparticle.hpp"
|
|---|
| 24 | #include "Bond/bond.hpp"
|
|---|
| 25 | #include "CodePatterns/Assert.hpp"
|
|---|
| 26 | #include "CodePatterns/Log.hpp"
|
|---|
| 27 | #include "CodePatterns/Verbose.hpp"
|
|---|
| 28 | #include "element.hpp"
|
|---|
| 29 | #include "WorldTime.hpp"
|
|---|
| 30 |
|
|---|
| 31 | /** Constructor of class BondedParticle.
|
|---|
| 32 | */
|
|---|
| 33 | BondedParticle::BondedParticle()
|
|---|
| 34 | {
|
|---|
| 35 | ListOfBonds.push_back(BondList());
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | /** Destructor of class BondedParticle.
|
|---|
| 39 | */
|
|---|
| 40 | BondedParticle::~BondedParticle()
|
|---|
| 41 | {
|
|---|
| 42 | const size_t max = ListOfBonds.size();
|
|---|
| 43 | for (size_t i = 0; i < max; ++i) {
|
|---|
| 44 | ClearBondsAtStep(i);
|
|---|
| 45 | }
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
|
|---|
| 49 | * \param *file output stream
|
|---|
| 50 | */
|
|---|
| 51 | void BondedParticle::OutputOrder(ofstream *file) const
|
|---|
| 52 | {
|
|---|
| 53 | *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
|
|---|
| 54 | //Log() << Verbose(2) << "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl;
|
|---|
| 55 | };
|
|---|
| 56 |
|
|---|
| 57 | /** Prints all bonds of this atom with total degree.
|
|---|
| 58 | */
|
|---|
| 59 | void BondedParticle::OutputBondOfAtom(std::ostream &ost) const
|
|---|
| 60 | {
|
|---|
| 61 | const BondList& ListOfBonds = getListOfBonds();
|
|---|
| 62 | ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: ";
|
|---|
| 63 | int TotalDegree = 0;
|
|---|
| 64 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
|
|---|
| 65 | ost << **Runner << "\t";
|
|---|
| 66 | TotalDegree += (*Runner)->BondDegree;
|
|---|
| 67 | }
|
|---|
| 68 | ost << " -- TotalDegree: " << TotalDegree;
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | /** Output of atom::Nr along with all bond partners.
|
|---|
| 72 | * \param *AdjacencyFile output stream
|
|---|
| 73 | */
|
|---|
| 74 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
|
|---|
| 75 | {
|
|---|
| 76 | const BondList& ListOfBonds = getListOfBonds();
|
|---|
| 77 | *AdjacencyFile << getNr() << "\t";
|
|---|
| 78 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
|---|
| 79 | *AdjacencyFile << (*Runner)->GetOtherAtom(this)->getNr() << "\t";
|
|---|
| 80 | *AdjacencyFile << endl;
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 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.
|
|---|
| 85 | * \param *AdjacencyFile output stream
|
|---|
| 86 | */
|
|---|
| 87 | void BondedParticle::OutputBonds(ofstream * const BondFile) const
|
|---|
| 88 | {
|
|---|
| 89 | const BondList& ListOfBonds = getListOfBonds();
|
|---|
| 90 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
|
|---|
| 91 | if (getNr() < (*Runner)->GetOtherAtom(this)->getNr())
|
|---|
| 92 | *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n";
|
|---|
| 93 | };
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Adds a bond between this bonded particle and another. Returns present instance if this
|
|---|
| 97 | * bond already exists.
|
|---|
| 98 | *
|
|---|
| 99 | * @param _step time step to access
|
|---|
| 100 | * @param bonding partner
|
|---|
| 101 | * @return const reference to created bond or to already present bonds
|
|---|
| 102 | */
|
|---|
| 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;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | bond* newBond = new bond((atom*) this, (atom*) Partner, 1);
|
|---|
| 114 | RegisterBond(_step, newBond);
|
|---|
| 115 | Partner->RegisterBond(_step, newBond);
|
|---|
| 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);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /** Puts a given bond into atom::ListOfBonds.
|
|---|
| 130 | * @param _step time step to access
|
|---|
| 131 | * \param *Binder bond to insert
|
|---|
| 132 | */
|
|---|
| 133 | bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder)
|
|---|
| 134 | {
|
|---|
| 135 | OBSERVE;
|
|---|
| 136 | bool status = false;
|
|---|
| 137 | if (Binder != NULL) {
|
|---|
| 138 | if (Binder->Contains(this)) {
|
|---|
| 139 | if (WorldTime::getTime() == _step)
|
|---|
| 140 | NOTIFY(AtomObservable::BondsChanged);
|
|---|
| 141 | //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
|
|---|
| 142 | BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
|---|
| 143 | ListOfBonds.push_back(Binder);
|
|---|
| 144 | status = true;
|
|---|
| 145 | } else {
|
|---|
| 146 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
|
|---|
| 147 | }
|
|---|
| 148 | } else {
|
|---|
| 149 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
|
|---|
| 150 | }
|
|---|
| 151 | return status;
|
|---|
| 152 | };
|
|---|
| 153 |
|
|---|
| 154 | /** Removes a given bond from atom::ListOfBonds.
|
|---|
| 155 | * @param _step time step to access
|
|---|
| 156 | * \param *Binder bond to remove
|
|---|
| 157 | */
|
|---|
| 158 | bool BondedParticle::UnregisterBond(bond *Binder)
|
|---|
| 159 | {
|
|---|
| 160 | OBSERVE;
|
|---|
| 161 | bool status = false;
|
|---|
| 162 | ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL.");
|
|---|
| 163 | const int step = ContainsBondAtStep(Binder);
|
|---|
| 164 | if (step != -1) {
|
|---|
| 165 | NOTIFY(AtomObservable::BondsChanged);
|
|---|
| 166 | //LOG(3,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
|
|---|
| 167 | ListOfBonds[step].remove(Binder);
|
|---|
| 168 | status = true;
|
|---|
| 169 | } else {
|
|---|
| 170 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
|
|---|
| 171 | }
|
|---|
| 172 | return status;
|
|---|
| 173 | };
|
|---|
| 174 |
|
|---|
| 175 | /** Removes all bonds from atom::ListOfBonds.
|
|---|
| 176 | * \note Does not do any memory de-allocation.
|
|---|
| 177 | */
|
|---|
| 178 | void BondedParticle::UnregisterAllBond(const unsigned int _step)
|
|---|
| 179 | {
|
|---|
| 180 | ListOfBonds[_step].clear();
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 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 | {
|
|---|
| 189 | OBSERVE;
|
|---|
| 190 | if (WorldTime::getTime() == _step)
|
|---|
| 191 | NOTIFY(AtomObservable::BondsChanged);
|
|---|
| 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 |
|
|---|
| 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 | */
|
|---|
| 206 | int BondedParticle::ContainsBondAtStep(bond *Binder) const
|
|---|
| 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 |
|
|---|
| 228 | /** Corrects the bond degree by one at most if necessary.
|
|---|
| 229 | * \return number of corrections done
|
|---|
| 230 | */
|
|---|
| 231 | int BondedParticle::CorrectBondDegree()
|
|---|
| 232 | {
|
|---|
| 233 | OBSERVE;
|
|---|
| 234 | NOTIFY(AtomObservable::BondsChanged);
|
|---|
| 235 | int NoBonds = 0;
|
|---|
| 236 | int OtherNoBonds = 0;
|
|---|
| 237 | int FalseBondDegree = 0;
|
|---|
| 238 | atom *OtherWalker = NULL;
|
|---|
| 239 | bond *CandidateBond = NULL;
|
|---|
| 240 |
|
|---|
| 241 | NoBonds = CountBonds();
|
|---|
| 242 | //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl;
|
|---|
| 243 | if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
|
|---|
| 244 | const BondList& ListOfBonds = getListOfBonds();
|
|---|
| 245 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
|
|---|
| 246 | OtherWalker = (*Runner)->GetOtherAtom(this);
|
|---|
| 247 | OtherNoBonds = OtherWalker->CountBonds();
|
|---|
| 248 | //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl;
|
|---|
| 249 | if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate
|
|---|
| 250 | const BondList& OtherListOfBonds = OtherWalker->getListOfBonds();
|
|---|
| 251 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first
|
|---|
| 252 | CandidateBond = (*Runner);
|
|---|
| 253 | //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl;
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 | }
|
|---|
| 257 | if ((CandidateBond != NULL)) {
|
|---|
| 258 | CandidateBond->BondDegree++;
|
|---|
| 259 | //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl;
|
|---|
| 260 | } else {
|
|---|
| 261 | DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl);
|
|---|
| 262 | FalseBondDegree++;
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 | return FalseBondDegree;
|
|---|
| 266 | };
|
|---|
| 267 |
|
|---|
| 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;
|
|---|
| 275 | const BondList& ListOfBonds = getListOfBonds();
|
|---|
| 276 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
|---|
| 277 | Runner != ListOfBonds.end();
|
|---|
| 278 | (++Runner))
|
|---|
| 279 | NoBonds += (*Runner)->BondDegree;
|
|---|
| 280 | return NoBonds;
|
|---|
| 281 | };
|
|---|
| 282 |
|
|---|
| 283 | /** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
|
|---|
| 284 | * @param _step time step to access
|
|---|
| 285 | * \param *BondPartner atom to check for
|
|---|
| 286 | * \return true - bond exists, false - bond does not exist
|
|---|
| 287 | */
|
|---|
| 288 | bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
|
|---|
| 289 | {
|
|---|
| 290 | bool status = false;
|
|---|
| 291 |
|
|---|
| 292 | const BondList& ListOfBonds = getListOfBondsAtStep(_step);
|
|---|
| 293 | for (BondList::const_iterator runner = ListOfBonds.begin();
|
|---|
| 294 | runner != ListOfBonds.end();
|
|---|
| 295 | runner++) {
|
|---|
| 296 | status = status || ((*runner)->Contains(BondPartner));
|
|---|
| 297 | }
|
|---|
| 298 | return status;
|
|---|
| 299 | };
|
|---|
| 300 |
|
|---|
| 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 |
|
|---|