source: src/World.cpp@ 112b09

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 112b09 was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

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