source: src/World.cpp@ 498c519

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 498c519 was 8e1f7af, checked in by Frederik Heber <heber@…>, 15 years ago

class config added to World.

  • World now has the global configuration.
  • new function World::getConfig().
  • TextWindow::TextWindow() uses thr above to obtain the configuration.

Signed-off-by: Frederik Heber <heber@…>

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