/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * atom_bondedparticle.cpp * * Created on: Oct 19, 2009 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/MemDebug.hpp" #include "atom.hpp" #include "atom_bondedparticle.hpp" #include "Bond/bond.hpp" #include "CodePatterns/Assert.hpp" #include "CodePatterns/Log.hpp" #include "CodePatterns/Verbose.hpp" #include "element.hpp" /** Constructor of class BondedParticle. */ BondedParticle::BondedParticle() { ListOfBonds.push_back(BondList()); }; /** Destructor of class BondedParticle. */ BondedParticle::~BondedParticle() { const size_t max = ListOfBonds.size(); for (size_t i = 0; i < max; ++i) { ClearBondsAtStep(i); } }; /** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file. * \param *file output stream */ void BondedParticle::OutputOrder(ofstream *file) const { *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl; //Log() << Verbose(2) << "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl; }; /** Prints all bonds of this atom with total degree. */ void BondedParticle::OutputBondOfAtom(std::ostream &ost) const { const BondList& ListOfBonds = getListOfBonds(); ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: "; int TotalDegree = 0; for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) { ost << **Runner << "\t"; TotalDegree += (*Runner)->BondDegree; } ost << " -- TotalDegree: " << TotalDegree; }; /** Output of atom::Nr along with all bond partners. * \param *AdjacencyFile output stream */ void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const { const BondList& ListOfBonds = getListOfBonds(); *AdjacencyFile << getNr() << "\t"; for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) *AdjacencyFile << (*Runner)->GetOtherAtom(this)->getNr() << "\t"; *AdjacencyFile << endl; }; /** Output of atom::Nr along each bond partner per line. * Only bonds are printed where atom::Nr is smaller than the one of the bond partner. * \param *AdjacencyFile output stream */ void BondedParticle::OutputBonds(ofstream * const BondFile) const { const BondList& ListOfBonds = getListOfBonds(); for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) if (getNr() < (*Runner)->GetOtherAtom(this)->getNr()) *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n"; }; /** * Adds a bond between this bonded particle and another. Does nothing if this * bond already exists. * * @param _step time step to access * \param bonding partner */ void BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner) { if (IsBondedTo(_step, Partner)) { return; } bond* newBond = new bond((atom*) this, (atom*) Partner, 1); RegisterBond(_step, newBond); Partner->RegisterBond(_step, newBond); } /** Puts a given bond into atom::ListOfBonds. * @param _step time step to access * \param *Binder bond to insert */ bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder) { bool status = false; if (Binder != NULL) { if (Binder->Contains(this)) { //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step); BondList& ListOfBonds = getListOfBondsAtStep(_step); ListOfBonds.push_back(Binder); status = true; } else { DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl); } } else { DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl); } return status; }; /** Removes a given bond from atom::ListOfBonds. * @param _step time step to access * \param *Binder bond to remove */ bool BondedParticle::UnregisterBond(bond *Binder) { bool status = false; ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL."); const int step = ContainsBondAtStep(Binder); if (step != -1) { //LOG(3,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step); ListOfBonds[step].remove(Binder); status = true; } else { DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl); } return status; }; /** Removes all bonds from atom::ListOfBonds. * \note Does not do any memory de-allocation. */ void BondedParticle::UnregisterAllBond(const unsigned int _step) { ListOfBonds[_step].clear(); } /** Removes all bonds of given \a _step with freeing memory. * * @param _step time step whose bonds to free */ void BondedParticle::ClearBondsAtStep(const unsigned int _step) { //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]); for (BondList::iterator iter = (ListOfBonds[_step]).begin(); !(ListOfBonds[_step]).empty(); iter = (ListOfBonds[_step]).begin()) { //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds); delete((*iter)); // will also unregister with us and remove from list } } /** Searches for the time step where the given bond \a *Binder is a bond of this particle. * * @param Binder bond to check * @return >=0 - first time step where bond appears, -1 - bond not present in lists */ int BondedParticle::ContainsBondAtStep(bond *Binder) { int step = -1; int tempstep = 0; for(std::vector::const_iterator iter = ListOfBonds.begin(); iter != ListOfBonds.end(); ++iter,++tempstep) { for (BondList::const_iterator bonditer = iter->begin(); bonditer != iter->end(); ++bonditer) { if ((*bonditer) == Binder) { step = tempstep; break; } } if (step != -1) break; } return step; } /** Corrects the bond degree by one at most if necessary. * \return number of corrections done */ int BondedParticle::CorrectBondDegree() { int NoBonds = 0; int OtherNoBonds = 0; int FalseBondDegree = 0; atom *OtherWalker = NULL; bond *CandidateBond = NULL; NoBonds = CountBonds(); //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl; if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch const BondList& ListOfBonds = getListOfBonds(); for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) { OtherWalker = (*Runner)->GetOtherAtom(this); OtherNoBonds = OtherWalker->CountBonds(); //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl; if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate const BondList& OtherListOfBonds = OtherWalker->getListOfBonds(); if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first CandidateBond = (*Runner); //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl; } } } if ((CandidateBond != NULL)) { CandidateBond->BondDegree++; //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl; } else { DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl); FalseBondDegree++; } } return FalseBondDegree; }; /** Counts the number of bonds weighted by bond::BondDegree. * @param _step time step to access * \param bonds times bond::BondDegree */ int BondedParticle::CountBonds() const { int NoBonds = 0; const BondList& ListOfBonds = getListOfBonds(); for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) NoBonds += (*Runner)->BondDegree; return NoBonds; }; /** Checks whether there is a bond between \a this atom and the given \a *BondPartner. * @param _step time step to access * \param *BondPartner atom to check for * \return true - bond exists, false - bond does not exist */ bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const { bool status = false; const BondList& ListOfBonds = getListOfBondsAtStep(_step); for (BondList::const_iterator runner = ListOfBonds.begin(); runner != ListOfBonds.end(); runner++) { status = status || ((*runner)->Contains(BondPartner)); } return status; }; std::ostream & BondedParticle::operator << (std::ostream &ost) const { ParticleInfo::operator<<(ost); ost << "," << getPosition(); return ost; } std::ostream & operator << (std::ostream &ost, const BondedParticle &a) { a.ParticleInfo::operator<<(ost); ost << "," << a.getPosition(); return ost; }