source: src/World.cpp@ 40f928

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 Candidate_v1.7.0 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 40f928 was fa0b18, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added Observed and Selectiv iterators over internal Structures of the World.

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