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