1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Formula.cpp
|
---|
10 | *
|
---|
11 | * Created on: Jul 21, 2010
|
---|
12 | * Author: crueger
|
---|
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 "Formula.hpp"
|
---|
23 |
|
---|
24 | #include <sstream>
|
---|
25 |
|
---|
26 | #include "World.hpp"
|
---|
27 | #include "Element/periodentafel.hpp"
|
---|
28 | #include "Element/element.hpp"
|
---|
29 | #include "CodePatterns/Assert.hpp"
|
---|
30 | #include "CodePatterns/Range.hpp"
|
---|
31 |
|
---|
32 | using namespace std;
|
---|
33 |
|
---|
34 | Formula::Formula() :
|
---|
35 | numElements(0)
|
---|
36 | {}
|
---|
37 |
|
---|
38 | Formula::Formula(const Formula &src) :
|
---|
39 | elementCounts(src.elementCounts),
|
---|
40 | numElements(src.numElements)
|
---|
41 | {}
|
---|
42 |
|
---|
43 | Formula::Formula(const string &formula) :
|
---|
44 | numElements(0)
|
---|
45 | {
|
---|
46 | fromString(formula);
|
---|
47 | }
|
---|
48 |
|
---|
49 | Formula::~Formula()
|
---|
50 | {}
|
---|
51 |
|
---|
52 | Formula &Formula::operator=(const Formula &rhs){
|
---|
53 | // No self-assignment check needed
|
---|
54 | elementCounts=rhs.elementCounts;
|
---|
55 | numElements=rhs.numElements;
|
---|
56 | return *this;
|
---|
57 | }
|
---|
58 |
|
---|
59 | std::string Formula::toString() const{
|
---|
60 | stringstream sstr;
|
---|
61 | for(const_iterator iter=end();iter!=begin();){
|
---|
62 | --iter;
|
---|
63 | sstr << (*iter).first->getSymbol();
|
---|
64 | if((*iter).second>1)
|
---|
65 | sstr << (*iter).second;
|
---|
66 | }
|
---|
67 | return sstr.str();
|
---|
68 | }
|
---|
69 |
|
---|
70 | void Formula::fromString(const std::string &formula) throw(FormulaStringParseException){
|
---|
71 | // make this transactional, in case an error is thrown
|
---|
72 | Formula res;
|
---|
73 | string::const_iterator begin = formula.begin();
|
---|
74 | string::const_iterator end = formula.end();
|
---|
75 | res.parseFromString(begin,end,static_cast<char>(0));
|
---|
76 | (*this)=res;
|
---|
77 | }
|
---|
78 |
|
---|
79 | int Formula::parseMaybeNumber(std::string::const_iterator &it,string::const_iterator &end) throw(FormulaStringParseException){
|
---|
80 | static const range<char> Numbers = makeRange('0',static_cast<char>('9'+1));
|
---|
81 | int count = 0;
|
---|
82 | while(it!=end && Numbers.isInRange(*it))
|
---|
83 | count = (count*10) + ((*it++)-Numbers.first);
|
---|
84 | // one is implicit
|
---|
85 | count = (count!=0)?count:1;
|
---|
86 | return count;
|
---|
87 | }
|
---|
88 |
|
---|
89 | void Formula::parseFromString(std::string::const_iterator &it,string::const_iterator &end,char delimiter) throw(FormulaStringParseException){
|
---|
90 | // some constants needed for parsing... Assumes ASCII, change if other encodings are used
|
---|
91 | static const range<char> CapitalLetters = makeRange('A',static_cast<char>('Z'+1));
|
---|
92 | static const range<char> SmallLetters = makeRange('a',static_cast<char>('z'+1));
|
---|
93 | map<char,char> delimiters;
|
---|
94 | delimiters['('] = ')';
|
---|
95 | delimiters['['] = ']';
|
---|
96 | // clean the formula
|
---|
97 | clear();
|
---|
98 | for(/*send from above*/;it!=end && *it!=delimiter;/*updated in loop*/){
|
---|
99 | // we might have a sub formula
|
---|
100 | if(delimiters.count(*it)){
|
---|
101 | Formula sub;
|
---|
102 | char nextdelim=delimiters[*it];
|
---|
103 | sub.parseFromString(++it,end,nextdelim);
|
---|
104 | if(!sub.getElementCount()){
|
---|
105 | throw FormulaStringParseException() << FormulaString( string(it, end) );
|
---|
106 | }
|
---|
107 | int count = parseMaybeNumber(++it,end);
|
---|
108 | addFormula(sub,count);
|
---|
109 | continue;
|
---|
110 | }
|
---|
111 | string shorthand;
|
---|
112 | // Atom names start with a capital letter
|
---|
113 | if(!CapitalLetters.isInRange(*it))
|
---|
114 | throw FormulaStringParseException() << FormulaString( string(it, end) );
|
---|
115 | shorthand+=(*it++);
|
---|
116 | // the rest of the name follows
|
---|
117 | while(it!=end && SmallLetters.isInRange(*it))
|
---|
118 | shorthand+=(*it++);
|
---|
119 | int count = parseMaybeNumber(it,end);
|
---|
120 | // test if the shorthand exists
|
---|
121 | if(!World::getInstance().getPeriode()->FindElement(shorthand))
|
---|
122 | throw FormulaStringParseException() << FormulaString( string(it, end) );
|
---|
123 | // done, we can get the next one
|
---|
124 | addElements(shorthand,count);
|
---|
125 | }
|
---|
126 | if(it==end && delimiter!=0){
|
---|
127 | throw FormulaStringParseException() << FormulaString( string(it, end) );
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | bool Formula::checkOut(ostream *output) const{
|
---|
132 | bool result = true;
|
---|
133 | int No = 1;
|
---|
134 |
|
---|
135 | if (output != NULL) {
|
---|
136 | *output << "# Ion type data (PP = PseudoPotential, Z = atomic number)" << endl;
|
---|
137 | *output << "#Ion_TypeNr.\tAmount\tZ\tRGauss\tL_Max(PP)L_Loc(PP)IonMass\t# chemical name, symbol" << endl;
|
---|
138 | for(const_iterator iter=begin(); iter!=end();++iter){
|
---|
139 | result = result && (*iter).first->Checkout(output, No++, (*iter).second);
|
---|
140 | }
|
---|
141 | return result;
|
---|
142 | } else
|
---|
143 | return false;
|
---|
144 | }
|
---|
145 |
|
---|
146 | unsigned int Formula::getElementCount() const{
|
---|
147 | return numElements;
|
---|
148 | }
|
---|
149 |
|
---|
150 | bool Formula::hasElement(const element *element) const{
|
---|
151 | ASSERT(element,"Invalid pointer in Formula::hasElement(element*)");
|
---|
152 | return hasElement(element->getNumber());
|
---|
153 | }
|
---|
154 |
|
---|
155 | bool Formula::hasElement(atomicNumber_t Z) const{
|
---|
156 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
157 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
158 | return elementCounts.size()>=Z && elementCounts[Z-1];
|
---|
159 | }
|
---|
160 |
|
---|
161 | bool Formula::hasElement(const string &shorthand) const{
|
---|
162 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
163 | return hasElement(element);
|
---|
164 | }
|
---|
165 |
|
---|
166 | void Formula::operator+=(const element *element){
|
---|
167 | ASSERT(element,"Invalid pointer in increment of Formula");
|
---|
168 | operator+=(element->getNumber());
|
---|
169 | }
|
---|
170 |
|
---|
171 | void Formula::operator+=(atomicNumber_t Z){
|
---|
172 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
173 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
174 | elementCounts.resize(max<atomicNumber_t>(Z,elementCounts.size()),0); // No-op when we already have the right size
|
---|
175 | // might need to update number of elements
|
---|
176 | if(!elementCounts[Z-1]){
|
---|
177 | numElements++;
|
---|
178 | }
|
---|
179 | elementCounts[Z-1]++; // atomic numbers start at 1
|
---|
180 | }
|
---|
181 |
|
---|
182 | void Formula::operator+=(const string &shorthand){
|
---|
183 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
184 | operator+=(element);
|
---|
185 | }
|
---|
186 |
|
---|
187 | void Formula::operator-=(const element *element){
|
---|
188 | ASSERT(element,"Invalid pointer in decrement of Formula");
|
---|
189 | operator-=(element->getNumber());
|
---|
190 | }
|
---|
191 |
|
---|
192 | void Formula::operator-=(atomicNumber_t Z){
|
---|
193 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
194 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
195 | ASSERT(elementCounts.size()>=Z && elementCounts[Z-1], "Element not in Formula upon decrement");
|
---|
196 | elementCounts[Z-1]--; // atomic numbers start at 1
|
---|
197 | // might need to update number of elements
|
---|
198 | if(!elementCounts[Z-1]){
|
---|
199 | numElements--;
|
---|
200 | // resize the Array if this was at the last position
|
---|
201 | if(Z==elementCounts.size()){
|
---|
202 | // find the first element from the back that is not equal to zero
|
---|
203 | set_t::reverse_iterator riter = find_if(elementCounts.rbegin(),
|
---|
204 | elementCounts.rend(),
|
---|
205 | bind1st(not_equal_to<mapped_type>(),0));
|
---|
206 | // see how many elements are in this range
|
---|
207 | set_t::reverse_iterator::difference_type diff = riter - elementCounts.rbegin();
|
---|
208 | elementCounts.resize(elementCounts.size()-diff);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | void Formula::operator-=(const string &shorthand){
|
---|
214 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
215 | operator-=(element);
|
---|
216 | }
|
---|
217 |
|
---|
218 | void Formula::addElements(const element *element,unsigned int count){
|
---|
219 | ASSERT(element,"Invalid pointer in Formula::addElements(element*)");
|
---|
220 | addElements(element->getNumber(),count);
|
---|
221 | }
|
---|
222 |
|
---|
223 | void Formula::addElements(atomicNumber_t Z,unsigned int count){
|
---|
224 | if(count==0) return;
|
---|
225 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
226 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
227 | elementCounts.resize(max<atomicNumber_t>(Z,elementCounts.size()),0); // No-op when we already have the right size
|
---|
228 | // might need to update number of elements
|
---|
229 | if(!elementCounts[Z-1]){
|
---|
230 | numElements++;
|
---|
231 | }
|
---|
232 | elementCounts[Z-1]+=count;
|
---|
233 | }
|
---|
234 |
|
---|
235 | void Formula::addElements(const string &shorthand,unsigned int count){
|
---|
236 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
237 | addElements(element,count);
|
---|
238 | }
|
---|
239 |
|
---|
240 | void Formula::addFormula(const Formula &formula,unsigned int n){
|
---|
241 | for(Formula::const_iterator iter=formula.begin();iter!=formula.end();++iter){
|
---|
242 | this->addElements(iter->first,iter->second*n);
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | enumeration<Formula::key_type> Formula::enumerateElements() const{
|
---|
247 | enumeration<key_type> res(1);
|
---|
248 | for(Formula::const_iterator iter=begin();iter!=end();++iter){
|
---|
249 | res.add(iter->first);
|
---|
250 | }
|
---|
251 | return res;
|
---|
252 | }
|
---|
253 |
|
---|
254 | const unsigned int Formula::operator[](const element *element) const{
|
---|
255 | ASSERT(element,"Invalid pointer in access of Formula");
|
---|
256 | return operator[](element->getNumber());
|
---|
257 | }
|
---|
258 |
|
---|
259 | const unsigned int Formula::operator[](atomicNumber_t Z) const{
|
---|
260 | ASSERT(Z>0,"Invalid atomic Number");
|
---|
261 | ASSERT(World::getInstance().getPeriode()->FindElement(Z),"No Element with this number in Periodentafel");
|
---|
262 | if(elementCounts.size()<Z)
|
---|
263 | return 0;
|
---|
264 | return elementCounts[Z-1]; // atomic numbers start at 1
|
---|
265 | }
|
---|
266 |
|
---|
267 | const unsigned int Formula::operator[](std::string shorthand) const{
|
---|
268 | const element * element = World::getInstance().getPeriode()->FindElement(shorthand);
|
---|
269 | return operator[](element);
|
---|
270 | }
|
---|
271 |
|
---|
272 | bool Formula::operator==(const Formula &rhs) const{
|
---|
273 | // quick check... number of elements used
|
---|
274 | if(numElements != rhs.numElements){
|
---|
275 | return false;
|
---|
276 | }
|
---|
277 | // second quick check, size of vectors (== last element in formula)
|
---|
278 | if(elementCounts.size()!=rhs.elementCounts.size()){
|
---|
279 | return false;
|
---|
280 | }
|
---|
281 | // slow check: all elements
|
---|
282 | // direct access to internal structure means all element-counts have to be compared
|
---|
283 | // this avoids access to periodentafel to find elements though and is probably faster
|
---|
284 | // in total
|
---|
285 | return equal(elementCounts.begin(),
|
---|
286 | elementCounts.end(),
|
---|
287 | rhs.elementCounts.begin());
|
---|
288 | }
|
---|
289 |
|
---|
290 | bool Formula::operator!=(const Formula &rhs) const{
|
---|
291 | return !operator==(rhs);
|
---|
292 | }
|
---|
293 |
|
---|
294 | Formula::iterator Formula::begin(){
|
---|
295 | return iterator(elementCounts,0);
|
---|
296 | }
|
---|
297 | Formula::const_iterator Formula::begin() const{
|
---|
298 | // this is the only place where this is needed, so this is better than making it mutable
|
---|
299 | return const_iterator(const_cast<set_t&>(elementCounts),0);
|
---|
300 | }
|
---|
301 | Formula::iterator Formula::end(){
|
---|
302 | return iterator(elementCounts);
|
---|
303 | }
|
---|
304 | Formula::const_iterator Formula::end() const{
|
---|
305 | // this is the only place where this is needed, so this is better than making it mutable
|
---|
306 | return const_iterator(const_cast<set_t&>(elementCounts));
|
---|
307 | }
|
---|
308 |
|
---|
309 | void Formula::clear(){
|
---|
310 | elementCounts.clear();
|
---|
311 | numElements = 0;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**************** Iterator structure ********************/
|
---|
315 |
|
---|
316 | template <class result_type>
|
---|
317 | Formula::_iterator<result_type>::_iterator(set_t &_set) :
|
---|
318 | set(&_set)
|
---|
319 | {
|
---|
320 | pos=set->size();
|
---|
321 | }
|
---|
322 |
|
---|
323 | template <class result_type>
|
---|
324 | Formula::_iterator<result_type>::_iterator(set_t &_set,size_t _pos) :
|
---|
325 | set(&_set),pos(_pos)
|
---|
326 | {
|
---|
327 | ASSERT(pos<=set->size(),"invalid position in iterator construction");
|
---|
328 | while(pos<set->size() && (*set)[pos]==0) ++pos;
|
---|
329 | }
|
---|
330 |
|
---|
331 | template <class result_type>
|
---|
332 | Formula::_iterator<result_type>::_iterator(const _iterator &rhs) :
|
---|
333 | set(rhs.set),pos(rhs.pos)
|
---|
334 | {}
|
---|
335 |
|
---|
336 | template <class result_type>
|
---|
337 | Formula::_iterator<result_type>::~_iterator(){}
|
---|
338 |
|
---|
339 | template <class result_type>
|
---|
340 | Formula::_iterator<result_type>&
|
---|
341 | Formula::_iterator<result_type>::operator=(const _iterator<result_type> &rhs){
|
---|
342 | set=rhs.set;
|
---|
343 | pos=rhs.pos;
|
---|
344 | return *this;
|
---|
345 | }
|
---|
346 |
|
---|
347 | template <class result_type>
|
---|
348 | bool
|
---|
349 | Formula::_iterator<result_type>::operator==(const _iterator<result_type> &rhs){
|
---|
350 | return set==rhs.set && pos==rhs.pos;
|
---|
351 | }
|
---|
352 |
|
---|
353 | template <class result_type>
|
---|
354 | bool
|
---|
355 | Formula::_iterator<result_type>::operator!=(const _iterator<result_type> &rhs){
|
---|
356 | return !operator==(rhs);
|
---|
357 | }
|
---|
358 |
|
---|
359 | template <class result_type>
|
---|
360 | Formula::_iterator<result_type>
|
---|
361 | Formula::_iterator<result_type>::operator++(){
|
---|
362 | ASSERT(pos!=set->size(),"Incrementing Formula::iterator beyond end");
|
---|
363 | pos++;
|
---|
364 | while(pos<set->size() && (*set)[pos]==0) ++pos;
|
---|
365 | return *this;
|
---|
366 | }
|
---|
367 |
|
---|
368 | template <class result_type>
|
---|
369 | Formula::_iterator<result_type>
|
---|
370 | Formula::_iterator<result_type>::operator++(int){
|
---|
371 | Formula::_iterator<result_type> retval = *this;
|
---|
372 | ++(*this);
|
---|
373 | return retval;
|
---|
374 | }
|
---|
375 |
|
---|
376 | template <class result_type>
|
---|
377 | Formula::_iterator<result_type>
|
---|
378 | Formula::_iterator<result_type>::operator--(){
|
---|
379 | ASSERT(pos!=0,"Decrementing Formula::iterator beyond begin");
|
---|
380 | pos--;
|
---|
381 | while(pos>0 && (*set)[pos]==0) --pos;
|
---|
382 | return *this;
|
---|
383 | }
|
---|
384 |
|
---|
385 | template <class result_type>
|
---|
386 | Formula::_iterator<result_type>
|
---|
387 | Formula::_iterator<result_type>::operator--(int){
|
---|
388 | Formula::_iterator<result_type> retval = *this;
|
---|
389 | --(*this);
|
---|
390 | return retval;
|
---|
391 | }
|
---|
392 |
|
---|
393 | template <class result_type>
|
---|
394 | result_type
|
---|
395 | Formula::_iterator<result_type>::operator*(){
|
---|
396 | const element *element = World::getInstance().getPeriode()->FindElement(pos+1);
|
---|
397 | ASSERT(element,"Element with position of iterator not found");
|
---|
398 | return make_pair(element,(*set)[pos]);
|
---|
399 | }
|
---|
400 |
|
---|
401 | template <class result_type>
|
---|
402 | result_type*
|
---|
403 | Formula::_iterator<result_type>::operator->(){
|
---|
404 | // no one can keep this value around, so a static is ok to avoid temporaries
|
---|
405 | static value_type value=make_pair(reinterpret_cast<element*>(0),0); // no default constructor for std::pair
|
---|
406 | const element *element = World::getInstance().getPeriode()->FindElement(pos+1);
|
---|
407 | ASSERT(element,"Element with position of iterator not found");
|
---|
408 | value = make_pair(element,(*set)[pos]);
|
---|
409 | return &value;
|
---|
410 | }
|
---|
411 |
|
---|
412 | // explicit instantiation of all iterator template methods
|
---|
413 | // this is quite ugly, but there is no better way unless we expose iterator implementation
|
---|
414 |
|
---|
415 | // instantiate Formula::iterator
|
---|
416 | template Formula::iterator::_iterator(set_t&);
|
---|
417 | template Formula::iterator::_iterator(set_t&,size_t);
|
---|
418 | template Formula::iterator::_iterator(const Formula::iterator&);
|
---|
419 | template Formula::iterator::~_iterator();
|
---|
420 | template Formula::iterator &Formula::iterator::operator=(const Formula::iterator&);
|
---|
421 | template bool Formula::iterator::operator==(const Formula::iterator&);
|
---|
422 | template bool Formula::iterator::operator!=(const Formula::iterator&);
|
---|
423 | template Formula::iterator Formula::iterator::operator++();
|
---|
424 | template Formula::iterator Formula::iterator::operator++(int);
|
---|
425 | template Formula::iterator Formula::iterator::operator--();
|
---|
426 | template Formula::iterator Formula::iterator::operator--(int);
|
---|
427 | template Formula::value_type Formula::iterator::operator*();
|
---|
428 | template Formula::value_type *Formula::iterator::operator->();
|
---|
429 |
|
---|
430 | // instantiate Formula::const_iterator
|
---|
431 | template Formula::const_iterator::_iterator(set_t&);
|
---|
432 | template Formula::const_iterator::_iterator(set_t&,size_t);
|
---|
433 | template Formula::const_iterator::_iterator(const Formula::const_iterator&);
|
---|
434 | template Formula::const_iterator::~_iterator();
|
---|
435 | template Formula::const_iterator &Formula::const_iterator::operator=(const Formula::const_iterator&);
|
---|
436 | template bool Formula::const_iterator::operator==(const Formula::const_iterator&);
|
---|
437 | template bool Formula::const_iterator::operator!=(const Formula::const_iterator&);
|
---|
438 | template Formula::const_iterator Formula::const_iterator::operator++();
|
---|
439 | template Formula::Formula::const_iterator Formula::const_iterator::operator++(int);
|
---|
440 | template Formula::Formula::const_iterator Formula::const_iterator::operator--();
|
---|
441 | template Formula::Formula::const_iterator Formula::const_iterator::operator--(int);
|
---|
442 | template const Formula::value_type Formula::const_iterator::operator*();
|
---|
443 | template const Formula::value_type *Formula::const_iterator::operator->();
|
---|
444 |
|
---|
445 | /********************** I/O of Formulas ************************************************/
|
---|
446 |
|
---|
447 | std::ostream &operator<<(std::ostream &ost,const Formula &formula){
|
---|
448 | ost << formula.toString();
|
---|
449 | return ost;
|
---|
450 | }
|
---|