source: src/World.cpp@ 986ed3

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 986ed3 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
Line 
1/*
2 * World.cpp
3 *
4 * Created on: Feb 3, 2010
5 * Author: crueger
6 */
7
8#include "Helpers/MemDebug.hpp"
9
10#include "World.hpp"
11
12#include "atom.hpp"
13#include "config.hpp"
14#include "molecule.hpp"
15#include "periodentafel.hpp"
16#include "ThermoStatContainer.hpp"
17#include "Descriptors/AtomDescriptor.hpp"
18#include "Descriptors/AtomDescriptor_impl.hpp"
19#include "Descriptors/MoleculeDescriptor.hpp"
20#include "Descriptors/MoleculeDescriptor_impl.hpp"
21#include "Descriptors/SelectiveIterator_impl.hpp"
22#include "Actions/ManipulateAtomsProcess.hpp"
23#include "Helpers/Assert.hpp"
24#include "Box.hpp"
25#include "Matrix.hpp"
26
27#include "Patterns/Singleton_impl.hpp"
28
29using namespace std;
30
31/******************************* getter and setter ************************/
32periodentafel *&World::getPeriode(){
33 return periode;
34}
35
36config *&World::getConfig(){
37 return configuration;
38}
39
40// Atoms
41
42atom* World::getAtom(AtomDescriptor descriptor){
43 return descriptor.find();
44}
45
46vector<atom*> World::getAllAtoms(AtomDescriptor descriptor){
47 return descriptor.findAll();
48}
49
50vector<atom*> World::getAllAtoms(){
51 return getAllAtoms(AllAtoms());
52}
53
54int World::numAtoms(){
55 return atoms.size();
56}
57
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
68std::vector<molecule*> World::getAllMolecules(){
69 return getAllMolecules(AllMolecules());
70}
71
72int World::numMolecules(){
73 return molecules_deprecated->ListOfMolecules.size();
74}
75
76// system
77
78Box& World::getDomain() {
79 return *cell_size;
80}
81
82void World::setDomain(const Matrix &mat){
83 *cell_size = mat;
84}
85
86void World::setDomain(double * matrix)
87{
88 Matrix M = ReturnFullMatrixforSymmetric(matrix);
89 cell_size->setM(M);
90}
91
92std::string World::getDefaultName() {
93 return defaultName;
94}
95
96void World::setDefaultName(std::string name)
97{
98 defaultName = name;
99};
100
101class ThermoStatContainer * World::getThermostats()
102{
103 return Thermostats;
104}
105
106
107int World::getExitFlag() {
108 return ExitFlag;
109}
110
111void World::setExitFlag(int flag) {
112 if (ExitFlag < flag)
113 ExitFlag = flag;
114}
115
116/******************** Methods to change World state *********************/
117
118molecule* World::createMolecule(){
119 OBSERVE;
120 molecule *mol = NULL;
121 mol = NewMolecule();
122 ASSERT(!molecules.count(currMoleculeId),"currMoleculeId did not specify an unused ID");
123 mol->setId(currMoleculeId++);
124 // store the molecule by ID
125 molecules[mol->getId()] = mol;
126 mol->signOn(this);
127 return mol;
128}
129
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];
138 ASSERT(mol,"Molecule id that was meant to be destroyed did not exist");
139 DeleteMolecule(mol);
140 molecules.erase(id);
141}
142
143atom *World::createAtom(){
144 OBSERVE;
145 atomId_t id = getNextAtomId();
146 atom *res = NewAtom(id);
147 res->setWorld(this);
148 // store the atom by ID
149 atoms[res->getId()] = res;
150 return res;
151}
152
153
154int World::registerAtom(atom *atom){
155 OBSERVE;
156 atomId_t id = getNextAtomId();
157 atom->setId(id);
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
169void World::destroyAtom(atomId_t id) {
170 OBSERVE;
171 atom *atom = atoms[id];
172 ASSERT(atom,"Atom ID that was meant to be destroyed did not exist");
173 DeleteAtom(atom);
174 atoms.erase(id);
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];
184 ASSERT(target,"Atom with that ID not found");
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 }
197}
198
199ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name,AtomDescriptor descr){
200 return new ManipulateAtomsProcess(op, descr,name,true);
201}
202
203ManipulateAtomsProcess* World::manipulateAtoms(boost::function<void(atom*)> op,std::string name){
204 return manipulateAtoms(op,name,AllAtoms());
205}
206
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}
217/******************************* IDManagement *****************************/
218
219// Atoms
220
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);
230 return id;
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}
243
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}
262
263// Molecules
264
265/******************************* Iterators ********************************/
266
267// Build the AtomIterator from template
268CONSTRUCT_SELECTIVE_ITERATOR(atom*,World::AtomSet,AtomDescriptor);
269
270
271World::AtomIterator World::getAtomIter(AtomDescriptor descr){
272 return AtomIterator(descr,atoms);
273}
274
275World::AtomIterator World::atomEnd(){
276 return AtomIterator(AllAtoms(),atoms,atoms.end());
277}
278
279// build the MoleculeIterator from template
280CONSTRUCT_SELECTIVE_ITERATOR(molecule*,World::MoleculeSet,MoleculeDescriptor);
281
282World::MoleculeIterator World::getMoleculeIter(MoleculeDescriptor descr){
283 return MoleculeIterator(descr,molecules);
284}
285
286World::MoleculeIterator World::moleculeEnd(){
287 return MoleculeIterator(AllMolecules(),molecules,molecules.end());
288}
289
290/******************************* Singleton Stuff **************************/
291
292World::World() :
293 Observable("World"),
294 periode(new periodentafel),
295 configuration(new config),
296 Thermostats(new ThermoStatContainer),
297 ExitFlag(0),
298 atoms(),
299 currAtomId(0),
300 molecules(),
301 currMoleculeId(0),
302 molecules_deprecated(new MoleculeListClass(this))
303{
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);
310 defaultName = "none";
311 molecules_deprecated->signOn(this);
312}
313
314World::~World()
315{
316 molecules_deprecated->signOff(this);
317 delete cell_size;
318 delete molecules_deprecated;
319 delete periode;
320 delete configuration;
321 delete Thermostats;
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);
330 }
331 atoms.clear();
332}
333
334// Explicit instantiation of the singleton mechanism at this point
335
336CONSTRUCT_SINGLETON(World)
337
338/******************************* deprecated Legacy Stuff ***********************/
339
340MoleculeListClass *&World::getMolecules() {
341 return molecules_deprecated;
342}
Note: See TracBrowser for help on using the repository browser.