source: src/World.cpp@ 436f04

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 436f04 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
Line 
1/*
2 * World.cpp
3 *
4 * Created on: Feb 3, 2010
5 * Author: crueger
6 */
7
8#include "Helpers/MemDebug.hpp"
9
10#include "World.hpp"
11
12#include "atom.hpp"
13#include "config.hpp"
14#include "molecule.hpp"
15#include "periodentafel.hpp"
16#include "ThermoStatContainer.hpp"
17#include "Descriptors/AtomDescriptor.hpp"
18#include "Descriptors/AtomDescriptor_impl.hpp"
19#include "Descriptors/MoleculeDescriptor.hpp"
20#include "Descriptors/MoleculeDescriptor_impl.hpp"
21#include "Descriptors/SelectiveIterator_impl.hpp"
22#include "Actions/ManipulateAtomsProcess.hpp"
23#include "Helpers/Assert.hpp"
24
25#include "Patterns/Singleton_impl.hpp"
26
27using namespace std;
28
29/******************************* getter and setter ************************/
30periodentafel *&World::getPeriode(){
31 return periode;
32}
33
34config *&World::getConfig(){
35 return configuration;
36}
37
38// Atoms
39
40atom* World::getAtom(AtomDescriptor descriptor){
41 return descriptor.find();
42}
43
44vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
45 return descriptor.findAll();
46}
47
48vector<atom*> World::getAllAtoms(){
49 return getAllAtoms(AllAtoms());
50}
51
52int World::numAtoms(){
53 return atoms.size();
54}
55
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
66std::vector<molecule*> World::getAllMolecules(){
67 return getAllMolecules(AllMolecules());
68}
69
70int World::numMolecules(){
71 return molecules_deprecated->ListOfMolecules.size();
72}
73
74// system
75
76double * World::getDomain() {
77 return cell_size;
78}
79
80void World::setDomain(double * matrix)
81{
82
83}
84
85std::string World::getDefaultName() {
86 return defaultName;
87}
88
89void World::setDefaultName(std::string name)
90{
91 defaultName = name;
92};
93
94class ThermoStatContainer * World::getThermostats()
95{
96 return Thermostats;
97}
98
99
100int World::getExitFlag() {
101 return ExitFlag;
102}
103
104void World::setExitFlag(int flag) {
105 if (ExitFlag < flag)
106 ExitFlag = flag;
107}
108
109/******************** Methods to change World state *********************/
110
111molecule* World::createMolecule(){
112 OBSERVE;
113 molecule *mol = NULL;
114 mol = NewMolecule();
115 ASSERT(!molecules.count(currMoleculeId),"currMoleculeId did not specify an unused ID");
116 mol->setId(currMoleculeId++);
117 // store the molecule by ID
118 molecules[mol->getId()] = mol;
119 mol->signOn(this);
120 return mol;
121}
122
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];
131 ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
132 DeleteMolecule(mol);
133 molecules.erase(id);
134}
135
136double *World::cell_size = NULL;
137
138atom *World::createAtom(){
139 OBSERVE;
140 atomId_t id = getNextAtomId();
141 atom *res = NewAtom(id);
142 res->setWorld(this);
143 // store the atom by ID
144 atoms[res->getId()] = res;
145 return res;
146}
147
148
149int World::registerAtom(atom *atom){
150 OBSERVE;
151 atomId_t id = getNextAtomId();
152 atom->setId(id);
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
164void World::destroyAtom(atomId_t id) {
165 OBSERVE;
166 atom *atom = atoms[id];
167 ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
168 DeleteAtom(atom);
169 atoms.erase(id);
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];
179 ASSERT(target,"Atom with that ID not found");
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 }
192}
193
194ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
195 return new ManipulateAtomsProcess(op, descr,name,true);
196}
197
198ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
199 return manipulateAtoms(op,name,AllAtoms());
200}
201
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}
212/******************************* IDManagement *****************************/
213
214// Atoms
215
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);
225 return id;
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}
238
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}
257
258// Molecules
259
260/******************************* Iterators ********************************/
261
262// Build the AtomIterator from template
263CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
264
265
266World::AtomIterator World::getAtomIter(AtomDescriptor descr){
267 return AtomIterator(descr,atoms);
268}
269
270World::AtomIterator World::atomEnd(){
271 return AtomIterator(AllAtoms(),atoms,atoms.end());
272}
273
274// build the MoleculeIterator from template
275CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
276
277World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
278 return MoleculeIterator(descr,molecules);
279}
280
281World::MoleculeIterator World::moleculeEnd(){
282 return MoleculeIterator(AllMolecules(),molecules,molecules.end());
283}
284
285/******************************* Singleton Stuff **************************/
286
287World::World() :
288 Observable("World"),
289 periode(new periodentafel),
290 configuration(new config),
291 Thermostats(new ThermoStatContainer),
292 ExitFlag(0),
293 atoms(),
294 currAtomId(0),
295 molecules(),
296 currMoleculeId(0),
297 molecules_deprecated(new MoleculeListClass(this))
298{
299 cell_size = new double[6];
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.;
306 defaultName = "none";
307 molecules_deprecated->signOn(this);
308}
309
310World::~World()
311{
312 molecules_deprecated->signOff(this);
313 delete[] cell_size;
314 delete molecules_deprecated;
315 delete periode;
316 delete configuration;
317 delete Thermostats;
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);
326 }
327 atoms.clear();
328}
329
330// Explicit instantiation of the singleton mechanism at this point
331
332CONSTRUCT_SINGLETON(World)
333
334/******************************* deprecated Legacy Stuff ***********************/
335
336MoleculeListClass *&World::getMolecules() {
337 return molecules_deprecated;
338}
Note: See TracBrowser for help on using the repository browser.