antioch-0.4.0
List of all members | Public Member Functions | Private Member Functions | Private Attributes | Friends
Antioch::Units< T > Class Template Reference

An advanced unit class. More...

#include <units.h>

Public Member Functions

 Units (const std::string &sym, const std::string &na, const T &conva, const T &convb, int mi, int kgi=0, int si=0, int Ai=0, int Ki=0, int moli=0, int cdi=0, int radi=0)
 Fully descriptive constructor, mainly for internal purposes. More...
 
 Units (const std::string &sym, const std::string &na="")
 User-friendly constructor, from a symbol and an optionnal name. More...
 
 Units (const std::string &sym, const Converter< T > &conv, const std::string &na="")
 Constructor for a user imposed conversion. More...
 
 Units ()
 Default constructor. More...
 
 ~Units ()
 Default destructor. More...
 
void print (std::ostream &os=std::cout) const
 Formatted print. More...
 
bool operator== (const Units< T > &rhs) const
 Comparison operator. More...
 
bool operator!= (const Units< T > &rhs) const
 Comparison operator, not equal is not "equal". More...
 
Units< T > & operator= (const Units< T > &rhs)
 Assignement operator. More...
 
Units< T > & operator+= (const Units< T > &rhs)
 Adding operator. More...
 
Units< T > & operator-= (const Units< T > &rhs)
 Substracting operator. More...
 
Units< T > operator+ (const Units< T > &rhs) const
 Adding operator, uses Units &operator+=(const Units&) More...
 
Units< T > operator- (const Units< T > &rhs) const
 Substracting operator, uses Units &operator-=(const Units&) More...
 
Units< T > & operator*= (int r)
 Multiplying operator by an integer. More...
 
Units< T > operator* (int r) const
 Multiplying operator by an integer, uses Units &operator*=(int) More...
 
Units< T > & operator/= (int r)
 dividing operator by an integer More...
 
Units< T > operator/ (int r) const
 Dividing operator by an integer, uses Units &operator/=(int) More...
 
void equalize (const Units< T > &rhs)
 Alternative call to Units& operator=(const Units &) More...
 
void equalize (Units< T > *rhs)
 Alternative call to Units& operator=(const Units &) More...
 
void equalize (std::string unit)
 Alternative call to Units& operator=(const Units &) More...
 
void add (const Units< T > &rhs)
 Alternative call to Units& operator+=(const Units &) More...
 
void add (Units< T > *rhs)
 Alternative call to Units& operator+=(const Units &) More...
 
void add (std::string unit)
 Alternative call to Units& operator+=(const Units &) More...
 
void substract (const Units< T > &rhs)
 Alternative call to Units& operator-=(const Units &) More...
 
void substract (Units< T > *rhs)
 Alternative call to Units& operator-=(const Units &) More...
 
void substract (const std::string &unit)
 Alternative call to Units& operator-=(const Units &) More...
 
void root (int r)
 Alternative method to Units& operator/=(int) More...
 
void set_unit (const std::string &sym, std::string na)
 Units setter, sets the unit to the given symbol and name. More...
 
void set_unit (const std::string &sym)
 Units setter, sets the unit to the given symbol and name. More...
 
void set_name (const std::string &na)
 Name setter. More...
 
void set_symbol (const std::string &symb)
 Symbol setter. More...
 
void set_SI_converter (const Converter< T > &conv)
 Coefficient setter. More...
 
void set_power_to_SI (const InSI &mat)
 Power std::vector setter. More...
 
bool is_homogeneous (const Units< T > &rhs) const
 Homogenity testing with another Units. More...
 
bool is_homogeneous (std::string target) const
 Homogenity testing with a std::string. More...
 
bool is_united () const
 Test if the unit is non empty. More...
 
factor_to_some_unit (const Units< T > &target) const
 Calculates the factor to any given unit. More...
 
factor_to_some_unit (const std::string &target) const
 Calculates the factor to any given unit, uses factor_to_some_unit(const Units&). More...
 
translator_to_some_unit (const Units< T > &target) const
 Calculates the translator to any given unit, see factor_to_some_unit(const Units&) for the equations. More...
 
translator_to_some_unit (const std::string &target) const
 Calculates the translator to any given unit, uses translator_to_some_unit(const Units&). More...
 
const std::string get_name () const
 String name getter. More...
 
const std::string get_symbol () const
 String symbol getter. More...
 
const Converter< T > get_SI_coef () const
 Coefficient getter. More...
 
get_SI_factor () const
 Multiplicative coefficient getter. More...
 
get_SI_translator () const
 Translationnal coefficient getter. More...
 
const InSI get_power () const
 Power std::vector getter. More...
 
const std::string get_SI_symb () const
 Corresponding SI symbol getter. More...
 
int get_SI_power (const std::string &SIask) const
 Power of the asked SI unit. More...
 
const std::string get_SI_convenient_symb () const
 Corresponding SI symbol getter. More...
 
const std::string harmonized_symbol (const std::string &input="") const
 Simplify the symbol when possible. More...
 
const std::string contracted_symbol (const std::string &input="") const
 Contract the symbol when possible. More...
 
void clear ()
 Clear the unit. More...
 
void showAll (std::ostream &out=std::cout)
 showAll(). More...
 

Private Member Functions

const std::string manipulate_symbol (std::string input, bool contract) const
 Root method for contracting or harmonizing the symbol. More...
 
void fill_in_power (bool doConv)
 This method fills the power vector and will calculate the coefficient if the bool doConv is set to true. More...
 
bool parse_single_unit (int signe, std::string unit, bool doConv)
 Calculates the corresponding coefficient and power of the given unit. More...
 
void parse_prefix_unit (int &iUnit, int &iPre, const std::string &unit) const
 Unit parser, parse a std::string of type Prefixes[] knownUnits[]. More...
 
int indexUnit (std::string unit) const
 Scanner of knownUnits[], sends back the corresponding iUnit, -1 if not found. More...
 
int parse_power (std::string unit, int &nc) const
 brief parser for std::string of type "some_unit power_associated". More...
 
bool is_number (char c) const
 Small method to check if a character is a numerical. More...
 
Converter< T > raise (const Converter< T > &tbm, int power) const
 Raise to an integer power a Converter object. More...
 
Converter< T > raise (const Converter< T > &tbm, double power) const
 Overload to simplify division. More...
 
void develop_symbol (std::string &subsymbol)
 Supress the parenthesises. More...
 
void reverse_power_symbol (std::string &subsymbol)
 Treat part of symbol depending on parenthesises. More...
 
bool is_in_symb (char c) const
 Small method that checks if the symbol is within a unit std::string. More...
 
int n_dimension_of_units () const
 Number of dimensions of the unit in the SI basis. More...
 
const std::string add_SI (int pow, std::string SIsymb) const
 Small method to add the SI symbol and power to a std::string. More...
 
void symbol_to_the_power (int r, const int &key)
 Rewrite the symbol to put it at a given power. More...
 
int get_integer_power (int unit, int r, const int &key)
 Multiply or divide a power. More...
 
void check_if_name (std::string &nameOut, const Units< T > &un) const
 Fill the name if empty and name obvious. More...
 

Private Attributes

std::string symbol
 Strings for the symbol. More...
 
std::string name
 Strings for the name. More...
 
Converter< T > toSI
 A Converter for the coefficient. More...
 
InSI power
 An InSI for the power vector. More...
 

Friends

std::ostream & operator<< (std::ostream &os, const Units< T > &unit)
 Formatted print. More...
 

Detailed Description

template<typename T = double>
class Antioch::Units< T >

An advanced unit class.

This class contains:

The idea of this class is to keep track of the unit without performing any operation on the values. To characterize an unit, the unit is projected against the SI basis of units, with the addition of angle unit:

(meter,kilogram,second,ampere,kelvin,mol,candela,radian)

Thus any unit can be expressed as a linear composition of this std::vector. This is the role of the class InSI to store this linear composition. This linear composition is called the power vector.

With the linear composition, comes a coefficient that correspond to the conversion of the considered unit to the SI system. The class Converter takes care of storing it.

Thus this class performs a parsing of the symbol of the given unit, compares each unit of the symbol to a set of known units, and construct the linear composition and coefficient by successive additions of the units of the symbol. For instance, the unit m.A/s is seen as $\mathrm{m} + \mathrm{A} - \mathrm{s}$, and thus the composition and coefficient are calculated correspondingly:

At each unit parsing level, the unit is parsed between a prefix, a symbol and a power. The symbol "cm-3" will be parsed as a prefix "c", a symbol "m" and a power "-3". To this parsing, the symbol before the unit is considered. If there is none or it is a dot, nothing to do, but if this is a slash, the power has to be multiplied by -1. Thus the Converter object will have the corresponding SIPrefixes values (Prefixe[6] is this case, which is ("c",1e-2)), and the InSI power vector will add the value "-3" to the corresponding power (0 in this case) if the character before the unit is a dot or does not exist, the value 3 if this character is a slash. The correspondancies between the index in the power vector and the unit is done explicitely.

A special treatment is reserved for the kg unit, as it contains a prefix.

This method supports a wide variety of symbol writing, like the use of parenthesises. The used symbol is a fully developped one.

Definition at line 111 of file units.h.

Constructor & Destructor Documentation

template<typename T >
Antioch::Units< T >::Units ( const std::string &  sym,
const std::string &  na,
const T &  conva,
const T &  convb,
int  mi,
int  kgi = 0,
int  si = 0,
int  Ai = 0,
int  Ki = 0,
int  moli = 0,
int  cdi = 0,
int  radi = 0 
)
inline

Fully descriptive constructor, mainly for internal purposes.

Definition at line 592 of file units.h.

References Antioch::Units< T >::develop_symbol(), and Antioch::Units< T >::symbol.

594  : //fully descriptive constructor
595  symbol(sym),
596  name(na),
597  toSI(conva,convb),
598  power(mi,kgi,si,Ai,Ki,moli,cdi,radi)
599 {
601 }
void develop_symbol(std::string &subsymbol)
Supress the parenthesises.
Definition: units.h:669
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
template<typename T >
Antioch::Units< T >::Units ( const std::string &  sym,
const std::string &  na = "" 
)
inline

User-friendly constructor, from a symbol and an optionnal name.

Definition at line 605 of file units.h.

References Antioch::Units< T >::fill_in_power().

605  : //constructors when automatic conversion
606  symbol(sym),
607  name(na)
608 {
609  fill_in_power(true);
610 }
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
void fill_in_power(bool doConv)
This method fills the power vector and will calculate the coefficient if the bool doConv is set to tr...
Definition: units.h:623
template<typename T >
Antioch::Units< T >::Units ( const std::string &  sym,
const Converter< T > &  conv,
const std::string &  na = "" 
)
inline

Constructor for a user imposed conversion.

Definition at line 614 of file units.h.

References Antioch::Units< T >::fill_in_power().

614  ://constructor for given conversion
615  symbol(sym),
616  name(na),
617  toSI(conv)
618 {
619  fill_in_power(false);
620 }
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
void fill_in_power(bool doConv)
This method fills the power vector and will calculate the coefficient if the bool doConv is set to tr...
Definition: units.h:623
template<typename T = double>
Antioch::Units< T >::Units ( )
inline

Default constructor.

Definition at line 123 of file units.h.

123 {}
template<typename T = double>
Antioch::Units< T >::~Units ( )
inline

Default destructor.

Definition at line 126 of file units.h.

126 {}

Member Function Documentation

template<typename T = double>
void Antioch::Units< T >::add ( const Units< T > &  rhs)
inline

Alternative call to Units& operator+=(const Units &)

Definition at line 248 of file units.h.

248 {*this += rhs;}
template<typename T = double>
void Antioch::Units< T >::add ( Units< T > *  rhs)
inline

Alternative call to Units& operator+=(const Units &)

Definition at line 250 of file units.h.

250 {*this += *rhs;}
template<typename T = double>
void Antioch::Units< T >::add ( std::string  unit)
inline

Alternative call to Units& operator+=(const Units &)

Same remarks than void equalize(std::string).

Definition at line 255 of file units.h.

255 {*this += Units<T>(unit);}
template<typename T >
const std::string Antioch::Units< T >::add_SI ( int  pow,
std::string  SIsymb 
) const
private

Small method to add the SI symbol and power to a std::string.

This method is useful in the std::string const Units::get_SI_symb() const method.

Definition at line 977 of file units.h.

References std::pow().

978 {
979  std::string out("");
980 
981  if(pow == 0)return out;
982 
983  out = symb;
984  if(pow != 1)
985  {
986  std::stringstream po;
987  po << pow;
988  out += po.str();
989  }
990 
991  return out;
992 
993 }
Antioch::enable_if_c< Antioch::is_valarray< T >::value, typename Antioch::state_type< T >::type >::type pow(const T &in, const T2 &n)
template<typename T = double>
void Antioch::Units< T >::check_if_name ( std::string &  nameOut,
const Units< T > &  un 
) const
private

Fill the name if empty and name obvious.

Usually we give only the symbol, even for simple unit. Thus this method will fill the name if the name is obvious, i.e. if the dimension of the power vector is 1. The suffixe is given by the factor value.

template<typename T = double>
void Antioch::Units< T >::clear ( )
inline

Clear the unit.

Definition at line 390 of file units.h.

References Antioch::InSI::clear(), Antioch::Units< T >::name, Antioch::Units< T >::power, Antioch::Units< T >::symbol, and Antioch::Units< T >::toSI.

Referenced by Antioch::ChemKinParser< NumericType >::rate_constant_preexponential_parameter(), and Antioch::read_reaction_set_data().

390 {symbol.clear();name.clear();toSI.clear();power.clear();}
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
void clear()
Set all the powers to zeros.
Definition: insi.h:276
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
template<typename T = double>
const std::string Antioch::Units< T >::contracted_symbol ( const std::string &  input = "") const
inline

Contract the symbol when possible.

The contraction will aggregate only exactly equal units.

Definition at line 387 of file units.h.

References Antioch::Units< T >::manipulate_symbol().

387 {return manipulate_symbol(input,true);}
const std::string manipulate_symbol(std::string input, bool contract) const
Root method for contracting or harmonizing the symbol.
Definition: units.h:996
template<typename T >
void Antioch::Units< T >::develop_symbol ( std::string &  subsymbol)
private

Supress the parenthesises.

Two types of parenthesises are to be treated:

  • serial parenthesises ().()
  • intricated parenthesises (())

The idea is to treat every serial pairs one by one. Thus every pair found is sent back to develop_symbol(std::string&). Once the we have treated the inside string by this recursive method, we treat the string:

Definition at line 669 of file units.h.

References std::pow().

Referenced by Antioch::Units< T >::Units().

670 {
671  if(subsymb == "no unit" || subsymb == "No unit" || subsymb == "NO UNIT")
672  {
673  subsymb.clear();
674  return;
675  }
676  if(subsymb.find("(") == std::string::npos)return;
677 
678 //model is a serie ().()/()..., we find it
679 // we count the opening parenthesises (no) and
680 // the closing parenthesises (nc). We need to
681 // keep track of the position of opening (po)
682 // and closing (pc) parenthesises. We have a pair
683 // when no == nc.
684  unsigned int no(0),nc(0);
685  unsigned int po(0),pc(subsymb.size() - 1);
686  for(unsigned int cc = 0; cc < subsymb.size(); cc++)
687  {
688  if(subsymb[cc] == '(')
689  {
690  no++;
691  if(po == 0 && cc != 0)po = cc;
692  }
693  if(subsymb[cc] == ')')
694  {
695  nc++;
696  pc = cc;
697  }
698  if(no == 0)continue;
699 
700  if(no == nc)//found a pair
701  {
702  //develop it
703  if(pc == po + 1)
704  {
705  unsigned int off(0);
706  if(po == 0)off = 1;//if ( is the first character or not
707  subsymb.erase(po - 1 + off,3 - off);//if yes, suppress '()', if not suppress '.()' or '/()'
708  }else
709  {
710  std::string insideStr = subsymb.substr(po + 1,pc - po - 1);
711  develop_symbol(insideStr);
712  if(po != 0)if(subsymb[po - 1] == '/')reverse_power_symbol(insideStr);
713  subsymb.replace(po,pc - po + 1,insideStr);
714  }
715  //reset the system
716  no = 0;
717  nc = 0;
718  po = 0;
719  pc = subsymb.size() - 1;
720  cc -= 2;
721  }
722  }
723 
724 //if first character is a power determinant ('/' or '.')
725  if(subsymb[0] == '/') //we change only the first atomic unit
726  {
727  subsymb.erase(0,1);
728  po = subsymb.find(".");
729  if(po > subsymb.find("/"))po = subsymb.find("/");
730  std::string curUnit = subsymb.substr(0,po);
731  int nc(0),pow = - parse_power(curUnit,nc); //power
732  if(pow != 1)
733  {
734  std::ostringstream np;
735  np << pow;
736  if(po < subsymb.size())
737  {
738  subsymb.replace(po - nc,nc,np.str());
739  }else
740  {
741  subsymb += np.str();
742  }
743  }else
744  {
745  subsymb.erase(po - nc,nc);
746  }
747  }else if(subsymb[0] == '.')
748  {
749  subsymb.erase(0,1);
750  }
751 
752 }
void develop_symbol(std::string &subsymbol)
Supress the parenthesises.
Definition: units.h:669
void reverse_power_symbol(std::string &subsymbol)
Treat part of symbol depending on parenthesises.
Definition: units.h:1154
Antioch::enable_if_c< Antioch::is_valarray< T >::value, typename Antioch::state_type< T >::type >::type pow(const T &in, const T2 &n)
int parse_power(std::string unit, int &nc) const
brief parser for std::string of type "some_unit power_associated".
Definition: units.h:862
template<typename T = double>
void Antioch::Units< T >::equalize ( const Units< T > &  rhs)
inline

Alternative call to Units& operator=(const Units &)

Definition at line 232 of file units.h.

232 {*this = rhs;}
template<typename T = double>
void Antioch::Units< T >::equalize ( Units< T > *  rhs)
inline

Alternative call to Units& operator=(const Units &)

Definition at line 234 of file units.h.

234 {*this = *rhs;}
template<typename T = double>
void Antioch::Units< T >::equalize ( std::string  unit)
inline

Alternative call to Units& operator=(const Units &)

This method should not really be used. It is provided for simplicity, but equalizing a Units object to a std::string is weird. Instead, the user should prefer to write ``*this = Units(std::string)''. This is exactly what does the method.

It is provided to ensure that the idea of equalizing the unit to a symbol is coded, but in this object, a unit is more than just a symbol.

Definition at line 246 of file units.h.

246 {*this = Units<T>(unit);}
template<typename T >
T Antioch::Units< T >::factor_to_some_unit ( const Units< T > &  target) const

Calculates the factor to any given unit.

Before calculating the factor to another unit, the homogeneity is tested. If the units are homogeneous, then the factor is the current factor divided by the target factor:

\[ \left.\begin{array}{l} \mathrm{SI} = f_1 \times \mathrm{unit}_1 + t_1 \\ \mathrm{SI} = f_2 \times \mathrm{unit}_2 + t_2 \end{array}\right\} \Rightarrow \mathrm{unit}_2 = \mathrm{unit}_1 \times \frac{f_1}{f_2} + \frac{t_1 - t_2}{f_2} \]

It is important to understand that this is the factor to the wanted unit. Thus to express a value of unit $_1$ in unit $_2$, one should use the conversion rule given above. factor_to_some_unit(unit $_2$) will provide the factor $\frac{f_1}{f_2}$ while translator_to_some_unit(unit $_2$) will provide the translationnal term $\frac{t_1 - t_2}{f_2}$.

Definition at line 755 of file units.h.

References antioch_unit_error, Antioch::Units< T >::get_SI_factor(), and Antioch::Units< T >::get_symbol().

Referenced by Antioch::read_reaction_set_data(), test_factor(), and tester().

756 {
757  if(is_homogeneous(target))
758  {
759  return get_SI_factor()/target.get_SI_factor();
760  }else
761  {
762  antioch_unit_error("Units are not homogeneous:\n\"" + symbol + "\" and \"" + target.get_symbol() + "\".");
763  return -1.;
764  }
765 }
T get_SI_factor() const
Multiplicative coefficient getter.
Definition: units.h:334
#define antioch_unit_error(description)
std::string symbol
Strings for the symbol.
Definition: units.h:579
bool is_homogeneous(const Units< T > &rhs) const
Homogenity testing with another Units.
Definition: units.h:892
template<typename T >
T Antioch::Units< T >::factor_to_some_unit ( const std::string &  target) const

Calculates the factor to any given unit, uses factor_to_some_unit(const Units&).

Definition at line 768 of file units.h.

769 {
770  return factor_to_some_unit(Units<T>(target));
771 }
T factor_to_some_unit(const Units< T > &target) const
Calculates the factor to any given unit.
Definition: units.h:755
template<typename T >
void Antioch::Units< T >::fill_in_power ( bool  doConv)
private

This method fills the power vector and will calculate the coefficient if the bool doConv is set to true.

The method scans the symbol, and calls bool parse_single_unit(int,std::string,bool) for each unit found. The character in front of the unit ('.' or '/') is taken into account into the first integer (resp. 1 or -1). This is this latter method that actually calculates the coefficients and power vector.

Definition at line 623 of file units.h.

References antioch_unit_error.

Referenced by Antioch::Units< T >::Units().

624 {
625  if(symbol.empty())return; // no unity
627 
628  std::string tmp(""),symboltmp(symbol);
629  int signe(1),istart(0);
630 
631  while(symboltmp != contracted_symbol(symboltmp))symboltmp = contracted_symbol(symboltmp);
632  if(symboltmp.empty())return; // no unity
633 
634  if(symboltmp[0] == '/')
635  {
636  signe = -1;
637  istart = 1;
638  }
639  for(unsigned int i = istart ; i < symboltmp.size() ; i++)
640  {
641  if(symboltmp[i] == '.')
642  {
643  if(!parse_single_unit(signe,tmp,doConv))
644  {
645  antioch_unit_error("In symbol " + symboltmp + ", unit \"" + tmp + "\" does not ring a bell");
646  break;
647  }
648  signe = 1;
649  tmp.clear();
650  }else if(symboltmp[i] == '/')
651  {
652  if(!parse_single_unit(signe,tmp,doConv))
653  {
654  antioch_unit_error("In symbol " + symboltmp + ", unit \"" + tmp + "\" does not ring a bell");
655  break;
656  }
657  signe = -1;
658  tmp.clear();
659  }else
660  {
661  tmp += symboltmp[i];
662  }
663  }
664  if(!parse_single_unit(signe,tmp,doConv))
665  antioch_unit_error("In symbol " + symboltmp + ", unit \"" + tmp + "\" does not ring a bell");
666 }
void develop_symbol(std::string &subsymbol)
Supress the parenthesises.
Definition: units.h:669
#define antioch_unit_error(description)
const std::string contracted_symbol(const std::string &input="") const
Contract the symbol when possible.
Definition: units.h:387
bool parse_single_unit(int signe, std::string unit, bool doConv)
Calculates the corresponding coefficient and power of the given unit.
Definition: units.h:812
std::string symbol
Strings for the symbol.
Definition: units.h:579
template<typename T >
int Antioch::Units< T >::get_integer_power ( int  unit,
int  r,
const int &  key 
)
private

Multiply or divide a power.

It ensures that the result of the operation performed is an integer. Natural in case of multiplication, to be checked in case of division. It returns 0 if it is not correct, this has to be checked by the wrapping method. The key is to choose between division or multiplication. Key is:

  • 1 for multiplication
  • -1 for division

Definition at line 1210 of file units.h.

References antioch_error.

1211 {
1212  if(key == 1)//multiplication
1213  {
1214  return unit * r;
1215  }else if(key == -1)//division
1216  {
1217  if(unit%r != 0)return 0;
1218  return unit / r;
1219  }else
1220  {
1221  std::cerr << "Key is not acceptable. This is a private method, there is a big problem..." << std::endl;
1222  antioch_error();
1223  return 0;
1224  }
1225 
1226 
1227 }
#define antioch_error()
template<typename T = double>
const std::string Antioch::Units< T >::get_name ( ) const
inline

String name getter.

Definition at line 328 of file units.h.

References Antioch::Units< T >::name.

Referenced by Antioch::Units< T >::operator+=(), Antioch::Units< T >::operator-=(), and Antioch::Units< T >::operator=().

328 {return name;}
std::string name
Strings for the name.
Definition: units.h:581
template<typename T = double>
const InSI Antioch::Units< T >::get_power ( ) const
inline

Power std::vector getter.

Definition at line 338 of file units.h.

References Antioch::Units< T >::power.

Referenced by Antioch::Units< T >::is_homogeneous(), Antioch::Units< T >::operator+=(), and Antioch::Units< T >::operator-=().

338 {return power;}
InSI power
An InSI for the power vector.
Definition: units.h:585
template<typename T = double>
const Converter<T> Antioch::Units< T >::get_SI_coef ( ) const
inline

Coefficient getter.

Definition at line 332 of file units.h.

References Antioch::Units< T >::toSI.

Referenced by Antioch::Units< T >::operator+=(), and Antioch::Units< T >::operator-=().

332 {return toSI;}
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
template<typename T >
const std::string Antioch::Units< T >::get_SI_convenient_symb ( ) const

Corresponding SI symbol getter.

This method returns the symbol that of the SI unit (so a coefficient of (1.,0.)). If none is found, it returns const std::string get_SI_symb(). For instance a unit J/s will be returned as W.

This method is not able to combine different SI units. The unit J.s will not be recognized as J.s, only as m2.kg.s-1

Definition at line 1125 of file units.h.

References Antioch::UnitBaseStorage::known_units().

1126 {
1127  if(this->n_dimension_of_units() == 0)
1128  {
1129  std::string outStr("");
1130  return outStr;
1131  }else
1132  {
1133  for(int iUnit = 1; iUnit < UnitBaseStorage::known_units().n_known_units(); iUnit++)
1134  {
1135  if(is_homogeneous(UnitBaseStorage::known_units().stored(iUnit).symbol()) &&
1136  UnitBaseStorage::known_units().stored(iUnit).converter().geta() == 1. &&
1137  UnitBaseStorage::known_units().stored(iUnit).converter().getb() == 0.)return UnitBaseStorage::known_units().stored(iUnit).symbol();
1138  }
1139  return get_SI_symb();
1140  }
1141 }
const std::string get_SI_symb() const
Corresponding SI symbol getter.
Definition: units.h:929
int n_dimension_of_units() const
Number of dimensions of the unit in the SI basis.
Definition: units.h:1108
std::string symbol
Strings for the symbol.
Definition: units.h:579
const UnitBaseConstant::UnitBaseStore< long double > known_units()
Definition: unit_defs.h:65
bool is_homogeneous(const Units< T > &rhs) const
Homogenity testing with another Units.
Definition: units.h:892
template<typename T = double>
T Antioch::Units< T >::get_SI_factor ( ) const
inline

Multiplicative coefficient getter.

Definition at line 334 of file units.h.

References Antioch::Units< T >::toSI.

Referenced by Antioch::Units< T >::factor_to_some_unit(), Antioch::read_reaction_set_data(), test_factor(), AntiochTesting::ArrheniusRateTest< long double >::test_reset_scalar_params(), tester(), and Antioch::Units< T >::translator_to_some_unit().

334 {return toSI.geta();}
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
template<typename T >
int Antioch::Units< T >::get_SI_power ( const std::string &  SIask) const

Power of the asked SI unit.

This method returns the power to the asked unit SI.

Definition at line 912 of file units.h.

References antioch_unit_error.

913 {
914  if(SIask == "m") return power.get_m();
915  if(SIask == "kg") return power.get_kg();
916  if(SIask == "s") return power.get_s();
917  if(SIask == "A") return power.get_A();
918  if(SIask == "K") return power.get_K();
919  if(SIask == "mol")return power.get_mol();
920  if(SIask == "cd") return power.get_cd();
921  if(SIask == "rad")return power.get_rad();
922 
923  antioch_unit_error(SIask + " is not a SI symbol");
924 
925  return 0;
926 }
int get_m() const
meter power getter
Definition: insi.h:100
int get_rad() const
radian power getter
Definition: insi.h:114
int get_mol() const
mol power getter
Definition: insi.h:110
int get_s() const
second power getter
Definition: insi.h:104
int get_kg() const
kilogramme power getter
Definition: insi.h:102
#define antioch_unit_error(description)
InSI power
An InSI for the power vector.
Definition: units.h:585
int get_A() const
ampere power getter
Definition: insi.h:106
int get_K() const
kelvin power getter
Definition: insi.h:108
int get_cd() const
candela power getter
Definition: insi.h:112
template<typename T >
std::string const Antioch::Units< T >::get_SI_symb ( ) const

Corresponding SI symbol getter.

This method returns the symbol that correspond to the power vector. For instance a unit J/s will be returned as m2.kg.s-3

Definition at line 929 of file units.h.

930 {
931  std::string SISymb;
932  SISymb.clear();
933 
934  if(power.get_m() != 0)SISymb += add_SI(power.get_m(),"m");
935 
936  if(power.get_kg() != 0)
937  {
938  if(!SISymb.empty())SISymb += ".";
939  SISymb += add_SI(power.get_kg(),"kg");
940  }
941  if(power.get_s() != 0)
942  {
943  if(!SISymb.empty())SISymb += ".";
944  SISymb += add_SI(power.get_s(),"s");
945  }
946  if(power.get_A() != 0)
947  {
948  if(!SISymb.empty())SISymb += ".";
949  SISymb += add_SI(power.get_A(),"A");
950  }
951  if(power.get_K() != 0)
952  {
953  if(!SISymb.empty())SISymb += ".";
954  SISymb += add_SI(power.get_K(),"K");
955  }
956  if(power.get_mol() != 0)
957  {
958  if(!SISymb.empty())SISymb += ".";
959  SISymb += add_SI(power.get_mol(),"mol");
960  }
961  if(power.get_cd() != 0)
962  {
963  if(!SISymb.empty())SISymb += ".";
964  SISymb += add_SI(power.get_cd(),"cd");
965  }
966  if(power.get_rad() != 0)
967  {
968  if(!SISymb.empty())SISymb += ".";
969  SISymb += add_SI(power.get_rad(),"rad");
970  }
971 
972  return SISymb;
973 
974 }
int get_m() const
meter power getter
Definition: insi.h:100
int get_rad() const
radian power getter
Definition: insi.h:114
int get_mol() const
mol power getter
Definition: insi.h:110
int get_s() const
second power getter
Definition: insi.h:104
int get_kg() const
kilogramme power getter
Definition: insi.h:102
InSI power
An InSI for the power vector.
Definition: units.h:585
const std::string add_SI(int pow, std::string SIsymb) const
Small method to add the SI symbol and power to a std::string.
Definition: units.h:977
int get_A() const
ampere power getter
Definition: insi.h:106
int get_K() const
kelvin power getter
Definition: insi.h:108
int get_cd() const
candela power getter
Definition: insi.h:112
template<typename T = double>
T Antioch::Units< T >::get_SI_translator ( ) const
inline

Translationnal coefficient getter.

Definition at line 336 of file units.h.

References Antioch::Units< T >::toSI.

Referenced by Antioch::Units< T >::translator_to_some_unit().

336 {return toSI.getb();}
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
template<typename T = double>
const std::string Antioch::Units< T >::get_symbol ( ) const
inline
template<typename T = double>
const std::string Antioch::Units< T >::harmonized_symbol ( const std::string &  input = "") const
inline

Simplify the symbol when possible.

This method will densify the symbol. As calculations merely add the symbol, it is necessary to be able to group together the same units and eliminate those that compensate.

The method will scan unit by unit the symbol std::string and combine the units that are homogeneous. The unit "l" is converted to "m3" to ensure meter harmonization. The chosen unit symbol between two homogeneous units is the first symbol encountered.

Todo:
The litre management is a fix specific to litre. What should be done would be a pre-treatment of the symbol, test if its power vector is a multiple of one of the unity of the base std::vector, and make the change.

Definition at line 381 of file units.h.

References Antioch::Units< T >::manipulate_symbol().

381 {return manipulate_symbol(input,false);}
const std::string manipulate_symbol(std::string input, bool contract) const
Root method for contracting or harmonizing the symbol.
Definition: units.h:996
template<typename T >
int Antioch::Units< T >::indexUnit ( std::string  unit) const
private

Scanner of knownUnits[], sends back the corresponding iUnit, -1 if not found.

Definition at line 840 of file units.h.

References Antioch::UnitBaseStorage::known_units().

841 {
842  int iUnit;
843 
844  if(unit.empty())return -1;
845 
846  if(unit == "g")//special case to adapt SI
847  {
848  unit = "kg";
849  }
850  for (iUnit = 0; iUnit < UnitBaseStorage::known_units().n_known_units() ; iUnit++)
851  {
852  if(unit == UnitBaseStorage::known_units().stored(iUnit).symbol())break;
853  }
854 
855  if(iUnit >= UnitBaseStorage::known_units().n_known_units())iUnit=-1;
856 
857 
858  return iUnit;
859 }
const UnitBaseConstant::UnitBaseStore< long double > known_units()
Definition: unit_defs.h:65
template<typename T >
bool Antioch::Units< T >::is_homogeneous ( const Units< T > &  rhs) const

Homogenity testing with another Units.

Testing the homogeneity between units is different from testing equality. J and cal are homogeneous but not equal for instance. Thus this method merely compares the power vectors, if they're equal the units are homogeneous, if not, they're not.

Definition at line 892 of file units.h.

References Antioch::Units< T >::get_power().

Referenced by Antioch::Units< T >::manipulate_symbol(), Antioch::read_reaction_set_data(), test_factor(), test_homogeneity(), and Antioch::verify_unit_of_parameter().

893 {
894  return (power == rhs.get_power());
895 }
InSI power
An InSI for the power vector.
Definition: units.h:585
template<typename T >
bool Antioch::Units< T >::is_homogeneous ( std::string  target) const

Homogenity testing with a std::string.

Testing homogeneity with a std::string needs to build a Units object and testing homogeneity with it. In the case of an empty std::string, instead of building a Units object the boolean is_united() is tested.

Definition at line 898 of file units.h.

899 {
900  if(target.empty())
901  {
902  return (!this->is_united());
903  }else
904  {
905  Units<T> rhs(target);
906  return is_homogeneous(rhs);
907  }
908 
909 }
bool is_united() const
Test if the unit is non empty.
Definition: units.h:298
bool is_homogeneous(const Units< T > &rhs) const
Homogenity testing with another Units.
Definition: units.h:892
template<typename T >
bool Antioch::Units< T >::is_in_symb ( char  c) const
private

Small method that checks if the symbol is within a unit std::string.

This method checks if the symbol is not a number (bool is_number(char)), and not any of the following character: '/', '.' and '-'. Then this symbol is consider to belong to a unit symbol.

Definition at line 1242 of file units.h.

1243 {
1244 
1245  return (c != '/' &&
1246  c != '.' &&
1247  c != '-' &&
1248  !this->is_number(c));
1249 }
bool is_number(char c) const
Small method to check if a character is a numerical.
Definition: units.h:488
template<typename T = double>
bool Antioch::Units< T >::is_number ( char  c) const
inlineprivate

Small method to check if a character is a numerical.

This method uses the values of the ascii table, which basically means that is checks if the ascii integer associated to the character is in the range [48,57].

Definition at line 488 of file units.h.

488 {return ((int)c <= 57 && (int)c >= 48);} //ascii table
template<typename T = double>
bool Antioch::Units< T >::is_united ( ) const
inline

Test if the unit is non empty.

Definition at line 298 of file units.h.

References Antioch::InSI::empty(), and Antioch::Units< T >::power.

Referenced by Antioch::read_reaction_set_data(), and Antioch::verify_unit_of_parameter().

298 {return !power.empty();}
InSI power
An InSI for the power vector.
Definition: units.h:585
bool empty() const
Check if empty (all values to zero)
Definition: insi.h:289
template<typename T >
std::string const Antioch::Units< T >::manipulate_symbol ( std::string  input,
bool  contract 
) const
private

Root method for contracting or harmonizing the symbol.

The symbol MUST be developped prior to be used in this method.

Definition at line 996 of file units.h.

References antioch_unit_error, Antioch::Units< T >::get_symbol(), and Antioch::Units< T >::is_homogeneous().

Referenced by Antioch::Units< T >::contracted_symbol(), and Antioch::Units< T >::harmonized_symbol().

997 {
998  std::string harmSymb("");
999  if(input.empty())input = symbol;
1000 
1001  std::vector<std::string> unitvec;
1002  std::vector<int> powervec;
1003  std::string curUnit(""),interUnit(".");//,strPower;
1004 
1005  for(unsigned int i = 0; i < input.size(); i++)
1006  {
1007  if(input[i] != '.' && input[i] != '/')curUnit += input[i];
1008  if(input[i] == '.' || input[i] == '/' || i == input.size() - 1 )
1009  {
1010  if(curUnit.empty())continue;
1011 //first, parsing the unit
1012  int nc(0);
1013  int iUnit(-1),iPre(-1);
1014  int curPower = parse_power(curUnit,nc); //power
1015 // strPower = curUnit.substr(curUnit.length() - nc,std::string::npos); //resulting power
1016  curUnit = curUnit.substr(0,curUnit.length() - nc); //resulting unit
1017  parse_prefix_unit(iUnit,iPre,curUnit);
1018  // unit is UnitBaseStorage::Prefixes[iPre].symbol() + UnitBaseStorage::knownUnits[iUnit].get_symbol() + strPower
1019  if(iUnit == -1)
1020  {
1021  antioch_unit_error("The unit \"" + curUnit + "\" is unknown. No harmonized symbol will be produced.");
1022  harmSymb.clear();
1023  return harmSymb;
1024  }
1025 //updating variables:
1026 // - curUnit is the unit (std::string)
1027 // - curPower is the power (int)
1028  if(interUnit == "/")curPower *= -1;
1029 
1030 // checking what we've got
1031  unsigned int j;
1032  for(j = 0; j < unitvec.size(); j++)
1033  {
1034  Units<T> tmp(unitvec[j]);
1035  bool same(tmp.get_symbol() == curUnit);//only contraction: strong condition
1036  if(!contract && !same)same = tmp.is_homogeneous(curUnit); //harmonizing: weak condition
1037  if(same)//if in there, update the power,
1038  {
1039  powervec[j] += curPower;
1040  break;
1041  }
1042  }
1043  if(j >= unitvec.size())//if not, add
1044  {
1045  unitvec.push_back(curUnit);
1046  powervec.push_back(curPower);
1047  }
1048  interUnit = input[i];
1049  curUnit.clear();
1050  }
1051  }
1052 
1053 //litre fix: only if harmonizing
1054 // if there is l AND m in the vector, all is converted to m.
1055  if(!contract)
1056  {
1057  bool ism(false),isl(false);
1058  unsigned int herem(0),herel(0);
1059  for(unsigned int i = 0; i < unitvec.size(); i++)
1060  {
1061  if(unitvec[i] == "m")
1062  {
1063  ism = true;
1064  herem = i;
1065  }
1066  if(unitvec[i] == "l")
1067  {
1068  isl = true;
1069  herel = i;
1070  }
1071  }
1072  if(ism && isl)
1073  {
1074  powervec[herem] += 3 * powervec[herel];
1075  unitvec.erase(unitvec.begin() + herel);
1076  powervec.erase(powervec.begin() + herel);
1077  }
1078  }
1079 //now writing harmsymb
1080 
1081  std::ostringstream outsym;
1082  int k(0);
1083  for(unsigned int i = 0; i < unitvec.size(); i++)
1084  {
1085  if(powervec[i] == 0)continue; //ignore deleted unit
1086  if(k != 0) // need a symbol, '.' or '/'
1087  {
1088  if(powervec[i] > 0) // '.'
1089  {
1090  outsym << ".";
1091  }else if(powervec[i] < 0) // '/' and reverse the power
1092  {
1093  outsym << "/";
1094  powervec[i] *= -1;
1095  }
1096  }
1097  outsym << unitvec[i];
1098  if(powervec[i] != 1)outsym << powervec[i];
1099  k++;
1100  }
1101 
1102  harmSymb = outsym.str();
1103 
1104  return harmSymb;
1105 }
#define antioch_unit_error(description)
void parse_prefix_unit(int &iUnit, int &iPre, const std::string &unit) const
Unit parser, parse a std::string of type Prefixes[] knownUnits[].
Definition: units.h:788
int parse_power(std::string unit, int &nc) const
brief parser for std::string of type "some_unit power_associated".
Definition: units.h:862
std::string symbol
Strings for the symbol.
Definition: units.h:579
template<typename T >
int Antioch::Units< T >::n_dimension_of_units ( ) const
private

Number of dimensions of the unit in the SI basis.

Definition at line 1108 of file units.h.

1109 {
1110  int ndim(0);
1111 
1112  if(power.get_m() != 0)ndim++;
1113  if(power.get_kg() != 0)ndim++;
1114  if(power.get_s() != 0)ndim++;
1115  if(power.get_A() != 0)ndim++;
1116  if(power.get_K() != 0)ndim++;
1117  if(power.get_mol() != 0)ndim++;
1118  if(power.get_cd() != 0)ndim++;
1119  if(power.get_rad() != 0)ndim++;
1120 
1121  return ndim;
1122 }
int get_m() const
meter power getter
Definition: insi.h:100
int get_rad() const
radian power getter
Definition: insi.h:114
int get_mol() const
mol power getter
Definition: insi.h:110
int get_s() const
second power getter
Definition: insi.h:104
int get_kg() const
kilogramme power getter
Definition: insi.h:102
InSI power
An InSI for the power vector.
Definition: units.h:585
int get_A() const
ampere power getter
Definition: insi.h:106
int get_K() const
kelvin power getter
Definition: insi.h:108
int get_cd() const
candela power getter
Definition: insi.h:112
template<typename T = double>
bool Antioch::Units< T >::operator!= ( const Units< T > &  rhs) const
inline

Comparison operator, not equal is not "equal".

Definition at line 153 of file units.h.

153 {return !(*this == rhs);}
template<typename T >
Units< T > Antioch::Units< T >::operator* ( int  r) const

Multiplying operator by an integer, uses Units &operator*=(int)

Definition at line 1351 of file units.h.

1352 {
1353  return (Units<T>(*this) *= r);
1354 }
template<typename T >
Units< T > & Antioch::Units< T >::operator*= ( int  r)

Multiplying operator by an integer.

This operator is needed for power elevation operation of a physical quantity.

The operations needed are as follow:

  • parsing the symbol so all the power are multiplied by the integer, done by the method void symbol_to_the_power(int,std::string);
  • multiplying the power vector by the integer;
  • elevating the coefficient to the power, done by the method Converter raise(const Converter&, int).

Elevating to the power with a double is not supported, and the physical sense of it anyway is not obvious...

Definition at line 1310 of file units.h.

1311 {
1312  power *= r;
1313  toSI = this->raise(toSI,r);
1314  this->symbol_to_the_power(r,1);
1315  if(symbol == "failed")
1316  {
1317  symbol = this->get_SI_symb();
1318  }
1319 
1320  return *this;
1321 }
const std::string get_SI_symb() const
Corresponding SI symbol getter.
Definition: units.h:929
void symbol_to_the_power(int r, const int &key)
Rewrite the symbol to put it at a given power.
Definition: units.h:1172
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
template<typename T >
Units< T > Antioch::Units< T >::operator+ ( const Units< T > &  rhs) const

Adding operator, uses Units &operator+=(const Units&)

Definition at line 1339 of file units.h.

1340 {
1341  return (Units<T>(*this) += rhs);
1342 }
template<typename T >
Units< T > & Antioch::Units< T >::operator+= ( const Units< T > &  rhs)

Adding operator.

Adding units occurs when multiplying physical quantities.

Adding a unit needs to add first the name if the added name is not empty. Addition of name consist simply of putting them side by side with a blank space in between.

The symbol is added in the same fashion, but with a dot in between to ensure parsability. If the current symbol is empty, them it is simply copied.

The power is added and the coefficient is multiplied.

Definition at line 1274 of file units.h.

References Antioch::Units< T >::get_name(), Antioch::Units< T >::get_power(), Antioch::Units< T >::get_SI_coef(), and Antioch::Units< T >::get_symbol().

1275 {
1276 
1277 // the name
1278  if(!rhs.get_name().empty())name += " " + rhs.get_name();
1279 
1280 // the symbol
1281  if(rhs.get_symbol().empty())return *this;
1282  if(!symbol.empty())
1283  {
1284  symbol += ".(" + rhs.get_symbol() + ")";
1285  }else
1286  {
1287  symbol = rhs.get_symbol();
1288  }
1289  toSI *= rhs.get_SI_coef();
1290  power += rhs.get_power();
1291  return *this;
1292 }
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
template<typename T >
Units< T > Antioch::Units< T >::operator- ( const Units< T > &  rhs) const

Substracting operator, uses Units &operator-=(const Units&)

Definition at line 1345 of file units.h.

1346 {
1347  return (Units<T>(*this) -= rhs);
1348 }
template<typename T >
Units< T > & Antioch::Units< T >::operator-= ( const Units< T > &  rhs)

Substracting operator.

Substracting units occurs when dividing physical quantities.

Substracting is close to adding (see Units &operator+=(const Units&)), with little differences. The adding of the name is done with the sequence blank-slash-blank (" / ") between the names. The symbol needs a bit more processing: if the added symbol is not empty, is it put between parenthesises and added with a slash in between. Then the void develop_symbol() method is called to suppress the parenthesises and obtain a correct symbol.

The coefficient is divided and the power vector substracted.

Definition at line 1295 of file units.h.

References Antioch::Units< T >::get_name(), Antioch::Units< T >::get_power(), Antioch::Units< T >::get_SI_coef(), and Antioch::Units< T >::get_symbol().

1296 {
1297  if(!rhs.get_name().empty())name += " / " + rhs.get_name();
1298  if(!rhs.get_symbol().empty())
1299  {
1300  symbol += "/(" + rhs.get_symbol() + ")";
1301  }
1302  this->develop_symbol(symbol);
1303  toSI /= rhs.get_SI_coef();
1304  power -= rhs.get_power();
1305 
1306  return *this;
1307 }
void develop_symbol(std::string &subsymbol)
Supress the parenthesises.
Definition: units.h:669
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
template<typename T >
Units< T > Antioch::Units< T >::operator/ ( int  r) const

Dividing operator by an integer, uses Units &operator/=(int)

Definition at line 1357 of file units.h.

1358 {
1359  return (Units<T>(*this) /= r);
1360 }
template<typename T >
Units< T > & Antioch::Units< T >::operator/= ( int  r)

dividing operator by an integer

This operator is needed for root operation of a physical quantity.

The operations needed are as follow:

  • dividing the power vector by the integer, test that the resulting power is an integer;
  • parsing the symbol so all the power are divided by the integer, done by the method void symbol_to_the_power(int,std::string), if the symbol contains unit symbol that gives non integer resulting power, then the SI symbol is called;
  • dividing the coefficient to the power, done by the method Converter raise(const Converter&, double).

Definition at line 1324 of file units.h.

1325 {
1326  power /= r;//check consistency of root
1327  this->symbol_to_the_power(r,-1);
1328  if(symbol == "failed")
1329  {
1330  symbol = this->get_SI_symb();
1331  }
1332  toSI = this->raise(toSI,1./(T)r);
1333 
1334  return *this;
1335 }
const std::string get_SI_symb() const
Corresponding SI symbol getter.
Definition: units.h:929
void symbol_to_the_power(int r, const int &key)
Rewrite the symbol to put it at a given power.
Definition: units.h:1172
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
template<typename T >
Units< T > & Antioch::Units< T >::operator= ( const Units< T > &  rhs)

Assignement operator.

It assigns the symbol std::string, the power vector and coefficient are filled.

Definition at line 1262 of file units.h.

References Antioch::Units< T >::get_name(), and Antioch::Units< T >::get_symbol().

1263 {
1264  if(this == &rhs){return *this;}
1265  name = rhs.get_name();
1266  symbol = rhs.get_symbol();
1267  toSI.clear();
1268  power.clear();
1269  fill_in_power(true);
1270  return *this;
1271 }
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
void clear()
Set all the powers to zeros.
Definition: insi.h:276
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
void fill_in_power(bool doConv)
This method fills the power vector and will calculate the coefficient if the bool doConv is set to tr...
Definition: units.h:623
template<typename T = double>
bool Antioch::Units< T >::operator== ( const Units< T > &  rhs) const

Comparison operator.

Compares the power vector and the coefficient, regardless of the symbol and the name.

template<typename T >
int Antioch::Units< T >::parse_power ( std::string  unit,
int &  nc 
) const
private

brief parser for std::string of type "some_unit power_associated".

Parameters
std::stringunit. Input, std::string to be parsed.
int&nc. Output, number of charaters of the power.
Returns
the power associated to the unit.

Definition at line 862 of file units.h.

References antioch_unit_error.

863 {
864  int ip = 1, loc = unit.length();
865  char c = unit[loc-1];
866  nc = 0;
867  while(is_number(c)){
868  nc++;
869  loc--;
870  c = unit[loc-1];
871  }
872 
873  if(unit[loc-1] == '-'){
874  loc--;
875  nc++;
876  }
877 
878  std::string power = unit.substr(loc,std::string::npos);
879  if(power.size() > 0)
880  {
881  std::stringstream p;
882  p << power;
883  p >> ip;
884  }
885 
886  if(ip == 0)antioch_unit_error("Invalid power found: " + unit);
887 
888  return ip;
889 }
#define antioch_unit_error(description)
bool is_number(char c) const
Small method to check if a character is a numerical.
Definition: units.h:488
InSI power
An InSI for the power vector.
Definition: units.h:585
template<typename T >
void Antioch::Units< T >::parse_prefix_unit ( int &  iUnit,
int &  iPre,
const std::string &  unit 
) const
private

Unit parser, parse a std::string of type Prefixes[] knownUnits[].

Parameters
int&iUnit. Output, indice of the found unit, if not found, set to -1.
int&iPre. Output, indice of the found prefixes, if not found, set to -1.
std::stringunit. Input, std::string to be parsed.

This method will parse a std::string, assuming the std::string is of the form Prefixes[]knownUnits[]. No power should be present (see int parse_power(std::string,int&) for power management).

This method scans the Prefixes[] array, if one is found at the start of the std::string, then the remaining std::string is tested to see if it is a unit (method int indexUnit(std::string)). If the test fail the scanning resumes. If a Prefixes[] and a knownUnits[] are found, then its over. If no Prefixes[] associated to a knownUnits[] is found, then the whole std::string is tested to be a knownUnits[].

This method relies on the hypothesis that all possible pairs of Prefixes[] and knownUnits[] (including no prefix) are different.

Definition at line 788 of file units.h.

References Antioch::UnitBaseStorage::known_prefixes(), and Antioch::UnitBaseStorage::known_units().

789 {
790  iPre = -1;
791 
792  iUnit = UnitBaseStorage::known_units().stored_index(unit);
793 //prefix, if exists, is one or two character
794  if(iUnit == -1 && unit.size() > 1)
795  {
796  std::string pre = unit.substr(0,1);
797  std::string un = unit.substr(1,std::string::npos);
798  iPre = UnitBaseStorage::known_prefixes().stored_index(pre);
799  iUnit = UnitBaseStorage::known_units().stored_index(un);
800  if(iPre == -1 && iUnit == -1 && unit.size() > 2)
801  {
802  pre = unit.substr(0,2);
803  un = unit.substr(2,std::string::npos);
804  iPre = UnitBaseStorage::known_prefixes().stored_index(pre);
805  iUnit = UnitBaseStorage::known_units().stored_index(un);
806  }
807  }
808  return;
809 }
const UnitBaseConstant::SIPrefixeStore< long double > known_prefixes()
Definition: unit_defs.h:71
const UnitBaseConstant::UnitBaseStore< long double > known_units()
Definition: unit_defs.h:65
template<typename T >
bool Antioch::Units< T >::parse_single_unit ( int  signe,
std::string  unit,
bool  doConv 
)
private

Calculates the corresponding coefficient and power of the given unit.

Parameters
intsigne, 1 or -1. Image of the sign before the unit '.' or '/'.
std::stringunit. The unit to be parsed.
booldoConv. Switch to perform or not the coefficient calculation.

The method works in two times:

  • first parsing the symbol into prefix + symbol + power
  • second update the power vector and, if doConv is true, the coefficient.

The parsing uses the methods int parse_power(std::string,int&) and parse_prefix_unit(int&,int&,std::string). The idea is to scan the knownUnits[] and Prefixes[] array and find the corresponding unit and prefix. The method int parse_power returns the power, while the integer iPre and iUnit characterize the prefix and unit, with a value of -1 if not found. An unknown unit will return an error.

The update then is twofold. Adding to the power vector the power vector of the found unit multiplied by the signe and the power:

\[ <p> = <p> + \left(<p>_{iUnit} \times s_u \times p_u\right) \]

with $<p>$ being the updated power vector, $p_u$ the power of the parsed unit, $<p>_{iUnit}$ the power vector of the found knownUnit[], $s_u$ the integer associated to the sign of the parsed unit.

If the conversion is to be done, multiplying the coefficient by the coefficient of the known unit found, multiplied by the coefficient of the prefix, raise to the power by the parsed power, updated by the signe:

\[ (c) = (c) \times \left( pr_{iPre} \times (c)_{iUnit}\right)^{s_u \times p_u} \]

with $(c)$ being the updated complex coefficient, $(c)_{iUnit}$ the complex coefficient of the found unit, $pr_{iPre}$ the coefficient of the found Prefixes (if found, if not, it is unity), $s_u$ the sign of the parsed unit and $p_u$ the power of the parsed unit.

Definition at line 812 of file units.h.

References Antioch::UnitBaseStorage::known_prefixes(), and Antioch::UnitBaseStorage::known_units().

813 {
814  int iUnit = -1, nc = 0;
815  int ipower = parse_power(unit,nc); // find power
816  int iPre = -1;
817 
818  unit = unit.substr(0,unit.length()-nc); //unit without power
819 
820  parse_prefix_unit(iUnit,iPre,unit);//find prefix and unit
821 
822  if(iUnit == -1)return false; //not found
823 
824  T pre = 1.L;
825  if(iPre != -1)pre = UnitBaseStorage::known_prefixes().stored(iPre).value<T>();
826  if(UnitBaseStorage::known_units().stored(iUnit).symbol() == "kg" &&
827  unit != "kg")pre *= 1e-3L; //rescale
828  InSI powerTmp = UnitBaseStorage::known_units().stored(iUnit).power_array();
829  power += (powerTmp * signe * ipower);
830  if(doConv)
831  {
832  Converter<T> convTmp = UnitBaseStorage::known_units().stored(iUnit).converter() * pre;
833  toSI += raise(convTmp,signe * ipower);
834  }
835 
836  return true;
837 }
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
void parse_prefix_unit(int &iUnit, int &iPre, const std::string &unit) const
Unit parser, parse a std::string of type Prefixes[] knownUnits[].
Definition: units.h:788
InSI power
An InSI for the power vector.
Definition: units.h:585
const UnitBaseConstant::SIPrefixeStore< long double > known_prefixes()
Definition: unit_defs.h:71
int parse_power(std::string unit, int &nc) const
brief parser for std::string of type "some_unit power_associated".
Definition: units.h:862
const UnitBaseConstant::UnitBaseStore< long double > known_units()
Definition: unit_defs.h:65
template<typename T >
void Antioch::Units< T >::print ( std::ostream &  os = std::cout) const

Formatted print.

Defaults to std::cout.

Definition at line 1363 of file units.h.

1364  {
1365  os << name << " (" << symbol << "), "
1366  << toSI << ", "
1367  << power << std::endl;
1368  }
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
template<typename T >
Converter< T > Antioch::Units< T >::raise ( const Converter< T > &  tbm,
int  power 
) const
private

Raise to an integer power a Converter object.

The rising to the power of a type Converter is done as follow:

\[ (a,b)^i =\left\{\begin{array}{ll} (a^i,b) & \mathrm{if}\ i\ \mathrm{equal\ 1} \\ (a^i,0) & \mathrm{else} \end{array}\right. \]

Returns
A Converter object which is the results of the operation.

Definition at line 1230 of file units.h.

References Antioch::Converter< T >::geta(), Antioch::Converter< T >::getb(), and std::pow().

1231 {
1232  return Converter<T>(std::pow(tbm.geta(),power),(power != 1)?0.:tbm.getb());
1233 }
Antioch::enable_if_c< Antioch::is_valarray< T >::value, typename Antioch::state_type< T >::type >::type pow(const T &in, const T2 &n)
InSI power
An InSI for the power vector.
Definition: units.h:585
template<typename T >
Converter< T > Antioch::Units< T >::raise ( const Converter< T > &  tbm,
double  power 
) const
private

Overload to simplify division.

Another method, to secure multiplication. Division is thus put apart, to secure against non integer power.

Definition at line 1236 of file units.h.

References Antioch::Converter< T >::geta(), Antioch::Converter< T >::getb(), and std::pow().

1237 {
1238  return Converter<T>(std::pow(tbm.geta(),power),(power != 1.)?0.:tbm.getb());
1239 }
Antioch::enable_if_c< Antioch::is_valarray< T >::value, typename Antioch::state_type< T >::type >::type pow(const T &in, const T2 &n)
InSI power
An InSI for the power vector.
Definition: units.h:585
template<typename T >
void Antioch::Units< T >::reverse_power_symbol ( std::string &  subsymbol)
private

Treat part of symbol depending on parenthesises.

Inverse the power sign determinant ('.' or '/'). Note that the first power will not be affected. Ex: J.mol-1/K will be changed to J/mol-1.K, leaving J unchanged.

Definition at line 1154 of file units.h.

1155 {
1156  unsigned int curInter;
1157  curInter = subsymbol.find(".");
1158  if(subsymbol.find("/") < curInter)curInter = subsymbol.find("/");
1159  while(curInter < subsymbol.size())
1160  {
1161  (subsymbol[curInter] == '.')?
1162  subsymbol.replace(curInter,1,"/"):
1163  subsymbol.replace(curInter,1,".");
1164  curInter++;
1165  unsigned int tmp = subsymbol.find(".",curInter);
1166  if(subsymbol.find("/",curInter) < tmp)tmp = subsymbol.find("/",curInter);
1167  curInter = tmp;
1168  }
1169 }
template<typename T = double>
void Antioch::Units< T >::root ( int  r)
inline

Alternative method to Units& operator/=(int)

Definition at line 266 of file units.h.

266 {*this /= r;}
template<typename T = double>
void Antioch::Units< T >::set_name ( const std::string &  na)
inline

Name setter.

Definition at line 273 of file units.h.

References Antioch::Units< T >::name.

273 {name = na;}
std::string name
Strings for the name.
Definition: units.h:581
template<typename T = double>
void Antioch::Units< T >::set_power_to_SI ( const InSI mat)
inline

Power std::vector setter.

Definition at line 279 of file units.h.

References Antioch::Units< T >::power.

279 {power = mat;}
InSI power
An InSI for the power vector.
Definition: units.h:585
template<typename T = double>
void Antioch::Units< T >::set_SI_converter ( const Converter< T > &  conv)
inline

Coefficient setter.

Definition at line 277 of file units.h.

References Antioch::init_clone(), and Antioch::Units< T >::toSI.

277 {Antioch::init_clone(toSI,conv);}
void init_clone(T &output, const T &example)
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
template<typename T = double>
void Antioch::Units< T >::set_symbol ( const std::string &  symb)
inline

Symbol setter.

Definition at line 275 of file units.h.

References Antioch::Units< T >::symbol.

275 {symbol = symb;}
std::string symbol
Strings for the symbol.
Definition: units.h:579
template<typename T >
void Antioch::Units< T >::set_unit ( const std::string &  sym,
std::string  na 
)

Units setter, sets the unit to the given symbol and name.

Definition at line 1144 of file units.h.

Referenced by Antioch::read_reaction_set_data(), test_factor(), and Antioch::verify_unit_of_parameter().

1145 {
1146  symbol = sym;
1147  name = na;
1148  toSI.clear();
1149  power.clear();
1150  fill_in_power(true);
1151 }
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
void clear()
Set all the powers to zeros.
Definition: insi.h:276
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
void fill_in_power(bool doConv)
This method fills the power vector and will calculate the coefficient if the bool doConv is set to tr...
Definition: units.h:623
template<typename T = double>
void Antioch::Units< T >::set_unit ( const std::string &  sym)
inline

Units setter, sets the unit to the given symbol and name.

Definition at line 271 of file units.h.

References Antioch::Units< T >::set_unit().

Referenced by Antioch::Units< T >::set_unit().

271 {set_unit(sym,"");}
void set_unit(const std::string &sym, std::string na)
Units setter, sets the unit to the given symbol and name.
Definition: units.h:1144
template<typename T >
void Antioch::Units< T >::showAll ( std::ostream &  out = std::cout)

showAll().

Definition at line 1252 of file units.h.

1253 {
1254  out << "Unit description:" << std::endl;
1255  out << "name: " << name << std::endl;
1256  out << "symbol: " << symbol << std::endl;
1257  out << "SI decomposition: " << power << std::endl;
1258  out << "SI converter: " << toSI << std::endl << std::endl;
1259 }
Converter< T > toSI
A Converter for the coefficient.
Definition: units.h:583
InSI power
An InSI for the power vector.
Definition: units.h:585
std::string symbol
Strings for the symbol.
Definition: units.h:579
std::string name
Strings for the name.
Definition: units.h:581
template<typename T = double>
void Antioch::Units< T >::substract ( const Units< T > &  rhs)
inline
template<typename T = double>
void Antioch::Units< T >::substract ( Units< T > *  rhs)
inline

Alternative call to Units& operator-=(const Units &)

Definition at line 259 of file units.h.

259 {*this -= *rhs;}
template<typename T = double>
void Antioch::Units< T >::substract ( const std::string &  unit)
inline

Alternative call to Units& operator-=(const Units &)

Same remarks than void equalize(std::string).

Definition at line 264 of file units.h.

264 {*this -= Units<T>(unit);}
template<typename T >
void Antioch::Units< T >::symbol_to_the_power ( int  r,
const int &  key 
)
private

Rewrite the symbol to put it at a given power.

This method scans the symbol, parse the units, and changes the power by the power multiplied or divided by the given integer. It uses the method get_integer_power(int,int,int) to ensure integer power.

Definition at line 1172 of file units.h.

1173 {
1174  if(n_dimension_of_units() == 0)return;
1175  std::string curUnit("");
1176  std::string tmpSymbol = contracted_symbol();
1177  for(unsigned int i = 0; i < tmpSymbol.size(); i++)
1178  {
1179  if(tmpSymbol[i] != '.' && tmpSymbol[i] != '/')curUnit += tmpSymbol[i];
1180  if(tmpSymbol[i] == '.' || tmpSymbol[i] == '/' || i == tmpSymbol.size() - 1 )
1181  {
1182  int nc(0);
1183  std::ostringstream po;
1184  int resultPower = get_integer_power(parse_power(curUnit,nc),r,key); //power
1185  if(resultPower == 0)
1186  {
1187  symbol = "failed";
1188  return;
1189  }
1190  po << resultPower;
1191  std::string postr = po.str();
1192  if(resultPower == 1)postr = "";
1193  if(nc != 0)
1194  {
1195  (i != tmpSymbol.size() - 1)?tmpSymbol.replace(i - nc,nc,postr):
1196  tmpSymbol.replace(i - nc + 1,nc,postr); //resulting unit
1197  }else
1198  {
1199  (i != tmpSymbol.size() - 1)?tmpSymbol.insert(i,postr):tmpSymbol.insert(i + 1,postr);
1200  }
1201  curUnit.clear();
1202  i += postr.size();
1203  }
1204  }
1205 
1206  symbol = tmpSymbol;
1207 }
int n_dimension_of_units() const
Number of dimensions of the unit in the SI basis.
Definition: units.h:1108
int get_integer_power(int unit, int r, const int &key)
Multiply or divide a power.
Definition: units.h:1210
const std::string contracted_symbol(const std::string &input="") const
Contract the symbol when possible.
Definition: units.h:387
int parse_power(std::string unit, int &nc) const
brief parser for std::string of type "some_unit power_associated".
Definition: units.h:862
std::string symbol
Strings for the symbol.
Definition: units.h:579
template<typename T >
T Antioch::Units< T >::translator_to_some_unit ( const Units< T > &  target) const

Calculates the translator to any given unit, see factor_to_some_unit(const Units&) for the equations.

Definition at line 774 of file units.h.

References antioch_unit_error, Antioch::Units< T >::get_SI_factor(), and Antioch::Units< T >::get_SI_translator().

775 {
776  if(is_homogeneous(target))
777  {
778  return ((this->get_SI_translator() - target.get_SI_translator())/target.get_SI_factor());
779  }
780  else
781  {
782  antioch_unit_error("Units are not homogeneous.");
783  return -1.;
784  }
785 }
#define antioch_unit_error(description)
T get_SI_translator() const
Translationnal coefficient getter.
Definition: units.h:336
bool is_homogeneous(const Units< T > &rhs) const
Homogenity testing with another Units.
Definition: units.h:892
template<typename T = double>
T Antioch::Units< T >::translator_to_some_unit ( const std::string &  target) const
inline

Calculates the translator to any given unit, uses translator_to_some_unit(const Units&).

Definition at line 325 of file units.h.

References Antioch::Units< T >::translator_to_some_unit().

Referenced by Antioch::Units< T >::translator_to_some_unit().

325 {return translator_to_some_unit(Units<T>(target));}
T translator_to_some_unit(const Units< T > &target) const
Calculates the translator to any given unit, see factor_to_some_unit(const Units&) for the equations...
Definition: units.h:774

Friends And Related Function Documentation

template<typename T = double>
std::ostream& operator<< ( std::ostream &  os,
const Units< T > &  unit 
)
friend

Formatted print.

Allows you to do std::cout << object << std::endl;

Definition at line 138 of file units.h.

140  {
141  unit.print(os);
142  return os;
143  }

Member Data Documentation

template<typename T = double>
std::string Antioch::Units< T >::name
private

Strings for the name.

Definition at line 581 of file units.h.

Referenced by Antioch::Units< T >::clear(), Antioch::Units< T >::get_name(), and Antioch::Units< T >::set_name().

template<typename T = double>
InSI Antioch::Units< T >::power
private
template<typename T = double>
std::string Antioch::Units< T >::symbol
private
template<typename T = double>
Converter<T> Antioch::Units< T >::toSI
private

The documentation for this class was generated from the following file:

Generated on Thu Jul 7 2016 11:09:50 for antioch-0.4.0 by  doxygen 1.8.8