source: src/World.cpp@ 02ee15

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 Candidate_v1.7.0 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 02ee15 was 46d958, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Made the world solely responsible for creating and destroying atoms.

  • Property mode set to 100644
File size: 5.5 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 "Actions/ManipulateAtomsProcess.hpp"
16
17using namespace std;
18
19/******************************* getter and setter ************************/
20periodentafel *&World::getPeriode(){
21 return periode;
22}
23
24atom* World::getAtom(AtomDescriptor descriptor){
25 return descriptor.find();
26}
27
28vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
29 return descriptor.findAll();
30}
31
32int World::numAtoms(){
33 return atoms.size();
34}
35
36int World::numMolecules(){
37 return molecules_deprecated->ListOfMolecules.size();
38}
39
40/******************** Methods to change World state *********************/
41
42molecule* World::createMolecule(){
43 OBSERVE;
44 molecule *mol = NULL;
45 mol = new molecule(periode);
46 molecules_deprecated->insert(mol);
47 molecules.insert(mol);
48 mol->signOn(this);
49 return mol;
50}
51
52
53atom *World::createAtom(){
54 OBSERVE;
55 atom *res = NewAtom();
56 res->setId(currAtomId++);
57 res->setWorld(this);
58 atoms[res->getId()] = res;
59 return res;
60}
61
62int World::registerAtom(atom *atom){
63 OBSERVE;
64 atom->setId(currAtomId++);
65 atom->setWorld(this);
66 atoms[atom->getId()] = atom;
67 return atom->getId();
68}
69
70void World::destroyAtom(atom* atom){
71 OBSERVE;
72 int id = atom->getId();
73 destroyAtom(id);
74}
75
76void World::destroyAtom(int id) {
77 OBSERVE;
78 atom *atom = atoms[id];
79 assert(atom);
80 DeleteAtom(atom);
81 atoms.erase(id);
82}
83
84ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
85 return new ManipulateAtomsProcess(op, descr,name,true);
86}
87
88/********************* Internal Change methods for double Callback and Observer mechanism ********/
89
90void World::doManipulate(ManipulateAtomsProcess *proc){
91 proc->signOn(this);
92 {
93 OBSERVE;
94 proc->doManipulate(this);
95 }
96 proc->signOff(this);
97}
98
99/******************************* Iterators ********************************/
100
101World::AtomIterator::AtomIterator(){
102 state = World::get()->atomEnd();
103}
104
105World::AtomIterator::AtomIterator(AtomDescriptor _descr, World* _world) :
106 descr(_descr.get_impl()),
107 world(_world),
108 index(0)
109{
110 state = world->atoms.begin();
111 advanceState();
112}
113
114World::AtomIterator::AtomIterator(const AtomIterator& rhs) :
115 state(rhs.state),
116 descr(rhs.descr),
117 index(rhs.index),
118 world(rhs.world)
119 {}
120
121World::AtomIterator& World::AtomIterator::operator=(const AtomIterator& rhs)
122{
123 if(&rhs!=this){
124 state=rhs.state;
125 descr=rhs.descr;
126 index=rhs.index;
127 world=rhs.world;
128 }
129 return *this;
130}
131
132World::AtomIterator& World::AtomIterator::operator++(){
133 ++state;
134 ++index;
135 advanceState();
136 return *this;
137}
138
139World::AtomIterator World::AtomIterator::operator++(int){
140 AtomIterator res(*this);
141 ++(*this);
142 return res;
143}
144
145bool World::AtomIterator::operator==(const AtomIterator& rhs){
146 return state==rhs.state;
147}
148
149bool World::AtomIterator::operator==(const World::AtomList::iterator& rhs){
150 return state==rhs;
151}
152
153bool World::AtomIterator::operator!=(const AtomIterator& rhs){
154 return state!=rhs.state;
155}
156
157bool World::AtomIterator::operator!=(const World::AtomList::iterator& rhs){
158 return state!=rhs;
159}
160
161atom* World::AtomIterator::operator*(){
162 return (*state).second;
163}
164
165void World::AtomIterator::advanceState(){
166 while((state!=world->atoms.end()) && (!descr->predicate(*state))){
167 ++state;
168 ++index;
169 }
170}
171
172int World::AtomIterator::getCount(){
173 return index;
174}
175
176World::AtomIterator World::getAtomIter(AtomDescriptor descr){
177 return AtomIterator(descr,this);
178}
179
180World::AtomList::iterator World::atomEnd(){
181 return atoms.end();
182}
183
184/******************************* Singleton Stuff **************************/
185
186// TODO: Hide boost-thread using Autotools stuff when no threads are used
187World* World::theWorld = 0;
188boost::mutex World::worldLock;
189
190
191
192World::World() :
193 currAtomId(0),
194 periode(new periodentafel),
195 molecules_deprecated(new MoleculeListClass),
196 atoms()
197{
198 molecules_deprecated->signOn(this);
199}
200
201World::~World()
202{
203 delete molecules_deprecated;
204 delete periode;
205 AtomList::iterator iter;
206 for(iter=atoms.begin();iter!=atoms.end();++iter){
207 DeleteAtom((*iter).second);
208 }
209 atoms.clear();
210}
211
212World* World::get(){
213 // boost supports RAII-Style locking, so we don't need to unlock
214 boost::mutex::scoped_lock guard(worldLock);
215 if(!theWorld) {
216 theWorld = new World();
217 }
218 return theWorld;
219}
220
221void World::destroy(){
222 // boost supports RAII-Style locking, so we don't need to unlock
223 boost::mutex::scoped_lock guard(worldLock);
224 delete theWorld;
225 theWorld = 0;
226}
227
228World* World::reset(){
229 World* oldWorld = 0;
230 {
231 // boost supports RAII-Style locking, so we don't need to unlock
232 boost::mutex::scoped_lock guard(worldLock);
233
234 oldWorld = theWorld;
235 theWorld = new World();
236 // oldworld does not need protection any more,
237 // since we should have the only reference
238
239 // worldLock handles access to the pointer,
240 // not to the object
241 } // scope-end releases the lock
242
243 // we have to let all the observers know that the
244 // oldWorld was destroyed. oldWorld calls subjectKilled
245 // upon destruction. Every Observer getting that signal
246 // should see that it gets the updated new world
247 delete oldWorld;
248}
249
250/******************************* deprecated Legacy Stuff ***********************/
251
252MoleculeListClass *&World::getMolecules() {
253 return molecules_deprecated;
254}
Note: See TracBrowser for help on using the repository browser.