1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * ValueStorage.cpp
|
---|
10 | *
|
---|
11 | * Created on: Jul 22, 2010
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "CodePatterns/Singleton_impl.hpp"
|
---|
23 |
|
---|
24 | #include "Actions/OptionTrait.hpp"
|
---|
25 | #include "Actions/OptionRegistry.hpp"
|
---|
26 | #include "Actions/Values.hpp"
|
---|
27 | #include "Actions/ValueStorage.hpp"
|
---|
28 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
29 | #include "Descriptors/MoleculeIdDescriptor.hpp"
|
---|
30 | #include "CodePatterns/Assert.hpp"
|
---|
31 | #include "CodePatterns/Log.hpp"
|
---|
32 | #include "CodePatterns/Verbose.hpp"
|
---|
33 | #include "LinearAlgebra/BoxVector.hpp"
|
---|
34 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
35 | #include "LinearAlgebra/Vector.hpp"
|
---|
36 | #include "RandomNumbers/RandomNumberDistribution_Parameters.hpp"
|
---|
37 | #include "atom.hpp"
|
---|
38 | #include "Box.hpp"
|
---|
39 | #include "element.hpp"
|
---|
40 | #include "molecule.hpp"
|
---|
41 | #include "periodentafel.hpp"
|
---|
42 | #include "World.hpp"
|
---|
43 |
|
---|
44 | ValueStorage::ValueStorage() :
|
---|
45 | OptionRegistry_instance(OptionRegistry::getInstance())
|
---|
46 | {};
|
---|
47 |
|
---|
48 | ValueStorage::~ValueStorage() {};
|
---|
49 |
|
---|
50 |
|
---|
51 | bool ValueStorage::isCurrentValuePresent(const char *name) const
|
---|
52 | {
|
---|
53 | return (CurrentValueMap.find(name) != CurrentValueMap.end());
|
---|
54 | }
|
---|
55 |
|
---|
56 | void ValueStorage::queryCurrentValue(const char * name, const atom * &_T)
|
---|
57 | {
|
---|
58 | int atomID = -1;
|
---|
59 | if (typeid( atom *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
60 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
61 | throw MissingValueException(__FILE__, __LINE__);
|
---|
62 | atomID = lexical_cast<int>(CurrentValueMap[name].c_str());
|
---|
63 | CurrentValueMap.erase(name);
|
---|
64 | } else if (typeid( const atom *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
65 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
66 | throw MissingValueException(__FILE__, __LINE__);
|
---|
67 | atomID = lexical_cast<int>(CurrentValueMap[name].c_str());
|
---|
68 | CurrentValueMap.erase(name);
|
---|
69 | } else
|
---|
70 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
71 | _T = World::getInstance().getAtom(AtomById(atomID));
|
---|
72 | }
|
---|
73 |
|
---|
74 | void ValueStorage::queryCurrentValue(const char * name, const element * &_T) {
|
---|
75 | int Z = -1;
|
---|
76 | if (typeid(const element *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
77 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
78 | throw MissingValueException(__FILE__, __LINE__);
|
---|
79 | Z = lexical_cast<int>(CurrentValueMap[name].c_str());
|
---|
80 | CurrentValueMap.erase(name);
|
---|
81 | } else
|
---|
82 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
83 | _T = World::getInstance().getPeriode()->FindElement(Z);
|
---|
84 | }
|
---|
85 |
|
---|
86 | void ValueStorage::queryCurrentValue(const char * name, const molecule * &_T) {
|
---|
87 | int molID = -1;
|
---|
88 | if (typeid( const molecule *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
89 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
90 | throw MissingValueException(__FILE__, __LINE__);
|
---|
91 | molID = lexical_cast<int>(CurrentValueMap[name].c_str());
|
---|
92 | CurrentValueMap.erase(name);
|
---|
93 | } else
|
---|
94 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
95 | _T = World::getInstance().getMolecule(MoleculeById(molID));
|
---|
96 | }
|
---|
97 |
|
---|
98 | void ValueStorage::queryCurrentValue(const char * name, class Box &_T) {
|
---|
99 | RealSpaceMatrix M;
|
---|
100 | double tmp;
|
---|
101 | if (typeid( Box ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
102 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
103 | throw MissingValueException(__FILE__, __LINE__);
|
---|
104 | std::istringstream stream(CurrentValueMap[name]);
|
---|
105 | stream >> tmp;
|
---|
106 | M.set(0,0,tmp);
|
---|
107 | stream >> tmp;
|
---|
108 | M.set(0,1,tmp);
|
---|
109 | M.set(1,0,tmp);
|
---|
110 | stream >> tmp;
|
---|
111 | M.set(0,2,tmp);
|
---|
112 | M.set(2,0,tmp);
|
---|
113 | stream >> tmp;
|
---|
114 | M.set(1,1,tmp);
|
---|
115 | stream >> tmp;
|
---|
116 | M.set(1,2,tmp);
|
---|
117 | M.set(2,1,tmp);
|
---|
118 | stream >> tmp;
|
---|
119 | M.set(2,2,tmp);
|
---|
120 | _T = M;
|
---|
121 | CurrentValueMap.erase(name);
|
---|
122 | } else
|
---|
123 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
124 | }
|
---|
125 |
|
---|
126 | void ValueStorage::queryCurrentValue(const char * name, class Vector &_T) {
|
---|
127 | if (typeid( Vector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
128 | std::istringstream stream(CurrentValueMap[name]);
|
---|
129 | CurrentValueMap.erase(name);
|
---|
130 | for (size_t i = 0; i < NDIM; ++i) {
|
---|
131 | stream >> _T[i];
|
---|
132 | ASSERT(!stream.fail(),
|
---|
133 | "ValueStorage::queryCurrentValue() - Vector in value map has only "+toString(i)+" components!");
|
---|
134 | }
|
---|
135 | } else
|
---|
136 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
137 | }
|
---|
138 |
|
---|
139 | void ValueStorage::queryCurrentValue(const char * name, class BoxVector &_T) {
|
---|
140 | if (typeid( BoxVector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
141 | std::istringstream stream(CurrentValueMap[name]);
|
---|
142 | CurrentValueMap.erase(name);
|
---|
143 | for (size_t i = 0; i < NDIM; ++i) {
|
---|
144 | stream >> _T[i];
|
---|
145 | ASSERT(!stream.fail(),
|
---|
146 | "ValueStorage::queryCurrentValue() - BoxVector in value map has only "+toString(i)+" components!");
|
---|
147 | }
|
---|
148 | } else
|
---|
149 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
150 | }
|
---|
151 |
|
---|
152 | void ValueStorage::queryCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T) {
|
---|
153 | if (typeid( RandomNumberDistribution_Parameters ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
154 | std::istringstream stream(CurrentValueMap[name]);
|
---|
155 | DoLog(0) && (Log() << Verbose(0) << "ValueStorage::queryCurrentValue() for "+toString(name)+" is "+CurrentValueMap[name]+"." << std::endl);
|
---|
156 | CurrentValueMap.erase(name);
|
---|
157 | stream >> _T;
|
---|
158 | ASSERT(!stream.fail(),
|
---|
159 | "ValueStorage::queryCurrentValue() - RandomNumberDistribution_Parameters in value map has only no components!");
|
---|
160 | } else
|
---|
161 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
162 | }
|
---|
163 |
|
---|
164 | void ValueStorage::queryCurrentValue(const char * name, std::vector<const atom *>&_T)
|
---|
165 | {
|
---|
166 | int atomID = -1;
|
---|
167 | size_t count = 0;
|
---|
168 | atom *Walker = NULL;
|
---|
169 | if (typeid( std::vector<const atom *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
170 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
171 | throw MissingValueException(__FILE__, __LINE__);
|
---|
172 | std::istringstream stream(CurrentValueMap[name]);
|
---|
173 | CurrentValueMap.erase(name);
|
---|
174 | while (!stream.fail()) {
|
---|
175 | stream >> atomID >> ws;
|
---|
176 | ASSERT((!stream.fail()) || (count != 0),
|
---|
177 | "ValueStorage::queryCurrentValue() - Atom is missing id!");
|
---|
178 | if (!stream.fail()) {
|
---|
179 | Walker = World::getInstance().getAtom(AtomById(atomID));
|
---|
180 | if (Walker != NULL)
|
---|
181 | _T.push_back(Walker);
|
---|
182 | atomID = -1;
|
---|
183 | Walker = NULL;
|
---|
184 | ++count;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | } else
|
---|
188 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
189 | }
|
---|
190 |
|
---|
191 | void ValueStorage::queryCurrentValue(const char * name, std::vector<const element *>&_T)
|
---|
192 | {
|
---|
193 | int Z = -1;
|
---|
194 | size_t count = 0;
|
---|
195 | const element *elemental = NULL;
|
---|
196 | if (typeid( std::vector<const element *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
197 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
198 | throw MissingValueException(__FILE__, __LINE__);
|
---|
199 | std::istringstream stream(CurrentValueMap[name]);
|
---|
200 | CurrentValueMap.erase(name);
|
---|
201 | while (!stream.fail()) {
|
---|
202 | stream >> Z >> ws;
|
---|
203 | ASSERT((!stream.fail()) || (count != 0),
|
---|
204 | "ValueStorage::queryCurrentValue() - atomic number is missing!");
|
---|
205 | if (!stream.fail()) {
|
---|
206 | elemental = World::getInstance().getPeriode()->FindElement(Z);
|
---|
207 | if (elemental != NULL)
|
---|
208 | _T.push_back(elemental);
|
---|
209 | Z = -1;
|
---|
210 | ++count;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | } else
|
---|
214 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
215 | }
|
---|
216 |
|
---|
217 | void ValueStorage::queryCurrentValue(const char * name, std::vector<const molecule *>&_T)
|
---|
218 | {
|
---|
219 | int molID = -1;
|
---|
220 | size_t count = 0;
|
---|
221 | molecule *mol = NULL;
|
---|
222 | if (typeid( std::vector<const molecule *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
223 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
224 | throw MissingValueException(__FILE__, __LINE__);
|
---|
225 | std::istringstream stream(CurrentValueMap[name]);
|
---|
226 | CurrentValueMap.erase(name);
|
---|
227 | while (!stream.fail()) {
|
---|
228 | stream >> molID >> ws;
|
---|
229 | ASSERT((!stream.fail()) || (count != 0),
|
---|
230 | "ValueStorage::queryCurrentValue() - molecule is missing id!");
|
---|
231 | if (!stream.fail()) {
|
---|
232 | mol = World::getInstance().getMolecule(MoleculeById(molID));
|
---|
233 | if (mol != NULL)
|
---|
234 | _T.push_back(mol);
|
---|
235 | molID = -1;
|
---|
236 | mol = NULL;
|
---|
237 | ++count;
|
---|
238 | }
|
---|
239 | }
|
---|
240 | } else
|
---|
241 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
242 | }
|
---|
243 |
|
---|
244 | void ValueStorage::queryCurrentValue(const char * name, boost::filesystem::path&_T)
|
---|
245 | {
|
---|
246 | std::string tmp;
|
---|
247 | if (typeid( boost::filesystem::path ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
248 | if (CurrentValueMap.find(name) == CurrentValueMap.end())
|
---|
249 | throw MissingValueException(__FILE__, __LINE__);
|
---|
250 | std::istringstream stream(CurrentValueMap[name]);
|
---|
251 | CurrentValueMap.erase(name);
|
---|
252 | stream >> tmp >> ws;
|
---|
253 | ASSERT(!stream.fail(),
|
---|
254 | "ValueStorage::queryCurrentValue() - boost::filesystem::path is missing!");
|
---|
255 | _T = tmp;
|
---|
256 | } else
|
---|
257 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
258 | }
|
---|
259 |
|
---|
260 | void ValueStorage::setCurrentValue(const char * name, const atom * &_T)
|
---|
261 | {
|
---|
262 | if (typeid( const atom *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
263 | std::ostringstream stream;
|
---|
264 | stream << _T->getId();
|
---|
265 | CurrentValueMap[name] = stream.str();
|
---|
266 | } else
|
---|
267 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
268 | }
|
---|
269 |
|
---|
270 | void ValueStorage::setCurrentValue(const char * name, const element * &_T)
|
---|
271 | {
|
---|
272 | if (typeid( const element *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
273 | std::ostringstream stream;
|
---|
274 | stream << _T->getAtomicNumber();
|
---|
275 | CurrentValueMap[name] = stream.str();
|
---|
276 | } else
|
---|
277 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
278 | }
|
---|
279 |
|
---|
280 | void ValueStorage::setCurrentValue(const char * name, const molecule * &_T)
|
---|
281 | {
|
---|
282 | if (typeid( const molecule *) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
283 | std::ostringstream stream;
|
---|
284 | stream << _T->getId();
|
---|
285 | CurrentValueMap[name] = stream.str();
|
---|
286 | } else
|
---|
287 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
288 | }
|
---|
289 |
|
---|
290 | void ValueStorage::setCurrentValue(const char * name, class Box &_T)
|
---|
291 | {
|
---|
292 | const RealSpaceMatrix &M = _T.getM();
|
---|
293 | if (typeid( Box ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
294 | std::ostringstream stream;
|
---|
295 | stream << M.at(0,0) << " ";
|
---|
296 | stream << M.at(0,1) << " ";
|
---|
297 | stream << M.at(0,2) << " ";
|
---|
298 | stream << M.at(1,1) << " ";
|
---|
299 | stream << M.at(1,2) << " ";
|
---|
300 | stream << M.at(2,2) << " ";
|
---|
301 | CurrentValueMap[name] = stream.str();
|
---|
302 | } else
|
---|
303 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
304 | }
|
---|
305 |
|
---|
306 | void ValueStorage::setCurrentValue(const char * name, class Vector &_T)
|
---|
307 | {
|
---|
308 | if (typeid( Vector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())){
|
---|
309 | std::ostringstream stream;
|
---|
310 | stream << _T[0] << " ";
|
---|
311 | stream << _T[1] << " ";
|
---|
312 | stream << _T[2] << " ";
|
---|
313 | CurrentValueMap[name] = stream.str();
|
---|
314 | } else if (typeid( BoxVector ) == *(OptionRegistry_instance.getOptionByName(name)->getType())){
|
---|
315 | std::ostringstream stream;
|
---|
316 | stream << _T[0] << " ";
|
---|
317 | stream << _T[1] << " ";
|
---|
318 | stream << _T[2] << " ";
|
---|
319 | CurrentValueMap[name] = stream.str();
|
---|
320 | } else
|
---|
321 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
322 | }
|
---|
323 |
|
---|
324 | void ValueStorage::setCurrentValue(const char * name, class RandomNumberDistribution_Parameters &_T)
|
---|
325 | {
|
---|
326 | if (typeid( RandomNumberDistribution_Parameters ) == *(OptionRegistry_instance.getOptionByName(name)->getType())){
|
---|
327 | std::ostringstream stream;
|
---|
328 | stream << _T;
|
---|
329 | CurrentValueMap[name] = stream.str();
|
---|
330 | DoLog(0) && (Log() << Verbose(0) << "ValueStorage::setCurrentValue() for "+toString(name)+" to "+stream.str()+"." << std::endl);
|
---|
331 | } else
|
---|
332 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
333 | }
|
---|
334 |
|
---|
335 | void ValueStorage::setCurrentValue(const char * name, std::vector<const atom *>&_T)
|
---|
336 | {
|
---|
337 | if (typeid( std::vector<const atom *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
338 | std::ostringstream stream;
|
---|
339 | for (std::vector<const atom *>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) {
|
---|
340 | stream << (*iter)->getId() << " ";
|
---|
341 | }
|
---|
342 | CurrentValueMap[name] = stream.str();
|
---|
343 | } else
|
---|
344 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
345 | }
|
---|
346 |
|
---|
347 | void ValueStorage::setCurrentValue(const char * name, std::vector<const element *>&_T)
|
---|
348 | {
|
---|
349 | if (typeid( std::vector<const element *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
350 | std::ostringstream stream;
|
---|
351 | for (std::vector<const element *>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) {
|
---|
352 | stream << (*iter)->getAtomicNumber() << " ";
|
---|
353 | }
|
---|
354 | CurrentValueMap[name] = stream.str();
|
---|
355 | } else
|
---|
356 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
357 | }
|
---|
358 |
|
---|
359 | void ValueStorage::setCurrentValue(const char * name, std::vector<const molecule *>&_T)
|
---|
360 | {
|
---|
361 | if (typeid( std::vector<const molecule *> ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
362 | std::ostringstream stream;
|
---|
363 | for (std::vector<const molecule *>::const_iterator iter = _T.begin(); iter != _T.end(); ++iter) {
|
---|
364 | stream << (*iter)->getId() << " ";
|
---|
365 | }
|
---|
366 | CurrentValueMap[name] = stream.str();
|
---|
367 | } else
|
---|
368 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
369 | }
|
---|
370 |
|
---|
371 | void ValueStorage::setCurrentValue(const char * name, boost::filesystem::path &_T)
|
---|
372 | {
|
---|
373 | if (typeid( boost::filesystem::path ) == *(OptionRegistry_instance.getOptionByName(name)->getType())) {
|
---|
374 | std::ostringstream stream;
|
---|
375 | stream << _T.string();
|
---|
376 | CurrentValueMap[name] = stream.str();
|
---|
377 | } else
|
---|
378 | throw IllegalTypeException(__FILE__,__LINE__);
|
---|
379 | }
|
---|
380 |
|
---|
381 | const std::string ValueStorage::getCurrentValue(std::string actionname) {
|
---|
382 | return CurrentValueMap[actionname];
|
---|
383 | }
|
---|
384 |
|
---|
385 | CONSTRUCT_SINGLETON(ValueStorage)
|
---|