source: src/World.cpp@ db7cb0

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 db7cb0 was 654394, checked in by Frederik Heber <heber@…>, 15 years ago

MEMFIX: World::numMoleculeDefragSkips and World::lastMoleculePoolSize were not set in constructor.

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