source: src/World.cpp@ 4a611e

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 4a611e was 0d1ad0, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Merge branch 'stable' into StructureRefactoring

Conflicts:

molecuilder/src/World.cpp

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