Flow123d  JS_before_hm-2039-g19af1f327
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 #include "input/type_record.hh"
21 
22 
23 /*******************************************************************
24  * implementation of BasicFactors
25  */
26 
28  UnitsMap base_units_map = {
29  { "*m", { 1, UnitSI().m() } },
30  { "*g", { 0.001, UnitSI().kg() } },
31  { "*s", { 1, UnitSI().s() } },
32  { "*A", { 1, UnitSI().A() } },
33  { "*K", { 1, UnitSI().K() } },
34  { "*cd", { 1, UnitSI().cd() } },
35  { "*mol",{ 1, UnitSI().mol() } },
36 
37  { "*N", { 1, UnitSI().m().kg().s(-2) } },
38  { "*J", { 1, UnitSI().m(2).kg().s(-2) } },
39  { "*W", { 1, UnitSI().m(2).kg().s(-3) } },
40  { "*Pa", { 1, UnitSI().m(-1).kg().s(-2) } },
41  { "*C", { 1, UnitSI().A(1).s(1) } },
42  { "*D", { 9.869233E-13, UnitSI().m(2) } }, // Darcy
43  { "*l", { 1e-3, UnitSI().m(3) } }, // litr
44 
45  //{ "cm", { 0.01, UnitSI().m() } },
46  //{ "dm", { 0.1, UnitSI().m() } },
47  { "t", { 1000, UnitSI().kg() } },
48  { "min", { 60, UnitSI().s() } },
49  { "h", { 3600, UnitSI().s() } },
50  { "d", { 24*3600, UnitSI().s() } },
51  { "y", { 365.2425*24*3600, UnitSI().s() } },
52  //{ "hPa", { 100, UnitSI().m(-1).kg().s(-2) } },
53 
54  { "rad", { 1, UnitSI().m(0) } }
55  };
56 
57  // map of prefixes and multiplicative constants
58  std::map<std::string, double> prefix_map = {
59  { "a", 1e-18 },
60  { "f", 1e-15 },
61  { "p", 1e-12 },
62  { "n", 1e-9 },
63  { "u", 1e-6 },
64  { "m", 1e-3 },
65  { "c", 1e-2 },
66  { "d", 1e-1 },
67  { "", 1 },
68  { "h", 1e+2 }, // deka is missing as it is a two letter prefix
69  { "k", 1e+3 },
70  { "M", 1e+6 },
71  { "G", 1e+9 },
72  { "T", 1e+12 },
73  { "P", 1e+15 },
74  { "E", 1e+18 }
75 
76  };
77 
78  // add derived units
80  for (it=base_units_map.begin(); it!=base_units_map.end(); ++it) {
81  if (it->first.at(0)=='*') {
82  std::string shortcut = it->first.substr(1);
83  double coef = it->second.coef_;
84 
85  for (std::map<std::string, double>::iterator prefix_it=prefix_map.begin(); prefix_it!=prefix_map.end(); ++prefix_it) {
86  std::string key = prefix_it->first + shortcut;
87  units_map_.insert(std::pair<std::string, DerivedUnit>( key, { coef*prefix_it->second, it->second.unit_ } ));
88  }
89  } else {
90  units_map_.insert( std::pair<std::string, DerivedUnit>( it->first, it->second ) );
91  }
92  }
93 }
94 
95 
96 /*******************************************************************
97  * implementation of UnitConverter
98  */
99 
101  return Input::Type::Record("Unit",
102  "Specify the unit of an input value. "
103  "Evaluation of the unit formula results into a coeficient and a "
104  "unit in terms of powers of base SI units. The unit must match the"
105  "expected SI unit of the value, while the value provided on the input "
106  "is multiplied by the coefficient before further processing. "
107  "The unit formula have a form:\n"
108  "```\n"
109  "<UnitExpr>;<Variable>=<Number>*<UnitExpr>;...,\n"
110  "```\n"
111  "where ```<Variable>``` is a variable name and ```<UnitExpr>``` is a units expression "
112  "which consists of products and divisions of terms.\n\n"
113  "A term has a form: "
114  "```<Base>^<N>```, where ```<N>``` is an integer exponent and ```<Base>``` "
115  "is either a base SI unit, a derived unit, or a variable defined in the same unit formula. "
116  "Example, unit for the pressure head:\n\n"
117  "```MPa/rho/g_; rho = 990*kg*m^-3; g_ = 9.8*m*s^-2```"
118  )
119  .allow_auto_conversion("unit_formula")
121  "Definition of unit." )
122  .close();
123 }
124 
125 
127 : coef_(1.0) {}
128 
129 
131 
132 
134 {
135  typedef spirit_namespace::position_iterator< std::string::iterator > PosnIterT;
136 
137  std::string::iterator begin = s.begin();
138  std::string::iterator end = s.end();
139 
140  const PosnIterT posn_begin( begin, end );
141  const PosnIterT posn_end( end, end );
142 
144 
145  try {
146  spirit_namespace::parse( begin, end,
148  spirit_namespace::space_p );
149  semantic_actions.check_unit_data();
150  } catch (ExcInvalidUnit &e) {
151  e << EI_UnitDefinition(s);
152  throw;
153  }
154 
155  return semantic_actions.unit_data();
156 }
157 
158 
159 double UnitConverter::convert(std::string actual_unit) {
160  unit_si_.reset();
161  coef_ = 1.0;
162  UnitData unit_data = read_unit(actual_unit);
163 
164  Formula &formula = unit_data.find("")->second;
165  for( std::vector<struct Factor>::iterator it = formula.factors_.begin(); it !=formula.factors_.end(); ++it ) {
166  add_converted_unit(*it, unit_data, unit_si_, coef_);
167  }
168 
169  return coef_;
170 }
171 
172 
173 void UnitConverter::add_converted_unit(Factor factor, UnitData &unit_data, UnitSI &unit_si, double &coef) {
174  if (factor.basic_) {
176  ASSERT_DBG(it != UnitConverter::basic_factors.units_map_.end())(factor.factor_).error("Undefined unit.");
177  coef *= pow(it->second.coef_, factor.exponent_);
178  unit_si.multiply(it->second.unit_, factor.exponent_);
179  } else {
181  ASSERT_DBG(it != unit_data.end())(factor.factor_).error("Undefined unit.");
182  coef *= pow(it->second.coef_, factor.exponent_);
183  for( std::vector<struct Factor>::iterator in_it = it->second.factors_.begin(); in_it !=it->second.factors_.end(); ++in_it ) {
184  Factor new_factor = Factor(in_it->factor_, in_it->exponent_*factor.exponent_, in_it->basic_ );
185  add_converted_unit(new_factor, unit_data, unit_si, coef);
186  }
187  }
188 }
189 
UnitSI::cd
UnitSI & cd(int exp=1)
Definition: unit_si.cc:100
UnitConverter::read_unit
UnitData read_unit(std::string s)
Parse and check unit defined in string format.
Definition: unit_converter.cc:133
UnitSI::kg
UnitSI & kg(int exp=1)
Definition: unit_si.cc:70
UnitConverter::unit_si
UnitSI unit_si() const
Return unit_si_.
Definition: unit_converter.hh:115
units_converter::Semantic_actions::unit_data
UnitData unit_data() const
Definition: unit_converter_template.hh:185
UnitConverter::add_converted_unit
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.
Definition: unit_converter.cc:173
BasicFactors
Helper class. Defines basic factors of SI, non-SI and derived units.
Definition: unit_converter.hh:75
UnitSI::multiply
void multiply(const UnitSI &other, int exp=1)
Multiply with power of given unit.
Definition: unit_si.cc:205
UnitSI::A
UnitSI & A(int exp=1)
Definition: unit_si.cc:82
ASSERT_DBG
#define ASSERT_DBG(expr)
Definition: include_fadbad.hh:28
Factor::factor_
std::string factor_
string represantation of unit or user defined constant
Definition: unit_converter.hh:54
std::vector
Definition: doxy_dummy_defs.hh:7
units_converter::UnitSIGrammer
Definition of unit grammar.
Definition: unit_converter_template.hh:210
UnitSI::mol
UnitSI & mol(int exp=1)
Definition: unit_si.cc:94
UnitSI::m
UnitSI & m(int exp=1)
Methods set values of exponents for SI units with similar name.
Definition: unit_si.cc:64
type_record.hh
Formula::factors_
std::vector< struct Factor > factors_
factors of formula
Definition: unit_converter.hh:63
UnitConverter::UnitConverter
UnitConverter()
Constructor.
Definition: unit_converter.cc:126
Input::Type::Record::allow_auto_conversion
virtual Record & allow_auto_conversion(const string &from_key)
Allows shorter input of the Record providing only value of the from_key given as the parameter.
Definition: type_record.cc:133
Input::Type::Default::obligatory
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
UnitSI
Class for representation SI units of Fields.
Definition: unit_si.hh:40
UnitConverter::coef_
double coef_
Definition: unit_converter.hh:137
Formula
Definition: unit_converter.hh:58
Input::Type::Record::declare_key
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:503
UnitConverter::convert
double convert(std::string actual_unit)
Definition: unit_converter.cc:159
std::map< std::string, struct DerivedUnit >
UnitConverter::get_input_type
static const Input::Type::Record & get_input_type()
Definition: unit_converter.cc:100
Input::Type::Record::close
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:304
Factor::basic_
bool basic_
unit is basic (strict defined in application) / derived (defined by user as formula)
Definition: unit_converter.hh:56
units_converter::Semantic_actions::check_unit_data
void check_unit_data()
Check unit_data_ object.
Definition: unit_converter_template.hh:155
Input::Type
Definition: balance.hh:41
UnitSI::reset
void reset()
Reset UnitSI object (set vector of exponents to zeros and set undef flag)
Definition: unit_si.cc:212
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
BasicFactors::BasicFactors
BasicFactors()
Constructor.
Definition: unit_converter.cc:27
Input::Type::String
Class for declaration of the input data that are in string format.
Definition: type_base.hh:582
UnitSI::s
UnitSI & s(int exp=1)
Definition: unit_si.cc:76
UnitConverter::basic_factors
static const BasicFactors basic_factors
Define all base and derived units given by their symbol.
Definition: unit_converter.hh:96
UnitSI::K
UnitSI & K(int exp=1)
Definition: unit_si.cc:88
units_converter::Semantic_actions
Class manages parsing of user defined field unit.
Definition: unit_converter_template.hh:73
UnitConverter::unit_si_
UnitSI unit_si_
Definition: unit_converter.hh:142
Factor
Store structure given by parser.
Definition: unit_converter.hh:49
BasicFactors::units_map_
UnitsMap units_map_
Define all base and derived units given by their symbol.
Definition: unit_converter.hh:85
unit_converter_template.hh
Factor::exponent_
int exponent_
exponent
Definition: unit_converter.hh:55