source: src/World.cpp@ 7c4e29

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

Added a class that allows constructing Processes that have a result

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