Changeset 88d586


Ignore:
Timestamp:
Mar 3, 2010, 5:47:40 PM (15 years ago)
Author:
Tillmann Crueger <crueger@…>
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:
57adc7, 57f5cf
Parents:
1c51c8
Message:

Added mechanisms that allow reuse of IDs and changing Ids of Atoms

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/World.cpp

    r1c51c8 r88d586  
    8686atom *World::createAtom(){
    8787  OBSERVE;
    88   atom *res = NewAtom();
    89   assert(!atoms.count(currAtomId));
    90   res->setId(currAtomId++);
     88  atomId_t id = getNextAtomId();
     89  atom *res = NewAtom(id);
    9190  res->setWorld(this);
    9291  // store the atom by ID
     
    9796int World::registerAtom(atom *atom){
    9897  OBSERVE;
    99   assert(!atoms.count(currAtomId));
    100   atom->setId(currAtomId++);
     98  atomId_t id = getNextAtomId();
     99  atom->setId(id);
    101100  atom->setWorld(this);
    102101  atoms[atom->getId()] = atom;
     
    116115  DeleteAtom(atom);
    117116  atoms.erase(id);
     117  releaseAtomId(id);
     118}
     119
     120bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
     121  OBSERVE;
     122  // in case this call did not originate from inside the atom, we redirect it,
     123  // to also let it know that it has changed
     124  if(!target){
     125    target = atoms[oldId];
     126    assert(target && "Atom with that ID not found");
     127    return target->changeId(newId);
     128  }
     129  else{
     130    if(reserveAtomId(newId)){
     131      atoms.erase(oldId);
     132      atoms.insert(pair<atomId_t,atom*>(newId,target));
     133      return true;
     134    }
     135    else{
     136      return false;
     137    }
     138  }
    118139}
    119140
     
    136157  proc->signOff(this);
    137158}
    138 
     159/******************************* IDManagement *****************************/
     160
     161atomId_t World::getNextAtomId(){
     162  // see if we can reuse some Id
     163  if(atomIdPool.empty()){
     164    return currAtomId++;
     165  }
     166  else{
     167    // we give out the first ID from the pool
     168    atomId_t id = *(atomIdPool.begin());
     169    atomIdPool.erase(id);
     170  }
     171}
     172
     173void World::releaseAtomId(atomId_t id){
     174  atomIdPool.insert(id);
     175  // defragmentation of the pool
     176  set<atomId_t>::reverse_iterator iter;
     177  // go through all Ids in the pool that lie immediately below the border
     178  while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
     179    atomIdPool.erase(--currAtomId);
     180  }
     181}
     182
     183bool World::reserveAtomId(atomId_t id){
     184  if(id>=currAtomId ){
     185    // add all ids between the new one and current border as available
     186    for(atomId_t pos=currAtomId; pos<id; ++pos){
     187      atomIdPool.insert(pos);
     188    }
     189    currAtomId=id+1;
     190    return true;
     191  }
     192  else if(atomIdPool.count(id)){
     193    atomIdPool.erase(id);
     194    return true;
     195  }
     196  else{
     197    // this ID could not be reserved
     198    return false;
     199  }
     200}
    139201/******************************* Iterators ********************************/
    140202
  • src/World.hpp

    r1c51c8 r88d586  
    130130   */
    131131  void destroyAtom(atomId_t);
     132
     133  /**
     134   * used when changing an atom Id.
     135   * Unless you are calling this method from inside an atom don't fiddle with the third parameter.
     136   *
     137   * Return value indicates wether the change could be done or not.
     138   */
     139  bool changeAtomId(atomId_t oldId, atomId_t newId, atom* target=0);
    132140
    133141  /**
     
    226234
    227235private:
     236
     237  atomId_t getNextAtomId();
     238  void releaseAtomId(atomId_t);
     239  bool reserveAtomId(atomId_t);
     240
    228241  periodentafel *periode;
    229242  AtomSet atoms;
     243  std::set<atomId_t> atomIdPool; //<!stores the pool for all available AtomIds below currAtomId
    230244  atomId_t currAtomId; //!< stores the next available Id for atoms
    231245  MoleculeSet molecules;
  • src/atom.cpp

    r1c51c8 r88d586  
    290290}
    291291
    292 void atom::setId(int _id) {
     292bool atom::changeId(atomId_t newId){
     293  // first we move ourselves in the world
     294  // the world lets us know if that succeeded
     295  if(world->changeAtomId(id,newId,this)){
     296    id = newId;
     297    return true;
     298  }
     299  else{
     300    return false;
     301  }
     302}
     303
     304void atom::setId(atomId_t _id) {
    293305  id=_id;
    294306}
     
    298310}
    299311
    300 atom* NewAtom(){
    301   return new atom();
    302 }
    303 
    304 void  DeleteAtom(atom* atom){
     312atom* NewAtom(atomId_t _id){
     313  atom * res =new atom();
     314  res->setId(_id);
     315  return res;
     316}
     317
     318void DeleteAtom(atom* atom){
    305319  delete atom;
    306320}
  • src/atom.hpp

    r1c51c8 r88d586  
    4040 */
    4141class atom : public TesselPoint, public TrajectoryParticle, public GraphNode, public BondedParticle, public virtual ParticleInfo, public virtual AtomInfo {
    42   friend atom* NewAtom();
     42  friend atom* NewAtom(atomId_t);
    4343  friend void  DeleteAtom(atom*);
    4444  public:
     
    8080
    8181  virtual int getId();
    82   virtual void setId(int);
     82  virtual bool changeId(atomId_t newId);
     83
     84  /**
     85   * this function sets the Id without notifying the world. Only use it, if the world has already
     86   * gotten an ID for this Atom.
     87   */
     88   virtual void setId(atomId_t);
     89
    8390  protected:
    8491    /**
     
    101108  private:
    102109    World* world;
    103     int id;
     110    atomId_t id;
    104111};
    105112
     
    109116 * Use World::createAtom() instead.
    110117 */
    111 atom* NewAtom();
     118atom* NewAtom(atomId_t _id);
    112119
    113120/**
Note: See TracChangeset for help on using the changeset viewer.