Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/molecule.cpp

    rf17e1c raafd77  
    4949molecule::molecule(const periodentafel * const teil) :
    5050  Observable("molecule"),
    51   elemente(teil),  MDSteps(0),  BondCount(0), NoNonHydrogen(0), NoNonBonds(0),
     51  elemente(teil),  MDSteps(0),  BondCount(0), ElementCount(0), NoNonHydrogen(0), NoNonBonds(0),
    5252  NoCyclicBonds(0), BondDistance(0.),  ActiveFlag(false), IndexNr(-1),
     53  formula(this,boost::bind(&molecule::calcFormula,this),"formula"),
    5354  AtomCount(this,boost::bind(&molecule::doCountAtoms,this),"AtomCount"), last_atom(0),  InternalPointer(atoms.begin())
    5455{
    5556
     57  // other stuff
     58  for(int i=MAX_ELEMENTS;i--;)
     59    ElementsInMolecule[i] = 0;
    5660  strcpy(name,World::getInstance().getDefaultName().c_str());
    5761};
     
    97101}
    98102
    99 const Formula &molecule::getFormula(){
    100   return formula;
    101 }
    102 
    103 unsigned int molecule::getElementCount(){
    104   return formula.getElementCount();
    105 }
    106 
    107 bool molecule::hasElement(const element *element) const{
    108   return formula.hasElement(element);
    109 }
    110 
    111 bool molecule::hasElement(atomicNumber_t Z) const{
    112   return formula.hasElement(Z);
    113 }
    114 
    115 bool molecule::hasElement(const string &shorthand) const{
    116   return formula.hasElement(shorthand);
     103const std::string molecule::getFormula(){
     104  return *formula;
     105}
     106
     107std::string molecule::calcFormula(){
     108  std::map<atomicNumber_t,unsigned int> counts;
     109  stringstream sstr;
     110  periodentafel *periode = World::getInstance().getPeriode();
     111  for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
     112    counts[(*iter)->type->getNumber()]++;
     113  }
     114  std::map<atomicNumber_t,unsigned int>::reverse_iterator iter;
     115  for(iter = counts.rbegin(); iter != counts.rend(); ++iter) {
     116    atomicNumber_t Z = (*iter).first;
     117    sstr << periode->FindElement(Z)->symbol << (*iter).second;
     118  }
     119  return sstr.str();
    117120}
    118121
     
    207210    pointer->sort = &pointer->nr;
    208211    if (pointer->type != NULL) {
    209       formula += pointer->type;
     212      if (ElementsInMolecule[pointer->type->Z] == 0)
     213        ElementCount++;
     214      ElementsInMolecule[pointer->type->Z]++; // increase number of elements
    210215      if (pointer->type->Z != 1)
    211216        NoNonHydrogen++;
     
    646651
    647652  // copy values
     653  copy->CountElements();
    648654  if (hasBondStructure()) {  // if adjaceny list is present
    649655    copy->BondDistance = BondDistance;
     
    768774  ASSERT(pointer, "Null pointer passed to molecule::RemoveAtom().");
    769775  OBSERVE;
    770   formula-=pointer->type;
     776  if (ElementsInMolecule[pointer->type->Z] != 0)  { // this would indicate an error
     777    ElementsInMolecule[pointer->type->Z]--;  // decrease number of atom of this element
     778  } else
     779    DoeLog(1) && (eLog()<< Verbose(1) << "Atom " << pointer->getName() << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl);
     780  if (ElementsInMolecule[pointer->type->Z] == 0)  // was last atom of this element?
     781    ElementCount--;
    771782  RemoveBonds(pointer);
    772783  erase(pointer);
     
    782793  if (pointer == NULL)
    783794    return false;
    784   formula-=pointer->type;
     795  if (ElementsInMolecule[pointer->type->Z] != 0)  // this would indicate an error
     796    ElementsInMolecule[pointer->type->Z]--; // decrease number of atom of this element
     797  else
     798    DoeLog(1) && (eLog()<< Verbose(1) << "Atom " << pointer->getName() << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl);
     799  if (ElementsInMolecule[pointer->type->Z] == 0)  // was last atom of this element?
     800    ElementCount--;
    785801  erase(pointer);
    786802  return true;
     
    855871{
    856872  int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
     873  CountElements();
    857874
    858875  for (int i=0;i<MAX_ELEMENTS;++i) {
     
    881898{
    882899  int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
     900  CountElements();
    883901
    884902  if (output == NULL) {
     
    922940bool molecule::Checkout(ofstream * const output)  const
    923941{
    924   return formula.checkOut(output);
     942  return elemente->Checkout(output, ElementsInMolecule);
    925943};
    926944
     
    980998};
    981999
     1000/** Brings molecule::ElementCount and molecule::ElementsInMolecule up-to-date.
     1001 */
     1002void molecule::CountElements()
     1003{
     1004  for(int i=MAX_ELEMENTS;i--;)
     1005    ElementsInMolecule[i] = 0;
     1006  ElementCount = 0;
     1007
     1008  SetIndexedArrayForEachAtomTo ( ElementsInMolecule, &element::Z, &Increment, 1);
     1009
     1010  for(int i=MAX_ELEMENTS;i--;)
     1011    ElementCount += (ElementsInMolecule[i] != 0 ? 1 : 0);
     1012};
     1013
    9821014/** Determines whether two molecules actually contain the same atoms and coordination.
    9831015 * \param *out output stream for debugging
     
    9981030  /// first count both their atoms and elements and update lists thereby ...
    9991031  //Log() << Verbose(0) << "Counting atoms, updating list" << endl;
     1032  CountElements();
     1033  OtherMolecule->CountElements();
    10001034
    10011035  /// ... and compare:
     
    10071041    } else Log() << Verbose(4) << "AtomCounts match: " << getAtomCount() << " == " << OtherMolecule->getAtomCount() << endl;
    10081042  }
    1009   /// -# Formula
     1043  /// -# ElementCount
    10101044  if (result) {
    1011     if (formula != OtherMolecule->formula) {
    1012       DoLog(4) && (Log() << Verbose(4) << "Formulas don't match: " << formula << " == " << OtherMolecule->formula << endl);
     1045    if (ElementCount != OtherMolecule->ElementCount) {
     1046      DoLog(4) && (Log() << Verbose(4) << "ElementCount don't match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl);
    10131047      result = false;
    1014     } else Log() << Verbose(4) << "Formulas match: " << formula << " == " << OtherMolecule->formula << endl;
     1048    } else Log() << Verbose(4) << "ElementCount match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl;
     1049  }
     1050  /// -# ElementsInMolecule
     1051  if (result) {
     1052    for (flag=MAX_ELEMENTS;flag--;) {
     1053      //Log() << Verbose(5) << "Element " <<  flag << ": " << ElementsInMolecule[flag] << " <-> " << OtherMolecule->ElementsInMolecule[flag] << "." << endl;
     1054      if (ElementsInMolecule[flag] != OtherMolecule->ElementsInMolecule[flag])
     1055        break;
     1056    }
     1057    if (flag < MAX_ELEMENTS) {
     1058      DoLog(4) && (Log() << Verbose(4) << "ElementsInMolecule don't match." << endl);
     1059      result = false;
     1060    } else Log() << Verbose(4) << "ElementsInMolecule match." << endl;
    10151061  }
    10161062  /// then determine and compare center of gravity for each molecule ...
Note: See TracChangeset for help on using the changeset viewer.