Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/molecule.cpp

    rd103d3 r59fff1  
    150150
    151151molecule::iterator molecule::begin(){
    152   return molecule::iterator(atoms.begin(),this);
     152  return iterator(atomIds.begin(), FromIdToAtom());
    153153}
    154154
    155155molecule::const_iterator molecule::begin() const{
    156   return atoms.begin();
     156  return const_iterator(atomIds.begin(), FromIdToAtom());
    157157}
    158158
    159159molecule::iterator molecule::end(){
    160   return molecule::iterator(atoms.end(),this);
     160  return iterator(atomIds.end(), FromIdToAtom());
    161161}
    162162
    163163molecule::const_iterator molecule::end() const{
    164   return atoms.end();
     164  return const_iterator(atomIds.end(), FromIdToAtom());
    165165}
    166166
    167167bool molecule::empty() const
    168168{
    169   return (begin() == end());
     169  return (atomIds.empty());
    170170}
    171171
     
    173173{
    174174  size_t counter = 0;
    175   for (molecule::const_iterator iter = begin(); iter != end (); ++iter)
     175  for (const_iterator iter = begin(); iter != end (); ++iter)
    176176    counter++;
    177177  return counter;
     
    181181{
    182182  OBSERVE;
    183   molecule::const_iterator iter = loc;
    184   iter++;
    185   atom* atom = *loc;
    186   atomIds.erase( atom->getId() );
    187   atoms.remove( atom );
    188   formula-=atom->getType();
    189   atom->removeFromMolecule();
     183  const_iterator iter = loc;
     184  ++iter;
     185  atom * const _atom = const_cast<atom *>(*loc);
     186  atomIds.erase( _atom->getId() );
     187  formula-=_atom->getType();
     188  _atom->removeFromMolecule();
    190189  return iter;
    191190}
     
    194193{
    195194  OBSERVE;
    196   molecule::const_iterator iter = find(key);
     195  const_iterator iter = find(key);
    197196  if (iter != end()){
    198     iter++;
     197    ++iter;
    199198    atomIds.erase( key->getId() );
    200     atoms.remove( key );
    201199    formula-=key->getType();
    202200    key->removeFromMolecule();
     
    207205molecule::const_iterator molecule::find ( atom * key ) const
    208206{
    209   molecule::const_iterator iter;
    210   for (molecule::const_iterator Runner = begin(); Runner != end(); ++Runner) {
    211     if (*Runner == key)
    212       return molecule::const_iterator(Runner);
    213   }
    214   return molecule::const_iterator(atoms.end());
     207  return const_iterator(atomIds.find(key->getId()), FromIdToAtom());
    215208}
    216209
     
    220213  pair<atomIdSet::iterator,bool> res = atomIds.insert(key->getId());
    221214  if (res.second) { // push atom if went well
    222     atoms.push_back(key);
    223215    formula+=key->getType();
    224     return pair<iterator,bool>(molecule::iterator(--end()),res.second);
     216    return pair<iterator,bool>(iterator(res.first, FromIdToAtom()),res.second);
    225217  } else {
    226     return pair<iterator,bool>(molecule::iterator(end()),res.second);
     218    return pair<iterator,bool>(end(),res.second);
    227219  }
    228220}
     
    235227{
    236228  World::AtomComposite vector_of_atoms;
    237   BOOST_FOREACH(atom *_atom, atoms)
    238     vector_of_atoms.push_back(_atom);
     229//  std::copy(MyIter(atomIds.begin(), FromIdToAtom()),
     230//      MyIter(atomIds.end(), FromIdToAtom()),
     231//      vector_of_atoms.begin());
     232//  for (MyIter iter = MyIter(atomIds.begin(), FromIdToAtom());
     233//      iter != MyIter(atomIds.end(), FromIdToAtom());
     234//      ++iter)
     235  for (molecule::iterator iter = begin(); iter != end(); ++iter)
     236    vector_of_atoms.push_back(*iter);
    239237  return vector_of_atoms;
    240238}
     
    635633
    636634  // copy all atoms
    637   for_each(atoms.begin(),atoms.end(),bind1st(mem_fun(&molecule::AddCopyAtom),copy));
     635  std::map< const atom *, atom *> FatherFinder;
     636  for (iterator iter = begin(); iter != end(); ++iter) {
     637    atom * const copy_atom = copy->AddCopyAtom(*iter);
     638    FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
     639  }
     640
     641  // copy all bonds
     642  for(const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
     643    const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
     644    for(BondList::const_iterator BondRunner = ListOfBonds.begin();
     645        BondRunner != ListOfBonds.end();
     646        ++BondRunner)
     647      if ((*BondRunner)->leftatom == *AtomRunner) {
     648        bond *Binder = (*BondRunner);
     649        // get the pendant atoms of current bond in the copy molecule
     650        ASSERT(FatherFinder.count(Binder->leftatom),
     651            "molecule::CopyMolecule() - No copy of original left atom "
     652            +toString(Binder->leftatom)+" for bond copy found");
     653        ASSERT(FatherFinder.count(Binder->rightatom),
     654            "molecule::CopyMolecule() - No copy of original right atom "
     655            +toString(Binder->rightatom)+"  for bond copy found");
     656        atom * const LeftAtom = FatherFinder[Binder->leftatom];
     657        atom * const RightAtom = FatherFinder[Binder->rightatom];
     658
     659        bond * const NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->BondDegree);
     660        NewBond->Cyclic = Binder->Cyclic;
     661        if (Binder->Cyclic)
     662          copy->NoCyclicBonds++;
     663        NewBond->Type = Binder->Type;
     664      }
     665  }
     666  // correct fathers
     667  //for_each(begin(),end(),mem_fun(&atom::CorrectFather));
     668
     669  return copy;
     670};
     671
     672
     673/** Destroys all atoms inside this molecule.
     674 */
     675void molecule::removeAtomsinMolecule()
     676{
     677  // remove each atom from world
     678  for(iterator AtomRunner = begin(); !empty(); AtomRunner = begin())
     679    World::getInstance().destroyAtom(*AtomRunner);
     680};
     681
     682
     683/**
     684 * Copies all atoms of a molecule which are within the defined parallelepiped.
     685 *
     686 * @param offest for the origin of the parallelepiped
     687 * @param three vectors forming the matrix that defines the shape of the parallelpiped
     688 */
     689molecule* molecule::CopyMoleculeFromSubRegion(const Shape &region) const {
     690  molecule *copy = World::getInstance().createMolecule();
     691
     692  // copy all atoms
     693  std::map< const atom *, atom *> FatherFinder;
     694  for (iterator iter = begin(); iter != end(); ++iter) {
     695    if((*iter)->IsInShape(region)){
     696      atom * const copy_atom = copy->AddCopyAtom(*iter);
     697      FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
     698    }
     699  }
    638700
    639701  // copy all bonds
     
    645707      if ((*BondRunner)->leftatom == *AtomRunner) {
    646708        bond *Binder = (*BondRunner);
    647         // get the pendant atoms of current bond in the copy molecule
    648         atomSet::iterator leftiter=find_if(copy->atoms.begin(),copy->atoms.end(),bind2nd(mem_fun(&atom::isFather),Binder->leftatom));
    649         atomSet::iterator rightiter=find_if(copy->atoms.begin(),copy->atoms.end(),bind2nd(mem_fun(&atom::isFather),Binder->rightatom));
    650         ASSERT(leftiter!=copy->atoms.end(),"No copy of original left atom for bond copy found");
    651         ASSERT(leftiter!=copy->atoms.end(),"No copy of original right atom for bond copy found");
    652         atom *LeftAtom = *leftiter;
    653         atom *RightAtom = *rightiter;
    654 
    655         bond *NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->BondDegree);
    656         NewBond->Cyclic = Binder->Cyclic;
    657         if (Binder->Cyclic)
    658           copy->NoCyclicBonds++;
    659         NewBond->Type = Binder->Type;
     709        if ((FatherFinder.count(Binder->leftatom))
     710            && (FatherFinder.count(Binder->rightatom))) {
     711          // if copy present, then it must be from subregion
     712          atom * const LeftAtom = FatherFinder[Binder->leftatom];
     713          atom * const RightAtom = FatherFinder[Binder->rightatom];
     714
     715          bond * const NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->BondDegree);
     716          NewBond->Cyclic = Binder->Cyclic;
     717          if (Binder->Cyclic)
     718            copy->NoCyclicBonds++;
     719          NewBond->Type = Binder->Type;
     720        }
    660721      }
    661722  }
    662723  // correct fathers
    663   //for_each(atoms.begin(),atoms.end(),mem_fun(&atom::CorrectFather));
    664 
    665   return copy;
    666 };
    667 
    668 
    669 /** Destroys all atoms inside this molecule.
    670  */
    671 void molecule::removeAtomsinMolecule()
    672 {
    673   // remove each atom from world
    674   for(molecule::const_iterator AtomRunner = begin(); !empty(); AtomRunner = begin())
    675     World::getInstance().destroyAtom(*AtomRunner);
    676 };
    677 
    678 
    679 /**
    680  * Copies all atoms of a molecule which are within the defined parallelepiped.
    681  *
    682  * @param offest for the origin of the parallelepiped
    683  * @param three vectors forming the matrix that defines the shape of the parallelpiped
    684  */
    685 molecule* molecule::CopyMoleculeFromSubRegion(const Shape &region) const {
    686   molecule *copy = World::getInstance().createMolecule();
    687 
    688   BOOST_FOREACH(atom *iter,atoms){
    689     if(iter->IsInShape(region)){
    690       copy->AddCopyAtom(iter);
    691     }
    692   }
     724  //for_each(begin(),end(),mem_fun(&atom::CorrectFather));
    693725
    694726  //TODO: copy->BuildInducedSubgraph(this);
     
    709741
    710742  // some checks to make sure we are able to create the bond
    711   ASSERT(atom1, "First atom in bond-creation was an invalid pointer");
    712   ASSERT(atom2, "Second atom in bond-creation was an invalid pointer");
    713   ASSERT(FindAtom(atom1->getNr()),"First atom in bond-creation was not part of molecule");
    714   ASSERT(FindAtom(atom2->getNr()),"Second atom in bond-creation was not part of molecule");
     743  ASSERT(atom1,
     744      "molecule::AddBond() - First atom "+toString(atom1)
     745      +" is not a invalid pointer");
     746  ASSERT(atom2,
     747      "molecule::AddBond() - Second atom "+toString(atom2)
     748      +" is not a invalid pointer");
     749  ASSERT(isInMolecule(atom1),
     750      "molecule::AddBond() - First atom "+toString(atom1)
     751      +" is not part of molecule");
     752  ASSERT(isInMolecule(atom2),
     753      "molecule::AddBond() - Second atom "+toString(atom2)
     754      +" is not part of molecule");
    715755
    716756  Binder = new bond(atom1, atom2, degree);
    717757  atom1->RegisterBond(WorldTime::getTime(), Binder);
    718758  atom2->RegisterBond(WorldTime::getTime(), Binder);
    719   if ((atom1->getType() != NULL) && (atom1->getType()->getAtomicNumber() != 1) && (atom2->getType() != NULL) && (atom2->getType()->getAtomicNumber() != 1))
     759  if ((atom1->getType() != NULL)
     760      && (atom1->getType()->getAtomicNumber() != 1)
     761      && (atom2->getType() != NULL)
     762      && (atom2->getType()->getAtomicNumber() != 1))
    720763    NoNonBonds++;
    721764
     
    820863atom * molecule::FindAtom(int Nr)  const
    821864{
    822   molecule::const_iterator iter = begin();
     865  molecule::iterator iter = begin();
    823866  for (; iter != end(); ++iter)
    824     if ((*iter)->getNr() == Nr)
    825       break;
     867  if ((*iter)->getNr() == Nr)
     868    break;
    826869  if (iter != end()) {
    827870    //LOG(0, "Found Atom Nr. " << walker->getNr());
    828871    return (*iter);
    829872  } else {
    830     LOG(0, "Atom not found in list.");
     873    ELOG(1, "Atom not found in molecule " << getName() << "'s list.");
    831874    return NULL;
    832875  }
    833 };
     876}
     877
     878/** Checks whether the given atom is a member of this molecule.
     879 *
     880 *  We make use here of molecule::atomIds to get a result on
     881 *
     882 * @param _atom atom to check
     883 * @return true - is member, false - is not
     884 */
     885bool molecule::isInMolecule(const atom * const _atom)
     886{
     887  ASSERT(_atom->getMolecule() == this,
     888      "molecule::isInMolecule() - atom is not designated to be in molecule '"
     889      +toString(this->getName())+"'.");
     890  molecule::atomIdSet::const_iterator iter = atomIds.find(_atom->getId());
     891  return (iter != atomIds.end());
     892}
    834893
    835894/** Asks for atom number, and checks whether in list.
     
    878937    enumeration<const element*> elementLookup = formula.enumerateElements();
    879938    *output << "#Ion_TypeNr._Nr.R[0]    R[1]    R[2]    MoveType (0 MoveIon, 1 FixedIon)" << endl;
    880     for_each(atoms.begin(),atoms.end(),boost::bind(&atom::OutputArrayIndexed,_1,output,elementLookup,AtomNo,(const char*)0));
     939    for_each(begin(),end(),boost::bind(&atom::OutputArrayIndexed,_1,output,elementLookup,AtomNo,(const char*)0));
    881940    return true;
    882941  }
     
    900959      memset(AtomNo,0,(MAX_ELEMENTS-1)*sizeof(*AtomNo));
    901960      enumeration<const element*> elementLookup = formula.enumerateElements();
    902       for_each(atoms.begin(),atoms.end(),boost::bind(&atom::OutputTrajectory,_1,output,elementLookup, AtomNo, (const int)step));
     961      for_each(begin(),end(),boost::bind(&atom::OutputTrajectory,_1,output,elementLookup, AtomNo, (const int)step));
    903962    }
    904963    return true;
     
    9411000    for (int step=0;step<MDSteps;step++) {
    9421001      *output << getAtomCount() << "\n\tCreated by molecuilder, step " << step << ", on " << ctime(&now);
    943       for_each(atoms.begin(),atoms.end(),boost::bind(&atom::OutputTrajectoryXYZ,_1,output,step));
     1002      for_each(begin(),end(),boost::bind(&atom::OutputTrajectoryXYZ,_1,output,step));
    9441003    }
    9451004    return true;
     
    9581017    now = time((time_t *)NULL);   // Get the system time and put it into 'now' as 'calender time'
    9591018    *output << getAtomCount() << "\n\tCreated by molecuilder on " << ctime(&now);
    960     for_each(atoms.begin(),atoms.end(),bind2nd(mem_fun(&atom::OutputXYZLine),output));
     1019    for_each(begin(),end(),bind2nd(mem_fun(&atom::OutputXYZLine),output));
    9611020    return true;
    9621021  } else
     
    9721031  int i = 0;
    9731032  NoNonHydrogen = 0;
    974   for (molecule::const_iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
     1033  for (molecule::iterator iter = begin(); iter != end(); ++iter) {
    9751034    (*iter)->setNr(i);   // update number in molecule (for easier referencing in FragmentMolecule lateron)
    9761035    if ((*iter)->getType()->getAtomicNumber() != 1) // count non-hydrogen atoms whilst at it
Note: See TracChangeset for help on using the changeset viewer.