source: src/World.cpp@ 148d8f0

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 148d8f0 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
RevLine 
[5d1611]1/*
2 * World.cpp
3 *
4 * Created on: Feb 3, 2010
5 * Author: crueger
6 */
7
8#include "World.hpp"
9
[d346b6]10#include "atom.hpp"
[8e1f7af]11#include "config.hpp"
[354859]12#include "molecule.hpp"
13#include "periodentafel.hpp"
[fc1b24]14#include "Descriptors/AtomDescriptor.hpp"
[865a945]15#include "Descriptors/AtomDescriptor_impl.hpp"
[1c51c8]16#include "Descriptors/MoleculeDescriptor.hpp"
17#include "Descriptors/MoleculeDescriptor_impl.hpp"
[6e97e5]18#include "Descriptors/SelectiveIterator_impl.hpp"
[7c4e29]19#include "Actions/ManipulateAtomsProcess.hpp"
[d346b6]20
[23b547]21#include "Patterns/Singleton_impl.hpp"
22
[d346b6]23using namespace std;
[4d9c01]24
[5d1611]25/******************************* getter and setter ************************/
[354859]26periodentafel *&World::getPeriode(){
[5d1611]27 return periode;
28}
29
[8e1f7af]30config *&World::getConfig(){
31 return configuration;
32}
33
[1c51c8]34// Atoms
35
[7a1ce5]36atom* World::getAtom(AtomDescriptor descriptor){
[fc1b24]37 return descriptor.find();
38}
39
[7a1ce5]40vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
[fc1b24]41 return descriptor.findAll();
42}
43
[0e2a47]44vector<atom*> World::getAllAtoms(){
45 return getAllAtoms(AllAtoms());
46}
47
[354859]48int World::numAtoms(){
49 return atoms.size();
50}
51
[1c51c8]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
[354859]62int World::numMolecules(){
63 return molecules_deprecated->ListOfMolecules.size();
64}
65
[5f612ee]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
[afb47f]93/******************** Methods to change World state *********************/
94
[354859]95molecule* World::createMolecule(){
96 OBSERVE;
97 molecule *mol = NULL;
[cbc5fb]98 mol = NewMolecule();
[d2dbac0]99 assert(!molecules.count(currMoleculeId));
[cbc5fb]100 mol->setId(currMoleculeId++);
[244d26]101 // store the molecule by ID
[cbc5fb]102 molecules[mol->getId()] = mol;
[354859]103 mol->signOn(this);
104 return mol;
105}
106
[cbc5fb]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
[5f612ee]120double *World::cell_size = NULL;
121char *World::defaultName = NULL;
[7c4e29]122
[46d958]123atom *World::createAtom(){
124 OBSERVE;
[88d586]125 atomId_t id = getNextAtomId();
126 atom *res = NewAtom(id);
[46d958]127 res->setWorld(this);
[244d26]128 // store the atom by ID
[46d958]129 atoms[res->getId()] = res;
130 return res;
131}
132
[5f612ee]133
[46d958]134int World::registerAtom(atom *atom){
135 OBSERVE;
[88d586]136 atomId_t id = getNextAtomId();
137 atom->setId(id);
[46d958]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
[cbc5fb]149void World::destroyAtom(atomId_t id) {
[46d958]150 OBSERVE;
151 atom *atom = atoms[id];
152 assert(atom);
153 DeleteAtom(atom);
154 atoms.erase(id);
[88d586]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 }
[46d958]177}
178
[7c4e29]179ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
180 return new ManipulateAtomsProcess(op, descr,name,true);
181}
182
[0e2a47]183ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
184 return manipulateAtoms(op,name,AllAtoms());
185}
186
[afb47f]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}
[88d586]197/******************************* IDManagement *****************************/
198
[57adc7]199// Atoms
200
[88d586]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);
[23b547]210 return id;
[88d586]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}
[afb47f]223
[88d586]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}
[57adc7]242
243// Molecules
244
[865a945]245/******************************* Iterators ********************************/
246
[6e97e5]247// Build the AtomIterator from template
248CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
249
[865a945]250
251World::AtomIterator World::getAtomIter(AtomDescriptor descr){
[6e97e5]252 return AtomIterator(descr,atoms);
[865a945]253}
[354859]254
[6e97e5]255World::AtomIterator World::atomEnd(){
256 return AtomIterator(AllAtoms(),atoms,atoms.end());
[7c4e29]257}
258
[6e97e5]259// build the MoleculeIterator from template
260CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
261
[1c51c8]262World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
[6e97e5]263 return MoleculeIterator(descr,molecules);
[1c51c8]264}
265
[6e97e5]266World::MoleculeIterator World::moleculeEnd(){
267 return MoleculeIterator(AllMolecules(),molecules,molecules.end());
[1c51c8]268}
269
[5d1611]270/******************************* Singleton Stuff **************************/
271
[7a1ce5]272World::World() :
[354859]273 periode(new periodentafel),
[8e1f7af]274 configuration(new config),
[d2dbac0]275 atoms(),
[24a5e0]276 currAtomId(0),
277 molecules(),
278 currMoleculeId(0),
279 molecules_deprecated(new MoleculeListClass(this))
[7dad10]280{
[b34306]281 cell_size = new double[6];
[fd179f]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.;
[5f612ee]288 defaultName = new char[MAXSTRINGSIZE];
289 strcpy(defaultName, "none");
[7dad10]290 molecules_deprecated->signOn(this);
291}
[5d1611]292
293World::~World()
[354859]294{
[028c2e]295 molecules_deprecated->signOff(this);
[5f612ee]296 delete[] cell_size;
297 delete[] defaultName;
[46d958]298 delete molecules_deprecated;
[354859]299 delete periode;
[8e1f7af]300 delete configuration;
[cbc5fb]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);
[46d958]309 }
310 atoms.clear();
[354859]311}
[5d1611]312
[23b547]313// Explicit instantiation of the singleton mechanism at this point
[5d1611]314
[23b547]315CONSTRUCT_SINGLETON(World)
[5d1611]316
317/******************************* deprecated Legacy Stuff ***********************/
318
[354859]319MoleculeListClass *&World::getMolecules() {
320 return molecules_deprecated;
[5d1611]321}
Note: See TracBrowser for help on using the repository browser.