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
Line 
1/*
2 * World.cpp
3 *
4 * Created on: Feb 3, 2010
5 * Author: crueger
6 */
7
8#include "World.hpp"
9
10#include "atom.hpp"
11#include "molecule.hpp"
12#include "periodentafel.hpp"
13#include "Descriptors/AtomDescriptor.hpp"
14#include "Descriptors/AtomDescriptor_impl.hpp"
15#include "Descriptors/MoleculeDescriptor.hpp"
16#include "Descriptors/MoleculeDescriptor_impl.hpp"
17#include "Actions/ManipulateAtomsProcess.hpp"
18
19#include "Patterns/Singleton_impl.hpp"
20
21using namespace std;
22
23/******************************* getter and setter ************************/
24periodentafel *&World::getPeriode(){
25 return periode;
26}
27
28// Atoms
29
30atom* World::getAtom(AtomDescriptor descriptor){
31 return descriptor.find();
32}
33
34vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
35 return descriptor.findAll();
36}
37
38vector<atom*> World::getAllAtoms(){
39 return getAllAtoms(AllAtoms());
40}
41
42int World::numAtoms(){
43 return atoms.size();
44}
45
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
56int World::numMolecules(){
57 return molecules_deprecated->ListOfMolecules.size();
58}
59
60/******************** Methods to change World state *********************/
61
62molecule* World::createMolecule(){
63 OBSERVE;
64 molecule *mol = NULL;
65 mol = NewMolecule();
66 assert(!molecules.count(currMoleculeId));
67 mol->setId(currMoleculeId++);
68 // store the molecule by ID
69 molecules[mol->getId()] = mol;
70 mol->signOn(this);
71 return mol;
72}
73
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
87
88atom *World::createAtom(){
89 OBSERVE;
90 atomId_t id = getNextAtomId();
91 atom *res = NewAtom(id);
92 res->setWorld(this);
93 // store the atom by ID
94 atoms[res->getId()] = res;
95 return res;
96}
97
98int World::registerAtom(atom *atom){
99 OBSERVE;
100 atomId_t id = getNextAtomId();
101 atom->setId(id);
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
113void World::destroyAtom(atomId_t id) {
114 OBSERVE;
115 atom *atom = atoms[id];
116 assert(atom);
117 DeleteAtom(atom);
118 atoms.erase(id);
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 }
141}
142
143ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
144 return new ManipulateAtomsProcess(op, descr,name,true);
145}
146
147ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
148 return manipulateAtoms(op,name,AllAtoms());
149}
150
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}
161/******************************* IDManagement *****************************/
162
163// Atoms
164
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);
174 return id;
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}
187
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}
206
207// Molecules
208
209/******************************* Iterators ********************************/
210
211/*
212 * Actual Implementation of the iterators can be found in WorldIterators.cpp
213 */
214
215World::AtomIterator World::getAtomIter(AtomDescriptor descr){
216 return AtomIterator(descr,this);
217}
218
219World::AtomSet::iterator World::atomEnd(){
220 return atoms.end();
221}
222
223World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
224 return MoleculeIterator(descr,this);
225}
226
227World::MoleculeSet::iterator World::moleculeEnd(){
228 return molecules.end();
229}
230
231/******************************* Singleton Stuff **************************/
232
233World::World() :
234 periode(new periodentafel),
235 atoms(),
236 currAtomId(0),
237 molecules(),
238 currMoleculeId(0),
239 molecules_deprecated(new MoleculeListClass(this))
240{
241 molecules_deprecated->signOn(this);
242}
243
244World::~World()
245{
246 molecules_deprecated->signOff(this);
247 delete molecules_deprecated;
248 delete periode;
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);
257 }
258 atoms.clear();
259}
260
261// Explicit instantiation of the singleton mechanism at this point
262
263CONSTRUCT_SINGLETON(World)
264
265/******************************* deprecated Legacy Stuff ***********************/
266
267MoleculeListClass *&World::getMolecules() {
268 return molecules_deprecated;
269}
Note: See TracBrowser for help on using the repository browser.