Flow123d  JS_before_hm-978-gf0793cd
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.
79  UnitsMap units_map_;
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_ */
UnitSI unit_
derived SI unit
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.
bool basic_
unit is basic (strict defined in application) / derived (defined by user as formula) ...
Definitions of ASSERTS.
std::vector< struct Factor > factors_
factors of formula
UnitSI unit_si() const
Return unit_si_.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
std::map< std::string, struct DerivedUnit > UnitsMap
Yes & convert(fmt::ULongLong)
TYPEDEF_ERR_INFO(EI_UnitDefinition, std::string)
Formula()
Constructor.
Factor(std::string factor, int exponent, bool basic=true)
int exponent_
exponent
std::string factor_
string represantation of unit or user defined constant
Store structure given by parser.
DECLARE_INPUT_EXCEPTION(ExcInvalidUnit,<< "Invalid definition of unit: "<< EI_UnitDefinition::qval<< "\n"<< EI_UnitError::val<< ".\n")
std::map< std::string, struct Formula > UnitData
Class for representation SI units of Fields.
Definition: unit_si.hh:40
Factor()
Constructor.
static const BasicFactors basic_factors
Define all base and derived units given by their symbol.
double coef_
multiplicative coeficient
double coef_
multiplicative coeficient