source: src/World.cpp@ 6cfa36

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 6cfa36 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
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"
[fc1b24]16#include "Descriptors/AtomDescriptor.hpp"
[865a945]17#include "Descriptors/AtomDescriptor_impl.hpp"
[1c51c8]18#include "Descriptors/MoleculeDescriptor.hpp"
19#include "Descriptors/MoleculeDescriptor_impl.hpp"
[6e97e5]20#include "Descriptors/SelectiveIterator_impl.hpp"
[7c4e29]21#include "Actions/ManipulateAtomsProcess.hpp"
[d346b6]22
[23b547]23#include "Patterns/Singleton_impl.hpp"
24
[d346b6]25using namespace std;
[4d9c01]26
[5d1611]27/******************************* getter and setter ************************/
[354859]28periodentafel *&World::getPeriode(){
[5d1611]29 return periode;
30}
31
[8e1f7af]32config *&World::getConfig(){
33 return configuration;
34}
35
[1c51c8]36// Atoms
37
[7a1ce5]38atom* World::getAtom(AtomDescriptor descriptor){
[fc1b24]39 return descriptor.find();
40}
41
[7a1ce5]42vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
[fc1b24]43 return descriptor.findAll();
44}
45
[0e2a47]46vector<atom*> World::getAllAtoms(){
47 return getAllAtoms(AllAtoms());
48}
49
[354859]50int World::numAtoms(){
51 return atoms.size();
52}
53
[1c51c8]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
[97ebf8]64std::vector<molecule*> World::getAllMolecules(){
65 return getAllMolecules(AllMolecules());
66}
67
[354859]68int World::numMolecules(){
69 return molecules_deprecated->ListOfMolecules.size();
70}
71
[5f612ee]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);
[93d732b]91 if (length < MAXSTRINGSIZE) {
92 defaultName = new char[length+2];
[5f612ee]93 strncpy(defaultName, name, length);
[93d732b]94 } else {
95 defaultName = new char[MAXSTRINGSIZE];
96 strncpy(defaultName, "none", MAXSTRINGSIZE-1);
97 }
[5f612ee]98};
99
100
[afb47f]101/******************** Methods to change World state *********************/
102
[354859]103molecule* World::createMolecule(){
104 OBSERVE;
105 molecule *mol = NULL;
[cbc5fb]106 mol = NewMolecule();
[d2dbac0]107 assert(!molecules.count(currMoleculeId));
[cbc5fb]108 mol->setId(currMoleculeId++);
[244d26]109 // store the molecule by ID
[cbc5fb]110 molecules[mol->getId()] = mol;
[354859]111 mol->signOn(this);
112 return mol;
113}
114
[cbc5fb]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
[5f612ee]128double *World::cell_size = NULL;
129char *World::defaultName = NULL;
[7c4e29]130
[46d958]131atom *World::createAtom(){
132 OBSERVE;
[88d586]133 atomId_t id = getNextAtomId();
134 atom *res = NewAtom(id);
[46d958]135 res->setWorld(this);
[244d26]136 // store the atom by ID
[46d958]137 atoms[res->getId()] = res;
138 return res;
139}
140
[5f612ee]141
[46d958]142int World::registerAtom(atom *atom){
143 OBSERVE;
[88d586]144 atomId_t id = getNextAtomId();
145 atom->setId(id);
[46d958]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
[cbc5fb]157void World::destroyAtom(atomId_t id) {
[46d958]158 OBSERVE;
159 atom *atom = atoms[id];
160 assert(atom);
161 DeleteAtom(atom);
162 atoms.erase(id);
[88d586]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 }
[46d958]185}
186
[7c4e29]187ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
188 return new ManipulateAtomsProcess(op, descr,name,true);
189}
190
[0e2a47]191ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
192 return manipulateAtoms(op,name,AllAtoms());
193}
194
[afb47f]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}
[88d586]205/******************************* IDManagement *****************************/
206
[57adc7]207// Atoms
208
[88d586]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);
[23b547]218 return id;
[88d586]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}
[afb47f]231
[88d586]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}
[57adc7]250
251// Molecules
252
[865a945]253/******************************* Iterators ********************************/
254
[6e97e5]255// Build the AtomIterator from template
256CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
257
[865a945]258
259World::AtomIterator World::getAtomIter(AtomDescriptor descr){
[6e97e5]260 return AtomIterator(descr,atoms);
[865a945]261}
[354859]262
[6e97e5]263World::AtomIterator World::atomEnd(){
264 return AtomIterator(AllAtoms(),atoms,atoms.end());
[7c4e29]265}
266
[6e97e5]267// build the MoleculeIterator from template
268CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
269
[1c51c8]270World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
[6e97e5]271 return MoleculeIterator(descr,molecules);
[1c51c8]272}
273
[6e97e5]274World::MoleculeIterator World::moleculeEnd(){
275 return MoleculeIterator(AllMolecules(),molecules,molecules.end());
[1c51c8]276}
277
[5d1611]278/******************************* Singleton Stuff **************************/
279
[7a1ce5]280World::World() :
[cd5047]281 Observable("World"),
[354859]282 periode(new periodentafel),
[8e1f7af]283 configuration(new config),
[d2dbac0]284 atoms(),
[24a5e0]285 currAtomId(0),
286 molecules(),
287 currMoleculeId(0),
288 molecules_deprecated(new MoleculeListClass(this))
[7dad10]289{
[b34306]290 cell_size = new double[6];
[fd179f]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.;
[5f612ee]297 defaultName = new char[MAXSTRINGSIZE];
298 strcpy(defaultName, "none");
[7dad10]299 molecules_deprecated->signOn(this);
300}
[5d1611]301
302World::~World()
[354859]303{
[028c2e]304 molecules_deprecated->signOff(this);
[5f612ee]305 delete[] cell_size;
306 delete[] defaultName;
[46d958]307 delete molecules_deprecated;
[354859]308 delete periode;
[8e1f7af]309 delete configuration;
[cbc5fb]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);
[46d958]318 }
319 atoms.clear();
[354859]320}
[5d1611]321
[23b547]322// Explicit instantiation of the singleton mechanism at this point
[5d1611]323
[23b547]324CONSTRUCT_SINGLETON(World)
[5d1611]325
326/******************************* deprecated Legacy Stuff ***********************/
327
[354859]328MoleculeListClass *&World::getMolecules() {
329 return molecules_deprecated;
[5d1611]330}
Note: See TracBrowser for help on using the repository browser.