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