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 <functional>
|
---|
13 |
|
---|
14 | #include "atom.hpp"
|
---|
15 | #include "config.hpp"
|
---|
16 | #include "molecule.hpp"
|
---|
17 | #include "periodentafel.hpp"
|
---|
18 | #include "ThermoStatContainer.hpp"
|
---|
19 | #include "Descriptors/AtomDescriptor.hpp"
|
---|
20 | #include "Descriptors/AtomDescriptor_impl.hpp"
|
---|
21 | #include "Descriptors/MoleculeDescriptor.hpp"
|
---|
22 | #include "Descriptors/MoleculeDescriptor_impl.hpp"
|
---|
23 | #include "Descriptors/SelectiveIterator_impl.hpp"
|
---|
24 | #include "Actions/ManipulateAtomsProcess.hpp"
|
---|
25 | #include "Helpers/Assert.hpp"
|
---|
26 | #include "Box.hpp"
|
---|
27 | #include "Matrix.hpp"
|
---|
28 | #include "defs.hpp"
|
---|
29 |
|
---|
30 | #include "Patterns/Singleton_impl.hpp"
|
---|
31 | #include "Patterns/ObservedContainer_impl.hpp"
|
---|
32 |
|
---|
33 | using namespace std;
|
---|
34 |
|
---|
35 | /******************************* getter and setter ************************/
|
---|
36 | periodentafel *&World::getPeriode(){
|
---|
37 | return periode;
|
---|
38 | }
|
---|
39 |
|
---|
40 | config *&World::getConfig(){
|
---|
41 | return configuration;
|
---|
42 | }
|
---|
43 |
|
---|
44 | // Atoms
|
---|
45 |
|
---|
46 | atom* World::getAtom(AtomDescriptor descriptor){
|
---|
47 | return descriptor.find();
|
---|
48 | }
|
---|
49 |
|
---|
50 | vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
|
---|
51 | return descriptor.findAll();
|
---|
52 | }
|
---|
53 |
|
---|
54 | vector<atom*> World::getAllAtoms(){
|
---|
55 | return getAllAtoms(AllAtoms());
|
---|
56 | }
|
---|
57 |
|
---|
58 | int World::numAtoms(){
|
---|
59 | return atoms.size();
|
---|
60 | }
|
---|
61 |
|
---|
62 | // Molecules
|
---|
63 |
|
---|
64 | molecule *World::getMolecule(MoleculeDescriptor descriptor){
|
---|
65 | return descriptor.find();
|
---|
66 | }
|
---|
67 |
|
---|
68 | std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
|
---|
69 | return descriptor.findAll();
|
---|
70 | }
|
---|
71 |
|
---|
72 | std::vector<molecule*> World::getAllMolecules(){
|
---|
73 | return getAllMolecules(AllMolecules());
|
---|
74 | }
|
---|
75 |
|
---|
76 | int World::numMolecules(){
|
---|
77 | return molecules_deprecated->ListOfMolecules.size();
|
---|
78 | }
|
---|
79 |
|
---|
80 | // system
|
---|
81 |
|
---|
82 | Box& World::getDomain() {
|
---|
83 | return *cell_size;
|
---|
84 | }
|
---|
85 |
|
---|
86 | void World::setDomain(const Matrix &mat){
|
---|
87 | OBSERVE;
|
---|
88 | *cell_size = mat;
|
---|
89 | }
|
---|
90 |
|
---|
91 | void World::setDomain(double * matrix)
|
---|
92 | {
|
---|
93 | OBSERVE;
|
---|
94 | Matrix M = ReturnFullMatrixforSymmetric(matrix);
|
---|
95 | cell_size->setM(M);
|
---|
96 | }
|
---|
97 |
|
---|
98 | std::string World::getDefaultName() {
|
---|
99 | return defaultName;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void World::setDefaultName(std::string name)
|
---|
103 | {
|
---|
104 | OBSERVE;
|
---|
105 | defaultName = name;
|
---|
106 | };
|
---|
107 |
|
---|
108 | class ThermoStatContainer * World::getThermostats()
|
---|
109 | {
|
---|
110 | return Thermostats;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | int World::getExitFlag() {
|
---|
115 | return ExitFlag;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void World::setExitFlag(int flag) {
|
---|
119 | if (ExitFlag < flag)
|
---|
120 | ExitFlag = flag;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /******************** Methods to change World state *********************/
|
---|
124 |
|
---|
125 | molecule* World::createMolecule(){
|
---|
126 | OBSERVE;
|
---|
127 | molecule *mol = NULL;
|
---|
128 | mol = NewMolecule();
|
---|
129 | moleculeId_t id = getNextMoleculeId();
|
---|
130 | ASSERT(!molecules.count(id),"proposed id did not specify an unused ID");
|
---|
131 | mol->setId(id);
|
---|
132 | // store the molecule by ID
|
---|
133 | molecules[mol->getId()] = mol;
|
---|
134 | mol->signOn(this);
|
---|
135 | return mol;
|
---|
136 | }
|
---|
137 |
|
---|
138 | void World::destroyMolecule(molecule* mol){
|
---|
139 | OBSERVE;
|
---|
140 | destroyMolecule(mol->getId());
|
---|
141 | }
|
---|
142 |
|
---|
143 | void World::destroyMolecule(moleculeId_t id){
|
---|
144 | OBSERVE;
|
---|
145 | molecule *mol = molecules[id];
|
---|
146 | ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
|
---|
147 | DeleteMolecule(mol);
|
---|
148 | molecules.erase(id);
|
---|
149 | releaseMoleculeId(id);
|
---|
150 | }
|
---|
151 |
|
---|
152 | atom *World::createAtom(){
|
---|
153 | OBSERVE;
|
---|
154 | atomId_t id = getNextAtomId();
|
---|
155 | ASSERT(!atoms.count(id),"proposed id did not specify an unused ID");
|
---|
156 | atom *res = NewAtom(id);
|
---|
157 | res->setWorld(this);
|
---|
158 | // store the atom by ID
|
---|
159 | atoms[res->getId()] = res;
|
---|
160 | return res;
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 | int World::registerAtom(atom *atom){
|
---|
165 | OBSERVE;
|
---|
166 | atomId_t id = getNextAtomId();
|
---|
167 | atom->setId(id);
|
---|
168 | atom->setWorld(this);
|
---|
169 | atoms[atom->getId()] = atom;
|
---|
170 | return atom->getId();
|
---|
171 | }
|
---|
172 |
|
---|
173 | void World::destroyAtom(atom* atom){
|
---|
174 | OBSERVE;
|
---|
175 | int id = atom->getId();
|
---|
176 | destroyAtom(id);
|
---|
177 | }
|
---|
178 |
|
---|
179 | void World::destroyAtom(atomId_t id) {
|
---|
180 | OBSERVE;
|
---|
181 | atom *atom = atoms[id];
|
---|
182 | ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
|
---|
183 | DeleteAtom(atom);
|
---|
184 | atoms.erase(id);
|
---|
185 | releaseAtomId(id);
|
---|
186 | }
|
---|
187 |
|
---|
188 | bool World::changeAtomId(atomId_t oldId, atomId_t newId, atom* target){
|
---|
189 | OBSERVE;
|
---|
190 | // in case this call did not originate from inside the atom, we redirect it,
|
---|
191 | // to also let it know that it has changed
|
---|
192 | if(!target){
|
---|
193 | target = atoms[oldId];
|
---|
194 | ASSERT(target,"Atom with that ID not found");
|
---|
195 | return target->changeId(newId);
|
---|
196 | }
|
---|
197 | else{
|
---|
198 | if(reserveAtomId(newId)){
|
---|
199 | atoms.erase(oldId);
|
---|
200 | atoms.insert(pair<atomId_t,atom*>(newId,target));
|
---|
201 | return true;
|
---|
202 | }
|
---|
203 | else{
|
---|
204 | return false;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
|
---|
210 | return new ManipulateAtomsProcess(op, descr,name,true);
|
---|
211 | }
|
---|
212 |
|
---|
213 | ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
|
---|
214 | return manipulateAtoms(op,name,AllAtoms());
|
---|
215 | }
|
---|
216 |
|
---|
217 | /********************* Internal Change methods for double Callback and Observer mechanism ********/
|
---|
218 |
|
---|
219 | void World::doManipulate(ManipulateAtomsProcess *proc){
|
---|
220 | proc->signOn(this);
|
---|
221 | {
|
---|
222 | OBSERVE;
|
---|
223 | proc->doManipulate(this);
|
---|
224 | }
|
---|
225 | proc->signOff(this);
|
---|
226 | }
|
---|
227 | /******************************* IDManagement *****************************/
|
---|
228 |
|
---|
229 | // Atoms
|
---|
230 |
|
---|
231 | atomId_t World::getNextAtomId(){
|
---|
232 | // try to find an Id in the pool;
|
---|
233 | if(!atomIdPool.empty()){
|
---|
234 | atomIdPool_t::iterator iter=atomIdPool.begin();
|
---|
235 | atomId_t id = iter->first;
|
---|
236 | pair<atomId_t,atomId_t> newRange = make_pair(id+1,iter->second);
|
---|
237 | // we wont use this iterator anymore, so we don't care about invalidating
|
---|
238 | atomIdPool.erase(iter);
|
---|
239 | if(newRange.first<newRange.second){
|
---|
240 | atomIdPool.insert(newRange);
|
---|
241 | }
|
---|
242 | return id;
|
---|
243 | }
|
---|
244 | // Nothing in the pool... we are out of luck
|
---|
245 | return currAtomId++;
|
---|
246 | }
|
---|
247 |
|
---|
248 | void World::releaseAtomId(atomId_t id){
|
---|
249 | atomIdPool.insert(make_pair(id,id+1));
|
---|
250 | defragAtomIdPool();
|
---|
251 | }
|
---|
252 |
|
---|
253 | bool World::reserveAtomId(atomId_t id){
|
---|
254 | if(id>=currAtomId ){
|
---|
255 | pair<atomId_t,atomId_t> newRange = make_pair(currAtomId,id);
|
---|
256 | if(newRange.first<newRange.second){
|
---|
257 | atomIdPool.insert(newRange);
|
---|
258 | }
|
---|
259 | currAtomId=id+1;
|
---|
260 | defragAtomIdPool();
|
---|
261 | return true;
|
---|
262 | }
|
---|
263 | // look for a range that matches the request
|
---|
264 | for(atomIdPool_t::iterator iter=atomIdPool.begin();iter!=atomIdPool.end();++iter){
|
---|
265 | if(iter->first>id){
|
---|
266 | // we have coverd all available ranges... nothing to be found here
|
---|
267 | break;
|
---|
268 | }
|
---|
269 | // no need to check first, since it has to be <=id, since otherwise we would have broken out
|
---|
270 | if(iter->second > id){
|
---|
271 | // we found a matching range... get the id from this range
|
---|
272 |
|
---|
273 | // split up this range at the point of id
|
---|
274 | pair<atomId_t,atomId_t> bottomRange = make_pair(iter->first,id);
|
---|
275 | pair<atomId_t,atomId_t> topRange = make_pair(id+1,iter->second);
|
---|
276 | // remove this range
|
---|
277 | atomIdPool.erase(iter);
|
---|
278 | if(bottomRange.first<bottomRange.second){
|
---|
279 | atomIdPool.insert(bottomRange);
|
---|
280 | }
|
---|
281 | if(topRange.first<topRange.second){
|
---|
282 | atomIdPool.insert(topRange);
|
---|
283 | }
|
---|
284 | defragAtomIdPool();
|
---|
285 | return true;
|
---|
286 | }
|
---|
287 | }
|
---|
288 | // this ID could not be reserved
|
---|
289 | return false;
|
---|
290 | }
|
---|
291 |
|
---|
292 | void World::defragAtomIdPool(){
|
---|
293 | // check if the situation is bad enough to make defragging neccessary
|
---|
294 | if((numAtomDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
|
---|
295 | (atomIdPool.size()<lastAtomPoolSize+MAX_POOL_FRAGMENTATION)){
|
---|
296 | ++numAtomDefragSkips;
|
---|
297 | return;
|
---|
298 | }
|
---|
299 | for(atomIdPool_t::iterator iter = atomIdPool.begin();iter!=atomIdPool.end();){
|
---|
300 | // see if this range is adjacent to the next one
|
---|
301 | atomIdPool_t::iterator next = iter;
|
---|
302 | next++;
|
---|
303 | if(next!=atomIdPool.end() && (next->first==iter->second)){
|
---|
304 | // merge the two ranges
|
---|
305 | pair<atomId_t,atomId_t> newRange = make_pair(iter->first,next->second);
|
---|
306 | atomIdPool.erase(iter);
|
---|
307 | atomIdPool.erase(next);
|
---|
308 | pair<atomIdPool_t::iterator,bool> res = atomIdPool.insert(newRange);
|
---|
309 | ASSERT(res.second,"Id-Pool was confused");
|
---|
310 | iter=res.first;
|
---|
311 | continue;
|
---|
312 | }
|
---|
313 | ++iter;
|
---|
314 | }
|
---|
315 | if(!atomIdPool.empty()){
|
---|
316 | // check if the last range is at the border
|
---|
317 | atomIdPool_t::iterator iter = atomIdPool.end();
|
---|
318 | iter--;
|
---|
319 | if(iter->second==currAtomId){
|
---|
320 | currAtomId=iter->first;
|
---|
321 | atomIdPool.erase(iter);
|
---|
322 | }
|
---|
323 | }
|
---|
324 | lastAtomPoolSize=atomIdPool.size();
|
---|
325 | numAtomDefragSkips=0;
|
---|
326 | }
|
---|
327 |
|
---|
328 | // Molecules
|
---|
329 |
|
---|
330 | moleculeId_t World::getNextMoleculeId(){
|
---|
331 | // try to find an Id in the pool;
|
---|
332 | if(!moleculeIdPool.empty()){
|
---|
333 | moleculeIdPool_t::iterator iter=moleculeIdPool.begin();
|
---|
334 | moleculeId_t id = iter->first;
|
---|
335 | pair<moleculeId_t,moleculeId_t> newRange = make_pair(id+1,iter->second);
|
---|
336 | // we wont use this iterator anymore, so we don't care about invalidating
|
---|
337 | moleculeIdPool.erase(iter);
|
---|
338 | if(newRange.first<newRange.second){
|
---|
339 | moleculeIdPool.insert(newRange);
|
---|
340 | }
|
---|
341 | return id;
|
---|
342 | }
|
---|
343 | // Nothing in the pool... we are out of luck
|
---|
344 | return currMoleculeId++;
|
---|
345 | }
|
---|
346 |
|
---|
347 | void World::releaseMoleculeId(moleculeId_t id){
|
---|
348 | moleculeIdPool.insert(make_pair(id,id+1));
|
---|
349 | defragMoleculeIdPool();
|
---|
350 | }
|
---|
351 |
|
---|
352 | bool World::reserveMoleculeId(moleculeId_t id){
|
---|
353 | if(id>=currMoleculeId ){
|
---|
354 | pair<moleculeId_t,moleculeId_t> newRange = make_pair(currMoleculeId,id);
|
---|
355 | if(newRange.first<newRange.second){
|
---|
356 | moleculeIdPool.insert(newRange);
|
---|
357 | }
|
---|
358 | currMoleculeId=id+1;
|
---|
359 | defragMoleculeIdPool();
|
---|
360 | return true;
|
---|
361 | }
|
---|
362 | // look for a range that matches the request
|
---|
363 | for(moleculeIdPool_t::iterator iter=moleculeIdPool.begin();iter!=moleculeIdPool.end();++iter){
|
---|
364 | if(iter->first>id){
|
---|
365 | // we have coverd all available ranges... nothing to be found here
|
---|
366 | break;
|
---|
367 | }
|
---|
368 | // no need to check first, since it has to be <=id, since otherwise we would have broken out
|
---|
369 | if(iter->second > id){
|
---|
370 | // we found a matching range... get the id from this range
|
---|
371 |
|
---|
372 | // split up this range at the point of id
|
---|
373 | pair<moleculeId_t,moleculeId_t> bottomRange = make_pair(iter->first,id);
|
---|
374 | pair<moleculeId_t,moleculeId_t> topRange = make_pair(id+1,iter->second);
|
---|
375 | // remove this range
|
---|
376 | moleculeIdPool.erase(iter);
|
---|
377 | if(bottomRange.first<bottomRange.second){
|
---|
378 | moleculeIdPool.insert(bottomRange);
|
---|
379 | }
|
---|
380 | if(topRange.first<topRange.second){
|
---|
381 | moleculeIdPool.insert(topRange);
|
---|
382 | }
|
---|
383 | defragMoleculeIdPool();
|
---|
384 | return true;
|
---|
385 | }
|
---|
386 | }
|
---|
387 | // this ID could not be reserved
|
---|
388 | return false;
|
---|
389 | }
|
---|
390 |
|
---|
391 | void World::defragMoleculeIdPool(){
|
---|
392 | // check if the situation is bad enough to make defragging neccessary
|
---|
393 | if((numMoleculeDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
|
---|
394 | (moleculeIdPool.size()<lastMoleculePoolSize+MAX_POOL_FRAGMENTATION)){
|
---|
395 | ++numMoleculeDefragSkips;
|
---|
396 | return;
|
---|
397 | }
|
---|
398 | for(moleculeIdPool_t::iterator iter = moleculeIdPool.begin();iter!=moleculeIdPool.end();){
|
---|
399 | // see if this range is adjacent to the next one
|
---|
400 | moleculeIdPool_t::iterator next = iter;
|
---|
401 | next++;
|
---|
402 | if(next!=moleculeIdPool.end() && (next->first==iter->second)){
|
---|
403 | // merge the two ranges
|
---|
404 | pair<moleculeId_t,moleculeId_t> newRange = make_pair(iter->first,next->second);
|
---|
405 | moleculeIdPool.erase(iter);
|
---|
406 | moleculeIdPool.erase(next);
|
---|
407 | pair<moleculeIdPool_t::iterator,bool> res = moleculeIdPool.insert(newRange);
|
---|
408 | ASSERT(res.second,"Id-Pool was confused");
|
---|
409 | iter=res.first;
|
---|
410 | continue;
|
---|
411 | }
|
---|
412 | ++iter;
|
---|
413 | }
|
---|
414 | if(!moleculeIdPool.empty()){
|
---|
415 | // check if the last range is at the border
|
---|
416 | moleculeIdPool_t::iterator iter = moleculeIdPool.end();
|
---|
417 | iter--;
|
---|
418 | if(iter->second==currMoleculeId){
|
---|
419 | currMoleculeId=iter->first;
|
---|
420 | moleculeIdPool.erase(iter);
|
---|
421 | }
|
---|
422 | }
|
---|
423 | lastMoleculePoolSize=moleculeIdPool.size();
|
---|
424 | numMoleculeDefragSkips=0;
|
---|
425 | }
|
---|
426 |
|
---|
427 | /******************************* Iterators ********************************/
|
---|
428 |
|
---|
429 | // external parts with observers
|
---|
430 |
|
---|
431 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
|
---|
432 |
|
---|
433 | World::AtomIterator
|
---|
434 | World::getAtomIter(AtomDescriptor descr){
|
---|
435 | return AtomIterator(descr,atoms);
|
---|
436 | }
|
---|
437 |
|
---|
438 | World::AtomIterator
|
---|
439 | World::getAtomIter(){
|
---|
440 | return AtomIterator(AllAtoms(),atoms);
|
---|
441 | }
|
---|
442 |
|
---|
443 | World::AtomIterator
|
---|
444 | World::atomEnd(){
|
---|
445 | return AtomIterator(AllAtoms(),atoms,atoms.end());
|
---|
446 | }
|
---|
447 |
|
---|
448 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
|
---|
449 |
|
---|
450 | World::MoleculeIterator
|
---|
451 | World::getMoleculeIter(MoleculeDescriptor descr){
|
---|
452 | return MoleculeIterator(descr,molecules);
|
---|
453 | }
|
---|
454 |
|
---|
455 | World::MoleculeIterator
|
---|
456 | World::getMoleculeIter(){
|
---|
457 | return MoleculeIterator(AllMolecules(),molecules);
|
---|
458 | }
|
---|
459 |
|
---|
460 | World::MoleculeIterator
|
---|
461 | World::moleculeEnd(){
|
---|
462 | return MoleculeIterator(AllMolecules(),molecules,molecules.end());
|
---|
463 | }
|
---|
464 |
|
---|
465 | // Internal parts, without observers
|
---|
466 |
|
---|
467 | // Build the AtomIterator from template
|
---|
468 | CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet::set_t,AtomDescriptor);
|
---|
469 |
|
---|
470 |
|
---|
471 | World::internal_AtomIterator
|
---|
472 | World::getAtomIter_internal(AtomDescriptor descr){
|
---|
473 | return internal_AtomIterator(descr,atoms.getContent());
|
---|
474 | }
|
---|
475 |
|
---|
476 | World::internal_AtomIterator
|
---|
477 | World::atomEnd_internal(){
|
---|
478 | return internal_AtomIterator(AllAtoms(),atoms.getContent(),atoms.end_internal());
|
---|
479 | }
|
---|
480 |
|
---|
481 | // build the MoleculeIterator from template
|
---|
482 | CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet::set_t,MoleculeDescriptor);
|
---|
483 |
|
---|
484 | World::internal_MoleculeIterator World::getMoleculeIter_internal(MoleculeDescriptor descr){
|
---|
485 | return internal_MoleculeIterator(descr,molecules.getContent());
|
---|
486 | }
|
---|
487 |
|
---|
488 | World::internal_MoleculeIterator World::moleculeEnd_internal(){
|
---|
489 | return internal_MoleculeIterator(AllMolecules(),molecules.getContent(),molecules.end_internal());
|
---|
490 | }
|
---|
491 |
|
---|
492 | /************************** Selection of Atoms and molecules ******************/
|
---|
493 |
|
---|
494 | // Atoms
|
---|
495 |
|
---|
496 | void World::clearAtomSelection(){
|
---|
497 | selectedAtoms.clear();
|
---|
498 | }
|
---|
499 |
|
---|
500 | void World::selectAtom(atom *atom){
|
---|
501 | ASSERT(atom,"Invalid pointer in selection of atom");
|
---|
502 | selectedAtoms[atom->getId()]=atom;
|
---|
503 | }
|
---|
504 |
|
---|
505 | void World::selectAtom(atomId_t id){
|
---|
506 | ASSERT(atoms.count(id),"Atom Id selected that was not in the world");
|
---|
507 | selectedAtoms[id]=atoms[id];
|
---|
508 | }
|
---|
509 |
|
---|
510 | void World::selectAllAtoms(AtomDescriptor descr){
|
---|
511 | internal_AtomIterator begin = getAtomIter_internal(descr);
|
---|
512 | internal_AtomIterator end = atomEnd_internal();
|
---|
513 | void (World::*func)(atom*) = &World::selectAtom; // needed for type resolution of overloaded function
|
---|
514 | for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
|
---|
515 | }
|
---|
516 |
|
---|
517 | void World::selectAtomsOfMolecule(molecule *_mol){
|
---|
518 | ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
|
---|
519 | // need to make it const to get the fast iterators
|
---|
520 | const molecule *mol = _mol;
|
---|
521 | void (World::*func)(atom*) = &World::selectAtom; // needed for type resolution of overloaded function
|
---|
522 | for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is select... see above
|
---|
523 | }
|
---|
524 |
|
---|
525 | void World::selectAtomsOfMolecule(moleculeId_t id){
|
---|
526 | ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
|
---|
527 | selectAtomsOfMolecule(molecules[id]);
|
---|
528 | }
|
---|
529 |
|
---|
530 | void World::unselectAtom(atom *atom){
|
---|
531 | ASSERT(atom,"Invalid pointer in unselection of atom");
|
---|
532 | unselectAtom(atom->getId());
|
---|
533 | }
|
---|
534 |
|
---|
535 | void World::unselectAtom(atomId_t id){
|
---|
536 | ASSERT(atoms.count(id),"Atom Id unselected that was not in the world");
|
---|
537 | selectedAtoms.erase(id);
|
---|
538 | }
|
---|
539 |
|
---|
540 | void World::unselectAllAtoms(AtomDescriptor descr){
|
---|
541 | internal_AtomIterator begin = getAtomIter_internal(descr);
|
---|
542 | internal_AtomIterator end = atomEnd_internal();
|
---|
543 | void (World::*func)(atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
|
---|
544 | for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
|
---|
545 | }
|
---|
546 |
|
---|
547 | void World::unselectAtomsOfMolecule(molecule *_mol){
|
---|
548 | ASSERT(_mol,"Invalid pointer to molecule in selection of Atoms of Molecule");
|
---|
549 | // need to make it const to get the fast iterators
|
---|
550 | const molecule *mol = _mol;
|
---|
551 | void (World::*func)(atom*) = &World::unselectAtom; // needed for type resolution of overloaded function
|
---|
552 | for_each(mol->begin(),mol->end(),bind1st(mem_fun(func),this)); // func is unsselect... see above
|
---|
553 | }
|
---|
554 |
|
---|
555 | void World::unselectAtomsOfMolecule(moleculeId_t id){
|
---|
556 | ASSERT(molecules.count(id),"No molecule with the given id upon Selection of atoms from molecule");
|
---|
557 | unselectAtomsOfMolecule(molecules[id]);
|
---|
558 | }
|
---|
559 |
|
---|
560 | size_t World::countSelectecAtoms() {
|
---|
561 | size_t count = 0;
|
---|
562 | for (internal_AtomIterator iter = atomBegin_internal(); iter != atomEnd_internal(); ++iter)
|
---|
563 | count++;
|
---|
564 | return count;
|
---|
565 | }
|
---|
566 |
|
---|
567 | // Molecules
|
---|
568 |
|
---|
569 | void World::clearMoleculeSelection(){
|
---|
570 | selectedMolecules.clear();
|
---|
571 | }
|
---|
572 |
|
---|
573 | void World::selectMolecule(molecule *mol){
|
---|
574 | ASSERT(mol,"Invalid pointer to molecule in selection");
|
---|
575 | selectedMolecules[mol->getId()]=mol;
|
---|
576 | }
|
---|
577 |
|
---|
578 | void World::selectMolecule(moleculeId_t id){
|
---|
579 | ASSERT(molecules.count(id),"Molecule Id selected that was not in the world");
|
---|
580 | selectedMolecules[id]=molecules[id];
|
---|
581 | }
|
---|
582 |
|
---|
583 | void World::selectAllMoleculess(MoleculeDescriptor descr){
|
---|
584 | internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
|
---|
585 | internal_MoleculeIterator end = moleculeEnd_internal();
|
---|
586 | void (World::*func)(molecule*) = &World::selectMolecule; // needed for type resolution of overloaded function
|
---|
587 | for_each(begin,end,bind1st(mem_fun(func),this)); // func is select... see above
|
---|
588 | }
|
---|
589 |
|
---|
590 | void World::selectMoleculeOfAtom(atom *atom){
|
---|
591 | ASSERT(atom,"Invalid atom pointer in selection of MoleculeOfAtom");
|
---|
592 | molecule *mol=atom->getMolecule();
|
---|
593 | // the atom might not be part of a molecule
|
---|
594 | if(mol){
|
---|
595 | selectMolecule(mol);
|
---|
596 | }
|
---|
597 | }
|
---|
598 |
|
---|
599 | void World::selectMoleculeOfAtom(atomId_t id){
|
---|
600 | ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
|
---|
601 | selectMoleculeOfAtom(atoms[id]);
|
---|
602 | }
|
---|
603 |
|
---|
604 | void World::unselectMolecule(molecule *mol){
|
---|
605 | ASSERT(mol,"invalid pointer in unselection of molecule");
|
---|
606 | unselectMolecule(mol->getId());
|
---|
607 | }
|
---|
608 |
|
---|
609 | void World::unselectMolecule(moleculeId_t id){
|
---|
610 | ASSERT(molecules.count(id),"No such molecule with ID in unselection");
|
---|
611 | selectedMolecules.erase(id);
|
---|
612 | }
|
---|
613 |
|
---|
614 | void World::unselectAllMoleculess(MoleculeDescriptor descr){
|
---|
615 | internal_MoleculeIterator begin = getMoleculeIter_internal(descr);
|
---|
616 | internal_MoleculeIterator end = moleculeEnd_internal();
|
---|
617 | void (World::*func)(molecule*) = &World::unselectMolecule; // needed for type resolution of overloaded function
|
---|
618 | for_each(begin,end,bind1st(mem_fun(func),this)); // func is unselect... see above
|
---|
619 | }
|
---|
620 |
|
---|
621 | void World::unselectMoleculeOfAtom(atom *atom){
|
---|
622 | ASSERT(atom,"Invalid atom pointer in selection of MoleculeOfAtom");
|
---|
623 | molecule *mol=atom->getMolecule();
|
---|
624 | // the atom might not be part of a molecule
|
---|
625 | if(mol){
|
---|
626 | unselectMolecule(mol);
|
---|
627 | }
|
---|
628 | }
|
---|
629 |
|
---|
630 | void World::unselectMoleculeOfAtom(atomId_t id){
|
---|
631 | ASSERT(atoms.count(id),"No such atom with given ID in selection of Molecules of Atom");\
|
---|
632 | unselectMoleculeOfAtom(atoms[id]);
|
---|
633 | }
|
---|
634 |
|
---|
635 | size_t World::countSelectecMolecules() {
|
---|
636 | size_t count = 0;
|
---|
637 | for (internal_MoleculeIterator iter = moleculeBegin_internal(); iter != moleculeEnd_internal(); ++iter)
|
---|
638 | count++;
|
---|
639 | return count;
|
---|
640 | }
|
---|
641 |
|
---|
642 | /******************* Iterators over Selection *****************************/
|
---|
643 | World::AtomSelectionIterator World::beginAtomSelection(){
|
---|
644 | return selectedAtoms.begin();
|
---|
645 | }
|
---|
646 |
|
---|
647 | World::AtomSelectionIterator World::endAtomSelection(){
|
---|
648 | return selectedAtoms.end();
|
---|
649 | }
|
---|
650 |
|
---|
651 |
|
---|
652 | World::MoleculeSelectionIterator World::beginMoleculeSelection(){
|
---|
653 | return selectedMolecules.begin();
|
---|
654 | }
|
---|
655 |
|
---|
656 | World::MoleculeSelectionIterator World::endMoleculeSelection(){
|
---|
657 | return selectedMolecules.end();
|
---|
658 | }
|
---|
659 |
|
---|
660 | /******************************* Singleton Stuff **************************/
|
---|
661 |
|
---|
662 | World::World() :
|
---|
663 | Observable("World"),
|
---|
664 | periode(new periodentafel),
|
---|
665 | configuration(new config),
|
---|
666 | Thermostats(new ThermoStatContainer),
|
---|
667 | ExitFlag(0),
|
---|
668 | atoms(this),
|
---|
669 | selectedAtoms(this),
|
---|
670 | currAtomId(0),
|
---|
671 | lastAtomPoolSize(0),
|
---|
672 | numAtomDefragSkips(0),
|
---|
673 | molecules(this),
|
---|
674 | selectedMolecules(this),
|
---|
675 | currMoleculeId(0),
|
---|
676 | molecules_deprecated(new MoleculeListClass(this))
|
---|
677 | {
|
---|
678 | cell_size = new Box;
|
---|
679 | Matrix domain;
|
---|
680 | domain.at(0,0) = 20;
|
---|
681 | domain.at(1,1) = 20;
|
---|
682 | domain.at(2,2) = 20;
|
---|
683 | cell_size->setM(domain);
|
---|
684 | defaultName = "none";
|
---|
685 | molecules_deprecated->signOn(this);
|
---|
686 | }
|
---|
687 |
|
---|
688 | World::~World()
|
---|
689 | {
|
---|
690 | molecules_deprecated->signOff(this);
|
---|
691 | delete cell_size;
|
---|
692 | delete molecules_deprecated;
|
---|
693 | delete periode;
|
---|
694 | delete configuration;
|
---|
695 | delete Thermostats;
|
---|
696 | MoleculeSet::iterator molIter;
|
---|
697 | for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
|
---|
698 | DeleteMolecule((*molIter).second);
|
---|
699 | }
|
---|
700 | molecules.clear();
|
---|
701 | AtomSet::iterator atIter;
|
---|
702 | for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
|
---|
703 | DeleteAtom((*atIter).second);
|
---|
704 | }
|
---|
705 | atoms.clear();
|
---|
706 | }
|
---|
707 |
|
---|
708 | // Explicit instantiation of the singleton mechanism at this point
|
---|
709 |
|
---|
710 | CONSTRUCT_SINGLETON(World)
|
---|
711 |
|
---|
712 | /******************************* deprecated Legacy Stuff ***********************/
|
---|
713 |
|
---|
714 | MoleculeListClass *&World::getMolecules() {
|
---|
715 | return molecules_deprecated;
|
---|
716 | }
|
---|