source: src/World.cpp@ 6d574a

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

Replaced several old-style asserts with more usable ASSERTs

  • Property mode set to 100644
File size: 7.3 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"
[6d574a]22#include "Helpers/Assert.hpp"
[d346b6]23
[23b547]24#include "Patterns/Singleton_impl.hpp"
25
[d346b6]26using namespace std;
[4d9c01]27
[5d1611]28/******************************* getter and setter ************************/
[354859]29periodentafel *&World::getPeriode(){
[5d1611]30 return periode;
31}
32
[8e1f7af]33config *&World::getConfig(){
34 return configuration;
35}
36
[1c51c8]37// Atoms
38
[7a1ce5]39atom* World::getAtom(AtomDescriptor descriptor){
[fc1b24]40 return descriptor.find();
41}
42
[7a1ce5]43vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
[fc1b24]44 return descriptor.findAll();
45}
46
[0e2a47]47vector<atom*> World::getAllAtoms(){
48 return getAllAtoms(AllAtoms());
49}
50
[354859]51int World::numAtoms(){
52 return atoms.size();
53}
54
[1c51c8]55// Molecules
56
57molecule *World::getMolecule(MoleculeDescriptor descriptor){
58 return descriptor.find();
59}
60
61std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor){
62 return descriptor.findAll();
63}
64
[97ebf8]65std::vector<molecule*> World::getAllMolecules(){
66 return getAllMolecules(AllMolecules());
67}
68
[354859]69int World::numMolecules(){
70 return molecules_deprecated->ListOfMolecules.size();
71}
72
[5f612ee]73// system
74
75double * World::getDomain() {
76 return cell_size;
77}
78
79void World::setDomain(double * matrix)
80{
81
82}
83
[387b36]84std::string World::getDefaultName() {
[5f612ee]85 return defaultName;
86}
87
[387b36]88void World::setDefaultName(std::string name)
[5f612ee]89{
[387b36]90 defaultName = name;
[5f612ee]91};
92
[e4b5de]93int World::getExitFlag() {
94 return ExitFlag;
95}
96
97void World::setExitFlag(int flag) {
98 if (ExitFlag < flag)
99 ExitFlag = flag;
100}
[5f612ee]101
[afb47f]102/******************** Methods to change World state *********************/
103
[354859]104molecule* World::createMolecule(){
105 OBSERVE;
106 molecule *mol = NULL;
[cbc5fb]107 mol = NewMolecule();
[6d574a]108 ASSERT(!molecules.count(currMoleculeId),"currMoleculeId did not specify an unused ID");
[cbc5fb]109 mol->setId(currMoleculeId++);
[244d26]110 // store the molecule by ID
[cbc5fb]111 molecules[mol->getId()] = mol;
[354859]112 mol->signOn(this);
113 return mol;
114}
115
[cbc5fb]116void World::destroyMolecule(molecule* mol){
117 OBSERVE;
118 destroyMolecule(mol->getId());
119}
120
121void World::destroyMolecule(moleculeId_t id){
122 OBSERVE;
123 molecule *mol = molecules[id];
[6d574a]124 ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
[cbc5fb]125 DeleteMolecule(mol);
126 molecules.erase(id);
127}
128
[5f612ee]129double *World::cell_size = 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];
[6d574a]160 ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
[46d958]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];
[6d574a]172 ASSERT(target,"Atom with that ID not found");
[88d586]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),
[e4b5de]284 ExitFlag(0),
[d2dbac0]285 atoms(),
[24a5e0]286 currAtomId(0),
287 molecules(),
288 currMoleculeId(0),
289 molecules_deprecated(new MoleculeListClass(this))
[7dad10]290{
[b34306]291 cell_size = new double[6];
[fd179f]292 cell_size[0] = 20.;
293 cell_size[1] = 0.;
294 cell_size[2] = 20.;
295 cell_size[3] = 0.;
296 cell_size[4] = 0.;
297 cell_size[5] = 20.;
[387b36]298 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;
[46d958]306 delete molecules_deprecated;
[354859]307 delete periode;
[8e1f7af]308 delete configuration;
[cbc5fb]309 MoleculeSet::iterator molIter;
310 for(molIter=molecules.begin();molIter!=molecules.end();++molIter){
311 DeleteMolecule((*molIter).second);
312 }
313 molecules.clear();
314 AtomSet::iterator atIter;
315 for(atIter=atoms.begin();atIter!=atoms.end();++atIter){
316 DeleteAtom((*atIter).second);
[46d958]317 }
318 atoms.clear();
[354859]319}
[5d1611]320
[23b547]321// Explicit instantiation of the singleton mechanism at this point
[5d1611]322
[23b547]323CONSTRUCT_SINGLETON(World)
[5d1611]324
325/******************************* deprecated Legacy Stuff ***********************/
326
[354859]327MoleculeListClass *&World::getMolecules() {
328 return molecules_deprecated;
[5d1611]329}
Note: See TracBrowser for help on using the repository browser.