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