Changeset 7d82a5 for src/Atom


Ignore:
Timestamp:
Feb 13, 2013, 3:47:38 PM (12 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
b4b364
Parents:
c8302f3
git-author:
Frederik Heber <heber@…> (11/02/12 13:38:08)
git-committer:
Frederik Heber <heber@…> (02/13/13 15:47:38)
Message:

Changed bond::ptr into boost::shared_ptr.

Changes:

  • refactored some BondedParticle functions, cleaning up the interface in terms of private and public functionality, added some helper functions to find bonds.
  • ptr = NULL --> .reset().
  • now initialising to NULL.
  • BondsPerSP is now list of boost::shared_ptr as well.
  • some include of bond.hpp instead of forward references necessary.
  • leftatom and rightatom are now set to NULL on UnregisterBond().
  • bonds are not deleted anymore but simply unregistered.
  • FIX: BondedParticleInfo did unrequiredly inherit AtomObservable.
  • removed UnregisterAllBonds(), code is now in RemoveAllBonds() where UnregisterBond is called for both bond partners. This ensures that the bond is correctly released and shared_ptr free'd.
Location:
src/Atom
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/Atom/atom_bondedparticle.cpp

    rc8302f3 r7d82a5  
    114114  }
    115115
    116   bond* newBond = new bond((atom*) this, (atom*) Partner, 1);
     116  bond::ptr newBond(new bond((atom*) this, (atom*) Partner, 1));
    117117  RegisterBond(_step, newBond);
    118118  Partner->RegisterBond(_step, newBond);
     
    121121}
    122122
    123 /** Removes a bond of this atom to a given \a Partner.
    124  *
    125  * @param _step time step
    126  * @param Partner bond partner
    127  */
    128 void BondedParticle::removeBond(const unsigned int _step, BondedParticle * const Partner)
     123/** Helper function to find the time step to a given bond in \a Binder.
     124 *
     125 * \param Binder bond to look for
     126 * \return ListOfBonds::size() - not found, else - step containing \a Binder
     127 */
     128unsigned int BondedParticle::findBondsStep(bond::ptr const Binder) const
     129{
     130
     131  size_t _step = 0;
     132  for (;_step < ListOfBonds.size();++_step) {
     133    const BondList& ListOfBonds = getListOfBondsAtStep(_step);
     134    if (std::find(ListOfBonds.begin(), ListOfBonds.end(), Binder) != ListOfBonds.end())
     135      break;
     136  }
     137  return _step;
     138}
     139
     140/** Helper function to find the iterator to a bond at a given time \a step to
     141 * a given bond partner in \a Partner.
     142 *
     143 * \param _step time step to look at
     144 * \param Partner bond partner to look for
     145 * \return ListOfBonds::end() - not found, else - iterator pointing \a Binder
     146 */
     147BondList::const_iterator BondedParticle::findBondPartnerAtStep(
     148    const unsigned int _step,
     149    BondedParticle * const Partner) const
    129150{
    130151  const BondList& ListOfBonds = getListOfBondsAtStep(_step);
     
    134155          _1,
    135156          boost::cref(Partner)));
    136   if (iter != ListOfBonds.end()) {
    137     delete *iter; //dstor takes care of unregistering and all
     157  return iter;
     158}
     159
     160/** Removes a bond of this atom to a given \a Partner.
     161 *
     162 * @param _step time step
     163 * @param Partner bond partner
     164 */
     165void BondedParticle::removeBond(const unsigned int _step, BondedParticle * const Partner)
     166{
     167  BondList::const_iterator iter = findBondPartnerAtStep(_step, Partner);
     168  if (iter != getListOfBondsAtStep(_step).end()) {
     169    // iter becomes invalid upon first unregister,
     170    // hence store the bond someplace else first
     171    bond::ptr const Binder = *iter;
     172    UnregisterBond(_step, Binder);
     173    Partner->UnregisterBond(_step, Binder);
    138174  } else
    139175    ELOG(1, "BondedParticle::removeBond() - I cannot find the bond in between "
     
    145181 * @param Binder bond to remove
    146182 */
    147 void BondedParticle::removeBond(bond::ptr binder)
    148 {
    149   UnregisterBond(binder);
     183void BondedParticle::removeBond(bond::ptr &binder)
     184{
     185  if (binder != NULL) {
     186    atom * const Other = binder->GetOtherAtom(this);
     187    ASSERT( Other != NULL,
     188        "BondedParticle::removeBonds() - cannot find bond partner for "
     189        +toString(*binder)+".");
     190    // find bond at step
     191    unsigned int step = findBondsStep(binder);
     192    if (step != ListOfBonds.size()) {
     193      UnregisterBond(step, binder);
     194      Other->UnregisterBond(step, binder);
     195      binder.reset();
     196    }
     197  }
    150198}
    151199
     
    165213void BondedParticle::removeAllBonds(const unsigned int _step)
    166214{
    167   for (BondList::iterator iter = ListOfBonds[_step].begin();
    168       !ListOfBonds[_step].empty();
    169       iter = ListOfBonds[_step].begin()) {
    170     delete (*iter);
    171     // unregister/NOTIFY is done by bond::~bond()
     215  //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]);
     216  for (BondList::iterator iter = (ListOfBonds[_step]).begin();
     217      !(ListOfBonds[_step]).empty();
     218      iter = (ListOfBonds[_step]).begin()) {
     219    //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds);
     220    atom * const Other = (*iter)->GetOtherAtom(this);
     221    ASSERT( Other != NULL,
     222        "BondedParticle::removeAllBonds() - cannot find bond partner for "
     223        +toString(**iter)+".");
     224    Other->UnregisterBond(_step, *iter);
     225    UnregisterBond(_step, *iter);
    172226  }
    173227}
     
    179233bool BondedParticle::RegisterBond(const unsigned int _step, bond::ptr const Binder)
    180234{
    181   OBSERVE;
    182235  bool status = false;
    183236  if (Binder != NULL) {
     237    OBSERVE;
    184238    if (Binder->Contains(this)) {
    185239      //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
     
    200254
    201255/** Removes a given bond from atom::ListOfBonds.
     256 *
     257 * \warning This only removes this atom not its bond partner, i.e.
     258 * both atoms need to call this function to fully empty a bond.
     259 *
    202260 * @param _step time step to access
    203261 * \param *Binder bond to remove
    204262 */
    205 bool BondedParticle::UnregisterBond(bond::ptr const Binder)
    206 {
    207   OBSERVE;
     263bool BondedParticle::UnregisterBond(const unsigned int _step, bond::ptr const Binder)
     264{
    208265  bool status = false;
    209   ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL.");
    210   const int step = ContainsBondAtStep(Binder);
    211   if (step != -1) {
    212     //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
    213     ListOfBonds[step].remove(Binder);
    214     if (WorldTime::getTime() == step)
    215       NOTIFY(AtomObservable::BondsRemoved);
    216     status = true;
     266  if (Binder != NULL) {
     267    if (Binder->Contains(this)) {
     268      OBSERVE;
     269      //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
     270#ifndef NDEBUG
     271      BondList::const_iterator iter =
     272          std::find(ListOfBonds[_step].begin(), ListOfBonds[_step].end(), Binder);
     273      ASSERT( iter != ListOfBonds[_step].end(),
     274          "BondedParticle::UnregisterBond() - "+toString(*Binder)+" not contained at "
     275          +toString(_step));
     276#endif
     277      Binder->removeAtom(this);
     278      ListOfBonds[_step].remove(Binder);
     279      if (WorldTime::getTime() == _step)
     280        NOTIFY(AtomObservable::BondsRemoved);
     281      status = true;
     282    } else {
     283      ELOG(1, *Binder << " does not contain " << *this << ".");
     284    }
    217285  } else {
    218     ELOG(1, *Binder << " does not contain " << *this << ".");
     286    ELOG(1, "Binder is " << Binder << ".");
    219287  }
    220288  return status;
    221289};
    222290
    223 /** Removes all bonds from atom::ListOfBonds.
    224  * \note Does not do any memory de-allocation.
    225  */
    226 void BondedParticle::UnregisterAllBond(const unsigned int _step)
    227 {
    228   OBSERVE;
    229   NOTIFY(AtomObservable::BondsRemoved);
    230   ListOfBonds[_step].clear();
    231 }
    232 
    233291/** Removes all bonds of given \a _step with freeing memory.
    234292 *
     
    237295void BondedParticle::ClearBondsAtStep(const unsigned int _step)
    238296{
    239   OBSERVE;
    240   NOTIFY(AtomObservable::BondsRemoved);
    241   //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]);
    242   for (BondList::iterator iter = (ListOfBonds[_step]).begin();
    243       !(ListOfBonds[_step]).empty();
    244       iter = (ListOfBonds[_step]).begin()) {
    245     //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds);
    246     delete((*iter)); // will also unregister with us and remove from list
    247   }
     297  removeAllBonds(_step);
    248298}
    249299
     
    286336  int FalseBondDegree = 0;
    287337  atom *OtherWalker = NULL;
    288   bond::ptr CandidateBond = NULL;
     338  bond::ptr CandidateBond;
    289339
    290340  NoBonds = CountBonds();
  • src/Atom/atom_bondedparticle.hpp

    rc8302f3 r7d82a5  
    2424#include "atom_atominfo.hpp"
    2525#include "atom_bondedparticleinfo.hpp"
     26#include "atom_observable.hpp"
    2627#include "atom_particleinfo.hpp"
     28#include "atom_observable.hpp"
    2729
    2830/****************************************** forward declarations *****************************/
     
    3234/********************************************** declarations *******************************/
    3335
    34 class BondedParticle : public BondedParticleInfo, public virtual ParticleInfo, public virtual AtomInfo {
     36class BondedParticle :
     37    public BondedParticleInfo,
     38    public virtual ParticleInfo,
     39    public virtual AtomInfo,
     40    public virtual AtomObservable
     41{
    3542public:
    3643  BondedParticle();
     
    3946  bond::ptr const addBond(const unsigned int _step, BondedParticle* Partner);
    4047  void removeBond(const unsigned int _step, BondedParticle* Partner);
    41   void removeBond(bond::ptr binder);
     48  void removeBond(bond::ptr &binder);
    4249  void removeAllBonds();
    4350  void removeAllBonds(const unsigned int _step);
     
    5562protected:
    5663  bool RegisterBond(const unsigned int _step, bond::ptr const Binder);
    57   bool UnregisterBond(bond::ptr const Binder);
    58   void UnregisterAllBond(const unsigned int _step);
     64  bool UnregisterBond(const unsigned int _step, bond::ptr const Binder);
    5965
    6066  int ContainsBondAtStep(bond::ptr Binder) const;
    6167
     68private:
     69  unsigned int findBondsStep(bond::ptr const Binder) const;
     70  BondList::const_iterator findBondPartnerAtStep(
     71      const unsigned int _step,
     72      BondedParticle * const Partner) const;
    6273};
    6374
  • src/Atom/atom_bondedparticleinfo.hpp

    rc8302f3 r7d82a5  
    1919#endif
    2020
    21 #include "atom_observable.hpp"
     21//#include "atom_observable.hpp"
    2222#include "Bond/bond.hpp"
    2323
     
    3232/********************************************** declarations *******************************/
    3333
    34 class BondedParticleInfo  : public virtual AtomObservable {
     34class BondedParticleInfo // : public virtual AtomObservable // must be virtual(!)
     35{
    3536  friend class BondedParticle;
    3637public:
Note: See TracChangeset for help on using the changeset viewer.