source: src/World.cpp@ 6d574a

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

Replaced several old-style asserts with more usable ASSERTs

  • Property mode set to 100644
File size: 7.3 KB
Line 
1/*
2 * World.cpp
3 *
4 * Created on: Feb 3, 2010
5 * Author: crueger
6 */
7
8#include "Helpers/MemDebug.hpp"
9
10#include "World.hpp"
11
12#include "atom.hpp"
13#include "config.hpp"
14#include "molecule.hpp"
15#include "periodentafel.hpp"
16#include "Descriptors/AtomDescriptor.hpp"
17#include "Descriptors/AtomDescriptor_impl.hpp"
18#include "Descriptors/MoleculeDescriptor.hpp"
19#include "Descriptors/MoleculeDescriptor_impl.hpp"
20#include "Descriptors/SelectiveIterator_impl.hpp"
21#include "Actions/ManipulateAtomsProcess.hpp"
22#include "Helpers/Assert.hpp"
23
24#include "Patterns/Singleton_impl.hpp"
25
26using namespace std;
27
28/******************************* getter and setter ************************/
29periodentafel *&World::getPeriode(){
30 return periode;
31}
32
33config *&World::getConfig(){
34 return configuration;
35}
36
37// Atoms
38
39atom* World::getAtom(AtomDescriptor descriptor){
40 return descriptor.find();
41}
42
43vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
44 return descriptor.findAll();
45}
46
47vector<atom*> World::getAllAtoms(){
48 return getAllAtoms(AllAtoms());
49}
50
51int World::numAtoms(){
52 return atoms.size();
53}
54
55// Molecules
56
57molecule *World::getMolecule(MoleculeDescriptor descriptor){
58 return descriptor.find();
59}
60
61std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
62 return descriptor.findAll();
63}
64
65std::vector<molecule*> World::getAllMolecules(){
66 return getAllMolecules(AllMolecules());
67}
68
69int World::numMolecules(){
70 return molecules_deprecated->ListOfMolecules.size();
71}
72
73// system
74
75double * World::getDomain() {
76 return cell_size;
77}
78
79void World::setDomain(double * matrix)
80{
81
82}
83
84std::string World::getDefaultName() {
85 return defaultName;
86}
87
88void World::setDefaultName(std::string name)
89{
90 defaultName = name;
91};
92
93int World::getExitFlag() {
94 return ExitFlag;
95}
96
97void World::setExitFlag(int flag) {
98 if (ExitFlag < flag)
99 ExitFlag = flag;
100}
101
102/******************** Methods to change World state *********************/
103
104molecule* World::createMolecule(){
105 OBSERVE;
106 molecule *mol = NULL;
107 mol = NewMolecule();
108 ASSERT(!molecules.count(currMoleculeId),"currMoleculeId did not specify an unused ID");
109 mol->setId(currMoleculeId++);
110 // store the molecule by ID
111 molecules[mol->getId()] = mol;
112 mol->signOn(this);
113 return mol;
114}
115
116void World::destroyMolecule(molecule* mol){
117 OBSERVE;
118 destroyMolecule(mol->getId());
119}
120
121void World::destroyMolecule(moleculeId_t id){
122 OBSERVE;
123 molecule *mol = molecules[id];
124 ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
125 DeleteMolecule(mol);
126 molecules.erase(id);
127}
128
129double *World::cell_size = NULL;
130
131atom *World::createAtom(){
132 OBSERVE;
133 atomId_t id = getNextAtomId();
134 atom *res = NewAtom(id);
135 res->setWorld(this);
136 // store the atom by ID
137 atoms[res->getId()] = res;
138 return res;
139}
140
141
142int World::registerAtom(atom *atom){
143 OBSERVE;
144 atomId_t id = getNextAtomId();
145 atom->setId(id);
146 atom->setWorld(this);
147 atoms[atom->getId()] = atom;
148 return atom->getId();
149}
150
151void World::destroyAtom(atom* atom){
152 OBSERVE;
153 int id = atom->getId();
154 destroyAtom(id);
155}
156
157void World::destroyAtom(atomId_t id) {
158 OBSERVE;
159 atom *atom = atoms[id];
160 ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
161 DeleteAtom(atom);
162 atoms.erase(id);
163 releaseAtomId(id);
164}
165
166bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
167 OBSERVE;
168 // in case this call did not originate from inside the atom, we redirect it,
169 // to also let it know that it has changed
170 if(!target){
171 target = atoms[oldId];
172 ASSERT(target,"Atom with that ID not found");
173 return target->changeId(newId);
174 }
175 else{
176 if(reserveAtomId(newId)){
177 atoms.erase(oldId);
178 atoms.insert(pair<atomId_t,atom*>(newId,target));
179 return true;
180 }
181 else{
182 return false;
183 }
184 }
185}
186
187ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
188 return new ManipulateAtomsProcess(op, descr,name,true);
189}
190
191ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
192 return manipulateAtoms(op,name,AllAtoms());
193}
194
195/********************* Internal Change methods for double Callback and Observer mechanism ********/
196
197void World::doManipulate(ManipulateAtomsProcess *proc){
198 proc->signOn(this);
199 {
200 OBSERVE;
201 proc->doManipulate(this);
202 }
203 proc->signOff(this);
204}
205/******************************* IDManagement *****************************/
206
207// Atoms
208
209atomId_t World::getNextAtomId(){
210 // see if we can reuse some Id
211 if(atomIdPool.empty()){
212 return currAtomId++;
213 }
214 else{
215 // we give out the first ID from the pool
216 atomId_t id = *(atomIdPool.begin());
217 atomIdPool.erase(id);
218 return id;
219 }
220}
221
222void World::releaseAtomId(atomId_t id){
223 atomIdPool.insert(id);
224 // defragmentation of the pool
225 set<atomId_t>::reverse_iterator iter;
226 // go through all Ids in the pool that lie immediately below the border
227 while(!atomIdPool.empty() && *(atomIdPool.rbegin())==(currAtomId-1)){
228 atomIdPool.erase(--currAtomId);
229 }
230}
231
232bool World::reserveAtomId(atomId_t id){
233 if(id>=currAtomId ){
234 // add all ids between the new one and current border as available
235 for(atomId_t pos=currAtomId; pos<id; ++pos){
236 atomIdPool.insert(pos);
237 }
238 currAtomId=id+1;
239 return true;
240 }
241 else if(atomIdPool.count(id)){
242 atomIdPool.erase(id);
243 return true;
244 }
245 else{
246 // this ID could not be reserved
247 return false;
248 }
249}
250
251// Molecules
252
253/******************************* Iterators ********************************/
254
255// Build the AtomIterator from template
256CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
257
258
259World::AtomIterator World::getAtomIter(AtomDescriptor descr){
260 return AtomIterator(descr,atoms);
261}
262
263World::AtomIterator World::atomEnd(){
264 return AtomIterator(AllAtoms(),atoms,atoms.end());
265}
266
267// build the MoleculeIterator from template
268CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
269
270World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
271 return MoleculeIterator(descr,molecules);
272}
273
274World::MoleculeIterator World::moleculeEnd(){
275 return MoleculeIterator(AllMolecules(),molecules,molecules.end());
276}
277
278/******************************* Singleton Stuff **************************/
279
280World::World() :
281 Observable("World"),
282 periode(new periodentafel),
283 configuration(new config),
284 ExitFlag(0),
285 atoms(),
286 currAtomId(0),
287 molecules(),
288 currMoleculeId(0),
289 molecules_deprecated(new MoleculeListClass(this))
290{
291 cell_size = new double[6];
292 cell_size[0] = 20.;
293 cell_size[1] = 0.;
294 cell_size[2] = 20.;
295 cell_size[3] = 0.;
296 cell_size[4] = 0.;
297 cell_size[5] = 20.;
298 defaultName = "none";
299 molecules_deprecated->signOn(this);
300}
301
302World::~World()
303{
304 molecules_deprecated->signOff(this);
305 delete[] cell_size;
306 delete molecules_deprecated;
307 delete periode;
308 delete configuration;
309 MoleculeSet::iterator molIter;
310 for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
311 DeleteMolecule((*molIter).second);
312 }
313 molecules.clear();
314 AtomSet::iterator atIter;
315 for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
316 DeleteAtom((*atIter).second);
317 }
318 atoms.clear();
319}
320
321// Explicit instantiation of the singleton mechanism at this point
322
323CONSTRUCT_SINGLETON(World)
324
325/******************************* deprecated Legacy Stuff ***********************/
326
327MoleculeListClass *&World::getMolecules() {
328 return molecules_deprecated;
329}
Note: See TracBrowser for help on using the repository browser.