Ignore:
Timestamp:
Oct 18, 2009, 2:51:38 PM (16 years ago)
Author:
Frederik Heber <heber@…>
Children:
77894f
Parents:
b0ee98
git-author:
Frederik Heber <heber@…> (10/18/09 14:15:37)
git-committer:
Frederik Heber <heber@…> (10/18/09 14:51:38)
Message:

Huge refactoring: molecule::ListOfBondsPerAtom and molecule::NumberOfBondsPerAtom removed, atom::ListOfBonds introduced. Unit Test for ListOfBonds manipulation introduced.

  • changes to builder.cpp: removed CreateListOfBondsPerAtom() calls, as the creation of the global arrays is not necessary anymore
  • changes to LinkedCell: LinkedCell::CheckBounds(int[NDIM]) does not admonish out of bonds as this is not desired for the local offset which may become out of bounds.
  • changes to lists.hpp templates: BUGFIX: unlink() now sets ->next and ->previous to NULL, cleanup() uses removedwithoutcheck()
  • new templates for molecule.hpp: SumPerAtom() allows for summation of the return value of atom:...() member fiunctions. This is needed e.g. for atom::CorrectBondDegree()

Signed-off-by: Frederik Heber <heber@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/atom.cpp

    rb0ee98 r872b51  
    99#include "config.hpp"
    1010#include "element.hpp"
     11#include "lists.hpp"
    1112#include "memoryallocator.hpp"
    1213#include "parser.hpp"
     
    6768atom::~atom()
    6869{
     70  BondList::const_iterator Runner;
     71  while (!ListOfBonds.empty()) {
     72    Runner = ListOfBonds.begin();
     73    removewithoutcheck(*Runner);
     74  }
     75  unlink(this);
    6976  Free<int>(&ComponentNr, "atom::~atom: *ComponentNr");
    7077  Free<char>(&Name, "atom::~atom: *Name");
     
    118125{
    119126  return (node->IsInParallelepiped(offset, parallelepiped));
     127};
     128
     129/** Counts the number of bonds weighted by bond::BondDegree.
     130 * \param bonds times bond::BondDegree
     131 */
     132int atom::CountBonds() const
     133{
     134  int NoBonds = 0;
     135  for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
     136    NoBonds += (*Runner)->BondDegree;
     137  return NoBonds;
    120138};
    121139
     
    215233};
    216234
    217 /** Prints all bonds of this atom from given global lists.
     235/** Output graph info of this atom.
     236 * \param *out output stream
     237 */
     238void atom::OutputGraphInfo(ofstream *out) const
     239{
     240  *out << Verbose(2) << "Atom " << Name << " is " << ((SeparationVertex) ? "a" : "not a") << " separation vertex, components are ";
     241  OutputComponentNumber(out);
     242  *out << " with Lowpoint " << LowpointNr << " and Graph Nr. " << GraphNr << "." << endl;
     243};
     244
     245/** Output a list of flags, stating whether the bond was visited or not.
     246 * Note, we make use of the last entry of the ComponentNr always being -1 if allocated.
     247 * \param *out output stream for debugging
     248 */
     249void atom::OutputComponentNumber(ofstream *out) const
     250{
     251  if (ComponentNr != NULL) {
     252    for (int i=0; ComponentNr[i] != -1; i++)
     253      *out << ComponentNr[i] << " ";
     254  }
     255};
     256
     257
     258/** Prints all bonds of this atom with total degree.
    218259 * \param *out stream to output to
    219  * \param *NumberOfBondsPerAtom array with number of bonds per atomic index
    220  * \param ***ListOfBondsPerAtom array per atomic index of array with pointer to bond
    221260 * \return true - \a *out present, false - \a *out is NULL
    222261 */
    223 bool atom::OutputBondOfAtom(ofstream *out, int *NumberOfBondsPerAtom, bond ***ListOfBondsPerAtom) const
     262bool atom::OutputBondOfAtom(ofstream *out) const
    224263{
    225264  if (out != NULL) {
     
    227266    if (type->Z != 1) {   // regard only non-hydrogen
    228267#endif
    229       *out << Verbose(4) << "Atom " << Name << "/" << nr << " with " << NumberOfBondsPerAtom[nr] << " bonds: ";
     268      *out << Verbose(4) << "Atom " << Name << "/" << nr << " with " << ListOfBonds.size() << " bonds: ";
    230269      int TotalDegree = 0;
    231       for (int j=0;j<NumberOfBondsPerAtom[nr];j++) {
    232         *out << *ListOfBondsPerAtom[nr][j] << "\t";
    233         TotalDegree += ListOfBondsPerAtom[nr][j]->BondDegree;
     270      for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
     271        *out << **Runner << "\t";
     272        TotalDegree += (*Runner)->BondDegree;
    234273      }
    235274      *out << " -- TotalDegree: " << TotalDegree << endl;
     
    240279  } else
    241280    return false;
     281};
     282
     283/** Output of atom::nr along with all bond partners.
     284 * \param *AdjacencyFile output stream
     285 */
     286void atom::OutputAdjacency(ofstream *AdjacencyFile) const
     287{
     288  *AdjacencyFile << nr << "\t";
     289  for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
     290    *AdjacencyFile << (*Runner)->GetOtherAtom(this)->nr << "\t";
     291  *AdjacencyFile << endl;
     292};
     293
     294/** Puts a given bond into atom::ListOfBonds.
     295 * \param *Binder bond to insert
     296 */
     297bool atom::RegisterBond(bond *Binder)
     298{
     299  bool status = false;
     300  if (Binder != NULL) {
     301    if (Binder->Contains(this)) {
     302      ListOfBonds.push_back(Binder);
     303      status = true;
     304    } else {
     305      cout << Verbose(1) << "ERROR: " << *Binder << " does not contain " << *this << "." << endl;
     306    }
     307  } else {
     308    cout << Verbose(1) << "ERROR: Binder is " << Binder << "." << endl;
     309  }
     310  return status;
     311};
     312
     313/** Removes a given bond from atom::ListOfBonds.
     314 * \param *Binder bond to remove
     315 */
     316bool atom::UnregisterBond(bond *Binder)
     317{
     318  bool status = false;
     319  if (Binder != NULL) {
     320    if (Binder->Contains(this)) {
     321      ListOfBonds.remove(Binder);
     322      status = true;
     323    } else {
     324      cout << Verbose(1) << "ERROR: " << *Binder << " does not contain " << *this << "." << endl;
     325    }
     326  } else {
     327    cout << Verbose(1) << "ERROR: Binder is " << Binder << "." << endl;
     328  }
     329  return status;
     330};
     331
     332/** Removes all bonds from atom::ListOfBonds.
     333 * \note Does not do any memory de-allocation.
     334 */
     335void atom::UnregisterAllBond()
     336{
     337  ListOfBonds.clear();
     338};
     339
     340/** Corrects the bond degree by one at most if necessary.
     341 * \param *out output stream for debugging
     342 */
     343int atom::CorrectBondDegree(ofstream *out)
     344{
     345  int NoBonds = 0;
     346  int OtherNoBonds = 0;
     347  int FalseBondDegree = 0;
     348  atom *OtherWalker = NULL;
     349  bond *CandidateBond = NULL;
     350
     351  *out << Verbose(3) << "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl;
     352  NoBonds = CountBonds();
     353  if ((int)(type->NoValenceOrbitals) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
     354    for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
     355      OtherWalker = (*Runner)->GetOtherAtom(this);
     356      OtherNoBonds = OtherWalker->CountBonds();
     357      *out << Verbose(3) << "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << NoBonds << "?" << endl;
     358      if ((int)(OtherWalker->type->NoValenceOrbitals) > NoBonds) { // check if possible candidate
     359        if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherWalker->ListOfBonds.size())) { // pick the one with fewer number of bonds first
     360          CandidateBond = (*Runner);
     361          *out << Verbose(3) << "New candidate is " << *CandidateBond << "." << endl;
     362        }
     363      }
     364    }
     365    if ((CandidateBond != NULL)) {
     366      CandidateBond->BondDegree++;
     367      *out << Verbose(2) << "Increased bond degree for bond " << *CandidateBond << "." << endl;
     368    } else {
     369      *out << Verbose(2) << "Could not find correct degree for atom " << *this << "." << endl;
     370      FalseBondDegree++;
     371    }
     372  }
     373  return FalseBondDegree;
    242374};
    243375
     
    345477{
    346478  *file << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
    347   //cout << Verbose(2) << "Storing: " << Walker->nr << "\t" << (int)Walker->AdaptiveOrder << "\t" << (int)Walker->MaxOrder << "." << endl;
     479  //cout << Verbose(2) << "Storing: " << nr << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << "." << endl;
    348480}
    349481
Note: See TracChangeset for help on using the changeset viewer.