source: src/World.cpp@ 7ac4af

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

Made the world store the cell_size within a Box object.

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