Flow123d  release_3.0.0-968-gc87a28e79
unit_converter.hh
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.hh
15  * @brief
16  */
17 
18 #ifndef UNIT_CONVERTER_HH_
19 #define UNIT_CONVERTER_HH_
20 
21 
22 #include <map> // for map, map<>::value_compare
23 #include <string> // for string
24 #include <vector> // for vector
25 
26 #include "tools/unit_si.hh" // for UnitSI
27 #include "input/input_exception.hh" // for DECLARE_INPUT_EXCEPTION, Exception
28 #include "system/asserts.hh" // for Assert, ASSERT
29 #include "system/exceptions.hh" // for operator<<, ExcStream, EI, TYPED...
30 
31 // Declaration of exceptions
32 TYPEDEF_ERR_INFO(EI_UnitDefinition, std::string);
33 TYPEDEF_ERR_INFO(EI_ExpectedUnit, std::string);
34 TYPEDEF_ERR_INFO(EI_UnitError, std::string);
35 DECLARE_INPUT_EXCEPTION(ExcInvalidUnit,
36  << "Invalid definition of unit: " << EI_UnitDefinition::qval << "\n" << EI_UnitError::val << ".\n");
37 DECLARE_INPUT_EXCEPTION(ExcNoncorrespondingUnit,
38  << "Non-corresponding definition of unit: " << EI_UnitDefinition::qval << "\nExpected: unit with base format "
39  << EI_ExpectedUnit::qval << ".\n");
40 
41 
42 /// Store structure given by parser
43 struct Factor {
44  /// Constructor
45  Factor() : exponent_(1), basic_(true) {}
46  Factor(std::string factor, int exponent, bool basic = true) : factor_(factor), exponent_(exponent), basic_(basic) {}
47 
48  std::string factor_; //!< string represantation of unit or user defined constant
49  int exponent_; //!< exponent
50  bool basic_; //!< unit is basic (strict defined in application) / derived (defined by user as formula)
51 };
52 struct Formula {
53  /// Constructor
54  Formula() : coef_(1.0) {}
55 
56  double coef_; //!< multiplicative coeficient
57  std::vector<struct Factor> factors_; //!< factors of formula
58 };
60 
61 
62 
63 /**
64  * @brief Helper class. Defines basic factors of SI, non-SI and derived units.
65  *
66  * Class is instantiated as static object of UnitConverter class and provides check
67  * of user defined units.
68  */
69 class BasicFactors {
70 public:
71  struct DerivedUnit {
72  double coef_; //!< multiplicative coeficient
73  UnitSI unit_; //!< derived SI unit
74  };
75 
77 
78  /// Define all base and derived units given by their symbol.
80 
81  /// Constructor
82  BasicFactors();
83 
84 };
85 
86 
88 public:
89  /// Define all base and derived units given by their symbol.
91 
92  /// Constructor
93  UnitConverter();
94 
95  /// Convert string to coeficient and UnitSI representation, return coeficient
96  double convert(std::string actual_unit);
97 
98  /// Return @p unit_si_
99  inline UnitSI unit_si() const {
100  ASSERT(unit_si_.is_def()).error("UnitSI is not defined, first call convert method.");
101  return unit_si_;
102  }
103 
104 protected:
105  /**
106  * @brief Parse and check unit defined in string format.
107  *
108  * Return data in format \p UnitData
109  */
110  UnitData read_unit(std::string s);
111 
112  /// Calculates UnitSi and coeficient of Factor, recursively calls this method for user defined formula
113  void add_converted_unit(Factor factor, UnitData &unit_data, UnitSI &unit_si, double &coef);
114 
115  /**
116  * Coeficient of unit.
117  *
118  * Coeficient is used if unit is not in basic format. Example: if the unit is specified
119  * in minutes, coeficient has value 60.
120  */
121  double coef_;
122 
123  /**
124  * Basic format of converted SI unit
125  */
127 
128 };
129 
130 
131 #endif /* UNIT_CONVERTER_HH_ */
Formula::coef_
double coef_
multiplicative coeficient
Definition: unit_converter.hh:56
UnitConverter::read_unit
UnitData read_unit(std::string s)
Parse and check unit defined in string format.
Definition: unit_converter.cc:95
UnitConverter::unit_si
UnitSI unit_si() const
Return unit_si_.
Definition: unit_converter.hh:99
Factor::Factor
Factor(std::string factor, int exponent, bool basic=true)
Definition: unit_converter.hh:46
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:135
BasicFactors::DerivedUnit
Definition: unit_converter.hh:71
BasicFactors
Helper class. Defines basic factors of SI, non-SI and derived units.
Definition: unit_converter.hh:69
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:346
asserts.hh
Definitions of ASSERTS.
Factor::Factor
Factor()
Constructor.
Definition: unit_converter.hh:45
Factor::factor_
std::string factor_
string represantation of unit or user defined constant
Definition: unit_converter.hh:48
std::vector< struct Factor >
UnitData
std::map< std::string, struct Formula > UnitData
Definition: unit_converter.hh:59
BasicFactors::DerivedUnit::unit_
UnitSI unit_
derived SI unit
Definition: unit_converter.hh:73
exceptions.hh
BasicFactors::DerivedUnit::coef_
double coef_
multiplicative coeficient
Definition: unit_converter.hh:72
UnitConverter
Definition: unit_converter.hh:87
Formula::factors_
std::vector< struct Factor > factors_
factors of formula
Definition: unit_converter.hh:57
UnitConverter::UnitConverter
UnitConverter()
Constructor.
Definition: unit_converter.cc:88
DECLARE_INPUT_EXCEPTION
DECLARE_INPUT_EXCEPTION(ExcInvalidUnit,<< "Invalid definition of unit: "<< EI_UnitDefinition::qval<< "\n"<< EI_UnitError::val<< ".\n")
UnitSI
Class for representation SI units of Fields.
Definition: unit_si.hh:40
UnitConverter::coef_
double coef_
Definition: unit_converter.hh:121
BasicFactors::UnitsMap
std::map< std::string, struct DerivedUnit > UnitsMap
Definition: unit_converter.hh:76
Formula
Definition: unit_converter.hh:52
UnitConverter::convert
double convert(std::string actual_unit)
Convert string to coeficient and UnitSI representation, return coeficient.
Definition: unit_converter.cc:121
input_exception.hh
std::map< std::string, struct Formula >
Factor::basic_
bool basic_
unit is basic (strict defined in application) / derived (defined by user as formula)
Definition: unit_converter.hh:50
BasicFactors::BasicFactors
BasicFactors()
Constructor.
Definition: unit_converter.cc:26
unit_si.hh
UnitConverter::basic_factors
static const BasicFactors basic_factors
Define all base and derived units given by their symbol.
Definition: unit_converter.hh:90
UnitConverter::unit_si_
UnitSI unit_si_
Definition: unit_converter.hh:126
TYPEDEF_ERR_INFO
TYPEDEF_ERR_INFO(EI_UnitDefinition, std::string)
Formula::Formula
Formula()
Constructor.
Definition: unit_converter.hh:54
Factor
Store structure given by parser.
Definition: unit_converter.hh:43
BasicFactors::units_map_
UnitsMap units_map_
Define all base and derived units given by their symbol.
Definition: unit_converter.hh:79
UnitSI::is_def
bool is_def() const
Return true if the unit is defined.
Definition: unit_si.cc:201
Factor::exponent_
int exponent_
exponent
Definition: unit_converter.hh:49