source: src/World.cpp@ cf1a07

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

Made the world solely responsible for creating and erasing molecules.

  • Property mode set to 100644
File size: 4.9 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"
[7c4e29]15#include "Actions/ManipulateAtomsProcess.hpp"
[d346b6]16
17using namespace std;
[4d9c01]18
[5d1611]19/******************************* getter and setter ************************/
[354859]20periodentafel *&World::getPeriode(){
[5d1611]21 return periode;
22}
23
[7a1ce5]24atom* World::getAtom(AtomDescriptor descriptor){
[fc1b24]25 return descriptor.find();
26}
27
[7a1ce5]28vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
[fc1b24]29 return descriptor.findAll();
30}
31
[0e2a47]32vector<atom*> World::getAllAtoms(){
33 return getAllAtoms(AllAtoms());
34}
35
[354859]36int World::numAtoms(){
37 return atoms.size();
38}
39
40int World::numMolecules(){
41 return molecules_deprecated->ListOfMolecules.size();
42}
43
[afb47f]44/******************** Methods to change World state *********************/
45
[354859]46molecule* World::createMolecule(){
47 OBSERVE;
48 molecule *mol = NULL;
[cbc5fb]49 mol = NewMolecule();
[d2dbac0]50 assert(!molecules.count(currMoleculeId));
[cbc5fb]51 mol->setId(currMoleculeId++);
[244d26]52 // store the molecule by ID
[cbc5fb]53 molecules[mol->getId()] = mol;
[354859]54 mol->signOn(this);
55 return mol;
56}
57
[cbc5fb]58void World::destroyMolecule(molecule* mol){
59 OBSERVE;
60 destroyMolecule(mol->getId());
61}
62
63void World::destroyMolecule(moleculeId_t id){
64 OBSERVE;
65 molecule *mol = molecules[id];
66 assert(mol);
67 DeleteMolecule(mol);
68 molecules.erase(id);
69}
70
[7c4e29]71
[46d958]72atom *World::createAtom(){
73 OBSERVE;
74 atom *res = NewAtom();
[d2dbac0]75 assert(!atoms.count(currAtomId));
[46d958]76 res->setId(currAtomId++);
77 res->setWorld(this);
[244d26]78 // store the atom by ID
[46d958]79 atoms[res->getId()] = res;
80 return res;
81}
82
83int World::registerAtom(atom *atom){
84 OBSERVE;
[d2dbac0]85 assert(!atoms.count(currAtomId));
[46d958]86 atom->setId(currAtomId++);
87 atom->setWorld(this);
88 atoms[atom->getId()] = atom;
89 return atom->getId();
90}
91
92void World::destroyAtom(atom* atom){
93 OBSERVE;
94 int id = atom->getId();
95 destroyAtom(id);
96}
97
[cbc5fb]98void World::destroyAtom(atomId_t id) {
[46d958]99 OBSERVE;
100 atom *atom = atoms[id];
101 assert(atom);
102 DeleteAtom(atom);
103 atoms.erase(id);
104}
105
[7c4e29]106ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
107 return new ManipulateAtomsProcess(op, descr,name,true);
108}
109
[0e2a47]110ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
111 return manipulateAtoms(op,name,AllAtoms());
112}
113
[afb47f]114/********************* Internal Change methods for double Callback and Observer mechanism ********/
115
116void World::doManipulate(ManipulateAtomsProcess *proc){
117 proc->signOn(this);
118 {
119 OBSERVE;
120 proc->doManipulate(this);
121 }
122 proc->signOff(this);
123}
124
[865a945]125/******************************* Iterators ********************************/
126
[d2dbac0]127/*
128 * Actual Implementation of the iterators can be found in WorldIterators.cpp
129 */
[865a945]130
131World::AtomIterator World::getAtomIter(AtomDescriptor descr){
132 return AtomIterator(descr,this);
133}
[354859]134
[d2dbac0]135World::AtomSet::iterator World::atomEnd(){
[7c4e29]136 return atoms.end();
137}
138
[5d1611]139/******************************* Singleton Stuff **************************/
140
[4d9c01]141// TODO: Hide boost-thread using Autotools stuff when no threads are used
[5d1611]142World* World::theWorld = 0;
[4d9c01]143boost::mutex World::worldLock;
144
[7a1ce5]145World::World() :
[46d958]146 currAtomId(0),
[d2dbac0]147 currMoleculeId(0),
[354859]148 periode(new periodentafel),
[cbc5fb]149 molecules_deprecated(new MoleculeListClass(this)),
[d2dbac0]150 atoms(),
151 molecules()
[7dad10]152{
153 molecules_deprecated->signOn(this);
154}
[5d1611]155
156World::~World()
[354859]157{
[46d958]158 delete molecules_deprecated;
[354859]159 delete periode;
[cbc5fb]160 MoleculeSet::iterator molIter;
161 for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
162 DeleteMolecule((*molIter).second);
163 }
164 molecules.clear();
165 AtomSet::iterator atIter;
166 for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
167 DeleteAtom((*atIter).second);
[46d958]168 }
169 atoms.clear();
[354859]170}
[5d1611]171
172World* World::get(){
173 // boost supports RAII-Style locking, so we don't need to unlock
174 boost::mutex::scoped_lock guard(worldLock);
175 if(!theWorld) {
176 theWorld = new World();
177 }
178 return theWorld;
179}
180
181void World::destroy(){
182 // boost supports RAII-Style locking, so we don't need to unlock
183 boost::mutex::scoped_lock guard(worldLock);
184 delete theWorld;
185 theWorld = 0;
186}
187
188World* World::reset(){
189 World* oldWorld = 0;
190 {
191 // boost supports RAII-Style locking, so we don't need to unlock
192 boost::mutex::scoped_lock guard(worldLock);
193
194 oldWorld = theWorld;
195 theWorld = new World();
196 // oldworld does not need protection any more,
197 // since we should have the only reference
198
199 // worldLock handles access to the pointer,
200 // not to the object
201 } // scope-end releases the lock
202
203 // we have to let all the observers know that the
204 // oldWorld was destroyed. oldWorld calls subjectKilled
205 // upon destruction. Every Observer getting that signal
206 // should see that it gets the updated new world
207 delete oldWorld;
208}
209
210/******************************* deprecated Legacy Stuff ***********************/
211
[354859]212MoleculeListClass *&World::getMolecules() {
213 return molecules_deprecated;
[5d1611]214}
Note: See TracBrowser for help on using the repository browser.