Flow123d  JS_before_hm-1576-g4d0b70e
unit_converter.cc
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file unit_converter.cc
15  * @brief
16  */
17 
18 
20 
21 
22 /*******************************************************************
23  * implementation of BasicFactors
24  */
25 
27  UnitsMap base_units_map = {
28  { "*m", { 1, UnitSI().m() } },
29  { "*g", { 0.001, UnitSI().kg() } },
30  { "*s", { 1, UnitSI().s() } },
31  { "*A", { 1, UnitSI().A() } },
32  { "*K", { 1, UnitSI().K() } },
33  { "*cd", { 1, UnitSI().cd() } },
34  { "*mol",{ 1, UnitSI().mol() } },
35 
36  { "*N", { 1, UnitSI().m().kg().s(-2) } },
37  { "*J", { 1, UnitSI().m(2).kg().s(-2) } },
38  { "*W", { 1, UnitSI().m(2).kg().s(-3) } },
39  { "*Pa", { 1, UnitSI().m(-1).kg().s(-2) } },
40  { "*C", { 1, UnitSI().A(1).s(1) } },
41  { "*D", { 9.869233E-13, UnitSI().m(2) } }, // Darcy
42  { "*l", { 1e-3, UnitSI().m(3) } }, // litr
43 
44  //{ "cm", { 0.01, UnitSI().m() } },
45  //{ "dm", { 0.1, UnitSI().m() } },
46  { "t", { 1000, UnitSI().kg() } },
47  { "min", { 60, UnitSI().s() } },
48  { "h", { 3600, UnitSI().s() } },
49  { "d", { 24*3600, UnitSI().s() } },
50  { "y", { 365.2425*24*3600, UnitSI().s() } },
51  //{ "hPa", { 100, UnitSI().m(-1).kg().s(-2) } },
52 
53  { "rad", { 1, UnitSI().m(0) } }
54  };
55 
56  // map of prefixes and multiplicative constants
57  std::map<std::string, double> prefix_map = {
58  { "a", 1e-18 },
59  { "f", 1e-15 },
60  { "p", 1e-12 },
61  { "n", 1e-9 },
62  { "u", 1e-6 },
63  { "m", 1e-3 },
64  { "c", 1e-2 },
65  { "d", 1e-1 },
66  { "", 1 },
67  { "h", 1e+2 }, // deka is missing as it is a two letter prefix
68  { "k", 1e+3 },
69  { "M", 1e+6 },
70  { "G", 1e+9 },
71  { "T", 1e+12 },
72  { "P", 1e+15 },
73  { "E", 1e+18 }
74 
75  };
76 
77  // add derived units
79  for (it=base_units_map.begin(); it!=base_units_map.end(); ++it) {
80  if (it->first.at(0)=='*') {
81  std::string shortcut = it->first.substr(1);
82  double coef = it->second.coef_;
83 
84  for (std::map<std::string, double>::iterator prefix_it=prefix_map.begin(); prefix_it!=prefix_map.end(); ++prefix_it) {
85  std::string key = prefix_it->first + shortcut;
86  units_map_.insert(std::pair<std::string, DerivedUnit>( key, { coef*prefix_it->second, it->second.unit_ } ));
87  }
88  } else {
89  units_map_.insert( std::pair<std::string, DerivedUnit>( it->first, it->second ) );
90  }
91  }
92 }
93 
94 
95 /*******************************************************************
96  * implementation of UnitConverter
97  */
98 
100 : coef_(1.0) {}
101 
102 
104 
105 
107 {
108  typedef spirit_namespace::position_iterator< std::string::iterator > PosnIterT;
109 
110  std::string::iterator begin = s.begin();
111  std::string::iterator end = s.end();
112 
113  const PosnIterT posn_begin( begin, end );
114  const PosnIterT posn_end( end, end );
115 
117 
118  try {
119  spirit_namespace::parse( begin, end,
121  spirit_namespace::space_p );
122  semantic_actions.check_unit_data();
123  } catch (ExcInvalidUnit &e) {
124  e << EI_UnitDefinition(s);
125  throw;
126  }
127 
128  return semantic_actions.unit_data();
129 }
130 
131 
132 double UnitConverter::convert(std::string actual_unit) {
133  unit_si_.reset();
134  coef_ = 1.0;
135  UnitData unit_data = read_unit(actual_unit);
136 
137  Formula &formula = unit_data.find("")->second;
138  for( std::vector<struct Factor>::iterator it = formula.factors_.begin(); it !=formula.factors_.end(); ++it ) {
139  add_converted_unit(*it, unit_data, unit_si_, coef_);
140  }
141 
142  return coef_;
143 }
144 
145 
146 void UnitConverter::add_converted_unit(Factor factor, UnitData &unit_data, UnitSI &unit_si, double &coef) {
147  if (factor.basic_) {
149  ASSERT_DBG(it != UnitConverter::basic_factors.units_map_.end())(factor.factor_).error("Undefined unit.");
150  coef *= pow(it->second.coef_, factor.exponent_);
151  unit_si.multiply(it->second.unit_, factor.exponent_);
152  } else {
154  ASSERT_DBG(it != unit_data.end())(factor.factor_).error("Undefined unit.");
155  coef *= pow(it->second.coef_, factor.exponent_);
156  for( std::vector<struct Factor>::iterator in_it = it->second.factors_.begin(); in_it !=it->second.factors_.end(); ++in_it ) {
157  Factor new_factor = Factor(in_it->factor_, in_it->exponent_*factor.exponent_, in_it->basic_ );
158  add_converted_unit(new_factor, unit_data, unit_si, coef);
159  }
160  }
161 }
162 
Helper class. Defines basic factors of SI, non-SI and derived units.
UnitsMap units_map_
Define all base and derived units given by their symbol.
UnitData read_unit(std::string s)
Parse and check unit defined in string format.
bool basic_
unit is basic (strict defined in application) / derived (defined by user as formula) ...
std::vector< struct Factor > factors_
factors of formula
UnitSI & K(int exp=1)
Definition: unit_si.cc:88
UnitSI & A(int exp=1)
Definition: unit_si.cc:82
UnitSI unit_si() const
Return unit_si_.
void check_unit_data()
Check unit_data_ object.
Definition of unit grammar.
UnitConverter()
Constructor.
void add_converted_unit(Factor factor, UnitData &unit_data, UnitSI &unit_si, double &coef)
Calculates UnitSi and coeficient of Factor, recursively calls this method for user defined formula...
int exponent_
exponent
Class manages parsing of user defined field unit.
UnitSI & cd(int exp=1)
Definition: unit_si.cc:100
UnitSI & s(int exp=1)
Definition: unit_si.cc:76
UnitSI & kg(int exp=1)
Definition: unit_si.cc:70
double convert(std::string actual_unit)
Convert string to coeficient and UnitSI representation, return coeficient.
void multiply(const UnitSI &other, int exp=1)
Multiply with power of given unit.
Definition: unit_si.cc:205
std::string factor_
string represantation of unit or user defined constant
Store structure given by parser.
void reset()
Reset UnitSI object (set vector of exponents to zeros and set undef flag)
Definition: unit_si.cc:212
#define ASSERT_DBG(expr)
UnitSI & mol(int exp=1)
Definition: unit_si.cc:94
BasicFactors()
Constructor.
UnitSI & m(int exp=1)
Methods set values of exponents for SI units with similar name.
Definition: unit_si.cc:64
Class for representation SI units of Fields.
Definition: unit_si.hh:40
static const BasicFactors basic_factors
Define all base and derived units given by their symbol.