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