source: src/World.cpp@ b54ac8

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 b54ac8 was afb47f, checked in by Tillmann Crueger <crueger@…>, 16 years ago

Added a mechanism that allows the world to track changes done by the manipulators

  • 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
53ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
54 return new ManipulateAtomsProcess(op, descr,name,true);
55}
56
57/********************* Internal Change methods for double Callback and Observer mechanism ********/
58
59void World::doManipulate(ManipulateAtomsProcess *proc){
60 proc->signOn(this);
61 {
62 OBSERVE;
63 proc->doManipulate(this);
64 }
65 proc->signOff(this);
66}
67
68/******************************* Iterators ********************************/
69
70World::AtomIterator::AtomIterator(){
71 state = World::get()->atomEnd();
72}
73
74World::AtomIterator::AtomIterator(AtomDescriptor _descr, World* _world) :
75 descr(_descr.get_impl()),
76 world(_world),
77 index(0)
78{
79 state = world->atoms.begin();
80 advanceState();
81}
82
83World::AtomIterator::AtomIterator(const AtomIterator& rhs) :
84 state(rhs.state),
85 descr(rhs.descr),
86 index(rhs.index),
87 world(rhs.world)
88 {}
89
90World::AtomIterator& World::AtomIterator::operator=(const AtomIterator& rhs)
91{
92 if(&rhs!=this){
93 state=rhs.state;
94 descr=rhs.descr;
95 index=rhs.index;
96 world=rhs.world;
97 }
98 return *this;
99}
100
101World::AtomIterator& World::AtomIterator::operator++(){
102 ++state;
103 ++index;
104 advanceState();
105 return *this;
106}
107
108World::AtomIterator World::AtomIterator::operator++(int){
109 AtomIterator res(*this);
110 ++(*this);
111 return res;
112}
113
114bool World::AtomIterator::operator==(const AtomIterator& rhs){
115 return state==rhs.state;
116}
117
118bool World::AtomIterator::operator==(const World::AtomList::iterator& rhs){
119 return state==rhs;
120}
121
122bool World::AtomIterator::operator!=(const AtomIterator& rhs){
123 return state!=rhs.state;
124}
125
126bool World::AtomIterator::operator!=(const World::AtomList::iterator& rhs){
127 return state!=rhs;
128}
129
130atom* World::AtomIterator::operator*(){
131 return (*state).second;
132}
133
134void World::AtomIterator::advanceState(){
135 while((state!=world->atoms.end()) && (!descr->predicate(*state))){
136 ++state;
137 ++index;
138 }
139}
140
141int World::AtomIterator::getCount(){
142 return index;
143}
144
145World::AtomIterator World::getAtomIter(AtomDescriptor descr){
146 return AtomIterator(descr,this);
147}
148
149World::AtomList::iterator World::atomEnd(){
150 return atoms.end();
151}
152
153/******************************* Singleton Stuff **************************/
154
155// TODO: Hide boost-thread using Autotools stuff when no threads are used
156World* World::theWorld = 0;
157boost::mutex World::worldLock;
158
159
160
161World::World() :
162 dummyId(0),
163 periode(new periodentafel),
164 molecules_deprecated(new MoleculeListClass),
165 atoms()
166{
167 molecules_deprecated->signOn(this);
168}
169
170World::~World()
171{
172 delete periode;
173}
174
175World* World::get(){
176 // boost supports RAII-Style locking, so we don't need to unlock
177 boost::mutex::scoped_lock guard(worldLock);
178 if(!theWorld) {
179 theWorld = new World();
180 }
181 return theWorld;
182}
183
184void World::destroy(){
185 // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
186 theWorld->destroyLegacy();
187 //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
188
189 // boost supports RAII-Style locking, so we don't need to unlock
190 boost::mutex::scoped_lock guard(worldLock);
191 delete theWorld;
192 theWorld = 0;
193}
194
195World* World::reset(){
196 // For legacy reasons all atoms have to be destroyed first, since unregistering would cause deadlocks otherwise
197 theWorld->destroyLegacy();
198 //WARNING: at this point we have a small race condition, when sombody now tries to access the world.
199
200 World* oldWorld = 0;
201 {
202 // boost supports RAII-Style locking, so we don't need to unlock
203 boost::mutex::scoped_lock guard(worldLock);
204
205 oldWorld = theWorld;
206 theWorld = new World();
207 // oldworld does not need protection any more,
208 // since we should have the only reference
209
210 // worldLock handles access to the pointer,
211 // not to the object
212 } // scope-end releases the lock
213
214 // we have to let all the observers know that the
215 // oldWorld was destroyed. oldWorld calls subjectKilled
216 // upon destruction. Every Observer getting that signal
217 // should see that it gets the updated new world
218 delete oldWorld;
219}
220
221/******************************* deprecated Legacy Stuff ***********************/
222
223MoleculeListClass *&World::getMolecules() {
224 return molecules_deprecated;
225}
226
227// some legacy stuff to let the World know about items created outside
228void World::registerAtom(atom *theAtom){
229 OBSERVE;
230 atoms[dummyId++] = theAtom;
231}
232
233void World::destroyLegacy(){
234 //delete molecules_deprecated;
235}
236
237void World::unregisterAtom(atom *theAtom){
238 OBSERVE;
239 atoms.erase(theAtom->getId());
240}
Note: See TracBrowser for help on using the repository browser.