source: src/Formula.cpp@ 668e28

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 668e28 was 4d1d43, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Improved formula parsing to include more complex expressions

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