[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"
|
---|
| 24 | #include "bond.hpp"
|
---|
| 25 | #include "element.hpp"
|
---|
| 26 | #include "lists.hpp"
|
---|
[ad011c] | 27 | #include "CodePatterns/Log.hpp"
|
---|
| 28 | #include "CodePatterns/Verbose.hpp"
|
---|
[6b919f8] | 29 |
|
---|
| 30 | /** Constructor of class BondedParticle.
|
---|
| 31 | */
|
---|
[70ff32] | 32 | BondedParticle::BondedParticle()
|
---|
| 33 | {
|
---|
| 34 | };
|
---|
[6b919f8] | 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 | */
|
---|
[b453f9] | 50 | void BondedParticle::OutputOrder(ofstream *file) const
|
---|
[6b919f8] | 51 | {
|
---|
| 52 | *file << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
|
---|
[e138de] | 53 | //Log() << Verbose(2) << "Storing: " << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl;
|
---|
[6b919f8] | 54 | };
|
---|
| 55 |
|
---|
| 56 | /** Prints all bonds of this atom with total degree.
|
---|
| 57 | */
|
---|
[e138de] | 58 | void BondedParticle::OutputBondOfAtom() const
|
---|
[6b919f8] | 59 | {
|
---|
[68f03d] | 60 | DoLog(4) && (Log() << Verbose(4) << "Atom " << getName() << "/" << nr << " with " << ListOfBonds.size() << " bonds: " << endl);
|
---|
[e138de] | 61 | int TotalDegree = 0;
|
---|
| 62 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
|
---|
[a67d19] | 63 | DoLog(4) && (Log() << Verbose(4) << **Runner << endl);
|
---|
[e138de] | 64 | TotalDegree += (*Runner)->BondDegree;
|
---|
| 65 | }
|
---|
[a67d19] | 66 | DoLog(4) && (Log() << Verbose(4) << " -- TotalDegree: " << TotalDegree << endl);
|
---|
[6b919f8] | 67 | };
|
---|
| 68 |
|
---|
| 69 | /** Output of atom::nr along with all bond partners.
|
---|
| 70 | * \param *AdjacencyFile output stream
|
---|
| 71 | */
|
---|
[1f1b23] | 72 | void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
|
---|
[6b919f8] | 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 |
|
---|
[1f1b23] | 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 |
|
---|
[b8d4a3] | 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 |
|
---|
[6b919f8] | 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 {
|
---|
[58ed4a] | 118 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
|
---|
[6b919f8] | 119 | }
|
---|
| 120 | } else {
|
---|
[58ed4a] | 121 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
|
---|
[6b919f8] | 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 {
|
---|
[58ed4a] | 137 | DoeLog(1) && (eLog()<< Verbose(1) << *Binder << " does not contain " << *this << "." << endl);
|
---|
[6b919f8] | 138 | }
|
---|
| 139 | } else {
|
---|
[58ed4a] | 140 | DoeLog(1) && (eLog()<< Verbose(1) << "Binder is " << Binder << "." << endl);
|
---|
[6b919f8] | 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 | */
|
---|
[e138de] | 156 | int BondedParticle::CorrectBondDegree()
|
---|
[6b919f8] | 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();
|
---|
[791138] | 165 | //Log() << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl;
|
---|
[83f176] | 166 | if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
|
---|
[6b919f8] | 167 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
|
---|
| 168 | OtherWalker = (*Runner)->GetOtherAtom(this);
|
---|
| 169 | OtherNoBonds = OtherWalker->CountBonds();
|
---|
[791138] | 170 | //Log() << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?" << endl;
|
---|
[83f176] | 171 | if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate
|
---|
[6b919f8] | 172 | if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherWalker->ListOfBonds.size())) { // pick the one with fewer number of bonds first
|
---|
| 173 | CandidateBond = (*Runner);
|
---|
[e138de] | 174 | //Log() << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl;
|
---|
[6b919f8] | 175 | }
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | if ((CandidateBond != NULL)) {
|
---|
| 179 | CandidateBond->BondDegree++;
|
---|
[791138] | 180 | //Log() << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl;
|
---|
[6b919f8] | 181 | } else {
|
---|
[58ed4a] | 182 | DoeLog(2) && (eLog()<< Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl);
|
---|
[6b919f8] | 183 | FalseBondDegree++;
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 | return FalseBondDegree;
|
---|
| 187 | };
|
---|
| 188 |
|
---|
[b70721] | 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 |
|
---|
[d74077] | 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 |
|
---|