source: src/World.cpp@ 745a85

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
Last change on this file since 745a85 was 23b547, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added generic singleton Pattern that can be inherited to any class making that class a singleton.

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[5d1611]1/*
2 * World.cpp
3 *
4 * Created on: Feb 3, 2010
5 * Author: crueger
6 */
7
8#include "World.hpp"
9
[d346b6]10#include "atom.hpp"
[354859]11#include "molecule.hpp"
12#include "periodentafel.hpp"
[fc1b24]13#include "Descriptors/AtomDescriptor.hpp"
[865a945]14#include "Descriptors/AtomDescriptor_impl.hpp"
[1c51c8]15#include "Descriptors/MoleculeDescriptor.hpp"
16#include "Descriptors/MoleculeDescriptor_impl.hpp"
[7c4e29]17#include "Actions/ManipulateAtomsProcess.hpp"
[d346b6]18
[23b547]19#include "Patterns/Singleton_impl.hpp"
20
[d346b6]21using namespace std;
[4d9c01]22
[5d1611]23/******************************* getter and setter ************************/
[354859]24periodentafel *&World::getPeriode(){
[5d1611]25 return periode;
26}
27
[1c51c8]28// Atoms
29
[7a1ce5]30atom* World::getAtom(AtomDescriptor descriptor){
[fc1b24]31 return descriptor.find();
32}
33
[7a1ce5]34vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
[fc1b24]35 return descriptor.findAll();
36}
37
[0e2a47]38vector<atom*> World::getAllAtoms(){
39 return getAllAtoms(AllAtoms());
40}
41
[354859]42int World::numAtoms(){
43 return atoms.size();
44}
45
[1c51c8]46// Molecules
47
48molecule *World::getMolecule(MoleculeDescriptor descriptor){
49 return descriptor.find();
50}
51
52std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
53 return descriptor.findAll();
54}
55
[354859]56int World::numMolecules(){
57 return molecules_deprecated->ListOfMolecules.size();
58}
59
[afb47f]60/******************** Methods to change World state *********************/
61
[354859]62molecule* World::createMolecule(){
63 OBSERVE;
64 molecule *mol = NULL;
[cbc5fb]65 mol = NewMolecule();
[d2dbac0]66 assert(!molecules.count(currMoleculeId));
[cbc5fb]67 mol->setId(currMoleculeId++);
[244d26]68 // store the molecule by ID
[cbc5fb]69 molecules[mol->getId()] = mol;
[354859]70 mol->signOn(this);
71 return mol;
72}
73
[cbc5fb]74void World::destroyMolecule(molecule* mol){
75 OBSERVE;
76 destroyMolecule(mol->getId());
77}
78
79void World::destroyMolecule(moleculeId_t id){
80 OBSERVE;
81 molecule *mol = molecules[id];
82 assert(mol);
83 DeleteMolecule(mol);
84 molecules.erase(id);
85}
86
[7c4e29]87
[46d958]88atom *World::createAtom(){
89 OBSERVE;
[88d586]90 atomId_t id = getNextAtomId();
91 atom *res = NewAtom(id);
[46d958]92 res->setWorld(this);
[244d26]93 // store the atom by ID
[46d958]94 atoms[res->getId()] = res;
95 return res;
96}
97
98int World::registerAtom(atom *atom){
99 OBSERVE;
[88d586]100 atomId_t id = getNextAtomId();
101 atom->setId(id);
[46d958]102 atom->setWorld(this);
103 atoms[atom->getId()] = atom;
104 return atom->getId();
105}
106
107void World::destroyAtom(atom* atom){
108 OBSERVE;
109 int id = atom->getId();
110 destroyAtom(id);
111}
112
[cbc5fb]113void World::destroyAtom(atomId_t id) {
[46d958]114 OBSERVE;
115 atom *atom = atoms[id];
116 assert(atom);
117 DeleteAtom(atom);
118 atoms.erase(id);
[88d586]119 releaseAtomId(id);
120}
121
122bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
123 OBSERVE;
124 // in case this call did not originate from inside the atom, we redirect it,
125 // to also let it know that it has changed
126 if(!target){
127 target = atoms[oldId];
128 assert(target && "Atom with that ID not found");
129 return target->changeId(newId);
130 }
131 else{
132 if(reserveAtomId(newId)){
133 atoms.erase(oldId);
134 atoms.insert(pair<atomId_t,atom*>(newId,target));
135 return true;
136 }
137 else{
138 return false;
139 }
140 }
[46d958]141}
142
[7c4e29]143ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
144 return new ManipulateAtomsProcess(op, descr,name,true);
145}
146
[0e2a47]147ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
148 return manipulateAtoms(op,name,AllAtoms());
149}
150
[afb47f]151/********************* Internal Change methods for double Callback and Observer mechanism ********/
152
153void World::doManipulate(ManipulateAtomsProcess *proc){
154 proc->signOn(this);
155 {
156 OBSERVE;
157 proc->doManipulate(this);
158 }
159 proc->signOff(this);
160}
[88d586]161/******************************* IDManagement *****************************/
162
[57adc7]163// Atoms
164
[88d586]165atomId_t World::getNextAtomId(){
166 // see if we can reuse some Id
167 if(atomIdPool.empty()){
168 return currAtomId++;
169 }
170 else{
171 // we give out the first ID from the pool
172 atomId_t id = *(atomIdPool.begin());
173 atomIdPool.erase(id);
[23b547]174 return id;
[88d586]175 }
176}
177
178void World::releaseAtomId(atomId_t id){
179 atomIdPool.insert(id);
180 // defragmentation of the pool
181 set<atomId_t>::reverse_iterator iter;
182 // go through all Ids in the pool that lie immediately below the border
183 while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
184 atomIdPool.erase(--currAtomId);
185 }
186}
[afb47f]187
[88d586]188bool World::reserveAtomId(atomId_t id){
189 if(id>=currAtomId ){
190 // add all ids between the new one and current border as available
191 for(atomId_t pos=currAtomId; pos<id; ++pos){
192 atomIdPool.insert(pos);
193 }
194 currAtomId=id+1;
195 return true;
196 }
197 else if(atomIdPool.count(id)){
198 atomIdPool.erase(id);
199 return true;
200 }
201 else{
202 // this ID could not be reserved
203 return false;
204 }
205}
[57adc7]206
207// Molecules
208
[865a945]209/******************************* Iterators ********************************/
210
[d2dbac0]211/*
212 * Actual Implementation of the iterators can be found in WorldIterators.cpp
213 */
[865a945]214
215World::AtomIterator World::getAtomIter(AtomDescriptor descr){
216 return AtomIterator(descr,this);
217}
[354859]218
[d2dbac0]219World::AtomSet::iterator World::atomEnd(){
[7c4e29]220 return atoms.end();
221}
222
[1c51c8]223World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
224 return MoleculeIterator(descr,this);
225}
226
227World::MoleculeSet::iterator World::moleculeEnd(){
228 return molecules.end();
229}
230
[5d1611]231/******************************* Singleton Stuff **************************/
232
[7a1ce5]233World::World() :
[354859]234 periode(new periodentafel),
[d2dbac0]235 atoms(),
[24a5e0]236 currAtomId(0),
237 molecules(),
238 currMoleculeId(0),
239 molecules_deprecated(new MoleculeListClass(this))
[7dad10]240{
241 molecules_deprecated->signOn(this);
242}
[5d1611]243
244World::~World()
[354859]245{
[028c2e]246 molecules_deprecated->signOff(this);
[46d958]247 delete molecules_deprecated;
[354859]248 delete periode;
[cbc5fb]249 MoleculeSet::iterator molIter;
250 for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
251 DeleteMolecule((*molIter).second);
252 }
253 molecules.clear();
254 AtomSet::iterator atIter;
255 for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
256 DeleteAtom((*atIter).second);
[46d958]257 }
258 atoms.clear();
[354859]259}
[5d1611]260
[23b547]261// Explicit instantiation of the singleton mechanism at this point
[5d1611]262
[23b547]263CONSTRUCT_SINGLETON(World)
[5d1611]264
265/******************************* deprecated Legacy Stuff ***********************/
266
[354859]267MoleculeListClass *&World::getMolecules() {
268 return molecules_deprecated;
[5d1611]269}
Note: See TracBrowser for help on using the repository browser.